forked from google/periph
/
gpio.go
1361 lines (1278 loc) · 38.9 KB
/
gpio.go
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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2017 The Periph Authors. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file.
package bcm283x
import (
"errors"
"fmt"
"os"
"strconv"
"strings"
"time"
"periph.io/x/periph"
"periph.io/x/periph/conn/gpio"
"periph.io/x/periph/conn/gpio/gpioreg"
"periph.io/x/periph/conn/gpio/gpiostream"
"periph.io/x/periph/conn/physic"
"periph.io/x/periph/conn/pin"
"periph.io/x/periph/host/distro"
"periph.io/x/periph/host/pmem"
"periph.io/x/periph/host/sysfs"
"periph.io/x/periph/host/videocore"
)
// All the pins supported by the CPU.
var (
GPIO0 *Pin // I2C0_SDA
GPIO1 *Pin // I2C0_SCL
GPIO2 *Pin // I2C1_SDA
GPIO3 *Pin // I2C1_SCL
GPIO4 *Pin // CLK0
GPIO5 *Pin // CLK1
GPIO6 *Pin // CLK2
GPIO7 *Pin // SPI0_CS1
GPIO8 *Pin // SPI0_CS0
GPIO9 *Pin // SPI0_MISO
GPIO10 *Pin // SPI0_MOSI
GPIO11 *Pin // SPI0_CLK
GPIO12 *Pin // PWM0
GPIO13 *Pin // PWM1
GPIO14 *Pin // UART0_TX, UART1_TX
GPIO15 *Pin // UART0_RX, UART1_RX
GPIO16 *Pin // UART0_CTS, SPI1_CS2, UART1_CTS
GPIO17 *Pin // UART0_RTS, SPI1_CS1, UART1_RTS
GPIO18 *Pin // I2S_SCK, SPI1_CS0, PWM0
GPIO19 *Pin // I2S_WS, SPI1_MISO, PWM1
GPIO20 *Pin // I2S_DIN, SPI1_MOSI, CLK0
GPIO21 *Pin // I2S_DOUT, SPI1_CLK, CLK1
GPIO22 *Pin //
GPIO23 *Pin //
GPIO24 *Pin //
GPIO25 *Pin //
GPIO26 *Pin //
GPIO27 *Pin //
GPIO28 *Pin // I2C0_SDA, I2S_SCK
GPIO29 *Pin // I2C0_SCL, I2S_WS
GPIO30 *Pin // I2S_DIN, UART0_CTS, UART1_CTS
GPIO31 *Pin // I2S_DOUT, UART0_RTS, UART1_RTS
GPIO32 *Pin // CLK0, UART0_TX, UART1_TX
GPIO33 *Pin // UART0_RX, UART1_RX
GPIO34 *Pin // CLK0
GPIO35 *Pin // SPI0_CS1
GPIO36 *Pin // SPI0_CS0, UART0_TX
GPIO37 *Pin // SPI0_MISO, UART0_RX
GPIO38 *Pin // SPI0_MOSI, UART0_RTS
GPIO39 *Pin // SPI0_CLK, UART0_CTS
GPIO40 *Pin // PWM0, SPI2_MISO, UART1_TX
GPIO41 *Pin // PWM1, SPI2_MOSI, UART1_RX
GPIO42 *Pin // CLK1, SPI2_CLK, UART1_RTS
GPIO43 *Pin // CLK2, SPI2_CS0, UART1_CTS
GPIO44 *Pin // CLK1, I2C0_SDA, I2C1_SDA, SPI2_CS1
GPIO45 *Pin // PWM1, I2C0_SCL, I2C1_SCL, SPI2_CS2
GPIO46 *Pin //
// Pins 47~53 are not exposed because using them would lead to immediate SD
// Card corruption.
)
// Present returns true if running on a Broadcom bcm283x based CPU.
func Present() bool {
if isArm {
hardware, ok := distro.CPUInfo()["Hardware"]
return ok && strings.HasPrefix(hardware, "BCM")
}
return false
}
// PinsRead0To31 returns the value of all GPIO0 to GPIO31 at their corresponding
// bit as a single read operation.
//
// This function is extremely fast and does no error checking.
//
// The returned bits are valid for both inputs and outputs.
func PinsRead0To31() uint32 {
return drvGPIO.gpioMemory.level[0]
}
// PinsClear0To31 clears the value of GPIO0 to GPIO31 pin for the bit set at
// their corresponding bit as a single write operation.
//
// This function is extremely fast and does no error checking.
func PinsClear0To31(mask uint32) {
drvGPIO.gpioMemory.outputClear[0] = mask
}
// PinsSet0To31 sets the value of GPIO0 to GPIO31 pin for the bit set at their
// corresponding bit as a single write operation.
func PinsSet0To31(mask uint32) {
drvGPIO.gpioMemory.outputSet[0] = mask
}
// PinsRead32To46 returns the value of all GPIO32 to GPIO46 at their
// corresponding 'bit minus 32' as a single read operation.
//
// This function is extremely fast and does no error checking.
//
// The returned bits are valid for both inputs and outputs.
//
// Bits above 15 are guaranteed to be 0.
//
// This function is not recommended on Raspberry Pis as these GPIOs are not
// easily accessible.
func PinsRead32To46() uint32 {
return drvGPIO.gpioMemory.level[1] & 0x7fff
}
// PinsClear32To46 clears the value of GPIO31 to GPIO46 pin for the bit set at
// their corresponding 'bit minus 32' as a single write operation.
//
// This function is extremely fast and does no error checking.
//
// Bits above 15 are ignored.
//
// This function is not recommended on Raspberry Pis as these GPIOs are not
// easily accessible.
func PinsClear32To46(mask uint32) {
drvGPIO.gpioMemory.outputClear[1] = (mask & 0x7fff)
}
// PinsSet32To46 sets the value of GPIO31 to GPIO46 pin for the bit set at
// their corresponding 'bit minus 32' as a single write operation.
//
// This function is extremely fast and does no error checking.
//
// Bits above 15 are ignored.
//
// This function is not recommended on Raspberry Pis as these GPIOs are not
// easily accessible.
func PinsSet32To46(mask uint32) {
drvGPIO.gpioMemory.outputSet[1] = (mask & 0x7fff)
}
// PinsSetup0To27 sets the output current drive strength, output slew limiting
// and input hysteresis for GPIO 0 to 27.
//
// Default drive is 8mA, slew unlimited and hysteresis enabled.
//
// Can only be used if driver bcm283x-dma was loaded.
func PinsSetup0To27(drive physic.ElectricCurrent, slewLimit, hysteresis bool) error {
if drvDMA.gpioPadMemory == nil {
return errors.New("bcm283x-dma not initialized; try again as root?")
}
drvDMA.gpioPadMemory.pads0.set(toPad(drive, slewLimit, hysteresis))
return nil
}
// PinsSetup28To45 sets the output current drive strength, output slew limiting
// and input hysteresis for GPIO 28 to 45.
//
// Default drive is 16mA, slew unlimited and hysteresis enabled.
//
// Can only be used if driver bcm283x-dma was loaded.
//
// This function is not recommended on Raspberry Pis as these GPIOs are not
// easily accessible.
func PinsSetup28To45(drive physic.ElectricCurrent, slewLimit, hysteresis bool) error {
if drvDMA.gpioPadMemory == nil {
return errors.New("bcm283x-dma not initialized; try again as root?")
}
drvDMA.gpioPadMemory.pads1.set(toPad(drive, slewLimit, hysteresis))
return nil
}
// Pin is a GPIO number (GPIOnn) on BCM238(5|6|7).
//
// Pin implements gpio.PinIO.
type Pin struct {
// Immutable.
number int
name string
defaultPull gpio.Pull // Default pull at system boot, as per datasheet.
// Immutable after driver initialization.
sysfsPin *sysfs.Pin // Set to the corresponding sysfs.Pin, if any.
// Mutable.
usingEdge bool // Set when edge detection is enabled.
usingClock bool // Set when a CLK, PWM or I2S/PCM clock is used.
dmaCh *dmaChannel // Set when DMA is used for PWM or I2S/PCM.
dmaBuf *videocore.Mem // Set when DMA is used for PWM or I2S/PCM.
}
// String implements conn.Resource.
func (p *Pin) String() string {
return p.name
}
// Halt implements conn.Resource.
//
// If the pin is running a clock, PWM or waiting for edges, it is halted.
//
// In the case of clock or PWM, all pins with this clock source are also
// disabled.
func (p *Pin) Halt() error {
if p.usingEdge {
if err := p.sysfsPin.Halt(); err != nil {
return p.wrap(err)
}
p.usingEdge = false
}
return p.haltClock()
}
// Name implements pin.Pin.
func (p *Pin) Name() string {
return p.name
}
// Number implements pin.Pin.
//
// This is the GPIO number, not the pin number on a header.
func (p *Pin) Number() int {
return p.number
}
// Function implements pin.Pin.
func (p *Pin) Function() string {
return string(p.Func())
}
// Func implements pin.PinFunc.
func (p *Pin) Func() pin.Func {
if drvGPIO.gpioMemory == nil {
if p.sysfsPin == nil {
return pin.Func("ERR")
}
return p.sysfsPin.Func()
}
switch f := p.function(); f {
case in:
if p.FastRead() {
return gpio.IN_HIGH
}
return gpio.IN_LOW
case out:
if p.FastRead() {
return gpio.OUT_HIGH
}
return gpio.OUT_LOW
case alt0:
if s := mapping[p.number][0]; len(s) != 0 {
return s
}
return pin.Func("ALT0")
case alt1:
if s := mapping[p.number][1]; len(s) != 0 {
return s
}
return pin.Func("ALT1")
case alt2:
if s := mapping[p.number][2]; len(s) != 0 {
return s
}
return pin.Func("ALT2")
case alt3:
if s := mapping[p.number][3]; len(s) != 0 {
return s
}
return pin.Func("ALT3")
case alt4:
if s := mapping[p.number][4]; len(s) != 0 {
return s
}
return pin.Func("ALT4")
case alt5:
if s := mapping[p.number][5]; len(s) != 0 {
return s
}
return pin.Func("ALT5")
default:
return pin.Func("ERR")
}
}
// SupportedFuncs implements pin.PinFunc.
func (p *Pin) SupportedFuncs() []pin.Func {
f := make([]pin.Func, 0, 2+4)
f = append(f, gpio.IN, gpio.OUT)
for _, m := range mapping[p.number] {
if m != "" {
f = append(f, m)
}
}
return f
}
// SetFunc implements pin.PinFunc.
func (p *Pin) SetFunc(f pin.Func) error {
if drvGPIO.gpioMemory == nil {
if p.sysfsPin == nil {
return p.wrap(errors.New("subsystem gpiomem not initialized and sysfs not accessible"))
}
return p.sysfsPin.SetFunc(f)
}
switch f {
case gpio.FLOAT:
return p.In(gpio.Float, gpio.NoEdge)
case gpio.IN:
return p.In(gpio.PullNoChange, gpio.NoEdge)
case gpio.IN_LOW:
return p.In(gpio.PullDown, gpio.NoEdge)
case gpio.IN_HIGH:
return p.In(gpio.PullUp, gpio.NoEdge)
case gpio.OUT_HIGH:
return p.Out(gpio.High)
case gpio.OUT_LOW:
return p.Out(gpio.Low)
default:
isGeneral := f == f.Generalize()
for i, m := range mapping[p.number] {
if m == f || (isGeneral && m.Generalize() == f) {
if err := p.Halt(); err != nil {
return err
}
switch i {
case 0:
p.setFunction(alt0)
case 1:
p.setFunction(alt1)
case 2:
p.setFunction(alt2)
case 3:
p.setFunction(alt3)
case 4:
p.setFunction(alt4)
case 5:
p.setFunction(alt5)
}
return nil
}
}
return p.wrap(errors.New("unsupported function"))
}
}
// In implements gpio.PinIn.
//
// Specifying a value for pull other than gpio.PullNoChange causes this
// function to be slightly slower (about 1ms).
//
// For pull down, the resistor is 50KOhm~60kOhm
// For pull up, the resistor is 50kOhm~65kOhm
//
// The pull resistor stays set even after the processor shuts down. It is not
// possible to 'read back' what value was specified for each pin.
//
// Will fail if requesting to change a pin that is set to special functionality.
//
// Using edge detection requires opening a gpio sysfs file handle. On Raspbian,
// make sure the user is member of group 'gpio'. The pin will be exported at
// /sys/class/gpio/gpio*/. Note that the pin will not be unexported at
// shutdown.
//
// For edge detection, the processor samples the input at its CPU clock rate
// and looks for '011' to rising and '100' for falling detection to avoid
// glitches. Because gpio sysfs is used, the latency is unpredictable.
func (p *Pin) In(pull gpio.Pull, edge gpio.Edge) error {
if p.usingEdge && edge == gpio.NoEdge {
if err := p.sysfsPin.Halt(); err != nil {
return p.wrap(err)
}
}
if drvGPIO.gpioMemory == nil {
if p.sysfsPin == nil {
return p.wrap(errors.New("subsystem gpiomem not initialized and sysfs not accessible"))
}
if pull != gpio.PullNoChange {
return p.wrap(errors.New("pull cannot be used when subsystem gpiomem not initialized"))
}
if err := p.sysfsPin.In(pull, edge); err != nil {
return p.wrap(err)
}
p.usingEdge = edge != gpio.NoEdge
return nil
}
if err := p.haltClock(); err != nil {
return err
}
p.setFunction(in)
if pull != gpio.PullNoChange {
// Changing pull resistor requires a specific dance as described at
// https://www.raspberrypi.org/wp-content/uploads/2012/02/BCM2835-ARM-Peripherals.pdf
// page 101.
// Set Pull
switch pull {
case gpio.PullDown:
drvGPIO.gpioMemory.pullEnable = 1
case gpio.PullUp:
drvGPIO.gpioMemory.pullEnable = 2
case gpio.Float:
drvGPIO.gpioMemory.pullEnable = 0
}
// Datasheet states caller needs to sleep 150 cycles.
sleep150cycles()
offset := p.number / 32
drvGPIO.gpioMemory.pullEnableClock[offset] = 1 << uint(p.number%32)
sleep150cycles()
drvGPIO.gpioMemory.pullEnable = 0
drvGPIO.gpioMemory.pullEnableClock[offset] = 0
}
if edge != gpio.NoEdge {
if p.sysfsPin == nil {
return p.wrap(fmt.Errorf("pin %d is not exported by sysfs", p.number))
}
// This resets pending edges.
if err := p.sysfsPin.In(gpio.PullNoChange, edge); err != nil {
return p.wrap(err)
}
p.usingEdge = true
}
return nil
}
// Read implements gpio.PinIn.
//
// This function is fast. It works even if the pin is set as output.
func (p *Pin) Read() gpio.Level {
if drvGPIO.gpioMemory == nil {
if p.sysfsPin == nil {
return gpio.Low
}
return p.sysfsPin.Read()
}
if p.number < 32 {
// Important: do not remove the &31 here even if not necessary. Testing
// showed that it slows down the performance by several percents.
return gpio.Level((drvGPIO.gpioMemory.level[0] & (1 << uint(p.number&31))) != 0)
}
return gpio.Level((drvGPIO.gpioMemory.level[1] & (1 << uint(p.number&31))) != 0)
}
// FastRead return the current pin level without any error checking.
//
// This function is very fast. It works even if the pin is set as output.
func (p *Pin) FastRead() gpio.Level {
if p.number < 32 {
// Important: do not remove the &31 here even if not necessary. Testing
// showed that it slows down the performance by several percents.
return gpio.Level((drvGPIO.gpioMemory.level[0] & (1 << uint(p.number&31))) != 0)
}
return gpio.Level((drvGPIO.gpioMemory.level[1] & (1 << uint(p.number&31))) != 0)
}
// WaitForEdge implements gpio.PinIn.
func (p *Pin) WaitForEdge(timeout time.Duration) bool {
if p.sysfsPin != nil {
return p.sysfsPin.WaitForEdge(timeout)
}
return false
}
// Pull implements gpio.PinIn.
//
// bcm283x doesn't support querying the pull resistor of any GPIO pin.
func (p *Pin) Pull() gpio.Pull {
// TODO(maruel): The best that could be added is to cache the last set value
// and return it.
return gpio.PullNoChange
}
// DefaultPull implements gpio.PinIn.
//
// The CPU doesn't return the current pull.
func (p *Pin) DefaultPull() gpio.Pull {
return p.defaultPull
}
// Out implements gpio.PinOut.
//
// Fails if requesting to change a pin that is set to special functionality.
func (p *Pin) Out(l gpio.Level) error {
if drvGPIO.gpioMemory == nil {
if p.sysfsPin == nil {
return p.wrap(errors.New("subsystem gpiomem not initialized and sysfs not accessible"))
}
return p.sysfsPin.Out(l)
}
// TODO(maruel): This function call is very costly.
if err := p.Halt(); err != nil {
return err
}
// Change output before changing mode to not create any glitch.
p.FastOut(l)
p.setFunction(out)
return nil
}
// FastOut sets a pin output level with Absolutely No error checking.
//
// Out() Must be called once first before calling FastOut(), otherwise the
// behavior is undefined. Then FastOut() can be used for minimal CPU overhead
// to reach Mhz scale bit banging.
func (p *Pin) FastOut(l gpio.Level) {
mask := uint32(1) << uint(p.number&31)
if l == gpio.Low {
if p.number < 32 {
drvGPIO.gpioMemory.outputClear[0] = mask
} else {
drvGPIO.gpioMemory.outputClear[1] = mask
}
} else {
if p.number < 32 {
drvGPIO.gpioMemory.outputSet[0] = mask
} else {
drvGPIO.gpioMemory.outputSet[1] = mask
}
}
}
// BUG(maruel): PWM(): There is no conflict verification when multiple pins are
// used simultaneously. The last call to PWM() will affect all pins of the same
// type (CLK0, CLK2, PWM0 or PWM1).
// PWM implements gpio.PinOut.
//
// It outputs a periodic signal on supported pins without CPU usage.
//
// PWM pins
//
// PWM0 is exposed on pins 12, 18 and 40. However, PWM0 is used for generating
// clock for DMA and unavailable for PWM.
//
// PWM1 is exposed on pins 13, 19, 41 and 45.
//
// PWM1 uses 25Mhz clock source. The frequency must be a divisor of 25Mhz.
//
// DMA driven PWM is available for all pins except PWM1 pins, its resolution is
// 200KHz which is down-sampled from 25MHz clock above. The number of DMA driven
// PWM is limited.
//
// Furthermore, these can only be used if the drive "bcm283x-dma" was loaded.
// It can only be loaded if the process has root level access.
//
// The user must call either Halt(), In(), Out(), PWM(0,..) or
// PWM(gpio.DutyMax,..) to stop the clock source and DMA engine before exiting
// the program.
func (p *Pin) PWM(duty gpio.Duty, freq physic.Frequency) error {
if duty == 0 {
return p.Out(gpio.Low)
} else if duty == gpio.DutyMax {
return p.Out(gpio.High)
}
f := out
useDMA := false
switch p.number {
case 12, 40: // PWM0 alt0: disabled
useDMA = true
case 13, 41, 45: // PWM1
f = alt0
case 18: // PWM0 alt5: disabled
useDMA = true
case 19: // PWM1
f = alt5
default:
useDMA = true
}
// Intentionally check later, so a more informative error is returned on
// unsupported pins.
if drvGPIO.gpioMemory == nil {
return p.wrap(errors.New("subsystem gpiomem not initialized"))
}
if drvDMA.pwmMemory == nil || drvDMA.clockMemory == nil {
return p.wrap(errors.New("bcm283x-dma not initialized; try again as root?"))
}
if useDMA {
if m := drvDMA.pwmDMAFreq / 2; m < freq {
return p.wrap(fmt.Errorf("frequency must be at most %s", m))
}
// Total cycles in the period
rng := uint64(drvDMA.pwmDMAFreq / freq)
// Pulse width cycles
dat := uint32((rng*uint64(duty) + uint64(gpio.DutyHalf)) / uint64(gpio.DutyMax))
var err error
// TODO(simokawa): Reuse DMA buffer if possible.
if err = p.haltDMA(); err != nil {
return p.wrap(err)
}
// Start clock before DMA starts.
if _, err = setPWMClockSource(); err != nil {
return p.wrap(err)
}
if p.dmaCh, p.dmaBuf, err = startPWMbyDMA(p, uint32(rng), dat); err != nil {
return p.wrap(err)
}
} else {
if m := drvDMA.pwmBaseFreq / 2; m < freq {
return p.wrap(fmt.Errorf("frequency must be at most %s", m))
}
// Total cycles in the period
rng := uint64(drvDMA.pwmBaseFreq / freq)
// Pulse width cycles
dat := uint32((rng*uint64(duty) + uint64(gpio.DutyHalf)) / uint64(gpio.DutyMax))
if _, err := setPWMClockSource(); err != nil {
return p.wrap(err)
}
// Bit shift for PWM0 and PWM1
shift := uint((p.number & 1) * 8)
if shift == 0 {
drvDMA.pwmMemory.rng1 = uint32(rng)
Nanospin(10 * time.Nanosecond)
drvDMA.pwmMemory.dat1 = uint32(dat)
} else {
drvDMA.pwmMemory.rng2 = uint32(rng)
Nanospin(10 * time.Nanosecond)
drvDMA.pwmMemory.dat2 = uint32(dat)
}
Nanospin(10 * time.Nanosecond)
old := drvDMA.pwmMemory.ctl
drvDMA.pwmMemory.ctl = (old & ^(0xff << shift)) | ((pwm1Enable | pwm1MS) << shift)
}
p.usingClock = true
p.setFunction(f)
return nil
}
// StreamIn implements gpiostream.PinIn.
//
// DMA driven StreamOut is available for GPIO0 to GPIO31 pin and the maximum
// resolution is 200kHz.
func (p *Pin) StreamIn(pull gpio.Pull, s gpiostream.Stream) error {
b, ok := s.(*gpiostream.BitStream)
if !ok {
return errors.New("bcm283x: other Stream than BitStream are not implemented yet")
}
if !b.LSBF {
return errors.New("bcm283x: MSBF BitStream is not implemented yet")
}
if b.Duration() == 0 {
return errors.New("bcm283x: can't read to empty BitStream")
}
if drvGPIO.gpioMemory == nil {
return p.wrap(errors.New("subsystem gpiomem not initialized"))
}
if err := p.In(pull, gpio.NoEdge); err != nil {
return err
}
if err := dmaReadStream(p, b); err != nil {
return p.wrap(err)
}
return nil
}
// StreamOut implements gpiostream.PinOut.
//
// I2S/PCM driven StreamOut is available for GPIO21 pin. The resolution is up to
// 250MHz.
//
// For GPIO0 to GPIO31 except GPIO21 pin, DMA driven StreamOut is available and
// the maximum resolution is 200kHz.
func (p *Pin) StreamOut(s gpiostream.Stream) error {
if drvGPIO.gpioMemory == nil {
return p.wrap(errors.New("subsystem gpiomem not initialized"))
}
if err := p.Out(gpio.Low); err != nil {
return err
}
// If the pin is I2S_DOUT, use PCM for much nicer stream and lower memory
// usage.
if p.number == 21 || p.number == 31 {
alt := alt0
if p.number == 31 {
alt = alt2
}
p.setFunction(alt)
if err := dmaWriteStreamPCM(p, s); err != nil {
return p.wrap(err)
}
} else if err := dmaWriteStreamEdges(p, s); err != nil {
return p.wrap(err)
}
return nil
}
// Drive returns the configured output current drive strength for this GPIO.
//
// The current drive is configurable per GPIO groups: 0~27 and 28~45.
//
// The default value for GPIOs 0~27 is 8mA and for GPIOs 28~45 is 16mA.
//
// The value is a multiple 2mA between 2mA and 16mA.
//
// Can only be used if driver bcm283x-dma was loaded. Otherwise returns 0.
func (p *Pin) Drive() physic.ElectricCurrent {
if drvDMA.gpioPadMemory == nil {
return 0
}
var v pad
if p.number < 28 {
v = drvDMA.gpioPadMemory.pads0
} else {
// GPIO 46~53 are not exposed.
v = drvDMA.gpioPadMemory.pads1
}
switch v & 7 {
case padDrive2mA:
return 2 * physic.MilliAmpere
case padDrive4mA:
return 4 * physic.MilliAmpere
case padDrive6mA:
return 6 * physic.MilliAmpere
case padDrive8mA:
return 8 * physic.MilliAmpere
case padDrive10mA:
return 10 * physic.MilliAmpere
case padDrive12mA:
return 12 * physic.MilliAmpere
case padDrive14mA:
return 14 * physic.MilliAmpere
case padDrive16mA:
return 16 * physic.MilliAmpere
default:
return 0
}
}
// SlewLimit returns true if the output slew is limited to reduce interference.
//
// The slew is configurable per GPIO groups: 0~27 and 28~45.
//
// The default is true.
//
// Can only be used if driver bcm283x-dma was loaded. Otherwise returns false
// (the default value).
func (p *Pin) SlewLimit() bool {
if drvDMA.gpioPadMemory == nil {
return true
}
if p.number < 28 {
return drvDMA.gpioPadMemory.pads0&padSlewUnlimited == 0
}
return drvDMA.gpioPadMemory.pads1&padSlewUnlimited == 0
}
// Hysteresis returns true if the input hysteresis via a Schmitt trigger is
// enabled.
//
// The hysteresis is configurable per GPIO groups: 0~27 and 28~45.
//
// The default is true.
//
// Can only be used if driver bcm283x-dma was loaded. Otherwise returns true
// (the default value).
func (p *Pin) Hysteresis() bool {
if drvDMA.gpioPadMemory == nil {
return true
}
if p.number < 28 {
return drvDMA.gpioPadMemory.pads0&padHysteresisEnable != 0
}
return drvDMA.gpioPadMemory.pads1&padHysteresisEnable != 0
}
// Internal code.
func (p *Pin) haltDMA() error {
if p.dmaCh != nil {
p.dmaCh.reset()
p.dmaCh = nil
}
if p.dmaBuf != nil {
if err := p.dmaBuf.Close(); err != nil {
return p.wrap(err)
}
p.dmaBuf = nil
}
return nil
}
// haltClock disables the CLK/PWM clock if used.
func (p *Pin) haltClock() error {
if err := p.haltDMA(); err != nil {
return err
}
if !p.usingClock {
return nil
}
p.usingClock = false
// Disable PWMx.
switch p.number {
// PWM0 is not used.
case 12, 18, 40:
// PWM1
case 13, 19, 41, 45:
for _, i := range []int{13, 19, 41, 45} {
if cpuPins[i].usingClock {
return nil
}
}
shift := uint((p.number & 1) * 8)
drvDMA.pwmMemory.ctl &= ^(0xff << shift)
}
// Disable PWM clock if nobody use.
for _, pin := range cpuPins {
if pin.usingClock {
return nil
}
}
err := resetPWMClockSource()
return err
}
// function returns the current GPIO pin function.
func (p *Pin) function() function {
if drvGPIO.gpioMemory == nil {
return alt5
}
return function((drvGPIO.gpioMemory.functionSelect[p.number/10] >> uint((p.number%10)*3)) & 7)
}
// setFunction changes the GPIO pin function.
func (p *Pin) setFunction(f function) {
off := p.number / 10
shift := uint(p.number%10) * 3
drvGPIO.gpioMemory.functionSelect[off] = (drvGPIO.gpioMemory.functionSelect[off] &^ (7 << shift)) | (uint32(f) << shift)
// If a pin switches from a specific functionality back to GPIO, the alias
// should be updated. For example both GPIO13 and GPIO19 support PWM1. By
// default, PWM1 will be associated to GPIO13, even if
// GPIO19.SetFunc(gpio.PWM) is called.
// TODO(maruel): pinreg.Unregister()
// TODO(maruel): pinreg.Register()
}
func (p *Pin) wrap(err error) error {
return fmt.Errorf("bcm283x-gpio (%s): %v", p, err)
}
//
// Each pin can have one of 7 functions.
const (
in function = 0
out function = 1
alt0 function = 4
alt1 function = 5
alt2 function = 6
alt3 function = 7
alt4 function = 3
alt5 function = 2
)
// cpuPins is all the pins as supported by the CPU. There is no guarantee that
// they are actually connected to anything on the board.
var cpuPins = []Pin{
{number: 0, name: "GPIO0", defaultPull: gpio.PullUp},
{number: 1, name: "GPIO1", defaultPull: gpio.PullUp},
{number: 2, name: "GPIO2", defaultPull: gpio.PullUp},
{number: 3, name: "GPIO3", defaultPull: gpio.PullUp},
{number: 4, name: "GPIO4", defaultPull: gpio.PullUp},
{number: 5, name: "GPIO5", defaultPull: gpio.PullUp},
{number: 6, name: "GPIO6", defaultPull: gpio.PullUp},
{number: 7, name: "GPIO7", defaultPull: gpio.PullUp},
{number: 8, name: "GPIO8", defaultPull: gpio.PullUp},
{number: 9, name: "GPIO9", defaultPull: gpio.PullDown},
{number: 10, name: "GPIO10", defaultPull: gpio.PullDown},
{number: 11, name: "GPIO11", defaultPull: gpio.PullDown},
{number: 12, name: "GPIO12", defaultPull: gpio.PullDown},
{number: 13, name: "GPIO13", defaultPull: gpio.PullDown},
{number: 14, name: "GPIO14", defaultPull: gpio.PullDown},
{number: 15, name: "GPIO15", defaultPull: gpio.PullDown},
{number: 16, name: "GPIO16", defaultPull: gpio.PullDown},
{number: 17, name: "GPIO17", defaultPull: gpio.PullDown},
{number: 18, name: "GPIO18", defaultPull: gpio.PullDown},
{number: 19, name: "GPIO19", defaultPull: gpio.PullDown},
{number: 20, name: "GPIO20", defaultPull: gpio.PullDown},
{number: 21, name: "GPIO21", defaultPull: gpio.PullDown},
{number: 22, name: "GPIO22", defaultPull: gpio.PullDown},
{number: 23, name: "GPIO23", defaultPull: gpio.PullDown},
{number: 24, name: "GPIO24", defaultPull: gpio.PullDown},
{number: 25, name: "GPIO25", defaultPull: gpio.PullDown},
{number: 26, name: "GPIO26", defaultPull: gpio.PullDown},
{number: 27, name: "GPIO27", defaultPull: gpio.PullDown},
{number: 28, name: "GPIO28", defaultPull: gpio.Float},
{number: 29, name: "GPIO29", defaultPull: gpio.Float},
{number: 30, name: "GPIO30", defaultPull: gpio.PullDown},
{number: 31, name: "GPIO31", defaultPull: gpio.PullDown},
{number: 32, name: "GPIO32", defaultPull: gpio.PullDown},
{number: 33, name: "GPIO33", defaultPull: gpio.PullDown},
{number: 34, name: "GPIO34", defaultPull: gpio.PullUp},
{number: 35, name: "GPIO35", defaultPull: gpio.PullUp},
{number: 36, name: "GPIO36", defaultPull: gpio.PullUp},
{number: 37, name: "GPIO37", defaultPull: gpio.PullDown},
{number: 38, name: "GPIO38", defaultPull: gpio.PullDown},
{number: 39, name: "GPIO39", defaultPull: gpio.PullDown},
{number: 40, name: "GPIO40", defaultPull: gpio.PullDown},
{number: 41, name: "GPIO41", defaultPull: gpio.PullDown},
{number: 42, name: "GPIO42", defaultPull: gpio.PullDown},
{number: 43, name: "GPIO43", defaultPull: gpio.PullDown},
{number: 44, name: "GPIO44", defaultPull: gpio.Float},
{number: 45, name: "GPIO45", defaultPull: gpio.Float},
{number: 46, name: "GPIO46", defaultPull: gpio.PullUp},
}
// This excludes the functions in and out.
var mapping = [][6]pin.Func{
{"I2C0_SDA"}, // 0
{"I2C0_SCL"},
{"I2C1_SDA"},
{"I2C1_SCL"},
{"CLK0"},
{"CLK1"}, // 5
{"CLK2"},
{"SPI0_CS1"},
{"SPI0_CS0"},
{"SPI0_MISO"},
{"SPI0_MOSI"}, // 10
{"SPI0_CLK"},
{"PWM0"},
{"PWM1"},
{"UART0_TX", "", "", "", "", "UART1_TX"},
{"UART0_RX", "", "", "", "", "UART1_RX"}, // 15
{"", "", "", "UART0_CTS", "SPI1_CS2", "UART1_CTS"},
{"", "", "", "UART0_RTS", "SPI1_CS1", "UART1_RTS"},
{"I2S_SCK", "", "", "", "SPI1_CS0", "PWM0"},
{"I2S_WS", "", "", "", "SPI1_MISO", "PWM1"},
{"I2S_DIN", "", "", "", "SPI1_MOSI", "CLK0"}, // 20
{"I2S_DOUT", "", "", "", "SPI1_CLK", "CLK1"},
{""},
{""},
{""},
{""}, // 25
{""},
{""},
{"I2C0_SDA", "", "I2S_SCK", "", "", ""},
{"I2C0_SCL", "", "I2S_WS", "", "", ""},
{"", "", "I2S_DIN", "UART0_CTS", "", "UART1_CTS"}, // 30
{"", "", "I2S_DOUT", "UART0_RTS", "", "UART1_RTS"},
{"CLK0", "", "", "UART0_TX", "", "UART1_TX"},
{"", "", "", "UART0_RX", "", "UART1_RX"},
{"CLK0"},
{"SPI0_CS1"}, // 35
{"SPI0_CS0", "", "UART0_TX", "", "", ""},
{"SPI0_MISO", "", "UART0_RX", "", "", ""},
{"SPI0_MOSI", "", "UART0_RTS", "", "", ""},
{"SPI0_CLK", "", "UART0_CTS", "", "", ""},
{"PWM0", "", "", "", "SPI2_MISO", "UART1_TX"}, // 40
{"PWM1", "", "", "", "SPI2_MOSI", "UART1_RX"},
{"CLK1", "", "", "", "SPI2_CLK", "UART1_RTS"},
{"CLK2", "", "", "", "SPI2_CS0", "UART1_CTS"},
{"CLK1", "I2C0_SDA", "I2C1_SDA", "", "SPI2_CS1", ""},
{"PWM1", "I2C0_SCL", "I2C1_SCL", "", "SPI2_CS2", ""}, // 45
{""},
}
// function specifies the active functionality of a pin. The alternative
// function is GPIO pin dependent.
type function uint8
// Mapping as
// https://www.raspberrypi.org/wp-content/uploads/2012/02/BCM2835-ARM-Peripherals.pdf
// pages 90-91.
type gpioMap struct {
// 0x00 RW GPIO Function Select 0 (GPIO0-9)
// 0x04 RW GPIO Function Select 1 (GPIO10-19)
// 0x08 RW GPIO Function Select 2 (GPIO20-29)
// 0x0C RW GPIO Function Select 3 (GPIO30-39)
// 0x10 RW GPIO Function Select 4 (GPIO40-49)
// 0x14 RW GPIO Function Select 5 (GPIO50-53)
functionSelect [6]uint32 // GPFSEL0~GPFSEL5
// 0x18 - Reserved
dummy0 uint32
// 0x1C W GPIO Pin Output Set 0 (GPIO0-31)
// 0x20 W GPIO Pin Output Set 1 (GPIO32-53)
outputSet [2]uint32 // GPSET0-GPSET1
// 0x24 - Reserved
dummy1 uint32
// 0x28 W GPIO Pin Output Clear 0 (GPIO0-31)
// 0x2C W GPIO Pin Output Clear 1 (GPIO32-53)
outputClear [2]uint32 // GPCLR0-GPCLR1
// 0x30 - Reserved
dummy2 uint32
// 0x34 R GPIO Pin Level 0 (GPIO0-31)
// 0x38 R GPIO Pin Level 1 (GPIO32-53)