Skip to content

Repository files navigation

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-unknown-gnu-atmega328 --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]externcrate arduino;externcrate avr_delay;use arduino::{DDRB,PORTB};use core::ptr::write_volatile;use avr_delay::{delay, delay_ms, delay_us};#[no_mangle]pubexternfnmain(){letmut 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.pubmod std {#[lang = "eh_personality"]#[no_mangle]pubunsafeextern"C"fnrust_eh_personality(_state:(),_exception_object:*mut(),_context:*mut()) -> (){}#[lang = "panic_fmt"]#[unwind]pubexternfnrust_begin_panic(_msg:(),_file:&'staticstr,_line:u32) -> ! {loop{}}}

No attempt is made to handle arithmetic overruns.

Internal notes

Cargo publish

During cargo publish there could be

error: ran out of registers during register allocation

With that blocking issue is adding --no-verify considered the lesser evil as not releasing.

About

arduino-like delay routines based on busy-wait loops

Resources

Stars

16 stars

Watchers

4 watching

Forks

Releases

Packages

Used by

Contributors

Languages