#![no_std]
#![cfg_attr(not(doc), no_main)]
#![deny(missing_docs)]
use core::ptr::{addr_of, addr_of_mut};
use capsules_core::i2c_master::I2CMasterDriver;
use capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm;
use components::date_time_component_static;
use components::gpio::GpioComponent;
use components::led::LedsComponent;
use enum_primitive::cast::FromPrimitive;
use kernel::component::Component;
use kernel::debug;
use kernel::hil::gpio::{Configure, FloatingState};
use kernel::hil::i2c::I2CMaster;
use kernel::hil::led::LedHigh;
use kernel::hil::usb::Client;
use kernel::platform::{KernelResources, SyscallDriverLookup};
use kernel::scheduler::round_robin::RoundRobinSched;
use kernel::syscall::SyscallDriver;
use kernel::{capabilities, create_capability, static_init, Kernel};
use rp2040::adc::{Adc, Channel};
use rp2040::chip::{Rp2040, Rp2040DefaultPeripherals};
use rp2040::clocks::{
AdcAuxiliaryClockSource, PeripheralAuxiliaryClockSource, PllClock,
ReferenceAuxiliaryClockSource, ReferenceClockSource, RtcAuxiliaryClockSource,
SystemAuxiliaryClockSource, SystemClockSource, UsbAuxiliaryClockSource,
};
use rp2040::gpio::{GpioFunction, RPGpio, RPGpioPin};
use rp2040::i2c::I2c;
use rp2040::resets::Peripheral;
use rp2040::sysinfo;
use rp2040::timer::RPTimer;
mod io;
mod flash_bootloader;
#[no_mangle]
#[link_section = ".stack_buffer"]
pub static mut STACK_MEMORY: [u8; 0x1500] = [0; 0x1500];
#[used]
#[link_section = ".flash_bootloader"]
static FLASH_BOOTLOADER: [u8; 256] = flash_bootloader::FLASH_BOOTLOADER;
const FAULT_RESPONSE: capsules_system::process_policies::PanicFaultPolicy =
capsules_system::process_policies::PanicFaultPolicy {};
const NUM_PROCS: usize = 4;
static mut PROCESSES: [Option<&'static dyn kernel::process::Process>; NUM_PROCS] =
[None; NUM_PROCS];
static mut CHIP: Option<&'static Rp2040<Rp2040DefaultPeripherals>> = None;
static mut PROCESS_PRINTER: Option<&'static capsules_system::process_printer::ProcessPrinterText> =
None;
type TemperatureRp2040Sensor = components::temperature_rp2040::TemperatureRp2040ComponentType<
capsules_core::virtualizers::virtual_adc::AdcDevice<'static, rp2040::adc::Adc<'static>>,
>;
type TemperatureDriver = components::temperature::TemperatureComponentType<TemperatureRp2040Sensor>;
pub struct RaspberryPiPico {
ipc: kernel::ipc::IPC<{ NUM_PROCS as u8 }>,
console: &'static capsules_core::console::Console<'static>,
alarm: &'static capsules_core::alarm::AlarmDriver<
'static,
VirtualMuxAlarm<'static, rp2040::timer::RPTimer<'static>>,
>,
gpio: &'static capsules_core::gpio::GPIO<'static, RPGpioPin<'static>>,
led: &'static capsules_core::led::LedDriver<'static, LedHigh<'static, RPGpioPin<'static>>, 1>,
adc: &'static capsules_core::adc::AdcVirtualized<'static>,
temperature: &'static TemperatureDriver,
i2c: &'static capsules_core::i2c_master::I2CMasterDriver<'static, I2c<'static, 'static>>,
date_time:
&'static capsules_extra::date_time::DateTimeCapsule<'static, rp2040::rtc::Rtc<'static>>,
scheduler: &'static RoundRobinSched<'static>,
systick: cortexm0p::systick::SysTick,
}
impl SyscallDriverLookup for RaspberryPiPico {
fn with_driver<F, R>(&self, driver_num: usize, f: F) -> R
where
F: FnOnce(Option<&dyn SyscallDriver>) -> R,
{
match driver_num {
capsules_core::console::DRIVER_NUM => f(Some(self.console)),
capsules_core::alarm::DRIVER_NUM => f(Some(self.alarm)),
capsules_core::gpio::DRIVER_NUM => f(Some(self.gpio)),
capsules_core::led::DRIVER_NUM => f(Some(self.led)),
kernel::ipc::DRIVER_NUM => f(Some(&self.ipc)),
capsules_core::adc::DRIVER_NUM => f(Some(self.adc)),
capsules_extra::temperature::DRIVER_NUM => f(Some(self.temperature)),
capsules_core::i2c_master::DRIVER_NUM => f(Some(self.i2c)),
capsules_extra::date_time::DRIVER_NUM => f(Some(self.date_time)),
_ => f(None),
}
}
}
impl KernelResources<Rp2040<'static, Rp2040DefaultPeripherals<'static>>> for RaspberryPiPico {
type SyscallDriverLookup = Self;
type SyscallFilter = ();
type ProcessFault = ();
type Scheduler = RoundRobinSched<'static>;
type SchedulerTimer = cortexm0p::systick::SysTick;
type WatchDog = ();
type ContextSwitchCallback = ();
fn syscall_driver_lookup(&self) -> &Self::SyscallDriverLookup {
self
}
fn syscall_filter(&self) -> &Self::SyscallFilter {
&()
}
fn process_fault(&self) -> &Self::ProcessFault {
&()
}
fn scheduler(&self) -> &Self::Scheduler {
self.scheduler
}
fn scheduler_timer(&self) -> &Self::SchedulerTimer {
&self.systick
}
fn watchdog(&self) -> &Self::WatchDog {
&()
}
fn context_switch_callback(&self) -> &Self::ContextSwitchCallback {
&()
}
}
#[allow(dead_code)]
extern "C" {
fn jump_to_bootloader();
}
#[cfg(any(doc, all(target_arch = "arm", target_os = "none")))]
core::arch::global_asm!(
"
.section .jump_to_bootloader, \"ax\"
.global jump_to_bootloader
.thumb_func
jump_to_bootloader:
movs r0, #0
ldr r1, =(0xe0000000 + 0x0000ed08)
str r0, [r1]
ldmia r0!, {{r1, r2}}
msr msp, r1
bx r2
"
);
fn init_clocks(peripherals: &Rp2040DefaultPeripherals) {
peripherals.watchdog.start_tick(12);
peripherals.clocks.disable_resus();
peripherals.xosc.init();
peripherals.clocks.disable_sys_aux();
peripherals.clocks.disable_ref_aux();
peripherals
.resets
.reset(&[Peripheral::PllSys, Peripheral::PllUsb]);
peripherals
.resets
.unreset(&[Peripheral::PllSys, Peripheral::PllUsb], true);
peripherals
.clocks
.pll_init(PllClock::Sys, 12, 1, 1500 * 1000000, 6, 2);
peripherals
.clocks
.pll_init(PllClock::Usb, 12, 1, 480 * 1000000, 5, 2);
peripherals.clocks.configure_reference(
ReferenceClockSource::Xosc,
ReferenceAuxiliaryClockSource::PllUsb,
12000000,
12000000,
);
peripherals.clocks.configure_system(
SystemClockSource::Auxiliary,
SystemAuxiliaryClockSource::PllSys,
125000000,
125000000,
);
peripherals
.clocks
.configure_usb(UsbAuxiliaryClockSource::PllSys, 48000000, 48000000);
peripherals
.clocks
.configure_adc(AdcAuxiliaryClockSource::PllUsb, 48000000, 48000000);
peripherals
.clocks
.configure_rtc(RtcAuxiliaryClockSource::PllSys, 48000000, 46875);
peripherals
.clocks
.configure_peripheral(PeripheralAuxiliaryClockSource::System, 125000000);
}
#[inline(never)]
pub unsafe fn start() -> (
&'static kernel::Kernel,
RaspberryPiPico,
&'static rp2040::chip::Rp2040<'static, Rp2040DefaultPeripherals<'static>>,
) {
rp2040::init();
let peripherals = static_init!(Rp2040DefaultPeripherals, Rp2040DefaultPeripherals::new());
peripherals.resolve_dependencies();
peripherals.resets.reset_all_except(&[
Peripheral::IOQSpi,
Peripheral::PadsQSpi,
Peripheral::PllUsb,
Peripheral::PllSys,
]);
peripherals.resets.unreset_all_except(
&[
Peripheral::Adc,
Peripheral::Rtc,
Peripheral::Spi0,
Peripheral::Spi1,
Peripheral::Uart0,
Peripheral::Uart1,
Peripheral::UsbCtrl,
],
true,
);
init_clocks(peripherals);
peripherals.resets.unreset_all_except(&[], true);
(*addr_of_mut!(io::WRITER)).set_uart(&peripherals.uart0);
let gpio_tx = peripherals.pins.get_pin(RPGpio::GPIO0);
let gpio_rx = peripherals.pins.get_pin(RPGpio::GPIO1);
gpio_rx.set_function(GpioFunction::UART);
gpio_tx.set_function(GpioFunction::UART);
for pin in 26..30 {
peripherals
.pins
.get_pin(RPGpio::from_usize(pin).unwrap())
.deactivate_pads();
}
let chip = static_init!(
Rp2040<Rp2040DefaultPeripherals>,
Rp2040::new(peripherals, &peripherals.sio)
);
CHIP = Some(chip);
let board_kernel = static_init!(Kernel, Kernel::new(&*addr_of!(PROCESSES)));
let process_management_capability =
create_capability!(capabilities::ProcessManagementCapability);
let memory_allocation_capability = create_capability!(capabilities::MemoryAllocationCapability);
let mux_alarm = components::alarm::AlarmMuxComponent::new(&peripherals.timer)
.finalize(components::alarm_mux_component_static!(RPTimer));
let alarm = components::alarm::AlarmDriverComponent::new(
board_kernel,
capsules_core::alarm::DRIVER_NUM,
mux_alarm,
)
.finalize(components::alarm_component_static!(RPTimer));
let strings = static_init!(
[&str; 3],
[
"Raspberry Pi", "Pico - TockOS", "00000000000000000", ]
);
let cdc = components::cdc::CdcAcmComponent::new(
&peripherals.usb,
64,
peripherals.sysinfo.get_manufacturer_rp2040() as u16,
peripherals.sysinfo.get_part() as u16,
strings,
mux_alarm,
None,
)
.finalize(components::cdc_acm_component_static!(
rp2040::usb::UsbCtrl,
rp2040::timer::RPTimer
));
let uart_mux = components::console::UartMuxComponent::new(cdc, 115200)
.finalize(components::uart_mux_component_static!());
let console = components::console::ConsoleComponent::new(
board_kernel,
capsules_core::console::DRIVER_NUM,
uart_mux,
)
.finalize(components::console_component_static!());
components::debug_writer::DebugWriterComponent::new(uart_mux)
.finalize(components::debug_writer_component_static!());
cdc.enable();
cdc.attach();
let gpio = GpioComponent::new(
board_kernel,
capsules_core::gpio::DRIVER_NUM,
components::gpio_component_helper!(
RPGpioPin,
2 => peripherals.pins.get_pin(RPGpio::GPIO2),
3 => peripherals.pins.get_pin(RPGpio::GPIO3),
6 => peripherals.pins.get_pin(RPGpio::GPIO6),
7 => peripherals.pins.get_pin(RPGpio::GPIO7),
8 => peripherals.pins.get_pin(RPGpio::GPIO8),
9 => peripherals.pins.get_pin(RPGpio::GPIO9),
10 => peripherals.pins.get_pin(RPGpio::GPIO10),
11 => peripherals.pins.get_pin(RPGpio::GPIO11),
12 => peripherals.pins.get_pin(RPGpio::GPIO12),
13 => peripherals.pins.get_pin(RPGpio::GPIO13),
14 => peripherals.pins.get_pin(RPGpio::GPIO14),
15 => peripherals.pins.get_pin(RPGpio::GPIO15),
16 => peripherals.pins.get_pin(RPGpio::GPIO16),
17 => peripherals.pins.get_pin(RPGpio::GPIO17),
18 => peripherals.pins.get_pin(RPGpio::GPIO18),
19 => peripherals.pins.get_pin(RPGpio::GPIO19),
20 => peripherals.pins.get_pin(RPGpio::GPIO20),
21 => peripherals.pins.get_pin(RPGpio::GPIO21),
22 => peripherals.pins.get_pin(RPGpio::GPIO22),
23 => peripherals.pins.get_pin(RPGpio::GPIO23),
24 => peripherals.pins.get_pin(RPGpio::GPIO24),
),
)
.finalize(components::gpio_component_static!(RPGpioPin<'static>));
let led = LedsComponent::new().finalize(components::led_component_static!(
LedHigh<'static, RPGpioPin<'static>>,
LedHigh::new(peripherals.pins.get_pin(RPGpio::GPIO25))
));
peripherals.adc.init();
let adc_mux = components::adc::AdcMuxComponent::new(&peripherals.adc)
.finalize(components::adc_mux_component_static!(Adc));
let temp_sensor = components::temperature_rp2040::TemperatureRp2040Component::new(
adc_mux,
Channel::Channel4,
1.721,
0.706,
)
.finalize(components::temperature_rp2040_adc_component_static!(
rp2040::adc::Adc
));
match peripherals.rtc.rtc_init() {
Ok(()) => {}
Err(e) => {
debug!("error starting rtc {:?}", e);
}
};
let date_time = components::date_time::DateTimeComponent::new(
board_kernel,
capsules_extra::date_time::DRIVER_NUM,
&peripherals.rtc,
)
.finalize(date_time_component_static!(rp2040::rtc::Rtc<'static>));
let temp = components::temperature::TemperatureComponent::new(
board_kernel,
capsules_extra::temperature::DRIVER_NUM,
temp_sensor,
)
.finalize(components::temperature_component_static!(
TemperatureRp2040Sensor
));
let adc_channel_0 = components::adc::AdcComponent::new(adc_mux, Channel::Channel0)
.finalize(components::adc_component_static!(Adc));
let adc_channel_1 = components::adc::AdcComponent::new(adc_mux, Channel::Channel1)
.finalize(components::adc_component_static!(Adc));
let adc_channel_2 = components::adc::AdcComponent::new(adc_mux, Channel::Channel2)
.finalize(components::adc_component_static!(Adc));
let adc_channel_3 = components::adc::AdcComponent::new(adc_mux, Channel::Channel3)
.finalize(components::adc_component_static!(Adc));
let adc_syscall =
components::adc::AdcVirtualComponent::new(board_kernel, capsules_core::adc::DRIVER_NUM)
.finalize(components::adc_syscall_component_helper!(
adc_channel_0,
adc_channel_1,
adc_channel_2,
adc_channel_3,
));
let process_printer = components::process_printer::ProcessPrinterTextComponent::new()
.finalize(components::process_printer_text_component_static!());
PROCESS_PRINTER = Some(process_printer);
let process_console = components::process_console::ProcessConsoleComponent::new(
board_kernel,
uart_mux,
mux_alarm,
process_printer,
Some(cortexm0p::support::reset),
)
.finalize(components::process_console_component_static!(RPTimer));
let _ = process_console.start();
let sda_pin = peripherals.pins.get_pin(RPGpio::GPIO4);
let scl_pin = peripherals.pins.get_pin(RPGpio::GPIO5);
sda_pin.set_function(GpioFunction::I2C);
scl_pin.set_function(GpioFunction::I2C);
sda_pin.set_floating_state(FloatingState::PullUp);
scl_pin.set_floating_state(FloatingState::PullUp);
let i2c_master_buffer = static_init!(
[u8; capsules_core::i2c_master::BUFFER_LENGTH],
[0; capsules_core::i2c_master::BUFFER_LENGTH]
);
let i2c0 = &peripherals.i2c0;
let i2c = static_init!(
I2CMasterDriver<I2c<'static, 'static>>,
I2CMasterDriver::new(
i2c0,
i2c_master_buffer,
board_kernel.create_grant(
capsules_core::i2c_master::DRIVER_NUM,
&memory_allocation_capability
),
)
);
i2c0.init(10 * 1000);
i2c0.set_master_client(i2c);
let scheduler = components::sched::round_robin::RoundRobinComponent::new(&*addr_of!(PROCESSES))
.finalize(components::round_robin_component_static!(NUM_PROCS));
let raspberry_pi_pico = RaspberryPiPico {
ipc: kernel::ipc::IPC::new(
board_kernel,
kernel::ipc::DRIVER_NUM,
&memory_allocation_capability,
),
alarm,
gpio,
led,
console,
adc: adc_syscall,
temperature: temp,
i2c,
date_time,
scheduler,
systick: cortexm0p::systick::SysTick::new_with_calibration(125_000_000),
};
let platform_type = match peripherals.sysinfo.get_platform() {
sysinfo::Platform::Asic => "ASIC",
sysinfo::Platform::Fpga => "FPGA",
};
debug!(
"RP2040 Revision {} {}",
peripherals.sysinfo.get_revision(),
platform_type
);
debug!("Initialization complete. Enter main loop");
extern "C" {
static _sapps: u8;
static _eapps: u8;
static mut _sappmem: u8;
static _eappmem: u8;
}
kernel::process::load_processes(
board_kernel,
chip,
core::slice::from_raw_parts(
core::ptr::addr_of!(_sapps),
core::ptr::addr_of!(_eapps) as usize - core::ptr::addr_of!(_sapps) as usize,
),
core::slice::from_raw_parts_mut(
core::ptr::addr_of_mut!(_sappmem),
core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
),
&mut *addr_of_mut!(PROCESSES),
&FAULT_RESPONSE,
&process_management_capability,
)
.unwrap_or_else(|err| {
debug!("Error loading processes!");
debug!("{:?}", err);
});
(board_kernel, raspberry_pi_pico, chip)
}
#[no_mangle]
pub unsafe fn main() {
let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
let (board_kernel, platform, chip) = start();
board_kernel.kernel_loop(&platform, chip, Some(&platform.ipc), &main_loop_capability);
}