rp-hal Documentation

repository·main·Indexed 24 days ago

https://github.com/rp-rs/rp-hal

A collection of high-level Rust Hardware Abstraction Layers (HALs) for Raspberry Pi RP2040 and RP235x microcontrollers. Includes rp2040-hal, rp235x-hal, and supporting crates like rp-binary-info and rp-hal-common. Provides guidance on toolchain setup for thumbv6m, thumbv8m, and RISC-V targets, flashing via probe-rs or picotool, and utilizing Board Support Packages (BSPs).

Tokens
23.4K
Snippets
37
Records
144
Agent score
83%

What's inside rp-hal

  1. Overview of rp-hal packages

    main

    The rp-hal repository is organized into several crates depending on your target hardware and needs:

    Hardware Abstraction Layers (HALs)

    • rp2040-hal: High-level drivers for the RP2040's internal peripherals (SPI, I2C, etc.). It is hardware-agnostic regarding board pin-outs.
    • rp235x-hal: High-level drivers for the RP235x family (including RP2350A, RP2350B, RP2354A, and RP2354B). It supports both ARM and RISC-V modes.

    Supporting Crates

    • rp2040-hal-examples / rp235x-hal-examples: Practical examples for GPIO, I2C, SPI, UART, PWM, PIO, and more.
    • rp-binary-info: A library used to generate picotool compatible metadata (name, version, etc.) within your Rust binaries.
    • rp-hal-common: Shared code used internally to unify the two HALs.
    • Board Support Packages (BSPs): Available in a separate repository for specific boards like the Raspberry Pi Pico.
  2. Overview of rp235x-hal

    main

    The rp235x-hal package provides high-level Rust drivers for the Raspberry Pi RP235x microcontroller family, including:

    • RP2350A
    • RP2350B
    • RP2354A
    • RP2354B

    It implements the embedded-hal hardware abstraction interfaces. It is designed to be used directly for generic RP235x development or via Board Support Packages (BSPs) for specific hardware configurations.

  3. Use Board Support Packages (BSPs) instead of raw HAL examples

    main

    The rp2040-hal package provides high-level drivers, but the examples in this folder are non-board specific.

    If you are using a specific development board (like a Raspberry Pi Pico), it is recommended to use a Board Support Package (BSP) crate. BSPs pre-configure the pins according to the specific PCB design, making them easier to use than the generic HAL.

    You can find supported boards in the rp-hal-boards repository.

  4. Hardware wiring requirements for target tests

    main

    The existing test suite requires specific physical pin connections on the hardware (e.g., a Raspberry Pi Pico) to perform loopback testing. If you add new tests, ensure they are compatible with this existing configuration so the entire suite can run with a single setup.

    Required Connections:

    Test TypeGPIO ConnectionPico Pin Mapping
    SPI LoopbackGPIO 4 $\leftrightarrow$ GPIO 7Pins 6 and 10
    I2C LoopbackGPIO 0 $\leftrightarrow$ GPIO 2 AND GPIO 1 $\leftrightarrow$ GPIO 3Pins 1 & 4 AND Pins 2 & 5
  5. Understand the difference between HAL and BSP

    main

    When developing for RP235x, you have two main choices for hardware abstraction:

    1. rp235x-hal (Hardware Abstraction Layer): A library of high-level Rust drivers for the RP235x family. It is generic and does not assume a specific pinout or board layout. Use this if you are designing your own PCB or need low-level peripheral control.
    2. Board Support Packages (BSPs): These crates wrap the HAL and pre-configure pins according to a specific PCB design (e.g., a Pico board).

    Recommendation: If you are using a supported development board, use the corresponding BSP crate instead of the HAL directly to avoid manual pin configuration.

  6. Use Board Support Packages (BSPs) instead of rp2040-hal

    main

    The rp2040-hal crate provides high-level drivers and implements generic embedded-hal traits. However, if you are using a specific development board (like a Raspberry Pi Pico), you should use a Board Support Package (BSP) crate instead.

    BSPs use rp2040-hal internally but pre-configure the pins and peripherals according to the specific PCB design of that board, making it easier to get started with hardware-specific layouts.

  7. Embedded-HAL trait compatibility

    main
    The rp2040-hal crate implements traits from the embedded-hal ecosystem. It supports both version 0.2 and version 1.0 of embedded-hal simultaneously, allowing you to use different drivers incrementally as you upgrade your codebase.
  8. Build RP235x examples

    main

    You can build specific examples using cargo build with the --bin flag.

    Build for Arm mode (Default)

    By default, cargo build targets Arm mode. To build a specific example like blinky:

    $ cargo build --bin blinky

    Build for RISC-V mode

    To build for RISC-V, you must explicitly specify the target:

    $ cargo build --target=riscv32imac-unknown-none-elf --bin blinky

    Build with optimizations

    To build a production-ready binary, add the --release flag to either command.