moonbit-mcu-hal

    MoonBit Microcontroller Hardware Abstraction Layer & Simulation Suite

    mcu
    hal
    hardware
    embedded
    gpio
    uart
    spi
    i2c
    simulation
    Download zip
    Author
    Version
    0.2.1
    License
    Apache-2.0
    Last updated
    2 months ago
    Downloads
    40

    No dependencies

    #moonbit-mcu-hal

    CI License MoonBit

    moonbit-mcu-hal is a pure-MoonBit hardware-abstraction and simulation toolkit for embedded applications. It provides typed peripheral models, deterministic in-memory backends, protocol codecs, board profiles, and application-level building blocks that can be tested without physical hardware.

    #Features

    • GPIO, UART, SPI, I²C, PWM, ADC, timers, RTC, DMA, locks, RTOS scheduling, and device-tree parsing.
    • STM32F4, RP2040 Pico, and ESP32 peripheral adapters.
    • Virtual MMIO, GPIO, EEPROM, SPI flash, and bus components for zero-hardware testing.
    • CRC-8, CRC-16/MODBUS, COBS, framed packets, varuints, retry policies, and stream assembly.
    • Bounded queues, priority queues, bitmaps, statistics, fixed-point PID control, debouncing, and rate limiting.
    • Sensor calibration/filtering, environmental measurements, monochrome framebuffers, clipped drawing, numeric glyphs, and battery icons.
    • Persistent journals, wear-leveling accounting, power budgets, health counters, trace buffers, watchdogs, boot validation, and rollback selection.
    • Board resources, endpoint routing, USB control transfers, flash semantics, access capabilities, token buckets, date/time validation, and endian-safe utilities.

    #Package map

    PackagePurpose
    types, errorsShared hardware types and error taxonomy
    gpio, uart, spi, i2cCore digital and serial peripherals
    pwm, adc, timer, rtc, dmaTiming, analog, and transfer peripherals
    lock, rtosResource protection and cooperative scheduling
    sim, bspDeterministic virtual hardware and board support
    stm32, rp2040, esp32Vendor-family register adapters
    dts, registersDevice-tree descriptors and register transactions
    codec, protocol, collectionsBinary transport and embedded data structures
    sensors, display, control, telemetryApplication-facing embedded building blocks
    storage, flash, memoryPersistence, flash behavior, and predictable allocation
    power, diagnostics, boot, securityReliability and product lifecycle services
    board, network, usb, time, utilsIntegration and low-level helpers

    #Quick start

    moon version moon check --deny-warn moon test --deny-warn moon run src/main

    src/main exercises the clock, GPIO, UART, SPI, I²C EEPROM, PWM, ADC, timers, DMA, locks, simulator, vendor adapters, device tree, RTOS, and board support packages together.

    #Example

    let clock = @clock_delay.HalClock::default_clock()
    let delay = @clock_delay.HalDelay::new(clock)
    delay.delay_ms(10)
    let pin = @types.PinSpec::default_pin(@types.PortId::PortA, 5)
    let gpio = @gpio.GpioPin::new(@gpio.GpioConfig::default_output(pin))
    let _ = gpio.set_high()
    let eeprom = @sim.At24c02Eeprom::attach(
    @i2c.I2cMaster::new(@i2c.I2cConfig::default_config()),
    @i2c.I2cAddress::Addr7Bit(0x50),
    )
    let _ = eeprom.write_byte(0x10, (0x77 : Byte))
    let value = eeprom.read_byte(0x10).unwrap()
    println(value.to_int().to_string())

    #Verification

    moon fmt --check moon check --deny-warn moon info git diff --exit-code moon test --deny-warn moon test --target native --deny-warn moon test --enable-coverage --deny-warn moon coverage report -f summary moon coverage analyze

    The current local run contains 134 passing tests. Coverage is reported by the MoonBit toolchain rather than copied into a manually maintained percentage.

    #Benchmark

    moon run src/bench

    Measured locally with the wasm backend:

    iterations=1000 payload_bytes=256 crc16_elapsed_us=0 checksum=0 cobs_elapsed_us=0 encoded_bytes=257000 queue_elapsed_us=0 remaining=0

    The simulated clock has resolution coarser than these short loops, so elapsed 0 is the actual observed value. Workload size and encoded output remain deterministic; use a board timer for hardware latency comparisons.

    #Source scale

    $source = Get-ChildItem -Recurse src -Filter *.mbt | Where-Object { $_.Name -notlike '*_test.mbt' } $source | Get-Content | Measure-Object -Line

    The current formatted tree contains 8,765 non-test .mbt source lines and 1,297 test lines across 145 .mbt files. Re-run the command after changes; the result is not generated into source code.

    #Publishing

    The module is configured for Mooncakes in moon.mod. After local verification and a version update:

    moon publish

    The GitHub Actions publish workflow is tag-triggered and expects registry credentials from repository or environment secrets. No token is stored in the repository.

    #License

    Apache-2.0. See LICENSE.