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
11 Determines the handle of the GPIO chip device to access.
13 iterate through sysfs to find a GPIO chip device with a driver known to be
14 used for userspace GPIO access.
16 for dev in Path("/sys/bus/gpio/devices").glob("gpiochip*"):
17 drivers = set((dev / "of_node/compatible").read_text().split("\0"))
18 # check if driver names are intended for userspace control
20 "raspberrypi,rp1-gpio",
21 "raspberrypi,bcm2835-gpio",
22 "raspberrypi,bcm2711-gpio",
24 return lgpio.gpiochip_open(int(dev.name[-1]))
25 # return chip0 as a fallback
26 return lgpio.gpiochip_open(0)
29 CHIP = _get_gpiochip()
33 """Pins dont exist in CPython so...lets make our own!"""
40 # values of lg mode constants
46 # drive mode lg constants
54 _LG_MODES = IN | OUT | _LG_ALERT | _LG_GROUP
55 _LG_PULLS = PULL_NONE | PULL_UP | PULL_NONE | ACTIVE_LOW
56 _LG_DRIVES = OPEN_DRAIN
63 lgpio.exceptions = True
65 def __init__(self, bcm_number):
66 self.id = bcm_number # pylint: disable=invalid-name
71 def __eq__(self, other):
72 return self.id == other
74 def init(self, mode=IN, pull=None):
75 """Initialize the Pin"""
79 self._set_gpio_mode_in()
80 elif mode == self.OUT:
82 Pin._check_result(lgpio.gpio_claim_output(CHIP, self.id, Pin.LOW))
84 raise RuntimeError(f"Invalid mode for pin: {self.id}")
86 if self._mode != Pin.IN:
87 raise RuntimeError("Can only set pull resistor on input")
88 if pull in {Pin.PULL_UP, Pin.PULL_DOWN, Pin.PULL_NONE}:
89 self._set_gpio_mode_in(lflags=pull)
91 raise RuntimeError(f"Invalid pull for pin: {self.id}")
93 def value(self, val=None):
94 """Set or return the Pin Value"""
98 Pin._check_result(lgpio.gpio_write(CHIP, self.id, val))
101 Pin._check_result(lgpio.gpio_write(CHIP, self.id, val))
103 raise RuntimeError("Invalid value for pin")
105 return Pin._check_result(lgpio.gpio_read(CHIP, self.id))
108 def _check_result(result):
110 convert any result other than zero to a text message and pass it back
111 as a runtime exception. Typical usage: use the lgpio call as the
115 raise RuntimeError(lgpio.error_text(result))
118 def _set_gpio_mode_in(self, lflags=0):
120 claim a gpio as input, or modify the flags (PULL_UP, PULL_DOWN, ... )
122 # This gpio_free may seem redundant, but is required when
123 # changing the line-flags of an already acquired input line
125 lgpio.gpio_free(CHIP, self.id)
128 Pin._check_result(lgpio.gpio_claim_input(CHIP, self.id, lFlags=lflags))
152 SCLK = Pin(11) # Raspberry Pi naming
153 SCK = Pin(11) # CircuitPython naming
202 # ordered as spiId, sckId, mosiId, misoId
204 (0, SCLK, MOSI, MISO),
205 (1, SCLK_1, MOSI_1, MISO_1),
206 (2, SCLK_2, MOSI_2, MISO_2),
209 # ordered as uartId, txId, rxId
210 uartPorts = ((1, TXD, RXD),)
212 # These are the known hardware I2C ports / pins.
213 # For software I2C ports created with the i2c-gpio overlay, see:
214 # https://github.com/adafruit/Adafruit_Python_Extended_Bus
217 (0, D1, D0), # both pi 1 and pi 2 i2c ports!