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.post0.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.post0-py3-none-any.whl (184.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pymcu_stdlib-0.1.0a1.post0.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.post0.tar.gz
Algorithm Hash digest
SHA256 a6b2e4a146f7e19f3db01f0b8111a20b277a1aca5e76d51345958619795a530c
MD5 6d18fecedc603908483f1023b023133c
BLAKE2b-256 40b3b06d5f32d6f76b98f8f9a4fa80b902601d6eb393d4bdb524dd41d03e86f5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pymcu_stdlib-0.1.0a1.post0-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.post0-py3-none-any.whl
Algorithm Hash digest
SHA256 b4314676f19e81ff2e3fb71ba700d09ef96a208e58534dbb2846164226c82234
MD5 867796d2566e972a9943c487daf6fb21
BLAKE2b-256 8028200a5287e70fc3cc1ebe730b8e89d30329662e621630f15086786a75021c

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