Skip to content

Commit

Permalink
Add multiple interrupt sources to single handler; Fix examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jbeaurivage committed Dec 10, 2023
1 parent 4d69adb commit 0dc3123
Show file tree
Hide file tree
Showing 21 changed files with 364 additions and 565 deletions.
33 changes: 10 additions & 23 deletions boards/feather_m0/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,9 @@ optional = true
version = "0.3"
optional = true

[dependencies.cortex-m-interrupt]
version = "0.2.1-git"
optional = true
git = "https://github.com/datdenkikniet/cortex-m-interrupt.git"
rev = "9baa936"

[dev-dependencies]
cortex-m-rtic = "1.1.3"
rtic = { version = "2.0.1", features = ["thumbv6-backend"] }
rtic-monotonics = { version = "1.3.0", features = ["cortex-m-systick", "systick-10khz"] }
fugit = "0.3.6"
cortex-m = "0.7"
usbd-serial = "0.1"
Expand All @@ -58,7 +53,6 @@ panic-semihosting = "0.5"
defmt = "0.3"
defmt-rtt = "0.3"
panic-probe = "0.3"
embassy-executor = { version = "0.3.3", features = ["arch-cortex-m", "defmt", "executor-thread", "executor-interrupt", "nightly"] }

[features]
# ask the HAL to enable atsamd21g support
Expand All @@ -76,8 +70,8 @@ max-channels = ["dma", "atsamd-hal/max-channels"]
# Enable pins for the adalogger SD card reader
adalogger = []
sdmmc = ["embedded-sdmmc", "atsamd-hal/sdmmc"]
rtic = ["atsamd-hal/rtic"]
use_semihosting = []
nightly = []

[profile.dev]
incremental = false
Expand Down Expand Up @@ -146,7 +140,7 @@ required-features = ["adalogger", "usb", "sdmmc", "unproven"]

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

[[example]]
name = "uart"
Expand All @@ -158,31 +152,24 @@ required-features = ["dma"]

[[example]]
name = "async_dmac"
required-features = ["dma", "atsamd-hal/async", "atsamd-hal/rtic", "cortex-m-interrupt"]

[[example]]
name = "dmac_embassy"
required-features = ["dma", "atsamd-hal/async", "cortex-m-interrupt"]
required-features = ["dma", "atsamd-hal/async", "nightly"]

[[example]]
name = "async_timer"
required-features = ["atsamd-hal/async", "atsamd-hal/rtic", "cortex-m-interrupt"]
required-features = ["atsamd-hal/async", "nightly"]

[[example]]
name = "async_eic"
required-features = ["atsamd-hal/async", "atsamd-hal/rtic", "cortex-m-interrupt"]
required-features = ["atsamd-hal/async", "nightly"]

[[example]]
name = "async_i2c"
required-features = ["dma", "atsamd-hal/async", "atsamd-hal/rtic", "cortex-m-interrupt"]
required-features = ["dma", "atsamd-hal/async", "nightly"]

[[example]]
name = "async_spi"
required-features = ["dma", "atsamd-hal/async", "atsamd-hal/rtic", "cortex-m-interrupt"]
required-features = ["dma", "atsamd-hal/async", "nightly"]

[[example]]
name = "async_uart"
required-features = ["dma", "atsamd-hal/async", "atsamd-hal/rtic", "cortex-m-interrupt"]

[patch.crates-io]
cortex-m-rtic = { git = "https://github.com/rtic-rs/cortex-m-rtic.git", branch = "async-2022" }
required-features = ["dma", "atsamd-hal/async", "nightly"]
40 changes: 11 additions & 29 deletions boards/feather_m0/examples/async_dmac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]

use defmt_rtt as _;
use panic_probe as _;
Expand All @@ -13,20 +14,15 @@ atsamd_hal::bind_interrupts!(struct Irqs {

#[rtic::app(device = bsp::pac, dispatchers = [I2S])]
mod app {
use bsp::{hal, pac};
use bsp::hal;
use feather_m0 as bsp;
use hal::{
clock::{enable_internal_32kosc, ClockGenId, ClockSource, GenericClockController},
clock::GenericClockController,
dmac::{
self, Ch0, Channel, DmaController, PriorityLevel, ReadyFuture, TriggerAction,
TriggerSource,
Ch0, Channel, DmaController, PriorityLevel, ReadyFuture, TriggerAction, TriggerSource,
},
rtc::{Count32Mode, Rtc},
};

#[monotonic(binds = RTC, default = true)]
type Monotonic = Rtc<Count32Mode>;

#[shared]
struct Shared {}

Expand All @@ -36,34 +32,20 @@ mod app {
}

#[init]
fn init(cx: init::Context) -> (Shared, Local, init::Monotonics) {
fn init(cx: init::Context) -> (Shared, Local) {
let mut peripherals = cx.device;
let _core = cx.core;

let mut clocks = GenericClockController::with_external_32kosc(
let _clocks = GenericClockController::with_external_32kosc(
peripherals.GCLK,
&mut peripherals.PM,
&mut peripherals.SYSCTRL,
&mut peripherals.NVMCTRL,
);

enable_internal_32kosc(&mut peripherals.SYSCTRL);
let timer_clock = clocks
.configure_gclk_divider_and_source(ClockGenId::GCLK2, 1, ClockSource::OSC32K, false)
.unwrap();
clocks.configure_standby(ClockGenId::GCLK2, true);

// Setup RTC monotonic
let rtc_clock = clocks.rtc(&timer_clock).unwrap();
let rtc = Rtc::count32_mode(peripherals.RTC, rtc_clock.freq(), &mut peripherals.PM);

// Initialize DMA Controller
let dmac = DmaController::init(peripherals.DMAC, &mut peripherals.PM);
// Get handle to IRQ
let dmac_irq = dmac::Interrupts::new(cortex_m_interrupt::take_nvic_interrupt!(
pac::Interrupt::DMAC,
2
));

// Turn dmac into an async controller
let mut dmac = dmac.into_future(crate::Irqs);
// Get individual handles to DMA channels
Expand All @@ -73,15 +55,15 @@ mod app {
let channel = channels.0.init(PriorityLevel::LVL0);

async_task::spawn().ok();
(Shared {}, Local { channel }, init::Monotonics(rtc))
(Shared {}, Local { channel })
}

#[task(local = [channel])]
async fn async_task(cx: async_task::Context) {
let async_task::LocalResources { channel } = cx.local;
let channel = cx.local.channel;

let mut source = [0xff; 50];
let mut dest = [0x0; 50];
let mut source = [0xff; 500];
let mut dest = [0x0; 500];

defmt::info!(
"Launching a DMA transfer.\n\tSource: {}\n\tDestination: {}",
Expand Down
57 changes: 26 additions & 31 deletions boards/feather_m0/examples/async_eic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,38 @@
use defmt_rtt as _;
use panic_probe as _;

use bsp::{hal, pin_alias};
use feather_m0 as bsp;
use hal::{
clock::{enable_internal_32kosc, ClockGenId, ClockSource, GenericClockController},
ehal::digital::v2::ToggleableOutputPin,
eic::{
pin::{ExtInt2, Sense},
EIC,
},
gpio::{pin::PA18, Pin, PullUpInterrupt},
rtc::{Count32Mode, Rtc},
};

atsamd_hal::bind_interrupts!(struct Irqs {
EIC => atsamd_hal::eic::InterruptHandler;
});

#[rtic::app(device = bsp::pac, dispatchers = [I2S])]
mod app {
use bsp::{hal, pac, pin_alias};
use feather_m0 as bsp;
use hal::{
clock::{enable_internal_32kosc, ClockGenId, ClockSource, GenericClockController},
ehal::digital::v2::ToggleableOutputPin,
eic::{
pin::{ExtInt2, Sense},
EIC,
},
gpio::{pin::PA18, Pin, PullUpInterrupt},
rtc::{Count32Mode, Rtc},
};

#[monotonic(binds = RTC, default = true)]
type Monotonic = Rtc<Count32Mode>;
use super::*;

#[shared]
struct Shared {}

#[local]
struct Local {
extint: ExtInt2<Pin<PA18, PullUpInterrupt>, hal::pac::Interrupt>,
extint: ExtInt2<Pin<PA18, PullUpInterrupt>>,
red_led: bsp::RedLed,
}

#[init]
fn init(cx: init::Context) -> (Shared, Local, init::Monotonics) {
fn init(cx: init::Context) -> (Shared, Local) {
let mut peripherals = cx.device;
let _core = cx.core;

Expand All @@ -46,33 +49,25 @@ mod app {
let pins = bsp::Pins::new(peripherals.PORT);
let red_led: bsp::RedLed = pin_alias!(pins.red_led).into();

let timer_clock = clocks
let internal_clock = clocks
.configure_gclk_divider_and_source(ClockGenId::GCLK2, 1, ClockSource::OSC32K, false)
.unwrap();
clocks.configure_standby(ClockGenId::GCLK2, true);

let eic_irq = cortex_m_interrupt::take_nvic_interrupt!(pac::Interrupt::EIC, 2);
// tc4_irq.set_priority(2);

enable_internal_32kosc(&mut peripherals.SYSCTRL);

// Setup RTC monotonic
let rtc_clock = clocks.rtc(&timer_clock).unwrap();
let rtc = Rtc::count32_mode(peripherals.RTC, rtc_clock.freq(), &mut peripherals.PM);

// configure a clock for the EIC peripheral
let gclk0 = clocks.gclk0();
let eic_clock = clocks.eic(&gclk0).unwrap();
// Configure a clock for the EIC peripheral
let gclk2 = clocks.gclk2();
let eic_clock = clocks.eic(&gclk2).unwrap();

let mut eic =
EIC::init(&mut peripherals.PM, eic_clock, peripherals.EIC).into_future(eic_irq);
let mut eic = EIC::init(&mut peripherals.PM, eic_clock, peripherals.EIC).into_future(Irqs);
let button: Pin<_, PullUpInterrupt> = pins.d10.into();
let mut extint = ExtInt2::new(button, &mut eic);
extint.enable_interrupt_wake();

async_task::spawn().ok();

(Shared {}, Local { extint, red_led }, init::Monotonics(rtc))
(Shared {}, Local { extint, red_led })
}

#[task(local = [extint, red_led])]
Expand Down
43 changes: 16 additions & 27 deletions boards/feather_m0/examples/async_i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,19 @@
use defmt_rtt as _;
use panic_probe as _;

use atsamd_hal::sercom::Sercom3;
use bsp::hal;
use feather_m0 as bsp;
use fugit::MillisDuration;
use hal::{
clock::GenericClockController,
dmac::{Ch0, DmaController, PriorityLevel},
prelude::*,
sercom::{
i2c::{self, Config, I2cFutureDma},
Sercom3,
},
};
use rtic_monotonics::systick::Systick;

atsamd_hal::bind_interrupts!(struct Irqs {
SERCOM3 => atsamd_hal::sercom::i2c::InterruptHandler<Sercom3>;
Expand All @@ -15,19 +27,6 @@ atsamd_hal::bind_interrupts!(struct Irqs {
#[rtic::app(device = bsp::pac, dispatchers = [I2S])]
mod app {
use super::*;
use bsp::hal;
use feather_m0 as bsp;
use fugit::MillisDuration;
use hal::{
clock::{enable_internal_32kosc, ClockGenId, ClockSource, GenericClockController},
dmac::{Ch0, DmaController, PriorityLevel},
prelude::*,
rtc::{Count32Mode, Rtc},
sercom::i2c::{self, Config, I2cFutureDma},
};

#[monotonic(binds = RTC, default = true)]
type Monotonic = Rtc<Count32Mode>;

#[shared]
struct Shared {}
Expand All @@ -38,7 +37,7 @@ mod app {
}

#[init]
fn init(cx: init::Context) -> (Shared, Local, init::Monotonics) {
fn init(cx: init::Context) -> (Shared, Local) {
let mut peripherals = cx.device;
let _core = cx.core;

Expand All @@ -54,16 +53,6 @@ mod app {
// Take SDA and SCL
let (sda, scl) = (pins.sda, pins.scl);

enable_internal_32kosc(&mut peripherals.SYSCTRL);
let timer_clock = clocks
.configure_gclk_divider_and_source(ClockGenId::GCLK2, 1, ClockSource::OSC32K, false)
.unwrap();
clocks.configure_standby(ClockGenId::GCLK2, true);

// Setup RTC monotonic
let rtc_clock = clocks.rtc(&timer_clock).unwrap();
let rtc = Rtc::count32_mode(peripherals.RTC, rtc_clock.freq(), &mut peripherals.PM);

// Initialize DMA Controller
let dmac = DmaController::init(peripherals.DMAC, &mut peripherals.PM);

Expand Down Expand Up @@ -91,7 +80,7 @@ mod app {

async_task::spawn().ok();

(Shared {}, Local { i2c }, init::Monotonics(rtc))
(Shared {}, Local { i2c })
}

#[task(local = [i2c])]
Expand All @@ -107,7 +96,7 @@ mod app {
let mut buffer = [0xff; 4];
i2c.read(0x76, &mut buffer).await.unwrap();
defmt::info!("Read buffer: {:#x}", buffer);
crate::app::monotonics::delay(MillisDuration::<u32>::from_ticks(500).convert()).await;
Systick::delay(MillisDuration::<u32>::from_ticks(500).convert()).await;
}
}
}
Loading

0 comments on commit 0dc3123

Please sign in to comment.