Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ jobs:
- name: Test-compile HAL crate for an MCU
if: "${{ matrix.m.type == 'mcu' }}"
run: cd "mcu/${{ matrix.m.crate }}" && cargo build --features "${{ matrix.m.name }}" -Z build-std=core --target "../../avr-specs/avr-${{ matrix.m.spec }}.json"
- name: Compile doctests for an ATmega MCU
if: "${{ matrix.m.crate == 'atmega-hal' }}"
run: >-
cd "mcu/${{ matrix.m.crate }}" &&
cargo test --doc --features "${{ matrix.m.name }},enable-extra-adc" -Z build-std=core --target "../../avr-specs/avr-${{ matrix.m.spec }}.json"

ravedude:
name: "ravedude"
Expand Down
5 changes: 5 additions & 0 deletions mcu/atmega-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ _peripheral-spi = []
_peripheral-usart = []
no-globals = []

[dev-dependencies]
embedded-hal = "1.0"
ufmt = "0.2.0"
nb = "1.1.0"

[dependencies]
avr-hal-generic = { path = "../../avr-hal-generic/" }

Expand Down
44 changes: 30 additions & 14 deletions mcu/atmega-hal/src/atmega164pa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,25 @@ macro_rules! atmega164pa_timer_8bit_impl {
) => {
paste! {
avr_hal_generic::impl_simple_pwm! {
/// Use `$peripheral` for PWM (pins `$pin`,)
#[doc = concat!("Use `", stringify!($peripheral), "` for PWM.")]
///
/// # Example
/// ```
/// let mut timer0 = Timer0Pwm::new(dp.$peripheral, Prescaler::Prescale64);
/// ```no_run
#[doc = concat!("use atmega_hal::", stringify!($hal), " as hal;")]
#[doc = concat!("use hal::simple_pwm::{IntoPwmPin,", stringify!($timer), ",Prescaler};")]
///
/// let mut d0 = pins.d0.into_output().into_pwm(&mut timer0);
/// let mut d1 = pins.d1.into_output().into_pwm(&mut timer0);
/// let dp = hal::Peripherals::take().unwrap();
/// let pins = hal::pins!(dp);
#[doc = concat!("let mut timer = ", stringify!($timer), "::new(dp.", stringify!($peripheral), ", Prescaler::Prescale64);")]
///
/// d0.set_duty(128);
/// d0.enable();
$(
#[doc = paste!{ concat!(
"let mut ", stringify!([< $pin:lower >]), " = pins.", stringify!([< $pin:lower >]), ".into_output().into_pwm(&mut timer);\n",
stringify!([< $pin:lower >]), ".set_duty(128);\n",
stringify!([< $pin:lower >]), ".enable();\n",
"\n",
) }]
)+
/// ```
pub struct $timer {
timer: crate::$hal::pac::$peripheral,
Expand Down Expand Up @@ -189,17 +197,25 @@ macro_rules! atmega164pa_timer_16bit_impl {
) => {
paste! {
avr_hal_generic::impl_simple_pwm! {
/// Use `$peripheral` for PWM (pins `$pin`,)
#[doc = concat!("Use `", stringify!($peripheral), "` for PWM.")]
///
/// # Example
/// ```
/// let mut timer0 = Timer0Pwm::new(dp.$peripheral, Prescaler::Prescale64);
/// ```no_run
#[doc = concat!("use atmega_hal::", stringify!($hal), " as hal;")]
#[doc = concat!("use hal::simple_pwm::{IntoPwmPin,", stringify!($timer), ",Prescaler};")]
///
/// let mut d0 = pins.d0.into_output().into_pwm(&mut timer0);
/// let mut d1 = pins.d1.into_output().into_pwm(&mut timer0);
/// let dp = hal::Peripherals::take().unwrap();
/// let pins = hal::pins!(dp);
#[doc = concat!("let mut timer = ", stringify!($timer), "::new(dp.", stringify!($peripheral), ", Prescaler::Prescale64);")]
///
/// d0.set_duty(128);
/// d0.enable();
$(
#[doc = paste!{ concat!(
"let mut ", stringify!([< $pin:lower >]), " = pins.", stringify!([< $pin:lower >]), ".into_output().into_pwm(&mut timer);\n",
stringify!([< $pin:lower >]), ".set_duty(128);\n",
stringify!([< $pin:lower >]), ".enable();\n",
"\n",
) }]
)+
/// ```
pub struct $timer {
timer: crate::$hal::pac::$peripheral,
Expand Down
61 changes: 37 additions & 24 deletions mcu/atmega-hal/src/impl/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,49 @@ macro_rules! impl_mod_adc {
pub mod adc {
//! Analog-to-Digital Converter
//!
//! # Example
//!
//! Complete example source code can be found in the repository:
//! For full source code, please refer to the ATmega ADC example:
//! [`atmega2560-adc.rs`](https://github.com/Rahix/avr-hal/blob/main/examples/atmega2560/src/bin/atmega2560-adc.rs)
//!
//! # Example: Read pins using `analog_read()`
//!
//! ```no_run
#![doc = concat!("use atmega_hal::", stringify!($hal), " as hal;")]
//!
//! let dp = hal::Peripherals::take().unwrap();
//! let pins = hal::pins!(dp);
//!
//! let mut adc = hal::Adc::<avr_hal_generic::clock::MHz1>::new(dp.ADC, Default::default());
//!
$(
#![doc = paste!{ concat!(
"let ", stringify!([< input_ $pin_name:lower >]), " = pins.", stringify!([< $pin_name:lower >]), ".into_analog_input(&mut adc);\n",
"let ", stringify!([< value_ $pin_name:lower >]), " = ", stringify!([< input_ $pin_name:lower >]), ".analog_read(&mut adc);\n\n"
)}]
)*
//! ```
//! let dp = atmega_hal::Peripherals::take().unwrap();
//! let pins = atmega_hal::pins!(dp);
//!
//! let mut adc = Adc::new(dp.ADC, Default::default());
//! # Example: Read channels (including pins) using `read_blocking()`
//!
//! ```no_run
#![doc = concat!("use atmega_hal::", stringify!($hal), " as hal;")]
//!
//! let channels: [atmega_hal::adc::Channel; 4] = [
//! pins.pf0.into_analog_input(&mut adc).into_channel(),
//! pins.pf1.into_analog_input(&mut adc).into_channel(),
//! pins.pf2.into_analog_input(&mut adc).into_channel(),
//! pins.pf3.into_analog_input(&mut adc).into_channel(),
//! ];
//! let dp = hal::Peripherals::take().unwrap();
//! let pins = hal::pins!(dp);
//!
//! for (index, channel) in channels.iter().enumerate() {
//! let value = adc.read_blocking(channel);
//! ufmt::uwrite!(&mut serial, "CH{}: {} ", index, value).unwrap();
//! }
//! let mut adc = hal::Adc::<avr_hal_generic::clock::MHz1>::new(dp.ADC, Default::default());
//!
//! //
$(
#![doc = paste!{ concat!(
"let ", stringify!([< channel_ $pin_name:lower >]), " = pins.", stringify!([< $pin_name:lower >]), ".into_analog_input(&mut adc).into_channel();\n",
"let ", stringify!([< value_ $pin_name:lower >]), " = adc.read_blocking(&", stringify!([< channel_ $pin_name:lower >]), ");\n\n"
) }]
)*
$(
#![doc = paste!{ concat!(
"let ", stringify!([< value_ $channel_name:lower >]), " = adc.read_blocking(&hal::adc::channel::", stringify!([< $channel_name >]), ");\n\n"
) }]
)*
//! ```

use avr_hal_generic::paste::paste;
Expand All @@ -53,14 +74,6 @@ macro_rules! impl_mod_adc {
///
/// Some channels are not directly connected to pins. This module provides types which can be used
/// to access them.
///
/// # Example
/// ```
/// let dp = atmega_hal::Peripherals::take().unwrap();
/// let mut adc = atmega_hal::Adc::new(dp.ADC, Default::default());
///
/// let value = adc.read_blocking(&channel::Vbg);
/// ```
#[allow(non_camel_case_types)]
pub mod channel {
$(
Expand Down
9 changes: 5 additions & 4 deletions mcu/atmega-hal/src/impl/eeprom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@ macro_rules! impl_mod_eeprom {
//! Complete example source code can be found in the repository:
//! [`atmega2560-eeprom.rs`](https://github.com/Rahix/avr-hal/blob/main/examples/atmega2560/src/bin/atmega2560-eeprom.rs)
//!
//! ```
//! ```no_run
#![doc = concat!("use atmega_hal::", stringify!($hal), " as hal;")]
//! const BOOT_COUNT_OFFSET: u16 = 0;
//!
//! let dp = atmega_hal::Peripherals::take().unwrap();
//! let mut eeprom = Eeprom::new(dp.EEPROM);
//! let dp = hal::Peripherals::take().unwrap();
//! let mut eeprom = hal::Eeprom::new(dp.EEPROM);
//!
//! let mut boot_count = eeprom.read_byte(BOOT_COUNT_OFFSET);
//! boot_count = boot_count.wrapping_add(1);
//! eeprom.write_byte(BOOT_COUNT_OFFSET, boot_count);
//!
//! ufmt::uwriteln!(&mut serial, "Boot count: {}", boot_count).unwrap();
//! // ufmt::uwriteln!(&mut serial, "Boot count: {}", boot_count).unwrap();
//! ```
pub use avr_hal_generic::eeprom::{EepromOps, OutOfBoundsError};

Expand Down
28 changes: 18 additions & 10 deletions mcu/atmega-hal/src/impl/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,29 @@ macro_rules! impl_mod_i2c {
//! Complete example source code can be found in the repository:
//! [`atmega2560-i2cdetect.rs`](https://github.com/Rahix/avr-hal/blob/main/examples/atmega2560/src/bin/atmega2560-i2cdetect.rs)
//!
//! ```
//! let dp = atmega_hal::Peripherals::take().unwrap();
//! let pins = atmega_hal::pins!(dp);
//! ```no_run
#![doc = concat!("use atmega_hal::", stringify!($hal), " as hal;")]
//!
//! let dp = hal::Peripherals::take().unwrap();
//! let pins = hal::pins!(dp);
//!
//! let mut i2c = I2c::new(
//! dp.TWI,
//! pins.pd1.into_pull_up_input(),
//! pins.pd0.into_pull_up_input(),
//! 50_000,
//! );
//! type Clock = avr_hal_generic::clock::MHz16;
$(
#![doc = paste!{ concat!(
"let mut i2c = hal::i2c::", stringify!($interface), "::<Clock>::new(\n",
" dp.", stringify!($peripheral), ",\n",
" pins.", stringify!([< $sda:lower >]), ".into_pull_up_input(),\n",
" pins.", stringify!([< $scl:lower >]), ".into_pull_up_input(),\n",
" 50_000,\n",
");\n",
) }]
)+
//!
//! i2c.i2cdetect(&mut serial, atmega_hal::i2c::Direction::Read).unwrap();
//! // i2c.i2cdetect(&mut serial, hal::i2c::Direction::Read).unwrap();
//! ```

pub use avr_hal_generic::i2c::*;
use avr_hal_generic::paste::paste;
use crate::$hal as hal;

$(
Expand Down
18 changes: 12 additions & 6 deletions mcu/atmega-hal/src/impl/port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,24 @@ macro_rules! impl_mod_port {
//!
//! # Example
//!
//! Complete example source code can be found in the repository:
//! For full source code, please refer to the ATmega port example:
//! [`atmega2560-blink.rs`](https://github.com/Rahix/avr-hal/blob/main/examples/atmega2560/src/bin/atmega2560-blink.rs)
//!
//! ```
//! let dp = atmega_hal::Peripherals::take().unwrap();
//! let pins = atmega_hal::pins!(dp);
//! ```no_run
//! use atmega_hal::prelude::*;
#![doc = concat!("use atmega_hal::", stringify!($hal), " as hal;")]
//!
//! type Clock = atmega_hal::clock::MHz8;
//! let mut delay = atmega_hal::delay::Delay::<Clock>::new();
//!
//! let dp = hal::Peripherals::take().unwrap();
//! let pins = hal::pins!(dp);
//!
//! let mut led = pins.pb7.into_output();
//! let mut led = pins.pb2.into_output();
//!
//! loop {
//! led.toggle();
//! delay_ms(1000);
//! delay.delay_ms(1000u16);
//! }
//! ```
use avr_hal_generic::paste::paste;
Expand Down
Loading