2021-03-29 04:11:32 +02:00
|
|
|
#![no_std]
|
2022-11-21 23:31:31 +01:00
|
|
|
#![cfg_attr(feature = "nightly", feature(async_fn_in_trait, impl_trait_projections))]
|
2021-03-29 04:11:32 +02:00
|
|
|
|
|
|
|
// This mod MUST go first, so that the others see its macros.
|
|
|
|
pub(crate) mod fmt;
|
|
|
|
|
2022-12-10 12:57:45 +01:00
|
|
|
#[cfg(feature = "critical-section-impl")]
|
2022-12-10 08:26:35 +01:00
|
|
|
mod critical_section_impl;
|
2022-12-10 12:57:45 +01:00
|
|
|
|
2022-09-23 06:38:47 +02:00
|
|
|
mod intrinsics;
|
|
|
|
|
2022-11-15 16:12:07 +01:00
|
|
|
pub mod adc;
|
2023-04-26 22:15:10 +02:00
|
|
|
pub mod clocks;
|
2021-03-29 04:11:32 +02:00
|
|
|
pub mod dma;
|
2023-04-26 22:15:10 +02:00
|
|
|
pub mod flash;
|
2023-04-19 01:57:37 +02:00
|
|
|
mod float;
|
2021-03-29 04:11:32 +02:00
|
|
|
pub mod gpio;
|
2022-08-19 11:51:42 +02:00
|
|
|
pub mod i2c;
|
2023-04-26 22:15:10 +02:00
|
|
|
pub mod multicore;
|
2023-04-21 00:57:28 +02:00
|
|
|
pub mod pwm;
|
2023-04-26 22:15:10 +02:00
|
|
|
mod reset;
|
2022-09-23 06:38:47 +02:00
|
|
|
pub mod rom_data;
|
2022-09-16 06:45:27 +02:00
|
|
|
pub mod rtc;
|
2021-06-25 06:23:46 +02:00
|
|
|
pub mod spi;
|
2022-09-09 12:45:03 +02:00
|
|
|
#[cfg(feature = "time-driver")]
|
2021-07-12 02:45:42 +02:00
|
|
|
pub mod timer;
|
2021-03-29 04:11:32 +02:00
|
|
|
pub mod uart;
|
2022-08-24 23:46:07 +02:00
|
|
|
#[cfg(feature = "nightly")]
|
|
|
|
pub mod usb;
|
2022-12-24 02:51:06 +01:00
|
|
|
pub mod watchdog;
|
2021-06-25 03:38:03 +02:00
|
|
|
|
2023-04-26 22:15:10 +02:00
|
|
|
// PIO
|
|
|
|
// TODO: move `pio_instr_util` and `relocate` to inside `pio`
|
|
|
|
pub mod pio;
|
|
|
|
pub mod pio_instr_util;
|
2023-07-28 18:45:57 +02:00
|
|
|
pub(crate) mod relocate;
|
2022-06-11 05:08:57 +02:00
|
|
|
|
2023-04-26 22:15:10 +02:00
|
|
|
// Reexports
|
2023-07-28 13:23:22 +02:00
|
|
|
pub use embassy_hal_internal::{into_ref, Peripheral, PeripheralRef};
|
2022-06-11 05:08:57 +02:00
|
|
|
#[cfg(feature = "unstable-pac")]
|
2023-04-16 23:59:26 +02:00
|
|
|
pub use rp_pac as pac;
|
2022-06-11 05:08:57 +02:00
|
|
|
#[cfg(not(feature = "unstable-pac"))]
|
2023-04-16 23:59:26 +02:00
|
|
|
pub(crate) use rp_pac as pac;
|
2022-06-11 05:08:57 +02:00
|
|
|
|
2023-06-08 18:00:19 +02:00
|
|
|
#[cfg(feature = "rt")]
|
2023-06-08 16:39:05 +02:00
|
|
|
pub use crate::pac::NVIC_PRIO_BITS;
|
|
|
|
|
2023-07-28 13:23:22 +02:00
|
|
|
embassy_hal_internal::interrupt_mod!(
|
2023-06-08 16:08:40 +02:00
|
|
|
TIMER_IRQ_0,
|
|
|
|
TIMER_IRQ_1,
|
|
|
|
TIMER_IRQ_2,
|
|
|
|
TIMER_IRQ_3,
|
|
|
|
PWM_IRQ_WRAP,
|
|
|
|
USBCTRL_IRQ,
|
|
|
|
XIP_IRQ,
|
|
|
|
PIO0_IRQ_0,
|
|
|
|
PIO0_IRQ_1,
|
|
|
|
PIO1_IRQ_0,
|
|
|
|
PIO1_IRQ_1,
|
|
|
|
DMA_IRQ_0,
|
|
|
|
DMA_IRQ_1,
|
|
|
|
IO_IRQ_BANK0,
|
|
|
|
IO_IRQ_QSPI,
|
|
|
|
SIO_IRQ_PROC0,
|
|
|
|
SIO_IRQ_PROC1,
|
|
|
|
CLOCKS_IRQ,
|
|
|
|
SPI0_IRQ,
|
|
|
|
SPI1_IRQ,
|
|
|
|
UART0_IRQ,
|
|
|
|
UART1_IRQ,
|
|
|
|
ADC_IRQ_FIFO,
|
|
|
|
I2C0_IRQ,
|
|
|
|
I2C1_IRQ,
|
|
|
|
RTC_IRQ,
|
|
|
|
SWI_IRQ_0,
|
|
|
|
SWI_IRQ_1,
|
|
|
|
SWI_IRQ_2,
|
|
|
|
SWI_IRQ_3,
|
|
|
|
SWI_IRQ_4,
|
|
|
|
SWI_IRQ_5,
|
|
|
|
);
|
|
|
|
|
|
|
|
/// Macro to bind interrupts to handlers.
|
|
|
|
///
|
|
|
|
/// This defines the right interrupt handlers, and creates a unit struct (like `struct Irqs;`)
|
|
|
|
/// and implements the right [`Binding`]s for it. You can pass this struct to drivers to
|
|
|
|
/// prove at compile-time that the right interrupts have been bound.
|
2023-07-28 13:23:22 +02:00
|
|
|
// developer note: this macro can't be in `embassy-hal-internal` due to the use of `$crate`.
|
2023-06-08 16:08:40 +02:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! bind_interrupts {
|
|
|
|
($vis:vis struct $name:ident { $($irq:ident => $($handler:ty),*;)* }) => {
|
|
|
|
$vis struct $name;
|
|
|
|
|
|
|
|
$(
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
#[no_mangle]
|
|
|
|
unsafe extern "C" fn $irq() {
|
|
|
|
$(
|
|
|
|
<$handler as $crate::interrupt::typelevel::Handler<$crate::interrupt::typelevel::$irq>>::on_interrupt();
|
|
|
|
)*
|
|
|
|
}
|
|
|
|
|
|
|
|
$(
|
|
|
|
unsafe impl $crate::interrupt::typelevel::Binding<$crate::interrupt::typelevel::$irq, $handler> for $name {}
|
|
|
|
)*
|
|
|
|
)*
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-07-28 13:23:22 +02:00
|
|
|
embassy_hal_internal::peripherals! {
|
2021-03-29 04:11:32 +02:00
|
|
|
PIN_0,
|
|
|
|
PIN_1,
|
|
|
|
PIN_2,
|
|
|
|
PIN_3,
|
|
|
|
PI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_CH5,
|
|
|
|
PWM_CH6,
|
|
|
|
PWM_CH7,
|
|
|
|
|
2022-08-24 23:46:07 +02:00
|
|
|
USB,
|
2022-09-16 06:45:27 +02:00
|
|
|
|
|
|
|
RTC,
|
2022-10-26 10:01:52 +02:00
|
|
|
|
|
|
|
FLASH,
|
2022-11-15 16:12:07 +01:00
|
|
|
|
|
|
|
ADC,
|
2023-07-21 21:31:44 +02:00
|
|
|
ADC_TEMP_SENSOR,
|
2022-12-13 13:49:51 +01:00
|
|
|
|
|
|
|
CORE1,
|
2022-12-13 13:55:23 +01:00
|
|
|
|
2022-07-27 22:45:46 +02:00
|
|
|
PIO0,
|
|
|
|
PIO1,
|
2022-12-24 02:51:06 +01:00
|
|
|
|
|
|
|
WATCHDOG,
|
2021-03-29 04:11:32 +02:00
|
|
|
}
|
2021-05-12 01:57:01 +02:00
|
|
|
|
2023-05-09 18:36:17 +02:00
|
|
|
macro_rules! select_bootloader {
|
|
|
|
( $( $feature:literal => $loader:ident, )+ default => $default:ident ) => {
|
|
|
|
$(
|
|
|
|
#[cfg(feature = $feature)]
|
|
|
|
#[link_section = ".boot2"]
|
|
|
|
#[used]
|
|
|
|
static BOOT2: [u8; 256] = rp2040_boot2::$loader;
|
|
|
|
)*
|
|
|
|
|
|
|
|
#[cfg(not(any( $( feature = $feature),* )))]
|
|
|
|
#[link_section = ".boot2"]
|
|
|
|
#[used]
|
|
|
|
static BOOT2: [u8; 256] = rp2040_boot2::$default;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
select_bootloader! {
|
|
|
|
"boot2-at25sf128a" => BOOT_LOADER_AT25SF128A,
|
|
|
|
"boot2-gd25q64cs" => BOOT_LOADER_GD25Q64CS,
|
|
|
|
"boot2-generic-03h" => BOOT_LOADER_GENERIC_03H,
|
|
|
|
"boot2-is25lp080" => BOOT_LOADER_IS25LP080,
|
|
|
|
"boot2-ram-memcpy" => BOOT_LOADER_RAM_MEMCPY,
|
|
|
|
"boot2-w25q080" => BOOT_LOADER_W25Q080,
|
|
|
|
"boot2-w25x10cl" => BOOT_LOADER_W25X10CL,
|
|
|
|
default => BOOT_LOADER_W25Q080
|
|
|
|
}
|
2021-05-12 01:57:01 +02:00
|
|
|
|
2023-07-20 16:57:54 +02:00
|
|
|
/// Installs a stack guard for the CORE0 stack in MPU region 0.
|
|
|
|
/// Will fail if the MPU is already confgigured. This function requires
|
|
|
|
/// a `_stack_end` symbol to be defined by the linker script, and expexcts
|
|
|
|
/// `_stack_end` to be located at the lowest address (largest depth) of
|
|
|
|
/// the stack.
|
|
|
|
///
|
|
|
|
/// This method can *only* set up stack guards on the currently
|
|
|
|
/// executing core. Stack guards for CORE1 are set up automatically,
|
|
|
|
/// only CORE0 should ever use this.
|
|
|
|
///
|
|
|
|
/// # Usage
|
|
|
|
///
|
|
|
|
/// ```no_run
|
|
|
|
/// #![feature(type_alias_impl_trait)]
|
|
|
|
/// use embassy_rp::install_core0_stack_guard;
|
|
|
|
/// use embassy_executor::{Executor, Spawner};
|
|
|
|
///
|
|
|
|
/// #[embassy_executor::main]
|
|
|
|
/// async fn main(_spawner: Spawner) {
|
|
|
|
/// // set up by the linker as follows:
|
|
|
|
/// //
|
|
|
|
/// // MEMORY {
|
|
|
|
/// // STACK0: ORIGIN = 0x20040000, LENGTH = 4K
|
|
|
|
/// // }
|
|
|
|
/// //
|
|
|
|
/// // _stack_end = ORIGIN(STACK0);
|
|
|
|
/// // _stack_start = _stack_end + LENGTH(STACK0);
|
|
|
|
/// //
|
|
|
|
/// install_core0_stack_guard().expect("MPU already configured");
|
|
|
|
/// let p = embassy_rp::init(Default::default());
|
|
|
|
///
|
|
|
|
/// // ...
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn install_core0_stack_guard() -> Result<(), ()> {
|
|
|
|
extern "C" {
|
|
|
|
static mut _stack_end: usize;
|
|
|
|
}
|
|
|
|
unsafe { install_stack_guard(&mut _stack_end as *mut usize) }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn install_stack_guard(stack_bottom: *mut usize) -> Result<(), ()> {
|
|
|
|
let core = unsafe { cortex_m::Peripherals::steal() };
|
|
|
|
|
|
|
|
// Fail if MPU is already configured
|
|
|
|
if core.MPU.ctrl.read() != 0 {
|
|
|
|
return Err(());
|
|
|
|
}
|
|
|
|
|
|
|
|
// The minimum we can protect is 32 bytes on a 32 byte boundary, so round up which will
|
|
|
|
// just shorten the valid stack range a tad.
|
|
|
|
let addr = (stack_bottom as u32 + 31) & !31;
|
|
|
|
// Mask is 1 bit per 32 bytes of the 256 byte range... clear the bit for the segment we want
|
|
|
|
let subregion_select = 0xff ^ (1 << ((addr >> 5) & 7));
|
|
|
|
unsafe {
|
|
|
|
core.MPU.ctrl.write(5); // enable mpu with background default map
|
|
|
|
core.MPU.rbar.write((addr & !0xff) | (1 << 4)); // set address and update RNR
|
|
|
|
core.MPU.rasr.write(
|
|
|
|
1 // enable region
|
|
|
|
| (0x7 << 1) // size 2^(7 + 1) = 256
|
|
|
|
| (subregion_select << 8)
|
|
|
|
| 0x10000000, // XN = disable instruction fetch; no other bits means no permissions
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-05-12 01:57:01 +02:00
|
|
|
pub mod config {
|
2023-05-07 19:49:48 +02:00
|
|
|
use crate::clocks::ClockConfig;
|
|
|
|
|
2021-05-12 01:57:01 +02:00
|
|
|
#[non_exhaustive]
|
2023-05-07 19:49:48 +02:00
|
|
|
pub struct Config {
|
|
|
|
pub clocks: ClockConfig,
|
|
|
|
}
|
2021-05-12 01:57:01 +02:00
|
|
|
|
|
|
|
impl Default for Config {
|
|
|
|
fn default() -> Self {
|
2023-05-07 19:49:48 +02:00
|
|
|
Self {
|
|
|
|
clocks: ClockConfig::crystal(12_000_000),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Config {
|
|
|
|
pub fn new(clocks: ClockConfig) -> Self {
|
|
|
|
Self { clocks }
|
2021-05-12 01:57:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-07 19:49:48 +02:00
|
|
|
pub fn init(config: config::Config) -> Peripherals {
|
2021-05-12 01:57:01 +02:00
|
|
|
// Do this first, so that it panics if user is calling `init` a second time
|
|
|
|
// before doing anything important.
|
|
|
|
let peripherals = Peripherals::take();
|
|
|
|
|
|
|
|
unsafe {
|
2023-05-07 19:49:48 +02:00
|
|
|
clocks::init(config.clocks);
|
2022-09-09 12:45:03 +02:00
|
|
|
#[cfg(feature = "time-driver")]
|
2021-07-12 02:45:42 +02:00
|
|
|
timer::init();
|
2022-08-23 12:28:17 +02:00
|
|
|
dma::init();
|
2023-05-02 08:39:05 +02:00
|
|
|
gpio::init();
|
2021-05-12 01:57:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
peripherals
|
|
|
|
}
|
2022-08-24 23:46:07 +02:00
|
|
|
|
|
|
|
/// Extension trait for PAC regs, adding atomic xor/bitset/bitclear writes.
|
|
|
|
trait RegExt<T: Copy> {
|
2023-06-16 01:32:18 +02:00
|
|
|
fn write_xor<R>(&self, f: impl FnOnce(&mut T) -> R) -> R;
|
|
|
|
fn write_set<R>(&self, f: impl FnOnce(&mut T) -> R) -> R;
|
|
|
|
fn write_clear<R>(&self, f: impl FnOnce(&mut T) -> R) -> R;
|
2022-08-24 23:46:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Default + Copy, A: pac::common::Write> RegExt<T> for pac::common::Reg<T, A> {
|
2023-06-16 01:32:18 +02:00
|
|
|
fn write_xor<R>(&self, f: impl FnOnce(&mut T) -> R) -> R {
|
2022-08-24 23:46:07 +02:00
|
|
|
let mut val = Default::default();
|
|
|
|
let res = f(&mut val);
|
2023-06-16 01:32:18 +02:00
|
|
|
unsafe {
|
|
|
|
let ptr = (self.as_ptr() as *mut u8).add(0x1000) as *mut T;
|
|
|
|
ptr.write_volatile(val);
|
|
|
|
}
|
2022-08-24 23:46:07 +02:00
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2023-06-16 01:32:18 +02:00
|
|
|
fn write_set<R>(&self, f: impl FnOnce(&mut T) -> R) -> R {
|
2022-08-24 23:46:07 +02:00
|
|
|
let mut val = Default::default();
|
|
|
|
let res = f(&mut val);
|
2023-06-16 01:32:18 +02:00
|
|
|
unsafe {
|
|
|
|
let ptr = (self.as_ptr() as *mut u8).add(0x2000) as *mut T;
|
|
|
|
ptr.write_volatile(val);
|
|
|
|
}
|
2022-08-24 23:46:07 +02:00
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2023-06-16 01:32:18 +02:00
|
|
|
fn write_clear<R>(&self, f: impl FnOnce(&mut T) -> R) -> R {
|
2022-08-24 23:46:07 +02:00
|
|
|
let mut val = Default::default();
|
|
|
|
let res = f(&mut val);
|
2023-06-16 01:32:18 +02:00
|
|
|
unsafe {
|
|
|
|
let ptr = (self.as_ptr() as *mut u8).add(0x3000) as *mut T;
|
|
|
|
ptr.write_volatile(val);
|
|
|
|
}
|
2022-08-24 23:46:07 +02:00
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|