Skip to content

Commit

Permalink
Fixed atmega doctests
Browse files Browse the repository at this point in the history
  • Loading branch information
innermatrix committed Dec 30, 2024
1 parent 7935db6 commit 2812217
Show file tree
Hide file tree
Showing 11 changed files with 276 additions and 146 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ jobs:
run: >-
cd "mcu/${{ matrix.m.crate }}" &&
cargo test --doc --features "${{ matrix.m.name }}-no-deprecated-globals" -Z build-std=core --target "../../avr-specs/avr-${{ matrix.m.spec }}.json"
- name: Compile doctests for an MCU (no deprecated globals)
if: "${{ matrix.m.crate == 'atmega-hal' }}"
run: >-
cd "mcu/${{ matrix.m.crate }}" &&
cargo test --doc --features "${{ matrix.m.name }}-no-deprecated-globals,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 @@ -144,6 +144,11 @@ _peripheral-simple-pwm = []
_peripheral-spi = []
_peripheral-usart = []

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

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

Expand Down
62 changes: 41 additions & 21 deletions mcu/atmega-hal/src/atmega32u4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,19 @@ impl_mod_simple_pwm! {
/// Use `TC0` for PWM (pins `PB7`, `PD0`)
///
/// # Example
/// ```
/// ```no_run
/// use atmega_hal::atmega32u4 as hal;
/// use hal::simple_pwm::{IntoPwmPin,Timer0Pwm,Prescaler};
///
/// let dp = hal::Peripherals::take().unwrap();
/// let pins = hal::pins!(dp);
/// let mut timer0 = Timer0Pwm::new(dp.TC0, Prescaler::Prescale64);
///
/// let mut d11 = pins.d11.into_output().into_pwm(&mut timer0);
/// let mut d3 = pins.d3.into_output().into_pwm(&mut timer0);
/// let mut pb7 = pins.pb7.into_output().into_pwm(&mut timer0);
/// let mut pd0 = pins.pd0.into_output().into_pwm(&mut timer0);
///
/// d11.set_duty(128);
/// d11.enable();
/// pb7.set_duty(128);
/// pb7.enable();
/// ```
pub struct Timer0Pwm {
timer: hal::pac::TC0,
Expand Down Expand Up @@ -124,15 +129,20 @@ impl_mod_simple_pwm! {
/// Use `TC1` for PWM (pins `PB5`, `PB6`, `PB7`)
///
/// # Example
/// ```
/// ```no_run
/// use atmega_hal::atmega32u4 as hal;
/// use hal::simple_pwm::{IntoPwmPin,Timer1Pwm,Prescaler};
///
/// let dp = hal::Peripherals::take().unwrap();
/// let pins = hal::pins!(dp);
/// let mut timer1 = Timer1Pwm::new(dp.TC1, Prescaler::Prescale64);
///
/// let mut d9 = pins.d9.into_output().into_pwm(&mut timer1);
/// let mut d10 = pins.d10.into_output().into_pwm(&mut timer1);
/// let mut d11 = pins.d11.into_output().into_pwm(&mut timer1);
/// let mut pb5 = pins.pb5.into_output().into_pwm(&mut timer1);
/// let mut pb6 = pins.pb6.into_output().into_pwm(&mut timer1);
/// let mut pb7 = pins.pb7.into_output().into_pwm(&mut timer1);
///
/// d9.set_duty(128);
/// d9.enable();
/// pb5.set_duty(128);
/// pb5.enable();
/// ```
pub struct Timer1Pwm {
timer: hal::pac::TC1,
Expand Down Expand Up @@ -183,13 +193,18 @@ impl_mod_simple_pwm! {
/// Use `TC3` for PWM (pins `PC6`)
///
/// # Example
/// ```
/// ```no_run
/// use atmega_hal::atmega32u4 as hal;
/// use hal::simple_pwm::{IntoPwmPin,Timer3Pwm,Prescaler};
///
/// let dp = hal::Peripherals::take().unwrap();
/// let pins = hal::pins!(dp);
/// let mut timer3 = Timer3Pwm::new(dp.TC3, Prescaler::Prescale64);
///
/// let mut d5 = pins.d5.into_output().into_pwm(&mut timer3);
/// let mut pc6 = pins.pc6.into_output().into_pwm(&mut timer3);
///
/// d5.set_duty(128);
/// d5.enable();
/// pc6.set_duty(128);
/// pc6.enable();
/// ```
pub struct Timer3Pwm {
timer: hal::pac::TC3,
Expand Down Expand Up @@ -222,15 +237,20 @@ impl_mod_simple_pwm! {
/// Use `TC4` for PWM (pins `PB6`, `PC7`, `PD7`)
///
/// # Example
/// ```
/// ```no_run
/// use atmega_hal::atmega32u4 as hal;
/// use hal::simple_pwm::{IntoPwmPin,Timer4Pwm,Prescaler};
///
/// let dp = hal::Peripherals::take().unwrap();
/// let pins = hal::pins!(dp);
/// let mut timer4 = Timer4Pwm::new(dp.TC4, Prescaler::Prescale64);
///
/// let mut d6 = pins.d6.into_output().into_pwm(&mut timer4);
/// let mut d10 = pins.d10.into_output().into_pwm(&mut timer4);
/// let mut d13 = pins.d13.into_output().into_pwm(&mut timer4);
/// let mut pb6 = pins.pb6.into_output().into_pwm(&mut timer4);
/// let mut pc7 = pins.pc7.into_output().into_pwm(&mut timer4);
/// let mut pd7 = pins.pd7.into_output().into_pwm(&mut timer4);
///
/// d6.set_duty(128);
/// d6.enable();
/// pb6.set_duty(128);
/// pb6.enable();
/// ```
pub struct Timer4Pwm {
timer: hal::pac::TC4,
Expand Down
30 changes: 19 additions & 11 deletions mcu/atmega-hal/src/atmega8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,19 @@ impl_mod_simple_pwm! {
/// Use `TC1` for PWM (pins `PB1`, `PB2`)
///
/// # Example
/// ```
/// ```no_run
/// use atmega_hal::atmega8 as hal;
/// use hal::simple_pwm::{IntoPwmPin,Timer1Pwm,Prescaler};
///
/// let dp = hal::Peripherals::take().unwrap();
/// let pins = hal::pins!(dp);
/// let mut timer1 = Timer1Pwm::new(dp.TC1, Prescaler::Prescale64);
///
/// let mut b1 = pins.b1.into_output().into_pwm(&mut timer1);
/// let mut b2 = pins.b2.into_output().into_pwm(&mut timer1);
/// let mut pb1 = pins.pb1.into_output().into_pwm(&mut timer1);
/// let mut pb2 = pins.pb2.into_output().into_pwm(&mut timer1);
///
/// d9.set_duty(128);
/// d9.enable();
/// pb1.set_duty(128);
/// pb1.enable();
/// ```
pub struct Timer1Pwm {
timer: hal::pac::TC1,
Expand Down Expand Up @@ -118,14 +123,17 @@ impl_mod_simple_pwm! {
/// Use `TC2` for PWM (pin `PB3`)
///
/// # Example
/// ```
/// ```no_run
/// use atmega_hal::atmega8 as hal;
/// use hal::simple_pwm::{IntoPwmPin,Timer2Pwm,Prescaler};
///
/// let dp = hal::Peripherals::take().unwrap();
/// let pins = hal::pins!(dp);
/// let mut timer2 = Timer2Pwm::new(dp.TC2, Prescaler::Prescale64);
///
/// let mut d11 = pins.d11.into_output().into_pwm(&mut timer2);
/// let mut d3 = pins.d3.into_output().into_pwm(&mut timer2);
/// let mut pb3 = pins.pb3.into_output().into_pwm(&mut timer2);
///
/// d11.set_duty(128);
/// d11.enable();
/// pb3.set_duty(128);
/// ```
pub struct Timer2Pwm {
timer: hal::pac::TC2,
Expand Down Expand Up @@ -174,7 +182,7 @@ impl_mod_usart! {
peripheral: USART,
rx: PD0,
tx: PD1,
impl!: crate::r#impl::impl_usart_ubrrh_ucsrc,
impl!: impl_usart_ubrrh_ucsrc,
},
},
}
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 @@ -7,18 +7,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);
//! }
//! ```
pub use avr_hal_generic::port::{mode, PinMode, PinOps};
Expand Down
Loading

0 comments on commit 2812217

Please sign in to comment.