rp2040/
lib.rs

1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright Tock Contributors 2022.
4
5#![no_std]
6
7pub mod adc;
8pub mod chip;
9pub mod clocks;
10mod deferred_calls;
11pub mod gpio;
12pub mod i2c;
13pub mod interrupts;
14pub mod pio;
15pub mod pio_pwm;
16pub mod pio_spi;
17pub mod pwm;
18pub mod resets;
19pub mod rtc;
20pub mod spi;
21pub mod sysinfo;
22pub mod test;
23pub mod timer;
24pub mod uart;
25pub mod usb;
26pub mod watchdog;
27pub mod xosc;
28
29use cortexm0p::{initialize_ram_jump_to_main, unhandled_interrupt, CortexM0P, CortexMVariant};
30
31extern "C" {
32    // _estack is not really a function, but it makes the types work
33    // You should never actually invoke it!!
34    fn _estack();
35}
36
37#[cfg_attr(
38    all(target_arch = "arm", target_os = "none"),
39    link_section = ".vectors"
40)]
41// used Ensures that the symbol is kept until the final binary
42#[cfg_attr(all(target_arch = "arm", target_os = "none"), used)]
43pub static BASE_VECTORS: [unsafe extern "C" fn(); 16] = [
44    _estack,
45    initialize_ram_jump_to_main,
46    unhandled_interrupt,           // NMI
47    CortexM0P::HARD_FAULT_HANDLER, // Hard Fault
48    unhandled_interrupt,           // MemManage
49    unhandled_interrupt,           // BusFault
50    unhandled_interrupt,           // UsageFault
51    unhandled_interrupt,
52    unhandled_interrupt,
53    unhandled_interrupt,
54    unhandled_interrupt,
55    CortexM0P::SVC_HANDLER, // SVC
56    unhandled_interrupt,    // DebugMon
57    unhandled_interrupt,
58    unhandled_interrupt,        // PendSV
59    CortexM0P::SYSTICK_HANDLER, // SysTick
60];
61
62// RP2040 has total of 26 interrupts, but the SDK declares 32 as 26 - 32 might be manually handled
63#[cfg_attr(all(target_arch = "arm", target_os = "none"), link_section = ".irqs")]
64// used Ensures that the symbol is kept until the final binary
65#[cfg_attr(all(target_arch = "arm", target_os = "none"), used)]
66pub static IRQS: [unsafe extern "C" fn(); 32] = [
67    CortexM0P::GENERIC_ISR, // TIMER0 (0)
68    CortexM0P::GENERIC_ISR, // TIMER1 (1)
69    CortexM0P::GENERIC_ISR, // TIMER2 (2)
70    CortexM0P::GENERIC_ISR, // TIMER3 (3)
71    CortexM0P::GENERIC_ISR, // PWM WRAP (4)
72    CortexM0P::GENERIC_ISR, // USB (5)
73    CortexM0P::GENERIC_ISR, // XIP (6)
74    CortexM0P::GENERIC_ISR, // PIO0 INT0  (7)
75    CortexM0P::GENERIC_ISR, // PIO0 INT1 (8)
76    CortexM0P::GENERIC_ISR, // PIO1 INT0 (9)
77    CortexM0P::GENERIC_ISR, // PIO1 INT1 (10)
78    CortexM0P::GENERIC_ISR, // DMA0 (11)
79    CortexM0P::GENERIC_ISR, // DMA1 (12)
80    CortexM0P::GENERIC_ISR, // IO BANK 0 (13)
81    CortexM0P::GENERIC_ISR, // IO QSPI (14)
82    CortexM0P::GENERIC_ISR, // SIO PROC 0 (15)
83    CortexM0P::GENERIC_ISR, // SIO PROC 1 (16)
84    CortexM0P::GENERIC_ISR, // CLOCKS (17)
85    CortexM0P::GENERIC_ISR, // SPI 0 (18)
86    CortexM0P::GENERIC_ISR, // SPI 1 (19)
87    CortexM0P::GENERIC_ISR, // UART 0 (20)
88    CortexM0P::GENERIC_ISR, // UART 1 (21)
89    CortexM0P::GENERIC_ISR, // ADC FIFO (22)
90    CortexM0P::GENERIC_ISR, // I2C 0 (23)
91    CortexM0P::GENERIC_ISR, // I2C 1 (24)
92    CortexM0P::GENERIC_ISR, // RTC (25)
93    unhandled_interrupt,    // (26)
94    unhandled_interrupt,    // (27)
95    unhandled_interrupt,    // (28)
96    unhandled_interrupt,    // (29)
97    unhandled_interrupt,    // (30)
98    unhandled_interrupt,    // (31)
99];
100
101extern "C" {
102    static mut _szero: usize;
103    static mut _ezero: usize;
104    static mut _etext: usize;
105    static mut _srelocate: usize;
106    static mut _erelocate: usize;
107}
108
109pub unsafe fn init() {
110    cortexm0p::nvic::disable_all();
111    cortexm0p::nvic::clear_all_pending();
112    let sio = gpio::SIO::new();
113    let processor = sio.get_processor();
114    match processor {
115        chip::Processor::Processor0 => {}
116        _ => panic!(
117            "Kernel should run only using processor 0 (now processor {})",
118            processor as u8
119        ),
120    }
121}