@@ -75,6 +75,8 @@ import (
75
75
"syscall"
76
76
"time"
77
77
"unsafe"
78
+ "io/ioutil"
79
+ "fmt"
78
80
)
79
81
80
82
type Mode uint8
@@ -625,6 +627,38 @@ func backupIRQs() {
625
627
irqsBackup = uint64 (intrMem [irqEnable2 ])<< 32 | uint64 (intrMem [irqEnable1 ])
626
628
}
627
629
630
+ var pin_to_gpio_rev1 = [26 ]int {- 1 , - 1 , 0 , - 1 , 1 , - 1 , 4 , 14 , - 1 , 15 , 17 , 18 , 21 , - 1 , 22 , 23 , - 1 , 24 , 10 , - 1 , 9 , 25 , 11 , 8 , - 1 , 7 };
631
+ var pin_to_gpio_rev2 = [40 ]int {- 1 , - 1 , 2 , - 1 , 3 , - 1 , 4 , 14 , - 1 , 15 , 17 , 18 , 27 , - 1 , 22 , 23 , - 1 , 24 , 10 , - 1 , 9 , 25 , 11 , 8 , - 1 , 7 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 , - 1 };
632
+ var pin_to_gpio_rev3 = [40 ]int {- 1 , - 1 , 2 , - 1 , 3 , - 1 , 4 , 14 , - 1 , 15 , 17 , 18 , 27 , - 1 , 22 , 23 , - 1 , 24 , 10 , - 1 , 9 , 25 , 11 , 8 , - 1 , 7 , - 1 , - 1 , 5 , - 1 , 6 , 12 , 13 , - 1 , 19 , 16 , 26 , 20 , - 1 , 21 };
633
+
634
+ var pin_to_gpio []int = nil ;
635
+ func initGPIOconversion () (error ) {
636
+ file , err := os .Open ("/sys/firmware/devicetree/base/model" )
637
+ if err != nil {
638
+ return err
639
+ }
640
+
641
+ defer file .Close ()
642
+
643
+ model , err := ioutil .ReadAll (file )
644
+ if err != nil {
645
+ return err
646
+ }
647
+
648
+ switch string (bytes .TrimRight (model , "\000 " )) {
649
+ case "Raspberry Pi Model B Rev 1" :
650
+ pin_to_gpio = pin_to_gpio_rev1 [0 :26 ];
651
+ case "Raspberry Pi Model B Rev 2" :
652
+ pin_to_gpio = pin_to_gpio_rev2 [0 :40 ];
653
+ case "Raspberry Pi Model A Rev 2" :
654
+ pin_to_gpio = pin_to_gpio_rev2 [0 :40 ];
655
+ default :
656
+ pin_to_gpio = pin_to_gpio_rev3 [0 :40 ];
657
+ }
658
+
659
+ return nil
660
+ }
661
+
628
662
// Open and memory map GPIO memory range from /dev/mem .
629
663
// Some reflection magic is used to convert it to a unsafe []uint32 pointer
630
664
func Open () (err error ) {
@@ -676,6 +710,7 @@ func Open() (err error) {
676
710
677
711
backupIRQs () // back up enabled IRQs, to restore it later
678
712
713
+ initGPIOconversion ()
679
714
return nil
680
715
}
681
716
@@ -734,3 +769,12 @@ func getBase() (base int64) {
734
769
}
735
770
return int64 (out )
736
771
}
772
+
773
+ // Convert GPIO number from physical PIN to BCM convention
774
+ func GetBoardPin (board_pin int ) (Pin , error ) {
775
+ if board_pin < 1 || len (pin_to_gpio ) < board_pin || pin_to_gpio [board_pin - 1 ] == - 1 {
776
+ return Pin (0xff ), fmt .Errorf ("Pin %d is not wired" , board_pin )
777
+ }
778
+
779
+ return Pin (pin_to_gpio [board_pin - 1 ]), nil
780
+ }
0 commit comments