nrf-hal Documentation

repository·master·Indexed 20 days ago

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

Hardware Abstraction Layer (HAL) implementations for Nordic Semiconductor's nRF51, nRF52, and nRF91 microcontroller families, following the embedded-hal traits. The repository includes various demos for peripherals such as GPIO, AES-CCM/ECB encryption, I2S, PWM, QDEC, and Programmable Peripheral Interconnect (PPI).

Tokens
22.1K
Snippets
98
Records
140
Agent score
64%

What's inside nrf-hal

  1. What is nrf-hal?

    master
    The nrf-hal project provides Hardware Abstraction Layer (HAL) implementations for the nRF51, nRF52, and nRF91 families of microcontrollers. It is designed to work with the embedded-hal ecosystem, allowing for portable and type-safe hardware access on Nordic Semiconductor chips.
  2. What is Programmable Peripheral Interconnect (PPI)?

    master

    Programmable Peripheral Interconnect (PPI) is a feature that allows different peripherals on the chip to interact with each other directly. This interaction occurs without requiring CPU intervention, which reduces latency and CPU overhead.

    In this specific demo, the Bluetooth RADIO peripheral is used as an example of a peripheral interacting via PPI, though the demo does not implement specific Bluetooth functionality.

  3. What is the QDEC peripheral?

    master

    The Quadrature Decoder (QDEC) is a peripheral used to read sensor information from quadrature-encoded signals, commonly produced by mechanical or optical sensors. It supports buffered decoding of these signals.

    This specific demo uses an nRF52840 paired with a Bourns PEC11R rotary encoder to show how rotation can trigger interrupts and update state variables.

  4. Configure Secure vs Non-Secure mode for nRF9160

    master

    This HAL is designed for non-secure mode. Because the nRF9160 uses Arm TrustZone, you cannot run this HAL directly from a secure bootloader. You must use a bootloader (like Nordic's Secure Partition Manager) to move peripherals into the 'non-secure' world before jumping to your application.

    Memory Layout Requirements

    When using Nordic's Secure Partition Manager (SPM) v1.5.1, the non-secure (NS) application is expected to start at address 0x0005_0000. You must configure your application's memory.x linker script to reflect this start address for Flash.

    Verifying the Start Address

    You can verify the expected start address by observing the boot-up logs. If you see: SPM: NS image at 0x50000 This confirms the SPM is looking for your non-secure image at 0x0005_0000.

  5. Configure Secure vs Non-Secure mode for nRF5340

    master

    The nrf5340-app-hal is designed to run in non-secure mode. To use it, you must use a bootloader (like Nordic's Secure Partition Manager) that starts in secure mode, configures peripherals for the non-secure world, and then jumps to your application.

    Memory Layout Requirements

    When using Nordic's Secure Partition Manager (SPM) v1.7, your application is expected to start at flash address 0x0005_0000. You must configure your memory.x file to reflect this start address.

    Verifying the Start Address

    You can verify the expected non-secure (NS) image address by checking the boot-up logs. For example: SPM: NS image at 0x50000 indicates the SPM is looking for the image at 0x0005_0000.

    Customizing Flash Addresses

    If you need to change the flash address, provide a custom memory.x file in your application crate (or Board Support Crate) and use a build.rs file to copy it into the build directory.

  6. Run the QDEC demo

    master

    To run the Quadrature Decoder (QDEC) demo, use cargo-embed with the appropriate release flag and target for the nRF52840. This demo demonstrates using a rotary encoder to trigger interrupts and update variables based on rotation amount.

    $ cargo embed --release --target=thumbv7em-none-eabihf
  7. Set up the cross-compilation toolchain

    master

    To compile code for nRF chips, you must install the appropriate target for your specific hardware. If you encounter eh_personality compilation errors, it is a sign that the target has not been set correctly.

    For an nRF52840 chip, install the thumbv7em-none-eabihf target:

    rustup target add thumbv7em-none-eabihf
  8. Use the RTIC demo as a concurrency template

    master
    The rtic-demo provides a bare-bones template for building applications using the Real-Time Interrupt-driven Concurrency (RTIC) framework. While many other demos in the nrf-hal repository demonstrate specific hardware features (like CCM, ECB, or GPIOTE) using RTIC, this specific demo serves as a starting point for understanding how to structure a concurrent program on nRF hardware using the RTIC framework.
  9. Run the CCM encryption demo

    master

    The CCM encryption demo demonstrates AES-CCM (cipher block chaining) encryption and decryption. It encrypts a 251-byte message, measures processing time, and then repeats the process with progressively smaller data chunks to show performance scaling.

    To run this demo, you must select a microcontroller from the following supported series:

    • 52805
    • 52810
    • 52811
    • 52832
    • 52840

    This demo uses the rtt-target crate for communication/logging.

    $ cargo embed --release --features=52832 --target=thumbv7em-none-eabihf
  10. Run the WDT demo

    master

    To run the WDT demo using cargo-embed, use the following command. Ensure you specify the correct release profile and target architecture for your hardware (e.g., thumbv7em-none-eabihf).

    To see actual panic messages during a crash, use probe-run instead of standard logging.

    $ cargo embed --release --target=thumbv7em-none-eabihf