Skip to content

Commit

Permalink
embedded-hal v1 transition (#723)
Browse files Browse the repository at this point in the history
* Rempace embedded-hal with ehal_02 in preparation to add embedded-hal-1

* Implementations for `gpio` module

* Implement i2c

* Implement SPI

* Adapt to new peripheral macros

* Fix compile error

* Add copy bounds to sercom wordsize to satisfy ehal-01

* Fix wrong copy bounds

* Impl embedded-io::{Read, Write} for uart

* Fix Tier 1 examples

* impl embedded_hal_nb for uart

* Fix u16::max_value() clippy lint

* Fix clippy transmute lint

* Add block_on_flags method to uart

* Minor cleanup

* Impl embedded_hal_nb::serial for spi

* Update changelogs

* Implement ehal-1 timer traits

* Implement embedded-hal 1.0 pwm

* Update changelog

* Fix compile error

* Remove unproven feature from HAL

* Update changelog

* Fix SPI implementation

* Fix T1 examples (again)

* rustfmt

* Fix feather_m4 compilation

* Fix metro_m4 spi example

* Fix metro_m4 i2c example

* Reexport dma feature in T1 BSPs and fix i2c examples
  • Loading branch information
jbeaurivage authored May 1, 2024
1 parent ecf2f58 commit 3847d0f
Show file tree
Hide file tree
Showing 79 changed files with 1,343 additions and 445 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[workspace]
resolver = "2"
members = ["hal", "atsamd-hal-macros"]
exclude = ["pac", "boards"]
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ The Hardware Abstraction Layer (HAL - [![Crates.io](https://img.shields.io/crate

| Chip family | Documented features |
|:------------|:----------------------|
| [samd11c] | samd11c unproven |
| [samd11d] | samd11d unproven |
| [samd21g] | samd21g unproven usb |
| [samd21j] | samd21j unproven usb |
| [samd51g] | samd51g unproven usb |
| [samd51j] | samd51j unproven usb |
| [samd51n] | samd51n unproven usb |
| [samd51p] | samd51p unproven usb |
| [samd11c] | samd11c |
| [samd11d] | samd11d |
| [samd21g] | samd21g usb |
| [samd21j] | samd21j usb |
| [samd51g] | samd51g usb |
| [samd51j] | samd51j usb |
| [samd51n] | samd51n usb |
| [samd51p] | samd51p usb |

[samd11c]: https://atsamd-rs.github.io/docs/samd11c/thumbv6m-none-eabi/doc/atsamd_hal/index.html
[samd11d]: https://atsamd-rs.github.io/docs/samd11d/thumbv6m-none-eabi/doc/atsamd_hal/index.html
Expand Down
1 change: 1 addition & 0 deletions boards/atsame54_xpro/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Unreleased

- Implement `embedded-hal` `1.0` for GPIO, SPI, I2C, UART and fix examples
- Update the PACs to svd2rust 0.30.2.

# v0.6.0
Expand Down
3 changes: 1 addition & 2 deletions boards/atsame54_xpro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ panic-rtt-target = { version = "0.1", features = ["cortex-m"] }
rtt-target = { version = "0.3", features = ["cortex-m"] }

[features]
default = ["rt", "atsamd-hal/same54p", "atsamd-hal/unproven"]
default = ["rt", "atsamd-hal/same54p"]
rt = ["cortex-m-rt", "atsamd-hal/same54p-rt"]
unproven = ["atsamd-hal/unproven"]
usb = ["atsamd-hal/usb", "usb-device"]
can = ["atsamd-hal/can"]

Expand Down
7 changes: 3 additions & 4 deletions boards/atsame54_xpro/examples/blinky_rtic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ use atsame54_xpro as bsp;
use bsp::hal;
use bsp::hal::clock::v2 as clock;
use dwt_systick_monotonic::DwtSystick;
use dwt_systick_monotonic::ExtU32 as _;
use dwt_systick_monotonic::{fugit::RateExtU32, ExtU32};
// TODO: Any reason this cannot be in a HAL's prelude?
use hal::ehal::digital::v2::StatefulOutputPin as _;
use hal::prelude::*;
use hal::ehal::digital::StatefulOutputPin;
use panic_rtt_target as _;
use rtt_target::{rprintln, rtt_init_print};

Expand Down Expand Up @@ -45,7 +44,7 @@ mod app {
bsp::pin_alias!(pins.xosc1_x_in),
bsp::pin_alias!(pins.xosc1_x_out),
// Xosc1 on Same54Xpro is 12 MHz
12.MHz(),
12_u32.MHz(),
)
.enable();

Expand Down
1 change: 1 addition & 0 deletions boards/feather_m0/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Unreleased

- Implement `embedded-hal` `1.0` for GPIO, SPI, I2C, UART and fix examples
- Update the PACs to svd2rust 0.30.2.

# v0.13.0
Expand Down
11 changes: 4 additions & 7 deletions boards/feather_m0/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ panic-semihosting = "0.5"
# ask the HAL to enable atsamd21g support
default = ["rt", "atsamd-hal/samd21g"]
rt = ["cortex-m-rt", "atsamd-hal/samd21g-rt"]
unproven = ["atsamd-hal/unproven"]
use_rtt = ["atsamd-hal/use_rtt"]
usb = ["atsamd-hal/usb", "usb-device"]
# Enable pins for the radio on "RadioFruits" with RFM95, RFM96, RFM69
Expand Down Expand Up @@ -87,11 +86,9 @@ name = "timers"

[[example]]
name = "pwm"
required-features = ["unproven"]

[[example]]
name = "adc"
required-features = ["unproven"]

[[example]]
name = "ssd1306_graphicsmode_128x64_i2c"
Expand All @@ -113,7 +110,7 @@ name = "ssd1306_terminalmode_128x64_spi"

[[example]]
name = "usb_echo"
required-features = ["usb", "unproven"]
required-features = ["usb"]

[[example]]
name = "sleeping_timer"
Expand All @@ -127,15 +124,15 @@ required-features = ["dma"]

[[example]]
name = "clock"
required-features = ["usb", "unproven"]
required-features = ["usb"]

[[example]]
name = "adalogger"
required-features = ["adalogger", "usb", "sdmmc", "unproven"]
required-features = ["adalogger", "usb", "sdmmc"]

[[example]]
name = "blinky_rtic"
required-features = ["rtic", "unproven"]
required-features = ["rtic"]

[[example]]
name = "uart"
Expand Down
6 changes: 3 additions & 3 deletions boards/feather_m0/examples/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use pac::Peripherals;

use hal::clock::GenericClockController;
use hal::dmac::{DmaController, PriorityLevel};
use hal::ehal::blocking::i2c::WriteRead;
use hal::prelude::*;
use hal::ehal::i2c::I2c;
use hal::fugit::RateExtU32;
use hal::sercom::i2c;

const LENGTH: usize = 1;
Expand Down Expand Up @@ -57,7 +57,7 @@ fn main() -> ! {
let sercom3_clock = &clocks.sercom3_core(&gclk0).unwrap();
let pads = i2c::Pads::new(sda, scl);
let mut i2c = i2c::Config::new(&pm, peripherals.SERCOM3, pads, sercom3_clock.freq())
.baud(100.khz())
.baud(100.kHz())
.enable();

let mut buffer = [0x00; 1];
Expand Down
17 changes: 10 additions & 7 deletions boards/feather_m0/examples/pwm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ use feather_m0 as bsp;
use bsp::pin_alias;
use hal::clock::GenericClockController;
use hal::delay::Delay;
use hal::prelude::*;
use hal::ehal::{delay::DelayNs, pwm::SetDutyCycle};
use hal::fugit::RateExtU32;
use hal::pwm::Pwm3;
use pac::{CorePeripherals, Peripherals};

Expand All @@ -38,16 +39,18 @@ fn main() -> ! {
let gclk0 = clocks.gclk0();
let mut pwm3 = Pwm3::new(
&clocks.tcc2_tc3(&gclk0).unwrap(),
1.khz(),
1.kHz(),
peripherals.TC3,
&mut peripherals.PM,
);
let max_duty = pwm3.get_max_duty();
let max_duty = pwm3.max_duty_cycle();

loop {
pwm3.set_duty(max_duty / 2);
delay.delay_ms(1000u16);
pwm3.set_duty(max_duty / 8);
delay.delay_ms(1000u16);
// The embedded-hal spec requires that set_duty_cycle returns a Result.
// In our case, the function is infaillible so we can safely ignore the result.
let _ = pwm3.set_duty_cycle(max_duty / 2);
delay.delay_ms(1000);
let _ = pwm3.set_duty_cycle(max_duty / 8);
delay.delay_ms(1000);
}
}
4 changes: 3 additions & 1 deletion boards/feather_m0/examples/timers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ use feather_m0 as bsp;

use bsp::{entry, pin_alias};
use hal::clock::GenericClockController;
use hal::prelude::*;
use hal::ehal::digital::OutputPin;
use hal::nb;
use hal::time::Hertz;
use hal::timer::TimerCounter;
use hal::timer_traits::InterruptDrivenTimer;
use pac::Peripherals;

#[entry]
Expand Down
4 changes: 2 additions & 2 deletions boards/feather_m0/examples/uart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use feather_m0 as bsp;
use bsp::{entry, periph_alias, pin_alias};
use hal::clock::GenericClockController;
use hal::dmac::{DmaController, PriorityLevel};
use hal::prelude::*;
use hal::fugit::RateExtU32;

use pac::Peripherals;

Expand Down Expand Up @@ -49,7 +49,7 @@ fn main() -> ! {
// Setup UART peripheral
let uart = bsp::uart(
&mut clocks,
9600.hz(),
9600.Hz(),
uart_sercom,
&mut pm,
uart_rx,
Expand Down
1 change: 1 addition & 0 deletions boards/feather_m4/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Unreleased

- Implement `embedded-hal` `1.0` for GPIO, SPI, I2C, UART and fix examples
- Update the PACs to svd2rust 0.30.2.

# v0.11.0
Expand Down
10 changes: 4 additions & 6 deletions boards/feather_m4/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ heapless = "0.7"
# ask the HAL to enable atsamd51j support
default = ["rt", "atsamd-hal/samd51j"]
rt = ["cortex-m-rt", "atsamd-hal/samd51j-rt"]
unproven = ["atsamd-hal/unproven"]
usb = ["atsamd-hal/usb", "usb-device"]
dma = ["atsamd-hal/dma", "unproven"]
dma = ["atsamd-hal/dma"]
max-channels = ["dma", "atsamd-hal/dma"]


Expand All @@ -64,7 +63,6 @@ opt-level = "s"

[[example]]
name = "pwm"
required-features = ["unproven"]

[[example]]
name = "usb_echo"
Expand All @@ -80,15 +78,15 @@ required-features = ["dma"]

[[example]]
name = "pukcc_test"
required-features = ["unproven", "usb"]
required-features = ["usb"]

[[example]]
name = "nvm_dsu"
required-features = ["unproven", "usb"]
required-features = ["usb"]

[[example]]
name = "smart_eeprom"
required-features = ["unproven", "usb"]
required-features = [ "usb"]

[[example]]
name = "i2c"
Expand Down
14 changes: 6 additions & 8 deletions boards/feather_m4/examples/clocking_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

use panic_halt as _;

use core::fmt::Write as _;

use atsamd_hal::{
clock::v2::{
self as clock,
Expand All @@ -15,10 +13,9 @@ use atsamd_hal::{
rtcosc::RtcOsc,
xosc32k::{ControlGainMode, Xosc1k, Xosc32k, Xosc32kBase},
},
ehal::serial::Read as _,
ehal::serial::Write,
embedded_io::{Read, Write},
fugit::RateExtU32,
gpio::{Pins, PA04, PA05},
prelude::*,
rtc::{ClockMode, Rtc},
sercom::{
uart::{self, BaudMode, Flags, Oversampling},
Expand Down Expand Up @@ -142,7 +139,7 @@ mod app {
// In the future, the `Rtc` will take ownership of the `RtcOsc`
let rtc = Rtc::clock_mode(device.RTC, rtc_osc.freq(), &mut mclk);

writeln!(&mut uart as &mut dyn Write<_, Error = _>, "RTIC booted!").unwrap();
writeln!(&mut uart as &mut dyn Write<Error = _>, "RTIC booted!").unwrap();

(
SharedResources { uart, rtc },
Expand All @@ -156,12 +153,13 @@ mod app {
let mut uart = cx.shared.uart;
let mut rtc = cx.shared.rtc;
// Read from `Uart` to clean interrupt flag
let _ = uart.lock(|u| u.read().unwrap());
let mut buf = [0];
let _ = uart.lock(|u| u.read(&mut buf).unwrap());

// Print out `DateTime` coming from `Rtc`
uart.lock(|u| {
writeln!(
u as &mut dyn Write<_, Error = _>,
u as &mut dyn Write<Error = _>,
"{:#?}",
rtc.lock(|r| r.current_time())
)
Expand Down
4 changes: 2 additions & 2 deletions boards/feather_m4/examples/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use pac::Peripherals;

use hal::clock::GenericClockController;
use hal::dmac::{DmaController, PriorityLevel};
use hal::ehal::blocking::i2c::WriteRead;
use hal::prelude::*;
use hal::ehal::i2c::I2c;
use hal::fugit::RateExtU32;
use hal::sercom::i2c;

const LENGTH: usize = 1;
Expand Down
5 changes: 3 additions & 2 deletions boards/feather_m4/examples/neopixel_rainbow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ use panic_semihosting as _;
use bsp::entry;
use hal::clock::GenericClockController;
use hal::delay::Delay;
use hal::ehal::delay::DelayNs;
use hal::pac::{CorePeripherals, Peripherals};
use hal::prelude::*;
use hal::time::Hertz;
use hal::timer::*;
use hal::timer_traits::InterruptDrivenTimer;

use smart_leds::{
hsv::{hsv2rgb, Hsv},
Expand Down Expand Up @@ -62,7 +63,7 @@ fn main() -> ! {
val: 2,
})];
neopixel.write(colors.iter().cloned()).unwrap();
delay.delay_ms(5u8);
delay.delay_ms(5);
}
}
}
2 changes: 1 addition & 1 deletion boards/feather_m4/examples/nvm_dsu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use panic_halt as _;
use panic_semihosting as _;

use bsp::entry;
use ehal::digital::v2::ToggleableOutputPin;
use ehal::digital::StatefulOutputPin;
use hal::clock::GenericClockController;
use hal::dsu::Dsu;
use hal::nvm::{retrieve_bank_size, Bank, Nvm, WriteGranularity, BLOCKSIZE};
Expand Down
2 changes: 1 addition & 1 deletion boards/feather_m4/examples/pukcc_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use panic_halt as _;
use panic_semihosting as _;

use bsp::entry;
use ehal::digital::v2::ToggleableOutputPin;
use ehal::digital::StatefulOutputPin;
use hal::clock::GenericClockController;
use hal::pac::{interrupt, CorePeripherals, Peripherals};
use hal::{pukcc::*, usb::UsbBus};
Expand Down
2 changes: 0 additions & 2 deletions boards/feather_m4/examples/pwm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
#![no_main]

// Pulse Width Modulation
//
// cargo build --features="unproven"

use bsp::hal;
use feather_m4 as bsp;
Expand Down
7 changes: 5 additions & 2 deletions boards/feather_m4/examples/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ use panic_semihosting as _;
use bsp::{entry, periph_alias, pin_alias};
use hal::clock::GenericClockController;
use hal::delay::Delay;
use hal::ehal::delay::DelayNs;
use hal::ehal_nb::serial::Write;
use hal::fugit::RateExtU32;
use hal::nb;
use hal::pac::gclk::genctrl::SRCSELECT_A;
use hal::pac::gclk::pchctrl::GENSELECT_A;
use hal::pac::{CorePeripherals, Peripherals};
use hal::prelude::*;

#[entry]
fn main() -> ! {
Expand Down Expand Up @@ -49,6 +52,6 @@ fn main() -> ! {
for byte in b"Hello, world!" {
nb::block!(uart.write(*byte)).unwrap();
}
delay.delay_ms(1000u16);
delay.delay_ms(1000);
}
}
2 changes: 1 addition & 1 deletion boards/feather_m4/examples/smart_eeprom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use panic_halt as _;
use panic_semihosting as _;

use bsp::entry;
use ehal::digital::v2::ToggleableOutputPin;
use ehal::digital::StatefulOutputPin;
use hal::clock::GenericClockController;
use hal::nvm::{smart_eeprom, Nvm};
use hal::pac::{interrupt, CorePeripherals, Peripherals};
Expand Down
Loading

0 comments on commit 3847d0f

Please sign in to comment.