]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/am65xx/pin.py
7040e251588adb19e9e81f5e22962351bfcad07d
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / am65xx / pin.py
1 # SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
2 #
3 # SPDX-License-Identifier: MIT
4 # SPDX-FileCopyrightText: 2022 Martin Schnur for Siemens AG
5 #
6 # SPDX-License-Identifier: MIT
7 """TI AM65XX pin names"""
8
9 import mraa
10
11
12 class Pin:
13     """Pins don't exist in CPython so...lets make our own!"""
14
15     # pin modes
16     IN = 0
17     OUT = 1
18     ADC = 2
19     DAC = 3
20     PWM = 4
21     # pin values
22     LOW = 0
23     HIGH = 1
24     # pin pulls
25     PULL_NONE = 0
26     PULL_UP = 1
27     PULL_DOWN = 2
28
29     id = None
30     _value = LOW
31     _mode = IN
32
33     def __init__(self, pin_id=None):
34         self.id = pin_id
35         self._mode = None
36         self._pull = None
37         self._pin = None
38
39     def __repr__(self):
40         return str(self.id)
41
42     def __eq__(self, other):
43         return self.id == other
44
45     def init(self, mode=IN, pull=None):
46         """Initialize the Pin"""
47         if self.id is None:
48             raise RuntimeError("Can not init a None type pin.")
49         if mode is not None:
50             if mode == self.IN:
51                 self._mode = self.IN
52                 mypin = mraa.Gpio(self.id)
53                 mypin.dir(mraa.DIR_IN)
54             elif mode == self.OUT:
55                 self._mode = self.OUT
56                 mypin = mraa.Gpio(self.id)
57                 mypin.dir(mraa.DIR_OUT)
58             elif mode in (self.ADC, self.DAC):
59                 # ADC (DAC not available) only available on Pin 0-5 (X12 Pin 1-6)
60                 if self.id not in (0, 1, 2, 3, 4, 5):
61                     raise ValueError("Pin does not have ADC capabilities")
62                 self._pin = mraa.Aio(self.id)
63             elif mode == self.PWM:
64                 # PWM only available on Pin 4-9 (X10 Pin 1-2, X11 Pin 5-8)
65                 if self.id not in (4, 5, 6, 7, 8, 9):
66                     raise ValueError("Pin does not have PWM capabilities")
67                 return
68             else:
69                 raise RuntimeError("Invalid mode for pin: %s" % self.id)
70             self._mode = mode
71         if pull is not None:
72             if self._mode != self.IN:
73                 raise RuntimeError("Cannot set pull resistor on output")
74             if pull == self.PULL_UP:
75                 mypin = mraa.Gpio(self.id)
76                 mypin.dir(mraa.DIR_IN)
77             elif pull == self.PULL_DOWN:
78                 mypin = mraa.Gpio(self.id)
79                 mypin.dir(mraa.DIR_IN)
80             else:
81                 raise RuntimeError("Invalid pull for pin: %s" % self.id)
82
83     def value(self, val=None):
84         """Set or return the Pin Value"""
85         # Digital In / Out
86         if self._mode in (Pin.IN, Pin.OUT):
87             if val is not None:
88                 if val == self.LOW:
89                     self._value = val
90                     mypin = mraa.Gpio(self.id)
91                     mypin.write(0)
92                 elif val == self.HIGH:
93                     self._value = val
94                     mypin = mraa.Gpio(self.id)
95                     mypin.write(1)
96                 else:
97                     raise RuntimeError("Invalid value for pin")
98                 return None
99             return mraa.Gpio.read(mraa.Gpio(self.id))
100         # Analog In
101         if self._mode == Pin.ADC:
102             if val is None:
103                 # Read ADC here
104                 mypin = mraa.Aio(self.id)
105                 mypin.read()
106                 return mypin.read()
107             # read only
108             raise AttributeError("'AnalogIn' object has no attribute 'value'")
109         # Analog Out
110         if self._mode == Pin.DAC:
111             """if val is None:
112                 # write only
113                 raise AttributeError("unreadable attribute")
114             # Set DAC here
115             mypin = mraa.Aio(self.id)
116             mypin.setBit(val)"""
117             raise AttributeError(
118                 "AM65xx doesn't have an DAC! No Analog Output possible!"
119             )
120         raise RuntimeError(
121             "No action for mode {} with value {}".format(self._mode, val)
122         )
123
124
125 # Digital Pins (GPIO 0-19)
126 D0 = Pin(0)
127 D1 = Pin(1)
128 D2 = Pin(2)
129 D3 = Pin(3)
130 D4 = Pin(4)
131 D5 = Pin(5)
132 D6 = Pin(6)
133 D7 = Pin(7)
134 D8 = Pin(8)
135 D9 = Pin(9)
136 D10 = Pin(10)
137 D11 = Pin(11)
138 D12 = Pin(12)
139 D13 = Pin(13)
140 D14 = Pin(14)
141 D15 = Pin(15)
142 D16 = Pin(16)
143 D17 = Pin(17)
144 D18 = Pin(18)
145 D19 = Pin(19)
146
147 # Analog Pins (AIO 0-5, only ADC!)
148 A0 = Pin(0)
149 A1 = Pin(1)
150 A2 = Pin(2)
151 A3 = Pin(3)
152 A4 = Pin(4)
153 A5 = Pin(5)
154
155 # I2C allocation
156 I2C_SCL = "SCL"
157 I2C_SDA = "SDA"
158
159 # SPI allocation
160 SPIO_SCLK = D13
161 SPIO_MISO = D12
162 SPIO_MOSI = D11
163 SPIO_SS = D10
164
165 # UART allocation
166 UART_TX = "TX"
167 UART_RX = "RX"
168
169 # pwmOuts (GPIO 4-9)
170 PWM_4 = D4
171 PWM_5 = D5
172 PWM_6 = D6
173 PWM_7 = D7
174 PWM_8 = D8
175 PWM_9 = D9
176
177 # I2C
178 # ordered as sclID, sdaID
179 # i2c-4 (/dev/i2c-4) -> X10 Pin9, Pin10 (SDA, SCL)
180 i2cPorts = ((4, I2C_SCL, I2C_SDA),)
181
182 # SPI
183 # ordered as spiId, sckId, mosiID, misoID
184 spiPorts = ((0, SPIO_SCLK, SPIO_MOSI, SPIO_MISO),)
185
186 # UART
187 # use pySerial = dev/ttyS1
188 # ordered as uartID, txID, rxID
189 uartPorts = ((0, UART_TX, UART_RX),)
190
191 # PWM
192 pwmOuts = (
193     ((0, 0), PWM_4),
194     ((0, 1), PWM_5),
195     ((2, 0), PWM_6),
196     ((2, 1), PWM_7),
197     ((4, 0), PWM_8),
198     ((4, 1), PWM_9),
199 )