]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/bcm283x/pin.py
amlogic/a311d: Import get_dts_alias and eliminate duplicate function
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / bcm283x / pin.py
1 # SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
2 #
3 # SPDX-License-Identifier: MIT
4 """Broadcom BCM283x pin names"""
5 from RPi import GPIO
6
7 GPIO.setmode(GPIO.BCM)  # Use BCM pins D4 = GPIO #4
8 GPIO.setwarnings(False)  # shh!
9
10
11 class Pin:
12     """Pins dont exist in CPython so...lets make our own!"""
13
14     IN = 0
15     OUT = 1
16     LOW = 0
17     HIGH = 1
18     PULL_NONE = 0
19     PULL_UP = 1
20     PULL_DOWN = 2
21
22     id = None
23     _value = LOW
24     _mode = IN
25
26     def __init__(self, bcm_number):
27         self.id = bcm_number
28
29     def __repr__(self):
30         return str(self.id)
31
32     def __eq__(self, other):
33         return self.id == other
34
35     def init(self, mode=IN, pull=None):
36         """Initialize the Pin"""
37         if mode is not None:
38             if mode == self.IN:
39                 self._mode = self.IN
40                 GPIO.setup(self.id, GPIO.IN)
41             elif mode == self.OUT:
42                 self._mode = self.OUT
43                 GPIO.setup(self.id, GPIO.OUT)
44             else:
45                 raise RuntimeError("Invalid mode for pin: %s" % self.id)
46         if pull is not None:
47             if self._mode != self.IN:
48                 raise RuntimeError("Cannot set pull resistor on output")
49             if pull == self.PULL_UP:
50                 GPIO.setup(self.id, GPIO.IN, pull_up_down=GPIO.PUD_UP)
51             elif pull == self.PULL_DOWN:
52                 GPIO.setup(self.id, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
53             else:
54                 raise RuntimeError("Invalid pull for pin: %s" % self.id)
55
56     def value(self, val=None):
57         """Set or return the Pin Value"""
58         if val is not None:
59             if val == self.LOW:
60                 self._value = val
61                 GPIO.output(self.id, val)
62             elif val == self.HIGH:
63                 self._value = val
64                 GPIO.output(self.id, val)
65             else:
66                 raise RuntimeError("Invalid value for pin")
67             return None
68         return GPIO.input(self.id)
69
70
71 # Pi 1B rev1 only?
72 D0 = Pin(0)
73 D1 = Pin(1)
74
75 D2 = Pin(2)
76 SDA = Pin(2)
77 D3 = Pin(3)
78 SCL = Pin(3)
79
80 D4 = Pin(4)
81 D5 = Pin(5)
82 D6 = Pin(6)
83
84 D7 = Pin(7)
85 CE1 = Pin(7)
86 D8 = Pin(8)
87 CE0 = Pin(8)
88 D9 = Pin(9)
89 MISO = Pin(9)
90 D10 = Pin(10)
91 MOSI = Pin(10)
92 D11 = Pin(11)
93 SCLK = Pin(11)  # Raspberry Pi naming
94 SCK = Pin(11)  # CircuitPython naming
95
96 D12 = Pin(12)
97 D13 = Pin(13)
98
99 D14 = Pin(14)
100 TXD = Pin(14)
101 D15 = Pin(15)
102 RXD = Pin(15)
103
104 D16 = Pin(16)
105 D17 = Pin(17)
106 D18 = Pin(18)
107 D19 = Pin(19)
108 MISO_1 = Pin(19)
109 D20 = Pin(20)
110 MOSI_1 = Pin(20)
111 D21 = Pin(21)
112 SCLK_1 = Pin(21)
113 SCK_1 = Pin(21)
114 D22 = Pin(22)
115 D23 = Pin(23)
116 D24 = Pin(24)
117 D25 = Pin(25)
118 D26 = Pin(26)
119 D27 = Pin(27)
120 D28 = Pin(28)
121 D29 = Pin(29)
122 D30 = Pin(30)
123 D31 = Pin(31)
124 D32 = Pin(32)
125 D33 = Pin(33)
126 D34 = Pin(34)
127 D35 = Pin(35)
128 D36 = Pin(36)
129 D37 = Pin(37)
130 D38 = Pin(38)
131 D39 = Pin(39)
132 D40 = Pin(40)
133 MISO_2 = Pin(40)
134 D41 = Pin(41)
135 MOSI_2 = Pin(41)
136 D42 = Pin(42)
137 SCLK_2 = Pin(42)
138 SCK_2 = Pin(43)
139 D43 = Pin(43)
140 D44 = Pin(44)
141 D45 = Pin(45)
142
143 # ordered as spiId, sckId, mosiId, misoId
144 spiPorts = (
145     (0, SCLK, MOSI, MISO),
146     (1, SCLK_1, MOSI_1, MISO_1),
147     (2, SCLK_2, MOSI_2, MISO_2),
148 )
149
150 # ordered as uartId, txId, rxId
151 uartPorts = ((1, TXD, RXD),)
152
153 # These are the known hardware I2C ports / pins.
154 # For software I2C ports created with the i2c-gpio overlay, see:
155 #     https://github.com/adafruit/Adafruit_Python_Extended_Bus
156 i2cPorts = (
157     (1, SCL, SDA),
158     (0, D1, D0),  # both pi 1 and pi 2 i2c ports!
159 )