Skip to content

Commit

Permalink
Add support of SAMD20 in HAL
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaizen Wolf committed Dec 27, 2021
1 parent 5316edf commit d37e970
Show file tree
Hide file tree
Showing 35 changed files with 652 additions and 274 deletions.
13 changes: 7 additions & 6 deletions hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ edition = "2021"
rust-version = "1.56"

[package.metadata.docs.rs]
features = ["samd21g", "samd21g-rt", "unproven", "usb"]
features = ["device", "samd20", "samd20j-rt", "samd21g", "samd21g-rt", "unproven", "usb"]

[dependencies]
bitfield = "0.13"
Expand Down Expand Up @@ -70,6 +70,11 @@ path = "../pac/atsamd11d"
version = "0.11"
optional = true

[dependencies.atsamd20j]
path = "../pac/atsamd20j"
version = "0.1"
optional = true

[dependencies.atsamd21e]
path = "../pac/atsamd21e"
version = "0.11"
Expand All @@ -80,11 +85,6 @@ path = "../pac/atsamd21g"
version = "0.11"
optional = true

[dependencies.atsamd20j]
path = "../pac/atsamd20j"
version = "0.11"
optional = true

[dependencies.atsamd21j]
path = "../pac/atsamd21j"
version = "0.11"
Expand Down Expand Up @@ -211,6 +211,7 @@ same54n = ["atsame54n", "same54", "min-samd51n"]
same54n-rt = ["same54n", "atsame54n/rt"]
same54p = ["atsame54p", "same54", "min-samd51p"]
same54p-rt = ["same54p", "atsame54p/rt"]
min-samd20j = []
min-samd21g = []
min-samd21j = ["min-samd21g"]
min-samd51g = []
Expand Down
3 changes: 2 additions & 1 deletion hal/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# HAL for working with atsamd & atsame devices

This crate provides a type-safe API for working with `samd11`, `samd21`, `samd51`, `same51`, `same53`, and `same54` based devices.
This crate provides a type-safe API for working with `samd11`, `samd20`, `samd21`, `samd51`, `same51`, `same53`, and `same54` based devices.

[![Crates.io](https://img.shields.io/crates/v/atsamd-hal.svg)](https://crates.io/crates/atsamd-hal)
[![Docs](https://docs.rs/atsamd-hal/badge.svg)](https://docs.rs/atsamd-hal/)
Expand All @@ -11,6 +11,7 @@ This crate provides a type-safe API for working with `samd11`, `samd21`, `samd51
## Supported Devices

* `atsamd11c` (via the `samd11c` feature) [[pac]](https://github.com/atsamd-rs/atsamd/tree/master/pac/atsamd11c)
* `atsamd20j` (via the `samd20j` feature) [[pac]](https://github.com/atsamd-rs/atsamd/tree/master/pac/atsamd20j)
* `atsamd21e` (via the `samd21e` feature) [[pac]](https://github.com/atsamd-rs/atsamd/tree/master/pac/atsamd21e)
* `atsamd21g` (via the `samd21g` feature) [[pac]](https://github.com/atsamd-rs/atsamd/tree/master/pac/atsamd21g)
* `atsamd21j` (via the `samd21j` feature) [[pac]](https://github.com/atsamd-rs/atsamd/tree/master/pac/atsamd21j)
Expand Down
6 changes: 3 additions & 3 deletions hal/src/dmac/channel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
//! requires setting a priority level, as well as enabling or disabling
//! interrupt requests (only for the specific channel being initialized).
#![cfg_attr(
not(any(feature = "samd11", feature = "samd21")),
feature = "min-samd51g",
doc = "# Burst Length and FIFO Threshold (SAMD51/SAME5x only)
The transfer burst length can be configured through the
Expand Down Expand Up @@ -145,7 +145,7 @@ impl<Id: ChId, S: Status> Channel<Id, S> {
// Software reset the channel for good measure
self._reset_private();

#[cfg(any(feature = "samd11", feature = "samd21"))]
#[cfg(not(feature = "min-samd51g"))]
// Setup priority level
self.regs.chctrlb.modify(|_, w| w.lvl().bits(lvl as u8));

Expand Down Expand Up @@ -253,7 +253,7 @@ impl<Id: ChId> Channel<Id, Ready> {
// SAFETY: This is actually safe because we are writing the correct enum value
// (imported from the PAC) into the register
unsafe {
#[cfg(any(feature = "samd11", feature = "samd21"))]
#[cfg(not(feature = "min-samd51g"))]
self.regs.chctrlb.modify(|_, w| {
w.trigsrc().bits(trig_src as u8);
w.trigact().bits(trig_act as u8)
Expand Down
16 changes: 8 additions & 8 deletions hal/src/dmac/channel/reg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
//! This module adds a [`RegisterBlock`] struct, which acts as a proxy for the
//! registers a single DMAC [`Channel`](super::Channel) can read/write. Its
//! purpose is to remediate the inadequacies of the PAC. In particular, for
//! SAMD11/SAMD21, the CHID register must be written with the correct channel ID
//! before accessing the channel specific registers. There is a provided
//! `with_chid` method that takes a closure with the register read/write proxies
//! to ensure any read/write to these registers are done in an interrupt-safe
//! way. For SAMD51+, `with_chid` returns the register block which contains the
//! registers owned by a specific channel.
//! SAMD11/SAMD20/SAMD21, the CHID register must be written with the correct
//! channel ID before accessing the channel specific registers. There is a
//! provided `with_chid` method that takes a closure with the register
//! read/write proxies to ensure any read/write to these registers are done in
//! an interrupt-safe way. For SAMD51+, `with_chid` returns the register block
//! which contains the registers owned by a specific channel.
use super::super::dma_controller::ChId;
use core::marker::PhantomData;
Expand All @@ -20,7 +20,7 @@ use crate::pac::{
Peripherals, DMAC,
};

#[cfg(any(feature = "samd11", feature = "samd21"))]
#[cfg(any(feature = "samd11", feature = "samd20", feature = "samd21"))]
use pac::dmac as channel_regs;

#[cfg(feature = "min-samd51g")]
Expand All @@ -45,7 +45,7 @@ pub(super) trait Register<Id: ChId> {
/// the CHID register, then access the channel control registers.
/// If an interrupt were to change the CHID register and not reset it
/// to the expected value, we would be faced with undefined behaviour.
#[cfg(any(feature = "samd11", feature = "samd21"))]
#[cfg(any(feature = "samd11", feature = "samd20", feature = "samd21"))]
#[inline]
fn with_chid<F: FnOnce(&DMAC) -> R, R>(&mut self, fun: F) -> R {
// SAFETY: This method is ONLY safe if the individual channels are GUARANTEED
Expand Down
2 changes: 1 addition & 1 deletion hal/src/dmac/dma_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use modular_bitfield::prelude::*;
use paste::paste;
use seq_macro::seq;

#[cfg(any(feature = "samd11", feature = "samd21"))]
#[cfg(any(feature = "samd11", feature = "samd20", feature = "samd21"))]
pub use crate::pac::dmac::chctrlb::{
LVL_A as PriorityLevel, TRIGACT_A as TriggerAction, TRIGSRC_A as TriggerSource,
};
Expand Down
162 changes: 133 additions & 29 deletions hal/src/gpio/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub type PfF = v2::AlternateF;
/// Peripheral Function G
pub type PfG = v2::AlternateG;
/// Peripheral Function H
#[cfg(any(feature = "samd21", feature = "min-samd51g"))]
#[cfg(any(feature = "samd20", feature = "samd21", feature = "min-samd51g"))]
pub type PfH = v2::AlternateH;
/// Peripheral Function I
#[cfg(feature = "min-samd51g")]
Expand Down Expand Up @@ -256,7 +256,7 @@ where
}

/// Configures the pin to operate with a peripheral
#[cfg(any(feature = "samd21", feature = "min-samd51g"))]
#[cfg(any(feature = "samd20", feature = "samd21", feature = "min-samd51g"))]
#[allow(unused_variables)]
#[inline]
pub fn into_function_h(self, port: &mut Port) -> Pin<I, PfH> {
Expand Down Expand Up @@ -617,9 +617,17 @@ port!([
(PA10, Pa10),
#[cfg(not(feature = "samd11"))]
(PA11, Pa11),
#[cfg(any(feature = "min-samd21g", feature = "min-samd51g"))]
#[cfg(any(
feature = "min-samd20j",
feature = "min-samd21g",
feature = "min-samd51g"
))]
(PA12, Pa12),
#[cfg(any(feature = "min-samd21g", feature = "min-samd51g"))]
#[cfg(any(
feature = "min-samd20j",
feature = "min-samd21g",
feature = "min-samd51g"
))]
(PA13, Pa13),
(PA14, Pa14),
(PA15, Pa15),
Expand All @@ -631,9 +639,17 @@ port!([
(PA18, Pa18),
#[cfg(not(feature = "samd11"))]
(PA19, Pa19),
#[cfg(any(feature = "min-samd21g", feature = "min-samd51g"))]
#[cfg(any(
feature = "min-samd20j",
feature = "min-samd21g",
feature = "min-samd51g"
))]
(PA20, Pa20),
#[cfg(any(feature = "min-samd21g", feature = "min-samd51g"))]
#[cfg(any(
feature = "min-samd20j",
feature = "min-samd21g",
feature = "min-samd51g"
))]
(PA21, Pa21),
#[cfg(not(feature = "samd11c"))]
(PA22, Pa22),
Expand All @@ -643,45 +659,117 @@ port!([
(PA25, Pa25),
#[cfg(not(feature = "samd11"))]
(PA27, Pa27),
#[cfg(any(feature = "samd11", feature = "samd21"))]
#[cfg(any(feature = "samd11", feature = "samd20", feature = "samd21"))]
(PA28, Pa28),
(PA30, Pa30),
(PA31, Pa31),
#[cfg(any(feature = "min-samd21j", feature = "min-samd51j"))]
#[cfg(any(
feature = "min-samd20j",
feature = "min-samd21j",
feature = "min-samd51j"
))]
(PB00, Pb0),
#[cfg(any(feature = "min-samd21j", feature = "min-samd51j"))]
#[cfg(any(
feature = "min-samd20j",
feature = "min-samd21j",
feature = "min-samd51j"
))]
(PB01, Pb1),
#[cfg(any(feature = "min-samd21g", feature = "min-samd51g"))]
#[cfg(any(
feature = "min-samd20j",
feature = "min-samd21g",
feature = "min-samd51g"
))]
(PB02, Pb2),
#[cfg(any(feature = "min-samd21g", feature = "min-samd51g"))]
#[cfg(any(
feature = "min-samd20j",
feature = "min-samd21g",
feature = "min-samd51g"
))]
(PB03, Pb3),
#[cfg(any(feature = "min-samd21j", feature = "min-samd51j"))]
#[cfg(any(
feature = "min-samd20j",
feature = "min-samd21j",
feature = "min-samd51j"
))]
(PB04, Pb4),
#[cfg(any(feature = "min-samd21j", feature = "min-samd51j"))]
#[cfg(any(
feature = "min-samd20j",
feature = "min-samd21j",
feature = "min-samd51j"
))]
(PB05, Pb5),
#[cfg(any(feature = "min-samd21j", feature = "min-samd51j"))]
#[cfg(any(
feature = "min-samd20j",
feature = "min-samd21j",
feature = "min-samd51j"
))]
(PB06, Pb6),
#[cfg(any(feature = "min-samd21j", feature = "min-samd51j"))]
#[cfg(any(
feature = "min-samd20j",
feature = "min-samd21j",
feature = "min-samd51j"
))]
(PB07, Pb7),
#[cfg(any(feature = "min-samd21g", feature = "min-samd51g"))]
#[cfg(any(
feature = "min-samd20j",
feature = "min-samd21g",
feature = "min-samd51g"
))]
(PB08, Pb8),
#[cfg(any(feature = "min-samd21g", feature = "min-samd51g"))]
#[cfg(any(
feature = "min-samd20j",
feature = "min-samd21g",
feature = "min-samd51g"
))]
(PB09, Pb9),
#[cfg(any(feature = "min-samd21g", feature = "min-samd51g"))]
#[cfg(any(
feature = "min-samd20j",
feature = "min-samd21g",
feature = "min-samd51g"
))]
(PB10, Pb10),
#[cfg(any(feature = "min-samd21g", feature = "min-samd51g"))]
#[cfg(any(
feature = "min-samd20j",
feature = "min-samd21g",
feature = "min-samd51g"
))]
(PB11, Pb11),
#[cfg(any(feature = "min-samd21j", feature = "min-samd51j"))]
#[cfg(any(
feature = "min-samd20j",
feature = "min-samd21j",
feature = "min-samd51j"
))]
(PB12, Pb12),
#[cfg(any(feature = "min-samd21j", feature = "min-samd51j"))]
#[cfg(any(
feature = "min-samd20j",
feature = "min-samd21j",
feature = "min-samd51j"
))]
(PB13, Pb13),
#[cfg(any(feature = "min-samd21j", feature = "min-samd51j"))]
#[cfg(any(
feature = "min-samd20j",
feature = "min-samd21j",
feature = "min-samd51j"
))]
(PB14, Pb14),
#[cfg(any(feature = "min-samd21j", feature = "min-samd51j"))]
#[cfg(any(
feature = "min-samd20j",
feature = "min-samd21j",
feature = "min-samd51j"
))]
(PB15, Pb15),
#[cfg(any(feature = "min-samd21j", feature = "min-samd51j"))]
#[cfg(any(
feature = "min-samd20j",
feature = "min-samd21j",
feature = "min-samd51j"
))]
(PB16, Pb16),
#[cfg(any(feature = "min-samd21j", feature = "min-samd51j"))]
#[cfg(any(
feature = "min-samd20j",
feature = "min-samd21j",
feature = "min-samd51j"
))]
(PB17, Pb17),
#[cfg(any(feature = "min-samd51n"))]
(PB18, Pb18),
Expand All @@ -691,9 +779,17 @@ port!([
(PB20, Pb20),
#[cfg(any(feature = "min-samd51n"))]
(PB21, Pb21),
#[cfg(any(feature = "min-samd21g", feature = "min-samd51g"))]
#[cfg(any(
feature = "min-samd20j",
feature = "min-samd21g",
feature = "min-samd51g"
))]
(PB22, Pb22),
#[cfg(any(feature = "min-samd21g", feature = "min-samd51g"))]
#[cfg(any(
feature = "min-samd20j",
feature = "min-samd21g",
feature = "min-samd51g"
))]
(PB23, Pb23),
#[cfg(any(feature = "min-samd51n"))]
(PB24, Pb24),
Expand All @@ -707,9 +803,17 @@ port!([
(PB28, Pb28),
#[cfg(any(feature = "min-samd51p"))]
(PB29, Pb29),
#[cfg(any(feature = "min-samd21j", feature = "min-samd51j"))]
#[cfg(any(
feature = "min-samd20j",
feature = "min-samd21j",
feature = "min-samd51j"
))]
(PB30, Pb30),
#[cfg(any(feature = "min-samd21j", feature = "min-samd51j"))]
#[cfg(any(
feature = "min-samd20j",
feature = "min-samd21j",
feature = "min-samd51j"
))]
(PB31, Pb31),
#[cfg(any(feature = "min-samd51n"))]
(PC00, Pc0),
Expand Down
6 changes: 3 additions & 3 deletions hal/src/gpio/v2/dynpin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub enum DynAlternate {
E,
F,
G,
#[cfg(any(feature = "samd21", feature = "min-samd51g"))]
#[cfg(any(feature = "samd20", feature = "samd21", feature = "min-samd51g"))]
H,
#[cfg(feature = "min-samd51g")]
I,
Expand Down Expand Up @@ -184,7 +184,7 @@ macro_rules! dyn_alternate {
}

dyn_alternate!(B, C, D, E, F, G);
#[cfg(any(feature = "samd21", feature = "min-samd51g"))]
#[cfg(any(feature = "samd20", feature = "samd21", feature = "min-samd51g"))]
dyn_alternate!(H);
#[cfg(feature = "min-samd51g")]
dyn_alternate!(I, J, K, L, M, N);
Expand All @@ -197,7 +197,7 @@ dyn_alternate!(I, J, K, L, M, N);
#[derive(PartialEq, Clone, Copy)]
pub enum DynGroup {
A,
#[cfg(any(feature = "samd21", feature = "min-samd51g"))]
#[cfg(any(feature = "samd20", feature = "samd21", feature = "min-samd51g"))]
B,
#[cfg(feature = "min-samd51n")]
C,
Expand Down
Loading

0 comments on commit d37e970

Please sign in to comment.