Fork of the ruduino library from avr-rust: https://github.com/avr-rust/ruduino
Go to file
Savanni D'Gerinel f516e1216b Rename MISO/MOSI to SerialDataIn and SerialDataOut 2023-04-02 11:35:35 -04:00
.github/workflows Add Docker based CI pipeline 2020-07-24 20:13:34 +12:00
core_generator Rename MISO/MOSI to SerialDataIn and SerialDataOut 2023-04-02 11:35:35 -04:00
examples Add a UART example 2020-07-25 02:45:22 +12:00
src Rename MISO/MOSI to SerialDataIn and SerialDataOut 2023-04-02 11:35:35 -04:00
.dockerignore Add an empty whitespace line to test CI 2020-07-24 20:17:22 +12:00
.gitignore Add a flake for rust-avr 2022-10-30 13:57:46 -04:00
Cargo.toml Fix delay dependency to git revision 849918a8dfb2 2022-06-16 21:44:03 +02:00
Dockerfile.ci [CI] git revert e6167ec6 2022-06-29 15:35:04 +02:00
LICENSE-APACHE Initial commit 2016-07-06 19:59:51 -04:00
LICENSE-MIT Initial commit 2016-07-06 19:59:51 -04:00
README.md Update the examples in the README to newer APIs 2021-02-02 02:25:32 +13:00
avr-atmega328p.json Fixed warning unused no-compiler-rt 2022-06-08 14:57:40 +02:00
build.rs Move the target-cpu feature enable logic back into a build script 2021-01-29 21:46:01 +13:00
flake.lock Add a flake for rust-avr 2022-10-30 13:57:46 -04:00
flake.nix Add a flake for rust-avr 2022-10-30 13:57:46 -04:00
regenerate-cores.sh regenerate-cores.sh: Enforce bash 2022-05-21 22:53:37 +02:00

README.md

Ruduino

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

Overview

Register and bit definitions

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

Prelude

Disable interrupts.

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

Timers

Configure a timer.

const DESIRED_HZ_TIM1: f64 = 2.0;
const TIM1_PRESCALER: u64 = 1024;
const INTERRUPT_EVERY_1_HZ_1024_PRESCALER: u16 =
    ((ruduino::config::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 BAUD: u64 = 9600;
const UBRR: u16 = (ruduino::config::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);
}