From: Carter Nelson Date: Fri, 7 May 2021 20:46:56 +0000 (-0700) Subject: Merge pull request #436 from renpytom/master X-Git-Tag: 6.8.1~1 X-Git-Url: https://git.ayoreis.com/Adafruit_Blinka-hackapet.git/commitdiff_plain/12f887ab8527a910ad423c3eb97d65f41d4d3e1a?hp=9b25bf6b88354b753aedeeec3699be45539a1e07 Merge pull request #436 from renpytom/master Add support for ftdi URLs in the BLINKA_FT232H variable. --- diff --git a/README.rst b/README.rst index 40df1a9..d802c16 100755 --- a/README.rst +++ b/README.rst @@ -17,16 +17,19 @@ Introduction :target: https://github.com/psf/black :alt: Code Style: Black -This repository contains a selection of packages mirroring the CircuitPython API -on hosts running micropython. Working code exists to emulate the CircuitPython packages; +This repository contains a selection of packages emulating the CircuitPython API +for devices or hosts running CPython or MicroPython. Working code exists to emulate these CircuitPython packages: -* **board** - breakout-specific pin identities -* **microcontroller** - chip-specific pin identities * **analogio** - analog input/output pins, using pin identities from board+microcontroller packages -* **digitalio** - digital input/output pins, using pin identities from board+microcontroller packages * **bitbangio** - software-driven interfaces for I2C, SPI +* **board** - breakout-specific pin identities * **busio** - hardware-driven interfaces for I2C, SPI, UART +* **digitalio** - digital input/output pins, using pin identities from board+microcontroller packages +* **microcontroller** - chip-specific pin identities +* **micropython** - MicroPython-specific module +* **neopixel_write** - low-level interface to NeoPixels * **pulseio** - contains classes that provide access to basic pulse IO (PWM) +* **pwmio** - contains classes that provide access to basic pulse IO (PWM) For details, see the `Blinka API reference `_. @@ -34,12 +37,14 @@ For details, see the `Blinka API reference Dependencies ============= -The Micropython compatibility layers described above are intended to provide a CircuitPython-like API for devices which -are running CPython or Micropython. Since corresponding packages should be built-in to any standard -CircuitPython image, they have no value on a device already running CircuitPython and would likely conflict in unhappy ways. +The emulation described above is intended to provide a +CircuitPython-like API for devices which are running CPython or +Micropython. Since corresponding packages should be built-in to any +standard CircuitPython image, they have no value on a device already +running CircuitPython and would likely conflict in unhappy ways. The test suites in the test/src folder under **testing.universal** are by design -intended to run on *either* CircuitPython *or* Micropython+compatibility layer to prove conformance. +intended to run on *either* CircuitPython *or* CPython/Micropython+compatibility layer to prove conformance. Installing from PyPI ===================== diff --git a/src/adafruit_blinka/board/lubancat/lubancat_stm32mp157.py b/src/adafruit_blinka/board/lubancat/lubancat_stm32mp157.py new file mode 100644 index 0000000..33dfdcd --- /dev/null +++ b/src/adafruit_blinka/board/lubancat/lubancat_stm32mp157.py @@ -0,0 +1,49 @@ +"""Pin definitions for the LubanCat STM32MP157.""" + +from adafruit_blinka.microcontroller.stm32.stm32mp157 import pin + +# Pro board pin header J1 named GPIO_PAx, pin header J2 named GPIO_PBx + +# Board pin name [= alias] = RPI name [= alias] = pin name + +# connector J1 +GPIO_PA3 = USART1_CTS = PZ3 = pin.PZ3 +GPIO_PA4 = USART1_RTS = PZ5 = pin.PZ5 +GPIO_PA5 = USART1_TX = PZ7 = pin.PZ7 +GPIO_PA6 = USART1_RX = PZ6 = pin.PZ6 +GPIO_PA7 = USART3_TX = PB10 = pin.PB10 +GPIO_PA8 = USART3_RX = PB12 = pin.PB12 +GPIO_PA11 = FDCAN1_TX = PA12 = pin.PA12 +GPIO_PA12 = FDCAN1_RX = PA11 = pin.PA11 +# connector J2 +GPIO_PB7 = UART4_TX = PG11 = pin.PG11 +GPIO_PB8 = UART4_RX = PB2 = pin.PB2 +GPIO_PB11 = QSPI_IO0 = PF8 = pin.PF8 +GPIO_PB12 = QSPI_IO1 = PF9 = pin.PF9 +GPIO_PB13 = QSPI_IO2 = PF7 = pin.PF7 +GPIO_PB14 = QSPI_IO3 = PF6 = pin.PF6 +GPIO_PB15 = QSPI_CLK = PF10 = pin.PF10 +GPIO_PB16 = QSPI_NCS = PB6 = pin.PB6 + +# general gpio as LED、KEY function +# LED +LED_RED = PA13 = pin.PA13 +LED_GREEN = PB5 = pin.PG2 +LED_BLUE = PB5 = pin.PB5 +# KEY +KEY1 = PB13 = pin.PB13 +KEY2 = PH7 = pin.PH7 +# BEEP +BEEP = PC13 = pin.PC13 + +# general gpio as I2C function +# I2C1 +GPIO_PA13 = I2C1_SCL = SCL1 = SCL = pin.PF14 +GPIO_PA14 = I2C1_SDA = SDA1 = SDA = pin.PF15 +# I2C2 +GPIO_PA15 = I2C2_SCL = SCL2 = pin.PZ0 +GPIO_PA16 = I2C2_SDA = SDA2 = pin.PZ1 + +# general gpio as analog input function +GPIO_PB3 = ADC_IN0 = ANA0 = A0 = pin.PAN0 +GPIO_PB4 = ADC_IN1 = ANA1 = A1 = pin.PAN1 diff --git a/src/adafruit_blinka/board/pico_u2if.py b/src/adafruit_blinka/board/pico_u2if.py new file mode 100644 index 0000000..bcdfe47 --- /dev/null +++ b/src/adafruit_blinka/board/pico_u2if.py @@ -0,0 +1,46 @@ +"""Pin definitions for the MicroChip MCP2221""" +from adafruit_blinka.microcontroller.pico_u2if import pin + +GP0 = pin.GP0 +GP1 = pin.GP1 +GP2 = pin.GP2 +GP3 = pin.GP3 +GP4 = pin.GP4 +GP5 = pin.GP5 +GP6 = pin.GP6 +GP7 = pin.GP7 +GP8 = pin.GP8 +GP9 = pin.GP9 +GP10 = pin.GP10 +GP11 = pin.GP11 +GP12 = pin.GP12 +GP13 = pin.GP13 +GP14 = pin.GP14 +GP15 = pin.GP15 +GP16 = pin.GP16 +GP17 = pin.GP17 +GP18 = pin.GP18 +GP19 = pin.GP19 +GP20 = pin.GP20 +GP21 = pin.GP21 +GP22 = pin.GP22 +GP26 = pin.GP26 +GP27 = pin.GP27 +GP28 = pin.GP28 + +ADC0 = GP26 +ADC1 = GP27 + +SCL = SCL0 = GP5 +SDA = SDA0 = GP4 + +SCL1 = GP15 +SDA1 = GP14 + +SCLK = SCK = SCLK0 = SCK0 = GP18 +MOSI = MOSI0 = GP19 +MISO = MISO0 = GP12 + +SCLK1 = SCK1 = GP10 +MOSI1 = GP11 +MISO1 = GP12 diff --git a/src/adafruit_blinka/board/raspberrypi/pico.py b/src/adafruit_blinka/board/raspberrypi/pico.py new file mode 100755 index 0000000..e554ad1 --- /dev/null +++ b/src/adafruit_blinka/board/raspberrypi/pico.py @@ -0,0 +1,41 @@ +"""Raspberry Pi Pico pin names""" + +from adafruit_blinka.microcontroller.rp2040 import pin + +GP0 = pin.GP0 +GP1 = pin.GP1 +GP2 = pin.GP2 +GP3 = pin.GP3 +GP4 = pin.GP4 +GP5 = pin.GP5 +GP6 = pin.GP6 +GP7 = pin.GP7 +GP8 = pin.GP8 +GP9 = pin.GP9 +GP10 = pin.GP10 +GP11 = pin.GP11 +GP12 = pin.GP12 +GP13 = pin.GP13 +GP14 = pin.GP14 +GP15 = pin.GP15 +GP16 = pin.GP16 +GP17 = pin.GP17 +GP18 = pin.GP18 +GP19 = pin.GP19 +GP20 = pin.GP20 +GP21 = pin.GP21 +GP22 = pin.GP22 +GP23 = pin.GP23 +GP24 = pin.GP24 +GP25 = pin.GP25 +GP26 = pin.GP26 +GP27 = pin.GP27 +GP28 = pin.GP28 +LED = GP25 +SMPS_MODE = GP23 +VBUS_SENSE = GP24 +A0 = GP26 +A1 = GP27 +A2 = GP28 +A3 = pin.GP29 +VOLTAGE_MONITOR = pin.GP29 diff --git a/src/adafruit_blinka/microcontroller/allwinner/h6/pin.py b/src/adafruit_blinka/microcontroller/allwinner/h6/pin.py index af944e3..7b0a723 100644 --- a/src/adafruit_blinka/microcontroller/allwinner/h6/pin.py +++ b/src/adafruit_blinka/microcontroller/allwinner/h6/pin.py @@ -39,8 +39,8 @@ SPI1_MISO = PH6 PH8 = Pin((1, 230)) PH9 = Pin((1, 231)) -PL8 = Pin((1, 360)) -PL9 = Pin((1, 361)) +PL8 = Pin((0, 8)) +PL9 = Pin((0, 9)) i2cPorts = ((0, TWI0_SCL, TWI0_SDA),) spiPorts = ((0, SPI1_SCLK, SPI1_MOSI, SPI1_MISO),) diff --git a/src/adafruit_blinka/microcontroller/allwinner/h616/pin.py b/src/adafruit_blinka/microcontroller/allwinner/h616/pin.py index 160aee2..5c046d1 100644 --- a/src/adafruit_blinka/microcontroller/allwinner/h616/pin.py +++ b/src/adafruit_blinka/microcontroller/allwinner/h616/pin.py @@ -2,7 +2,7 @@ from adafruit_blinka.microcontroller.generic_linux.libgpiod_pin import Pin PC0 = Pin((1, 64)) -SPI0_CLK = PC0 +SPI0_SCLK = PC0 PC1 = Pin((1, 65)) PC2 = Pin((1, 66)) SPI0_MOSI = PC2 @@ -65,7 +65,7 @@ TWI3_SDA = PH5 SPI1_CS0 = PH5 PH6 = Pin((1, 230)) UART2_RX = PH6 -SPI1_CLK = PH6 +SPI1_SCLK = PH6 PH7 = Pin((1, 231)) SPI1_MOSI = PH7 PH8 = Pin((1, 232)) @@ -95,8 +95,8 @@ PI16 = Pin((1, 272)) i2cPorts = ((3, TWI3_SCL, TWI3_SDA),) # ordered as spiId, sckId, mosiId, misoId spiPorts = ( - (0, SPI0_CLK, SPI0_MOSI, SPI0_MISO), - (1, SPI1_CLK, SPI1_MOSI, SPI1_MISO), + (0, SPI0_SCLK, SPI0_MOSI, SPI0_MISO), + (1, SPI1_SCLK, SPI1_MOSI, SPI1_MISO), ) # ordered as uartId, txId, rxId uartPorts = ( diff --git a/src/adafruit_blinka/microcontroller/bcm283x/pin.py b/src/adafruit_blinka/microcontroller/bcm283x/pin.py index 5022366..48eb0c0 100644 --- a/src/adafruit_blinka/microcontroller/bcm283x/pin.py +++ b/src/adafruit_blinka/microcontroller/bcm283x/pin.py @@ -153,4 +153,5 @@ uartPorts = ((1, TXD, RXD),) i2cPorts = ( (1, SCL, SDA), (0, D1, D0), # both pi 1 and pi 2 i2c ports! + (10, D45, D44), # internal i2c bus for the CM4 ) diff --git a/src/adafruit_blinka/microcontroller/bcm283x/pulseio/PulseIn.py b/src/adafruit_blinka/microcontroller/bcm283x/pulseio/PulseIn.py index 19dc98f..fd9febe 100644 --- a/src/adafruit_blinka/microcontroller/bcm283x/pulseio/PulseIn.py +++ b/src/adafruit_blinka/microcontroller/bcm283x/pulseio/PulseIn.py @@ -25,7 +25,7 @@ def final(): atexit.register(final) -# pylint: disable=c-extension-no-member +# pylint: disable=c-extension-no-member, consider-using-with class PulseIn: """PulseIn Class to read PWM signals""" diff --git a/src/adafruit_blinka/microcontroller/nova/__init__.py b/src/adafruit_blinka/microcontroller/nova/__init__.py index 8ab7ce0..d9b7f75 100644 --- a/src/adafruit_blinka/microcontroller/nova/__init__.py +++ b/src/adafruit_blinka/microcontroller/nova/__init__.py @@ -8,13 +8,13 @@ class Connection: @staticmethod def getInstance(): - """ Static access method. """ + """Static access method.""" if Connection.__instance is None: Connection() return Connection.__instance def __init__(self): - """ Virtually private constructor. """ + """Virtually private constructor.""" if Connection.__instance is not None: raise Exception("This class is a singleton!") diff --git a/src/adafruit_blinka/microcontroller/pico_u2if/__init__.py b/src/adafruit_blinka/microcontroller/pico_u2if/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/adafruit_blinka/microcontroller/pico_u2if/analogio.py b/src/adafruit_blinka/microcontroller/pico_u2if/analogio.py new file mode 100644 index 0000000..2ae480f --- /dev/null +++ b/src/adafruit_blinka/microcontroller/pico_u2if/analogio.py @@ -0,0 +1,35 @@ +""" +`analogio` - Analog input and output control +================================================= +See `CircuitPython:analogio` in CircuitPython for more details. +* Author(s): Carter Nelson +""" +from adafruit_blinka import ContextManaged +from .pico_u2if import pico_u2if + + +class AnalogIn(ContextManaged): + """Analog Input Class""" + + def __init__(self, pin): + # per their pinout, why only two? + if pin.id not in (26, 27): + raise ValueError("Pin does not support ADC.") + self.pin_id = pin.id + pico_u2if.adc_init_pin(self.pin_id) + + @property + def value(self): + """Read the ADC and return the value""" + return pico_u2if.adc_get_value(self.pin_id) << 4 + + # pylint: disable=no-self-use + @value.setter + def value(self, value): + # emulate what CircuitPython does + raise AttributeError("'AnalogIn' object has no attribute 'value'") + + # pylint: enable=no-self-use + + def deinit(self): + pass diff --git a/src/adafruit_blinka/microcontroller/pico_u2if/i2c.py b/src/adafruit_blinka/microcontroller/pico_u2if/i2c.py new file mode 100644 index 0000000..ee393bb --- /dev/null +++ b/src/adafruit_blinka/microcontroller/pico_u2if/i2c.py @@ -0,0 +1,62 @@ +"""I2C Class for Pico u2if""" +from .pico_u2if import pico_u2if + + +class I2C: + """Custom I2C Class for Pico u2if""" + + def __init__(self, scl, sda, *, frequency=100000): + index = None + if scl.id == 5 and sda.id == 4: + index = 0 + if scl.id == 15 and sda.id == 14: + index = 1 + if index is None: + raise ValueError("I2C not found on specified pins.") + self._index = index + pico_u2if.i2c_set_port(self._index) + pico_u2if.i2c_configure(frequency) + + def scan(self): + """Perform an I2C Device Scan""" + pico_u2if.i2c_set_port(self._index) + return pico_u2if.i2c_scan() + + # pylint: disable=unused-argument + def writeto(self, address, buffer, *, start=0, end=None, stop=True): + """Write data from the buffer to an address""" + pico_u2if.i2c_set_port(self._index) + pico_u2if.i2c_writeto(address, buffer, start=start, end=end) + + def readfrom_into(self, address, buffer, *, start=0, end=None, stop=True): + """Read data from an address and into the buffer""" + pico_u2if.i2c_set_port(self._index) + pico_u2if.i2c_readfrom_into(address, buffer, start=start, end=end) + + def writeto_then_readfrom( + self, + address, + buffer_out, + buffer_in, + *, + out_start=0, + out_end=None, + in_start=0, + in_end=None, + stop=False + ): + """Write data from buffer_out to an address and then + read data from an address and into buffer_in + """ + pico_u2if.i2c_set_port(self._index) + pico_u2if.i2c_writeto_then_readfrom( + address, + buffer_out, + buffer_in, + out_start=out_start, + out_end=out_end, + in_start=in_start, + in_end=in_end, + ) + + # pylint: enable=unused-argument diff --git a/src/adafruit_blinka/microcontroller/pico_u2if/neopixel.py b/src/adafruit_blinka/microcontroller/pico_u2if/neopixel.py new file mode 100644 index 0000000..4953c2d --- /dev/null +++ b/src/adafruit_blinka/microcontroller/pico_u2if/neopixel.py @@ -0,0 +1,17 @@ +"""NeoPixel write for Pico u2if.""" + +from .pico_u2if import pico_u2if + + +def neopixel_write(gpio, buf): + """NeoPixel Writing Function""" + + # pad output buffer from 3 bpp to 4 bpp + buffer = [] + for i in range(0, len(buf), 3): + buffer.append(0) + buffer.append(buf[i + 2]) + buffer.append(buf[i + 1]) + buffer.append(buf[i]) + + pico_u2if.neopixel_write(gpio, buffer) diff --git a/src/adafruit_blinka/microcontroller/pico_u2if/pico_u2if.py b/src/adafruit_blinka/microcontroller/pico_u2if/pico_u2if.py new file mode 100644 index 0000000..687bfc0 --- /dev/null +++ b/src/adafruit_blinka/microcontroller/pico_u2if/pico_u2if.py @@ -0,0 +1,492 @@ +"""Chip Definition for Pico with u2if firmware""" +# https://github.com/execuc/u2if + +import os +import time +import hid + +# Use to set delay between reset and device reopen. if negative, don't reset at all +PICO_U2IF_RESET_DELAY = float(os.environ.get("PICO_U2IF_RESET_DELAY", 1)) + +# pylint: disable=import-outside-toplevel,too-many-branches,too-many-statements +# pylint: disable=too-many-arguments,too-many-function-args, too-many-public-methods + + +class Pico_u2if: + """MCP2221 Device Class Definition""" + + VID = 0xCAFE + PID = 0x4005 + + # MISC + RESP_OK = 0x01 + SYS_RESET = 0x10 + + # GPIO + GPIO_INIT_PIN = 0x20 + GPIO_SET_VALUE = 0x21 + GPIO_GET_VALUE = 0x22 + + # ADC + ADC_INIT_PIN = 0x40 + ADC_GET_VALUE = 0x41 + + # I2C + I2C0_INIT = 0x80 + I2C0_DEINIT = 0x81 + I2C0_WRITE = 0x82 + I2C0_READ = 0x83 + I2C0_WRITE_FROM_UART = 0x84 + I2C1_INIT = I2C0_INIT + 0x10 + I2C1_DEINIT = I2C0_DEINIT + 0x10 + I2C1_WRITE = I2C0_WRITE + 0x10 + I2C1_READ = I2C0_READ + 0x10 + I2C1_WRITE_FROM_UART = I2C0_WRITE_FROM_UART + 0x10 + + # SPI + SPI0_INIT = 0x60 + SPI0_DEINIT = 0x61 + SPI0_WRITE = 0x62 + SPI0_READ = 0x63 + SPI0_WRITE_FROM_UART = 0x64 + SPI1_INIT = SPI0_INIT + 0x10 + SPI1_DEINIT = SPI0_DEINIT + 0x10 + SPI1_WRITE = SPI0_WRITE + 0x10 + SPI1_READ = SPI0_READ + 0x10 + SPI1_WRITE_FROM_UART = SPI0_WRITE_FROM_UART + 0x10 + + # WS2812B (LED) + WS2812B_INIT = 0xA0 + WS2812B_DEINIT = 0xA1 + WS2812B_WRITE = 0xA2 + + # PWM + PWM_INIT_PIN = 0x30 + PWM_DEINIT_PIN = 0x31 + PWM_SET_FREQ = 0x32 + PWM_GET_FREQ = 0x33 + PWM_SET_DUTY_U16 = 0x34 + PWM_GET_DUTY_U16 = 0x35 + PWM_SET_DUTY_NS = 0x36 + PWM_GET_DUTY_NS = 0x37 + + def __init__(self): + self._hid = hid.device() + self._hid.open(Pico_u2if.VID, Pico_u2if.PID) + if PICO_U2IF_RESET_DELAY >= 0: + self._reset() + self._i2c_index = None + self._spi_index = None + self._serial = None + self._neopixel_initialized = False + self._uart_rx_buffer = None + + def _hid_xfer(self, report, response=True): + """Perform HID Transfer""" + # first byte is report ID, which =0 + # remaing bytes = 64 byte report data + # https://github.com/libusb/hidapi/blob/083223e77952e1ef57e6b77796536a3359c1b2a3/hidapi/hidapi.h#L185 + self._hid.write(b"\0" + report + b"\0" * (64 - len(report))) + if response: + # return is 64 byte response report + return self._hid.read(64) + return None + + def _reset(self): + self._hid_xfer(bytes([self.SYS_RESET]), False) + time.sleep(PICO_U2IF_RESET_DELAY) + start = time.monotonic() + while time.monotonic() - start < 5: + try: + self._hid.open(Pico_u2if.VID, Pico_u2if.PID) + except OSError: + time.sleep(0.1) + continue + return + raise OSError("Pico open error.") + + # ---------------------------------------------------------------- + # GPIO + # ---------------------------------------------------------------- + def gpio_init_pin(self, pin_id, direction, pull): + """Configure GPIO Pin.""" + self._hid_xfer( + bytes( + [ + self.GPIO_INIT_PIN, + pin_id, + direction, + pull, + ] + ) + ) + + def gpio_set_pin(self, pin_id, value): + """Set Current GPIO Pin Value""" + self._hid_xfer( + bytes( + [ + self.GPIO_SET_VALUE, + pin_id, + int(value), + ] + ) + ) + + def gpio_get_pin(self, pin_id): + """Get Current GPIO Pin Value""" + resp = self._hid_xfer( + bytes( + [ + self.GPIO_GET_VALUE, + pin_id, + ] + ), + True, + ) + return resp[3] != 0x00 + + # ---------------------------------------------------------------- + # ADC + # ---------------------------------------------------------------- + def adc_init_pin(self, pin_id): + """Configure ADC Pin.""" + self._hid_xfer( + bytes( + [ + self.ADC_INIT_PIN, + pin_id, + ] + ) + ) + + def adc_get_value(self, pin_id): + """Get ADC value for pin.""" + resp = self._hid_xfer( + bytes( + [ + self.ADC_GET_VALUE, + pin_id, + ] + ), + True, + ) + return int.from_bytes(resp[3 : 3 + 2], byteorder="little") + + # ---------------------------------------------------------------- + # I2C + # ---------------------------------------------------------------- + def i2c_configure(self, baudrate, pullup=False): + """Configure I2C.""" + if self._i2c_index is None: + raise RuntimeError("I2C bus not initialized.") + + resp = self._hid_xfer( + bytes( + [ + self.I2C0_INIT if self._i2c_index == 0 else self.I2C1_INIT, + 0x00 if not pullup else 0x01, + ] + ) + + baudrate.to_bytes(4, byteorder="little"), + True, + ) + if resp[1] != self.RESP_OK: + raise RuntimeError("I2C init error.") + + def i2c_set_port(self, index): + """Set I2C port.""" + if index not in (0, 1): + raise ValueError("I2C index must be 0 or 1.") + self._i2c_index = index + + def _i2c_write(self, address, buffer, start=0, end=None, stop=True): + """Write data from the buffer to an address""" + if self._i2c_index is None: + raise RuntimeError("I2C bus not initialized.") + + end = end if end else len(buffer) + + write_cmd = self.I2C0_WRITE if self._i2c_index == 0 else self.I2C1_WRITE + stop_flag = 0x01 if stop else 0x00 + + while (end - start) > 0: + remain_bytes = end - start + chunk = min(remain_bytes, 64 - 7) + resp = self._hid_xfer( + bytes([write_cmd, address, stop_flag]) + + remain_bytes.to_bytes(4, byteorder="little") + + buffer[start : (start + chunk)], + True, + ) + if resp[1] != self.RESP_OK: + raise RuntimeError("I2C write error") + start += chunk + + def _i2c_read(self, address, buffer, start=0, end=None): + """Read data from an address and into the buffer""" + # TODO: support chunkified reads + if self._i2c_index is None: + raise RuntimeError("I2C bus not initialized.") + + end = end if end else len(buffer) + + read_cmd = self.I2C0_READ if self._i2c_index == 0 else self.I2C1_READ + stop_flag = 0x01 # always stop + read_size = end - start + + resp = self._hid_xfer(bytes([read_cmd, address, stop_flag, read_size]), True) + if resp[1] != self.RESP_OK: + raise RuntimeError("I2C write error") + # move into buffer + for i in range(read_size): + buffer[start + i] = resp[i + 2] + + def i2c_writeto(self, address, buffer, *, start=0, end=None): + """Write data from the buffer to an address""" + self._i2c_write(address, buffer, start, end) + + def i2c_readfrom_into(self, address, buffer, *, start=0, end=None): + """Read data from an address and into the buffer""" + self._i2c_read(address, buffer, start, end) + + def i2c_writeto_then_readfrom( + self, + address, + out_buffer, + in_buffer, + *, + out_start=0, + out_end=None, + in_start=0, + in_end=None + ): + """Write data from buffer_out to an address and then + read data from an address and into buffer_in + """ + self._i2c_write(address, out_buffer, out_start, out_end, False) + self._i2c_read(address, in_buffer, in_start, in_end) + + def i2c_scan(self, *, start=0, end=0x79): + """Perform an I2C Device Scan""" + if self._i2c_index is None: + raise RuntimeError("I2C bus not initialized.") + found = [] + for addr in range(start, end + 1): + # try a write + try: + self.i2c_writeto(addr, b"\x00\x00\x00") + except RuntimeError: # no reply! + continue + # store if success + found.append(addr) + return found + + # ---------------------------------------------------------------- + # SPI + # ---------------------------------------------------------------- + def spi_configure(self, baudrate): + """Configure SPI.""" + if self._spi_index is None: + raise RuntimeError("SPI bus not initialized.") + + resp = self._hid_xfer( + bytes( + [ + self.SPI0_INIT if self._spi_index == 0 else self.SPI1_INIT, + 0x00, # mode, not yet implemented + ] + ) + + baudrate.to_bytes(4, byteorder="little"), + True, + ) + if resp[1] != self.RESP_OK: + raise RuntimeError("SPI init error.") + + def spi_set_port(self, index): + """Set SPI port.""" + if index not in (0, 1): + raise ValueError("SPI index must be 0 or 1.") + self._spi_index = index + + def spi_write(self, buffer, *, start=0, end=None): + """SPI write.""" + if self._spi_index is None: + raise RuntimeError("SPI bus not initialized.") + + end = end if end else len(buffer) + + write_cmd = self.SPI0_WRITE if self._spi_index == 0 else self.SPI1_WRITE + + while (end - start) > 0: + remain_bytes = end - start + chunk = min(remain_bytes, 64 - 3) + resp = self._hid_xfer( + bytes([write_cmd, chunk]) + buffer[start : (start + chunk)], True + ) + if resp[1] != self.RESP_OK: + raise RuntimeError("SPI write error") + start += chunk + + def spi_readinto(self, buffer, *, start=0, end=None, write_value=0): + """SPI readinto.""" + if self._spi_index is None: + raise RuntimeError("SPI bus not initialized.") + + end = end if end else len(buffer) + read_cmd = self.SPI0_READ if self._spi_index == 0 else self.SPI1_READ + read_size = end - start + + resp = self._hid_xfer(bytes([read_cmd, write_value, read_size]), True) + if resp[1] != self.RESP_OK: + raise RuntimeError("SPI write error") + # move into buffer + for i in range(read_size): + buffer[start + i] = resp[i + 2] + + def spi_write_readinto( + self, + buffer_out, + buffer_in, + *, + out_start=0, + out_end=None, + in_start=0, + in_end=None + ): + """SPI write and readinto.""" + raise NotImplementedError("SPI write_readinto Not implemented") + + # ---------------------------------------------------------------- + # NEOPIXEL + # ---------------------------------------------------------------- + def neopixel_write(self, gpio, buf): + """NeoPixel write.""" + # open serial (data is sent over this) + if self._serial is None: + import serial + import serial.tools.list_ports + + ports = serial.tools.list_ports.comports() + for port in ports: + if port.vid == self.VID and port.pid == self.PID: + self._serial = serial.Serial(port.device) + break + if self._serial is None: + raise RuntimeError("Could not find Pico com port.") + + # init + if not self._neopixel_initialized: + # deinit any current setup + # pylint: disable=protected-access + self._hid_xfer(bytes([self.WS2812B_DEINIT])) + resp = self._hid_xfer( + bytes( + [ + self.WS2812B_INIT, + gpio._pin.id, + ] + ), + True, + ) + if resp[1] != self.RESP_OK: + raise RuntimeError("Neopixel init error") + self._neopixel_initialized = True + + self._serial.reset_output_buffer() + + # write + # command is done over HID + remain_bytes = len(buf) + resp = self._hid_xfer( + bytes([self.WS2812B_WRITE]) + remain_bytes.to_bytes(4, byteorder="little"), + True, + ) + if resp[1] != self.RESP_OK: + # pylint: disable=no-else-raise + if resp[2] == 0x01: + raise RuntimeError( + "Neopixel write error : too many pixel for the firmware." + ) + elif resp[2] == 0x02: + print(resp[0:10]) + raise RuntimeError( + "Neopixel write error : transfer already in progress." + ) + else: + raise RuntimeError("Neopixel write error.") + # buffer is sent over serial + self._serial.write(buf) + # hack (see u2if) + if len(buf) % 64 == 0: + self._serial.write([0]) + self._serial.flush() + # polling loop to wait for write complete? + resp = self._hid.read(64) + while resp[0] != self.WS2812B_WRITE: + resp = self._hid.read(64) + if resp[1] != self.RESP_OK: + raise RuntimeError("Neopixel write (flush) error.") + + # ---------------------------------------------------------------- + # PWM + # ---------------------------------------------------------------- + # pylint: disable=unused-argument + def pwm_configure(self, pin, frequency=500, duty_cycle=0, variable_frequency=False): + """Configure PWM.""" + self.pwm_deinit(pin) + resp = self._hid_xfer(bytes([self.PWM_INIT_PIN, pin.id]), True) + if resp[1] != self.RESP_OK: + raise RuntimeError("PWM init error.") + + self.pwm_set_frequency(pin, frequency) + self.pwm_set_duty_cycle(pin, duty_cycle) + + def pwm_deinit(self, pin): + """Deinit PWM.""" + self._hid_xfer(bytes([self.PWM_DEINIT_PIN, pin.id])) + + def pwm_get_frequency(self, pin): + """PWM get freq.""" + resp = self._hid_xfer(bytes([self.PWM_GET_FREQ, pin.id]), True) + if resp[1] != self.RESP_OK: + raise RuntimeError("PWM get frequency error.") + return int.from_bytes(resp[3 : 3 + 4], byteorder="little") + + def pwm_set_frequency(self, pin, frequency): + """PWM set freq.""" + resp = self._hid_xfer( + bytes([self.PWM_SET_FREQ, pin.id]) + + frequency.to_bytes(4, byteorder="little"), + True, + ) + if resp[1] != self.RESP_OK: + # pylint: disable=no-else-raise + if resp[3] == 0x01: + raise RuntimeError("PWM different frequency on same slice.") + elif resp[3] == 0x02: + raise RuntimeError("PWM frequency too low.") + elif resp[3] == 0x03: + raise RuntimeError("PWM frequency too high.") + else: + raise RuntimeError("PWM frequency error.") + + def pwm_get_duty_cycle(self, pin): + """PWM get duty cycle.""" + resp = self._hid_xfer(bytes([self.PWM_GET_DUTY_U16, pin.id]), True) + if resp[1] != self.RESP_OK: + raise RuntimeError("PWM get duty cycle error.") + return int.from_bytes(resp[3 : 3 + 4], byteorder="little") + + def pwm_set_duty_cycle(self, pin, duty_cycle): + """PWM set duty cycle.""" + resp = self._hid_xfer( + bytes([self.PWM_SET_DUTY_U16, pin.id]) + + duty_cycle.to_bytes(2, byteorder="little"), + True, + ) + if resp[1] != self.RESP_OK: + raise RuntimeError("PWM set duty cycle error.") + + +pico_u2if = Pico_u2if() diff --git a/src/adafruit_blinka/microcontroller/pico_u2if/pin.py b/src/adafruit_blinka/microcontroller/pico_u2if/pin.py new file mode 100644 index 0000000..ef22587 --- /dev/null +++ b/src/adafruit_blinka/microcontroller/pico_u2if/pin.py @@ -0,0 +1,84 @@ +"""PICO pin names""" +from .pico_u2if import pico_u2if + + +class Pin: + """A basic Pin class for use with MCP2221.""" + + # pin modes + IN = 0 + OUT = 1 + # pin values + LOW = 0 + HIGH = 1 + # pin pulls + PULL_NONE = 0 + PULL_UP = 1 + PULL_DOWN = 2 + + def __init__(self, pin_id=None): + self.id = pin_id + self._mode = None + self._pull = None + + def init(self, mode=IN, pull=PULL_NONE): + """Initialize the Pin""" + pull = Pin.PULL_NONE if pull is None else pull + if self.id is None: + raise RuntimeError("Can not init a None type pin.") + if mode not in (Pin.IN, Pin.OUT): + raise ValueError("Incorrect mode value.") + if pull not in (Pin.PULL_NONE, Pin.PULL_UP, Pin.PULL_DOWN): + raise ValueError("Incorrect pull value.") + + pico_u2if.gpio_init_pin(self.id, mode, pull) + + self._mode = mode + self._pull = pull + + def value(self, val=None): + """Set or return the Pin Value""" + # Digital In / Out + if self._mode in (Pin.IN, Pin.OUT): + # digital read + if val is None: + return pico_u2if.gpio_get_pin(self.id) + # digital write + if val in (Pin.LOW, Pin.HIGH): + pico_u2if.gpio_set_pin(self.id, val) + return None + # nope + raise ValueError("Invalid value for pin.") + + raise RuntimeError( + "No action for mode {} with value {}".format(self._mode, val) + ) + + +# create pin instances for each pin +GP0 = Pin(0) +GP1 = Pin(1) +GP2 = Pin(2) +GP3 = Pin(3) +GP4 = Pin(4) +GP5 = Pin(5) +GP6 = Pin(6) +GP7 = Pin(7) +GP8 = Pin(8) +GP9 = Pin(9) +GP10 = Pin(10) +GP11 = Pin(11) +GP12 = Pin(12) +GP13 = Pin(13) +GP14 = Pin(14) +GP15 = Pin(15) +GP16 = Pin(16) +GP17 = Pin(17) +GP18 = Pin(18) +GP19 = Pin(19) +GP20 = Pin(20) +GP21 = Pin(21) +GP22 = Pin(22) +GP26 = Pin(26) +GP27 = Pin(27) +GP28 = Pin(28) diff --git a/src/adafruit_blinka/microcontroller/pico_u2if/pwmio.py b/src/adafruit_blinka/microcontroller/pico_u2if/pwmio.py new file mode 100644 index 0000000..f69d28e --- /dev/null +++ b/src/adafruit_blinka/microcontroller/pico_u2if/pwmio.py @@ -0,0 +1,47 @@ +"""PWMOut Class for Pico u2if""" +from .pico_u2if import pico_u2if + + +class PWMOut: + """Pulse Width Modulation Output Class""" + + def __init__(self, pin, *, frequency=500, duty_cycle=0, variable_frequency=False): + pico_u2if.pwm_configure( + pin, + frequency=frequency, + duty_cycle=duty_cycle, + variable_frequency=variable_frequency, + ) + + self._pin = pin + + def __del__(self): + self.deinit() + + def __enter__(self): + return self + + def __exit__(self, t, value, traceback): + self.deinit() + + def deinit(self): + """Deinit PWM.""" + pico_u2if.pwm_deinit(self._pin) + + @property + def duty_cycle(self): + """The PWM's output duty cycle, 16-bit.""" + return pico_u2if.pwm_get_duty_cycle(self._pin) + + @duty_cycle.setter + def duty_cycle(self, duty_cycle): + pico_u2if.pwm_set_duty_cycle(self._pin, duty_cycle) + + @property + def frequency(self): + """The PWM's output frequency in Hertz.""" + return pico_u2if.pwm_get_frequency(self._pin) + + @frequency.setter + def frequency(self, frequency): + pico_u2if.pwm_set_frequency(self._pin, frequency) diff --git a/src/adafruit_blinka/microcontroller/pico_u2if/spi.py b/src/adafruit_blinka/microcontroller/pico_u2if/spi.py new file mode 100644 index 0000000..e4d7202 --- /dev/null +++ b/src/adafruit_blinka/microcontroller/pico_u2if/spi.py @@ -0,0 +1,71 @@ +"""SPI Class for Pico u2if""" +from .pico_u2if import pico_u2if + +# pylint: disable=protected-access, no-self-use +class SPI: + """Custom SPI Class for Pico u2if""" + + MSB = 0 + + def __init__(self, clock, *, baudrate=100000): + index = None + if clock.id == 18: + index = 0 + if clock.id == 10: + index = 1 + if index is None: + raise ValueError("No SPI port on specified pin.") + self._index = index + self._frequency = baudrate + pico_u2if.spi_set_port(self._index) + pico_u2if.spi_configure(self._frequency) + + # pylint: disable=too-many-arguments,unused-argument + def init( + self, + baudrate=1000000, + polarity=0, + phase=0, + bits=8, + firstbit=MSB, + sck=None, + mosi=None, + miso=None, + ): + """Initialize the Port""" + self._frequency = baudrate + pico_u2if.spi_set_port(self._index) + pico_u2if.spi_configure(self._frequency) + + # pylint: enable=too-many-arguments + + @property + def frequency(self): + """Return the current frequency""" + return self._frequency + + def write(self, buf, start=0, end=None): + """Write data from the buffer to SPI""" + pico_u2if.spi_write(buf, start=start, end=end) + + def readinto(self, buf, start=0, end=None, write_value=0): + """Read data from SPI and into the buffer""" + pico_u2if.spi_readinto(buf, start=start, end=end, write_value=write_value) + + # pylint: disable=too-many-arguments + def write_readinto( + self, buffer_out, buffer_in, out_start=0, out_end=None, in_start=0, in_end=None + ): + """Perform a half-duplex write from buffer_out and then + read data into buffer_in + """ + pico_u2if.spi_write_readinto( + buffer_out, + buffer_in, + out_start=out_start, + out_end=out_end, + in_start=in_start, + in_end=in_end, + ) + + # pylint: enable=too-many-arguments diff --git a/src/adafruit_blinka/microcontroller/rp2040/__init__.py b/src/adafruit_blinka/microcontroller/rp2040/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/adafruit_blinka/microcontroller/rp2040/pin.py b/src/adafruit_blinka/microcontroller/rp2040/pin.py new file mode 100755 index 0000000..6febdc4 --- /dev/null +++ b/src/adafruit_blinka/microcontroller/rp2040/pin.py @@ -0,0 +1,102 @@ +"""RP2040 pins""" + +from microcontroller import Pin + +GP0 = Pin(0) +GP1 = Pin(1) +GP2 = Pin(2) +GP3 = Pin(3) +GP4 = Pin(4) +GP5 = Pin(5) +GP6 = Pin(6) +GP7 = Pin(7) +GP8 = Pin(8) +GP9 = Pin(9) +GP10 = Pin(10) +GP11 = Pin(11) +GP12 = Pin(12) +GP13 = Pin(13) +GP14 = Pin(14) +GP15 = Pin(15) +GP16 = Pin(16) +GP17 = Pin(17) +GP18 = Pin(18) +GP19 = Pin(19) +GP20 = Pin(20) +GP21 = Pin(21) +GP22 = Pin(22) +GP23 = Pin(23) +GP24 = Pin(24) +GP25 = Pin(25) +GP26 = Pin(26) +GP27 = Pin(27) +GP28 = Pin(28) +GP29 = Pin(29) + +# ordered as spiId, sckId, mosiId (tx), misoId (rx) +SPI_PORTS = ( + (0, GP2, GP3, GP0), + (0, GP2, GP3, GP4), + (0, GP2, GP7, GP0), + (0, GP2, GP7, GP4), + (0, GP6, GP3, GP0), + (0, GP6, GP3, GP4), + (0, GP6, GP7, GP0), + (0, GP6, GP7, GP4), + (1, GP10, GP11, GP8), + (1, GP10, GP11, GP12), + (1, GP10, GP15, GP8), + (1, GP10, GP15, GP12), + (1, GP14, GP11, GP8), + (1, GP14, GP11, GP12), + (1, GP14, GP15, GP8), + (1, GP14, GP15, GP12), +) + +# ordered as uartId, txId, rxId +UART_PORTS = ( + (0, GP0, GP1), + (0, GP0, GP13), + (0, GP12, GP1), + (0, GP12, GP13), + (1, GP4, GP5), + (1, GP4, GP9), + (1, GP8, GP5), + (1, GP8, GP9), +) + +# ordered as scl, sda +I2C_PORTS = ( + (0, GP1, GP0), + (0, GP1, GP4), + (0, GP1, GP8), + (0, GP1, GP12), + (0, GP5, GP0), + (0, GP5, GP4), + (0, GP5, GP8), + (0, GP5, GP12), + (0, GP9, GP0), + (0, GP9, GP4), + (0, GP9, GP8), + (0, GP9, GP12), + (0, GP13, GP0), + (0, GP13, GP4), + (0, GP13, GP8), + (0, GP13, GP12), + (1, GP3, GP2), + (1, GP3, GP6), + (1, GP3, GP10), + (1, GP3, GP14), + (1, GP7, GP2), + (1, GP7, GP6), + (1, GP7, GP10), + (1, GP7, GP14), + (1, GP11, GP2), + (1, GP11, GP6), + (1, GP11, GP10), + (1, GP11, GP14), + (1, GP15, GP2), + (1, GP15, GP6), + (1, GP15, GP10), + (1, GP15, GP14), +) diff --git a/src/adafruit_blinka/microcontroller/stm32/stm32mp157/pin.py b/src/adafruit_blinka/microcontroller/stm32/stm32mp157/pin.py index 0196982..6aa6426 100755 --- a/src/adafruit_blinka/microcontroller/stm32/stm32mp157/pin.py +++ b/src/adafruit_blinka/microcontroller/stm32/stm32mp157/pin.py @@ -152,3 +152,18 @@ I2C_PORTS = ( (1, PD12, PF15), (5, PA11, PA12), ) + +# support busio port check +# 0 - linux system -> i2c-0 +# 1 - linux system -> i2c-1 +i2cPorts = ( + (0, PF14, PF15), + (1, PZ0, PZ1), +) + +# SysFS analog inputs, Ordered as analog analogInId, device, and channel +# Because stm32mp157 analog io used special port name,it doesn't like gpiod named form +# so support analog io in this way +PAN0 = 0 +PAN1 = 0 +analogIns = ((PAN0, 0, 0),) diff --git a/src/analogio.py b/src/analogio.py index 1db939a..04d3afc 100644 --- a/src/analogio.py +++ b/src/analogio.py @@ -25,7 +25,11 @@ elif detector.chip.RK3399: from adafruit_blinka.microcontroller.generic_linux.sysfs_analogin import AnalogIn elif detector.chip.IMX6ULL: from adafruit_blinka.microcontroller.generic_linux.sysfs_analogin import AnalogIn +elif detector.chip.STM32MP157: + from adafruit_blinka.microcontroller.generic_linux.sysfs_analogin import AnalogIn elif "sphinx" in sys.modules: pass +elif detector.board.pico_u2if: + from adafruit_blinka.microcontroller.pico_u2if.analogio import AnalogIn else: raise NotImplementedError("analogio not supported for this board.") diff --git a/src/board.py b/src/board.py index e4909f2..c8eed9e 100755 --- a/src/board.py +++ b/src/board.py @@ -44,6 +44,9 @@ elif board_id == ap_board.NODEMCU: elif board_id == ap_board.PYBOARD: from adafruit_blinka.board.pyboard import * +elif board_id == ap_board.RASPBERRY_PI_PICO: + from adafruit_blinka.board.raspberrypi.pico import * + elif detector.board.any_raspberry_pi_40_pin: from adafruit_blinka.board.raspberrypi.raspi_40pin import * @@ -56,6 +59,9 @@ elif detector.board.RASPBERRY_PI_B_REV1: elif detector.board.RASPBERRY_PI_A or detector.board.RASPBERRY_PI_B_REV2: from adafruit_blinka.board.raspberrypi.raspi_1b_rev2 import * +elif board_id == ap_board.BEAGLEBONE: + from adafruit_blinka.board.beagleboard.beaglebone_black import * + elif board_id == ap_board.BEAGLEBONE_BLACK: from adafruit_blinka.board.beagleboard.beaglebone_black import * @@ -203,28 +209,37 @@ elif board_id == ap_board.STM32MP157C_DK2: elif board_id == ap_board.LUBANCAT_IMX6ULL: from adafruit_blinka.board.lubancat.lubancat_imx6ull import * +elif board_id == ap_board.LUBANCAT_STM32MP157: + from adafruit_blinka.board.lubancat.lubancat_stm32mp157 import * + elif board_id == ap_board.NANOPI_NEO_AIR: from adafruit_blinka.board.nanopi.neoair import * elif board_id == ap_board.NANOPI_DUO2: from adafruit_blinka.board.nanopi.duo2 import * +elif board_id == ap_board.PICO_U2IF: + from adafruit_blinka.board.pico_u2if import * + elif "sphinx" in sys.modules: pass else: raise NotImplementedError("Board not supported {}".format(board_id)) +if "SCL" in locals() and "SDA" in locals(): + + def I2C(): + """The singleton I2C interface""" + import busio -def I2C(): - """The singleton I2C interface""" - import busio + return busio.I2C(SCL, SDA) - return busio.I2C(SCL, SDA) +if "SCLK" in locals() and "MOSI" in locals() and "MISO" in locals(): -def SPI(): - """The singleton SPI interface""" - import busio + def SPI(): + """The singleton SPI interface""" + import busio - return busio.SPI(SCLK, MOSI, MISO) + return busio.SPI(SCLK, MOSI, MISO) diff --git a/src/busio.py b/src/busio.py index 6749459..3fd6b5b 100755 --- a/src/busio.py +++ b/src/busio.py @@ -18,7 +18,7 @@ from adafruit_blinka import Enum, Lockable, agnostic from adafruit_blinka.agnostic import board_id, detector # pylint: disable=import-outside-toplevel,too-many-branches,too-many-statements -# pylint: disable=too-many-arguments,too-many-function-args +# pylint: disable=too-many-arguments,too-many-function-args, consider-using-with class I2C(Lockable): @@ -53,6 +53,11 @@ class I2C(Lockable): self._i2c = _I2C(frequency=frequency) return + if detector.board.pico_u2if: + from adafruit_blinka.microcontroller.pico_u2if.i2c import I2C as _I2C + + self._i2c = _I2C(scl, sda, frequency=frequency) + return if detector.board.any_embedded_linux: from adafruit_blinka.microcontroller.generic_linux.i2c import I2C as _I2C else: @@ -171,6 +176,12 @@ class SPI(Lockable): self._spi = _SPI() self._pins = (SCK, MOSI, MISO) return + if detector.board.pico_u2if: + from adafruit_blinka.microcontroller.pico_u2if.spi import SPI as _SPI + + self._spi = _SPI(clock) # this is really all that's needed + self._pins = (clock, clock, clock) # will determine MOSI/MISO from clock + return if detector.board.any_embedded_linux: from adafruit_blinka.microcontroller.generic_linux.spi import SPI as _SPI else: @@ -286,6 +297,9 @@ class SPI(Lockable): elif detector.board.any_lubancat and detector.chip.id == ap_chip.IMX6ULL: from adafruit_blinka.microcontroller.nxp_imx6ull.pin import Pin from adafruit_blinka.microcontroller.generic_linux.spi import SPI as _SPI + elif detector.board.pico_u2if: + from adafruit_blinka.microcontroller.pico_u2if.spi import SPI as _SPI + from adafruit_blinka.microcontroller.pico_u2if.pin import Pin else: from machine import SPI as _SPI from machine import Pin diff --git a/src/digitalio.py b/src/digitalio.py index a59866b..7b4ea8f 100755 --- a/src/digitalio.py +++ b/src/digitalio.py @@ -61,6 +61,8 @@ elif detector.board.greatfet_one: from adafruit_blinka.microcontroller.nxp_lpc4330.pin import Pin elif detector.chip.STM32F405: from machine import Pin +elif detector.chip.RP2040: + from machine import Pin elif detector.board.microchip_mcp2221: from adafruit_blinka.microcontroller.mcp2221.pin import Pin elif detector.chip.PENTIUM_N3710: @@ -71,8 +73,12 @@ elif detector.chip.MT8167: from adafruit_blinka.microcontroller.mt8167.pin import Pin elif detector.chip.H5: from adafruit_blinka.microcontroller.allwinner.h5.pin import Pin +elif detector.chip.H6: + from adafruit_blinka.microcontroller.allwinner.h6.pin import Pin elif detector.chip.H616: from adafruit_blinka.microcontroller.allwinner.h616.pin import Pin +elif detector.board.pico_u2if: + from adafruit_blinka.microcontroller.pico_u2if.pin import Pin from adafruit_blinka import Enum, ContextManaged diff --git a/src/microcontroller/__init__.py b/src/microcontroller/__init__.py index eed4757..2057a7f 100755 --- a/src/microcontroller/__init__.py +++ b/src/microcontroller/__init__.py @@ -17,7 +17,7 @@ class Pin(Enum): def __init__(self, pin_id): """Identifier for pin, referencing platform-specific pin id""" - self._id = pin_id + self.id = pin_id def __repr__(self): # pylint: disable=import-outside-toplevel, cyclic-import @@ -43,6 +43,8 @@ if chip_id == ap_chip.ESP8266: from adafruit_blinka.microcontroller.esp8266 import * elif chip_id == ap_chip.STM32F405: from adafruit_blinka.microcontroller.stm32.stm32f405 import * +elif chip_id == ap_chip.RP2040: + from adafruit_blinka.microcontroller.rp2040 import * elif chip_id == ap_chip.BCM2XXX: from adafruit_blinka.microcontroller.bcm283x import * elif chip_id == ap_chip.DRA74X: diff --git a/src/microcontroller/pin.py b/src/microcontroller/pin.py index 88c2d00..4916c19 100755 --- a/src/microcontroller/pin.py +++ b/src/microcontroller/pin.py @@ -10,6 +10,8 @@ if chip_id == ap_chip.ESP8266: from adafruit_blinka.microcontroller.esp8266.pin import * elif chip_id == ap_chip.STM32F405: from adafruit_blinka.microcontroller.stm32.stm32f405.pin import * +elif chip_id == ap_chip.RP2040: + from adafruit_blinka.microcontroller.rp2040.pin import * elif chip_id == ap_chip.BCM2XXX: from adafruit_blinka.microcontroller.bcm283x.pin import * elif chip_id == ap_chip.DRA74X: @@ -72,5 +74,7 @@ elif chip_id == ap_chip.STM32MP157: from adafruit_blinka.microcontroller.stm32.stm32mp157.pin import * elif chip_id == ap_chip.MT8167: from adafruit_blinka.microcontroller.mt8167.pin import * +elif chip_id == ap_chip.PICO_U2IF: + from adafruit_blinka.microcontroller.pico_u2if.pin import * else: raise NotImplementedError("Microcontroller not supported: ", chip_id) diff --git a/src/neopixel_write.py b/src/neopixel_write.py index 2c85f01..00ffa0b 100644 --- a/src/neopixel_write.py +++ b/src/neopixel_write.py @@ -14,6 +14,8 @@ from adafruit_blinka.agnostic import detector if detector.board.any_raspberry_pi: from adafruit_blinka.microcontroller.bcm283x import neopixel as _neopixel +elif detector.board.pico_u2if: + from adafruit_blinka.microcontroller.pico_u2if import neopixel as _neopixel elif "sphinx" in sys.modules: pass else: diff --git a/src/pwmio.py b/src/pwmio.py index 3a6b5bd..dbb8bb7 100644 --- a/src/pwmio.py +++ b/src/pwmio.py @@ -29,6 +29,8 @@ elif detector.board.greatfet_one: from adafruit_blinka.microcontroller.nxp_lpc4330.pwmout import PWMOut elif detector.board.any_lubancat: from adafruit_blinka.microcontroller.generic_linux.sysfs_pwmout import PWMOut +elif detector.board.pico_u2if: + from adafruit_blinka.microcontroller.pico_u2if.pwmio import PWMOut elif "sphinx" in sys.modules: pass else: