Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add refactored support for Arduino Diecimila #177

Merged
merged 9 commits into from
Apr 24, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ jobs:
m:
- type: board
name: arduino-uno
- type: board
name: arduino-diecimila
- type: board
name: arduino-leonardo
- type: board
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ members = [
"arduino-hal",

# Examples
"examples/arduino-diecimila",
"examples/arduino-leonardo",
"examples/arduino-mega2560",
"examples/arduino-nano",
Expand Down
1 change: 1 addition & 0 deletions arduino-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ rt = ["atmega-hal/rt"]

board-selected = []
mcu-atmega=[]
arduino-diecimila = ["mcu-atmega", "atmega-hal/atmega168", "board-selected"]
arduino-leonardo = ["mcu-atmega", "atmega-hal/atmega32u4", "board-selected"]
arduino-mega2560 = ["mcu-atmega", "atmega-hal/atmega2560", "board-selected"]
arduino-nano = ["mcu-atmega", "atmega-hal/atmega328p", "atmega-hal/enable-extra-adc", "board-selected"]
Expand Down
1 change: 1 addition & 0 deletions arduino-hal/src/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub use avr_hal_generic::clock::*;

pub(crate) mod default {
#[cfg(any(
feature = "arduino-diecimila",
feature = "arduino-leonardo",
feature = "arduino-mega2560",
feature = "arduino-nano",
Expand Down
13 changes: 11 additions & 2 deletions arduino-hal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ compile_error!(

Please select one of the following

* arduino-diecimila
* arduino-leonardo
* arduino-mega2560
* arduino-nano
Expand Down Expand Up @@ -84,7 +85,11 @@ pub use usart::Usart;
#[cfg(feature = "board-selected")]
pub mod prelude {
cfg_if::cfg_if! {
if #[cfg(any(feature = "arduino-mega2560", feature = "arduino-uno"))] {
if #[cfg(any(
feature = "arduino-diecimila",
feature = "arduino-mega2560",
feature = "arduino-uno"
))] {
pub use crate::hal::usart::BaudrateArduinoExt as _;
} else {
pub use crate::hal::usart::BaudrateExt as _;
Expand Down Expand Up @@ -118,7 +123,11 @@ macro_rules! default_serial {
}
// See comment in avr-hal-generic/src/usart.rs for why these boards use
// the BaudrateArduinoExt trait instead of BaudrateExt
#[cfg(any(feature = "arduino-mega2560", feature = "arduino-uno"))]
#[cfg(any(
feature = "arduino-diecimila",
feature = "arduino-mega2560",
feature = "arduino-uno"
))]
#[macro_export]
macro_rules! default_serial {
($p:expr, $pins:expr, $baud:expr) => {
Expand Down
126 changes: 126 additions & 0 deletions arduino-hal/src/port/diecimila.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
pub use atmega_hal::port::mode;
pub use atmega_hal::port::Pin;

avr_hal_generic::renamed_pins! {
type Pin = Pin;

pub struct Pins from atmega_hal::Pins {
/// `A0`
///
/// * ADC0 (ADC input channel 0)
/// * PCINT8 (pin change interrupt 8)
pub a0: atmega_hal::port::PC0 = pc0,
/// `A1`
///
/// * ADC1 (ADC input channel 1)
/// * PCINT9 (pin change interrupt 9)
pub a1: atmega_hal::port::PC1 = pc1,
/// `A2`
///
/// * ADC2 (ADC input channel 2)
/// * PCINT10 (pin change interrupt 10)
pub a2: atmega_hal::port::PC2 = pc2,
/// `A3`
///
/// * ADC3 (ADC input channel 3)
/// * PCINT11 (pin change interrupt 11)
pub a3: atmega_hal::port::PC3 = pc3,
/// `A4`
///
/// * ADC4 (ADC input channel 4)
/// * SDA (2-wire serial bus data input/output line)
/// * PCINT12 (pin change interrupt 12)
pub a4: atmega_hal::port::PC4 = pc4,
/// `A5`
///
/// ADC5 (ADC input channel 5)
/// SCL (2-wire serial bus clock line)
/// PCINT13 (pin change interrupt 13)
pub a5: atmega_hal::port::PC5 = pc5,

/// `D0` / `RX`
///
/// * RXD (USART input pin)
/// * PCINT16 (pin change interrupt 16)
pub d0: atmega_hal::port::PD0 = pd0,
/// `D1` / `TX`
///
/// * TXD (USART output pin)
/// * PCINT17 (pin change interrupt 17)
pub d1: atmega_hal::port::PD1 = pd1,
/// `D2`
///
/// * INT0 (external interrupt 0 input)
/// * PCINT18 (pin change interrupt 18)
pub d2: atmega_hal::port::PD2 = pd2,
/// `D3`
///
/// * **PWM**: [atmega168_hal::timer::Timer3Pwm]
/// * INT1 (external interrupt 1 input)
/// * OC2B (Timer/Counter2 output compare match B output)
/// * PCINT19 (pin change interrupt 19)
pub d3: atmega_hal::port::PD3 = pd3,
/// `D4`
///
/// * XCK (USART external clock input/output)
/// * T0 (Timer/Counter 0 external counter input)
/// * PCINT20 (pin change interrupt 20)
pub d4: atmega_hal::port::PD4 = pd4,
/// `D5`
///
/// * **PWM**: [atmega168_hal::timer::Timer3Pwm]
/// * T1 (Timer/Counter 1 external counter input)
/// * OC0B (Timer/Counter0 output compare match B output)
/// * PCINT21 (pin change interrupt 21)
pub d5: atmega_hal::port::PD5 = pd5,
/// `D6`
///
/// * **PWM**: [atmega168_hal::timer::Timer3Pwm]
/// * AIN0 (analog comparator positive input)
/// * OC0A (Timer/Counter0 output compare match A output)
/// * PCINT22 (pin change interrupt 22)
pub d6: atmega_hal::port::PD6 = pd6,
/// `D7`
///
/// * AIN1 (analog comparator negative input)
/// * PCINT23 (pin change interrupt 23)
pub d7: atmega_hal::port::PD7 = pd7,
/// `D8`
///
/// * ICP1 (Timer/Counter1 input capture input)
/// * CLKO (divided system clock output)
/// * PCINT0 (pin change interrupt 0)
pub d8: atmega_hal::port::PB0 = pb0,
/// `D9`
///
/// * **PWM**: [atmega168_hal::timer::Timer3Pwm]
/// * OC1A (Timer/Counter1 output compare match A output)
/// * PCINT1 (pin change interrupt 1)
pub d9: atmega_hal::port::PB1 = pb1,
/// `D10`
///
/// * **PWM**: [atmega168_hal::timer::Timer3Pwm]
/// * SS (SPI bus master slave select)
/// * OC1B (Timer/Counter1 output compare match B output)
/// * PCINT2 (pin change interrupt 2)
pub d10: atmega_hal::port::PB2 = pb2,
/// `D11`
///
/// * **PWM**: [atmega168_hal::timer::Timer3Pwm]
/// * MOSI (SPI bus master/slave input)
/// * OC2A (Timer/Counter2 output compare match A output)
/// * PCINT3 (pin change interrupt 3)
pub d11: atmega_hal::port::PB3 = pb3,
/// `D12`
///
/// * MISO (SPI bus master input/slave output)
/// * PCINT4 (pin change interrupt 4)
pub d12: atmega_hal::port::PB4 = pb4,
/// `D13`
///
/// * SCK (SPI bus master clock input)
/// * PCINT5 (pin change interrupt 5)
/// * L LED on Arduino Uno
pub d13: atmega_hal::port::PB5 = pb5,
}
}
4 changes: 4 additions & 0 deletions arduino-hal/src/port/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#[cfg(feature = "arduino-diecimila")]
mod diecimila;
#[cfg(feature = "arduino-diecimila")]
pub use diecimila::*;
#[cfg(feature = "arduino-leonardo")]
mod leonardo;
#[cfg(feature = "arduino-leonardo")]
Expand Down
8 changes: 8 additions & 0 deletions examples/arduino-diecimila/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[build]
target = "../../avr-specs/avr-atmega168.json"

[target.'cfg(target_arch = "avr")']
runner = "ravedude decimila"
explicite marked this conversation as resolved.
Show resolved Hide resolved

[unstable]
build-std = ["core"]
16 changes: 16 additions & 0 deletions examples/arduino-diecimila/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "arduino-diecimila-examples"
version = "0.0.0"
authors = ["Rahix <[email protected]>"]
edition = "2018"
publish = false

[dependencies]
panic-halt = "0.2.0"
ufmt = "0.1.0"
nb = "0.1.2"
embedded-hal = "0.2.3"

[dependencies.arduino-hal]
path = "../../arduino-hal/"
features = ["arduino-diecimila"]
17 changes: 17 additions & 0 deletions examples/arduino-diecimila/src/bin/diecimila-blink.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#![no_std]
#![no_main]

use panic_halt as _;

#[arduino_hal::entry]
fn main() -> ! {
let dp = arduino_hal::Peripherals::take().unwrap();
let pins = arduino_hal::pins!(dp);

let mut led = pins.d13.into_output().downgrade();

loop {
led.toggle();
arduino_hal::delay_ms(1000);
}
}
24 changes: 24 additions & 0 deletions examples/arduino-diecimila/src/bin/diecimila-usart.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#![no_std]
#![no_main]

use arduino_hal::prelude::*;
use panic_halt as _;

use embedded_hal::serial::Read;

#[arduino_hal::entry]
fn main() -> ! {
let dp = arduino_hal::Peripherals::take().unwrap();
let pins = arduino_hal::pins!(dp);
let mut serial = arduino_hal::default_serial!(dp, pins, 57600);

ufmt::uwriteln!(&mut serial, "Hello from Arduino!\r").void_unwrap();

loop {
// Read a byte from the serial connectiondefault_se
let b = nb::block!(serial.read()).void_unwrap();

// Answer
ufmt::uwriteln!(&mut serial, "Got {}!\r", b).void_unwrap();
}
}