#![no_std]
#![no_main]
#![deny(missing_docs)]
use capsules::virtual_alarm::VirtualMuxAlarm;
use components::gpio::GpioComponent;
use kernel::capabilities;
use kernel::common::dynamic_deferred_call::{DynamicDeferredCall, DynamicDeferredCallClientState};
use kernel::component::Component;
use kernel::debug;
use kernel::hil::gpio::Configure;
use kernel::hil::led::LedLow;
use kernel::Platform;
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 = 1;
static mut PROCESSES: [Option<&'static dyn kernel::procs::ProcessType>; NUM_PROCS] = [None];
type Chip = imxrt1050::chip::Imxrt10xx<imxrt1050::chip::Imxrt10xxDefaultPeripherals>;
static mut CHIP: Option<&'static Chip> = None;
const FAULT_RESPONSE: kernel::procs::FaultResponse = kernel::procs::FaultResponse::Panic;
#[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; 0x1000] = [0; 0x1000];
struct Imxrt1050EVKB {
alarm: &'static capsules::alarm::AlarmDriver<
'static,
VirtualMuxAlarm<'static, imxrt1050::gpt::Gpt1<'static>>,
>,
button: &'static capsules::button::Button<'static, imxrt1050::gpio::Pin<'static>>,
console: &'static capsules::console::Console<'static>,
gpio: &'static capsules::gpio::GPIO<'static, imxrt1050::gpio::Pin<'static>>,
ipc: kernel::ipc::IPC<NUM_PROCS>,
led: &'static capsules::led::LedDriver<'static, LedLow<'static, imxrt1050::gpio::Pin<'static>>>,
ninedof: &'static capsules::ninedof::NineDof<'static>,
}
impl Platform for Imxrt1050EVKB {
fn with_driver<F, R>(&self, driver_num: usize, f: F) -> R
where
F: FnOnce(Option<&dyn kernel::Driver>) -> R,
{
match driver_num {
capsules::alarm::DRIVER_NUM => f(Some(self.alarm)),
capsules::button::DRIVER_NUM => f(Some(self.button)),
capsules::console::DRIVER_NUM => f(Some(self.console)),
capsules::gpio::DRIVER_NUM => f(Some(self.gpio)),
kernel::ipc::DRIVER_NUM => f(Some(&self.ipc)),
capsules::led::DRIVER_NUM => f(Some(self.led)),
capsules::ninedof::DRIVER_NUM => f(Some(self.ninedof)),
_ => f(None),
}
}
}
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();
}
#[no_mangle]
pub unsafe fn reset_handler() {
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(&PROCESSES));
let dynamic_deferred_call_clients =
static_init!([DynamicDeferredCallClientState; 2], Default::default());
let dynamic_deferred_caller = static_init!(
DynamicDeferredCall,
DynamicDeferredCall::new(dynamic_deferred_call_clients)
);
DynamicDeferredCall::set_global_instance(dynamic_deferred_caller);
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,
dynamic_deferred_caller,
)
.finalize(());
io::WRITER.set_initialized();
let memory_allocation_capability = create_capability!(capabilities::MemoryAllocationCapability);
let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
let process_management_capability =
create_capability!(capabilities::ProcessManagementCapability);
let console = components::console::ConsoleComponent::new(board_kernel, lpuart_mux).finalize(());
components::debug_writer::DebugWriterComponent::new(lpuart_mux).finalize(());
let led = components::led::LedsComponent::new(components::led_component_helper!(
LedLow<'static, imxrt1050::gpio::Pin<'static>>,
LedLow::new(peripherals.ports.pin(imxrt1050::gpio::PinId::AdB0_09)),
))
.finalize(components::led_component_buf!(
LedLow<'static, imxrt1050::gpio::Pin<'static>>
));
let button = components::button::ButtonComponent::new(
board_kernel,
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_buf!(imxrt1050::gpio::Pin));
let gpt1 = &peripherals.gpt1;
let mux_alarm = components::alarm::AlarmMuxComponent::new(gpt1).finalize(
components::alarm_mux_component_helper!(imxrt1050::gpt::Gpt1),
);
let alarm = components::alarm::AlarmDriverComponent::new(board_kernel, mux_alarm)
.finalize(components::alarm_component_helper!(imxrt1050::gpt::Gpt1));
let gpio = GpioComponent::new(
board_kernel,
components::gpio_component_helper!(
imxrt1050::gpio::Pin<'static>,
0 => peripherals.ports.pin(imxrt1050::gpio::PinId::AdB0_09)
),
)
.finalize(components::gpio_component_buf!(
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, dynamic_deferred_caller)
.finalize(components::i2c_mux_component_helper!());
let fxos8700 = components::fxos8700::Fxos8700Component::new(
mux_i2c,
peripherals.ports.pin(PinId::AdB1_00),
)
.finalize(());
let ninedof = components::ninedof::NineDofComponent::new(board_kernel)
.finalize(components::ninedof_component_helper!(fxos8700));
let imxrt1050 = Imxrt1050EVKB {
console: console,
ipc: kernel::ipc::IPC::new(board_kernel, &memory_allocation_capability),
led: led,
button: button,
ninedof: ninedof,
alarm: alarm,
gpio: gpio,
};
debug!("Tock OS initialization complete. Entering main loop");
extern "C" {
static _sapps: u8;
static _eapps: u8;
static mut _sappmem: u8;
static _eappmem: u8;
}
kernel::procs::load_processes(
board_kernel,
chip,
core::slice::from_raw_parts(
&_sapps as *const u8,
&_eapps as *const u8 as usize - &_sapps as *const u8 as usize,
),
core::slice::from_raw_parts_mut(
&mut _sappmem as *mut u8,
&_eappmem as *const u8 as usize - &_sappmem as *const u8 as usize,
),
&mut PROCESSES,
FAULT_RESPONSE,
&process_management_capability,
)
.unwrap_or_else(|err| {
debug!("Error loading processes!");
debug!("{:?}", err);
});
let scheduler = components::sched::round_robin::RoundRobinComponent::new(&PROCESSES)
.finalize(components::rr_component_helper!(NUM_PROCS));
board_kernel.kernel_loop(
&imxrt1050,
chip,
Some(&imxrt1050.ipc),
scheduler,
&main_loop_capability,
);
}