diff options
Diffstat (limited to 'embassy-mcxa/src/lib.rs')
| -rw-r--r-- | embassy-mcxa/src/lib.rs | 448 |
1 files changed, 448 insertions, 0 deletions
diff --git a/embassy-mcxa/src/lib.rs b/embassy-mcxa/src/lib.rs new file mode 100644 index 000000000..6383353db --- /dev/null +++ b/embassy-mcxa/src/lib.rs | |||
| @@ -0,0 +1,448 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![allow(async_fn_in_trait)] | ||
| 3 | #![doc = include_str!("../README.md")] | ||
| 4 | |||
| 5 | // //! ## Feature flags | ||
| 6 | // #![doc = document_features::document_features!(feature_label = r#"<span class="stab portability"><code>{feature}</code></span>"#)] | ||
| 7 | |||
| 8 | pub mod clocks; // still provide clock helpers | ||
| 9 | pub mod dma; | ||
| 10 | pub mod gpio; | ||
| 11 | |||
| 12 | pub mod adc; | ||
| 13 | pub mod clkout; | ||
| 14 | pub mod config; | ||
| 15 | pub mod crc; | ||
| 16 | pub mod i2c; | ||
| 17 | pub mod interrupt; | ||
| 18 | pub mod lpuart; | ||
| 19 | pub mod ostimer; | ||
| 20 | pub mod reset_reason; | ||
| 21 | pub mod rtc; | ||
| 22 | pub mod trng; | ||
| 23 | |||
| 24 | use crate::interrupt::InterruptExt; | ||
| 25 | pub use crate::pac::NVIC_PRIO_BITS; | ||
| 26 | |||
| 27 | #[rustfmt::skip] | ||
| 28 | embassy_hal_internal::peripherals!( | ||
| 29 | ADC0, | ||
| 30 | ADC1, | ||
| 31 | ADC2, | ||
| 32 | ADC3, | ||
| 33 | |||
| 34 | AOI0, | ||
| 35 | AOI1, | ||
| 36 | |||
| 37 | CAN0, | ||
| 38 | CAN1, | ||
| 39 | |||
| 40 | CDOG0, | ||
| 41 | CDOG1, | ||
| 42 | |||
| 43 | // CLKOUT is not specifically a peripheral (it's part of SYSCON), | ||
| 44 | // but we still want it to be a singleton. | ||
| 45 | CLKOUT, | ||
| 46 | |||
| 47 | CMC, | ||
| 48 | CMP0, | ||
| 49 | CMP1, | ||
| 50 | CRC0, | ||
| 51 | |||
| 52 | CTIMER0, | ||
| 53 | CTIMER1, | ||
| 54 | CTIMER2, | ||
| 55 | CTIMER3, | ||
| 56 | CTIMER4, | ||
| 57 | |||
| 58 | DBGMAILBOX, | ||
| 59 | DMA0, | ||
| 60 | DMA_CH0, | ||
| 61 | DMA_CH1, | ||
| 62 | DMA_CH2, | ||
| 63 | DMA_CH3, | ||
| 64 | DMA_CH4, | ||
| 65 | DMA_CH5, | ||
| 66 | DMA_CH6, | ||
| 67 | DMA_CH7, | ||
| 68 | EDMA0_TCD0, | ||
| 69 | EIM0, | ||
| 70 | EQDC0, | ||
| 71 | EQDC1, | ||
| 72 | ERM0, | ||
| 73 | FLEXIO0, | ||
| 74 | FLEXPWM0, | ||
| 75 | FLEXPWM1, | ||
| 76 | FMC0, | ||
| 77 | FMU0, | ||
| 78 | FREQME0, | ||
| 79 | GLIKEY0, | ||
| 80 | |||
| 81 | GPIO0, | ||
| 82 | GPIO1, | ||
| 83 | GPIO2, | ||
| 84 | GPIO3, | ||
| 85 | GPIO4, | ||
| 86 | |||
| 87 | I3C0, | ||
| 88 | INPUTMUX0, | ||
| 89 | |||
| 90 | LPI2C0, | ||
| 91 | LPI2C1, | ||
| 92 | LPI2C2, | ||
| 93 | LPI2C3, | ||
| 94 | |||
| 95 | LPSPI0, | ||
| 96 | LPSPI1, | ||
| 97 | |||
| 98 | LPTMR0, | ||
| 99 | |||
| 100 | LPUART0, | ||
| 101 | LPUART1, | ||
| 102 | LPUART2, | ||
| 103 | LPUART3, | ||
| 104 | LPUART4, | ||
| 105 | LPUART5, | ||
| 106 | |||
| 107 | MAU0, | ||
| 108 | MBC0, | ||
| 109 | MRCC0, | ||
| 110 | OPAMP0, | ||
| 111 | |||
| 112 | #[cfg(not(feature = "time"))] | ||
| 113 | OSTIMER0, | ||
| 114 | |||
| 115 | P0_0, | ||
| 116 | P0_1, | ||
| 117 | P0_2, | ||
| 118 | P0_3, | ||
| 119 | P0_4, | ||
| 120 | P0_5, | ||
| 121 | P0_6, | ||
| 122 | P0_7, | ||
| 123 | P0_8, | ||
| 124 | P0_9, | ||
| 125 | P0_10, | ||
| 126 | P0_11, | ||
| 127 | P0_12, | ||
| 128 | P0_13, | ||
| 129 | P0_14, | ||
| 130 | P0_15, | ||
| 131 | P0_16, | ||
| 132 | P0_17, | ||
| 133 | P0_18, | ||
| 134 | P0_19, | ||
| 135 | P0_20, | ||
| 136 | P0_21, | ||
| 137 | P0_22, | ||
| 138 | P0_23, | ||
| 139 | P0_24, | ||
| 140 | P0_25, | ||
| 141 | P0_26, | ||
| 142 | P0_27, | ||
| 143 | P0_28, | ||
| 144 | P0_29, | ||
| 145 | P0_30, | ||
| 146 | P0_31, | ||
| 147 | |||
| 148 | P1_0, | ||
| 149 | P1_1, | ||
| 150 | P1_2, | ||
| 151 | P1_3, | ||
| 152 | P1_4, | ||
| 153 | P1_5, | ||
| 154 | P1_6, | ||
| 155 | P1_7, | ||
| 156 | P1_8, | ||
| 157 | P1_9, | ||
| 158 | P1_10, | ||
| 159 | P1_11, | ||
| 160 | P1_12, | ||
| 161 | P1_13, | ||
| 162 | P1_14, | ||
| 163 | P1_15, | ||
| 164 | P1_16, | ||
| 165 | P1_17, | ||
| 166 | P1_18, | ||
| 167 | P1_19, | ||
| 168 | P1_20, | ||
| 169 | P1_21, | ||
| 170 | P1_22, | ||
| 171 | P1_23, | ||
| 172 | P1_24, | ||
| 173 | P1_25, | ||
| 174 | P1_26, | ||
| 175 | P1_27, | ||
| 176 | P1_28, | ||
| 177 | P1_29, | ||
| 178 | P1_30, | ||
| 179 | P1_31, | ||
| 180 | |||
| 181 | P2_0, | ||
| 182 | P2_1, | ||
| 183 | P2_2, | ||
| 184 | P2_3, | ||
| 185 | P2_4, | ||
| 186 | P2_5, | ||
| 187 | P2_6, | ||
| 188 | P2_7, | ||
| 189 | P2_8, | ||
| 190 | P2_9, | ||
| 191 | P2_10, | ||
| 192 | P2_11, | ||
| 193 | P2_12, | ||
| 194 | P2_13, | ||
| 195 | P2_14, | ||
| 196 | P2_15, | ||
| 197 | P2_16, | ||
| 198 | P2_17, | ||
| 199 | P2_18, | ||
| 200 | P2_19, | ||
| 201 | P2_20, | ||
| 202 | P2_21, | ||
| 203 | P2_22, | ||
| 204 | P2_23, | ||
| 205 | P2_24, | ||
| 206 | P2_25, | ||
| 207 | P2_26, | ||
| 208 | P2_27, | ||
| 209 | P2_28, | ||
| 210 | P2_29, | ||
| 211 | P2_30, | ||
| 212 | P2_31, | ||
| 213 | |||
| 214 | P3_0, | ||
| 215 | P3_1, | ||
| 216 | P3_2, | ||
| 217 | P3_3, | ||
| 218 | P3_4, | ||
| 219 | P3_5, | ||
| 220 | P3_6, | ||
| 221 | P3_7, | ||
| 222 | P3_8, | ||
| 223 | P3_9, | ||
| 224 | P3_10, | ||
| 225 | P3_11, | ||
| 226 | P3_12, | ||
| 227 | P3_13, | ||
| 228 | P3_14, | ||
| 229 | P3_15, | ||
| 230 | P3_16, | ||
| 231 | P3_17, | ||
| 232 | P3_18, | ||
| 233 | P3_19, | ||
| 234 | P3_20, | ||
| 235 | P3_21, | ||
| 236 | P3_22, | ||
| 237 | P3_23, | ||
| 238 | P3_24, | ||
| 239 | P3_25, | ||
| 240 | P3_26, | ||
| 241 | P3_27, | ||
| 242 | P3_28, | ||
| 243 | P3_29, | ||
| 244 | P3_30, | ||
| 245 | P3_31, | ||
| 246 | |||
| 247 | P4_0, | ||
| 248 | P4_1, | ||
| 249 | P4_2, | ||
| 250 | P4_3, | ||
| 251 | P4_4, | ||
| 252 | P4_5, | ||
| 253 | P4_6, | ||
| 254 | P4_7, | ||
| 255 | P4_8, | ||
| 256 | P4_9, | ||
| 257 | P4_10, | ||
| 258 | P4_11, | ||
| 259 | P4_12, | ||
| 260 | P4_13, | ||
| 261 | P4_14, | ||
| 262 | P4_15, | ||
| 263 | P4_16, | ||
| 264 | P4_17, | ||
| 265 | P4_18, | ||
| 266 | P4_19, | ||
| 267 | P4_20, | ||
| 268 | P4_21, | ||
| 269 | P4_22, | ||
| 270 | P4_23, | ||
| 271 | P4_24, | ||
| 272 | P4_25, | ||
| 273 | P4_26, | ||
| 274 | P4_27, | ||
| 275 | P4_28, | ||
| 276 | P4_29, | ||
| 277 | P4_30, | ||
| 278 | P4_31, | ||
| 279 | |||
| 280 | P5_0, | ||
| 281 | P5_1, | ||
| 282 | P5_2, | ||
| 283 | P5_3, | ||
| 284 | P5_4, | ||
| 285 | P5_5, | ||
| 286 | P5_6, | ||
| 287 | P5_7, | ||
| 288 | P5_8, | ||
| 289 | P5_9, | ||
| 290 | P5_10, | ||
| 291 | P5_11, | ||
| 292 | P5_12, | ||
| 293 | P5_13, | ||
| 294 | P5_14, | ||
| 295 | P5_15, | ||
| 296 | P5_16, | ||
| 297 | P5_17, | ||
| 298 | P5_18, | ||
| 299 | P5_19, | ||
| 300 | P5_20, | ||
| 301 | P5_21, | ||
| 302 | P5_22, | ||
| 303 | P5_23, | ||
| 304 | P5_24, | ||
| 305 | P5_25, | ||
| 306 | P5_26, | ||
| 307 | P5_27, | ||
| 308 | P5_28, | ||
| 309 | P5_29, | ||
| 310 | P5_30, | ||
| 311 | P5_31, | ||
| 312 | |||
| 313 | PKC0, | ||
| 314 | |||
| 315 | PORT0, | ||
| 316 | PORT1, | ||
| 317 | PORT2, | ||
| 318 | PORT3, | ||
| 319 | PORT4, | ||
| 320 | |||
| 321 | RTC0, | ||
| 322 | SAU, | ||
| 323 | SCG0, | ||
| 324 | SCN_SCB, | ||
| 325 | SGI0, | ||
| 326 | SMARTDMA0, | ||
| 327 | SPC0, | ||
| 328 | SYSCON, | ||
| 329 | TDET0, | ||
| 330 | TRNG0, | ||
| 331 | UDF0, | ||
| 332 | USB0, | ||
| 333 | UTICK0, | ||
| 334 | VBAT0, | ||
| 335 | WAKETIMER0, | ||
| 336 | WUU0, | ||
| 337 | WWDT0, | ||
| 338 | ); | ||
| 339 | |||
| 340 | // Use cortex-m-rt's #[interrupt] attribute directly; PAC does not re-export it. | ||
| 341 | |||
| 342 | // Re-export interrupt traits and types | ||
| 343 | #[cfg(feature = "unstable-pac")] | ||
| 344 | pub use mcxa_pac as pac; | ||
| 345 | #[cfg(not(feature = "unstable-pac"))] | ||
| 346 | pub(crate) use mcxa_pac as pac; | ||
| 347 | |||
| 348 | /// Initialize HAL with configuration (mirrors embassy-imxrt style). Minimal: just take peripherals. | ||
| 349 | /// Also applies configurable NVIC priority for the OSTIMER OS_EVENT interrupt (no enabling). | ||
| 350 | pub fn init(cfg: crate::config::Config) -> Peripherals { | ||
| 351 | let peripherals = Peripherals::take(); | ||
| 352 | // Apply user-configured priority early; enabling is left to examples/apps | ||
| 353 | #[cfg(feature = "time")] | ||
| 354 | crate::interrupt::OS_EVENT.set_priority(cfg.time_interrupt_priority); | ||
| 355 | // Apply user-configured priority early; enabling is left to examples/apps | ||
| 356 | crate::interrupt::RTC.set_priority(cfg.rtc_interrupt_priority); | ||
| 357 | // Apply user-configured priority early; enabling is left to examples/apps | ||
| 358 | crate::interrupt::GPIO0.set_priority(cfg.gpio_interrupt_priority); | ||
| 359 | // Apply user-configured priority early; enabling is left to examples/apps | ||
| 360 | crate::interrupt::GPIO1.set_priority(cfg.gpio_interrupt_priority); | ||
| 361 | // Apply user-configured priority early; enabling is left to examples/apps | ||
| 362 | crate::interrupt::GPIO2.set_priority(cfg.gpio_interrupt_priority); | ||
| 363 | // Apply user-configured priority early; enabling is left to examples/apps | ||
| 364 | crate::interrupt::GPIO3.set_priority(cfg.gpio_interrupt_priority); | ||
| 365 | // Apply user-configured priority early; enabling is left to examples/apps | ||
| 366 | crate::interrupt::GPIO4.set_priority(cfg.gpio_interrupt_priority); | ||
| 367 | |||
| 368 | // Configure clocks | ||
| 369 | crate::clocks::init(cfg.clock_cfg).unwrap(); | ||
| 370 | |||
| 371 | unsafe { | ||
| 372 | crate::gpio::init(); | ||
| 373 | } | ||
| 374 | |||
| 375 | // Initialize DMA controller (clock, reset, configuration) | ||
| 376 | crate::dma::init(); | ||
| 377 | |||
| 378 | // Initialize embassy-time global driver backed by OSTIMER0 | ||
| 379 | #[cfg(feature = "time")] | ||
| 380 | crate::ostimer::time_driver::init(crate::config::Config::default().time_interrupt_priority, 1_000_000); | ||
| 381 | |||
| 382 | // Enable GPIO clocks | ||
| 383 | unsafe { | ||
| 384 | _ = crate::clocks::enable_and_reset::<crate::peripherals::PORT0>(&crate::clocks::periph_helpers::NoConfig); | ||
| 385 | _ = crate::clocks::enable_and_reset::<crate::peripherals::GPIO0>(&crate::clocks::periph_helpers::NoConfig); | ||
| 386 | |||
| 387 | _ = crate::clocks::enable_and_reset::<crate::peripherals::PORT1>(&crate::clocks::periph_helpers::NoConfig); | ||
| 388 | _ = crate::clocks::enable_and_reset::<crate::peripherals::GPIO1>(&crate::clocks::periph_helpers::NoConfig); | ||
| 389 | |||
| 390 | _ = crate::clocks::enable_and_reset::<crate::peripherals::PORT2>(&crate::clocks::periph_helpers::NoConfig); | ||
| 391 | _ = crate::clocks::enable_and_reset::<crate::peripherals::GPIO2>(&crate::clocks::periph_helpers::NoConfig); | ||
| 392 | |||
| 393 | _ = crate::clocks::enable_and_reset::<crate::peripherals::PORT3>(&crate::clocks::periph_helpers::NoConfig); | ||
| 394 | _ = crate::clocks::enable_and_reset::<crate::peripherals::GPIO3>(&crate::clocks::periph_helpers::NoConfig); | ||
| 395 | |||
| 396 | _ = crate::clocks::enable_and_reset::<crate::peripherals::PORT4>(&crate::clocks::periph_helpers::NoConfig); | ||
| 397 | _ = crate::clocks::enable_and_reset::<crate::peripherals::GPIO4>(&crate::clocks::periph_helpers::NoConfig); | ||
| 398 | } | ||
| 399 | |||
| 400 | peripherals | ||
| 401 | } | ||
| 402 | |||
| 403 | /// Macro to bind interrupts to handlers, similar to embassy-imxrt. | ||
| 404 | /// | ||
| 405 | /// Example: | ||
| 406 | /// - Bind OS_EVENT to the OSTIMER time-driver handler | ||
| 407 | /// bind_interrupts!(struct Irqs { OS_EVENT => crate::ostimer::time_driver::OsEventHandler; }); | ||
| 408 | #[macro_export] | ||
| 409 | macro_rules! bind_interrupts { | ||
| 410 | ($(#[$attr:meta])* $vis:vis struct $name:ident { | ||
| 411 | $( | ||
| 412 | $(#[cfg($cond_irq:meta)])? | ||
| 413 | $irq:ident => $( | ||
| 414 | $(#[cfg($cond_handler:meta)])? | ||
| 415 | $handler:ty | ||
| 416 | ),*; | ||
| 417 | )* | ||
| 418 | }) => { | ||
| 419 | #[derive(Copy, Clone)] | ||
| 420 | $(#[$attr])* | ||
| 421 | $vis struct $name; | ||
| 422 | |||
| 423 | $( | ||
| 424 | #[allow(non_snake_case)] | ||
| 425 | #[no_mangle] | ||
| 426 | $(#[cfg($cond_irq)])? | ||
| 427 | unsafe extern "C" fn $irq() { | ||
| 428 | unsafe { | ||
| 429 | $( | ||
| 430 | $(#[cfg($cond_handler)])? | ||
| 431 | <$handler as $crate::interrupt::typelevel::Handler<$crate::interrupt::typelevel::$irq>>::on_interrupt(); | ||
| 432 | )* | ||
| 433 | } | ||
| 434 | } | ||
| 435 | |||
| 436 | $(#[cfg($cond_irq)])? | ||
| 437 | $crate::bind_interrupts!(@inner | ||
| 438 | $( | ||
| 439 | $(#[cfg($cond_handler)])? | ||
| 440 | unsafe impl $crate::interrupt::typelevel::Binding<$crate::interrupt::typelevel::$irq, $handler> for $name {} | ||
| 441 | )* | ||
| 442 | ); | ||
| 443 | )* | ||
| 444 | }; | ||
| 445 | (@inner $($t:tt)*) => { | ||
| 446 | $($t)* | ||
| 447 | } | ||
| 448 | } | ||
