-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathPWM.swift
More file actions
722 lines (613 loc) · 29.1 KB
/
Copy pathPWM.swift
File metadata and controls
722 lines (613 loc) · 29.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
/*
SwiftyGPIO
Copyright (c) 2016 Umberto Raimondi
Licensed under the MIT license, as follows:
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.)
*/
#if os(Linux)
import Glibc
#else
import Darwin.C
#endif
import Foundation
extension SwiftyGPIO {
public static func hardwarePWMs(for board: SupportedBoard) -> [Int:[GPIOName:PWMOutput]]? {
switch board {
case .RaspberryPiRev1:
fallthrough
case .RaspberryPiRev2:
fallthrough
case .RaspberryPiPlusZero:
return PWMRPI1
case .RaspberryPi2:
fallthrough
case .RaspberryPi3:
return PWMRPI23
default:
return nil
}
}
}
// MARK: - PWM Presets
extension SwiftyGPIO {
// RaspberryPis ARMv6 (all 1, Zero, Zero W) PWMs, only accessible ones, divided in channels (can use only one for each channel)
static let PWMRPI1: [Int:[GPIOName:PWMOutput]] = [
0: [.P12: RaspberryPWM(gpioId: 12, alt: 0, channel:0, phy: RaspberryPHY.phyFor(baseAddr: 0x20000000)),
.P18: RaspberryPWM(gpioId: 18, alt: 5, channel:0, phy: RaspberryPHY.phyFor(baseAddr: 0x20000000))],
1: [.P13: RaspberryPWM(gpioId: 13, alt: 0, channel:1, phy: RaspberryPHY.phyFor(baseAddr: 0x20000000)),
.P19: RaspberryPWM(gpioId: 19, alt: 5, channel:1, phy: RaspberryPHY.phyFor(baseAddr: 0x20000000))]
]
// RaspberryPis ARMv7 (2-3) PWMs, only accessible ones, divided in channels (can use only one for each channel)
static let PWMRPI23: [Int:[GPIOName:PWMOutput]] = [
0: [.P12: RaspberryPWM(gpioId: 12, alt: 0, channel:0, phy: RaspberryPHY.phyFor(baseAddr: 0x3F000000)),
.P18: RaspberryPWM(gpioId: 18, alt: 5, channel:0, phy: RaspberryPHY.phyFor(baseAddr: 0x3F000000))],
1: [.P13: RaspberryPWM(gpioId: 13, alt: 0, channel:1, phy: RaspberryPHY.phyFor(baseAddr: 0x3F000000)),
.P19: RaspberryPWM(gpioId: 19, alt: 5, channel:1, phy: RaspberryPHY.phyFor(baseAddr: 0x3F000000))]
]
}
// MARK: PWM
public protocol PWMOutput {
func initPWM()
func startPWM(period ns: Int, duty percent: Float)
func stopPWM()
func initPWMPattern(bytes count: Int, at frequency: Int, with resetDelay: Int, dutyzero: Int, dutyone: Int)
func sendDataWithPattern(values: [UInt8])
func waitOnSendData()
func cleanupPattern()
}
final public class RaspberryPHY {
private static var existingPhy: RaspberryPHY? = nil
static func phyFor(baseAddr: Int) -> RaspberryPHY {
// If the physical base has changed, throw away the existing cache.
// This generally shouldn't happen, as the base address is fixed.
// It is reasonable to create a new instance if there is a mismatch, though.
if let phy = existingPhy, phy.BCM2708_PERI_BASE == baseAddr {
return phy
}
let phy = RaspberryPHY(baseAddr: baseAddr)
existingPhy = phy
return phy
}
let BCM2708_PERI_BASE: Int
let GPIO_BASE: Int // GPIO Register
let PWM_BASE: Int // PWM Register
let CLOCK_BASE: Int // Clock Manager Register
let BCM2708_PHY_BASE: Int = 0x7e000000
let PWM_PHY_BASE: Int
var gpioBasePointer: UnsafeMutablePointer<UInt>!
var pwmBasePointer: UnsafeMutablePointer<UInt>!
var clockBasePointer: UnsafeMutablePointer<UInt>!
var dmaBasePointers: [UnsafeMutablePointer<UInt>]! = []
public private(set) var isInitialized: Bool = false
public private(set) var isReadyForPwm: Bool = false
private init(baseAddr: Int) {
BCM2708_PERI_BASE = baseAddr
GPIO_BASE = BCM2708_PERI_BASE + 0x200000 // GPIO Register
PWM_BASE = BCM2708_PERI_BASE + 0x20C000 // PWM Register
CLOCK_BASE = BCM2708_PERI_BASE + 0x101000 // Clock Manager Register
PWM_PHY_BASE = BCM2708_PHY_BASE + 0x20C000 // PWM controller physical address
}
func initPhy() {
guard !isInitialized else { return }
var mem_fd: Int32 = 0
//The only mem device that support PWM is /dev/mem
mem_fd=open("/dev/mem", O_RDWR | O_SYNC)
guard mem_fd > 0 else {
fatalError("Can't open /dev/mem , use sudo!")
}
gpioBasePointer = memmap(from: mem_fd, at: GPIO_BASE)
pwmBasePointer = memmap(from: mem_fd, at: PWM_BASE)
clockBasePointer = memmap(from: mem_fd, at: CLOCK_BASE)
let DMAOffsets: [Int] = [0x00007000, 0x00007100, 0x00007200, 0x00007300,
0x00007400, 0x00007500, 0x00007600, 0x00007700,
0x00007800, 0x00007900, 0x00007a00, 0x00007b00,
0x00007c00, 0x00007d00, 0x00007e00, 0x00e05000]
for dma_addr in DMAOffsets.map({ BCM2708_PERI_BASE + $0 }) {
let pageOffset = dma_addr % PAGE_SIZE
let adjusted_dma_addr = dma_addr - pageOffset
let dma_map = UnsafeMutableRawPointer(memmap(from: mem_fd, at: adjusted_dma_addr))
let dmaBasePointer = (dma_map + pageOffset).assumingMemoryBound(to: UInt.self)
dmaBasePointers.append(dmaBasePointer)
}
close(mem_fd)
}
func setPwmClock() {
guard !isReadyForPwm else { return }
killClock()
let idiv = UInt(2) // 250Mhz base frequency
// Configure the clock and divisor that will be used to generate the signal
clockBasePointer.advanced(by: 41).pointee = CLKM_PASSWD | (idiv << CLKM_DIV_DIVI) //CM CTL DIV register: Set DIVI value
clockBasePointer.advanced(by: 40).pointee = CLKM_PASSWD | CLKM_CTL_ENAB | CLKM_CTL_SRC_PLLD //CM CTL register: Enable clock, MASH 0, source PLLD
pwmBasePointer.pointee = 0 //PWM CTL register: Everything at 0, enable flag included, disables previous PWM
usleep(10)
isReadyForPwm = true
}
func killClock() {
clockBasePointer.advanced(by: 40).pointee = CLKM_PASSWD | CLKM_CTL_KILL //CM CTL register: Set KILL flag
usleep(10)
isReadyForPwm = false
}
/// Maps a block of memory and returns the pointer
internal func memmap(from mem_fd: Int32, at offset: Int) -> UnsafeMutablePointer<UInt> {
let m = mmap(
nil, //Any adddress in our space will do
PAGE_SIZE, //Map length
PROT_READ|PROT_WRITE, // Enable reading & writting to mapped memory
MAP_SHARED, //Shared with other processes
mem_fd, //File to map
off_t(offset) //Offset to GPIO peripheral
)!
if (Int(bitPattern: m) == -1) { //MAP_FAILED not available, but its value is (void*)-1
perror("mmap error")
abort()
}
let pointer = m.assumingMemoryBound(to: UInt.self)
return pointer
}
}
public class RaspberryPWM: PWMOutput {
let gpioId: UInt
let alt: UInt
let channel: Int
let pwmdma: Int
let phy: RaspberryPHY
var dmaBasePointer: UnsafeMutablePointer<UInt>!
var dmaCallbackPointer: UnsafeMutablePointer<DMACallback>! = nil
var pwmRawPointer: UnsafeMutablePointer<UInt>! = nil
var mailbox: MailBox! = nil
var zeroPattern: Int = 0
var onePattern: Int = 0
var symbolBits: Int = 0
var patternFrequency: Int = 0
var patternDelay: Int = 0
var dataLength: Int = 0
public init(gpioId: UInt, alt: UInt, channel: Int, phy: RaspberryPHY, dmanum: Int = 5) {
self.phy = phy
self.gpioId = gpioId
self.alt = alt
self.channel = channel
self.pwmdma = dmanum
}
/// Init PWM on this pin, set alternative function
public func initPWM() {
phy.initPhy()
dmaBasePointer = phy.dmaBasePointers[pwmdma]
// set PWM alternate function for this GPIO
setAlt()
}
/// Start a PWM signal with specific period in ns and duty cycle from 0 to 100.
/// The signal starts, asynchronously(manged by a device external to the CPU), once this method is called and
/// needs to be stopped manually calling `stopPWM()`.
public func startPWM(period ns: Int, duty percent: Float) {
if !phy.isReadyForPwm {
phy.setPwmClock()
}
// @ 250Mhz, one slot in the PWM channel is 4 ns. All valid periods are multiples of that.
// Pick a range that is at least as large as requested that we can represent, and round the
// desired duty cycle to the nearest whole slot.
//
// Double precision is used here to handle very low frequencies in the range of <15Hz.
// Otherwise we lose precision calculating data.
let range = UInt(max((Double(ns) / 4.0).rounded(.awayFromZero), 1))
let data = min(UInt((Double(percent) * Double(range) / 100.0).rounded(.toNearestOrAwayFromZero)), range)
// Configure the parameters for the M/S algorithm, S the number of total slots in RNG1 and M the number of slots with high value in DAT1
let RNG = (channel == 0) ? 4 : 8
let DAT = (channel == 0) ? 5 : 9
phy.pwmBasePointer.advanced(by: RNG).pointee = range //RNG1 register
phy.pwmBasePointer.advanced(by: DAT).pointee = data //DAT1 register
let PWMCTL_MSEN = (channel == 0) ? PWMCTL_MSEN1 : PWMCTL_MSEN2
let PWMCTL_PWEN = (channel == 0) ? PWMCTL_PWEN1 : PWMCTL_PWEN2
let oldCtl = phy.pwmBasePointer.pointee
phy.pwmBasePointer.pointee = oldCtl | PWMCTL_MSEN | PWMCTL_PWEN //PWM CTL register, channel enabled, M/S mode
}
public func stopPWM() {
let PWMCTL_MSEN = (channel == 0) ? PWMCTL_MSEN1 : PWMCTL_MSEN2
let PWMCTL_PWEN = (channel == 0) ? PWMCTL_PWEN1 : PWMCTL_PWEN2
let oldCtl = phy.pwmBasePointer.pointee
phy.pwmBasePointer.pointee = oldCtl & ~PWMCTL_MSEN & ~PWMCTL_PWEN //PWM CTL register, clear just the channel.
}
/// Maps a block of memory and returns the pointer
internal func memmap(from mem_fd: Int32, at offset: Int) -> UnsafeMutablePointer<UInt> {
let m = mmap(
nil, //Any adddress in our space will do
PAGE_SIZE, //Map length
PROT_READ|PROT_WRITE, // Enable reading & writting to mapped memory
MAP_SHARED, //Shared with other processes
mem_fd, //File to map
off_t(offset) //Offset to GPIO peripheral
)!
if (Int(bitPattern: m) == -1) { //MAP_FAILED not available, but its value is (void*)-1
perror("mmap error")
abort()
}
let pointer = m.assumingMemoryBound(to: UInt.self)
return pointer
}
/// Set the alternative function for this GPIO
internal func setAlt() {
let altid = (self.alt<=3) ? self.alt+4 : self.alt==4 ? 3 : 2
let ptr = phy.gpioBasePointer.advanced(by: Int(gpioId/10)) // GPFSELn 0..5
ptr.pointee &= ~(7<<((gpioId%10)*3))
ptr.pointee |= (altid<<((gpioId%10)*3))
}
/// Calculate the DIVI value that will divide the selected base clock frequency to obtain the desired frequency.
///
/// For low frequencies, the DIVI value is calculated again increasing the scale value until an acceptable value
/// for the divisor is found. DIVI should be smaller than half the maximum value (0x1000) to reduce jitter.
/// The scale value (increased by 10 every time the DIVI value is too high) will be used to increase the number
/// of samples generated by the M/S algorithm.
///
/// - Parameter base: base clock that will be used to generate the signal
///
/// - Parameter desired: desired target frequency
///
/// - Returns: divi divisor value, and scale value as a multiple of ten
///
internal func calculateDIVI(base: ClockSource, desired: UInt) -> (divi: UInt, scale: UInt) {
var divi: UInt = base.rawValue/desired
var scale: UInt = 1
while divi > 0x800 {
// Divisor too high (greater then half the limit), would not be generated properly
scale *= 10
divi = base.rawValue/(desired*scale)
}
if divi < 1 {
divi = 1
}
return (divi, scale)
}
/// Calculate the unscaled DIVI value that will divide the selected base clock frequency to obtain the desired frequency.
/// The resulting divisor doesn't take advantage of a scaling factor, for higher DIVI values the resulting signal could
/// have high distortion.
///
/// - Parameter base: base clock that will be used to generate the signal
///
/// - Parameter desired: desired target frequency
///
/// - Returns: divi divisor value
///
internal func calculateUnscaledDIVI(base: ClockSource, desired: UInt) -> UInt {
var divi: UInt = base.rawValue/desired
if divi > 0x1000 {
// Divisor too high (greater then half the limit), would not be generated properly
divi = 0x1000
}
if divi < 1 {
divi = 1
}
return divi
}
}
/// Pattern base PWM
extension RaspberryPWM {
/// Start the DMA feeding the PWM FIFO. This will stream the entire DMA buffer out of both PWM channels.
internal func dma_start(dmaCallback address: UInt) {
dmaBasePointer.pointee = DMACS_RESET
usleep(10)
dmaBasePointer.pointee = DMACS_INT | DMACS_END
dmaBasePointer.advanced(by: 1).pointee = address //CONBLK_AD
dmaBasePointer.advanced(by: 8).pointee = 7 //DEBUG: clear debug error flags
dmaBasePointer.pointee = DMACS_WAIT_OUTSTANDING_WRITES | (15 << DMACS_PANIC_PRIORITY) | (15 << DMACS_PRIORITY) | DMACS_ACTIVE
}
/// Wait for any executing DMA operation to complete before returning.
internal func dma_wait() {
while (dmaBasePointer.pointee & DMACS_ACTIVE > 0) && !(dmaBasePointer.pointee & DMACS_ERROR > 0) {
usleep(10)
}
if (dmaBasePointer.pointee & DMACS_ERROR)>0 {
fatalError("DMA Error: \(dmaBasePointer.advanced(by: 8).pointee)")
}
}
/// Wait for the last signal to be completely generated
public func waitOnSendData() {
dma_wait()
}
/// Stop the PWM and clean up any related structure
public func cleanupPattern() {
dma_wait()
// Stop the PWM
phy.pwmBasePointer.pointee = 0
usleep(10)
// Kill the Clock
phy.killClock()
mailbox.cleanup()
}
/// Initiliazes the PWM signal generator
///
/// - Parameter bytes: length of the data in bytes, fixed
/// - Parameter at: signal frequency
/// - Parameter with: length in us of the section with low signal that will be put at the end of the generated bit stream
/// - Parameter dutyzero: duty cycle of the pattern for zero
/// - Parameter dutyone: duty cycle of the pattern for one
///
public func initPWMPattern(bytes count: Int, at frequency: Int, with resetDelay: Int, dutyzero: Int, dutyone: Int) {
(zeroPattern, onePattern, symbolBits) = getRepresentation(zero: dutyzero, one: dutyone)
guard symbolBits > 0 else {fatalError("Couldn't generate a valid pattern for the provided duty cycle values, try with more spaced values.")}
patternFrequency = frequency
patternDelay = resetDelay
dataLength = count
// Round to the next 32 bit size
let dataSize = (( ((dataLength * 8 * symbolBits) + ((patternDelay * (patternFrequency * symbolBits)) / 1000000)) / 8) & ~0x3) + 4
let size = dataSize + MemoryLayout<DMACallback>.stride
// Round up to page size multiple
let mboxsize = (size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1)
mailbox = MailBox(handle: -1, size: mboxsize, isRaspi2: phy.BCM2708_PERI_BASE != 0x20000000)
guard let mailbox = mailbox else {fatalError("Could allocate mailbox.")}
dmaCallbackPointer = mailbox.baseVirtualAddress.assumingMemoryBound(to: DMACallback.self)
pwmRawPointer = (mailbox.baseVirtualAddress + MemoryLayout<DMACallback>.stride).assumingMemoryBound(to: UInt.self)
// Fill PWM buffer with zeros
let rows = dataSize / MemoryLayout<UInt>.stride
for pos in 0..<rows {
pwmRawPointer.advanced(by: pos).pointee = 0
}
// Stop the PWM
phy.pwmBasePointer.pointee = 0
usleep(10)
// Stop the clock killing the clock
phy.killClock()
// Check the BUSY flag, doesn't always work
//while (clockBasePointer.advanced(by: 40).pointee & (1 << 7)) != 0 {}
// Configure clock
let idiv = calculateUnscaledDIVI(base: .PLLD, desired: UInt(symbolBits * patternFrequency))
phy.clockBasePointer.advanced(by: 41).pointee = CLKM_PASSWD | (idiv << CLKM_DIV_DIVI) //Set DIVI value
phy.clockBasePointer.advanced(by: 40).pointee = CLKM_PASSWD | CLKM_CTL_ENAB | CLKM_CTL_SRC_PLLD //Enable clock, MASH 0, source PLLD
usleep(10)
// Check the BUSY flag, doesn't always work
//while (clockBasePointer.advanced(by: 40).pointee & (1 << 7)) != 0 {}
// Configure PWM
phy.pwmBasePointer.advanced(by: 4).pointee = 32 // RNG1: 32-bits per word to serialize
usleep(10)
phy.pwmBasePointer.pointee = PWMCTL_CLRF1
usleep(10)
phy.pwmBasePointer.advanced(by: 2).pointee = PWMDMAC_ENAB | 7 << PWMDMAC_PANIC | 3 << PWMDMAC_DREQ
usleep(10)
phy.pwmBasePointer.pointee = PWMCTL_USEF1 | PWMCTL_MODE1 //| PWMCTL_USEF2 | PWMCTL_MODE2 // For 2nd chan
usleep(10)
phy.pwmBasePointer.pointee |= PWMCTL_PWEN1 //| PWMCTL_PWEN2 // For 2nd chan
// Initialize the DMA control block
dmaCallbackPointer.pointee = DMACallback(
ti: DMATI_NO_WIDE_BURSTS | // 32-bit transfers
DMATI_WAIT_RESP | // wait for write complete
DMATI_DEST_DREQ | // user peripheral flow control
UInt32(0x5 << DMATI_PERMAP) | // PWM peripheral
DMATI_SRC_INC, // Increment src addr
source_ad: UInt32(mailbox.virtualTobaseBusAddress(UnsafeMutableRawPointer(pwmRawPointer))),
dest_ad: UInt32(phy.PWM_PHY_BASE + 0x18), // PWM FIF1 Register, shared between channels
txfr_len: UInt32(dataSize),
stride: 0,
nextconbk: 0)
dmaBasePointer.pointee = 0 //0 CS
dmaBasePointer.advanced(by: 5).pointee = 0 //0 TXFR_LEN
}
/// Send data using the pattern information already provided
public func sendDataWithPattern(values: [UInt8]) {
guard symbolBits > 0 else {fatalError("Couldn't generate a valid pattern for the provided duty cycle values, try with more spaced values.")}
// Wait for the previous signal to end
dma_wait()
// Convert from raw uint8 data to a sequence of patterns
let stream = dataToBitStream(data: values, zero: zeroPattern, one: onePattern, width: symbolBits)
// Copy the pattern stream to the raw pwm buffer location
stream.withUnsafeBytes { (ptr: UnsafeRawBufferPointer) in
#if swift(>=4.1)
UnsafeMutableRawPointer(pwmRawPointer).copyMemory(from: ptr.baseAddress!, byteCount: stream.count * MemoryLayout<UInt32>.stride)
#else
UnsafeMutableRawPointer(pwmRawPointer).copyBytes(from: ptr.baseAddress!, count: stream.count * MemoryLayout<UInt32>.stride)
#endif
}
// Start the DMA transfer toward the PWM FIFO
dma_start(dmaCallback: mailbox.virtualTobaseBusAddress(mailbox.baseVirtualAddress))
}
/// Calculate an approximated bit pattern representation n-bits wide and filled with ones from the left with the given % value.
/// The width of the pattern is calculated as the smaller one for which the representation of zero and one differ.
/// Possible width starting from 3 bits to 16, try to adjust your percentage levels to have the smaller possible patterns.
///
/// Pattern at 33% with width 3 bits: 100
/// Pattern at 60% with width 3 bits: 110
/// Pattern at 10% with width 8 bits: 10000000
/// Pattern at 12% with width 8 bits: 10000000
/// Pattern at 20% with width 8 bits: 11000000
///
/// - Parameter zero: percentage of fill for the zero value
/// - Parameter one: percentage of fill for the one value
///
/// - Returns: patterns for zero and one and first acceptable bit width
///
private func getRepresentation(zero: Int, one: Int) -> (zero: Int, one: Int, width: Int) {
for size in 3...16 {
let (z, o) = getRepresentation(zero: zero, one: one, bits: size)
if (z ^ o) > 0 {
return (z, o, size)
}
}
return (0, 0, 0)
}
/// Calculate an approximated bit pattern representation n-bits wide and filled with ones from the left with the given % value
///
/// Pattern at 33% with width 3 bits: 100
/// Pattern at 60% with width 3 bits: 110
/// Pattern at 10% with width 8 bits: 10000000
/// Pattern at 12% with width 8 bits: 10000000
/// Pattern at 20% with width 8 bits: 11000000
///
/// - Parameter zero: percentage of fill for the zero value
/// - Parameter one: percentage of fill for the one value
/// - Parameter bits: number of bits that will be used to generate the bit pattern
///
/// - Returns: patterns for zero and one
///
private func getRepresentation(zero: Int, one: Int, bits: Int) -> (zero: Int, one: Int) {
// Mask to extract only the n-bits value we want from the final result
let mask = (1 << bits) - 1
// How many bits must be at 1 for the zero and one bit pattern
var z = Int((Float(zero) * Float(bits)/100).rounded(.toNearestOrAwayFromZero))
z = (z == 0) ? 1 : z
var o = Int((Float(one) * Float(bits)/100).rounded(.toNearestOrAwayFromZero))
o = (o == 0) ? 1 : o
// To get the pattern we want:
// 1 << (bit_width - number_of_ones) , this gets us a 1 at the position number_of_ones+1, everything else unset
// the_above - 1 , this gets us the specular mask that we'll negate and than mask
z = ~((1 << (bits - z)) - 1) & mask
o = ~((1 << (bits - o)) - 1) & mask
return (z, o)
}
/// Convert each bit component of the data to a n bit representation
private func dataToBitStream(data: [UInt8], zero: Int, one: Int, width: Int ) -> [UInt32] {
var output = [UInt32](repeating:0, count: data.count * symbolBits / 4 + 1)
var count = 0
output.withUnsafeMutableBytes { (bptr: UnsafeMutableRawBufferPointer) in
for byte in data {
for i in (0...7).reversed() {
// Get the bitset for this bit value that will be appended to output
let s: UInt = ((byte & (1 << UInt8(i))) > 0) ? UInt(one) : UInt(zero)
// The byte where the pattern starts
var startAt = count/8
// The offset within the byte for the starting position
var withOffset = count % 8
// Of how many positions the pattern need to be shifted to be located correctly within this byte (overlapping bits
// will be pushed to the right). Can be negative.
var shiftAmount = 8 - withOffset - symbolBits
repeat {
// Value to be set, shifted as needed
let setMask = (shiftAmount >= 0) ? (s << UInt(shiftAmount)) : (s >> UInt(-shiftAmount))
// Calculate little endian id from startAt to fill the UInt32 with the right endianess
let littleId = (startAt/4) * 4 + (3 - startAt%4)
// Adds this bitmask to the current byte without touching the rest
bptr[littleId] |= UInt8(setMask & 0xFF)
// If the pattern overlaps in the next byte, we'll have offset=0
withOffset = 0
// If the pattern overlaps, let's increment startAt to point to the next byte
startAt += 1
// If the pattern overlaps, this is the amount of shifting needed to push bits
// to the left, bits that were already added in the previous iteration.
shiftAmount = 8 + shiftAmount
// Print the current state of the byte of this iteration
// printUInt8(bptr[littleId])
// If the amount is <8 there are still bits in the pattern that need to be added to the stream
} while shiftAmount < 8
// Counts the number of bits successfully added to the stream
count += symbolBits
}
}
}
// Print the result of the conversion as it will be sent to the PWM (same endianess)
// for element in output {
// printBinary(element)
// }
return output
}
private func printBinary(_ value: UInt32) {
var res = ""
for i in (0...31).reversed() {
res += ((value & (1 << UInt32(i))) > 0) ? "1" : "0"
}
print(res)
}
private func printUInt8(_ value: UInt8) {
var res = ""
for i in (0...7).reversed() {
res += ((value & (1 << UInt8(i))) > 0) ? "1" : "0"
}
print(res)
}
}
// MARK: - PWM Contants
// Constants for the Clock Manager General Purpose Control Register
let CLKM_PASSWD: UInt = 0x5A000000
let CLKM_CTL_KILL: UInt = (1 << 5)
let CLKM_CTL_ENAB: UInt = (1 << 4)
let CLKM_CTL_SRC_OSC: UInt = 1 // 19.2 MHz oscillator
let CLKM_CTL_SRC_PLLA: UInt = 4 // ~393.216 MHz PLLA (Audio)
let CLKM_CTL_SRC_PLLC: UInt = 5 // 1000 MHz PLLC (changes with overclock settings)
let CLKM_CTL_SRC_PLLD: UInt = 6 // 500 MHz PLLD
let CLKM_CTL_SRC_HDMI: UInt = 7 // 216 MHz HDMI auxiliary
// Constants for the Clock Manager General Purpose Divisors Register
let CLKM_DIV_DIVI: UInt = 12
let CLKM_DIV_DIVF: UInt = 0
// Constants for the PWM Control Register
let PWMCTL_MSEN2: UInt = (1 << 15)
// No PWMCTL_CLRF2, the FIFO is shared
let PWMCTL_USEF2: UInt = (1 << 13)
let PWMCTL_POLA2: UInt = (1 << 12)
let PWMCTL_SBIT2: UInt = (1 << 11)
let PWMCTL_RPTL2: UInt = (1 << 10)
let PWMCTL_MODE2: UInt = (1 << 9)
let PWMCTL_PWEN2: UInt = (1 << 8)
let PWMCTL_MSEN1: UInt = (1 << 7)
let PWMCTL_CLRF1: UInt = (1 << 6)
let PWMCTL_USEF1: UInt = (1 << 5)
let PWMCTL_POLA1: UInt = (1 << 4)
let PWMCTL_SBIT1: UInt = (1 << 3)
let PWMCTL_RPTL1: UInt = (1 << 2)
let PWMCTL_MODE1: UInt = (1 << 1)
let PWMCTL_PWEN1: UInt = (1 << 0)
// Clock sources
// 0 0 Hz Ground
// 1 19.2 MHz oscillator
// 2 0 Hz testdebug0
// 3 0 Hz testdebug1
// 4 ~393.216 MHz PLLA (Audio)
// 5 1000 MHz PLLC (changes with overclock settings)
// 6 500 MHz PLLD
// 7 216 MHz HDMI auxiliary
// 8-15 0 Hz Ground
enum ClockSource: UInt {
case Oscillator = 19200000
case PLLA = 393216000
case PLLC = 1000000000
case PLLD = 500000000
case HDMI = 216000000
}
// DMA Register
let DMACS_RESET: UInt = (1 << 31)
let DMACS_ABORT: UInt = (1 << 30)
let DMACS_WAIT_OUTSTANDING_WRITES: UInt = (1 << 28)
let DMACS_PANIC_PRIORITY: UInt = 20 // <<
let DMACS_PRIORITY: UInt = 16 // <<
let DMACS_ERROR: UInt = (1 << 8)
let DMACS_INT: UInt = (1 << 2)
let DMACS_END: UInt = (1 << 1)
let DMACS_ACTIVE: UInt = (1 << 0)
/*
* DMA Control Block in Main Memory
*
* Note: Must start at a 256 byte aligned address.
* Use corresponding register field definitions.
*/
struct DMACallback {
let ti: UInt32
let source_ad: UInt32
let dest_ad: UInt32
let txfr_len: UInt32
let stride: UInt32
let nextconbk: UInt32
let reserv1: UInt32 = 0
let reserv2: UInt32 = 0
}
// PWM DMAC Register
let PWMDMAC_ENAB: UInt = (1 << 31)
let PWMDMAC_PANIC: UInt = 8
let PWMDMAC_DREQ: UInt = 0
// DMA Register
let DMATI_NO_WIDE_BURSTS: UInt32 = (1 << 26)
let DMATI_PERMAP: UInt32 = 16
let DMATI_SRC_INC: UInt32 = (1 << 8)
let DMATI_DEST_DREQ: UInt32 = (1 << 6)
let DMATI_WAIT_RESP: UInt32 = (1 << 3)
// MARK: - Darwin / Xcode Support
#if os(OSX) || os(iOS)
private var O_SYNC: CInt { fatalError("Linux only") }
#endif