1 # SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
3 # SPDX-License-Identifier: MIT
4 """Broadcom BCM283x pin names"""
5 from pathlib import Path
10 Determines the handle of the GPIO chip device to access.
12 iterate through sysfs to find a GPIO chip device with a driver known to be
13 used for userspace GPIO access.
15 for dev in Path('/sys/bus/gpio/devices').glob('gpiochip*'):
16 drivers = set((dev / 'of_node/compatible').read_text().split('\0'))
17 # check if driver names are intended for userspace control
18 if drivers & {'raspberrypi,rp1-gpio',
19 'raspberrypi,bcm2835-gpio',
20 'raspberrypi,bcm2711-gpio'}:
21 return lgpio.gpiochip_open(int(dev.name[-1]))
22 # return chip0 as a fallback
23 return lgpio.gpiochip_open(0)
26 CHIP = _get_gpiochip()
30 """Pins dont exist in CPython so...lets make our own!"""
37 # values of lg mode constants
43 # drive mode lg constants
51 _LG_MODES = IN | OUT | _LG_ALERT | _LG_GROUP
52 _LG_PULLS = PULL_NONE | PULL_UP | PULL_NONE | ACTIVE_LOW
53 _LG_DRIVES = OPEN_DRAIN
60 lgpio.exceptions = True
62 def __init__(self, bcm_number):
63 self.id = bcm_number # pylint: disable=invalid-name
68 def __eq__(self, other):
69 return self.id == other
71 def init(self, mode=IN, pull=None):
72 """Initialize the Pin"""
76 self._set_gpio_mode_in()
77 elif mode == self.OUT:
79 Pin._check_result(lgpio.gpio_claim_output(CHIP, self.id,
82 raise RuntimeError(f"Invalid mode for pin: {self.id}")
84 if self._mode != Pin.IN:
85 raise RuntimeError("Can only set pull resistor on input")
86 if pull in {Pin.PULL_UP, Pin.PULL_DOWN, Pin.PULL_NONE}:
87 self._set_gpio_mode_in(lflags=pull)
89 raise RuntimeError(f"Invalid pull for pin: {self.id}")
91 def value(self, val=None):
92 """Set or return the Pin Value"""
96 Pin._check_result(lgpio.gpio_write(CHIP, self.id, val))
99 Pin._check_result(lgpio.gpio_write(CHIP, self.id, val))
101 raise RuntimeError("Invalid value for pin")
103 return Pin._check_result(lgpio.gpio_read(CHIP, self.id))
106 def _check_result(result):
108 convert any result other than zero to a text message and pass it back
109 as a runtime exception. Typical usage: use the lgpio call as the
113 raise RuntimeError(lgpio.error_text(result))
116 def _set_gpio_mode_in(self, lflags=0):
118 claim a gpio as input, or modify the flags (PULL_UP, PULL_DOWN, ... )
120 # This gpio_free may seem redundant, but is required when
121 # changing the line-flags of an already acquired input line
123 lgpio.gpio_free(CHIP, self.id)
126 Pin._check_result(lgpio.gpio_claim_input(CHIP, self.id, lFlags=lflags))
150 SCLK = Pin(11) # Raspberry Pi naming
151 SCK = Pin(11) # CircuitPython naming
200 # ordered as spiId, sckId, mosiId, misoId
202 (0, SCLK, MOSI, MISO),
203 (1, SCLK_1, MOSI_1, MISO_1),
204 (2, SCLK_2, MOSI_2, MISO_2),
207 # ordered as uartId, txId, rxId
208 uartPorts = ((1, TXD, RXD),)
210 # These are the known hardware I2C ports / pins.
211 # For software I2C ports created with the i2c-gpio overlay, see:
212 # https://github.com/adafruit/Adafruit_Python_Extended_Bus
215 (0, D1, D0), # both pi 1 and pi 2 i2c ports!