diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 507335a20c..63fc8e6511 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -119,6 +119,11 @@ jobs: - name: Test-compile HAL crate for an MCU if: "${{ matrix.m.type == 'mcu' }}" run: cd "mcu/${{ matrix.m.crate }}" && cargo build --features "${{ matrix.m.name }}" -Z build-std=core --target "../../avr-specs/avr-${{ matrix.m.spec }}.json" + - name: Test-compile HAL crate for an MCU (no deprecated globals) + if: "${{ matrix.m.crate == 'attiny-hal' }}" + 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" ravedude: name: "ravedude" 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, diff --git a/mcu/attiny-hal/Cargo.toml b/mcu/attiny-hal/Cargo.toml index 4e11377dcb..561e90da07 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" @@ -12,33 +12,66 @@ 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"] -attiny2313 = ["avr-device/attiny2313", "device-selected"] + +# MCU-specific targets. Due to use of deprecated globals, only one MCU can be selected at a time +# In attiny-hal 0.30 the defaults will change to no deprecated globals +attiny84 = ["attiny84-deprecated-globals"] +attiny85 = ["attiny85-deprecated-globals"] +attiny88 = ["attiny88-deprecated-globals"] +attiny167 = ["attiny167-deprecated-globals"] +attiny2313 = ["attiny2313-deprecated-globals"] + +# MCU-specific targets with deprecated globals. This is the default in attiny-hal <0.3.0 +attiny84-deprecated-globals = ["_mcu-attiny84", "deprecated-globals"] +attiny85-deprecated-globals = ["_mcu-attiny85", "deprecated-globals"] +attiny88-deprecated-globals = ["_mcu-attiny88", "deprecated-globals"] +attiny167-deprecated-globals = ["_mcu-attiny167", "deprecated-globals"] +attiny2313-deprecated-globals = ["_mcu-attiny2313", "deprecated-globals"] + +# MCU-specific targets without deprecated globals. This will be the default in attiny-hal 0.3.0 +attiny84-no-deprecated-globals = ["_mcu-attiny84"] +attiny85-no-deprecated-globals = ["_mcu-attiny85"] +attiny88-no-deprecated-globals = ["_mcu-attiny88"] +attiny167-no-deprecated-globals = ["_mcu-attiny167"] +attiny2313-no-deprecated-globals = ["_mcu-attiny2313"] critical-section-impl = ["avr-device/critical-section-impl"] -# Allow certain downstream crates to overwrite the device selection error by themselves. -disable-device-selection-error = [] +default = [] + +docsrs = [ + "attiny84-no-deprecated-globals", + "attiny85-no-deprecated-globals", + "attiny88-no-deprecated-globals", + "attiny167-no-deprecated-globals", + "attiny2313-no-deprecated-globals", +] + +# Include soon-to-be-deprecated globals in the crate. Only one MCU can be selected if deprecated globals are enabled +deprecated-globals = [] + +# When using this crate from another lib crate, you can use this feature to turn suppress the chip selection error in favor of your own error +disable-device-selection-error = ["_mcu-selected"] + +# MCU-specific implementation features +# Do not use directly; use either an -deprecated-globals feature or any number of -no-deprecated-globals features +_mcu-attiny84 = ["_mcu-selected", "_peripheral-simple-pwm", "avr-device/attiny84"] +_mcu-attiny85 = ["_mcu-selected", "_peripheral-adc", "_peripheral-simple-pwm", "avr-device/attiny85"] +_mcu-attiny88 = ["_mcu-selected", "_peripheral-adc", "_peripheral-spi", "_peripheral-simple-pwm", "avr-device/attiny88"] +_mcu-attiny167 = ["_mcu-selected", "_peripheral-adc", "_peripheral-spi", "avr-device/attiny167"] +_mcu-attiny2313 = ["_mcu-selected", "avr-device/attiny2313"] + +_mcu-selected = [] +_peripheral-adc = [] +_peripheral-spi = [] +_peripheral-simple-pwm = [] -# We must select a microcontroller to build on docs.rs -docsrs = ["attiny85"] [dependencies] avr-hal-generic = { path = "../../avr-hal-generic/" } [dependencies.avr-device] version = "0.5.4" - -# Because this crate has its own check that at least one device is selected, we -# can safely "circumvent" the check in `avr-device`. -# -# Why would we want that? Otherwise, as `avr-device` is compiled first, its -# error will be shown and ours won't which leads to a degraded user experience -# as the displayed error message does not really tell what needs to be done... features = ["device-selected"] [package.metadata.docs.rs] 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..98e47948ba --- /dev/null +++ b/mcu/attiny-hal/src/attiny167.rs @@ -0,0 +1,80 @@ +pub use avr_device::attiny167 as pac; + +pub struct Hal; + +use crate::r#impl::*; + +impl_mod_adc! { + hal: crate::attiny167, + 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, + }, +} + +impl_mod_eeprom! { + hal: crate::attiny167, + capacity: 512, + addr_width: u16, + addr_reg: eear, +} + +impl_mod_port! { + hal: crate::attiny167, + 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, +} + +impl_mod_spi! { + hal: crate::attiny167, + sclk: PA5, + mosi: PA4, + miso: PA2, + cs: PA6, +} + +impl_mod_wdt! { + hal: crate::attiny167, + 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..80d5c94914 --- /dev/null +++ b/mcu/attiny-hal/src/attiny2313.rs @@ -0,0 +1,28 @@ +pub use avr_device::attiny2313 as pac; + +pub struct Hal; + +use crate::r#impl::*; + +impl_mod_eeprom! { + hal: crate::attiny2313, + capacity: 128, + addr_width: u8, + addr_reg: eear, +} + +impl_mod_port! { + hal: crate::attiny2313, + 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, +} + +impl_mod_wdt! { + hal: crate::attiny2313, + 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..7f3c976bdc --- /dev/null +++ b/mcu/attiny-hal/src/attiny84.rs @@ -0,0 +1,108 @@ +pub use avr_device::attiny84 as pac; + +pub struct Hal; + +use crate::r#impl::*; + +impl_mod_eeprom! { + hal: crate::attiny84, + capacity: 512, + addr_width: u16, + addr_reg: eear, +} + +impl_mod_port! { + hal: crate::attiny84, + ports: { + A: [0, 1, 2, 3, 4, 5, 6, 7], + B: [0, 1, 2, 3], + }, + impl!: avr_hal_generic::impl_port_traditional, +} + +impl_mod_simple_pwm! { + hal: crate::attiny84, + impl: { + pub use avr_hal_generic::simple_pwm::{IntoPwmPin, Prescaler, PwmPinOps}; + + avr_hal_generic::impl_simple_pwm! { + /// Use `TC0` for PWM (pins `PB2`, `PA7`) + pub struct Timer0Pwm { + timer: crate::attiny84::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: { + hal::port::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()); + }, + }, + + hal::port::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()); + }, + }, + }, + } + } + + avr_hal_generic::impl_simple_pwm! { + /// Use `TC1` for PWM (pins `PA6`, 'PA5') + pub struct Timer1Pwm { + timer: crate::attiny84::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: { + hal::port::PA6: { + ocr: ocr1a, + into_pwm: |tim| if enable { + tim.tccr1a.modify(|_, w| w.com1a().bits(0b10)); + } else { + tim.tccr1a.modify(|_, w| w.com1a().disconnected()); + }, + }, + + hal::port::PA5: { + ocr: ocr1b, + into_pwm: |tim| if enable { + tim.tccr1a.modify(|_, w| w.com1b().bits(0b10)); + } else { + tim.tccr1a.modify(|_, w| w.com1b().disconnected()); + }, + }, + }, + } + } + }, +} + +impl_mod_wdt! { + hal: crate::attiny84, + 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..ea2b8c330e --- /dev/null +++ b/mcu/attiny-hal/src/attiny85.rs @@ -0,0 +1,150 @@ +pub use avr_device::attiny85 as pac; + +pub struct Hal; + +use crate::r#impl::*; + +impl_mod_adc! { + hal: crate::attiny85, + 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, + }, +} + +impl_mod_eeprom! { + hal: crate::attiny85, + capacity: 512, + addr_width: u16, + addr_reg: eear, +} + +impl_mod_port! { + hal: crate::attiny85, + ports: { + B: [0, 1, 2, 3, 4, 5], + }, + impl!: avr_hal_generic::impl_port_traditional, +} + +impl_mod_simple_pwm! { + hal: crate::attiny85, + impl: { + pub use avr_hal_generic::simple_pwm::{IntoPwmPin, Prescaler, PwmPinOps}; + 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::attiny85::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: { + hal::port::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()); + }, + }, + + hal::port::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()); + }, + }, + }, + } + } + + 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::attiny85::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: { + hal::port::PB4: { + ocr: ocr1b, + into_pwm: |tim| if enable { + tim.gtccr.modify(|_, w| w.com1b().bits(0b10)); + } else { + tim.gtccr.modify(|_, w| w.com1b().disconnected()); + }, + }, + }, + } + } + }, +} + +impl_mod_wdt! { + hal: crate::attiny85, + 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..176b4e55da --- /dev/null +++ b/mcu/attiny-hal/src/attiny88.rs @@ -0,0 +1,122 @@ +pub use avr_device::attiny88 as pac; + +pub struct Hal; + +use crate::r#impl::*; + +impl_mod_adc! { + hal: crate::attiny88, + 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, + }, +} + +impl_mod_eeprom! { + hal: crate::attiny88, + capacity: 64, + addr_width: u8, + addr_reg: eearl, +} + +impl_mod_port! { + hal: crate::attiny88, + 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, +} + +impl_mod_simple_pwm! { + hal: crate::attiny88, + impl: { + pub use avr_hal_generic::simple_pwm::{IntoPwmPin, Prescaler, PwmPinOps}; + + 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::attiny88::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: { + hal::port::PB1: { + ocr: ocr1a, + into_pwm: |tim| if enable { + tim.tccr1a.modify(|_, w| w.com1a().bits(0b10)); + } else { + tim.tccr1a.modify(|_, w| w.com1a().disconnected()); + }, + }, + + hal::port::PB2: { + ocr: ocr1b, + into_pwm: |tim| if enable { + tim.tccr1a.modify(|_, w| w.com1b().bits(0b10)); + } else { + tim.tccr1a.modify(|_, w| w.com1b().disconnected()); + }, + }, + }, + } + } + }, +} + +impl_mod_spi! { + hal: crate::attiny88, + sclk: PB5, + mosi: PB3, + miso: PB4, + cs: PB2, +} + +impl_mod_wdt! { + hal: crate::attiny88, + 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..edc24f9c4f --- /dev/null +++ b/mcu/attiny-hal/src/globals.rs @@ -0,0 +1,93 @@ +// Deprecated globals + +#[cfg(all( + // More than one MCU selected -> error + all( + feature = "_mcu-attiny167", + any( + feature = "_mcu-attiny2313", + feature = "_mcu-attiny84", + feature = "_mcu-attiny85", + feature = "_mcu-attiny88", + ) + ), + all( + feature = "_mcu-attiny2313", + any( + feature = "_mcu-attiny167", + feature = "_mcu-attiny84", + feature = "_mcu-attiny85", + feature = "_mcu-attiny88", + ) + ), + all( + feature = "_mcu-attiny84", + any( + feature = "_mcu-attiny167", + feature = "_mcu-attiny2313", + feature = "_mcu-attiny85", + feature = "_mcu-attiny88", + ) + ), + all( + feature = "_mcu-attiny85", + any( + feature = "_mcu-attiny167", + feature = "_mcu-attiny2313", + feature = "_mcu-attiny84", + feature = "_mcu-attiny88", + ) + ), + all( + feature = "_mcu-attiny88", + any( + feature = "_mcu-attiny167", + feature = "_mcu-attiny2313", + feature = "_mcu-attiny84", + feature = "_mcu-attiny85", + ) + ), +))] +compile_error!( + "When using deprecated globals (default in attiny-hal 0.1.x), you cannot target multiple chips. + + To target multiple chips, turn off deprecated globals by using the following features + + * attiny84-no-deprecated-globals instead of attiny84 + * attiny85-no-deprecated-globals instead of attiny85 + * attiny88-no-deprecated-globals instead of attiny88 + * attiny167-no-deprecated-globals instead of attiny167 + * attiny2313-no-deprecated-globals instead of attiny2313 + " +); + +#[cfg(feature = "_mcu-attiny167")] +pub use crate::attiny167 as hal; + +#[cfg(feature = "_mcu-attiny2313")] +pub use crate::attiny2313 as hal; + +#[cfg(feature = "_mcu-attiny84")] +pub use crate::attiny84 as hal; + +#[cfg(feature = "_mcu-attiny85")] +pub use crate::attiny85 as hal; + +#[cfg(feature = "_mcu-attiny88")] +pub use crate::attiny88 as hal; + +pub use hal::{eeprom, pac, port, wdt, Hal as Attiny, pins}; +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..4328369349 --- /dev/null +++ b/mcu/attiny-hal/src/impl/mod.rs @@ -0,0 +1,23 @@ +#[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::*; 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..8726812b3e --- /dev/null +++ b/mcu/attiny-hal/src/impl/simple_pwm.rs @@ -0,0 +1,15 @@ +macro_rules! impl_mod_simple_pwm { + ( + hal: crate::$hal:ident, + $(impl: { + $($impl:item)* + },)? + ) => { + pub mod simple_pwm { + use crate::$hal as hal; + $($($impl)*)? + } + } +} + +pub(crate) use impl_mod_simple_pwm; 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..2160d4695c 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 = "_mcu-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 chips as a feature. - Please select one of the following + Please select at least one of the following features + * attiny84 * attiny85 * attiny88 * attiny167 @@ -32,101 +23,28 @@ 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; - -#[cfg(feature = "attiny84")] -#[macro_export] -macro_rules! pins { - ($p:expr) => { - $crate::Pins::new($p.PORTA, $p.PORTB) - }; -} -#[cfg(feature = "attiny85")] -#[macro_export] -macro_rules! pins { - ($p:expr) => { - $crate::Pins::new($p.PORTB) - }; -} -#[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(crate) mod r#impl; + +#[cfg(feature = "_mcu-attiny167")] +pub mod attiny167; +#[cfg(feature = "_mcu-attiny2313")] +pub mod attiny2313; +#[cfg(feature = "_mcu-attiny84")] +pub mod attiny84; +#[cfg(feature = "_mcu-attiny85")] +pub mod attiny85; +#[cfg(feature = "_mcu-attiny88")] +pub mod attiny88; + +#[cfg(feature = "deprecated-globals")] +mod globals; +#[cfg(feature = "deprecated-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(), - }, -}