Skip to content

Commit 928daa8

Browse files
committed
Fixed attiny doctests
1 parent c5a9717 commit 928daa8

File tree

8 files changed

+109
-59
lines changed

8 files changed

+109
-59
lines changed

.github/workflows/ci.yml

+5
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

+4
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

+19-9
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,19 @@ impl_mod_simple_pwm! {
7272
/// Use `TC0` for PWM (pins `PB0`, `PB1`)
7373
///
7474
/// # Example
75-
/// ```
75+
/// ```no_run
76+
/// use attiny_hal::attiny85 as hal;
77+
/// use hal::simple_pwm::{IntoPwmPin,Timer0Pwm,Prescaler};
78+
///
79+
/// let dp = hal::Peripherals::take().unwrap();
80+
/// let pins = hal::pins!(dp);
7681
/// let mut timer0 = Timer0Pwm::new(dp.TC0, Prescaler::Prescale64);
7782
///
78-
/// let mut d0 = pins.d0.into_output().into_pwm(&mut timer0);
79-
/// let mut d1 = pins.d1.into_output().into_pwm(&mut timer0);
83+
/// let mut pb0 = pins.pb0.into_output().into_pwm(&mut timer0);
84+
/// let mut pb1 = pins.pb1.into_output().into_pwm(&mut timer0);
8085
///
81-
/// d0.set_duty(128);
82-
/// d0.enable();
86+
/// pb0.set_duty(128);
87+
/// pb0.enable();
8388
/// ```
8489
pub struct Timer0Pwm {
8590
timer: crate::attiny85::pac::TC0,
@@ -119,13 +124,18 @@ impl_mod_simple_pwm! {
119124
/// Use `TC1` for PWM (pins `PB4`)
120125
///
121126
/// # Example
122-
/// ```
127+
/// ```no_run
128+
/// use attiny_hal::attiny85 as hal;
129+
/// use hal::simple_pwm::{IntoPwmPin,Timer1Pwm,Prescaler};
130+
///
131+
/// let dp = hal::Peripherals::take().unwrap();
132+
/// let pins = hal::pins!(dp);
123133
/// let mut timer1 = Timer1Pwm::new(dp.TC1, Prescaler::Prescale64);
124134
///
125-
/// let mut d4 = pins.d4.into_output().into_pwm(&mut timer1);
135+
/// let mut pb4 = pins.pb4.into_output().into_pwm(&mut timer1);
126136
///
127-
/// d4.set_duty(128);
128-
/// d4.enable();
137+
/// pb4.set_duty(128);
138+
/// pb4.enable();
129139
/// ```
130140
pub struct Timer1Pwm {
131141
timer: crate::attiny85::pac::TC1,

mcu/attiny-hal/src/attiny88.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,19 @@ impl_mod_simple_pwm! {
7171
/// Use `TC1` for PWM (pins `PB1`, 'PB2')
7272
///
7373
/// # Example
74-
/// ```
74+
/// ```no_run
75+
/// use attiny_hal::attiny88 as hal;
76+
/// use hal::simple_pwm::{Timer1Pwm,Prescaler,IntoPwmPin};
77+
///
78+
/// let dp = hal::Peripherals::take().unwrap();
79+
/// let pins = hal::pins!(dp);
7580
/// let mut timer1 = Timer1Pwm::new(dp.TC1, Prescaler::Prescale64);
7681
///
77-
/// let mut d9 = pins.d9.into_output().into_pwm(&mut timer1);
78-
/// let mut d10 = pins.d10.into_output().into_pwm(&mut timer1);
82+
/// let mut pb1 = pins.pb1.into_output().into_pwm(&mut timer1);
83+
/// let mut pb2 = pins.pb2.into_output().into_pwm(&mut timer1);
7984
///
80-
/// d9.set_duty(128);
81-
/// d9.enable();
85+
/// pb1.set_duty(128);
86+
/// pb1.enable();
8287
/// ```
8388
pub struct Timer1Pwm {
8489
timer: crate::attiny88::pac::TC1,

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

+38-23
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,51 @@ 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()`
43+
//!
44+
//! ```no_run
45+
#![doc = concat!("use attiny_hal::", stringify!($hal), " as hal;")]
3046
//!
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-
//! ];
47+
//! let dp = hal::Peripherals::take().unwrap();
48+
//! let pins = hal::pins!(dp);
3749
//!
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-
//! }
50+
//! type Clock = avr_hal_generic::clock::MHz16;
51+
//! let mut adc = hal::Adc::<Clock>::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+
)*
4265
//! ```
4366
4467
use avr_hal_generic::paste::paste;
@@ -61,14 +84,6 @@ macro_rules! impl_mod_adc {
6184
///
6285
/// Some channels are not directly connected to pins. This module provides types which can be used
6386
/// 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-
/// ```
7287
#[allow(non_camel_case_types)]
7388
pub mod channel {
7489
$(

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

+4-5
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

+10-4
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,21 @@ macro_rules! impl_mod_port {
88
//! For full source code, please refer to the ATmega port example:
99
//! [`atmega2560-blink.rs`](https://github.com/Rahix/avr-hal/blob/main/examples/atmega2560/src/bin/atmega2560-blink.rs)
1010
//!
11-
//! ```
12-
//! let dp = attiny_hal::Peripherals::take().unwrap();
13-
//! let pins = attiny_hal::pins!(dp);
11+
//! ```no_run
12+
//! use attiny_hal::prelude::*;
13+
#![doc = concat!("use attiny_hal::", stringify!($hal), " as hal;")]
14+
//!
15+
//! type Clock = attiny_hal::clock::MHz8;
16+
//! let mut delay = attiny_hal::delay::Delay::<Clock>::new();
17+
//!
18+
//! let dp = hal::Peripherals::take().unwrap();
19+
//! let pins = hal::pins!(dp);
1420
//!
1521
//! let mut led = pins.pb2.into_output();
1622
//!
1723
//! loop {
1824
//! led.toggle();
19-
//! delay_ms(1000);
25+
//! delay.delay_ms(1000u16);
2026
//! }
2127
//! ```
2228

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

+19-13
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,36 @@ macro_rules! impl_mod_spi {
1414
//! For full source code, please refer to the ATmega SPI example:
1515
//! [`atmega2560-spi-feedback.rs`](https://github.com/Rahix/avr-hal/blob/main/examples/atmega2560/src/bin/atmega2560-spi-feedback.rs)
1616
//!
17-
//! ```
18-
//! let dp = attiny_hal::Peripherals::take().unwrap();
19-
//! let pins = attiny_hal::pins!(dp);
17+
//! ```no_run
18+
#![doc = concat!("use attiny_hal::", stringify!($hal), " as hal;")]
19+
//!
20+
//! use embedded_hal::digital::OutputPin;
21+
//! use embedded_hal::spi::SpiBus;
22+
//!
23+
//! let dp = hal::Peripherals::take().unwrap();
24+
//! let pins = hal::pins!(dp);
2025
//!
21-
//! let (mut spi, mut cs) = spi::Spi::new(
22-
//! dp.SPI,
23-
//! pins.pa4.into_output(),
24-
//! pins.pa6.into_output(),
25-
//! pins.pa5.into_pull_up_input(),
26-
//! pins.pa3.into_output(),
27-
//! spi::Settings::default(),
28-
//! );
26+
#![doc = paste!{ concat!(
27+
"let (mut spi, mut cs) = hal::spi::Spi::new(\n",
28+
" dp.SPI,\n",
29+
" pins.", stringify!([< $sclk:lower >]), ".into_output(),\n",
30+
" pins.", stringify!([< $mosi:lower >]), ".into_output(),\n",
31+
" pins.", stringify!([< $miso:lower >]), ".into_pull_up_input(),\n",
32+
" pins.", stringify!([< $cs:lower >]), ".into_output(),\n",
33+
" hal::spi::Settings::default(),\n",
34+
");\n",
35+
) }]
2936
//!
3037
//! let data_out = b"Hello World!";
3138
//! let mut data_in = [0u8; 12];
3239
//!
3340
//! cs.set_low().unwrap();
3441
//! spi.transfer(&mut data_in, data_out).unwrap();
3542
//! cs.set_high().unwrap();
36-
//!
37-
//! ufmt::uwriteln!(&mut serial, "data: {:?}", data_in).unwrap();
3843
//! ```
3944
4045
pub use avr_hal_generic::spi::*;
46+
use avr_hal_generic::paste::paste;
4147
use crate::$hal as hal;
4248

4349
pub type Spi = avr_hal_generic::spi::Spi<

0 commit comments

Comments
 (0)