Skip to main content

Standard Library for the PyMCU compiler

Project description

pymcu-stdlib

This package contains the standard library and HAL definitions for PyMCU, a Python-to-microcontroller toolchain that compiles a statically-typed subset of Python directly to bare-metal machine code.

These files provide hardware register mappings, HAL abstractions, and phantom types that allow Python IDEs (PyCharm, VS Code, etc.) to provide autocompletion and type checking for microcontroller code, while being compiled by the pymcuc compiler.

Purpose

Microcontroller code written for PyMCU uses specialized types and access patterns that are not native to standard Python. This package bridges that gap by:

  1. Defining Hardware Registers: Mapping register names (e.g., PORTB, DDRB) to their physical memory addresses for each supported chip.
  2. Providing Type Safety: Using phantom types like uint8, uint16, and ptr[T] to ensure correct data sizing at compile time.
  3. HAL Abstractions: Zero-cost abstractions (Pin, UART, ADC, PWM, SPI, I2C, EEPROM, Watchdog, Power) that inline to chip-specific code at compile time.
  4. Enabling IDE Features: Allowing "Go to Definition", autocompletion, and static analysis while writing firmware in Python.

Supported Chips

  • AVR ATmega family: ATmega48/48P, ATmega88/88P, ATmega168/168P, ATmega328/328P (Arduino Uno)
  • AVR ATtiny (PORTB only): ATtiny85, ATtiny45, ATtiny25, ATtiny13, ATtiny13a
  • AVR ATtiny (PORTA+PORTB): ATtiny84, ATtiny44, ATtiny24
  • AVR ATtiny (PORTD+PORTB): ATtiny2313, ATtiny4313
  • PIC: PIC14 (PIC16F84A, PIC16F877A), PIC14E (PIC16F18877), PIC18 (PIC18F45K50)
  • RISC-V: CH32V003 (experimental)

Installation

uv pip install pymcu

Or with pipx for CLI use:

pipx install pymcu

Usage

Import chip registers and HAL modules in your firmware source:

from pymcu.chips.atmega328p import PORTB, DDRB
from pymcu.types import uint8
from pymcu.hal.gpio import Pin

led = Pin("PB5", Pin.OUT)

while True:
    led.toggle()

For ATtiny targets:

from pymcu.chips.attiny85 import PORTB, DDRB
from pymcu.types import uint8
from pymcu.hal.gpio import Pin

led = Pin("PB0", Pin.OUT)

while True:
    led.toggle()

Note: These files are stubs. Running them directly with a standard Python interpreter will raise RuntimeError if you attempt to access hardware registers — they lack the physical hardware interface and must be compiled with the pymcuc toolchain to run on a microcontroller.

Structure

lib/src/pymcu/
  chips/          -- chip-specific register definitions
    atmega328p.py
    attiny85.py
    attiny84.py
    attiny2313.py
    ...
  hal/            -- zero-cost hardware abstractions
    gpio.py       -- Pin (digital I/O)
    uart.py       -- UART
    adc.py        -- ADC
    pwm.py        -- PWM
    spi.py        -- SPI (hardware)
    softspi.py    -- SPI (bit-bang)
    i2c.py        -- I2C
    eeprom.py     -- EEPROM
    watchdog.py   -- Watchdog timer
    power.py      -- Sleep / power modes
    _gpio/        -- chip-specific GPIO backend implementations
    _uart/        -- chip-specific UART backend implementations
    ...
  types.py        -- ptr[T], uint8, uint16, int8, inline, const, asm, ...
  math/           -- software math routines (division, etc.)

License

MIT License. See LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pymcu_stdlib-0.1.0a1.tar.gz (99.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pymcu_stdlib-0.1.0a1-py3-none-any.whl (184.3 kB view details)

Uploaded Python 3

File details

Details for the file pymcu_stdlib-0.1.0a1.tar.gz.

File metadata

  • Download URL: pymcu_stdlib-0.1.0a1.tar.gz
  • Upload date:
  • Size: 99.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.6 {"installer":{"name":"uv","version":"0.10.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for pymcu_stdlib-0.1.0a1.tar.gz
Algorithm Hash digest
SHA256 a62422e0a5274b76207977653453dfe2a8a4346ca0a0520f6f92b1dcb3a9a61b
MD5 6f858eea04e96d7c15cde89206be7be3
BLAKE2b-256 9da3adaf0837c5e4819474b6a3daefe463e1f36e2529f1fa4a37af3337ccdce8

See more details on using hashes here.

File details

Details for the file pymcu_stdlib-0.1.0a1-py3-none-any.whl.

File metadata

  • Download URL: pymcu_stdlib-0.1.0a1-py3-none-any.whl
  • Upload date:
  • Size: 184.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.6 {"installer":{"name":"uv","version":"0.10.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for pymcu_stdlib-0.1.0a1-py3-none-any.whl
Algorithm Hash digest
SHA256 a0e13d6dbdfec00d48ad8069b6937c132bfd13e6faf2c309ea32873351d8c760
MD5 4fcb9f2abfdf023fecb2ca04893aa58c
BLAKE2b-256 955dba714f2a03bb5318ba20ead7e42cfdd8a0e4f0168cb7d1e361ea8d08f4c3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page