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