Skip to content

Commit

Permalink
Fixed attiny doctests
Browse files Browse the repository at this point in the history
  • Loading branch information
innermatrix committed Dec 30, 2024
1 parent 42bd207 commit 4df103f
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 59 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ jobs:
run: >-
cd "mcu/${{ matrix.m.crate }}" &&
cargo build --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 == 'attiny-hal' }}"
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"
ravedude:
name: "ravedude"
Expand Down
4 changes: 4 additions & 0 deletions mcu/attiny-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ _peripheral-spi = []
_peripheral-simple-pwm = []


[dev-dependencies]
ufmt = "0.2.0"
embedded-hal = "1.0"

[dependencies]
avr-hal-generic = { path = "../../avr-hal-generic/" }

Expand Down
28 changes: 19 additions & 9 deletions mcu/attiny-hal/src/attiny85.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,19 @@ impl_mod_simple_pwm! {
/// Use `TC0` for PWM (pins `PB0`, `PB1`)
///
/// # Example
/// ```
/// ```no_run
/// use attiny_hal::attiny85 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 d0 = pins.d0.into_output().into_pwm(&mut timer0);
/// let mut d1 = pins.d1.into_output().into_pwm(&mut timer0);
/// let mut pb0 = pins.pb0.into_output().into_pwm(&mut timer0);
/// let mut pb1 = pins.pb1.into_output().into_pwm(&mut timer0);
///
/// d0.set_duty(128);
/// d0.enable();
/// pb0.set_duty(128);
/// pb0.enable();
/// ```
pub struct Timer0Pwm {
timer: crate::attiny85::pac::TC0,
Expand Down Expand Up @@ -119,13 +124,18 @@ impl_mod_simple_pwm! {
/// Use `TC1` for PWM (pins `PB4`)
///
/// # Example
/// ```
/// ```no_run
/// use attiny_hal::attiny85 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 d4 = pins.d4.into_output().into_pwm(&mut timer1);
/// let mut pb4 = pins.pb4.into_output().into_pwm(&mut timer1);
///
/// d4.set_duty(128);
/// d4.enable();
/// pb4.set_duty(128);
/// pb4.enable();
/// ```
pub struct Timer1Pwm {
timer: crate::attiny85::pac::TC1,
Expand Down
15 changes: 10 additions & 5 deletions mcu/attiny-hal/src/attiny88.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,19 @@ impl_mod_simple_pwm! {
/// Use `TC1` for PWM (pins `PB1`, 'PB2')
///
/// # Example
/// ```
/// ```no_run
/// use attiny_hal::attiny88 as hal;
/// use hal::simple_pwm::{Timer1Pwm,Prescaler,IntoPwmPin};
///
/// 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: crate::attiny88::pac::TC1,
Expand Down
61 changes: 38 additions & 23 deletions mcu/attiny-hal/src/impl/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,51 @@ macro_rules! impl_mod_adc {
pub mod adc {
//! Analog-to-Digital Converter
//!
//! # Example
//!
//! 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 attiny_hal::", stringify!($hal), " as hal;")]
//!
//! let dp = hal::Peripherals::take().unwrap();
//! let pins = hal::pins!(dp);
//!
//! type Clock = avr_hal_generic::clock::MHz16;
//! let mut adc = hal::Adc::<Clock>::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 = attiny_hal::Peripherals::take().unwrap();
//! let pins = attiny_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 attiny_hal::", stringify!($hal), " as hal;")]
//!
//! let channels: [attiny_hal::adc::Channel; 4] = [
//! pins.pa0.into_analog_input(&mut adc).into_channel(),
//! pins.pa1.into_analog_input(&mut adc).into_channel(),
//! pins.pa2.into_analog_input(&mut adc).into_channel(),
//! pins.pa3.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();
//! }
//! type Clock = avr_hal_generic::clock::MHz16;
//! let mut adc = hal::Adc::<Clock>::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;
Expand All @@ -61,14 +84,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 = attiny_hal::Peripherals::take().unwrap();
/// let mut adc = attiny_hal::Adc::new(dp.ADC, Default::default());
///
/// let value = adc.read_blocking(&channel::Vbg);
/// ```
#[allow(non_camel_case_types)]
pub mod channel {
$(
Expand Down
9 changes: 4 additions & 5 deletions mcu/attiny-hal/src/impl/eeprom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@ macro_rules! impl_mod_eeprom {
//! For full source code, please refer to the ATmega EEPROM example:
//! [`atmega2560-eeprom.rs`](https://github.com/Rahix/avr-hal/blob/main/examples/atmega2560/src/bin/atmega2560-eeprom.rs)
//!
//! ```
//! ```no_run
#![doc = concat!("use attiny_hal::", stringify!($hal), " as hal;")]
//! const BOOT_COUNT_OFFSET: u16 = 0;
//!
//! let dp = attiny_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();
//! ```
pub use avr_hal_generic::eeprom::{EepromOps, OutOfBoundsError};
Expand Down
14 changes: 10 additions & 4 deletions mcu/attiny-hal/src/impl/port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,21 @@ macro_rules! impl_mod_port {
//! 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 = attiny_hal::Peripherals::take().unwrap();
//! let pins = attiny_hal::pins!(dp);
//! ```no_run
//! use attiny_hal::prelude::*;
#![doc = concat!("use attiny_hal::", stringify!($hal), " as hal;")]
//!
//! type Clock = attiny_hal::clock::MHz8;
//! let mut delay = attiny_hal::delay::Delay::<Clock>::new();
//!
//! let dp = hal::Peripherals::take().unwrap();
//! let pins = hal::pins!(dp);
//!
//! let mut led = pins.pb2.into_output();
//!
//! loop {
//! led.toggle();
//! delay_ms(1000);
//! delay.delay_ms(1000u16);
//! }
//! ```
Expand Down
32 changes: 19 additions & 13 deletions mcu/attiny-hal/src/impl/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,36 @@ macro_rules! impl_mod_spi {
//! For full source code, please refer to the ATmega SPI example:
//! [`atmega2560-spi-feedback.rs`](https://github.com/Rahix/avr-hal/blob/main/examples/atmega2560/src/bin/atmega2560-spi-feedback.rs)
//!
//! ```
//! let dp = attiny_hal::Peripherals::take().unwrap();
//! let pins = attiny_hal::pins!(dp);
//! ```no_run
#![doc = concat!("use attiny_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.pa4.into_output(),
//! pins.pa6.into_output(),
//! pins.pa5.into_pull_up_input(),
//! pins.pa3.into_output(),
//! spi::Settings::default(),
//! );
#![doc = paste!{ concat!(
"let (mut spi, mut cs) = hal::spi::Spi::new(\n",
" dp.SPI,\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];
//!
//! cs.set_low().unwrap();
//! spi.transfer(&mut data_in, data_out).unwrap();
//! cs.set_high().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;

pub type Spi = avr_hal_generic::spi::Spi<
Expand Down

0 comments on commit 4df103f

Please sign in to comment.