From f697a3e8df7640b6d3ea3248c4553e809e45dd1d Mon Sep 17 00:00:00 2001 From: David Morrison Date: Mon, 8 Mar 2021 21:19:21 -0800 Subject: [PATCH 1/6] add the atmega8u2 chip --- .github/workflows/ci.yml | 4 ++++ Cargo.toml | 2 ++ avr-specs/avr-atmega8u2.json | 27 +++++++++++++++++++++++++++ mcu/atmega-hal/Cargo.toml | 1 + mcu/atmega-hal/src/lib.rs | 11 ++++++++++- mcu/atmega-hal/src/port.rs | 35 +++++++++++++++++++++++++++++++++++ mcu/atmega-hal/src/usart.rs | 14 ++++++++++++-- 7 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 avr-specs/avr-atmega8u2.json diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d7b7362064..988b3cee6b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -45,6 +45,10 @@ jobs: name: attiny88 spec: attiny88 crate: attiny-hal + - type: mcu + name: atmega8u2 + spec: atmega8u2 + crate: atmega-hal runs-on: ubuntu-latest steps: - name: Checkout code diff --git a/Cargo.toml b/Cargo.toml index c5d7012abf..48ada42dcc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,3 +36,5 @@ exclude = [ [patch.crates-io] # XXX: Temporary fix for avr-rust/rust#148 ufmt = { git = "https://github.com/Rahix/ufmt.git", rev = "12225dc1678e42fecb0e8635bf80f501e24817d9" } +# Temporary fix until avr-device has a version bump +avr-device = { git = "https://github.com/Rahix/avr-device", rev = "92e4d5f" } diff --git a/avr-specs/avr-atmega8u2.json b/avr-specs/avr-atmega8u2.json new file mode 100644 index 0000000000..30dc533ffe --- /dev/null +++ b/avr-specs/avr-atmega8u2.json @@ -0,0 +1,27 @@ +{ + "arch": "avr", + "atomic-cas": false, + "cpu": "atmega8u2", + "data-layout": "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8", + "eh-frame-header": false, + "exe-suffix": ".elf", + "executables": true, + "late-link-args": { + "gcc": [ + "-lgcc" + ] + }, + "linker": "avr-gcc", + "linker-is-gnu": true, + "llvm-target": "avr-unknown-unknown", + "max-atomic-width": 8, + "no-default-libraries": false, + "pre-link-args": { + "gcc": [ + "-mmcu=atmega8u2", + "-Wl,--as-needed" + ] + }, + "target-c-int-width": "16", + "target-pointer-width": "16" +} diff --git a/mcu/atmega-hal/Cargo.toml b/mcu/atmega-hal/Cargo.toml index 594221c3b8..c2957da2bd 100644 --- a/mcu/atmega-hal/Cargo.toml +++ b/mcu/atmega-hal/Cargo.toml @@ -8,6 +8,7 @@ edition = "2018" rt = ["avr-device/rt"] device-selected = [] enable-extra-adc = [] +atmega8u2 = ["avr-device/atmega8u2", "device-selected"] atmega48p = ["avr-device/atmega48p", "device-selected"] atmega168 = ["avr-device/atmega168", "device-selected"] atmega328p = ["avr-device/atmega328p", "device-selected"] diff --git a/mcu/atmega-hal/src/lib.rs b/mcu/atmega-hal/src/lib.rs index c6e2e834fd..6eb1505c51 100644 --- a/mcu/atmega-hal/src/lib.rs +++ b/mcu/atmega-hal/src/lib.rs @@ -9,6 +9,7 @@ compile_error!( Please select one of the following + * atmega8u2 * atmega48p * atmega168 * atmega328p @@ -40,6 +41,9 @@ pub use avr_device::atmega32u4 as pac; /// Reexport of `atmega48p` from `avr-device` #[cfg(feature = "atmega48p")] pub use avr_device::atmega48p as pac; +/// Reexport of `atmega8u2` from `avr-device` +#[cfg(feature = "atmega8u2")] +pub use avr_device::atmega8u2 as pac; /// See [`avr_device::entry`](https://docs.rs/avr-device/latest/avr_device/attr.entry.html). #[cfg(feature = "rt")] @@ -78,7 +82,12 @@ pub use usart::Usart; pub struct Atmega; -#[cfg(any(feature = "atmega48p", feature = "atmega168", feature = "atmega328p"))] +#[cfg(any( + feature = "atmega8u2", + feature = "atmega48p", + feature = "atmega168", + feature = "atmega328p" +))] #[macro_export] macro_rules! pins { ($p:expr) => { diff --git a/mcu/atmega-hal/src/port.rs b/mcu/atmega-hal/src/port.rs index 0e55756279..db592a60b1 100644 --- a/mcu/atmega-hal/src/port.rs +++ b/mcu/atmega-hal/src/port.rs @@ -33,6 +33,41 @@ avr_hal_generic::impl_port_traditional! { } } +#[cfg(feature = "atmega8u2")] +avr_hal_generic::impl_port_traditional! { + enum Ports { + PORTB: (crate::pac::PORTB, portb, pinb, ddrb), + PORTC: (crate::pac::PORTC, portc, pinc, ddrc), + PORTD: (crate::pac::PORTD, portd, pind, ddrd), + } + + pub struct Pins { + pb0: PB0 = (crate::pac::PORTB, PORTB, 0, portb, pinb, ddrb), + pb1: PB1 = (crate::pac::PORTB, PORTB, 1, portb, pinb, ddrb), + pb2: PB2 = (crate::pac::PORTB, PORTB, 2, portb, pinb, ddrb), + pb3: PB3 = (crate::pac::PORTB, PORTB, 3, portb, pinb, ddrb), + pb4: PB4 = (crate::pac::PORTB, PORTB, 4, portb, pinb, ddrb), + pb5: PB5 = (crate::pac::PORTB, PORTB, 5, portb, pinb, ddrb), + pb6: PB6 = (crate::pac::PORTB, PORTB, 6, portb, pinb, ddrb), + pb7: PB7 = (crate::pac::PORTB, PORTB, 7, portb, pinb, ddrb), + pc0: PC0 = (crate::pac::PORTC, PORTC, 0, portc, pinc, ddrc), + pc1: PC1 = (crate::pac::PORTC, PORTC, 1, portc, pinc, ddrc), + pc2: PC2 = (crate::pac::PORTC, PORTC, 2, portc, pinc, ddrc), + pc3: PC3 = (crate::pac::PORTC, PORTC, 3, portc, pinc, ddrc), + pc4: PC4 = (crate::pac::PORTC, PORTC, 4, portc, pinc, ddrc), + pc5: PC5 = (crate::pac::PORTC, PORTC, 5, portc, pinc, ddrc), + pc6: PC6 = (crate::pac::PORTC, PORTC, 6, portc, pinc, ddrc), + pc7: PC7 = (crate::pac::PORTC, PORTC, 7, portc, pinc, ddrc), + pd0: PD0 = (crate::pac::PORTD, PORTD, 0, portd, pind, ddrd), + pd1: PD1 = (crate::pac::PORTD, PORTD, 1, portd, pind, ddrd), + pd2: PD2 = (crate::pac::PORTD, PORTD, 2, portd, pind, ddrd), + pd3: PD3 = (crate::pac::PORTD, PORTD, 3, portd, pind, ddrd), + pd4: PD4 = (crate::pac::PORTD, PORTD, 4, portd, pind, ddrd), + pd5: PD5 = (crate::pac::PORTD, PORTD, 5, portd, pind, ddrd), + pd6: PD6 = (crate::pac::PORTD, PORTD, 6, portd, pind, ddrd), + pd7: PD7 = (crate::pac::PORTD, PORTD, 7, portd, pind, ddrd), + } +} #[cfg(feature = "atmega328pb")] avr_hal_generic::impl_port_traditional! { enum Ports { diff --git a/mcu/atmega-hal/src/usart.rs b/mcu/atmega-hal/src/usart.rs index b6216732c1..7696118f6c 100644 --- a/mcu/atmega-hal/src/usart.rs +++ b/mcu/atmega-hal/src/usart.rs @@ -41,14 +41,24 @@ avr_hal_generic::impl_usart_traditional! { tx: port::PB3, } -#[cfg(any(feature = "atmega32u4", feature = "atmega1280", feature = "atmega2560"))] +#[cfg(any( + feature = "atmega8u2", + feature = "atmega32u4", + feature = "atmega1280", + feature = "atmega2560" +))] pub type Usart1 = Usart< crate::pac::USART1, port::Pin, port::Pin, CLOCK, >; -#[cfg(any(feature = "atmega32u4", feature = "atmega1280", feature = "atmega2560"))] +#[cfg(any( + fetaure = "atmega8u2", + feature = "atmega32u4", + feature = "atmega1280", + feature = "atmega2560" +))] avr_hal_generic::impl_usart_traditional! { hal: crate::Atmega, peripheral: crate::pac::USART1, From f9a5ba227a5ca94136366880c86a65c0d5a9a7cd Mon Sep 17 00:00:00 2001 From: David Morrison Date: Tue, 9 Mar 2021 23:01:52 -0800 Subject: [PATCH 2/6] regenerate avr-specs --- avr-specs/sync-from-upstream.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/avr-specs/sync-from-upstream.py b/avr-specs/sync-from-upstream.py index d1f7060280..855175f5a9 100755 --- a/avr-specs/sync-from-upstream.py +++ b/avr-specs/sync-from-upstream.py @@ -4,6 +4,9 @@ import subprocess SPECS = { + "atmega8u2": { + "cpu": "atmega8u2", + }, "atmega32u4": { "cpu": "atmega32u4", }, From bccde1bb64bc67532dd1717159c29c787a79ce30 Mon Sep 17 00:00:00 2001 From: David Morrison Date: Sat, 17 Apr 2021 16:00:55 -0700 Subject: [PATCH 3/6] add SPI for 8u2 --- mcu/atmega-hal/src/spi.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mcu/atmega-hal/src/spi.rs b/mcu/atmega-hal/src/spi.rs index 8906c92671..329f6fc740 100644 --- a/mcu/atmega-hal/src/spi.rs +++ b/mcu/atmega-hal/src/spi.rs @@ -2,7 +2,7 @@ use crate::port; pub use avr_hal_generic::spi::*; -#[cfg(any(feature = "atmega2560", feature = "atmega32u4"))] +#[cfg(any(feature = "atmega8u2", feature = "atmega2560", feature = "atmega32u4"))] pub type Spi = avr_hal_generic::spi::Spi< crate::Atmega, crate::pac::SPI, @@ -11,7 +11,7 @@ pub type Spi = avr_hal_generic::spi::Spi< port::PB3, port::PB0, >; -#[cfg(any(feature = "atmega2560", feature = "atmega32u4"))] +#[cfg(any(feature = "atmega8u2", feature = "atmega2560", feature = "atmega32u4"))] avr_hal_generic::impl_spi! { hal: crate::Atmega, peripheral: crate::pac::SPI, From 892328ba43c5faf1c8a5b04cc079ea0625682e08 Mon Sep 17 00:00:00 2001 From: supersimple33 <40609224+supersimple33@users.noreply.github.com> Date: Wed, 24 Jul 2024 09:24:35 -0400 Subject: [PATCH 4/6] Duplicate code for atmega8 --- mcu/atmega-hal/src/port.rs | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/mcu/atmega-hal/src/port.rs b/mcu/atmega-hal/src/port.rs index 6afa3f5ec3..35b3165661 100644 --- a/mcu/atmega-hal/src/port.rs +++ b/mcu/atmega-hal/src/port.rs @@ -1,6 +1,6 @@ pub use avr_hal_generic::port::{mode, PinMode, PinOps}; -#[cfg(any(feature = "atmega48p", feature = "atmega168", feature = "atmega328p"))] +#[cfg(any(feature = "atmega48p", feature = "atmega168", feature = "atmega328p", feature="atmega8"))] avr_hal_generic::impl_port_traditional! { enum Ports { B: crate::pac::PORTB = [0, 1, 2, 3, 4, 5, 6, 7], @@ -114,12 +114,3 @@ avr_hal_generic::impl_port_traditional! { D: crate::pac::PORTD = [0, 1, 2, 3, 4, 5, 6, 7], } } - -#[cfg(any(feature = "atmega8"))] -avr_hal_generic::impl_port_traditional! { - enum Ports { - B: crate::pac::PORTB = [0, 1, 2, 3, 4, 5, 6, 7], - C: crate::pac::PORTC = [0, 1, 2, 3, 4, 5, 6], - D: crate::pac::PORTD = [0, 1, 2, 3, 4, 5, 6, 7], - } -} From 65cf1492be78ae296796b768ffb41ffb548c16fe Mon Sep 17 00:00:00 2001 From: supersimple33 <40609224+supersimple33@users.noreply.github.com> Date: Wed, 24 Jul 2024 09:31:17 -0400 Subject: [PATCH 5/6] Updating how ports are done --- mcu/atmega-hal/src/port.rs | 34 ++++------------------------------ 1 file changed, 4 insertions(+), 30 deletions(-) diff --git a/mcu/atmega-hal/src/port.rs b/mcu/atmega-hal/src/port.rs index 35b3165661..235edce2da 100644 --- a/mcu/atmega-hal/src/port.rs +++ b/mcu/atmega-hal/src/port.rs @@ -22,38 +22,12 @@ avr_hal_generic::impl_port_traditional! { #[cfg(feature = "atmega8u2")] avr_hal_generic::impl_port_traditional! { enum Ports { - PORTB: (crate::pac::PORTB, portb, pinb, ddrb), - PORTC: (crate::pac::PORTC, portc, pinc, ddrc), - PORTD: (crate::pac::PORTD, portd, pind, ddrd), - } - - pub struct Pins { - pb0: PB0 = (crate::pac::PORTB, PORTB, 0, portb, pinb, ddrb), - pb1: PB1 = (crate::pac::PORTB, PORTB, 1, portb, pinb, ddrb), - pb2: PB2 = (crate::pac::PORTB, PORTB, 2, portb, pinb, ddrb), - pb3: PB3 = (crate::pac::PORTB, PORTB, 3, portb, pinb, ddrb), - pb4: PB4 = (crate::pac::PORTB, PORTB, 4, portb, pinb, ddrb), - pb5: PB5 = (crate::pac::PORTB, PORTB, 5, portb, pinb, ddrb), - pb6: PB6 = (crate::pac::PORTB, PORTB, 6, portb, pinb, ddrb), - pb7: PB7 = (crate::pac::PORTB, PORTB, 7, portb, pinb, ddrb), - pc0: PC0 = (crate::pac::PORTC, PORTC, 0, portc, pinc, ddrc), - pc1: PC1 = (crate::pac::PORTC, PORTC, 1, portc, pinc, ddrc), - pc2: PC2 = (crate::pac::PORTC, PORTC, 2, portc, pinc, ddrc), - pc3: PC3 = (crate::pac::PORTC, PORTC, 3, portc, pinc, ddrc), - pc4: PC4 = (crate::pac::PORTC, PORTC, 4, portc, pinc, ddrc), - pc5: PC5 = (crate::pac::PORTC, PORTC, 5, portc, pinc, ddrc), - pc6: PC6 = (crate::pac::PORTC, PORTC, 6, portc, pinc, ddrc), - pc7: PC7 = (crate::pac::PORTC, PORTC, 7, portc, pinc, ddrc), - pd0: PD0 = (crate::pac::PORTD, PORTD, 0, portd, pind, ddrd), - pd1: PD1 = (crate::pac::PORTD, PORTD, 1, portd, pind, ddrd), - pd2: PD2 = (crate::pac::PORTD, PORTD, 2, portd, pind, ddrd), - pd3: PD3 = (crate::pac::PORTD, PORTD, 3, portd, pind, ddrd), - pd4: PD4 = (crate::pac::PORTD, PORTD, 4, portd, pind, ddrd), - pd5: PD5 = (crate::pac::PORTD, PORTD, 5, portd, pind, ddrd), - pd6: PD6 = (crate::pac::PORTD, PORTD, 6, portd, pind, ddrd), - pd7: PD7 = (crate::pac::PORTD, PORTD, 7, portd, pind, ddrd), + 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], } } + #[cfg(feature = "atmega328pb")] avr_hal_generic::impl_port_traditional! { enum Ports { From 404163c2f4e9c9899d1713d42cf1ccb65f18de23 Mon Sep 17 00:00:00 2001 From: supersimple33 <40609224+supersimple33@users.noreply.github.com> Date: Wed, 24 Jul 2024 09:42:03 -0400 Subject: [PATCH 6/6] AtMega8u2 does not have adc --- mcu/atmega-hal/Cargo.toml | 3 ++- mcu/atmega-hal/src/adc.rs | 3 +++ mcu/atmega-hal/src/lib.rs | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/mcu/atmega-hal/Cargo.toml b/mcu/atmega-hal/Cargo.toml index 4755633fe4..c5c179c799 100644 --- a/mcu/atmega-hal/Cargo.toml +++ b/mcu/atmega-hal/Cargo.toml @@ -14,7 +14,7 @@ categories = ["no-std", "embedded", "hardware-support"] rt = ["avr-device/rt"] device-selected = [] enable-extra-adc = [] -atmega8u2 = ["avr-device/atmega8u2", "device-selected"] +disable-adc = [] atmega48p = ["avr-device/atmega48p", "device-selected"] atmega164pa = ["avr-device/atmega164pa", "device-selected"] atmega168 = ["avr-device/atmega168", "device-selected"] @@ -27,6 +27,7 @@ atmega128a = ["avr-device/atmega128a", "device-selected"] atmega1280 = ["avr-device/atmega1280", "device-selected"] atmega1284p = ["avr-device/atmega1284p", "device-selected"] atmega8 = ["avr-device/atmega8", "device-selected"] +atmega8u2 = ["avr-device/atmega8u2", "device-selected", "disable-adc"] critical-section-impl = ["avr-device/critical-section-impl"] diff --git a/mcu/atmega-hal/src/adc.rs b/mcu/atmega-hal/src/adc.rs index d79f5866ec..e784358320 100644 --- a/mcu/atmega-hal/src/adc.rs +++ b/mcu/atmega-hal/src/adc.rs @@ -31,6 +31,7 @@ pub struct AdcSettings { pub ref_voltage: ReferenceVoltage, } +#[cfg(not(feature = "disable-adc"))] fn apply_settings(peripheral: &crate::pac::ADC, settings: AdcSettings) { peripheral.adcsra.write(|w| { w.aden().set_bit(); @@ -52,9 +53,11 @@ fn apply_settings(peripheral: &crate::pac::ADC, settings: AdcSettings) { } /// Check the [`avr_hal_generic::adc::Adc`] documentation. +#[cfg(not(feature = "disable-adc"))] pub type Adc = avr_hal_generic::adc::Adc; /// Check the [`avr_hal_generic::adc::Channel`] documentation. +#[cfg(not(feature = "disable-adc"))] pub type Channel = avr_hal_generic::adc::Channel; /// Additional channels diff --git a/mcu/atmega-hal/src/lib.rs b/mcu/atmega-hal/src/lib.rs index 0272e692fc..ae7f99cfd9 100644 --- a/mcu/atmega-hal/src/lib.rs +++ b/mcu/atmega-hal/src/lib.rs @@ -114,7 +114,7 @@ pub use avr_hal_generic::prelude; #[cfg(feature = "device-selected")] pub mod adc; -#[cfg(feature = "device-selected")] +#[cfg(all(feature = "device-selected", not(feature = "disable-adc")))] pub use adc::Adc; #[cfg(feature = "device-selected")]