From 2812217df39f818babc9a2ebd3e53b85c05d14f7 Mon Sep 17 00:00:00 2001 From: Iris Artin Date: Mon, 30 Dec 2024 07:30:47 -0500 Subject: [PATCH] Fixed atmega doctests --- .github/workflows/ci.yml | 5 + mcu/atmega-hal/Cargo.toml | 5 + mcu/atmega-hal/src/atmega32u4.rs | 62 +++++++---- mcu/atmega-hal/src/atmega8.rs | 30 ++++-- mcu/atmega-hal/src/impl/adc.rs | 61 ++++++----- mcu/atmega-hal/src/impl/eeprom.rs | 9 +- mcu/atmega-hal/src/impl/i2c.rs | 28 +++-- mcu/atmega-hal/src/impl/port.rs | 18 ++-- mcu/atmega-hal/src/impl/simple_pwm.rs | 143 +++++++++++++++++--------- mcu/atmega-hal/src/impl/spi.rs | 34 +++--- mcu/atmega-hal/src/impl/usart.rs | 27 +++-- 11 files changed, 276 insertions(+), 146 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e0b0da4aa4..4115c66d54 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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" diff --git a/mcu/atmega-hal/Cargo.toml b/mcu/atmega-hal/Cargo.toml index 5d19b35218..e7d24ad4ae 100644 --- a/mcu/atmega-hal/Cargo.toml +++ b/mcu/atmega-hal/Cargo.toml @@ -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/" } diff --git a/mcu/atmega-hal/src/atmega32u4.rs b/mcu/atmega-hal/src/atmega32u4.rs index 1b305f3a5b..74ec127feb 100644 --- a/mcu/atmega-hal/src/atmega32u4.rs +++ b/mcu/atmega-hal/src/atmega32u4.rs @@ -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, @@ -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, @@ -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, @@ -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, diff --git a/mcu/atmega-hal/src/atmega8.rs b/mcu/atmega-hal/src/atmega8.rs index c4ea253270..78ef83e33e 100644 --- a/mcu/atmega-hal/src/atmega8.rs +++ b/mcu/atmega-hal/src/atmega8.rs @@ -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, @@ -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, @@ -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, }, }, } diff --git a/mcu/atmega-hal/src/impl/adc.rs b/mcu/atmega-hal/src/impl/adc.rs index bb5982924f..7368f73a97 100644 --- a/mcu/atmega-hal/src/impl/adc.rs +++ b/mcu/atmega-hal/src/impl/adc.rs @@ -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::::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::::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; @@ -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 { $( diff --git a/mcu/atmega-hal/src/impl/eeprom.rs b/mcu/atmega-hal/src/impl/eeprom.rs index 234af3eadc..4f8327874a 100644 --- a/mcu/atmega-hal/src/impl/eeprom.rs +++ b/mcu/atmega-hal/src/impl/eeprom.rs @@ -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}; diff --git a/mcu/atmega-hal/src/impl/i2c.rs b/mcu/atmega-hal/src/impl/i2c.rs index 258e3d238f..e02146a031 100644 --- a/mcu/atmega-hal/src/impl/i2c.rs +++ b/mcu/atmega-hal/src/impl/i2c.rs @@ -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), "::::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; $( diff --git a/mcu/atmega-hal/src/impl/port.rs b/mcu/atmega-hal/src/impl/port.rs index 2b3f0a105a..70634716f5 100644 --- a/mcu/atmega-hal/src/impl/port.rs +++ b/mcu/atmega-hal/src/impl/port.rs @@ -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::::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}; diff --git a/mcu/atmega-hal/src/impl/simple_pwm.rs b/mcu/atmega-hal/src/impl/simple_pwm.rs index cf2caf9c2d..28ee56819f 100644 --- a/mcu/atmega-hal/src/impl/simple_pwm.rs +++ b/mcu/atmega-hal/src/impl/simple_pwm.rs @@ -40,14 +40,19 @@ macro_rules! impl_simple_pwm_48p_168_328p_328pb { /// Use `TC0` for PWM (pins `PD5`, `PD6`) /// /// # Example - /// ``` + /// ```no_run + #[doc = concat!("use atmega_hal::", stringify!($hal), " 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 d5 = pins.d5.into_output().into_pwm(&mut timer0); - /// let mut d6 = pins.d6.into_output().into_pwm(&mut timer0); + /// let mut pd5 = pins.pd5.into_output().into_pwm(&mut timer0); + /// let mut pd6 = pins.pd6.into_output().into_pwm(&mut timer0); /// - /// d5.set_duty(128); - /// d5.enable(); + /// pd5.set_duty(128); + /// pd5.enable(); /// ``` pub struct Timer0Pwm { timer: hal::pac::TC0, @@ -87,14 +92,19 @@ macro_rules! impl_simple_pwm_48p_168_328p_328pb { /// Use `TC1` for PWM (pins `PB1`, `PB2`) /// /// # Example - /// ``` + /// ```no_run + #[doc = concat!("use atmega_hal::", stringify!($hal), " 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 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, @@ -138,14 +148,19 @@ macro_rules! impl_simple_pwm_48p_168_328p_328pb { /// Use `TC2` for PWM (pins `PB3`, `PD3`) /// /// # Example - /// ``` + /// ```no_run + #[doc = concat!("use atmega_hal::", stringify!($hal), " 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); + /// let mut pd3 = pins.pd3.into_output().into_pwm(&mut timer2); /// - /// d11.set_duty(128); - /// d11.enable(); + /// pb3.set_duty(128); + /// pb3.enable(); /// ``` pub struct Timer2Pwm { timer: hal::pac::TC2, @@ -192,14 +207,19 @@ macro_rules! impl_simple_pwm_1280_2560 { /// Use `TC0` for PWM (pins `PB7`, `PG5`) /// /// # Example - /// ``` + /// ```no_run + #[doc = concat!("use atmega_hal::", stringify!($hal), " 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 d13 = pins.d13.into_output().into_pwm(&mut timer0); - /// let mut d4 = pins.d4.into_output().into_pwm(&mut timer0); + /// let mut pb7 = pins.pb7.into_output().into_pwm(&mut timer0); + /// let mut pg5 = pins.pg5.into_output().into_pwm(&mut timer0); /// - /// d13.set_duty(128); - /// d13.enable(); + /// pb7.set_duty(128); + /// pb7.enable(); /// ``` pub struct Timer0Pwm { timer: hal::pac::TC0, @@ -239,15 +259,20 @@ macro_rules! impl_simple_pwm_1280_2560 { /// Use `TC1` for PWM (pins `PB5`, `PB6`, `PB7`) /// /// # Example - /// ``` + /// ```no_run + #[doc = concat!("use atmega_hal::", stringify!($hal), " 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 d11 = pins.d11.into_output().into_pwm(&mut timer1); - /// let mut d12 = pins.d12.into_output().into_pwm(&mut timer1); - /// let mut d13 = pins.d13.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); /// - /// d11.set_duty(128); - /// d11.enable(); + /// pb5.set_duty(128); + /// pb5.enable(); /// ``` pub struct Timer1Pwm { timer: hal::pac::TC1, @@ -296,14 +321,19 @@ macro_rules! impl_simple_pwm_1280_2560 { /// Use `TC2` for PWM (pins `PB4`, `PH6`) /// /// # Example - /// ``` + /// ```no_run + #[doc = concat!("use atmega_hal::", stringify!($hal), " 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 d10 = pins.d10.into_output().into_pwm(&mut timer2); - /// let mut d9 = pins.d9.into_output().into_pwm(&mut timer2); + /// let mut pb4 = pins.pb4.into_output().into_pwm(&mut timer2); + /// let mut ph6 = pins.ph6.into_output().into_pwm(&mut timer2); /// - /// d10.set_duty(128); - /// d10.enable(); + /// pb4.set_duty(128); + /// pb4.enable(); /// ``` pub struct Timer2Pwm { @@ -348,15 +378,20 @@ macro_rules! impl_simple_pwm_1280_2560 { /// Use `TC3` for PWM (pins `PE3`, `PE4`, `PE5`) /// /// # Example - /// ``` + /// ```no_run + #[doc = concat!("use atmega_hal::", stringify!($hal), " 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 d2 = pins.d2.into_output().into_pwm(&mut timer3); - /// let mut d3 = pins.d3.into_output().into_pwm(&mut timer3); + /// let mut pe3 = pins.pe3.into_output().into_pwm(&mut timer3); + /// let mut pe4 = pins.pe4.into_output().into_pwm(&mut timer3); + /// let mut pe5 = pins.pe5.into_output().into_pwm(&mut timer3); /// - /// d5.set_duty(128); - /// d5.enable(); + /// pe3.set_duty(128); + /// pe3.enable(); /// ``` pub struct Timer3Pwm { timer: hal::pac::TC3, @@ -410,15 +445,20 @@ macro_rules! impl_simple_pwm_1280_2560 { /// Use `TC4` for PWM (pins `PH3`, `PH4`, `PH5`) /// /// # Example - /// ``` + /// ```no_run + #[doc = concat!("use atmega_hal::", stringify!($hal), " 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 d7 = pins.d7.into_output().into_pwm(&mut timer4); - /// let mut d8 = pins.d8.into_output().into_pwm(&mut timer4); + /// let mut ph3 = pins.ph3.into_output().into_pwm(&mut timer4); + /// let mut ph4 = pins.ph4.into_output().into_pwm(&mut timer4); + /// let mut ph5 = pins.ph5.into_output().into_pwm(&mut timer4); /// - /// d6.set_duty(128); - /// d6.enable(); + /// ph3.set_duty(128); + /// ph3.enable(); /// ``` pub struct Timer4Pwm { timer: hal::pac::TC4, @@ -472,15 +512,20 @@ macro_rules! impl_simple_pwm_1280_2560 { /// Use `TC5` for PWM (pins `PL3`, `PL4`, `PL5`) /// /// # Example - /// ``` + /// ```no_run + #[doc = concat!("use atmega_hal::", stringify!($hal), " as hal;")] + /// use hal::simple_pwm::{IntoPwmPin,Timer5Pwm,Prescaler}; + /// + /// let dp = hal::Peripherals::take().unwrap(); + /// let pins = hal::pins!(dp); /// let mut timer5 = Timer5Pwm::new(dp.TC5, Prescaler::Prescale64); /// - /// let mut d46 = pins.d46.into_output().into_pwm(&mut timer5); - /// let mut d45 = pins.d45.into_output().into_pwm(&mut timer5); - /// let mut d44 = pins.d44.into_output().into_pwm(&mut timer5); + /// let mut pl3 = pins.pl3.into_output().into_pwm(&mut timer5); + /// let mut pl4 = pins.pl4.into_output().into_pwm(&mut timer5); + /// let mut pl5 = pins.pl5.into_output().into_pwm(&mut timer5); /// - /// d46.set_duty(128); - /// d46.enable(); + /// pl3.set_duty(128); + /// pl3.enable(); /// ``` pub struct Timer5Pwm { timer: hal::pac::TC5, diff --git a/mcu/atmega-hal/src/impl/spi.rs b/mcu/atmega-hal/src/impl/spi.rs index 42e3d4718d..b3ddc8712e 100644 --- a/mcu/atmega-hal/src/impl/spi.rs +++ b/mcu/atmega-hal/src/impl/spi.rs @@ -21,18 +21,27 @@ macro_rules! impl_mod_spi { //! Complete example source code can be found in the repository //! [`atmega2560-spi-feedback.rs`](https://github.com/Rahix/avr-hal/blob/main/examples/atmega2560/src/bin/atmega2560-spi-feedback.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;")] + //! + //! use embedded_hal::digital::OutputPin; + //! use embedded_hal::spi::SpiBus; + //! + //! let dp = hal::Peripherals::take().unwrap(); + //! let pins = hal::pins!(dp); //! - //! let (mut spi, mut cs) = spi::Spi::new( - //! dp.SPI, - //! pins.pb1.into_output(), - //! pins.pb2.into_output(), - //! pins.pb3.into_pull_up_input(), - //! pins.pb0.into_output(), - //! spi::Settings::default(), - //! ); + $( + #![doc = paste!{ concat!( + "let (mut spi, mut cs) = hal::spi::", stringify!($interface), "::new(\n", + " dp.", stringify!($peripheral), ",\n", + " pins.", stringify!([< $sclk:lower >]), ".into_output(),\n", + " pins.", stringify!([< $mosi:lower >]), ".into_output(),\n", + " pins.", stringify!([< $miso:lower >]), ".into_pull_up_input(),\n", + " pins.", stringify!([< $cs:lower >]), ".into_output(),\n", + " hal::spi::Settings::default(),\n", + ");\n", + ) }] + )+ //! //! let data_out = b"Hello World!"; //! let mut data_in = [0u8; 12]; @@ -41,10 +50,11 @@ macro_rules! impl_mod_spi { //! spi.transfer(&mut data_in, data_out).unwrap(); //! cs.set_high().unwrap(); //! - //! ufmt::uwriteln!(&mut serial, "data: {:?}", data_in).unwrap(); + //! // ufmt::uwriteln!(&mut serial, "data: {:?}", data_in).unwrap(); //! ``` pub use avr_hal_generic::spi::*; + use avr_hal_generic::paste::paste; use crate::$hal as hal; $( diff --git a/mcu/atmega-hal/src/impl/usart.rs b/mcu/atmega-hal/src/impl/usart.rs index 03adf7be51..c18801c394 100644 --- a/mcu/atmega-hal/src/impl/usart.rs +++ b/mcu/atmega-hal/src/impl/usart.rs @@ -25,16 +25,25 @@ macro_rules! impl_mod_usart { //! *Note: [ufmt](https://crates.io/crates/ufmt/) is used instead of `core::fmt` because //! `core::fmt` code quickly grows too large for AVR platforms.* //! - //! ``` - //! 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;")] + //! + //! let dp = hal::Peripherals::take().unwrap(); + //! let pins = hal::pins!(dp); + //! //! - //! let mut serial = Usart::new( - //! dp.USART0, - //! pins.pe0, - //! pins.pe1.into_output(), - //! Baudrate::::new(57600), - //! ); + //! type Clock = avr_hal_generic::clock::MHz16; + $( + #![doc = paste!{ concat!( + "let mut serial = hal::usart::", stringify!($interface), "::new(\n", + " dp.", stringify!($peripheral), ",\n", + " pins.", stringify!([< $rx:lower >]), ",\n", + " pins.", stringify!([< $tx:lower >]), ".into_output(),\n", + " hal::usart::Baudrate::::new(57600),\n", + ");\n", + ) }] + )+ //! //! ufmt::uwriteln!(&mut serial, "Hello from ATmega!").unwrap(); //!