|
| 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 2024. |
| 4 | + |
| 5 | +//! Tock kernel for the Nordic Semiconductor nRF52840 development kit (DK). |
| 6 | +
|
| 7 | +#![no_std] |
| 8 | +#![no_main] |
| 9 | +#![deny(missing_docs)] |
| 10 | + |
| 11 | +use core::ptr::addr_of_mut; |
| 12 | +use kernel::component::Component; |
| 13 | +use kernel::debug; |
| 14 | +use kernel::platform::{KernelResources, SyscallDriverLookup}; |
| 15 | +use kernel::static_init; |
| 16 | +use kernel::{capabilities, create_capability}; |
| 17 | +use nrf52840::gpio::Pin; |
| 18 | +use nrf52840dk_lib::{self, PROCESSES}; |
| 19 | + |
| 20 | +// State for loading and holding applications. |
| 21 | +// How should the kernel respond when a process faults. |
| 22 | +const FAULT_RESPONSE: capsules_system::process_policies::PanicFaultPolicy = |
| 23 | + capsules_system::process_policies::PanicFaultPolicy {}; |
| 24 | + |
| 25 | +const CRYPT_SIZE: usize = 7 * kernel::hil::symmetric_encryption::AES128_BLOCK_SIZE; |
| 26 | + |
| 27 | +// Screen |
| 28 | +type ScreenDriver = components::screen::ScreenComponentType; |
| 29 | + |
| 30 | +struct Platform { |
| 31 | + base: nrf52840dk_lib::Platform, |
| 32 | + screen: &'static ScreenDriver, |
| 33 | + oracle: &'static capsules_extra::tutorials::encryption_oracle_chkpt5::EncryptionOracleDriver< |
| 34 | + 'static, |
| 35 | + nrf52840::aes::AesECB<'static>, |
| 36 | + >, |
| 37 | +} |
| 38 | + |
| 39 | +impl SyscallDriverLookup for Platform { |
| 40 | + fn with_driver<F, R>(&self, driver_num: usize, f: F) -> R |
| 41 | + where |
| 42 | + F: FnOnce(Option<&dyn kernel::syscall::SyscallDriver>) -> R, |
| 43 | + { |
| 44 | + match driver_num { |
| 45 | + capsules_extra::screen::DRIVER_NUM => f(Some(self.screen)), |
| 46 | + capsules_extra::tutorials::encryption_oracle_chkpt5::DRIVER_NUM => f(Some(self.oracle)), |
| 47 | + _ => self.base.with_driver(driver_num, f), |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +type Chip = nrf52840dk_lib::Chip; |
| 53 | + |
| 54 | +impl KernelResources<Chip> for Platform { |
| 55 | + type SyscallDriverLookup = Self; |
| 56 | + type SyscallFilter = <nrf52840dk_lib::Platform as KernelResources<Chip>>::SyscallFilter; |
| 57 | + type ProcessFault = <nrf52840dk_lib::Platform as KernelResources<Chip>>::ProcessFault; |
| 58 | + type Scheduler = <nrf52840dk_lib::Platform as KernelResources<Chip>>::Scheduler; |
| 59 | + type SchedulerTimer = <nrf52840dk_lib::Platform as KernelResources<Chip>>::SchedulerTimer; |
| 60 | + type WatchDog = <nrf52840dk_lib::Platform as KernelResources<Chip>>::WatchDog; |
| 61 | + type ContextSwitchCallback = |
| 62 | + <nrf52840dk_lib::Platform as KernelResources<Chip>>::ContextSwitchCallback; |
| 63 | + |
| 64 | + fn syscall_driver_lookup(&self) -> &Self::SyscallDriverLookup { |
| 65 | + self |
| 66 | + } |
| 67 | + fn syscall_filter(&self) -> &Self::SyscallFilter { |
| 68 | + self.base.syscall_filter() |
| 69 | + } |
| 70 | + fn process_fault(&self) -> &Self::ProcessFault { |
| 71 | + self.base.process_fault() |
| 72 | + } |
| 73 | + fn scheduler(&self) -> &Self::Scheduler { |
| 74 | + self.base.scheduler() |
| 75 | + } |
| 76 | + fn scheduler_timer(&self) -> &Self::SchedulerTimer { |
| 77 | + self.base.scheduler_timer() |
| 78 | + } |
| 79 | + fn watchdog(&self) -> &Self::WatchDog { |
| 80 | + self.base.watchdog() |
| 81 | + } |
| 82 | + fn context_switch_callback(&self) -> &Self::ContextSwitchCallback { |
| 83 | + self.base.context_switch_callback() |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +/// Main function called after RAM initialized. |
| 88 | +#[no_mangle] |
| 89 | +pub unsafe fn main() { |
| 90 | + let main_loop_capability = create_capability!(capabilities::MainLoopCapability); |
| 91 | + |
| 92 | + // Create the base board: |
| 93 | + let (board_kernel, base_platform, chip, nrf52840_peripherals, _mux_alarm) = |
| 94 | + nrf52840dk_lib::start_no_pconsole(); |
| 95 | + |
| 96 | + //-------------------------------------------------------------------------- |
| 97 | + // SCREEN |
| 98 | + //-------------------------------------------------------------------------- |
| 99 | + |
| 100 | + const SCREEN_I2C_SDA_PIN: Pin = Pin::P1_10; |
| 101 | + const SCREEN_I2C_SCL_PIN: Pin = Pin::P1_11; |
| 102 | + |
| 103 | + let i2c_bus = components::i2c::I2CMuxComponent::new(&nrf52840_peripherals.nrf52.twi1, None) |
| 104 | + .finalize(components::i2c_mux_component_static!(nrf52840::i2c::TWI)); |
| 105 | + nrf52840_peripherals.nrf52.twi1.configure( |
| 106 | + nrf52840::pinmux::Pinmux::new(SCREEN_I2C_SCL_PIN as u32), |
| 107 | + nrf52840::pinmux::Pinmux::new(SCREEN_I2C_SDA_PIN as u32), |
| 108 | + ); |
| 109 | + nrf52840_peripherals |
| 110 | + .nrf52 |
| 111 | + .twi1 |
| 112 | + .set_speed(nrf52840::i2c::Speed::K400); |
| 113 | + |
| 114 | + // I2C address is b011110X, and on this board D/C̅ is GND. |
| 115 | + let ssd1306_sh1106_i2c = components::i2c::I2CComponent::new(i2c_bus, 0x3c) |
| 116 | + .finalize(components::i2c_component_static!(nrf52840::i2c::TWI)); |
| 117 | + |
| 118 | + // Create the ssd1306 object for the actual screen driver. |
| 119 | + #[cfg(feature = "screen_ssd1306")] |
| 120 | + let ssd1306_sh1106 = components::ssd1306::Ssd1306Component::new(ssd1306_sh1106_i2c, true) |
| 121 | + .finalize(components::ssd1306_component_static!(nrf52840::i2c::TWI)); |
| 122 | + |
| 123 | + #[cfg(feature = "screen_sh1106")] |
| 124 | + let ssd1306_sh1106 = components::sh1106::Sh1106Component::new(ssd1306_sh1106_i2c, true) |
| 125 | + .finalize(components::sh1106_component_static!(nrf52840::i2c::TWI)); |
| 126 | + |
| 127 | + let screen = components::screen::ScreenComponent::new( |
| 128 | + board_kernel, |
| 129 | + capsules_extra::screen::DRIVER_NUM, |
| 130 | + ssd1306_sh1106, |
| 131 | + None, |
| 132 | + ) |
| 133 | + .finalize(components::screen_component_static!(1032)); |
| 134 | + |
| 135 | + ssd1306_sh1106.init_screen(); |
| 136 | + |
| 137 | + //-------------------------------------------------------------------------- |
| 138 | + // ENCRYPTION ORACLE |
| 139 | + //-------------------------------------------------------------------------- |
| 140 | + |
| 141 | + let aes_src_buffer = kernel::static_init!([u8; 16], [0; 16]); |
| 142 | + let aes_dst_buffer = kernel::static_init!([u8; CRYPT_SIZE], [0; CRYPT_SIZE]); |
| 143 | + |
| 144 | + let oracle = static_init!( |
| 145 | + capsules_extra::tutorials::encryption_oracle_chkpt5::EncryptionOracleDriver< |
| 146 | + 'static, |
| 147 | + nrf52840::aes::AesECB<'static>, |
| 148 | + >, |
| 149 | + capsules_extra::tutorials::encryption_oracle_chkpt5::EncryptionOracleDriver::new( |
| 150 | + &nrf52840_peripherals.nrf52.ecb, |
| 151 | + aes_src_buffer, |
| 152 | + aes_dst_buffer, |
| 153 | + board_kernel.create_grant( |
| 154 | + capsules_extra::tutorials::encryption_oracle_chkpt5::DRIVER_NUM, // our driver number |
| 155 | + &create_capability!(capabilities::MemoryAllocationCapability) |
| 156 | + ), |
| 157 | + ), |
| 158 | + ); |
| 159 | + |
| 160 | + kernel::hil::symmetric_encryption::AES128::set_client(&nrf52840_peripherals.nrf52.ecb, oracle); |
| 161 | + |
| 162 | + //-------------------------------------------------------------------------- |
| 163 | + // PLATFORM SETUP, SCHEDULER, AND START KERNEL LOOP |
| 164 | + //-------------------------------------------------------------------------- |
| 165 | + |
| 166 | + let platform = Platform { |
| 167 | + base: base_platform, |
| 168 | + screen, |
| 169 | + oracle, |
| 170 | + }; |
| 171 | + |
| 172 | + // These symbols are defined in the linker script. |
| 173 | + extern "C" { |
| 174 | + /// Beginning of the ROM region containing app images. |
| 175 | + static _sapps: u8; |
| 176 | + /// End of the ROM region containing app images. |
| 177 | + static _eapps: u8; |
| 178 | + /// Beginning of the RAM region for app memory. |
| 179 | + static mut _sappmem: u8; |
| 180 | + /// End of the RAM region for app memory. |
| 181 | + static _eappmem: u8; |
| 182 | + } |
| 183 | + |
| 184 | + let process_management_capability = |
| 185 | + create_capability!(capabilities::ProcessManagementCapability); |
| 186 | + |
| 187 | + kernel::process::load_processes( |
| 188 | + board_kernel, |
| 189 | + chip, |
| 190 | + core::slice::from_raw_parts( |
| 191 | + core::ptr::addr_of!(_sapps), |
| 192 | + core::ptr::addr_of!(_eapps) as usize - core::ptr::addr_of!(_sapps) as usize, |
| 193 | + ), |
| 194 | + core::slice::from_raw_parts_mut( |
| 195 | + core::ptr::addr_of_mut!(_sappmem), |
| 196 | + core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize, |
| 197 | + ), |
| 198 | + &mut *addr_of_mut!(PROCESSES), |
| 199 | + &FAULT_RESPONSE, |
| 200 | + &process_management_capability, |
| 201 | + ) |
| 202 | + .unwrap_or_else(|err| { |
| 203 | + debug!("Error loading processes!"); |
| 204 | + debug!("{:?}", err); |
| 205 | + }); |
| 206 | + |
| 207 | + board_kernel.kernel_loop( |
| 208 | + &platform, |
| 209 | + chip, |
| 210 | + Some(&platform.base.ipc), |
| 211 | + &main_loop_capability, |
| 212 | + ); |
| 213 | +} |
0 commit comments