From 297d3db409685ea548f4421911f305120efa024c Mon Sep 17 00:00:00 2001 From: Iris Artin Date: Sun, 9 Mar 2025 11:06:39 -0400 Subject: [PATCH 1/2] Allow impl_simple_pwm to accept a fully qualified pin type --- avr-hal-generic/src/simple_pwm.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/avr-hal-generic/src/simple_pwm.rs b/avr-hal-generic/src/simple_pwm.rs index 93b4081bf5..4952e3e925 100644 --- a/avr-hal-generic/src/simple_pwm.rs +++ b/avr-hal-generic/src/simple_pwm.rs @@ -123,7 +123,7 @@ macro_rules! impl_simple_pwm { timer: $TIMER:ty, init: |$init_timer:ident, $prescaler:ident| $init_block:block, pins: {$( - $PXi:ident: { + $PXi:ty: { ocr: $ocr:ident, $into_pwm:ident: |$pin_timer:ident| if enable $pin_enable_block:block else $pin_disable_block:block, From 8805c2f5444bb5aa3d91b5eef92b4d32735c132b Mon Sep 17 00:00:00 2001 From: Iris Artin Date: Fri, 7 Mar 2025 19:38:53 -0500 Subject: [PATCH 2/2] Refactor attiny-hal using separate modules per device --- mcu/attiny-hal/Cargo.toml | 14 +- mcu/attiny-hal/src/adc.rs | 209 ----------------------- mcu/attiny-hal/src/attiny167.rs | 75 +++++++++ mcu/attiny-hal/src/attiny2313.rs | 21 +++ mcu/attiny-hal/src/attiny84.rs | 54 ++++++ mcu/attiny-hal/src/attiny85.rs | 80 +++++++++ mcu/attiny-hal/src/attiny88.rs | 80 +++++++++ mcu/attiny-hal/src/eeprom.rs | 56 ------- mcu/attiny-hal/src/globals.rs | 84 ++++++++++ mcu/attiny-hal/src/impl/adc.rs | 149 ++++++++++++++++ mcu/attiny-hal/src/impl/eeprom.rs | 48 ++++++ mcu/attiny-hal/src/impl/mod.rs | 66 ++++++++ mcu/attiny-hal/src/impl/port.rs | 56 +++++++ mcu/attiny-hal/src/impl/simple_pwm.rs | 233 ++++++++++++++++++++++++++ mcu/attiny-hal/src/impl/spi.rs | 75 +++++++++ mcu/attiny-hal/src/impl/wdt.rs | 34 ++++ mcu/attiny-hal/src/lib.rs | 116 ++----------- mcu/attiny-hal/src/port.rs | 62 ------- mcu/attiny-hal/src/simple_pwm.rs | 217 ------------------------ mcu/attiny-hal/src/spi.rs | 72 -------- mcu/attiny-hal/src/wdt.rs | 44 ----- 21 files changed, 1080 insertions(+), 765 deletions(-) delete mode 100644 mcu/attiny-hal/src/adc.rs create mode 100644 mcu/attiny-hal/src/attiny167.rs create mode 100644 mcu/attiny-hal/src/attiny2313.rs create mode 100644 mcu/attiny-hal/src/attiny84.rs create mode 100644 mcu/attiny-hal/src/attiny85.rs create mode 100644 mcu/attiny-hal/src/attiny88.rs delete mode 100644 mcu/attiny-hal/src/eeprom.rs create mode 100644 mcu/attiny-hal/src/globals.rs create mode 100644 mcu/attiny-hal/src/impl/adc.rs create mode 100644 mcu/attiny-hal/src/impl/eeprom.rs create mode 100644 mcu/attiny-hal/src/impl/mod.rs create mode 100644 mcu/attiny-hal/src/impl/port.rs create mode 100644 mcu/attiny-hal/src/impl/simple_pwm.rs create mode 100644 mcu/attiny-hal/src/impl/spi.rs create mode 100644 mcu/attiny-hal/src/impl/wdt.rs delete mode 100644 mcu/attiny-hal/src/port.rs delete mode 100644 mcu/attiny-hal/src/simple_pwm.rs delete mode 100644 mcu/attiny-hal/src/spi.rs delete mode 100644 mcu/attiny-hal/src/wdt.rs diff --git a/mcu/attiny-hal/Cargo.toml b/mcu/attiny-hal/Cargo.toml index 737224607d..120e8ccdc2 100644 --- a/mcu/attiny-hal/Cargo.toml +++ b/mcu/attiny-hal/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "attiny-hal" -version = "0.1.0" +version = "0.2.0" authors = ["Rahix "] edition = "2021" @@ -13,10 +13,10 @@ categories = ["no-std", "embedded", "hardware-support"] [features] rt = ["avr-device/rt"] device-selected = [] -attiny84 = ["avr-device/attiny84", "device-selected"] -attiny85 = ["avr-device/attiny85", "device-selected"] -attiny88 = ["avr-device/attiny88", "device-selected"] -attiny167 = ["avr-device/attiny167", "device-selected"] +attiny84 = ["avr-device/attiny84", "device-selected", "_peripheral-simple-pwm"] +attiny85 = ["avr-device/attiny85", "device-selected", "_peripheral-adc", "_peripheral-simple-pwm"] +attiny88 = ["avr-device/attiny88", "device-selected", "_peripheral-adc", "_peripheral-spi", "_peripheral-simple-pwm"] +attiny167 = ["avr-device/attiny167", "device-selected", "_peripheral-adc", "_peripheral-spi"] attiny2313 = ["avr-device/attiny2313", "device-selected"] critical-section-impl = ["avr-device/critical-section-impl"] @@ -27,6 +27,10 @@ disable-device-selection-error = [] # We must select a microcontroller to build on docs.rs docsrs = ["attiny85"] +_peripheral-adc = [] +_peripheral-spi = [] +_peripheral-simple-pwm = [] + [dependencies] avr-hal-generic = { path = "../../avr-hal-generic/" } diff --git a/mcu/attiny-hal/src/adc.rs b/mcu/attiny-hal/src/adc.rs deleted file mode 100644 index 0856312086..0000000000 --- a/mcu/attiny-hal/src/adc.rs +++ /dev/null @@ -1,209 +0,0 @@ -#![allow(non_camel_case_types)] -//! 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) -//! -//! ``` -//! let dp = attiny_hal::Peripherals::take().unwrap(); -//! let pins = attiny_hal::pins!(dp); -//! -//! let mut adc = Adc::new(dp.ADC, Default::default()); -//! -//! 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(), -//! ]; -//! -//! for (index, channel) in channels.iter().enumerate() { -//! let value = adc.read_blocking(channel); -//! ufmt::uwrite!(&mut serial, "CH{}: {} ", index, value).unwrap(); -//! } -//! ``` - -use crate::port; -pub use avr_hal_generic::adc::{AdcChannel, AdcOps, ClockDivider}; - -/// Select the voltage reference for the ADC peripheral -/// -/// The internal voltage reference options may not be used if an external reference voltage is -/// being applied to the AREF pin. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -#[repr(u8)] -pub enum ReferenceVoltage { - /// Voltage applied to AREF pin. - #[cfg(any(feature = "attiny85", feature = "attiny167",))] - Aref, - /// Default reference voltage (default). - AVcc, - /// Internal 1.1V reference. - Internal1_1, - /// Internal 2.56V reference. - #[cfg(any(feature = "attiny85", feature = "attiny167",))] - Internal2_56, -} - -impl Default for ReferenceVoltage { - fn default() -> Self { - Self::AVcc - } -} - -/// Configuration for the ADC peripheral. -#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)] -pub struct AdcSettings { - pub clock_divider: ClockDivider, - pub ref_voltage: ReferenceVoltage, -} - -/// Check the [`avr_hal_generic::adc::Adc`] documentation. -pub type Adc = avr_hal_generic::adc::Adc; - -/// Check the [`avr_hal_generic::adc::Channel`] documentation. -pub type Channel = avr_hal_generic::adc::Channel; - -/// Additional channels -/// -/// 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); -/// ``` -pub mod channel { - #[cfg(feature = "attiny167")] - pub struct AVcc_4; - pub struct Vbg; - pub struct Gnd; - pub struct Temperature; -} - -fn apply_clock(peripheral: &crate::pac::ADC, settings: AdcSettings) { - peripheral.adcsra.write(|w| { - w.aden().set_bit(); - match settings.clock_divider { - ClockDivider::Factor2 => w.adps().prescaler_2(), - ClockDivider::Factor4 => w.adps().prescaler_4(), - ClockDivider::Factor8 => w.adps().prescaler_8(), - ClockDivider::Factor16 => w.adps().prescaler_16(), - ClockDivider::Factor32 => w.adps().prescaler_32(), - ClockDivider::Factor64 => w.adps().prescaler_64(), - ClockDivider::Factor128 => w.adps().prescaler_128(), - } - }); -} - -#[cfg(feature = "attiny85")] -avr_hal_generic::impl_adc! { - hal: crate::Attiny, - peripheral: crate::pac::ADC, - settings: AdcSettings, - apply_settings: |peripheral, settings| { - apply_clock(peripheral, settings); - peripheral.admux.write(|w| match settings.ref_voltage { - ReferenceVoltage::Aref => w.refs().aref(), - ReferenceVoltage::AVcc => w.refs().vcc(), - ReferenceVoltage::Internal1_1 => w.refs().internal().refs2().clear_bit(), - ReferenceVoltage::Internal2_56 => w.refs().internal().refs2().set_bit(), - }); - }, - channel_id: crate::pac::adc::admux::MUX_A, - set_channel: |peripheral, id| { - peripheral.admux.modify(|_, w| w.mux().variant(id)); - }, - pins: { - port::PB5: (crate::pac::adc::admux::MUX_A::ADC0, didr0::adc0d), - port::PB2: (crate::pac::adc::admux::MUX_A::ADC1, didr0::adc1d), - port::PB4: (crate::pac::adc::admux::MUX_A::ADC2, didr0::adc2d), - port::PB3: (crate::pac::adc::admux::MUX_A::ADC3, didr0::adc3d), - }, - channels: { - channel::Vbg: crate::pac::adc::admux::MUX_A::ADC_VBG, - channel::Gnd: crate::pac::adc::admux::MUX_A::ADC_GND, - channel::Temperature: crate::pac::adc::admux::MUX_A::TEMPSENS, - }, -} - -#[cfg(feature = "attiny88")] -avr_hal_generic::impl_adc! { - hal: crate::Attiny, - peripheral: crate::pac::ADC, - settings: AdcSettings, - apply_settings: |peripheral, settings| { - apply_clock(peripheral, settings); - peripheral.admux.write(|w| match settings.ref_voltage { - ReferenceVoltage::AVcc => w.refs0().avcc(), - ReferenceVoltage::Internal1_1 => w.refs0().internal(), - }); - }, - channel_id: crate::pac::adc::admux::MUX_A, - set_channel: |peripheral, id| { - peripheral.admux.modify(|_, w| w.mux().variant(id)); - }, - pins: { - port::PC0: (crate::pac::adc::admux::MUX_A::ADC0, didr0::adc0d), - port::PC1: (crate::pac::adc::admux::MUX_A::ADC1, didr0::adc1d), - port::PC2: (crate::pac::adc::admux::MUX_A::ADC2, didr0::adc2d), - port::PC3: (crate::pac::adc::admux::MUX_A::ADC3, didr0::adc3d), - port::PC4: (crate::pac::adc::admux::MUX_A::ADC4, didr0::adc4d), - port::PC5: (crate::pac::adc::admux::MUX_A::ADC5, didr0::adc5d), - port::PA0: (crate::pac::adc::admux::MUX_A::ADC6, didr0::adc6d), - port::PA1: (crate::pac::adc::admux::MUX_A::ADC7, didr0::adc7d), - }, - channels: { - channel::Vbg: crate::pac::adc::admux::MUX_A::ADC_VBG, - channel::Gnd: crate::pac::adc::admux::MUX_A::ADC_GND, - channel::Temperature: crate::pac::adc::admux::MUX_A::TEMPSENS, - }, -} - -#[cfg(feature = "attiny167")] -avr_hal_generic::impl_adc! { - hal: crate::Attiny, - peripheral: crate::pac::ADC, - settings: AdcSettings, - apply_settings: |peripheral, settings| { - apply_clock(peripheral, settings); - peripheral.amiscr.write(|w| match settings.ref_voltage { - ReferenceVoltage::Aref => w.arefen().set_bit(), - _ => w.arefen().clear_bit(), - }); - peripheral.admux.write(|w| match settings.ref_voltage { - ReferenceVoltage::Aref => w.refs().avcc(), - ReferenceVoltage::AVcc => w.refs().avcc(), - ReferenceVoltage::Internal1_1 => w.refs().internal_11(), - ReferenceVoltage::Internal2_56 => w.refs().internal_256(), - }); - }, - channel_id: crate::pac::adc::admux::MUX_A, - set_channel: |peripheral, id| { - peripheral.admux.modify(|_, w| w.mux().variant(id)); - }, - pins: { - port::PA0: (crate::pac::adc::admux::MUX_A::ADC0, didr0::adc0d), - port::PA1: (crate::pac::adc::admux::MUX_A::ADC1, didr0::adc1d), - port::PA2: (crate::pac::adc::admux::MUX_A::ADC2, didr0::adc2d), - port::PA3: (crate::pac::adc::admux::MUX_A::ADC3, didr0::adc3d), - port::PA4: (crate::pac::adc::admux::MUX_A::ADC4, didr0::adc4d), - port::PA5: (crate::pac::adc::admux::MUX_A::ADC5, didr0::adc5d), - port::PA6: (crate::pac::adc::admux::MUX_A::ADC6, didr0::adc6d), - port::PA7: (crate::pac::adc::admux::MUX_A::ADC7, didr0::adc7d), - port::PB5: (crate::pac::adc::admux::MUX_A::ADC8, didr1::adc8d), - port::PB6: (crate::pac::adc::admux::MUX_A::ADC9, didr1::adc9d), - port::PB7: (crate::pac::adc::admux::MUX_A::ADC10, didr1::adc10d), - }, - channels: { - channel::AVcc_4: crate::pac::adc::admux::MUX_A::ADC_AVCC_4, - channel::Vbg: crate::pac::adc::admux::MUX_A::ADC_VBG, - channel::Gnd: crate::pac::adc::admux::MUX_A::ADC_GND, - channel::Temperature: crate::pac::adc::admux::MUX_A::TEMPSENS, - }, -} diff --git a/mcu/attiny-hal/src/attiny167.rs b/mcu/attiny-hal/src/attiny167.rs new file mode 100644 index 0000000000..db673eca16 --- /dev/null +++ b/mcu/attiny-hal/src/attiny167.rs @@ -0,0 +1,75 @@ +use crate::r#impl::avr_hal; + +avr_hal! { + device: attiny167, + eeprom: { + capacity: 512, + addr_width: u16, + addr_reg: eear, + }, + port: { + ports: { + A: [0, 1, 2, 3, 4, 5, 6, 7], + B: [0, 1, 2, 3, 4, 5, 6, 7], + }, + impl!: avr_hal_generic::impl_port_traditional, + }, + spi: { + interfaces: { + Spi: { + peripheral: SPI, + sclk: PA5, + mosi: PA4, + miso: PA2, + cs: PA6, + impl!: avr_hal_generic::impl_spi, + }, + }, + }, + adc: { + references: { + /// Voltage applied to AREF pin. + Aref: |peripheral| { + peripheral.amiscr.write(|w| w.arefen().set_bit()); + peripheral.admux.write(|w| w.refs().avcc()); + }, + /// Default reference voltage (default). + AVcc: |peripheral| { + peripheral.amiscr.write(|w| w.arefen().clear_bit()); + peripheral.admux.write(|w| w.refs().avcc()); + }, + /// Internal 1.1V reference. + Internal1_1: |peripheral| { + peripheral.amiscr.write(|w| w.arefen().clear_bit()); + peripheral.admux.write(|w| w.refs().internal_11()); + }, + /// Internal 2.56V reference. + Internal2_56: |peripheral| { + peripheral.amiscr.write(|w| w.arefen().clear_bit()); + peripheral.admux.write(|w| w.refs().internal_256()); + }, + }, + pins: { + PA0: (hal::pac::adc::admux::MUX_A::ADC0, didr0::adc0d), + PA1: (hal::pac::adc::admux::MUX_A::ADC1, didr0::adc1d), + PA2: (hal::pac::adc::admux::MUX_A::ADC2, didr0::adc2d), + PA3: (hal::pac::adc::admux::MUX_A::ADC3, didr0::adc3d), + PA4: (hal::pac::adc::admux::MUX_A::ADC4, didr0::adc4d), + PA5: (hal::pac::adc::admux::MUX_A::ADC5, didr0::adc5d), + PA6: (hal::pac::adc::admux::MUX_A::ADC6, didr0::adc6d), + PA7: (hal::pac::adc::admux::MUX_A::ADC7, didr0::adc7d), + PB5: (hal::pac::adc::admux::MUX_A::ADC8, didr1::adc8d), + PB6: (hal::pac::adc::admux::MUX_A::ADC9, didr1::adc9d), + PB7: (hal::pac::adc::admux::MUX_A::ADC10, didr1::adc10d), + }, + channels: { + AVcc_4: hal::pac::adc::admux::MUX_A::ADC_AVCC_4, + Vbg: hal::pac::adc::admux::MUX_A::ADC_VBG, + Gnd: hal::pac::adc::admux::MUX_A::ADC_GND, + Temperature: hal::pac::adc::admux::MUX_A::TEMPSENS, + }, + }, + wdt: { + wdtcsr_name: wdtcr, + }, +} diff --git a/mcu/attiny-hal/src/attiny2313.rs b/mcu/attiny-hal/src/attiny2313.rs new file mode 100644 index 0000000000..62f587863e --- /dev/null +++ b/mcu/attiny-hal/src/attiny2313.rs @@ -0,0 +1,21 @@ +use crate::r#impl::avr_hal; + +avr_hal! { + device: attiny2313, + eeprom: { + capacity: 128, + addr_width: u8, + addr_reg: eear, + }, + port: { + ports: { + A: [0, 1, 2], + B: [0, 1, 2, 3, 4, 5, 6, 7], + D: [0, 1, 2, 3, 4, 5, 6], + }, + impl!: avr_hal_generic::impl_port_traditional, + }, + wdt: { + wdtcsr_name: wdtcr, + }, +} diff --git a/mcu/attiny-hal/src/attiny84.rs b/mcu/attiny-hal/src/attiny84.rs new file mode 100644 index 0000000000..d82fb948ce --- /dev/null +++ b/mcu/attiny-hal/src/attiny84.rs @@ -0,0 +1,54 @@ +use crate::r#impl::avr_hal; + +avr_hal! { + device: attiny84, + eeprom: { + capacity: 512, + addr_width: u16, + addr_reg: eear, + }, + port: { + ports: { + A: [0, 1, 2, 3, 4, 5, 6, 7], + B: [0, 1, 2, 3], + }, + impl!: avr_hal_generic::impl_port_traditional, + }, + pwm: { + timers: { + Timer0Pwm: { + peripheral: TC0, + tccr: tccr0, + pins: { + PB2: { + ocr: ocr0a, + com: com0a, + }, + PA7: { + ocr: ocr0b, + com: com0b, + }, + }, + impl!: crate::r#impl::timer_8bit_impl, + }, + Timer1Pwm: { + peripheral: TC1, + tccr: tccr1, + pins: { + PA6: { + ocr: ocr1a, + com: com1a, + }, + PA5: { + ocr: ocr1b, + com: com1b, + }, + }, + impl!: crate::r#impl::timer_16bit_impl, + }, + }, + }, + wdt: { + wdtcsr_name: wdtcsr, + }, +} diff --git a/mcu/attiny-hal/src/attiny85.rs b/mcu/attiny-hal/src/attiny85.rs new file mode 100644 index 0000000000..d0c0103dfa --- /dev/null +++ b/mcu/attiny-hal/src/attiny85.rs @@ -0,0 +1,80 @@ +use crate::r#impl::avr_hal; + +avr_hal! { + device: attiny85, + eeprom: { + capacity: 512, + addr_width: u16, + addr_reg: eear, + }, + port: { + ports: { + B: [0, 1, 2, 3, 4, 5], + }, + impl!: avr_hal_generic::impl_port_traditional, + }, + pwm: { + timers: { + Timer0Pwm: { + peripheral: TC0, + tccr: tccr0, + pins: { + PB0: { + ocr: ocr0a, + com: com0a, + }, + PB1: { + ocr: ocr0b, + com: com0b, + }, + }, + impl!: crate::r#impl::timer_8bit_impl, + }, + Timer1Pwm: { + peripheral: TC1, + tccr: tccr1, + pins: { + PB4: { + ocr: ocr1b, + com: com1b, + }, + }, + impl!: crate::r#impl::timer_8bit_separate_prescale, + }, + }, + }, + adc: { + references: { + /// Voltage applied to AREF pin. + Aref: |peripheral| { + peripheral.admux.write(|w| w.refs().aref()) + }, + /// Default reference voltage (default). + AVcc: |peripheral| { + peripheral.admux.write(|w| w.refs().vcc()) + }, + /// Internal 1.1V reference. + Internal1_1: |peripheral| { + peripheral.admux.write(|w| w.refs().internal().refs2().clear_bit()) + }, + /// Internal 2.56V reference. + Internal2_56: |peripheral| { + peripheral.admux.write(|w| w.refs().internal().refs2().set_bit()) + }, + }, + pins: { + PB5: (hal::pac::adc::admux::MUX_A::ADC0, didr0::adc0d), + PB2: (hal::pac::adc::admux::MUX_A::ADC1, didr0::adc1d), + PB4: (hal::pac::adc::admux::MUX_A::ADC2, didr0::adc2d), + PB3: (hal::pac::adc::admux::MUX_A::ADC3, didr0::adc3d), + }, + channels: { + Vbg: hal::pac::adc::admux::MUX_A::ADC_VBG, + Gnd: hal::pac::adc::admux::MUX_A::ADC_GND, + Temperature: hal::pac::adc::admux::MUX_A::TEMPSENS, + }, + }, + wdt: { + wdtcsr_name: wdtcr, + }, +} diff --git a/mcu/attiny-hal/src/attiny88.rs b/mcu/attiny-hal/src/attiny88.rs new file mode 100644 index 0000000000..39a061fb41 --- /dev/null +++ b/mcu/attiny-hal/src/attiny88.rs @@ -0,0 +1,80 @@ +use crate::r#impl::avr_hal; + +avr_hal! { + device: attiny88, + eeprom: { + capacity: 64, + addr_width: u8, + addr_reg: eearl, + }, + port: { + ports: { + A: [0, 1, 2, 3], + B: [0, 1, 2, 3, 4, 5, 6, 7], + C: [0, 1, 2, 3, 4, 5, 6, 7], + D: [0, 1, 2, 3, 4, 5, 6, 7], + }, + impl!: avr_hal_generic::impl_port_traditional, + }, + pwm: { + timers: { + Timer1Pwm: { + peripheral: TC1, + tccr: tccr1, + pins: { + PB1: { + ocr: ocr1a, + com: com1a, + }, + PB2: { + ocr: ocr1b, + com: com1b, + }, + }, + impl!: crate::r#impl::timer_16bit_impl, + }, + }, + }, + spi: { + interfaces: { + Spi: { + peripheral: SPI, + sclk: PB5, + mosi: PB3, + miso: PB4, + cs: PB2, + impl!: avr_hal_generic::impl_spi, + }, + }, + }, + adc: { + references: { + /// Default reference voltage (default). + AVcc: |peripheral| { + peripheral.admux.write(|w| w.refs0().avcc()) + }, + /// Internal 1.1V reference. + Internal1_1: |peripheral| { + peripheral.admux.write(|w| w.refs0().internal()) + }, + }, + pins: { + PC0: (hal::pac::adc::admux::MUX_A::ADC0, didr0::adc0d), + PC1: (hal::pac::adc::admux::MUX_A::ADC1, didr0::adc1d), + PC2: (hal::pac::adc::admux::MUX_A::ADC2, didr0::adc2d), + PC3: (hal::pac::adc::admux::MUX_A::ADC3, didr0::adc3d), + PC4: (hal::pac::adc::admux::MUX_A::ADC4, didr0::adc4d), + PC5: (hal::pac::adc::admux::MUX_A::ADC5, didr0::adc5d), + PA0: (hal::pac::adc::admux::MUX_A::ADC6, didr0::adc6d), + PA1: (hal::pac::adc::admux::MUX_A::ADC7, didr0::adc7d), + }, + channels: { + Vbg: hal::pac::adc::admux::MUX_A::ADC_VBG, + Gnd: hal::pac::adc::admux::MUX_A::ADC_GND, + Temperature: hal::pac::adc::admux::MUX_A::TEMPSENS, + }, + }, + wdt: { + wdtcsr_name: wdtcsr, + }, +} diff --git a/mcu/attiny-hal/src/eeprom.rs b/mcu/attiny-hal/src/eeprom.rs deleted file mode 100644 index f325215dfb..0000000000 --- a/mcu/attiny-hal/src/eeprom.rs +++ /dev/null @@ -1,56 +0,0 @@ -//! EEPROM -//! -//! # Example -//! -//! 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) -//! -//! ``` -//! const BOOT_COUNT_OFFSET: u16 = 0; -//! -//! let dp = attiny_hal::Peripherals::take().unwrap(); -//! let mut eeprom = 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}; - -pub type Eeprom = avr_hal_generic::eeprom::Eeprom; - -#[cfg(feature = "attiny2313")] -avr_hal_generic::impl_eeprom_attiny! { - hal: crate::Attiny, - peripheral: crate::pac::EEPROM, - capacity: 128, - addr_width: u8, - set_address: |peripheral, address| { - peripheral.eear.write(|w| w.bits(address)); - }, -} - -#[cfg(any(feature = "attiny167", feature = "attiny85"))] -avr_hal_generic::impl_eeprom_attiny! { - hal: crate::Attiny, - peripheral: crate::pac::EEPROM, - capacity: 512, - addr_width: u16, - set_address: |peripheral, address| { - peripheral.eear.write(|w| w.bits(address)); - }, -} - -#[cfg(feature = "attiny88")] -avr_hal_generic::impl_eeprom_attiny! { - hal: crate::Attiny, - peripheral: crate::pac::EEPROM, - capacity: 64, - addr_width: u8, - set_address: |peripheral, address| { - peripheral.eearl.write(|w| w.bits(address)); - }, -} diff --git a/mcu/attiny-hal/src/globals.rs b/mcu/attiny-hal/src/globals.rs new file mode 100644 index 0000000000..506ed9fa1d --- /dev/null +++ b/mcu/attiny-hal/src/globals.rs @@ -0,0 +1,84 @@ +// Deprecated globals + +#[cfg(all( + // More than one MCU selected -> error + all( + feature = "attiny167", + any( + feature = "attiny2313", + feature = "attiny84", + feature = "attiny85", + feature = "attiny88", + ) + ), + all( + feature = "attiny2313", + any( + feature = "attiny167", + feature = "attiny84", + feature = "attiny85", + feature = "attiny88", + ) + ), + all( + feature = "attiny84", + any( + feature = "attiny167", + feature = "attiny2313", + feature = "attiny85", + feature = "attiny88", + ) + ), + all( + feature = "attiny85", + any( + feature = "attiny167", + feature = "attiny2313", + feature = "attiny84", + feature = "attiny88", + ) + ), + all( + feature = "attiny88", + any( + feature = "attiny167", + feature = "attiny2313", + feature = "attiny84", + feature = "attiny85", + ) + ), +))] +compile_error!( + "When using globals, you cannot target multiple chips. Either choose exactly one chip feature, or add the `no-globals` feature." +); + +#[cfg(feature = "attiny167")] +pub use crate::attiny167 as hal; + +#[cfg(feature = "attiny2313")] +pub use crate::attiny2313 as hal; + +#[cfg(feature = "attiny84")] +pub use crate::attiny84 as hal; + +#[cfg(feature = "attiny85")] +pub use crate::attiny85 as hal; + +#[cfg(feature = "attiny88")] +pub use crate::attiny88 as hal; + +pub use hal::{eeprom, pac, pins, port, wdt, Hal as Attiny}; +pub use {eeprom::Eeprom, pac::Peripherals, port::Pins, wdt::Wdt}; + +#[cfg(feature = "_peripheral-simple-pwm")] +pub use hal::simple_pwm; + +#[cfg(feature = "_peripheral-adc")] +pub use crate::adc::Adc; +#[cfg(feature = "_peripheral-adc")] +pub use hal::adc; + +#[cfg(feature = "_peripheral-spi")] +pub use crate::spi::Spi; +#[cfg(feature = "_peripheral-spi")] +pub use hal::spi; diff --git a/mcu/attiny-hal/src/impl/adc.rs b/mcu/attiny-hal/src/impl/adc.rs new file mode 100644 index 0000000000..f244c7cb71 --- /dev/null +++ b/mcu/attiny-hal/src/impl/adc.rs @@ -0,0 +1,149 @@ +macro_rules! impl_mod_adc { + ( + hal: crate::$hal:ident, + references: { + $( + $(#[$reference_meta:meta])* + $reference_name:ident: |$peripheral_var:ident| $apply_reference:block, + )* + }, + pins: { + $($pin_name:ident: ($pin_channel:expr$(, $didr:ident::$didr_method:ident)?),)+ + }, + channels: { + $($channel_name:ident: $channel_mux: expr,)* + }, + ) => { + 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) + //! + //! ``` + //! let dp = attiny_hal::Peripherals::take().unwrap(); + //! let pins = attiny_hal::pins!(dp); + //! + //! let mut adc = Adc::new(dp.ADC, Default::default()); + //! + //! 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(), + //! ]; + //! + //! for (index, channel) in channels.iter().enumerate() { + //! let value = adc.read_blocking(channel); + //! ufmt::uwrite!(&mut serial, "CH{}: {} ", index, value).unwrap(); + //! } + //! ``` + + use avr_hal_generic::paste::paste; + use crate::$hal as hal; + + /// Select the voltage reference for the ADC peripheral + /// + /// The internal voltage reference options may not be used if an external reference voltage is + /// being applied to the AREF pin. + #[derive(Debug, Clone, Copy, PartialEq, Eq)] + #[repr(u8)] + pub enum ReferenceVoltage { + $( + $(#[$reference_meta])* + $reference_name, + )* + } + + /// Additional channels + /// + /// 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 { + $( + pub struct $channel_name; + )* + } + + pub use avr_hal_generic::adc::{AdcChannel, AdcOps, ClockDivider}; + + impl Default for ReferenceVoltage { + fn default() -> Self { + Self::AVcc + } + } + + /// Configuration for the ADC peripheral. + #[derive(Default, Debug, Clone, Copy, PartialEq, Eq)] + pub struct AdcSettings { + pub clock_divider: ClockDivider, + pub ref_voltage: ReferenceVoltage, + } + + /// Check the [`avr_hal_generic::adc::Adc`] documentation. + pub type Adc = + avr_hal_generic::adc::Adc; + + /// Check the [`avr_hal_generic::adc::Channel`] documentation. + pub type Channel = avr_hal_generic::adc::Channel; + + fn apply_clock(peripheral: &crate::$hal::pac::ADC, settings: AdcSettings) { + peripheral.adcsra.write(|w| { + w.aden().set_bit(); + match settings.clock_divider { + ClockDivider::Factor2 => w.adps().prescaler_2(), + ClockDivider::Factor4 => w.adps().prescaler_4(), + ClockDivider::Factor8 => w.adps().prescaler_8(), + ClockDivider::Factor16 => w.adps().prescaler_16(), + ClockDivider::Factor32 => w.adps().prescaler_32(), + ClockDivider::Factor64 => w.adps().prescaler_64(), + ClockDivider::Factor128 => w.adps().prescaler_128(), + } + }); + } + + paste! { + avr_hal_generic::impl_adc! { + hal: crate::$hal::Hal, + peripheral: crate::$hal::pac::ADC, + settings: crate::$hal::adc::AdcSettings, + apply_settings: |peripheral, settings| { + apply_clock(peripheral, settings); + match settings.ref_voltage { + $( + ReferenceVoltage::$reference_name => { + let $peripheral_var = peripheral; + $apply_reference + }, + )* + } + }, + channel_id: crate::$hal::pac::adc::admux::MUX_A, + set_channel: |peripheral, id| { + peripheral.admux.modify(|_, w| w.mux().variant(id)); + }, + pins: { + $(hal::port::[<$pin_name>]: ($pin_channel $(, $didr::$didr_method)?),)* + }, + channels: { + $(channel::$channel_name: $channel_mux,)* + }, + } + } + + } + pub use adc::Adc; + } +} +pub(crate) use impl_mod_adc; diff --git a/mcu/attiny-hal/src/impl/eeprom.rs b/mcu/attiny-hal/src/impl/eeprom.rs new file mode 100644 index 0000000000..c920d4af37 --- /dev/null +++ b/mcu/attiny-hal/src/impl/eeprom.rs @@ -0,0 +1,48 @@ +macro_rules! impl_mod_eeprom { + ( + hal: crate::$hal:ident, + capacity: $capacity:expr, + addr_width: $addr_width:ty, + addr_reg: $addr_reg:ident $(,)? + ) => { + pub mod eeprom { + //! EEPROM + //! + //! # Example + //! + //! 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) + //! + //! ``` + //! const BOOT_COUNT_OFFSET: u16 = 0; + //! + //! let dp = attiny_hal::Peripherals::take().unwrap(); + //! let mut eeprom = 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}; + + pub type Eeprom = + avr_hal_generic::eeprom::Eeprom; + + avr_hal_generic::impl_eeprom_attiny! { + hal: crate::$hal::Hal, + peripheral: crate::$hal::pac::EEPROM, + capacity: $capacity, + addr_width: $addr_width, + set_address: |peripheral, address| { + peripheral.$addr_reg.write(|w| w.bits(address)); + }, + } + } + pub use eeprom::Eeprom; + } +} + +pub(crate) use impl_mod_eeprom; diff --git a/mcu/attiny-hal/src/impl/mod.rs b/mcu/attiny-hal/src/impl/mod.rs new file mode 100644 index 0000000000..90f2d1a852 --- /dev/null +++ b/mcu/attiny-hal/src/impl/mod.rs @@ -0,0 +1,66 @@ +#[cfg(feature = "_peripheral-adc")] +mod adc; +#[cfg(feature = "_peripheral-adc")] +pub(crate) use adc::*; + +mod eeprom; +pub(crate) use eeprom::*; + +mod port; +pub(crate) use port::*; + +#[cfg(feature = "_peripheral-simple-pwm")] +mod simple_pwm; +#[cfg(feature = "_peripheral-simple-pwm")] +pub(crate) use simple_pwm::*; + +#[cfg(feature = "_peripheral-spi")] +mod spi; +#[cfg(feature = "_peripheral-spi")] +pub(crate) use spi::*; + +mod wdt; +pub(crate) use wdt::*; + +macro_rules! avr_hal { + ( + device: $device:ident, + eeprom: { $($eeprom:tt)* }, + port: { $($port:tt)* }, + $(pwm: { $($pwm:tt)* },)? + $(spi: { $($spi:tt)* },)? + $(adc: { $($adc:tt)* },)? + wdt: { $($wdt:tt)* }, + ) => { + pub use avr_device::$device as pac; + pub struct Hal; + use crate::r#impl::*; + + impl_mod_eeprom! { + hal: crate::$device, + $($eeprom)* + } + impl_mod_port! { + hal: crate::$device, + $($port)* + } + $(impl_mod_simple_pwm! { + hal: crate::$device, + $($pwm)* + })? + $(impl_mod_spi! { + hal: crate::$device, + $($spi)* + })? + $(impl_mod_adc! { + hal: crate::$device, + $($adc)* + })? + impl_mod_wdt! { + hal: crate::$device, + $($wdt)* + } + }; +} + +pub(crate) use avr_hal; diff --git a/mcu/attiny-hal/src/impl/port.rs b/mcu/attiny-hal/src/impl/port.rs new file mode 100644 index 0000000000..8bd68859a6 --- /dev/null +++ b/mcu/attiny-hal/src/impl/port.rs @@ -0,0 +1,56 @@ +macro_rules! impl_mod_port { + ( + hal: crate::$hal:ident, + ports: { + $($name:ident: [$($pin:literal),+],)+ + }, + impl!: $($impl_macro:ident)::+ $({ + $($arg_name:ident: $arg_value:expr,)* + })?, + ) => { + pub mod port { + //! Port + //! + //! # Example + //! + //! 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); + //! + //! let mut led = pins.pb2.into_output(); + //! + //! loop { + //! led.toggle(); + //! delay_ms(1000); + //! } + //! ``` + + use avr_hal_generic::paste::paste; + use crate::$hal as hal; + + paste! { + pub use avr_hal_generic::port::{mode, PinMode, PinOps}; + $($impl_macro)::+! { + enum Ports { + $($name: hal::pac::[< PORT $name >] = [$($pin),+],)+ + } + } + + #[macro_export] + macro_rules! [< $hal _pins >] { + ($p:expr) => { + $crate::$hal::port::Pins::new($($p.[< PORT $name >],)+) + } + } + + pub use [< $hal _pins >] as pins; + } + } + pub use {pac::Peripherals, port::Pins, port::pins}; + } +} + +pub(crate) use impl_mod_port; diff --git a/mcu/attiny-hal/src/impl/simple_pwm.rs b/mcu/attiny-hal/src/impl/simple_pwm.rs new file mode 100644 index 0000000000..e3c71bcfc3 --- /dev/null +++ b/mcu/attiny-hal/src/impl/simple_pwm.rs @@ -0,0 +1,233 @@ +macro_rules! impl_mod_simple_pwm { + ( + hal: crate::$hal:ident, + timers: { + $( + $timer:ident: { + peripheral: $peripheral:ident, + tccr: $tccr:ident, + pins: { + $( + $pin:ident: { + ocr: $ocr:ident, + com: $com:ident, + }, + )* + }, + impl!: $($impl_macro:ident)::+, + }, + )* + }, + ) => { + pub mod simple_pwm { + use crate::$hal as hal; + pub use avr_hal_generic::simple_pwm::{IntoPwmPin, Prescaler, PwmPinOps}; + use avr_hal_generic::paste::paste; + + $( + $($impl_macro)::+! { + hal: crate::$hal, + timer: $timer, + peripheral: $peripheral, + tccr: $tccr, + pins: { + $( + $pin: { + ocr: $ocr, + com: $com, + }, + )* + }, + } + )* + } + } +} +pub(crate) use impl_mod_simple_pwm; + +#[allow(unused_macros)] +macro_rules! timer_8bit_impl { + ( + hal: crate::$hal:ident, + timer: $timer:ident, + peripheral: $peripheral:ident, + tccr: $tccr:ident, + pins: { + $( + $pin:ident: { + ocr: $ocr:ident, + com: $com:ident, + }, + )* + }, + ) => { + paste! { + avr_hal_generic::impl_simple_pwm! { + /// Use `$peripheral` for PWM (pins `$pin`,) + /// + /// # Example + /// ``` + /// let mut timer0 = Timer0Pwm::new(dp.$peripheral, Prescaler::Prescale64); + /// + /// let mut d0 = pins.d0.into_output().into_pwm(&mut timer0); + /// let mut d1 = pins.d1.into_output().into_pwm(&mut timer0); + /// + /// d0.set_duty(128); + /// d0.enable(); + /// ``` + pub struct $timer { + timer: crate::$hal::pac::$peripheral, + init: |tim, prescaler| { + tim.[<$tccr a>].modify(|_r, w| w.wgm0().pwm_fast()); + tim.[<$tccr b>].modify(|_r, w| match prescaler { + Prescaler::Direct => w.cs0().direct(), + Prescaler::Prescale8 => w.cs0().prescale_8(), + Prescaler::Prescale64 => w.cs0().prescale_64(), + Prescaler::Prescale256 => w.cs0().prescale_256(), + Prescaler::Prescale1024 => w.cs0().prescale_1024(), + }); + }, + pins: { + $( + hal::port::$pin: { + ocr: $ocr, + into_pwm: |tim| if enable { + tim.[<$tccr a>].modify(|_r, w| w.$com().match_clear()); + } else { + tim.[<$tccr a>].modify(|_r, w| w.$com().disconnected()); + }, + }, + )* + }, + } + } + } + } +} +#[allow(unused_imports)] +pub(crate) use timer_8bit_impl; + +#[allow(unused_macros)] +macro_rules! timer_8bit_separate_prescale { + ( + hal: crate::$hal:ident, + timer: $timer:ident, + peripheral: $peripheral:ident, + tccr: $tccr:ident, + pins: { + $( + $pin:ident: { + ocr: $ocr:ident, + com: $com:ident, + }, + )* + }, + ) => { + paste! { + avr_hal_generic::impl_simple_pwm! { + /// Use `$peripheral` for PWM (pins `$pin`,) + /// + /// # Example + /// ``` + /// let mut timer1 = Timer1Pwm::new(dp.TC1, Prescaler::Prescale64); + /// + /// let mut d4 = pins.d4.into_output().into_pwm(&mut timer1); + /// + /// d4.set_duty(128); + /// d4.enable(); + /// ``` + pub struct $timer { + timer: crate::$hal::pac::$peripheral, + init: |tim, prescaler| { + tim.gtccr.modify(|_, w| w.pwm1b().bit(true)); + + tim.$tccr.modify(|_r, w| match prescaler { + Prescaler::Direct => w.cs1().direct(), + Prescaler::Prescale8 => w.cs1().prescale_8(), + Prescaler::Prescale64 => w.cs1().prescale_64(), + Prescaler::Prescale256 => w.cs1().prescale_256(), + Prescaler::Prescale1024 => w.cs1().prescale_1024(), + }); + }, + pins: { + $( + hal::port::$pin: { + ocr: $ocr, + into_pwm: |tim| if enable { + tim.gtccr.modify(|_r, w| w.$com().bits(0b10)); + } else { + tim.gtccr.modify(|_r, w| w.$com().disconnected()); + }, + }, + )* + }, + } + } + } + } +} +#[allow(unused_imports)] +pub(crate) use timer_8bit_separate_prescale; + +#[allow(unused_macros)] +macro_rules! timer_16bit_impl { + ( + hal: crate::$hal:ident, + timer: $timer:ident, + peripheral: $peripheral:ident, + tccr: $tccr:ident, + pins: { + $( + $pin:ident: { + ocr: $ocr:ident, + com: $com:ident, + }, + )* + }, + ) => { + paste! { + avr_hal_generic::impl_simple_pwm! { + /// Use `$peripheral` for PWM (pins `$pin`,) + /// + /// # Example + /// ``` + /// let mut timer1 = Timer1Pwm::new(dp.TC1, Prescaler::Prescale64); + /// + /// let mut d4 = pins.d4.into_output().into_pwm(&mut timer1); + /// + /// d4.set_duty(128); + /// d4.enable(); + /// ``` + pub struct $timer { + timer: crate::$hal::pac::$peripheral, + init: |tim, prescaler| { + tim.[<$tccr a>].modify(|_, w| w.wgm1().bits(0b01)); + tim.[<$tccr b>].modify(|_, w| w.wgm1().bits(0b01)); + + tim.[<$tccr b>].modify(|_r, w| match prescaler { + Prescaler::Direct => w.cs1().direct(), + Prescaler::Prescale8 => w.cs1().prescale_8(), + Prescaler::Prescale64 => w.cs1().prescale_64(), + Prescaler::Prescale256 => w.cs1().prescale_256(), + Prescaler::Prescale1024 => w.cs1().prescale_1024(), + }); + }, + pins: { + $( + hal::port::$pin: { + ocr: $ocr, + into_pwm: |tim| if enable { + tim.[<$tccr a>].modify(|_r, w| w.$com().bits(0b10)); + } else { + tim.[<$tccr a>].modify(|_r, w| w.$com().disconnected()); + }, + }, + )* + }, + } + } + } + } +} +#[allow(unused_imports)] +pub(crate) use timer_16bit_impl; diff --git a/mcu/attiny-hal/src/impl/spi.rs b/mcu/attiny-hal/src/impl/spi.rs new file mode 100644 index 0000000000..52f098af4d --- /dev/null +++ b/mcu/attiny-hal/src/impl/spi.rs @@ -0,0 +1,75 @@ +macro_rules! impl_mod_spi { + ( + hal: crate::$hal:ident, + interfaces: { + $( + $name:ident: { + peripheral: $peripheral:ident, + sclk: $sclk:ident, + mosi: $mosi:ident, + miso: $miso:ident, + cs: $cs:ident, + impl!: $($impl_macro:ident)::+, + }, + )+ + }, + ) => { + pub mod spi { + //! SPI + //! + //! # Example + //! + //! 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); + //! + //! 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(), + //! ); + //! + //! 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 crate::$hal as hal; + + $( + pub type $name = avr_hal_generic::spi::Spi< + crate::$hal::Hal, + crate::$hal::pac::$peripheral, + hal::port::$sclk, + hal::port::$mosi, + hal::port::$miso, + hal::port::$cs, + >; + + $($impl_macro)::+! { + hal: crate::$hal::Hal, + peripheral: crate::$hal::pac::$peripheral, + sclk: hal::port::$sclk, + mosi: hal::port::$mosi, + miso: hal::port::$miso, + cs: hal::port::$cs, + } + )+ + } + pub use spi::{$($name),+}; + } +} + +pub(crate) use impl_mod_spi; diff --git a/mcu/attiny-hal/src/impl/wdt.rs b/mcu/attiny-hal/src/impl/wdt.rs new file mode 100644 index 0000000000..5790fd7343 --- /dev/null +++ b/mcu/attiny-hal/src/impl/wdt.rs @@ -0,0 +1,34 @@ +macro_rules! impl_mod_wdt { + ( + hal: $($hal:ident)::+, + wdtcsr_name: $wdtcsr_name:ident $(,)? + ) => { + pub mod wdt { + pub use avr_hal_generic::wdt::{Timeout, WdtOps}; + + avr_hal_generic::impl_wdt! { + hal: $($hal)::+::Hal, + peripheral: $($hal)::+::pac::WDT, + mcusr: $($hal)::+::pac::cpu::MCUSR, + wdtcsr_name: $wdtcsr_name, + timeout: |to, w| match to { + Timeout::Ms16 => w.wdpl().cycles_2k_512k(), + Timeout::Ms32 => w.wdpl().cycles_4k_1024k(), + Timeout::Ms64 => w.wdpl().cycles_8k(), + Timeout::Ms125 => w.wdpl().cycles_16k(), + Timeout::Ms250 => w.wdpl().cycles_32k(), + Timeout::Ms500 => w.wdpl().cycles_64k(), + Timeout::Ms1000 => w.wdpl().cycles_128k(), + Timeout::Ms2000 => w.wdpl().cycles_256k(), + Timeout::Ms4000 => w.wdph().set_bit().wdpl().cycles_2k_512k(), + Timeout::Ms8000 => w.wdph().set_bit().wdpl().cycles_4k_1024k(), + }, + } + + pub type Wdt = avr_hal_generic::wdt::Wdt<$($hal)::+::Hal, $($hal)::+::pac::WDT>; + } + pub use wdt::Wdt; + }; +} + +pub(crate) use impl_mod_wdt; diff --git a/mcu/attiny-hal/src/lib.rs b/mcu/attiny-hal/src/lib.rs index a8bf1541e0..4ef2b279c2 100644 --- a/mcu/attiny-hal/src/lib.rs +++ b/mcu/attiny-hal/src/lib.rs @@ -3,28 +3,19 @@ //! `attiny-hal` //! ============= //! Common HAL (hardware abstraction layer) for ATtiny* microcontrollers. -//! -//! **Note**: This version of the documentation was built for -#![cfg_attr(feature = "attiny85", doc = "**ATtiny85**.")] -#![cfg_attr(feature = "attiny88", doc = "**ATtiny88**.")] -#![cfg_attr(feature = "attiny167", doc = "**ATtiny167**.")] -#![cfg_attr(feature = "attiny2313", doc = "**ATtiny2313**.")] -//! This means that only items which are available for this MCU are visible. If you are using -//! a different chip, try building the documentation locally with: -//! -//! ```text -//! cargo doc --features --open -//! ``` +// This crate can be configured in one of two ways: either you specify deprecated-globals and exactly one MCU +// Or you don't specify deprecated globals and at least one MCU #[cfg(all( not(feature = "device-selected"), not(feature = "disable-device-selection-error") ))] compile_error!( - "This crate requires you to specify your target chip as a feature. + "You must specify your target chip as a feature. - Please select one of the following + Please select one of the following: + * attiny84 * attiny85 * attiny88 * attiny167 @@ -32,101 +23,26 @@ compile_error!( " ); -/// Reexport of `attiny167` from `avr-device` -/// -#[cfg(feature = "attiny167")] -pub use avr_device::attiny167 as pac; -/// Reexport of `attiny2313` from `avr-device` -/// -#[cfg(feature = "attiny2313")] -pub use avr_device::attiny2313 as pac; -/// Reexport of `attiny84` from `avr-device` -/// -#[cfg(feature = "attiny84")] -pub use avr_device::attiny84 as pac; -/// Reexport of `attiny85` from `avr-device` -/// -#[cfg(feature = "attiny85")] -pub use avr_device::attiny85 as pac; -/// Reexport of `attiny88` from `avr-device` -/// -#[cfg(feature = "attiny88")] -pub use avr_device::attiny88 as pac; - /// See [`avr_device::entry`](https://docs.rs/avr-device/latest/avr_device/attr.entry.html). #[cfg(feature = "rt")] pub use avr_device::entry; -#[cfg(feature = "device-selected")] -pub use pac::Peripherals; - pub use avr_hal_generic::clock; pub use avr_hal_generic::delay; pub use avr_hal_generic::prelude; -// ATtiny2313 does not have ADC and will not compile with this module -#[cfg(all(feature = "device-selected", not(feature = "attiny2313")))] -pub mod adc; -#[cfg(all(feature = "device-selected", not(feature = "attiny2313")))] -pub use adc::Adc; - -#[cfg(feature = "device-selected")] -pub mod port; -#[cfg(feature = "device-selected")] -pub use port::Pins; - -#[cfg(feature = "device-selected")] -pub mod simple_pwm; - -#[cfg(feature = "device-selected")] -pub mod wdt; -#[cfg(feature = "device-selected")] -pub use wdt::Wdt; - -#[cfg(feature = "device-selected")] -pub mod eeprom; -#[cfg(feature = "device-selected")] -pub use eeprom::Eeprom; - -#[cfg(feature = "device-selected")] -pub mod spi; -#[cfg(feature = "device-selected")] -pub use spi::Spi; - -pub struct Attiny; +pub(crate) mod r#impl; +#[cfg(feature = "attiny167")] +pub mod attiny167; +#[cfg(feature = "attiny2313")] +pub mod attiny2313; #[cfg(feature = "attiny84")] -#[macro_export] -macro_rules! pins { - ($p:expr) => { - $crate::Pins::new($p.PORTA, $p.PORTB) - }; -} +pub mod attiny84; #[cfg(feature = "attiny85")] -#[macro_export] -macro_rules! pins { - ($p:expr) => { - $crate::Pins::new($p.PORTB) - }; -} +pub mod attiny85; #[cfg(feature = "attiny88")] -#[macro_export] -macro_rules! pins { - ($p:expr) => { - $crate::Pins::new($p.PORTA, $p.PORTB, $p.PORTC, $p.PORTD) - }; -} -#[cfg(feature = "attiny167")] -#[macro_export] -macro_rules! pins { - ($p:expr) => { - $crate::Pins::new($p.PORTA, $p.PORTB) - }; -} -#[cfg(feature = "attiny2313")] -#[macro_export] -macro_rules! pins { - ($p:expr) => { - $crate::Pins::new($p.PORTA, $p.PORTB, $p.PORTD) - }; -} +pub mod attiny88; + +mod globals; +pub use globals::*; diff --git a/mcu/attiny-hal/src/port.rs b/mcu/attiny-hal/src/port.rs deleted file mode 100644 index eaa0818ff1..0000000000 --- a/mcu/attiny-hal/src/port.rs +++ /dev/null @@ -1,62 +0,0 @@ -//! Port -//! -//! # Example -//! -//! 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); -//! -//! let mut led = pins.pb2.into_output(); -//! -//! loop { -//! led.toggle(); -//! delay_ms(1000); -//! } -//! ``` - -pub use avr_hal_generic::port::{mode, PinMode, PinOps}; - -#[cfg(feature = "attiny2313")] -avr_hal_generic::impl_port_traditional! { - enum Ports { - A: crate::pac::PORTA = [0, 1, 2], - B: crate::pac::PORTB = [0, 1, 2, 3, 4, 5, 6, 7], - D: crate::pac::PORTD = [0, 1, 2, 3, 4, 5, 6], - } -} - -#[cfg(feature = "attiny167")] -avr_hal_generic::impl_port_traditional! { - enum Ports { - A: crate::pac::PORTA = [0, 1, 2, 3, 4, 5, 6, 7], - B: crate::pac::PORTB = [0, 1, 2, 3, 4, 5, 6, 7], - } -} - -#[cfg(feature = "attiny84")] -avr_hal_generic::impl_port_traditional! { - enum Ports { - A: crate::pac::PORTA = [0, 1, 2, 3, 4, 5, 6, 7], - B: crate::pac::PORTB = [0, 1, 2, 3], - } -} - -#[cfg(feature = "attiny85")] -avr_hal_generic::impl_port_traditional! { - enum Ports { - B: crate::pac::PORTB = [0, 1, 2, 3, 4, 5], - } -} - -#[cfg(feature = "attiny88")] -avr_hal_generic::impl_port_traditional! { - enum Ports { - A: crate::pac::PORTA = [0, 1, 2, 3], - B: crate::pac::PORTB = [0, 1, 2, 3, 4, 5, 6, 7], - C: crate::pac::PORTC = [0, 1, 2, 3, 4, 5, 6, 7], - D: crate::pac::PORTD = [0, 1, 2, 3, 4, 5, 6, 7], - } -} diff --git a/mcu/attiny-hal/src/simple_pwm.rs b/mcu/attiny-hal/src/simple_pwm.rs deleted file mode 100644 index f16a89c9c0..0000000000 --- a/mcu/attiny-hal/src/simple_pwm.rs +++ /dev/null @@ -1,217 +0,0 @@ -pub use avr_hal_generic::simple_pwm::{IntoPwmPin, Prescaler, PwmPinOps}; - -#[cfg(any(feature = "attiny85", feature = "attiny84", feature = "attiny88"))] -use crate::port::*; - -#[cfg(feature = "attiny84")] -avr_hal_generic::impl_simple_pwm! { - /// Use `TC0` for PWM (pins `PB2`, `PA7`) - pub struct Timer0Pwm { - timer: crate::pac::TC0, - init: |tim, prescaler| { - tim.tccr0a.modify(|_r, w| w.wgm0().pwm_fast()); - tim.tccr0b.modify(|_r, w| match prescaler { - Prescaler::Direct => w.cs0().direct(), - Prescaler::Prescale8 => w.cs0().prescale_8(), - Prescaler::Prescale64 => w.cs0().prescale_64(), - Prescaler::Prescale256 => w.cs0().prescale_256(), - Prescaler::Prescale1024 => w.cs0().prescale_1024(), - }); - }, - pins: { - PB2: { - ocr: ocr0a, - into_pwm: |tim| if enable { - tim.tccr0a.modify(|_r, w| w.com0a().match_clear()); - } else { - tim.tccr0a.modify(|_r, w| w.com0a().disconnected()); - }, - }, - - PA7: { - ocr: ocr0b, - into_pwm: |tim| if enable { - tim.tccr0a.modify(|_r, w| w.com0b().match_clear()); - } else { - tim.tccr0a.modify(|_r, w| w.com0b().disconnected()); - }, - }, - }, - } -} - -#[cfg(feature = "attiny84")] -avr_hal_generic::impl_simple_pwm! { - /// Use `TC1` for PWM (pins `PA6`, 'PA5') - pub struct Timer1Pwm { - timer: crate::pac::TC1, - init: |tim, prescaler| { - tim.tccr1a.modify(|_, w| w.wgm1().bits(0b01)); - tim.tccr1b.modify(|_, w| w.wgm1().bits(0b01)); - - tim.tccr1b.modify(|_r, w| match prescaler { - Prescaler::Direct => w.cs1().direct(), - Prescaler::Prescale8 => w.cs1().prescale_8(), - Prescaler::Prescale64 => w.cs1().prescale_64(), - Prescaler::Prescale256 => w.cs1().prescale_256(), - Prescaler::Prescale1024 => w.cs1().prescale_1024(), - }); - }, - pins: { - PA6: { - ocr: ocr1a, - into_pwm: |tim| if enable { - tim.tccr1a.modify(|_, w| w.com1a().bits(0b10)); - } else { - tim.tccr1a.modify(|_, w| w.com1a().disconnected()); - }, - }, - - PA5: { - ocr: ocr1b, - into_pwm: |tim| if enable { - tim.tccr1a.modify(|_, w| w.com1b().bits(0b10)); - } else { - tim.tccr1a.modify(|_, w| w.com1b().disconnected()); - }, - }, - }, - } -} - -#[cfg(feature = "attiny85")] -avr_hal_generic::impl_simple_pwm! { - /// Use `TC0` for PWM (pins `PB0`, `PB1`) - /// - /// # Example - /// ``` - /// 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); - /// - /// d0.set_duty(128); - /// d0.enable(); - /// ``` - pub struct Timer0Pwm { - timer: crate::pac::TC0, - init: |tim, prescaler| { - tim.tccr0a.modify(|_r, w| w.wgm0().pwm_fast()); - tim.tccr0b.modify(|_r, w| match prescaler { - Prescaler::Direct => w.cs0().direct(), - Prescaler::Prescale8 => w.cs0().prescale_8(), - Prescaler::Prescale64 => w.cs0().prescale_64(), - Prescaler::Prescale256 => w.cs0().prescale_256(), - Prescaler::Prescale1024 => w.cs0().prescale_1024(), - }); - }, - pins: { - PB0: { - ocr: ocr0a, - into_pwm: |tim| if enable { - tim.tccr0a.modify(|_r, w| w.com0a().match_clear()); - } else { - tim.tccr0a.modify(|_r, w| w.com0a().disconnected()); - }, - }, - - PB1: { - ocr: ocr0b, - into_pwm: |tim| if enable { - tim.tccr0a.modify(|_r, w| w.com0b().match_clear()); - } else { - tim.tccr0a.modify(|_r, w| w.com0b().disconnected()); - }, - }, - }, - } -} - -#[cfg(feature = "attiny85")] -avr_hal_generic::impl_simple_pwm! { - /// Use `TC1` for PWM (pins `PB4`) - /// - /// # Example - /// ``` - /// let mut timer1 = Timer1Pwm::new(dp.TC1, Prescaler::Prescale64); - /// - /// let mut d4 = pins.d4.into_output().into_pwm(&mut timer1); - /// - /// d4.set_duty(128); - /// d4.enable(); - /// ``` - pub struct Timer1Pwm { - timer: crate::pac::TC1, - init: |tim, prescaler| { - tim.gtccr.modify(|_, w| w.pwm1b().bit(true)); - - tim.tccr1.modify(|_r, w| match prescaler { - Prescaler::Direct => w.cs1().direct(), - Prescaler::Prescale8 => w.cs1().prescale_8(), - Prescaler::Prescale64 => w.cs1().prescale_64(), - Prescaler::Prescale256 => w.cs1().prescale_256(), - Prescaler::Prescale1024 => w.cs1().prescale_1024(), - }); - }, - pins: { - PB4: { - ocr: ocr1b, - into_pwm: |tim| if enable { - tim.gtccr.modify(|_, w| w.com1b().bits(0b10)); - } else { - tim.gtccr.modify(|_, w| w.com1b().disconnected()); - }, - }, - }, - } -} - -#[cfg(feature = "attiny88")] -avr_hal_generic::impl_simple_pwm! { - /// Use `TC1` for PWM (pins `PB1`, 'PB2') - /// - /// # Example - /// ``` - /// 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); - /// - /// d9.set_duty(128); - /// d9.enable(); - /// ``` - pub struct Timer1Pwm { - timer: crate::pac::TC1, - init: |tim, prescaler| { - tim.tccr1a.modify(|_, w| w.wgm1().bits(0b01)); - tim.tccr1b.modify(|_, w| w.wgm1().bits(0b01)); - - tim.tccr1b.modify(|_r, w| match prescaler { - Prescaler::Direct => w.cs1().direct(), - Prescaler::Prescale8 => w.cs1().prescale_8(), - Prescaler::Prescale64 => w.cs1().prescale_64(), - Prescaler::Prescale256 => w.cs1().prescale_256(), - Prescaler::Prescale1024 => w.cs1().prescale_1024(), - }); - }, - pins: { - PB1: { - ocr: ocr1a, - into_pwm: |tim| if enable { - tim.tccr1a.modify(|_, w| w.com1a().bits(0b10)); - } else { - tim.tccr1a.modify(|_, w| w.com1a().disconnected()); - }, - }, - - PB2: { - ocr: ocr1b, - into_pwm: |tim| if enable { - tim.tccr1a.modify(|_, w| w.com1b().bits(0b10)); - } else { - tim.tccr1a.modify(|_, w| w.com1b().disconnected()); - }, - }, - }, - } -} diff --git a/mcu/attiny-hal/src/spi.rs b/mcu/attiny-hal/src/spi.rs deleted file mode 100644 index 65ae277263..0000000000 --- a/mcu/attiny-hal/src/spi.rs +++ /dev/null @@ -1,72 +0,0 @@ -//! SPI -//! -//! # Example -//! -//! 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); -//! -//! 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(), -//! ); -//! -//! 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(); -//! ``` - - -#[allow(unused_imports)] -use crate::port; -pub use avr_hal_generic::spi::*; - -#[cfg(feature = "attiny88")] -pub type Spi = avr_hal_generic::spi::Spi< - crate::Attiny, - crate::pac::SPI, - port::PB5, - port::PB3, - port::PB4, - port::PB2, ->; -#[cfg(feature = "attiny88")] -avr_hal_generic::impl_spi! { - hal: crate::Attiny, - peripheral: crate::pac::SPI, - sclk: port::PB5, - mosi: port::PB3, - miso: port::PB4, - cs: port::PB2, -} - -#[cfg(feature = "attiny167")] -pub type Spi = avr_hal_generic::spi::Spi< - crate::Attiny, - crate::pac::SPI, - port::PA5, - port::PA4, - port::PA2, - port::PA6, ->; -#[cfg(feature = "attiny167")] -avr_hal_generic::impl_spi! { - hal: crate::Attiny, - peripheral: crate::pac::SPI, - sclk: port::PA5, - mosi: port::PA4, - miso: port::PA2, - cs: port::PA6, -} diff --git a/mcu/attiny-hal/src/wdt.rs b/mcu/attiny-hal/src/wdt.rs deleted file mode 100644 index 1693051483..0000000000 --- a/mcu/attiny-hal/src/wdt.rs +++ /dev/null @@ -1,44 +0,0 @@ -#[allow(unused_imports)] -pub use avr_hal_generic::wdt::{Timeout, WdtOps}; - -pub type Wdt = avr_hal_generic::wdt::Wdt; - -#[cfg(any(feature = "attiny85", feature = "attiny167", feature = "attiny2313"))] -avr_hal_generic::impl_wdt! { - hal: crate::Attiny, - peripheral: crate::pac::WDT, - mcusr: crate::pac::cpu::MCUSR, - wdtcsr_name: wdtcr, - timeout: |to, w| match to { - Timeout::Ms16 => w.wdpl().cycles_2k_512k(), - Timeout::Ms32 => w.wdpl().cycles_4k_1024k(), - Timeout::Ms64 => w.wdpl().cycles_8k(), - Timeout::Ms125 => w.wdpl().cycles_16k(), - Timeout::Ms250 => w.wdpl().cycles_32k(), - Timeout::Ms500 => w.wdpl().cycles_64k(), - Timeout::Ms1000 => w.wdpl().cycles_128k(), - Timeout::Ms2000 => w.wdpl().cycles_256k(), - Timeout::Ms4000 => w.wdph().set_bit().wdpl().cycles_2k_512k(), - Timeout::Ms8000 => w.wdph().set_bit().wdpl().cycles_4k_1024k(), - }, -} - -#[cfg(any(feature = "attiny84", feature = "attiny88"))] -avr_hal_generic::impl_wdt! { - hal: crate::Attiny, - peripheral: crate::pac::WDT, - mcusr: crate::pac::cpu::MCUSR, - wdtcsr_name: wdtcsr, - timeout: |to, w| match to { - Timeout::Ms16 => w.wdpl().cycles_2k_512k(), - Timeout::Ms32 => w.wdpl().cycles_4k_1024k(), - Timeout::Ms64 => w.wdpl().cycles_8k(), - Timeout::Ms125 => w.wdpl().cycles_16k(), - Timeout::Ms250 => w.wdpl().cycles_32k(), - Timeout::Ms500 => w.wdpl().cycles_64k(), - Timeout::Ms1000 => w.wdpl().cycles_128k(), - Timeout::Ms2000 => w.wdpl().cycles_256k(), - Timeout::Ms4000 => w.wdph().set_bit().wdpl().cycles_2k_512k(), - Timeout::Ms8000 => w.wdph().set_bit().wdpl().cycles_4k_1024k(), - }, -}