avr-atomic

Crates.ioavr-atomic
lib.rsavr-atomic
version1.0.0
created_at2025-11-09 19:16:44.713765+00
updated_at2025-11-09 19:16:44.713765+00
descriptionFast atomic load/store without IRQ-disable for AVR
homepagehttps://bues.ch/
repositoryhttps://github.com/mbuesch/avr-atomic
max_upload_size
id1924373
size40,564
Michael Büsch (mbuesch)

documentation

README

Fast atomic load/store without IRQ-disable for AVR

A fast atomic type for 8-bit values on AVR microcontrollers. It is designed to be efficient by avoiding IRQ-disable/restore overhead.

This crate provides a simple and fast way to perform atomic load and store operations on 8-bit values: u8, i8, bool and any other user defined type that can be converted to and from u8.

Usage

Add this to your Cargo.toml:

[dependencies]
avr-atomic = "1"

Example: Basic types u8, i8, bool

use avr_atomic::AvrAtomic;

static VALUE_U8: AvrAtomic<u8> = AvrAtomic::new();
static VALUE_I8: AvrAtomic<i8> = AvrAtomic::new();
static VALUE_BOOL: AvrAtomic<bool> = AvrAtomic::new();

fn foo() {
    VALUE_U8.store(0x42);
    let value = VALUE_U8.load();

    VALUE_I8.store(-42);
    let value = VALUE_I8.load();

    VALUE_BOOL.store(true);
    let value = VALUE_BOOL.load();
}

Example: User defined type

use avr_atomic::{AvrAtomic, AvrAtomicConvert};

#[derive(Copy, Clone)]
struct MyFoo {
    inner: u8,
}

impl AvrAtomicConvert for MyFoo {
    fn from_u8(value: u8) -> Self {
        Self { inner: value }
    }

    fn to_u8(self) -> u8 {
        self.inner
    }
}

static VALUE: AvrAtomic<MyFoo> = AvrAtomic::new();

fn foo() {
    VALUE.store(MyFoo { inner: 2 } );

    let value = VALUE.load();
    assert_eq!(value.inner, 2);
}

License

This project is licensed under either of the following, at your option:

Commit count: 0

cargo fmt