Giter VIP home page Giter VIP logo

avr-rust-delay's Introduction

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]

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.

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.

avr-rust-delay's People

Contributors

dylanmckay avatar mbuesch avatar n8henrie avatar pusherofbrooms avatar rkarabut avatar stappersg avatar v1nsai avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.