Expand description
§embassy-atmega328p
An experimental, Embassy-compatible, no_std hardware abstraction layer for the Microchip ATmega328P.
This is an unofficial community project and is not an official Embassy project.
§Status
The initial implementation provides:
- GPIO ownership for ports B, C and D
- Floating-input and internal pull-up configurations
- Push-pull output pins
embedded-hal1.0 digital traits- Blocking USART0
- Blocking SPI master
- Blocking TWI/I2C master
- Blocking 10-bit ADC
- Timer0 and Timer2 fast PWM
- Bit-banged Dallas/Maxim 1-Wire with CRC-8
- Optional Embassy time driver using Timer1
- 16 MHz and 8 MHz clock configurations
- An Embassy Executor async blinky example
GPIO external interrupts and asynchronous peripheral transfers are not implemented yet.
§Installation
Add the crate to your application:
[dependencies]
embassy-atmega328p = "0.1.0"The default configuration enables the ATmega328P runtime and assumes a 16 MHz CPU clock, as used by a standard Arduino Uno or classic Arduino Nano.
§Async 16 MHz application
[dependencies]
embassy-atmega328p = {
version = "0.1.0",
features = ["time-driver-16mhz"]
}
embassy-executor = {
version = "0.10.0",
features = ["nightly", "platform-avr", "executor-thread"]
}
embassy-time = "0.5.1"
avr-device = {
version = "0.8.1",
features = ["atmega328p", "rt"]
}
panic-halt = "1.0"§Using an 8 MHz clock
Disable the default 16 MHz configuration and enable the 8 MHz time driver:
embassy-atmega328p = {
version = "0.1.0",
default-features = false,
features = ["time-driver-8mhz"]
}The selected feature must match the microcontroller’s actual clock and fuse configuration. Otherwise, all timer durations will be incorrect.
Do not enable clock-16mhz and clock-8mhz at the same time.
§Example
Complete examples are available in the repository:
https://github.com/nursude355/embassy-sude/tree/main/embassy-atmega328p/example
§Basic GPIO example
#![no_std]
#![no_main]
use embassy_atmega328p::gpio::{Level, Output};
use embedded_hal::digital::StatefulOutputPin;
use panic_halt as _;
#[avr_device::entry]
fn main() -> ! {
let peripherals = embassy_atmega328p::init();
let mut led = Output::new(peripherals.PB5, Level::Low);
loop {
led.toggle().unwrap();
}
}On Arduino Uno and classic Nano boards, PB5 is connected to digital pin 13.
PC6 is normally used as the reset pin. Do not use it as GPIO unless the reset-disable fuse has deliberately been programmed.
§Peripheral pin map
| Peripheral | Pins | Notes |
|---|---|---|
| USART0 | PD0/RX, PD1/TX | Blocking 8N1 |
| TWI/I2C | PC4/SDA, PC5/SCL | External pull-ups required |
| SPI | PB2/SS, PB3/MOSI, PB4/MISO, PB5/SCK | Master mode |
| ADC | PC0 through PC5 | Blocking 10-bit conversions |
| PWM0 | PD6/OC0A, PD5/OC0B | Timer0, 8-bit fast PWM |
| PWM2 | PB3/OC2A, PD3/OC2B | Timer2, 8-bit fast PWM |
| 1-Wire | Any GPIO | External pull-up normally required |
Pin ownership prevents the same pin from being safely used by two peripherals simultaneously.
§Timer1 ownership
When time-driver-16mhz or time-driver-8mhz is enabled, Timer1 is reserved by the Embassy time driver.
Applications must not reconfigure Timer1 while a time-driver feature is enabled. Timer0 and Timer2 remain available for PWM or application-specific use.
§AVR toolchain
Install the nightly toolchain and Rust source component:
rustup toolchain install nightly
rustup component add rust-src --toolchain nightlyCreate .cargo/config.toml inside the AVR application:
[build]
target = "avr-none"
[unstable]
build-std = ["core"]
[target.avr-none]
rustflags = [
"-C",
"target-cpu=atmega328p",
]Application profiles must abort on panic:
[profile.dev]
panic = "abort"
[profile.release]
panic = "abort"The final link step requires an AVR GNU toolchain that provides avr-gcc.
§License
Licensed under either of:
- Apache License, Version 2.0
- MIT License
at your option.
Modules§
- adc
- Blocking 10-bit analog-to-digital converter.
- delay
- Cycle-counted blocking delays.
- gpio
- General-purpose input/output.
- i2c
- Blocking TWI/I2C master driver.
- onewire
- Blocking Dallas/Maxim 1-Wire bus using any GPIO pin.
- pwm
- Fast-PWM outputs backed by Timer0 and Timer2.
- spi
- Blocking SPI master driver.
- usart
- Blocking USART0 driver.
Structs§
- Peripherals
- All peripheral ownership tokens exposed by this HAL.
Constants§
- CPU_HZ
- CPU clock frequency selected at compile time.