Skip to content

Commit 45f52a9

Browse files
committed
Fixed attiny doctests
1 parent 6515962 commit 45f52a9

File tree

8 files changed

+123
-61
lines changed

8 files changed

+123
-61
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ jobs:
124124
run: >-
125125
cd "mcu/${{ matrix.m.crate }}" &&
126126
cargo build --features "${{ matrix.m.name }}-no-deprecated-globals" -Z build-std=core --target "../../avr-specs/avr-${{ matrix.m.spec }}.json"
127+
- name: Compile doctests for an MCU (no deprecated globals)
128+
if: "${{ matrix.m.crate == 'attiny-hal' }}"
129+
run: >-
130+
cd "mcu/${{ matrix.m.crate }}" &&
131+
cargo test --doc --features "${{ matrix.m.name }}-no-deprecated-globals" -Z build-std=core --target "../../avr-specs/avr-${{ matrix.m.spec }}.json"
127132
128133
ravedude:
129134
name: "ravedude"

mcu/attiny-hal/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ _peripheral-spi = []
6767
_peripheral-simple-pwm = []
6868

6969

70+
[dev-dependencies]
71+
ufmt = "0.2.0"
72+
embedded-hal = "1.0"
73+
7074
[dependencies]
7175
avr-hal-generic = { path = "../../avr-hal-generic/" }
7276

mcu/attiny-hal/src/attiny85.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,19 @@ impl_mod_simple_pwm! {
6060
/// Use `TC0` for PWM (pins `PB0`, `PB1`)
6161
///
6262
/// # Example
63-
/// ```
63+
/// ```no_run
64+
/// use attiny_hal::attiny85 as hal;
65+
/// use hal::simple_pwm::{IntoPwmPin,Timer0Pwm,Prescaler};
66+
///
67+
/// let dp = hal::Peripherals::take().unwrap();
68+
/// let pins = hal::pins!(dp);
6469
/// let mut timer0 = Timer0Pwm::new(dp.TC0, Prescaler::Prescale64);
6570
///
66-
/// let mut d0 = pins.d0.into_output().into_pwm(&mut timer0);
67-
/// let mut d1 = pins.d1.into_output().into_pwm(&mut timer0);
71+
/// let mut pb0 = pins.pb0.into_output().into_pwm(&mut timer0);
72+
/// let mut pb1 = pins.pb1.into_output().into_pwm(&mut timer0);
6873
///
69-
/// d0.set_duty(128);
70-
/// d0.enable();
74+
/// pb0.set_duty(128);
75+
/// pb0.enable();
7176
/// ```
7277
pub struct Timer0Pwm {
7378
timer: crate::attiny85::pac::TC0,
@@ -107,13 +112,18 @@ impl_mod_simple_pwm! {
107112
/// Use `TC1` for PWM (pins `PB4`)
108113
///
109114
/// # Example
110-
/// ```
115+
/// ```no_run
116+
/// use attiny_hal::attiny85 as hal;
117+
/// use hal::simple_pwm::{IntoPwmPin,Timer1Pwm,Prescaler};
118+
///
119+
/// let dp = hal::Peripherals::take().unwrap();
120+
/// let pins = hal::pins!(dp);
111121
/// let mut timer1 = Timer1Pwm::new(dp.TC1, Prescaler::Prescale64);
112122
///
113-
/// let mut d4 = pins.d4.into_output().into_pwm(&mut timer1);
123+
/// let mut pb4 = pins.pb4.into_output().into_pwm(&mut timer1);
114124
///
115-
/// d4.set_duty(128);
116-
/// d4.enable();
125+
/// pb4.set_duty(128);
126+
/// pb4.enable();
117127
/// ```
118128
pub struct Timer1Pwm {
119129
timer: crate::attiny85::pac::TC1,

mcu/attiny-hal/src/attiny88.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,19 @@ impl_mod_simple_pwm! {
6060
/// Use `TC1` for PWM (pins `PB1`, 'PB2')
6161
///
6262
/// # Example
63-
/// ```
63+
/// ```no_run
64+
/// use attiny_hal::attiny88 as hal;
65+
/// use hal::simple_pwm::{Timer1Pwm,Prescaler,IntoPwmPin};
66+
///
67+
/// let dp = hal::Peripherals::take().unwrap();
68+
/// let pins = hal::pins!(dp);
6469
/// let mut timer1 = Timer1Pwm::new(dp.TC1, Prescaler::Prescale64);
6570
///
66-
/// let mut d9 = pins.d9.into_output().into_pwm(&mut timer1);
67-
/// let mut d10 = pins.d10.into_output().into_pwm(&mut timer1);
71+
/// let mut pb1 = pins.pb1.into_output().into_pwm(&mut timer1);
72+
/// let mut pb2 = pins.pb2.into_output().into_pwm(&mut timer1);
6873
///
69-
/// d9.set_duty(128);
70-
/// d9.enable();
74+
/// pb1.set_duty(128);
75+
/// pb1.enable();
7176
/// ```
7277
pub struct Timer1Pwm {
7378
timer: crate::attiny88::pac::TC1,

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

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,49 @@ macro_rules! impl_mod_adc {
1717
pub mod adc {
1818
//! Analog-to-Digital Converter
1919
//!
20-
//! # Example
21-
//!
2220
//! For full source code, please refer to the ATmega ADC example:
2321
//! [`atmega2560-adc.rs`](https://github.com/Rahix/avr-hal/blob/main/examples/atmega2560/src/bin/atmega2560-adc.rs)
2422
//!
23+
//! # Example: Read pins using `analog_read()`
24+
//!
25+
//! ```no_run
26+
#![doc = concat!("use attiny_hal::", stringify!($hal), " as hal;")]
27+
//!
28+
//! let dp = hal::Peripherals::take().unwrap();
29+
//! let pins = hal::pins!(dp);
30+
//!
31+
//! type Clock = avr_hal_generic::clock::MHz16;
32+
//! let mut adc = hal::Adc::<Clock>::new(dp.ADC, Default::default());
33+
//!
34+
$(
35+
#![doc = paste!{ concat!(
36+
"let ", stringify!([< input_ $pin_name:lower >]), " = pins.", stringify!([< $pin_name:lower >]), ".into_analog_input(&mut adc);\n",
37+
"let ", stringify!([< value_ $pin_name:lower >]), " = ", stringify!([< input_ $pin_name:lower >]), ".analog_read(&mut adc);\n\n"
38+
)}]
39+
)*
2540
//! ```
26-
//! let dp = attiny_hal::Peripherals::take().unwrap();
27-
//! let pins = attiny_hal::pins!(dp);
2841
//!
29-
//! let mut adc = Adc::new(dp.ADC, Default::default());
42+
//! # Example: Read channels (including pins) using `read_blocking()`
3043
//!
31-
//! let channels: [attiny_hal::adc::Channel; 4] = [
32-
//! pins.pa0.into_analog_input(&mut adc).into_channel(),
33-
//! pins.pa1.into_analog_input(&mut adc).into_channel(),
34-
//! pins.pa2.into_analog_input(&mut adc).into_channel(),
35-
//! pins.pa3.into_analog_input(&mut adc).into_channel(),
36-
//! ];
44+
//! ```no_run
45+
#![doc = concat!("use attiny_hal::", stringify!($hal), " as hal;")]
3746
//!
38-
//! for (index, channel) in channels.iter().enumerate() {
39-
//! let value = adc.read_blocking(channel);
40-
//! ufmt::uwrite!(&mut serial, "CH{}: {} ", index, value).unwrap();
41-
//! }
47+
//! let dp = hal::Peripherals::take().unwrap();
48+
//! let pins = hal::pins!(dp);
49+
//!
50+
//! type Clock = avr_hal_generic::clock::MHz16;
51+
//! let mut adc = hal::Adc::<Clock>::new(dp.ADC, Default::default());
52+
$(
53+
#![doc = paste!{ concat!(
54+
"let ", stringify!([< channel_ $pin_name:lower >]), " = pins.", stringify!([< $pin_name:lower >]), ".into_analog_input(&mut adc).into_channel();\n",
55+
"let ", stringify!([< value_ $pin_name:lower >]), " = adc.read_blocking(&", stringify!([< channel_ $pin_name:lower >]), ");\n\n"
56+
) }]
57+
)*
58+
$(
59+
#![doc = paste!{ concat!(
60+
"let ", stringify!([< value_ $channel_name:lower >]), " = adc.read_blocking(&hal::adc::channel::", stringify!([< $channel_name >]), ");\n\n"
61+
) }]
62+
)*
4263
//! ```
4364
4465
use avr_hal_generic::paste::paste;
@@ -61,14 +82,6 @@ macro_rules! impl_mod_adc {
6182
///
6283
/// Some channels are not directly connected to pins. This module provides types which can be used
6384
/// to access them.
64-
///
65-
/// # Example
66-
/// ```
67-
/// let dp = attiny_hal::Peripherals::take().unwrap();
68-
/// let mut adc = attiny_hal::Adc::new(dp.ADC, Default::default());
69-
///
70-
/// let value = adc.read_blocking(&channel::Vbg);
71-
/// ```
7285
#[allow(non_camel_case_types)]
7386
pub mod channel {
7487
$(

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,16 @@ macro_rules! impl_mod_eeprom {
1313
//! For full source code, please refer to the ATmega EEPROM example:
1414
//! [`atmega2560-eeprom.rs`](https://github.com/Rahix/avr-hal/blob/main/examples/atmega2560/src/bin/atmega2560-eeprom.rs)
1515
//!
16-
//! ```
16+
//! ```no_run
17+
#![doc = concat!("use attiny_hal::", stringify!($hal), " as hal;")]
1718
//! const BOOT_COUNT_OFFSET: u16 = 0;
1819
//!
19-
//! let dp = attiny_hal::Peripherals::take().unwrap();
20-
//! let mut eeprom = Eeprom::new(dp.EEPROM);
20+
//! let dp = hal::Peripherals::take().unwrap();
21+
//! let mut eeprom = hal::Eeprom::new(dp.EEPROM);
2122
//!
2223
//! let mut boot_count = eeprom.read_byte(BOOT_COUNT_OFFSET);
2324
//! boot_count = boot_count.wrapping_add(1);
2425
//! eeprom.write_byte(BOOT_COUNT_OFFSET, boot_count);
25-
//!
26-
//! ufmt::uwriteln!(&mut serial, "Boot count: {}", boot_count).unwrap();
2726
//! ```
2827
2928
pub use avr_hal_generic::eeprom::{EepromOps, OutOfBoundsError};

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

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,33 @@ macro_rules! impl_mod_port {
1616
//! For full source code, please refer to the ATmega port example:
1717
//! [`atmega2560-blink.rs`](https://github.com/Rahix/avr-hal/blob/main/examples/atmega2560/src/bin/atmega2560-blink.rs)
1818
//!
19-
//! ```
20-
//! let dp = attiny_hal::Peripherals::take().unwrap();
21-
//! let pins = attiny_hal::pins!(dp);
19+
//! ```no_run
20+
//! use attiny_hal::prelude::*;
21+
#![doc = concat!("use attiny_hal::", stringify!($hal), " as hal;")]
22+
//!
23+
//! type Clock = attiny_hal::clock::MHz8;
24+
//! let mut delay = attiny_hal::delay::Delay::<Clock>::new();
25+
//!
26+
//! let dp = hal::Peripherals::take().unwrap();
27+
//! let pins = hal::pins!(dp);
2228
//!
23-
//! let mut led = pins.pb2.into_output();
29+
$(
30+
$(
31+
#![doc = paste!{ concat!(
32+
"let mut ", stringify!([< led_p $name:lower $pin >]), " = pins.", stringify!([< p $name:lower $pin >]), ".into_output();",
33+
) }]
34+
)+
35+
)+
2436
//!
2537
//! loop {
26-
//! led.toggle();
27-
//! delay_ms(1000);
38+
$(
39+
$(
40+
#![doc = paste!{ concat!(
41+
" ", stringify!([< led_p $name:lower $pin >]), ".toggle();",
42+
) }]
43+
)+
44+
)+
45+
//! delay.delay_ms(1000u16);
2846
//! }
2947
//! ```
3048

mcu/attiny-hal/src/impl/spi.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,38 @@ macro_rules! impl_mod_spi {
2222
//! For full source code, please refer to the ATmega SPI example:
2323
//! [`atmega2560-spi-feedback.rs`](https://github.com/Rahix/avr-hal/blob/main/examples/atmega2560/src/bin/atmega2560-spi-feedback.rs)
2424
//!
25-
//! ```
26-
//! let dp = attiny_hal::Peripherals::take().unwrap();
27-
//! let pins = attiny_hal::pins!(dp);
25+
//! ```no_run
26+
#![doc = concat!("use attiny_hal::", stringify!($hal), " as hal;")]
2827
//!
29-
//! let (mut spi, mut cs) = spi::Spi::new(
30-
//! dp.SPI,
31-
//! pins.pa4.into_output(),
32-
//! pins.pa6.into_output(),
33-
//! pins.pa5.into_pull_up_input(),
34-
//! pins.pa3.into_output(),
35-
//! spi::Settings::default(),
36-
//! );
28+
//! use embedded_hal::digital::OutputPin;
29+
//! use embedded_hal::spi::SpiBus;
30+
//!
31+
//! let dp = hal::Peripherals::take().unwrap();
32+
//! let pins = hal::pins!(dp);
33+
//!
34+
$(
35+
#![doc = paste!{ concat!(
36+
"let (mut spi, mut cs) = hal::spi::Spi::new(\n",
37+
" dp.", stringify!($peripheral), ",\n",
38+
" pins.", stringify!([< $sclk:lower >]), ".into_output(),\n",
39+
" pins.", stringify!([< $mosi:lower >]), ".into_output(),\n",
40+
" pins.", stringify!([< $miso:lower >]), ".into_pull_up_input(),\n",
41+
" pins.", stringify!([< $cs:lower >]), ".into_output(),\n",
42+
" hal::spi::Settings::default(),\n",
43+
");\n",
44+
) }]
45+
)+
3746
//!
3847
//! let data_out = b"Hello World!";
3948
//! let mut data_in = [0u8; 12];
4049
//!
4150
//! cs.set_low().unwrap();
4251
//! spi.transfer(&mut data_in, data_out).unwrap();
4352
//! cs.set_high().unwrap();
44-
//!
45-
//! ufmt::uwriteln!(&mut serial, "data: {:?}", data_in).unwrap();
4653
//! ```
4754
4855
pub use avr_hal_generic::spi::*;
56+
use avr_hal_generic::paste::paste;
4957
use crate::$hal as hal;
5058

5159
$(

0 commit comments

Comments
 (0)