From 2cb45a8659d2d5c6f937ed7c0f39d6ef9944a9ea Mon Sep 17 00:00:00 2001 From: andber1 <82754113+andber1@users.noreply.github.com> Date: Mon, 10 Mar 2025 20:13:41 +0100 Subject: [PATCH] ATmega16 support --- mcu/atmega-hal/Cargo.toml | 1 + mcu/atmega-hal/src/adc.rs | 5 ++++- mcu/atmega-hal/src/eeprom.rs | 2 +- mcu/atmega-hal/src/i2c.rs | 4 ++-- mcu/atmega-hal/src/lib.rs | 8 +++++++- mcu/atmega-hal/src/port.rs | 2 +- mcu/atmega-hal/src/simple_pwm.rs | 11 ++++++++++- mcu/atmega-hal/src/usart.rs | 7 +++++++ mcu/atmega-hal/src/wdt.rs | 2 +- 9 files changed, 34 insertions(+), 8 deletions(-) diff --git a/mcu/atmega-hal/Cargo.toml b/mcu/atmega-hal/Cargo.toml index 7f8c5b6ce4..bff9738b51 100644 --- a/mcu/atmega-hal/Cargo.toml +++ b/mcu/atmega-hal/Cargo.toml @@ -15,6 +15,7 @@ rt = ["avr-device/rt"] device-selected = [] enable-extra-adc = [] atmega48p = ["avr-device/atmega48p", "device-selected"] +atmega16 = ["avr-device/atmega16", "device-selected"] atmega164pa = ["avr-device/atmega164pa", "device-selected"] atmega168 = ["avr-device/atmega168", "device-selected"] atmega328p = ["avr-device/atmega328p", "device-selected"] diff --git a/mcu/atmega-hal/src/adc.rs b/mcu/atmega-hal/src/adc.rs index a244225608..2881ae62fa 100644 --- a/mcu/atmega-hal/src/adc.rs +++ b/mcu/atmega-hal/src/adc.rs @@ -126,6 +126,7 @@ pub mod channel { pub struct ADC7; #[cfg(any( feature = "atmega1280", + feature = "atmega16", feature = "atmega168", feature = "atmega2560", feature = "atmega32a", @@ -136,6 +137,7 @@ pub mod channel { feature = "atmega128a", feature = "atmega1284p", feature = "atmega8", + feature = "atmega16", feature = "atmega164pa", feature = "atmega88p" ))] @@ -152,6 +154,7 @@ pub mod channel { feature = "atmega128a", feature = "atmega1284p", feature = "atmega8", + feature = "atmega16", feature = "atmega164pa", feature = "atmega88p" ))] @@ -202,7 +205,7 @@ avr_hal_generic::impl_adc! { }, } -#[cfg(any(feature = "atmega32a"))] +#[cfg(any(feature = "atmega16", feature = "atmega32a"))] avr_hal_generic::impl_adc! { hal: crate::Atmega, peripheral: crate::pac::ADC, diff --git a/mcu/atmega-hal/src/eeprom.rs b/mcu/atmega-hal/src/eeprom.rs index 9d1b946900..1a6c9fba2b 100644 --- a/mcu/atmega-hal/src/eeprom.rs +++ b/mcu/atmega-hal/src/eeprom.rs @@ -86,7 +86,7 @@ avr_hal_generic::impl_eeprom_atmega! { }, } -#[cfg(any(feature = "atmega8"))] +#[cfg(any(feature = "atmega8", feature = "atmega8"))] avr_hal_generic::impl_eeprom_atmega_old! { hal: crate::Atmega, peripheral: crate::pac::EEPROM, diff --git a/mcu/atmega-hal/src/i2c.rs b/mcu/atmega-hal/src/i2c.rs index e44c0f5d04..1d068366db 100644 --- a/mcu/atmega-hal/src/i2c.rs +++ b/mcu/atmega-hal/src/i2c.rs @@ -49,7 +49,7 @@ avr_hal_generic::impl_i2c_twi! { scl: port::PD0, } -#[cfg(any(feature = "atmega164pa"))] +#[cfg(any(feature = "atmega16", feature = "atmega164pa"))] pub type I2c = avr_hal_generic::i2c::I2c< crate::Atmega, crate::pac::TWI, @@ -57,7 +57,7 @@ pub type I2c = avr_hal_generic::i2c::I2c< port::Pin, CLOCK, >; -#[cfg(any(feature = "atmega164pa"))] +#[cfg(any(feature = "atmega16", feature = "atmega164pa"))] avr_hal_generic::impl_i2c_twi! { hal: crate::Atmega, peripheral: crate::pac::TWI, diff --git a/mcu/atmega-hal/src/lib.rs b/mcu/atmega-hal/src/lib.rs index 601a637705..d076052345 100644 --- a/mcu/atmega-hal/src/lib.rs +++ b/mcu/atmega-hal/src/lib.rs @@ -6,6 +6,7 @@ //! //! **Note**: This version of the documentation was built for #![cfg_attr(feature = "atmega48p", doc = "**ATmega48P**.")] +#![cfg_attr(feature = "atmega16", doc = "**ATmega16**.")] #![cfg_attr(feature = "atmega164pa", doc = "**ATmega164PA**.")] #![cfg_attr(feature = "atmega168", doc = "**ATmega168**.")] #![cfg_attr(feature = "atmega328p", doc = "**ATmega328P**.")] @@ -35,6 +36,7 @@ compile_error!( Please select one of the following * atmega48p + * atmega16 * atmega164pa * atmega168 * atmega328p @@ -61,6 +63,10 @@ pub use avr_device::atmega1284p as pac; /// #[cfg(feature = "atmega128a")] pub use avr_device::atmega128a as pac; +/// Reexport of `atmega16` from `avr-device` +/// +#[cfg(feature = "atmega16")] +pub use avr_device::atmega16 as pac; /// Reexport of `atmega164pa` from `avr-device` /// #[cfg(feature = "atmega164pa")] @@ -165,7 +171,7 @@ macro_rules! pins { $crate::Pins::new($p.PORTB, $p.PORTC, $p.PORTD) }; } -#[cfg(any(feature = "atmega164pa"))] +#[cfg(any(feature = "atmega16", feature = "atmega164pa"))] #[macro_export] macro_rules! pins { ($p:expr) => { diff --git a/mcu/atmega-hal/src/port.rs b/mcu/atmega-hal/src/port.rs index 0ef96b38d6..63e471d69a 100644 --- a/mcu/atmega-hal/src/port.rs +++ b/mcu/atmega-hal/src/port.rs @@ -33,7 +33,7 @@ avr_hal_generic::impl_port_traditional! { } } -#[cfg(any(feature = "atmega164pa"))] +#[cfg(any(feature = "atmega16", feature = "atmega164pa"))] avr_hal_generic::impl_port_traditional! { enum Ports { A: crate::pac::PORTA = [0, 1, 2, 3, 4, 5, 6 ,7], diff --git a/mcu/atmega-hal/src/simple_pwm.rs b/mcu/atmega-hal/src/simple_pwm.rs index 65b9568d41..296280f3c9 100644 --- a/mcu/atmega-hal/src/simple_pwm.rs +++ b/mcu/atmega-hal/src/simple_pwm.rs @@ -1128,7 +1128,7 @@ avr_hal_generic::impl_simple_pwm! { } } -#[cfg(any(feature = "atmega164pa"))] +#[cfg(any(feature = "atmega16", feature = "atmega164pa"))] avr_hal_generic::impl_simple_pwm! { /// Use `TC1` for PWM (pins `PD4`, `PD5`) /// @@ -1150,6 +1150,7 @@ avr_hal_generic::impl_simple_pwm! { tim.tccr1a.modify(|_r, w| w.wgm1().bits(0b01)); tim.tccr1a.modify(|_r, w| w.com1a().bits(0b00)); tim.tccr1a.modify(|_r, w| w.com1b().bits(0b00)); + #[cfg(any(feature = "atmega164pa"))] tim.tccr1b.modify(|_r, w| match prescaler { Prescaler::Direct => w.cs1().running_no_prescaling(), Prescaler::Prescale8 => w.cs1().running_clk_8(), @@ -1157,6 +1158,14 @@ avr_hal_generic::impl_simple_pwm! { Prescaler::Prescale256 => w.cs1().running_clk_256(), Prescaler::Prescale1024 => w.cs1().running_clk_1024(), }); + #[cfg(any(feature = "atmega16"))] + tim.tccr1b.modify(|_r, w| match prescaler { + Prescaler::Direct => w.cs1().val_0x01(), + Prescaler::Prescale8 => w.cs1().val_0x02(), + Prescaler::Prescale64 => w.cs1().val_0x03(), + Prescaler::Prescale256 => w.cs1().val_0x04(), + Prescaler::Prescale1024 => w.cs1().val_0x05(), + }); }, pins: { PD4: { diff --git a/mcu/atmega-hal/src/usart.rs b/mcu/atmega-hal/src/usart.rs index d6f76f7023..6f630fcfd3 100644 --- a/mcu/atmega-hal/src/usart.rs +++ b/mcu/atmega-hal/src/usart.rs @@ -40,6 +40,13 @@ pub type UsartWriter = pub type UsartReader = avr_hal_generic::usart::UsartReader; +#[cfg(any(feature = "atmega16"))] +pub type Usart0 = Usart< + crate::pac::USART, + port::Pin, + port::Pin, + CLOCK, +>; #[cfg(any( feature = "atmega88p", feature = "atmega168", diff --git a/mcu/atmega-hal/src/wdt.rs b/mcu/atmega-hal/src/wdt.rs index 434189a82e..36b19725d6 100644 --- a/mcu/atmega-hal/src/wdt.rs +++ b/mcu/atmega-hal/src/wdt.rs @@ -3,7 +3,7 @@ pub use avr_hal_generic::wdt::{Timeout, WdtOps}; pub type Wdt = avr_hal_generic::wdt::Wdt; -#[cfg(not(any(feature = "atmega8", feature = "atmega32a", feature = "atmega128a")))] +#[cfg(not(any(feature = "atmega8", feature = "atmega16", feature = "atmega32a", feature = "atmega128a")))] avr_hal_generic::impl_wdt! { hal: crate::Atmega, peripheral: crate::pac::WDT,