Skip to content

Commit 7545605

Browse files
committed
Fixed atmega doctests
1 parent 7935db6 commit 7545605

11 files changed

Lines changed: 275 additions & 145 deletions

File tree

.github/workflows/ci.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ jobs:
129129
run: >-
130130
cd "mcu/${{ matrix.m.crate }}" &&
131131
cargo test --doc --features "${{ matrix.m.name }}-no-deprecated-globals" -Z build-std=core --target "../../avr-specs/avr-${{ matrix.m.spec }}.json"
132+
- name: Compile doctests for an MCU (no deprecated globals)
133+
if: "${{ matrix.m.crate == 'atmega-hal' }}"
134+
run: >-
135+
cd "mcu/${{ matrix.m.crate }}" &&
136+
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"
132137
133138
ravedude:
134139
name: "ravedude"

mcu/atmega-hal/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ _peripheral-simple-pwm = []
144144
_peripheral-spi = []
145145
_peripheral-usart = []
146146

147+
[dev-dependencies]
148+
embedded-hal = "1.0"
149+
ufmt = "0.2.0"
150+
nb = "1.1.0"
151+
147152
[dependencies]
148153
avr-hal-generic = { path = "../../avr-hal-generic/" }
149154

mcu/atmega-hal/src/atmega32u4.rs

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,19 @@ impl_mod_simple_pwm! {
7777
/// Use `TC0` for PWM (pins `PB7`, `PD0`)
7878
///
7979
/// # Example
80-
/// ```
80+
/// ```no_run
81+
/// use atmega_hal::atmega32u4 as hal;
82+
/// use hal::simple_pwm::{IntoPwmPin,Timer0Pwm,Prescaler};
83+
///
84+
/// let dp = hal::Peripherals::take().unwrap();
85+
/// let pins = hal::pins!(dp);
8186
/// let mut timer0 = Timer0Pwm::new(dp.TC0, Prescaler::Prescale64);
8287
///
83-
/// let mut d11 = pins.d11.into_output().into_pwm(&mut timer0);
84-
/// let mut d3 = pins.d3.into_output().into_pwm(&mut timer0);
88+
/// let mut pb7 = pins.pb7.into_output().into_pwm(&mut timer0);
89+
/// let mut pd0 = pins.pd0.into_output().into_pwm(&mut timer0);
8590
///
86-
/// d11.set_duty(128);
87-
/// d11.enable();
91+
/// pb7.set_duty(128);
92+
/// pb7.enable();
8893
/// ```
8994
pub struct Timer0Pwm {
9095
timer: hal::pac::TC0,
@@ -124,15 +129,20 @@ impl_mod_simple_pwm! {
124129
/// Use `TC1` for PWM (pins `PB5`, `PB6`, `PB7`)
125130
///
126131
/// # Example
127-
/// ```
132+
/// ```no_run
133+
/// use atmega_hal::atmega32u4 as hal;
134+
/// use hal::simple_pwm::{IntoPwmPin,Timer1Pwm,Prescaler};
135+
///
136+
/// let dp = hal::Peripherals::take().unwrap();
137+
/// let pins = hal::pins!(dp);
128138
/// let mut timer1 = Timer1Pwm::new(dp.TC1, Prescaler::Prescale64);
129139
///
130-
/// let mut d9 = pins.d9.into_output().into_pwm(&mut timer1);
131-
/// let mut d10 = pins.d10.into_output().into_pwm(&mut timer1);
132-
/// let mut d11 = pins.d11.into_output().into_pwm(&mut timer1);
140+
/// let mut pb5 = pins.pb5.into_output().into_pwm(&mut timer1);
141+
/// let mut pb6 = pins.pb6.into_output().into_pwm(&mut timer1);
142+
/// let mut pb7 = pins.pb7.into_output().into_pwm(&mut timer1);
133143
///
134-
/// d9.set_duty(128);
135-
/// d9.enable();
144+
/// pb5.set_duty(128);
145+
/// pb5.enable();
136146
/// ```
137147
pub struct Timer1Pwm {
138148
timer: hal::pac::TC1,
@@ -183,13 +193,18 @@ impl_mod_simple_pwm! {
183193
/// Use `TC3` for PWM (pins `PC6`)
184194
///
185195
/// # Example
186-
/// ```
196+
/// ```no_run
197+
/// use atmega_hal::atmega32u4 as hal;
198+
/// use hal::simple_pwm::{IntoPwmPin,Timer3Pwm,Prescaler};
199+
///
200+
/// let dp = hal::Peripherals::take().unwrap();
201+
/// let pins = hal::pins!(dp);
187202
/// let mut timer3 = Timer3Pwm::new(dp.TC3, Prescaler::Prescale64);
188203
///
189-
/// let mut d5 = pins.d5.into_output().into_pwm(&mut timer3);
204+
/// let mut pc6 = pins.pc6.into_output().into_pwm(&mut timer3);
190205
///
191-
/// d5.set_duty(128);
192-
/// d5.enable();
206+
/// pc6.set_duty(128);
207+
/// pc6.enable();
193208
/// ```
194209
pub struct Timer3Pwm {
195210
timer: hal::pac::TC3,
@@ -222,15 +237,20 @@ impl_mod_simple_pwm! {
222237
/// Use `TC4` for PWM (pins `PB6`, `PC7`, `PD7`)
223238
///
224239
/// # Example
225-
/// ```
240+
/// ```no_run
241+
/// use atmega_hal::atmega32u4 as hal;
242+
/// use hal::simple_pwm::{IntoPwmPin,Timer4Pwm,Prescaler};
243+
///
244+
/// let dp = hal::Peripherals::take().unwrap();
245+
/// let pins = hal::pins!(dp);
226246
/// let mut timer4 = Timer4Pwm::new(dp.TC4, Prescaler::Prescale64);
227247
///
228-
/// let mut d6 = pins.d6.into_output().into_pwm(&mut timer4);
229-
/// let mut d10 = pins.d10.into_output().into_pwm(&mut timer4);
230-
/// let mut d13 = pins.d13.into_output().into_pwm(&mut timer4);
248+
/// let mut pb6 = pins.pb6.into_output().into_pwm(&mut timer4);
249+
/// let mut pc7 = pins.pc7.into_output().into_pwm(&mut timer4);
250+
/// let mut pd7 = pins.pd7.into_output().into_pwm(&mut timer4);
231251
///
232-
/// d6.set_duty(128);
233-
/// d6.enable();
252+
/// pb6.set_duty(128);
253+
/// pb6.enable();
234254
/// ```
235255
pub struct Timer4Pwm {
236256
timer: hal::pac::TC4,

mcu/atmega-hal/src/atmega8.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,19 @@ impl_mod_simple_pwm! {
6767
/// Use `TC1` for PWM (pins `PB1`, `PB2`)
6868
///
6969
/// # Example
70-
/// ```
70+
/// ```no_run
71+
/// use atmega_hal::atmega8 as hal;
72+
/// use hal::simple_pwm::{IntoPwmPin,Timer1Pwm,Prescaler};
73+
///
74+
/// let dp = hal::Peripherals::take().unwrap();
75+
/// let pins = hal::pins!(dp);
7176
/// let mut timer1 = Timer1Pwm::new(dp.TC1, Prescaler::Prescale64);
7277
///
73-
/// let mut b1 = pins.b1.into_output().into_pwm(&mut timer1);
74-
/// let mut b2 = pins.b2.into_output().into_pwm(&mut timer1);
78+
/// let mut pb1 = pins.pb1.into_output().into_pwm(&mut timer1);
79+
/// let mut pb2 = pins.pb2.into_output().into_pwm(&mut timer1);
7580
///
76-
/// d9.set_duty(128);
77-
/// d9.enable();
81+
/// pb1.set_duty(128);
82+
/// pb1.enable();
7883
/// ```
7984
pub struct Timer1Pwm {
8085
timer: hal::pac::TC1,
@@ -118,14 +123,17 @@ impl_mod_simple_pwm! {
118123
/// Use `TC2` for PWM (pin `PB3`)
119124
///
120125
/// # Example
121-
/// ```
126+
/// ```no_run
127+
/// use atmega_hal::atmega8 as hal;
128+
/// use hal::simple_pwm::{IntoPwmPin,Timer2Pwm,Prescaler};
129+
///
130+
/// let dp = hal::Peripherals::take().unwrap();
131+
/// let pins = hal::pins!(dp);
122132
/// let mut timer2 = Timer2Pwm::new(dp.TC2, Prescaler::Prescale64);
123133
///
124-
/// let mut d11 = pins.d11.into_output().into_pwm(&mut timer2);
125-
/// let mut d3 = pins.d3.into_output().into_pwm(&mut timer2);
134+
/// let mut pb3 = pins.pb3.into_output().into_pwm(&mut timer2);
126135
///
127-
/// d11.set_duty(128);
128-
/// d11.enable();
136+
/// pb3.set_duty(128);
129137
/// ```
130138
pub struct Timer2Pwm {
131139
timer: hal::pac::TC2,

mcu/atmega-hal/src/impl/adc.rs

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,49 @@ macro_rules! impl_mod_adc {
1919
pub mod adc {
2020
//! Analog-to-Digital Converter
2121
//!
22-
//! # Example
23-
//!
24-
//! Complete example source code can be found in the repository:
22+
//! For full source code, please refer to the ATmega ADC example:
2523
//! [`atmega2560-adc.rs`](https://github.com/Rahix/avr-hal/blob/main/examples/atmega2560/src/bin/atmega2560-adc.rs)
2624
//!
25+
//! # Example: Read pins using `analog_read()`
26+
//!
27+
//! ```no_run
28+
#![doc = concat!("use atmega_hal::", stringify!($hal), " as hal;")]
29+
//!
30+
//! let dp = hal::Peripherals::take().unwrap();
31+
//! let pins = hal::pins!(dp);
32+
//!
33+
//! let mut adc = hal::Adc::<avr_hal_generic::clock::MHz1>::new(dp.ADC, Default::default());
34+
//!
35+
$(
36+
#![doc = paste!{ concat!(
37+
"let ", stringify!([< input_ $pin_name:lower >]), " = pins.", stringify!([< $pin_name:lower >]), ".into_analog_input(&mut adc);\n",
38+
"let ", stringify!([< value_ $pin_name:lower >]), " = ", stringify!([< input_ $pin_name:lower >]), ".analog_read(&mut adc);\n\n"
39+
)}]
40+
)*
2741
//! ```
28-
//! let dp = atmega_hal::Peripherals::take().unwrap();
29-
//! let pins = atmega_hal::pins!(dp);
3042
//!
31-
//! let mut adc = Adc::new(dp.ADC, Default::default());
43+
//! # Example: Read channels (including pins) using `read_blocking()`
44+
//!
45+
//! ```no_run
46+
#![doc = concat!("use atmega_hal::", stringify!($hal), " as hal;")]
3247
//!
33-
//! let channels: [atmega_hal::adc::Channel; 4] = [
34-
//! pins.pf0.into_analog_input(&mut adc).into_channel(),
35-
//! pins.pf1.into_analog_input(&mut adc).into_channel(),
36-
//! pins.pf2.into_analog_input(&mut adc).into_channel(),
37-
//! pins.pf3.into_analog_input(&mut adc).into_channel(),
38-
//! ];
48+
//! let dp = hal::Peripherals::take().unwrap();
49+
//! let pins = hal::pins!(dp);
3950
//!
40-
//! for (index, channel) in channels.iter().enumerate() {
41-
//! let value = adc.read_blocking(channel);
42-
//! ufmt::uwrite!(&mut serial, "CH{}: {} ", index, value).unwrap();
43-
//! }
51+
//! let mut adc = hal::Adc::<avr_hal_generic::clock::MHz1>::new(dp.ADC, Default::default());
52+
//!
53+
//! //
54+
$(
55+
#![doc = paste!{ concat!(
56+
"let ", stringify!([< channel_ $pin_name:lower >]), " = pins.", stringify!([< $pin_name:lower >]), ".into_analog_input(&mut adc).into_channel();\n",
57+
"let ", stringify!([< value_ $pin_name:lower >]), " = adc.read_blocking(&", stringify!([< channel_ $pin_name:lower >]), ");\n\n"
58+
) }]
59+
)*
60+
$(
61+
#![doc = paste!{ concat!(
62+
"let ", stringify!([< value_ $channel_name:lower >]), " = adc.read_blocking(&hal::adc::channel::", stringify!([< $channel_name >]), ");\n\n"
63+
) }]
64+
)*
4465
//! ```
4566
4667
use avr_hal_generic::paste::paste;
@@ -53,14 +74,6 @@ macro_rules! impl_mod_adc {
5374
///
5475
/// Some channels are not directly connected to pins. This module provides types which can be used
5576
/// to access them.
56-
///
57-
/// # Example
58-
/// ```
59-
/// let dp = atmega_hal::Peripherals::take().unwrap();
60-
/// let mut adc = atmega_hal::Adc::new(dp.ADC, Default::default());
61-
///
62-
/// let value = adc.read_blocking(&channel::Vbg);
63-
/// ```
6477
#[allow(non_camel_case_types)]
6578
pub mod channel {
6679
$(

mcu/atmega-hal/src/impl/eeprom.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,18 @@ macro_rules! impl_mod_eeprom {
1616
//! Complete example source code can be found in the repository:
1717
//! [`atmega2560-eeprom.rs`](https://github.com/Rahix/avr-hal/blob/main/examples/atmega2560/src/bin/atmega2560-eeprom.rs)
1818
//!
19-
//! ```
19+
//! ```no_run
20+
#![doc = concat!("use atmega_hal::", stringify!($hal), " as hal;")]
2021
//! const BOOT_COUNT_OFFSET: u16 = 0;
2122
//!
22-
//! let dp = atmega_hal::Peripherals::take().unwrap();
23-
//! let mut eeprom = Eeprom::new(dp.EEPROM);
23+
//! let dp = hal::Peripherals::take().unwrap();
24+
//! let mut eeprom = hal::Eeprom::new(dp.EEPROM);
2425
//!
2526
//! let mut boot_count = eeprom.read_byte(BOOT_COUNT_OFFSET);
2627
//! boot_count = boot_count.wrapping_add(1);
2728
//! eeprom.write_byte(BOOT_COUNT_OFFSET, boot_count);
2829
//!
29-
//! ufmt::uwriteln!(&mut serial, "Boot count: {}", boot_count).unwrap();
30+
//! // ufmt::uwriteln!(&mut serial, "Boot count: {}", boot_count).unwrap();
3031
//! ```
3132
pub use avr_hal_generic::eeprom::{EepromOps, OutOfBoundsError};
3233

mcu/atmega-hal/src/impl/i2c.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,29 @@ macro_rules! impl_mod_i2c {
1717
//! Complete example source code can be found in the repository:
1818
//! [`atmega2560-i2cdetect.rs`](https://github.com/Rahix/avr-hal/blob/main/examples/atmega2560/src/bin/atmega2560-i2cdetect.rs)
1919
//!
20-
//! ```
21-
//! let dp = atmega_hal::Peripherals::take().unwrap();
22-
//! let pins = atmega_hal::pins!(dp);
20+
//! ```no_run
21+
#![doc = concat!("use atmega_hal::", stringify!($hal), " as hal;")]
22+
//!
23+
//! let dp = hal::Peripherals::take().unwrap();
24+
//! let pins = hal::pins!(dp);
2325
//!
24-
//! let mut i2c = I2c::new(
25-
//! dp.TWI,
26-
//! pins.pd1.into_pull_up_input(),
27-
//! pins.pd0.into_pull_up_input(),
28-
//! 50_000,
29-
//! );
26+
//! type Clock = avr_hal_generic::clock::MHz16;
27+
$(
28+
#![doc = paste!{ concat!(
29+
"let mut i2c = hal::i2c::", stringify!($interface), "::<Clock>::new(\n",
30+
" dp.", stringify!($peripheral), ",\n",
31+
" pins.", stringify!([< $sda:lower >]), ".into_pull_up_input(),\n",
32+
" pins.", stringify!([< $scl:lower >]), ".into_pull_up_input(),\n",
33+
" 50_000,\n",
34+
");\n",
35+
) }]
36+
)+
3037
//!
31-
//! i2c.i2cdetect(&mut serial, atmega_hal::i2c::Direction::Read).unwrap();
38+
//! // i2c.i2cdetect(&mut serial, hal::i2c::Direction::Read).unwrap();
3239
//! ```
3340
3441
pub use avr_hal_generic::i2c::*;
42+
use avr_hal_generic::paste::paste;
3543
use crate::$hal as hal;
3644

3745
$(

mcu/atmega-hal/src/impl/port.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,24 @@ macro_rules! impl_mod_port {
77
//!
88
//! # Example
99
//!
10-
//! Complete example source code can be found in the repository:
10+
//! For full source code, please refer to the ATmega port example:
1111
//! [`atmega2560-blink.rs`](https://github.com/Rahix/avr-hal/blob/main/examples/atmega2560/src/bin/atmega2560-blink.rs)
1212
//!
13-
//! ```
14-
//! let dp = atmega_hal::Peripherals::take().unwrap();
15-
//! let pins = atmega_hal::pins!(dp);
13+
//! ```no_run
14+
//! use atmega_hal::prelude::*;
15+
#![doc = concat!("use atmega_hal::", stringify!($hal), " as hal;")]
16+
//!
17+
//! type Clock = atmega_hal::clock::MHz8;
18+
//! let mut delay = atmega_hal::delay::Delay::<Clock>::new();
19+
//!
20+
//! let dp = hal::Peripherals::take().unwrap();
21+
//! let pins = hal::pins!(dp);
1622
//!
17-
//! let mut led = pins.pb7.into_output();
23+
//! let mut led = pins.pb2.into_output();
1824
//!
1925
//! loop {
2026
//! led.toggle();
21-
//! delay_ms(1000);
27+
//! delay.delay_ms(1000u16);
2228
//! }
2329
//! ```
2430
pub use avr_hal_generic::port::{mode, PinMode, PinOps};

0 commit comments

Comments
 (0)