#avr #arduino #uno

nightly ruduino

Reusable components for AVR microcontrollers

8 releases

0.2.5 Jul 31, 2020
0.2.4 Jul 25, 2020
0.2.0 Jun 19, 2020
0.1.2 Nov 5, 2018

#37 in Embedded development

Download history 1/week @ 2020-04-26 1/week @ 2020-05-03 8/week @ 2020-05-17 2/week @ 2020-05-24 5/week @ 2020-05-31 2/week @ 2020-06-07 18/week @ 2020-06-14 17/week @ 2020-06-21 10/week @ 2020-06-28 6/week @ 2020-07-05 11/week @ 2020-07-12 97/week @ 2020-07-19 135/week @ 2020-07-26 70/week @ 2020-08-02

95 downloads per month

MIT/Apache

56KB
1K SLoC

Ruduino

This library provides a set of reusable components for the Arduino Uno.

Overview

Register and bit definitions

use ruduino::PORTB; // Register
use ruduino::PORTB7; // Pin

Prelude

Disable interrupts.

without_interrupts(|| {
    unsafe { write_volatile(DDRB, 0xFF) }
})

Timers

Configure a timer.

const CPU_FREQUENCY_HZ: u64 = 16_000_000;
const DESIRED_HZ_TIM1: f64 = 2.0;
const TIM1_PRESCALER: u64 = 1024;
const INTERRUPT_EVERY_1_HZ_1024_PRESCALER: u16 =
    ((CPU_FREQUENCY_HZ as f64 / (DESIRED_HZ_TIM1 * TIM1_PRESCALER as f64)) as u64 - 1) as u16;

timer1::Timer::new()
    .waveform_generation_mode(timer1::WaveformGenerationMode::ClearOnTimerMatchOutputCompare)
    .clock_source(timer1::ClockSource::Prescale1024)
    .output_compare_1(Some(INTERRUPT_EVERY_1_HZ_1024_PRESCALER))
    .configure();

Set up an interrupt handler that will be called when the timer fires.

#[no_mangle]
pub unsafe extern "avr-interrupt" fn _ivr_timer1_compare_a() {
    let prev_value = read_volatile(PORTB);
    write_volatile(PORTB, prev_value ^ PINB5);
}

Hardware Serial Port

Configure the serial port.

const CPU_FREQUENCY_HZ: u64 = 16_000_000;
const BAUD: u64 = 9600;
const UBRR: u16 = (CPU_FREQUENCY_HZ / 16 / BAUD - 1) as u16;

serial::Serial::new(UBRR)
    .character_size(serial::CharacterSize::EightBits)
    .mode(serial::Mode::Asynchronous)
    .parity(serial::Parity::Disabled)
    .stop_bits(serial::StopBits::OneBit)
    .configure();

Transmit a sequence of bytes.

for &b in b"OK" {
    serial::transmit(b);
}

Read a byte if there's something available.

if let Some(b) = serial::try_receive() {
    serial::transmit(b);
    serial::transmit(b);
}

Dependencies

~150–520KB