#![no_std]
#![no_main]
#![deny(missing_docs)]
use core::ptr::{addr_of, addr_of_mut};
use capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm;
use components::gpio::GpioComponent;
use kernel::capabilities;
use kernel::component::Component;
use kernel::debug;
use kernel::hil::gpio::Configure;
use kernel::hil::led::LedLow;
use kernel::platform::{KernelResources, SyscallDriverLookup};
use kernel::scheduler::round_robin::RoundRobinSched;
use kernel::{create_capability, static_init};
use imxrt1050::iomuxc::DriveStrength;
use imxrt1050::iomuxc::MuxMode;
use imxrt1050::iomuxc::OpenDrainEn;
use imxrt1050::iomuxc::PadId;
use imxrt1050::iomuxc::PullKeepEn;
use imxrt1050::iomuxc::PullUpDown;
use imxrt1050::iomuxc::Sion;
use imxrt1050::iomuxc::Speed;
use imxrt10xx as imxrt1050;
pub mod io;
pub mod boot_header;
const NUM_PROCS: usize = 4;
static mut PROCESSES: [Option<&'static dyn kernel::process::Process>; NUM_PROCS] =
[None; NUM_PROCS];
type Chip = imxrt1050::chip::Imxrt10xx<imxrt1050::chip::Imxrt10xxDefaultPeripherals>;
static mut CHIP: Option<&'static Chip> = None;
static mut PROCESS_PRINTER: Option<&'static capsules_system::process_printer::ProcessPrinterText> =
None;
const FAULT_RESPONSE: capsules_system::process_policies::PanicFaultPolicy =
capsules_system::process_policies::PanicFaultPolicy {};
#[used]
#[link_section = ".boot_hdr"]
static BOOT_HDR: [u8; 8192] = boot_header::BOOT_HDR;
#[no_mangle]
#[link_section = ".stack_buffer"]
pub static mut STACK_MEMORY: [u8; 0x2000] = [0; 0x2000];
struct Imxrt1050EVKB {
alarm: &'static capsules_core::alarm::AlarmDriver<
'static,
VirtualMuxAlarm<'static, imxrt1050::gpt::Gpt1<'static>>,
>,
button: &'static capsules_core::button::Button<'static, imxrt1050::gpio::Pin<'static>>,
console: &'static capsules_core::console::Console<'static>,
gpio: &'static capsules_core::gpio::GPIO<'static, imxrt1050::gpio::Pin<'static>>,
ipc: kernel::ipc::IPC<{ NUM_PROCS as u8 }>,
led: &'static capsules_core::led::LedDriver<
'static,
LedLow<'static, imxrt1050::gpio::Pin<'static>>,
1,
>,
ninedof: &'static capsules_extra::ninedof::NineDof<'static>,
scheduler: &'static RoundRobinSched<'static>,
systick: cortexm7::systick::SysTick,
}
impl SyscallDriverLookup for Imxrt1050EVKB {
fn with_driver<F, R>(&self, driver_num: usize, f: F) -> R
where
F: FnOnce(Option<&dyn kernel::syscall::SyscallDriver>) -> R,
{
match driver_num {
capsules_core::alarm::DRIVER_NUM => f(Some(self.alarm)),
capsules_core::button::DRIVER_NUM => f(Some(self.button)),
capsules_core::console::DRIVER_NUM => f(Some(self.console)),
capsules_core::gpio::DRIVER_NUM => f(Some(self.gpio)),
kernel::ipc::DRIVER_NUM => f(Some(&self.ipc)),
capsules_core::led::DRIVER_NUM => f(Some(self.led)),
capsules_extra::ninedof::DRIVER_NUM => f(Some(self.ninedof)),
_ => f(None),
}
}
}
impl KernelResources<imxrt1050::chip::Imxrt10xx<imxrt1050::chip::Imxrt10xxDefaultPeripherals>>
for Imxrt1050EVKB
{
type SyscallDriverLookup = Self;
type SyscallFilter = ();
type ProcessFault = ();
type Scheduler = RoundRobinSched<'static>;
type SchedulerTimer = cortexm7::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 {
&()
}
}
unsafe fn set_pin_primary_functions(
peripherals: &'static imxrt1050::chip::Imxrt10xxDefaultPeripherals,
) {
use imxrt1050::gpio::PinId;
peripherals.ccm.enable_iomuxc_clock();
peripherals.ccm.enable_iomuxc_snvs_clock();
peripherals.ports.gpio1.enable_clock();
peripherals.iomuxc.enable_sw_mux_ctl_pad_gpio(
PadId::AdB0,
MuxMode::ALT5, Sion::Disabled,
9,
);
peripherals.iomuxc.configure_sw_pad_ctl_pad_gpio(
PadId::AdB0,
9,
PullUpDown::Pus0_100kOhmPullDown, PullKeepEn::Pke1PullKeeperEnabled, OpenDrainEn::Ode0OpenDrainDisabled, Speed::Medium2, DriveStrength::DSE6, );
let pin = peripherals.ports.pin(PinId::AdB0_09);
pin.make_output();
kernel::debug::assign_gpios(Some(pin), None, None);
peripherals.ports.gpio5.enable_clock();
peripherals.iomuxc_snvs.enable_sw_mux_ctl_pad_gpio(
MuxMode::ALT5, Sion::Disabled,
0,
);
peripherals.ports.pin(PinId::Wakeup).make_input();
}
unsafe fn setup_peripherals(peripherals: &imxrt1050::chip::Imxrt10xxDefaultPeripherals) {
cortexm7::nvic::Nvic::new(imxrt1050::nvic::LPUART1).enable();
peripherals.gpt1.enable_clock();
peripherals.gpt1.start(
peripherals.ccm.perclk_sel(),
peripherals.ccm.perclk_divider(),
);
cortexm7::nvic::Nvic::new(imxrt1050::nvic::GPT1).enable();
}
#[inline(never)]
unsafe fn start() -> (
&'static kernel::Kernel,
Imxrt1050EVKB,
&'static imxrt1050::chip::Imxrt10xx<imxrt1050::chip::Imxrt10xxDefaultPeripherals>,
) {
imxrt1050::init();
let ccm = static_init!(imxrt1050::ccm::Ccm, imxrt1050::ccm::Ccm::new());
let peripherals = static_init!(
imxrt1050::chip::Imxrt10xxDefaultPeripherals,
imxrt1050::chip::Imxrt10xxDefaultPeripherals::new(ccm)
);
peripherals.ccm.set_low_power_mode();
peripherals.lpuart1.disable_clock();
peripherals.lpuart2.disable_clock();
peripherals
.ccm
.set_uart_clock_sel(imxrt1050::ccm::UartClockSelection::PLL3);
peripherals.ccm.set_uart_clock_podf(1);
peripherals.lpuart1.set_baud();
set_pin_primary_functions(peripherals);
setup_peripherals(peripherals);
let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(&*addr_of!(PROCESSES)));
let chip = static_init!(Chip, Chip::new(peripherals));
CHIP = Some(chip);
peripherals.iomuxc.enable_sw_mux_ctl_pad_gpio(
PadId::AdB0,
MuxMode::ALT2, Sion::Disabled,
13,
);
peripherals.iomuxc.enable_sw_mux_ctl_pad_gpio(
PadId::AdB0,
MuxMode::ALT2, Sion::Disabled,
14,
);
peripherals.iomuxc.configure_sw_pad_ctl_pad_gpio(
PadId::AdB0,
13,
PullUpDown::Pus0_100kOhmPullDown, PullKeepEn::Pke1PullKeeperEnabled, OpenDrainEn::Ode0OpenDrainDisabled, Speed::Medium2, DriveStrength::DSE6, );
peripherals.iomuxc.configure_sw_pad_ctl_pad_gpio(
PadId::AdB0,
14,
PullUpDown::Pus0_100kOhmPullDown, PullKeepEn::Pke1PullKeeperEnabled, OpenDrainEn::Ode0OpenDrainDisabled, Speed::Medium2, DriveStrength::DSE6, );
peripherals.lpuart1.enable_clock();
let lpuart_mux = components::console::UartMuxComponent::new(&peripherals.lpuart1, 115200)
.finalize(components::uart_mux_component_static!());
(*addr_of_mut!(io::WRITER)).set_initialized();
let memory_allocation_capability = create_capability!(capabilities::MemoryAllocationCapability);
let process_management_capability =
create_capability!(capabilities::ProcessManagementCapability);
let console = components::console::ConsoleComponent::new(
board_kernel,
capsules_core::console::DRIVER_NUM,
lpuart_mux,
)
.finalize(components::console_component_static!());
components::debug_writer::DebugWriterComponent::new(lpuart_mux)
.finalize(components::debug_writer_component_static!());
let led = components::led::LedsComponent::new().finalize(components::led_component_static!(
LedLow<'static, imxrt1050::gpio::Pin<'static>>,
LedLow::new(peripherals.ports.pin(imxrt1050::gpio::PinId::AdB0_09)),
));
let button = components::button::ButtonComponent::new(
board_kernel,
capsules_core::button::DRIVER_NUM,
components::button_component_helper!(
imxrt1050::gpio::Pin,
(
peripherals.ports.pin(imxrt1050::gpio::PinId::Wakeup),
kernel::hil::gpio::ActivationMode::ActiveHigh,
kernel::hil::gpio::FloatingState::PullDown
)
),
)
.finalize(components::button_component_static!(imxrt1050::gpio::Pin));
let gpt1 = &peripherals.gpt1;
let mux_alarm = components::alarm::AlarmMuxComponent::new(gpt1).finalize(
components::alarm_mux_component_static!(imxrt1050::gpt::Gpt1),
);
let alarm = components::alarm::AlarmDriverComponent::new(
board_kernel,
capsules_core::alarm::DRIVER_NUM,
mux_alarm,
)
.finalize(components::alarm_component_static!(imxrt1050::gpt::Gpt1));
let gpio = GpioComponent::new(
board_kernel,
capsules_core::gpio::DRIVER_NUM,
components::gpio_component_helper!(
imxrt1050::gpio::Pin<'static>,
0 => peripherals.ports.pin(imxrt1050::gpio::PinId::AdB0_09)
),
)
.finalize(components::gpio_component_static!(
imxrt1050::gpio::Pin<'static>
));
peripherals.iomuxc.enable_sw_mux_ctl_pad_gpio(
PadId::AdB1,
MuxMode::ALT3, Sion::Enabled,
0,
);
peripherals.iomuxc.enable_lpi2c_scl_select_input();
peripherals.iomuxc.enable_sw_mux_ctl_pad_gpio(
PadId::AdB1,
MuxMode::ALT3, Sion::Enabled,
1,
);
peripherals.iomuxc.enable_lpi2c_sda_select_input();
peripherals.iomuxc.configure_sw_pad_ctl_pad_gpio(
PadId::AdB1,
0,
PullUpDown::Pus3_22kOhmPullUp, PullKeepEn::Pke1PullKeeperEnabled, OpenDrainEn::Ode1OpenDrainEnabled, Speed::Medium2, DriveStrength::DSE6, );
peripherals.iomuxc.configure_sw_pad_ctl_pad_gpio(
PadId::AdB1,
1,
PullUpDown::Pus3_22kOhmPullUp, PullKeepEn::Pke1PullKeeperEnabled, OpenDrainEn::Ode1OpenDrainEnabled, Speed::Medium2, DriveStrength::DSE6, );
peripherals.lpi2c1.enable_clock();
peripherals
.lpi2c1
.set_speed(imxrt1050::lpi2c::Lpi2cSpeed::Speed100k, 8);
use imxrt1050::gpio::PinId;
let mux_i2c = components::i2c::I2CMuxComponent::new(&peripherals.lpi2c1, None).finalize(
components::i2c_mux_component_static!(imxrt1050::lpi2c::Lpi2c),
);
let fxos8700 = components::fxos8700::Fxos8700Component::new(
mux_i2c,
0x1f,
peripherals.ports.pin(PinId::AdB1_00),
)
.finalize(components::fxos8700_component_static!(
imxrt1050::lpi2c::Lpi2c
));
let ninedof = components::ninedof::NineDofComponent::new(
board_kernel,
capsules_extra::ninedof::DRIVER_NUM,
)
.finalize(components::ninedof_component_static!(fxos8700));
let scheduler = components::sched::round_robin::RoundRobinComponent::new(&*addr_of!(PROCESSES))
.finalize(components::round_robin_component_static!(NUM_PROCS));
let imxrt1050 = Imxrt1050EVKB {
console,
ipc: kernel::ipc::IPC::new(
board_kernel,
kernel::ipc::DRIVER_NUM,
&memory_allocation_capability,
),
led,
button,
ninedof,
alarm,
gpio,
scheduler,
systick: cortexm7::systick::SysTick::new_with_calibration(792_000_000),
};
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,
lpuart_mux,
mux_alarm,
process_printer,
None,
)
.finalize(components::process_console_component_static!(
imxrt1050::gpt::Gpt1
));
let _ = process_console.start();
debug!("Tock OS initialization complete. Entering 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, imxrt1050, chip)
}
#[no_mangle]
pub unsafe fn main() {
let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
let (board_kernel, board, chip) = start();
board_kernel.kernel_loop(&board, chip, Some(&board.ipc), &main_loop_capability);
}