Skip to content

Commit f7bdb5c

Browse files
committed
Fixed attiny doctests
1 parent e9fc742 commit f7bdb5c

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
@@ -123,6 +123,11 @@ jobs:
123123
- name: Test-compile HAL crate for an MCU
124124
if: "${{ matrix.m.type == 'mcu' }}"
125125
run: cd "mcu/${{ matrix.m.crate }}" && cargo build --features "${{ matrix.m.name }}" -Z build-std=core --target "../../avr-specs/avr-${{ matrix.m.spec }}.json"
126+
- name: Compile doctests for an ATtiny MCU
127+
if: "${{ matrix.m.crate == 'attiny-hal' }}"
128+
run: >-
129+
cd "mcu/${{ matrix.m.crate }}" &&
130+
cargo test --doc --features "${{ matrix.m.name }}" -Z build-std=core --target "../../avr-specs/avr-${{ matrix.m.spec }}.json"
126131
127132
ravedude:
128133
name: "ravedude"

mcu/attiny-hal/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ _peripheral-simple-pwm = []
3434
no-globals = []
3535

3636

37+
[dev-dependencies]
38+
ufmt = "0.2.0"
39+
embedded-hal = "1.0"
40+
3741
[dependencies]
3842
avr-hal-generic = { path = "../../avr-hal-generic/" }
3943

mcu/attiny-hal/src/attiny85.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,19 @@ avr_hal! {
2020
/// Use `TC0` for PWM (pins `PB0`, `PB1`)
2121
///
2222
/// # Example
23-
/// ```
23+
/// ```no_run
24+
/// use attiny_hal::attiny85 as hal;
25+
/// use hal::simple_pwm::{IntoPwmPin,Timer0Pwm,Prescaler};
26+
///
27+
/// let dp = hal::Peripherals::take().unwrap();
28+
/// let pins = hal::pins!(dp);
2429
/// let mut timer0 = Timer0Pwm::new(dp.TC0, Prescaler::Prescale64);
2530
///
26-
/// let mut d0 = pins.d0.into_output().into_pwm(&mut timer0);
27-
/// let mut d1 = pins.d1.into_output().into_pwm(&mut timer0);
31+
/// let mut pb0 = pins.pb0.into_output().into_pwm(&mut timer0);
32+
/// let mut pb1 = pins.pb1.into_output().into_pwm(&mut timer0);
2833
///
29-
/// d0.set_duty(128);
30-
/// d0.enable();
34+
/// pb0.set_duty(128);
35+
/// pb0.enable();
3136
/// ```
3237
pub struct Timer0Pwm {
3338
timer: crate::attiny85::pac::TC0,
@@ -67,13 +72,18 @@ avr_hal! {
6772
/// Use `TC1` for PWM (pins `PB4`)
6873
///
6974
/// # Example
70-
/// ```
75+
/// ```no_run
76+
/// use attiny_hal::attiny85 as hal;
77+
/// use hal::simple_pwm::{IntoPwmPin,Timer1Pwm,Prescaler};
78+
///
79+
/// let dp = hal::Peripherals::take().unwrap();
80+
/// let pins = hal::pins!(dp);
7181
/// let mut timer1 = Timer1Pwm::new(dp.TC1, Prescaler::Prescale64);
7282
///
73-
/// let mut d4 = pins.d4.into_output().into_pwm(&mut timer1);
83+
/// let mut pb4 = pins.pb4.into_output().into_pwm(&mut timer1);
7484
///
75-
/// d4.set_duty(128);
76-
/// d4.enable();
85+
/// pb4.set_duty(128);
86+
/// pb4.enable();
7787
/// ```
7888
pub struct Timer1Pwm {
7989
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
@@ -24,14 +24,19 @@ avr_hal! {
2424
/// Use `TC1` for PWM (pins `PB1`, 'PB2')
2525
///
2626
/// # Example
27-
/// ```
27+
/// ```no_run
28+
/// use attiny_hal::attiny88 as hal;
29+
/// use hal::simple_pwm::{Timer1Pwm,Prescaler,IntoPwmPin};
30+
///
31+
/// let dp = hal::Peripherals::take().unwrap();
32+
/// let pins = hal::pins!(dp);
2833
/// let mut timer1 = Timer1Pwm::new(dp.TC1, Prescaler::Prescale64);
2934
///
30-
/// let mut d9 = pins.d9.into_output().into_pwm(&mut timer1);
31-
/// let mut d10 = pins.d10.into_output().into_pwm(&mut timer1);
35+
/// let mut pb1 = pins.pb1.into_output().into_pwm(&mut timer1);
36+
/// let mut pb2 = pins.pb2.into_output().into_pwm(&mut timer1);
3237
///
33-
/// d9.set_duty(128);
34-
/// d9.enable();
38+
/// pb1.set_duty(128);
39+
/// pb1.enable();
3540
/// ```
3641
pub struct Timer1Pwm {
3742
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)