]> Repositories - Adafruit_Blinka-hackapet.git/commitdiff
Merge pull request #7 from adafruit/master
authortwa127 <46624596+twa127@users.noreply.github.com>
Mon, 3 May 2021 19:43:20 +0000 (20:43 +0100)
committerGitHub <noreply@github.com>
Mon, 3 May 2021 19:43:20 +0000 (20:43 +0100)
Merge

28 files changed:
README.rst
src/adafruit_blinka/board/lubancat/lubancat_stm32mp157.py [new file with mode: 0644]
src/adafruit_blinka/board/pico_u2if.py [new file with mode: 0644]
src/adafruit_blinka/board/radxa/rockpi4.py [new file with mode: 0644]
src/adafruit_blinka/microcontroller/bcm283x/pin.py
src/adafruit_blinka/microcontroller/bcm283x/pulseio/PWMOut.py
src/adafruit_blinka/microcontroller/bcm283x/pulseio/PulseIn.py
src/adafruit_blinka/microcontroller/nova/__init__.py
src/adafruit_blinka/microcontroller/pico_u2if/__init__.py [new file with mode: 0644]
src/adafruit_blinka/microcontroller/pico_u2if/analogio.py [new file with mode: 0644]
src/adafruit_blinka/microcontroller/pico_u2if/i2c.py [new file with mode: 0644]
src/adafruit_blinka/microcontroller/pico_u2if/neopixel.py [new file with mode: 0644]
src/adafruit_blinka/microcontroller/pico_u2if/pico_u2if.py [new file with mode: 0644]
src/adafruit_blinka/microcontroller/pico_u2if/pin.py [new file with mode: 0644]
src/adafruit_blinka/microcontroller/pico_u2if/pwmio.py [new file with mode: 0644]
src/adafruit_blinka/microcontroller/pico_u2if/spi.py [new file with mode: 0644]
src/adafruit_blinka/microcontroller/rockchip/PWMOut.py [new file with mode: 0644]
src/adafruit_blinka/microcontroller/rockchip/rk3399/__init__.py [new file with mode: 0644]
src/adafruit_blinka/microcontroller/rockchip/rk3399/pin.py [new file with mode: 0644]
src/adafruit_blinka/microcontroller/stm32/stm32mp157/pin.py
src/analogio.py
src/board.py
src/busio.py
src/digitalio.py
src/microcontroller/__init__.py
src/microcontroller/pin.py
src/neopixel_write.py
src/pwmio.py

index 40df1a97b8ed2197ea54b64aa0a611136aa30b58..d802c16077e6b17398021eaff1ec27229c9f0bf0 100755 (executable)
@@ -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
 <https://circuitpython.readthedocs.io/projects/blinka/en/latest/index.html>`_.
@@ -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 (file)
index 0000000..33dfdcd
--- /dev/null
@@ -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 (file)
index 0000000..bcdfe47
--- /dev/null
@@ -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/radxa/rockpi4.py b/src/adafruit_blinka/board/radxa/rockpi4.py
new file mode 100644 (file)
index 0000000..e327ff3
--- /dev/null
@@ -0,0 +1,62 @@
+"""Pin definitions for the Rock Pi 4."""
+
+from adafruit_blinka.microcontroller.rockchip.rk3399 import pin
+
+D3 = pin.GPIO2_A7  # /I2C7_SDA/PIN 71/
+D5 = pin.GPIO2_B0  # /I2C7_SCL/PIN 72/
+D7 = pin.GPIO2_B3  # /SPI2_CLK/PIN 75/
+D8 = pin.GPIO4_C4  # /UART2_TXD/PIN 148/
+D10 = pin.GPIO4_C3  # /UART2_RXD/PIN 147/
+D11 = pin.GPIO4_C2  # /PWM0/PIN 146/
+D13 = pin.GPIO4_C6  # /PWM1/PIN 150/
+D15 = pin.GPIO4_C5  # /SPDIF_TX/PIN 149/
+D16 = pin.GPIO4_D2  # /PIN 154/
+D17 = pin.GPIO4_D4  # /PIN 156/
+D19 = pin.GPIO1_B0  # /UART4_TXD/SPI1_TXD/PIN 40/
+D21 = pin.GPIO1_A7  # /UART4_RXD/SPI1_RXD/PIN 39/
+D22 = pin.GPIO4_D5  # /PIN 157/
+D23 = pin.GPIO1_B1  # /SPI1_CLK/PIN 41/
+D24 = pin.GPIO1_B2  # /SPI1_CS/PIN 42/
+D27 = pin.GPIO2_A0  # /I2C2_SDA/PIN 64/
+D28 = pin.GPIO2_A1  # /I2C2_SCL/PIN 65/
+D29 = pin.GPIO2_B2  # /I2C6_SCL/SPI2_TXD/PIN 74/
+D31 = pin.GPIO2_B1  # /I2C6_SDA/SPI2_RXD/PIN 73/
+D32 = pin.GPIO3_C0  # /SPDIF_TX/UART3_CTS/PIN 112/
+D33 = pin.GPIO2_B4  # /SPI2_CS/PIN 76/
+D35 = pin.GPIO4_A5  # /I2S1_LRCK_TX/PIN 133/
+D36 = pin.GPIO4_A4  # /I2S1_LRCK_RX/PIN 132/
+D37 = pin.GPIO4_D6  # /PIN 158/
+D38 = pin.GPIO4_A6  # /I2S1_SDI/PIN 134/
+D40 = pin.GPIO4_A7  # /I2S1_SDO/PIN 135/
+
+SDA2 = D27
+SCL2 = D28
+
+SDA6 = D31
+SCL6 = D29
+
+SDA7 = D3
+SCL7 = D5
+
+SDA = SDA2
+SCL = SCL2
+
+SCLK = D7
+MOSI = D29
+MISO = D31
+CS = D33
+SCK = SCLK
+
+UART2_TX = D8
+UART2_RX = D10
+
+UART4_TX = D19
+UART4_RX = D21
+
+UART_TX = UART2_TX
+UART_RX = UART2_RX
+
+PWM0 = pin.PWM0
+PWM1 = pin.PWM1
+
+ADC_IN0 = pin.ADC_IN0
index 5022366a7d4dd1b299b5b17301d5b73beb0d2178..48eb0c06dc7f999d3f4372a76dfad88202ea78dd 100644 (file)
@@ -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
 )
index 3f47c63a6686d4bae53000b30b8a6dfcf63c2ccb..65f269f5b0463755e937e031e4771d4e8f50e9d0 100644 (file)
@@ -49,9 +49,10 @@ class PWMOut:
 
     def deinit(self):
         """Deinit the PWM."""
-        self._pwmpin.stop()
-        GPIO.cleanup(self._pin.id)
-        self._pwmpin = None
+        if self._pwmpin is not None:
+            self._pwmpin.stop()
+            GPIO.cleanup(self._pin.id)
+            self._pwmpin = None
 
     def _is_deinited(self):
         if self._pwmpin is None:
index 19dc98f32089364b6e266712f07ce39038dda5e1..fd9febee7dabdbe70f5fec1bd3d9810fb2943b43 100644 (file)
@@ -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"""
 
index 8ab7ce09e52ec6c9a2d0e6d7772dc84aef511be7..d9b7f7577d79b25e31118d489792d27b7f62c116 100644 (file)
@@ -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 (file)
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 (file)
index 0000000..2ae480f
--- /dev/null
@@ -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 (file)
index 0000000..ee393bb
--- /dev/null
@@ -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 (file)
index 0000000..4953c2d
--- /dev/null
@@ -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 (file)
index 0000000..687bfc0
--- /dev/null
@@ -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 (file)
index 0000000..ef22587
--- /dev/null
@@ -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 (file)
index 0000000..f69d28e
--- /dev/null
@@ -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 (file)
index 0000000..e4d7202
--- /dev/null
@@ -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/rockchip/PWMOut.py b/src/adafruit_blinka/microcontroller/rockchip/PWMOut.py
new file mode 100644 (file)
index 0000000..09c4a8e
--- /dev/null
@@ -0,0 +1,392 @@
+"""
+Much code from https://github.com/vsergeev/python-periphery/blob/master/periphery/pwm.py
+Copyright (c) 2015-2016 vsergeev / Ivan (Vanya) A. Sergeev
+License: MIT
+"""
+
+import os
+from time import sleep
+from errno import EACCES
+
+try:
+    from microcontroller.pin import pwmOuts
+except ImportError:
+    raise RuntimeError("No PWM outputs defined for this board.") from ImportError
+
+
+# pylint: disable=unnecessary-pass, too-many-instance-attributes
+
+
+class PWMError(IOError):
+    """Base class for PWM errors."""
+
+    pass
+
+
+# pylint: enable=unnecessary-pass
+
+
+class PWMOut:
+    """Pulse Width Modulation Output Class"""
+
+    # Number of retries to check for successful PWM export on open
+    PWM_STAT_RETRIES = 10
+    # Delay between check for successful PWM export on open (100ms)
+    PWM_STAT_DELAY = 0.1
+
+    # Sysfs paths
+    _chip_path = "pwmchip{}"
+    _channel_path = "pwm{}"
+
+    def __init__(self, pwm, *, frequency=500, duty_cycle=0, variable_frequency=False):
+        """Instantiate a PWM object and open the sysfs PWM corresponding to the
+        specified chip and channel.
+        Args:
+            pwm (str): PWM pin.
+            frequency (int, float): target frequency in Hertz (32-bit).
+            duty_cycle (int, float): The fraction of each pulse which is high (16-bit).
+            variable_frequency (bool): True if the frequency will change over time.
+        Returns:
+            PWM: PWM object.
+        Raises:
+            PWMError: if an I/O or OS error occurs.
+            TypeError: if `chip` or `channel` types are invalid.
+            LookupError: if PWM chip does not exist.
+            TimeoutError: if waiting for PWM export times out.
+        """
+
+        self._chip = None
+        self._channel = None
+        self._period_ns = 0
+        self._open(pwm, frequency, duty_cycle, variable_frequency)
+
+    def __del__(self):
+        self.close()
+
+    def __enter__(self):
+        return self
+
+    def __exit__(self, exc_type, exc_val, exc_tb):
+        self.close()
+
+    def _open(self, pwm, frequency, duty_cycle, variable_frequency):
+        for pwmout in pwmOuts:
+            if pwmout[1] == pwm:
+                self._chip = pwmout[0][0]
+                self._channel = pwmout[0][1]
+
+        self._chip_path = os.path.join(
+            "/sys/class/pwm", self._chip_path.format(self._chip)
+        )
+        self._channel_path = os.path.join(
+            self._chip_path, self._channel_path.format(self._channel)
+        )
+
+        if variable_frequency:
+            print("Variable Frequency is not supported, continuing without it...")
+
+        if not os.path.isdir(self._chip_path):
+            raise LookupError("Opening PWM: PWM chip {} not found.".format(self._chip))
+
+        if not os.path.isdir(self._channel_path):
+            # Exporting the PWM.
+            try:
+                with open(os.path.join(self._chip_path, "export"), "w") as f_export:
+                    f_export.write("{:d}\n".format(self._channel))
+            except IOError as e:
+                raise PWMError(
+                    e.errno, "Exporting PWM channel: " + e.strerror
+                ) from IOError
+
+            # Loop until PWM is exported
+            exported = False
+            for i in range(PWMOut.PWM_STAT_RETRIES):
+                if os.path.isdir(self._channel_path):
+                    exported = True
+                    break
+
+                sleep(PWMOut.PWM_STAT_DELAY)
+
+            if not exported:
+                raise TimeoutError(
+                    'Exporting PWM: waiting for "{:s}" timed out.'.format(
+                        self._channel_path
+                    )
+                )
+
+            # Loop until 'period' is writable, This could take some time after
+            # export as application of the udev rules after export is asynchronous.
+            # Without this loop, the following properties may not be writable yet.
+            for i in range(PWMOut.PWM_STAT_RETRIES):
+                try:
+                    with open(
+                        os.path.join(self._channel_path, "period"),
+                        "w",
+                    ):
+                        break
+                except IOError as e:
+                    if e.errno != EACCES or (
+                        e.errno == EACCES and i == PWMOut.PWM_STAT_RETRIES - 1
+                    ):
+                        raise PWMError(
+                            e.errno, "Opening PWM period: " + e.strerror
+                        ) from IOError
+
+                sleep(PWMOut.PWM_STAT_DELAY)
+
+            self.frequency = frequency
+            self.duty_cycle = duty_cycle
+
+            # Cache the period for fast duty cycle updates
+            self._period_ns = self._get_period_ns()
+
+    def close(self):
+        """Close the PWM."""
+        if self._channel is not None:
+            # Unexporting the PWM channel
+            try:
+                unexport_fd = os.open(
+                    os.path.join(self._chip_path, "unexport"), os.O_WRONLY
+                )
+                os.write(unexport_fd, "{:d}\n".format(self._channel).encode())
+                os.close(unexport_fd)
+            except OSError as e:
+                raise PWMError(e.errno, "Unexporting PWM: " + e.strerror) from OSError
+
+        self._chip = None
+        self._channel = None
+
+    def _write_channel_attr(self, attr, value):
+        with open(os.path.join(self._channel_path, attr), "w") as f_attr:
+            f_attr.write(value + "\n")
+
+    def _read_channel_attr(self, attr):
+        with open(os.path.join(self._channel_path, attr), "r") as f_attr:
+            return f_attr.read().strip()
+
+    # Methods
+
+    def enable(self):
+        """Enable the PWM output."""
+        self.enabled = True
+
+    def disable(self):
+        """Disable the PWM output."""
+        self.enabled = False
+
+    # Mutable properties
+
+    def _get_period(self):
+        return float(self.period_ms) / 1000
+
+    def _set_period(self, period):
+        if not isinstance(period, (int, float)):
+            raise TypeError("Invalid period type, should be int.")
+
+        self.period_ms = int(period * 1000)
+
+    period = property(_get_period, _set_period)
+    """Get or set the PWM's output period in seconds.
+
+    Raises:
+        PWMError: if an I/O or OS error occurs.
+        TypeError: if value type is not int.
+
+    :type: int, float
+    """
+
+    def _get_period_ms(self):
+        return self.period_us / 1000
+
+    def _set_period_ms(self, period_ms):
+        if not isinstance(period_ms, (int, float)):
+            raise TypeError("Invalid period type, should be int or float.")
+        self.period_us = int(period_ms * 1000)
+
+    period_ms = property(_get_period_ms, _set_period_ms)
+    """Get or set the PWM's output period in milliseconds.
+
+    Raises:
+        PWMError: if an I/O or OS error occurs.
+        TypeError: if value type is not int.
+
+    :type: int, float
+    """
+
+    def _get_period_us(self):
+        return self.period_ns / 1000
+
+    def _set_period_us(self, period_us):
+        if not isinstance(period_us, int):
+            raise TypeError("Invalid period type, should be int.")
+
+        self.period_ns = int(period_us * 1000)
+
+    period_us = property(_get_period_us, _set_period_us)
+    """Get or set the PWM's output period in microseconds.
+
+    Raises:
+        PWMError: if an I/O or OS error occurs.
+        TypeError: if value type is not int.
+
+    :type: int
+    """
+
+    def _get_period_ns(self):
+        period_ns = self._read_channel_attr("period")
+        try:
+            period_ns = int(period_ns)
+        except ValueError:
+            raise PWMError(
+                None, 'Unknown period value: "%s".' % period_ns
+            ) from ValueError
+
+        self._period_ns = period_ns
+
+        return period_ns
+
+    def _set_period_ns(self, period_ns):
+        if not isinstance(period_ns, int):
+            raise TypeError("Invalid period type, should be int.")
+
+        self._write_channel_attr("period", str(period_ns))
+
+        # Update our cached period
+        self._period_ns = period_ns
+
+    period_ns = property(_get_period_ns, _set_period_ns)
+    """Get or set the PWM's output period in nanoseconds.
+
+    Raises:
+        PWMError: if an I/O or OS error occurs.
+        TypeError: if value type is not int.
+
+    :type: int
+    """
+
+    def _get_duty_cycle_ns(self):
+        duty_cycle_ns_str = self._read_channel_attr("duty_cycle")
+
+        try:
+            duty_cycle_ns = int(duty_cycle_ns_str)
+        except ValueError:
+            raise PWMError(
+                None, 'Unknown duty cycle value: "{:s}"'.format(duty_cycle_ns_str)
+            ) from ValueError
+
+        return duty_cycle_ns
+
+    def _set_duty_cycle_ns(self, duty_cycle_ns):
+        if not isinstance(duty_cycle_ns, int):
+            raise TypeError("Invalid duty cycle type, should be int.")
+
+        self._write_channel_attr("duty_cycle", str(duty_cycle_ns))
+
+    duty_cycle_ns = property(_get_duty_cycle_ns, _set_duty_cycle_ns)
+    """Get or set the PWM's output duty cycle in nanoseconds.
+
+    Raises:
+        PWMError: if an I/O or OS error occurs.
+        TypeError: if value type is not int.
+
+    :type: int
+    """
+
+    def _get_duty_cycle(self):
+        return float(self.duty_cycle_ns) / self._period_ns
+
+    def _set_duty_cycle(self, duty_cycle):
+        if not isinstance(duty_cycle, (int, float)):
+            raise TypeError("Invalid duty cycle type, should be int or float.")
+
+        if not 0.0 <= duty_cycle <= 1.0:
+            raise ValueError("Invalid duty cycle value, should be between 0.0 and 1.0.")
+
+        # Convert duty cycle from ratio to nanoseconds
+        self.duty_cycle_ns = int(duty_cycle * self._period_ns)
+
+    duty_cycle = property(_get_duty_cycle, _set_duty_cycle)
+    """Get or set the PWM's output duty cycle as a ratio from 0.0 to 1.0.
+    Raises:
+        PWMError: if an I/O or OS error occurs.
+        TypeError: if value type is not int or float.
+        ValueError: if value is out of bounds of 0.0 to 1.0.
+    :type: int, float
+    """
+
+    def _get_frequency(self):
+        return 1.0 / self.period
+
+    def _set_frequency(self, frequency):
+        if not isinstance(frequency, (int, float)):
+            raise TypeError("Invalid frequency type, should be int or float.")
+
+        self.period = 1.0 / frequency
+
+    frequency = property(_get_frequency, _set_frequency)
+    """Get or set the PWM's output frequency in Hertz.
+    Raises:
+        PWMError: if an I/O or OS error occurs.
+        TypeError: if value type is not int or float.
+    :type: int, float
+    """
+
+    def _get_polarity(self):
+        return self._read_channel_attr("polarity")
+
+    def _set_polarity(self, polarity):
+        if not isinstance(polarity, str):
+            raise TypeError("Invalid polarity type, should be str.")
+
+        if polarity.lower() not in ["normal", "inversed"]:
+            raise ValueError('Invalid polarity, can be: "normal" or "inversed".')
+
+        self._write_channel_attr("polarity", polarity.lower())
+
+    polarity = property(_get_polarity, _set_polarity)
+    """Get or set the PWM's output polarity. Can be "normal" or "inversed".
+    Raises:
+        PWMError: if an I/O or OS error occurs.
+        TypeError: if value type is not str.
+        ValueError: if value is invalid.
+    :type: str
+    """
+
+    def _get_enabled(self):
+        enabled = self._read_channel_attr("enable")
+
+        if enabled == "1":
+            return True
+        if enabled == "0":
+            return False
+
+        raise PWMError(None, 'Unknown enabled value: "{:s}"'.format(enabled))
+
+    def _set_enabled(self, value):
+        if not isinstance(value, bool):
+            raise TypeError("Invalid enabled type, should be bool.")
+
+        self._write_channel_attr("enable", "1" if value else "0")
+
+    enabled = property(_get_enabled, _set_enabled)
+    """Get or set the PWM's output enabled state.
+    Raises:
+        PWMError: if an I/O or OS error occurs.
+        TypeError: if value type is not bool.
+    :type: bool
+    """
+
+    # String representation
+
+    def __str__(self):
+        return (
+            "PWM {:d}, chip {:d} (period={:f} sec, duty_cycle={:f}%,"
+            " polarity={:s}, enabled={:s})".format(
+                self._channel,
+                self._chip,
+                self.period,
+                self.duty_cycle * 100,
+                self.polarity,
+                str(self.enabled),
+            )
+        )
diff --git a/src/adafruit_blinka/microcontroller/rockchip/rk3399/__init__.py b/src/adafruit_blinka/microcontroller/rockchip/rk3399/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/src/adafruit_blinka/microcontroller/rockchip/rk3399/pin.py b/src/adafruit_blinka/microcontroller/rockchip/rk3399/pin.py
new file mode 100644 (file)
index 0000000..4419861
--- /dev/null
@@ -0,0 +1,77 @@
+"""A Pin class for use with Rockchip RK3399."""
+
+from adafruit_blinka.microcontroller.generic_linux.sysfs_pin import Pin
+
+GPIO1_A7 = Pin(39)
+GPIO1_B0 = Pin(40)
+GPIO1_B1 = Pin(41)
+GPIO1_B2 = Pin(42)
+GPIO2_A0 = Pin(64)
+GPIO2_A1 = Pin(65)
+GPIO2_A7 = Pin(71)
+GPIO2_B0 = Pin(72)
+GPIO2_B1 = Pin(73)
+GPIO2_B2 = Pin(74)
+GPIO2_B3 = Pin(75)
+GPIO2_B4 = Pin(76)
+GPIO3_C0 = Pin(112)
+GPIO4_A3 = Pin(131)
+GPIO4_A4 = Pin(132)
+GPIO4_A5 = Pin(133)
+GPIO4_A6 = Pin(134)
+GPIO4_A7 = Pin(135)
+GPIO4_C2 = Pin(146)
+GPIO4_C3 = Pin(147)
+GPIO4_C4 = Pin(148)
+GPIO4_C5 = Pin(149)
+GPIO4_C6 = Pin(150)
+GPIO4_D2 = Pin(154)
+GPIO4_D4 = Pin(156)
+GPIO4_D5 = Pin(157)
+GPIO4_D6 = Pin(158)
+ADC_IN0 = 1
+
+# I2C
+I2C2_SDA = GPIO2_A0
+I2C2_SCL = GPIO2_A1
+I2C6_SDA = GPIO2_B1
+I2C6_SCL = GPIO2_B2
+I2C7_SDA = GPIO2_A7
+I2C7_SCL = GPIO2_B0
+
+# SPI
+SPI1_CS = GPIO1_B2
+SPI1_SCLK = GPIO1_B1
+SPI1_MISO = GPIO1_B0
+SPI1_MOSI = GPIO1_A7
+SPI2_CS = GPIO2_B4
+SPI2_SCLK = GPIO2_A1
+SPI2_MISO = GPIO2_B1
+SPI2_MOSI = GPIO2_B2
+
+# UART
+UART0_TX = GPIO4_C4
+UART0_RX = GPIO4_C3
+
+# PWM
+PWM0 = GPIO4_C2
+PWM1 = GPIO4_C6
+
+# ordered as i2cId, SCL, SDA
+i2cPorts = (
+    (0, I2C2_SCL, I2C2_SDA),
+    (1, I2C6_SCL, I2C6_SDA),
+    (7, I2C7_SCL, I2C7_SDA),
+)
+
+# ordered as spiId, sckId, mosiId, misoId
+spiPorts = ((1, SPI1_SCLK, SPI1_MOSI, SPI1_MISO),)
+
+# SysFS pwm outputs, pwm channel and pin in first tuple
+pwmOuts = (
+    ((0, 0), PWM0),
+    ((1, 0), PWM1),
+)
+
+# SysFS analog inputs, Ordered as analog analogInId, device, and channel
+analogIns = ((ADC_IN0, 0, 0),)
index 019698249790627ad7e4fec5441280ab6b66ac0e..6aa6426a617f2baa5a6755d65c96694acf736ad2 100755 (executable)
@@ -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),)
index 8ca58c1c71c0c323aa1c4d6913f6487e5c15be0a..04d3afce8494afb0b38f47a45a54cfc964acb8d1 100644 (file)
@@ -21,9 +21,15 @@ elif detector.board.greatfet_one:
     from adafruit_blinka.microcontroller.nxp_lpc4330.analogio import AnalogOut
 elif detector.chip.RK3308:
     from adafruit_blinka.microcontroller.generic_linux.sysfs_analogin import AnalogIn
+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.")
index cf2076a307e42feaf05af241cf244ec7b907158b..45eaed6c3ac79596cd4b053ac555ebd191c493b2 100755 (executable)
@@ -56,6 +56,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 *
 
@@ -191,6 +194,9 @@ elif board_id == ap_board.ONION_OMEGA2:
 elif board_id == ap_board.ROCK_PI_S:
     from adafruit_blinka.board.radxa.rockpis import *
 
+elif board_id == ap_board.ROCK_PI_4:
+    from adafruit_blinka.board.radxa.rockpi4 import *
+
 elif board_id == ap_board.UDOO_X86:
     from adafruit_blinka.board.udoo_x86ultra import *
 
@@ -200,12 +206,18 @@ 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
 
index d4613a3620d5a77ee83a886e61502c42b85eb20d..3fd6b5b112ada16de279c606c652952778fa0358 100755 (executable)
@@ -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:
@@ -254,6 +265,9 @@ class SPI(Lockable):
         elif detector.board.ROCK_PI_S:
             from adafruit_blinka.microcontroller.generic_linux.spi import SPI as _SPI
             from adafruit_blinka.microcontroller.rockchip.rk3308.pin import Pin
+        elif detector.board.ROCK_PI_4:
+            from adafruit_blinka.microcontroller.generic_linux.spi import SPI as _SPI
+            from adafruit_blinka.microcontroller.rockchip.rk3399.pin import Pin
         elif detector.board.SIFIVE_UNLEASHED:
             from adafruit_blinka.microcontroller.generic_linux.spi import SPI as _SPI
             from adafruit_blinka.microcontroller.hfu540.pin import Pin
@@ -283,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
index 08d154967f2a456accfdb622c7cc9b08015f71d0..361663b04a3ab8c3890c187245d9d6459933cfd7 100755 (executable)
@@ -51,6 +51,8 @@ elif detector.chip.MIPS24KEC:
     from adafruit_blinka.microcontroller.mips24kec.pin import Pin
 elif detector.chip.RK3308:
     from adafruit_blinka.microcontroller.rockchip.rk3308.pin import Pin
+elif detector.chip.RK3399:
+    from adafruit_blinka.microcontroller.rockchip.rk3399.pin import Pin
 elif detector.board.ftdi_ft232h:
     from adafruit_blinka.microcontroller.ft232h.pin import Pin
 elif detector.board.binho_nova:
@@ -71,6 +73,8 @@ elif detector.chip.H5:
     from adafruit_blinka.microcontroller.allwinner.h5.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
 
index a5b3b75c4dea47f24eaab3397d96738662ae4f9d..eed47576345a9530148654681a4994a94d602bab 100755 (executable)
@@ -51,6 +51,10 @@ elif chip_id == ap_chip.AM33XX:
     from adafruit_blinka.microcontroller.am335x import *
 elif chip_id == ap_chip.SUN8I:
     from adafruit_blinka.microcontroller.allwinner.h3 import *
+elif chip_id == ap_chip.H5:
+    from adafruit_blinka.microcontroller.allwinner.h5.pin import *
+elif chip_id == ap_chip.H616:
+    from adafruit_blinka.microcontroller.allwinner.h616.pin import *
 elif chip_id == ap_chip.SAMA5:
     from adafruit_blinka.microcontroller.sama5 import *
 elif chip_id == ap_chip.T210:
@@ -75,6 +79,8 @@ elif chip_id == ap_chip.A33:
     from adafruit_blinka.microcontroller.allwinner.a33.pin import *
 elif chip_id == ap_chip.RK3308:
     from adafruit_blinka.microcontroller.rockchip.rk3308.pin import *
+elif chip_id == ap_chip.RK3399:
+    from adafruit_blinka.microcontroller.rockchip.rk3399.pin import *
 elif chip_id == ap_chip.H5:
     from adafruit_blinka.microcontroller.allwinner.h5.pin import *
 elif chip_id == ap_chip.IMX8MX:
index 4ac63dd15b9deb192ab1dcf01680c19920969805..f69d30e7aef0ddb76c33df7d9cab0b1aff3b6876 100755 (executable)
@@ -18,6 +18,10 @@ elif chip_id == ap_chip.AM33XX:
     from adafruit_blinka.microcontroller.am335x.pin import *
 elif chip_id == ap_chip.SUN8I:
     from adafruit_blinka.microcontroller.allwinner.h3.pin import *
+elif chip_id == ap_chip.H5:
+    from adafruit_blinka.microcontroller.allwinner.h5.pin import *
+elif chip_id == ap_chip.H616:
+    from adafruit_blinka.microcontroller.allwinner.h616.pin import *
 elif chip_id == ap_chip.SAMA5:
     from adafruit_blinka.microcontroller.sama5.pin import *
 elif chip_id == ap_chip.T210:
@@ -56,6 +60,8 @@ elif chip_id == ap_chip.A33:
     from adafruit_blinka.microcontroller.allwinner.a33.pin import *
 elif chip_id == ap_chip.RK3308:
     from adafruit_blinka.microcontroller.rockchip.rk3308.pin import *
+elif chip_id == ap_chip.RK3399:
+    from adafruit_blinka.microcontroller.rockchip.rk3399.pin import *
 elif chip_id == ap_chip.MIPS24KC:
     from adafruit_blinka.microcontroller.atheros.ar9331.pin import *
 elif chip_id == ap_chip.MIPS24KEC:
@@ -66,5 +72,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)
index 2c85f0166ff1b79555e6127563b6c7f78155bb16..00ffa0b8df09026c32605ddf4f78d2a089832ba1 100644 (file)
@@ -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:
index 05263c2a3e9a04cc5ae143c0be8b1d80f8a30266..dbb8bb79777c6ff3a4602c2fd2bd0bb1fffb2037 100644 (file)
@@ -22,13 +22,15 @@ elif detector.board.any_giant_board:
 elif detector.board.any_beaglebone:
     from adafruit_blinka.microcontroller.am335x.sysfs_pwmout import PWMOut
 elif detector.board.any_rock_pi_board:
-    from adafruit_blinka.microcontroller.generic_linux.sysfs_pwmout import PWMOut
+    from adafruit_blinka.microcontroller.rockchip.PWMOut import PWMOut
 elif detector.board.binho_nova:
     from adafruit_blinka.microcontroller.nova.pwmout import PWMOut
 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: