#avr #delay #arduino

nightly no-std avr_delay

A crate for AVR providing software-based delay functions

5 releases

0.3.2 Feb 13, 2021
0.3.1 Jul 25, 2020
0.3.0 Jul 25, 2020
0.2.1 Jul 24, 2020
0.2.0 Jul 24, 2020

#865 in Embedded development

Download history 18/week @ 2022-11-27 38/week @ 2022-12-04 35/week @ 2022-12-11 32/week @ 2022-12-18 25/week @ 2022-12-25 9/week @ 2023-01-01 22/week @ 2023-01-08 9/week @ 2023-01-15 43/week @ 2023-01-22 42/week @ 2023-01-29 25/week @ 2023-02-05 20/week @ 2023-02-12 44/week @ 2023-02-19 4/week @ 2023-02-26 39/week @ 2023-03-05 11/week @ 2023-03-12

99 downloads per month
Used in ruduino

MIT/Apache

9KB

avr-delay

Crates.io

API Documentation

The intent of this library is to provide avr specific delay routines similar to the ones provided by the arduino library. The public functions are:

$AVR_CPU_FREQUENCY_HZ

This crate uses the avr-config crate for fetching the CPU frequency. As such, the AVR_CPU_FREQUENCY_HZ environment variable will need to be set when compiling your crate for AVR.

Example:

export AVR_CPU_FREQUENCY_HZ=16000000
cargo build -Z build-std=core --target avr-atmega328p.json --release
delay(count: u32)

is a raw delay loop. Each loop is 4 cycles. The asm section can loop 65536 times. Initial overhead is about 13 cycles. Each outer loop has an overhead of about 11 cycles.

delay_us(us: u32)

delay us microseconds

delay_ms(ms: u32)

delay ms milliseconds

A simple example of how to use it follows.

Cargo.toml:

    [package]
    name = "dlyblink"
    version = "0.1.0"
    authors = ["John Jorgensen"]

    [dependencies]
    arduino = "0.1"
    avr_delay = { git = "https://github.com/avr-rust/delay" }

and your main.rs:

#![feature(asm, lang_items, unwind_attributes)]

#![no_std]
#![no_main]

extern crate arduino;
extern crate avr_delay;

use arduino::{DDRB, PORTB};
use core::ptr::write_volatile;
use avr_delay::{delay, delay_ms, delay_us};

#[no_mangle]
pub extern fn main() {
    let mut out: u8 = 0x00;
    unsafe { write_volatile(DDRB, 0xff) }
    loop {
        out = out ^ 0xff;
        unsafe { write_volatile(PORTB, out) }
        delay_ms(1000000);
    }
}

// These do not need to be in a module, but we group them here for clarity.
pub mod std {
    #[lang = "eh_personality"]
    #[no_mangle]
    pub unsafe extern "C" fn rust_eh_personality(_state: (), _exception_object: *mut (), _context: *mut ()) -> () {
    }
    
    #[lang = "panic_fmt"]
    #[unwind]
    pub extern fn rust_begin_panic(_msg: (), _file: &'static str, _line: u32) -> ! {
        loop { }
    }
}

No attempt is made to handle arithmetic overruns.

Dependencies

~0.7–1MB
~26K SLoC