USB to interfaces implementing MicroPython "machine" module functionalities on a computer.

Related tags

Miscellaneous u2if
Overview

u2if project

u2if(USB to interfaces) is an attempt to implement some of the MicroPython "machine" module functionalities on a computer. The goal is to be able to communicate with breakout boards (sensors, lcd...) simply from a python program on x86 machine. It uses a Raspberry PICO microcontroller to make the interface between the computer (USB) and peripherals/protocols.

Python led swith on/off:

import time
from machine import u2if, Pin

# Initialize GPIO to output and set the value HIGH
led = Pin(u2if.GP_3, Pin.OUT, value=Pin.HIGH)
time.sleep(1)
# Switch off the led
led.value(Pin.LOW)

Caution

It is in experimental state and not everything has been tested. Use with caution. It is supposed to work on windows, linux and Mac. To work in Linux in non-root, an udev rules has to be added (See Firmware readme).

Why this project ?

When I want to retrieve values from a sensor or even to play with a led or a button from the PC, I make an arduino program that communicate to the PC via the serial port. That umplies to define a serial "protocol" between the PC and the arduino and it is not necessarily reusable because it is specific.

Solutions already exist, for example Blinka from Adafruit via the FT2232H in CircuitPython or pyftdi in Python with the same IC.

Nevertheless I find it interesting to implement a majority of the functionalities of the machine module and add other protocols.

Implemented Interfaces

The following features are coded:

  • machine.Pin: input (+irq, +debounced), output (+pull down/up).
  • machine.Signal
  • machine.ADC: read (12bits)
  • machine.UART
  • machine.I2C
  • machine.SPI
  • machine.PWM
  • WS2812B led
  • I2S

Licenses and Project directories

This repository is presented as the sources of a python project (License). But it also contains the following projects:

How to use it

Upload PICO firmware

See Firmware README

Install u2if python package

Install python package from release file (u2if-..tar.gz) with python3 -m pip install dist/u2if-..tar.gz

Build u2if python package

To build package if wanted :

  • If needed, install build: python3 -m pip install --upgrade build
  • Build it: python3 -m build

Examples

There is no documentation but examples can help to use this library :

  • Led On/Off
  • Switch with or whithour irq and debouncing
  • PWM controlling servo-motor
  • Display testing: SSD1306 (I2C and SPI), GC9A01 (round lcd)
  • Some sensors simple test: MPU9250 (IMU), VL53L0X (range), BMP280(Temp)
  • UART read/Write
  • Analog read.
  • Rotrary encoder
  • I2C scan
  • Play 48khz 16bit .wav over I2S DAC (PAM5102)
  • ...

u2if pinout

For simplicity, the pins of the SPI, I2C and UART devices have been fixed. If a peripheral is not used, its pins can be used as a classic I/O.

Troubleshooting

Import error using MicroPython module

import ustruct

The ustruct module is belongs by micropython. There is a micropython-cpython-ustruct compatibility module, but it doesn't seem to work for me. If necessary modify:

import ustruct

to:

import struct as ustruct

import utime & const

Install micropython-cpython-utime and micropython-cpython-micropython.

Issues
  • Interested in adding other RP2040 boards?

    Interested in adding other RP2040 boards?

    Hey @execuc , this isn't an issue.

    First, thanks for writing this cool firmware. I did the work to add the additional support needed to allow using the Pico w/ u2if firmware from a PC via the Blinka library: https://learn.adafruit.com/circuitpython-libraries-on-any-computer-with-raspberry-pi-pico It all worked really well!

    I've also done some simple modifications to allow adding other RP2040 based boards: https://github.com/adafruit/u2if/pull/1 which generally came down to a pin re-mapping. Would you be interested in a pull request to add that capability here?

    Also - curious what build environment you used for your release artifacts? I was originally using gcc-arm-none-eabi-10-2020-q4-major to build for the PR linked above and had issues with it creating an infinite loop inside StreamedInterface::streamRxRead. Eventually tried just switching to gcc-arm-none-eabi-7-2018-q2-update which worked as expected. Really weird issue.

    opened by caternuson 4
  • i2c frequency accuracy

    i2c frequency accuracy

    Great project, I'm really excited to have i2c access on my Ubuntu desktop. I was trying your scan_i2c.py example and measured the SCL period with a logic analyzer and oscilloscope.

    i2c = I2C(i2c_index=0, frequency=400000) resulted in a frequency of 357kHz (about 11% lower) requesting frequency=450000 measured exactly 400kHz

    i2c = I2C(i2c_index=0, frequency=1000000) resulted in a frequency of 791kHz (about 21% lower) requesting frequency=1350000 measured exactly 1MHz

    I'm fine using these new values for my scripts, but I thought I would pass on the observation.

    opened by charkster 3
  • Allow for custom pin mappings and some other tweaks.

    Allow for custom pin mappings and some other tweaks.

    These are changes to just the firmware aspect of the code repository. As discussed in #2 .

    The main change here is a simple pin mapping capability driven by pre-processor logic. This allows creating u2if firmware builds for other RP2040 based boards that route different pins out. The pins are defined in pins.h and then mapped to u2if in PicoInterfacesBoard.h. Then in CMakeLists.txt, set the target board before building. I'm super unfamiliar with cmake, so there may be a better "cmake way" of doing this?

    The original variable names in PicoInterfacesBoard.h were left in place. For example:

            GP18_SPI0_CK = U2IF_SPI0_CK,
    

    may not have anything to do with GP18. It's just "pin to use for index 0 SPI clock". This was done to simply allow changing as little code as possible.

    Also, currently all interfaces are defined and left enabled. For boards that don't route out all of these, unused pins are specified. Might make sense to have the interfaces be enabled conditionally also. But deferring that work for now.

    Additional changes are:

    • Includes pin mappings for several Adafruit RP2040 based boards
    • Change to including pico-sdk as a submodule
    • Minor code fixes that help when building firmware with newer gcc versions
    opened by caternuson 2
  • change _i2c_scan from doing writes to a single read

    change _i2c_scan from doing writes to a single read

    I was not able to detect VCNL4010 ambient light and proximity sensor as it will not ACK a write to an invalid internal address (address 0x00 is not valid). It will ACK to a read of an invalid address. As micropython does not have a I2C quick command, a proper I2C scan is not possible to implement... so as a compromise a single byte read should be attempted instead of a multi-byte write. As most i2c device reads will use a repeated start after the address is written, a single byte read will most likely be ACK-ed by the majority of devices.

    opened by charkster 0
  • i2c.scan() implemented as multi-byte writes

    i2c.scan() implemented as multi-byte writes

    I was not able to detect the VCNL4010 ambient light sensor, as it will not ACK to a write to an invalid internal address (0x00 is not valid). This has the effect of the sensor not being detected from the i2c.scan() function. A further look at the i2c.scan() function sees it implement as multi-byte writes, which is not the spec recommended I2C quick commands.

    rpi_pico_i2c_scan_failure_vcnl4010

    opened by charkster 1
  • USB to UART bridge similar to the MCP2221

    USB to UART bridge similar to the MCP2221

    Would it be possible to implement a USB-to-UART bridge that shows up on USB-CDC similar to how the MCP2221a has the USB-HID and USB-CDC which maps the TX and RX pins to /dev/ttyACM0 on your laptop?

    I found this project https://github.com/Noltari/pico-uart-bridge but I would like to combine that functionality with the u2if firmware.

    opened by shaunmulligan 0
  • Adds support for Adafruit's MacroPad RP2040

    Adds support for Adafruit's MacroPad RP2040

    TODO

    • [x] Get new PID from Adafruit
    • [x] Create PRs for the following two repositories
      • [x] https://github.com/adafruit/Adafruit_Python_PlatformDetect
      • [x] https://github.com/adafruit/adafruit_blinka
    opened by jfurcean 4
  • Multiple retries may be necessary

    Multiple retries may be necessary

    While testing U2IF I've noticed that it sometimes takes more than one try to connect to the device:

    >>> dev = machine.Device()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/mnt/projects/u2if/.env/lib/python3.7/site-packages/source/machine/helper.py", line 9, in __call__
        cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
      File "/mnt/projects/u2if/.env/lib/python3.7/site-packages/source/machine/u2if.py", line 22, in __init__
        raise ValueError("No board found")
    ValueError: No board found
    >>> dev = machine.Device()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/mnt/projects/u2if/.env/lib/python3.7/site-packages/source/machine/helper.py", line 9, in __call__
        cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
      File "/mnt/projects/u2if/.env/lib/python3.7/site-packages/source/machine/u2if.py", line 22, in __init__
        raise ValueError("No board found")
    ValueError: No board found
    >>> dev = machine.Device()
    >>> dev.firmware_version
    [0, 5, 0]
    >>> dev.vid
    51966
    >>> dev.pid
    16389
    

    In the example above it took 3 attempts to connect to a RP2040 Pico. Given that the hid library seems to only return null/None with no apparent errors thrown would it be possible to add some kind of connection retry mechanism?

    opened by lesamouraipourpre 0
  • UDEV rules improvement

    UDEV rules improvement

    Below is a possible improvement to the udev-rules. It is based on the suggest udev-rules in the libusb/hidraw project Ref: https://github.com/libusb/hidapi/blob/6fcb0bb2282dbb505561a53689eba0c0536570d9/udev/69-hid.rules

    # PICO
    #KERNEL=="hidraw*", ATTRS{idVendor}=="cafe", ATTRS{idProduct}=="4005", TAG+="uaccess", GROUP="plugdev", MODE="0660"
    # Adafruit Feather
    #KERNEL=="hidraw*", ATTRS{idVendor}=="239a", ATTRS{idProduct}=="00f1", TAG+="uaccess", GROUP="plugdev", MODE="0660"
    # Adafruit ItsyBitsy
    #KERNEL=="hidraw*", ATTRS{idVendor}=="239a", ATTRS{idProduct}=="00fd", TAG+="uaccess", GROUP="plugdev", MODE="0660"
    # Adafruit QT2040 Trinkey
    #KERNEL=="hidraw*", ATTRS{idVendor}=="239a", ATTRS{idProduct}=="0109", TAG+="uaccess", GROUP="plugdev", MODE="0660"
    # Adafruit QTPY
    #KERNEL=="hidraw*", ATTRS{idVendor}=="239a", ATTRS{idProduct}=="00f7", TAG+="uaccess", GROUP="plugdev", MODE="0660"
    

    I have tested this connected from a Raspberry Pi 4 to a RP2040 Pico and they work

    > ls -l /dev/ttyACM0 
    crw-rw---- 1 root dialout 166, 0 Jun 29 16:46 /dev/ttyACM0
    
    > ls -l /dev/hidraw*
    crw-rw----+ 1 root plugdev 244, 2 Jun 29 16:46 /dev/hidraw2
    
    opened by lesamouraipourpre 1
Releases(1aaed6a)
Linux USB driver for the MOTU AVB series interfaces

motu-avb Linux USB driver for the MOTU AVB series interfaces Kernel parameters: samplerate: set the samplerate (its currently fixed at module load) de

null 42 Jul 20, 2022
MicroPython for M5Camera and OpenMV.

M5Camera_OpenMV このリポジトリは、M5Camera+OpenMVのMicroPython環境です。 以下リポジトリのソースコードをベースとして環境を構築しています。 This repository is a MicroPython for M5Camera and OpenMV. I

null 10 Jul 22, 2022
Bootloader for mimrxt MicroPython port

iMX RT10XX bootloader Bootloader for mimrxt MicroPython port The bootloader must be programed into RT10XX qspi Flash (or Hyperflash, not tested) using

Boris Lovosevic 3 May 26, 2022
ESP32 software USB host through general IO pins. We can connect up to 4 USB-LS HID (keyboard mouse joystick) devices simultaneously.

esp32_usb_soft_host esp32 USB-LS pure software host thru general IO pins. Up to 4 HID devices simultaneously. board ~$3 :https://www.aliexpress.com/pr

Samsonov Dima 274 Aug 10, 2022
ESP8266 software USB host through general IO pins. We can connect up to 2 USB-LS HID (keyboard mouse joystick) devices simultaneously.

esp8266_usb_soft_host Test for esp8266 usb host . Works with ESP-IDF v3.4 80MHz and 160MHz. WorkInProgress Test run with mouse & combined mouse & keyb

Samsonov Dima 27 Jul 30, 2022
Tiny and portable usb host and device stack for mcu with usb ip

Tiny and portable usb host and device stack for mcu with usb ip

sakumisu 404 Aug 2, 2022
USB-C_PCB_experiments - USB-C ports made from a flex PCB and an ATtiny84A

USB-C PCB Experiments This is part of an ongoing series of projects involving creative interpretations of the USB mechanical standards. You've probabl

Sam Ettinger 22 May 21, 2022
Anotter USB temperature logger that can record up to four channels with thermocouple or NTCs connected via CDC directly or SCPI to USB.

temperature-logger Anotter USB temperature logger that can record up to four channels with thermocouple or NTCs connected via CDC directly or SCPI to

Jana Marie Hemsing 47 Apr 6, 2022
A fully-functional open source and open hardware mechanical USB computer keyboard with only three keys!

threeboard threeboard is a fully-functional open source and open hardware mechanical USB computer keyboard with only three keys. It supports multiple

Conor Taylor 97 May 23, 2022
PLP Project Programming Language | Programming for projects and computer science and research on computer and programming.

PLPv2b PLP Project Programming Language Programming Language for projects and computer science and research on computer and programming. What is PLP L

PLP Language 5 Jun 23, 2022
CSC404: Computer Graphics [CG] & CSL402: Computer Graphics Lab [CG Lab]

COMPUTER-GRAPHICS-AND-COMPUTER-GRAPHICS-LAB CSC404: CG & CSL402: CG LAB [SEMESTER IV] Syllabus CG - Reference Books THE WALL MEGA SATISH - AUTHOR CG C

AMEY THAKUR 7 Apr 28, 2022
Well-organized, commented and documented sample project that shows the basic functionalities of the 42's mlx library.

miniLibX sample | slucas-s I developed this sample project to play around with the basic functionalities of the miniLibX, the simple graphics library

S. Lucas Serrano 40 Jul 28, 2022
A multiplatform C++ library for common and basic system functionalities.

axl.cm A multiplatform C++ library for common and basic system functionalities. Platforms Linux Windows C++ standard minimum: C++98 target: C++11 maxi

Axel 2 Apr 4, 2022
A model checker for the Dynamic Logic of Propositional Assignments (DL-PA) with solving and parameterized random formula generation functionalities.

A model checker for the Dynamic Logic of Propositional Assignments (DL-PA) with solving and parameterized random formula generation functionalities.

Jeffrey Yang 7 Dec 31, 2021
ozz-animation provides runtime character animation playback functionalities (loading, sampling, blending...)

ozz-animation open source c++ 3d skeletal animation library and toolset ozz-animation provides runtime character animation playback functionalities (l

Guillaume Blanc 1.7k Aug 11, 2022
An USB Rubberducky example for ESP32-S2 module with SD Card 🦆

ESP32-S2 WUD-Ducky This project is an attempt to add USB HID to the default example provided with @AprilBrother's WUD (*), however it also supports an

tobozo 53 Aug 8, 2022
An IOS module that fakes Wiimotes from the input of USB game controllers

fakemote An IOS module that fakes Wiimotes from the input of USB game controllers. Supported USB game controllers Device Name Vendor Name Vendor ID Pr

Sergi Granell 81 Aug 6, 2022
Müsli USB Pmod-compatible module

Müsli USB Pmod™ compatible module Overview Müsli is a 12-pin Pmod™ compatible module with a RP2040 MCU and a USB type A port. Müsli can be configured

Machdyne 6 Apr 14, 2022