#delay #avr #routines #function #arduino #ms #cycle

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

#1537 in Embedded development

31 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

~1.5MB
~35K SLoC