From ce0ee6c054ce822d8a5e2285d11b4c876be49dcc Mon Sep 17 00:00:00 2001 From: kaczmarczyck <43844792+kaczmarczyck@users.noreply.github.com> Date: Mon, 28 Jun 2021 14:55:20 +0200 Subject: [PATCH] Kernel minimization (#334) * adds patch to remove unused kernel components * new boards, rebase patches to match * rename boards for deployment --- .github/workflows/boards_build.yml | 8 +- README.md | 6 +- .../nordic/nrf52840_dongle_opensk/Cargo.toml | 14 + boards/nordic/nrf52840_dongle_opensk/Makefile | 28 ++ .../nordic/nrf52840_dongle_opensk/README.md | 41 ++ boards/nordic/nrf52840_dongle_opensk/build.rs | 4 + .../jtag/gdbinit_pca10040.jlink | 25 ++ .../jtag/jdbserver_pca10040.sh | 1 + .../nordic/nrf52840_dongle_opensk/layout.ld | 2 + .../nordic/nrf52840_dongle_opensk/src/io.rs | 65 +++ .../nordic/nrf52840_dongle_opensk/src/main.rs | 311 ++++++++++++++ boards/nordic/nrf52840_mdk_dfu/src/main.rs | 40 -- boards/nordic/nrf52840dk_opensk/Cargo.toml | 14 + boards/nordic/nrf52840dk_opensk/Makefile | 31 ++ boards/nordic/nrf52840dk_opensk/README.md | 65 +++ boards/nordic/nrf52840dk_opensk/build.rs | 4 + .../jtag/gdbinit_pca10040.jlink | 25 ++ .../jtag/jdbserver_pca10040.sh | 1 + boards/nordic/nrf52840dk_opensk/layout.ld | 2 + boards/nordic/nrf52840dk_opensk/src/io.rs | 102 +++++ boards/nordic/nrf52840dk_opensk/src/main.rs | 396 ++++++++++++++++++ deploy.py | 8 +- docs/install.md | 10 +- patches/tock/01-persistent-storage.patch | 54 +-- patches/tock/02-usb.patch | 46 +- ...oards.patch => 03-additional-boards.patch} | 7 +- patches/tock/03-usb-debugging.patch | 13 - .../{05-mpu-fix.patch => 04-mpu-fix.patch} | 0 ...update-uicr.patch => 05-update-uicr.patch} | 3 +- ...rotect.patch => 06-firmware-protect.patch} | 53 ++- reproduce_hashes.sh | 2 +- run_desktop_tests.sh | 8 +- 32 files changed, 1234 insertions(+), 155 deletions(-) create mode 100644 boards/nordic/nrf52840_dongle_opensk/Cargo.toml create mode 100644 boards/nordic/nrf52840_dongle_opensk/Makefile create mode 100644 boards/nordic/nrf52840_dongle_opensk/README.md create mode 100644 boards/nordic/nrf52840_dongle_opensk/build.rs create mode 100644 boards/nordic/nrf52840_dongle_opensk/jtag/gdbinit_pca10040.jlink create mode 100755 boards/nordic/nrf52840_dongle_opensk/jtag/jdbserver_pca10040.sh create mode 100644 boards/nordic/nrf52840_dongle_opensk/layout.ld create mode 100644 boards/nordic/nrf52840_dongle_opensk/src/io.rs create mode 100644 boards/nordic/nrf52840_dongle_opensk/src/main.rs create mode 100644 boards/nordic/nrf52840dk_opensk/Cargo.toml create mode 100644 boards/nordic/nrf52840dk_opensk/Makefile create mode 100644 boards/nordic/nrf52840dk_opensk/README.md create mode 100644 boards/nordic/nrf52840dk_opensk/build.rs create mode 100644 boards/nordic/nrf52840dk_opensk/jtag/gdbinit_pca10040.jlink create mode 100755 boards/nordic/nrf52840dk_opensk/jtag/jdbserver_pca10040.sh create mode 100644 boards/nordic/nrf52840dk_opensk/layout.ld create mode 100644 boards/nordic/nrf52840dk_opensk/src/io.rs create mode 100644 boards/nordic/nrf52840dk_opensk/src/main.rs rename patches/tock/{04-additional-boards.patch => 03-additional-boards.patch} (66%) delete mode 100644 patches/tock/03-usb-debugging.patch rename patches/tock/{05-mpu-fix.patch => 04-mpu-fix.patch} (100%) rename patches/tock/{06-update-uicr.patch => 05-update-uicr.patch} (99%) rename patches/tock/{07-firmware-protect.patch => 06-firmware-protect.patch} (91%) diff --git a/.github/workflows/boards_build.yml b/.github/workflows/boards_build.yml index d798495..b48429b 100644 --- a/.github/workflows/boards_build.yml +++ b/.github/workflows/boards_build.yml @@ -29,10 +29,10 @@ jobs: - name: Set up OpenSK run: ./setup.sh - - name: Building board nrf52840dk - run: ./deploy.py --board=nrf52840dk --no-app --programmer=none - - name: Building board nrf52840_dongle - run: ./deploy.py --board=nrf52840_dongle --no-app --programmer=none + - name: Building board nrf52840dk_opensk + run: ./deploy.py --board=nrf52840dk_opensk --no-app --programmer=none + - name: Building board nrf52840_dongle_opensk + run: ./deploy.py --board=nrf52840_dongle_opensk --no-app --programmer=none - name: Building board nrf52840_dongle_dfu run: ./deploy.py --board=nrf52840_dongle_dfu --no-app --programmer=none - name: Building board nrf52840_mdk_dfu diff --git a/README.md b/README.md index 9a49826..daaec49 100644 --- a/README.md +++ b/README.md @@ -64,9 +64,9 @@ For a more detailed guide, please refer to our ```shell # Nordic nRF52840-DK board - ./deploy.py --board=nrf52840dk --opensk + ./deploy.py --board=nrf52840dk_opensk --opensk # Nordic nRF52840-Dongle - ./deploy.py --board=nrf52840_dongle --opensk + ./deploy.py --board=nrf52840_dongle_opensk --opensk ``` 1. Finally you need to inject the cryptographic material if you enabled @@ -131,7 +131,7 @@ driver, before faulting the app, you can use the `--panic-console` flag of the ```shell # Example on Nordic nRF52840-DK board -./deploy.py --board=nrf52840dk --opensk --panic-console +./deploy.py --board=nrf52840dk_opensk --opensk --panic-console ``` ### Debugging memory allocations diff --git a/boards/nordic/nrf52840_dongle_opensk/Cargo.toml b/boards/nordic/nrf52840_dongle_opensk/Cargo.toml new file mode 100644 index 0000000..f3ccc0b --- /dev/null +++ b/boards/nordic/nrf52840_dongle_opensk/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "nrf52840_dongle_opensk" +version = "0.1.0" +authors = ["Tock Project Developers "] +build = "build.rs" +edition = "2018" + +[dependencies] +components = { path = "../../components" } +cortexm4 = { path = "../../../arch/cortex-m4" } +capsules = { path = "../../../capsules" } +kernel = { path = "../../../kernel" } +nrf52840 = { path = "../../../chips/nrf52840" } +nrf52_components = { path = "../nrf52_components" } diff --git a/boards/nordic/nrf52840_dongle_opensk/Makefile b/boards/nordic/nrf52840_dongle_opensk/Makefile new file mode 100644 index 0000000..db4912a --- /dev/null +++ b/boards/nordic/nrf52840_dongle_opensk/Makefile @@ -0,0 +1,28 @@ +# Makefile for building the tock kernel for the nRF development kit + +TARGET=thumbv7em-none-eabi +PLATFORM=nrf52840_dongle_opensk + +include ../../Makefile.common + +TOCKLOADER=tockloader + +# Where in the nrf52 flash to load the kernel with `tockloader` +KERNEL_ADDRESS=0x00000 + +# Upload programs over uart with tockloader +ifdef PORT + TOCKLOADER_GENERAL_FLAGS += --port $(PORT) +endif + +TOCKLOADER_JTAG_FLAGS = --jlink --board nrf52dk + +# Upload the kernel over JTAG +.PHONY: flash +flash: $(TOCK_ROOT_DIRECTORY)target/$(TARGET)/release/$(PLATFORM).bin + $(TOCKLOADER) $(TOCKLOADER_GENERAL_FLAGS) flash --address $(KERNEL_ADDRESS) $(TOCKLOADER_JTAG_FLAGS) $< + +# Upload the kernel over serial/bootloader +.PHONY: program +program: $(TOCK_ROOT_DIRECTORY)target/$(TARGET)/release/$(PLATFORM).hex + $(error Cannot program nRF52840-Dongle over USB. Use \`make flash\` and JTAG) diff --git a/boards/nordic/nrf52840_dongle_opensk/README.md b/boards/nordic/nrf52840_dongle_opensk/README.md new file mode 100644 index 0000000..bd0d557 --- /dev/null +++ b/boards/nordic/nrf52840_dongle_opensk/README.md @@ -0,0 +1,41 @@ +Platform-Specific Instructions: nRF52840-Dongle +=================================== + +This is an adapted nrf52840\_dongle made to work with OpenSK. + +The [nRF52840 Dongle](https://www.nordicsemi.com/Software-and-Tools/Development-Kits/nRF52840-Dongle) +is a platform based around the nRF52840, an SoC with an ARM Cortex-M4 and a BLE radio. +The kit is uses a USB key form factor and includes 1 button, 1 red LED and 1 RGB LED. + +## Getting Started + +To program the nRF52840 Dongle with Tock, you will need a JLink JTAG device and the +appropriate cables. An example setup is: + +- [JLink JTAG Device](https://www.digikey.com/product-detail/en/segger-microcontroller-systems/8.08.90-J-LINK-EDU/899-1008-ND/2263130) +- [ARM to TagConnect Adapter](https://www.digikey.com/product-detail/en/tag-connect-llc/TC2050-ARM2010/TC2050-ARM2010-ND/3528170) +- [10pin TagConnect Cable](https://www.digikey.com/product-detail/en/tag-connect-llc/TC2050-IDC-NL/TC2050-IDC-NL-ND/2605367) + +Then, follow the [Tock Getting Started guide](../../../doc/Getting_Started.md) + +JTAG is the preferred method to program. The development kit has the JTAG pins exposed either +through the half-moons pads or, below the PCB, on a Tag-Connect TC2050 connector footprint. +You need to [install JTAG software](../../../doc/Getting_Started.md#optional-requirements). + +## Programming the kernel +Once you have all software installed, you should be able to simply run +make flash in this directory to install a fresh kernel. + +## Programming user-level applications +You can program an application via JTAG using `tockloader`: + +```shell +$ cd libtock-c/examples/ +$ make +$ tockloader install --jlink --board nrf52dk +``` + +## Debugging + +See the [nrf52dk README](../nrf52dk/README.md) for information about debugging +the nRF52840 Dongle. diff --git a/boards/nordic/nrf52840_dongle_opensk/build.rs b/boards/nordic/nrf52840_dongle_opensk/build.rs new file mode 100644 index 0000000..1fdd492 --- /dev/null +++ b/boards/nordic/nrf52840_dongle_opensk/build.rs @@ -0,0 +1,4 @@ +fn main() { + println!("cargo:rerun-if-changed=layout.ld"); + println!("cargo:rerun-if-changed=../../kernel_layout.ld"); +} diff --git a/boards/nordic/nrf52840_dongle_opensk/jtag/gdbinit_pca10040.jlink b/boards/nordic/nrf52840_dongle_opensk/jtag/gdbinit_pca10040.jlink new file mode 100644 index 0000000..23a8c5b --- /dev/null +++ b/boards/nordic/nrf52840_dongle_opensk/jtag/gdbinit_pca10040.jlink @@ -0,0 +1,25 @@ +# +# +# +# J-LINK GDB SERVER initialization +# +# This connects to a GDB Server listening +# for commands on localhost at tcp port 2331 +target remote localhost:2331 +monitor speed 30 +file ../../../../target/thumbv7em-none-eabi/release/nrf52840_dongle +monitor reset +# +# CPU core initialization (to be done by user) +# +# Set the processor mode +# monitor reg cpsr = 0xd3 +# Set auto JTAG speed +monitor speed auto +# Setup GDB FOR FASTER DOWNLOADS +set remote memory-write-packet-size 1024 +set remote memory-write-packet-size fixed +# tui enable +# layout split +# layout service_pending_interrupts +b reset_handler diff --git a/boards/nordic/nrf52840_dongle_opensk/jtag/jdbserver_pca10040.sh b/boards/nordic/nrf52840_dongle_opensk/jtag/jdbserver_pca10040.sh new file mode 100755 index 0000000..1c9cd9b --- /dev/null +++ b/boards/nordic/nrf52840_dongle_opensk/jtag/jdbserver_pca10040.sh @@ -0,0 +1 @@ +JLinkGDBServer -device nRF52840_xxAA -speed 1200 -if swd -AutoConnect 1 -port 2331 diff --git a/boards/nordic/nrf52840_dongle_opensk/layout.ld b/boards/nordic/nrf52840_dongle_opensk/layout.ld new file mode 100644 index 0000000..a8da375 --- /dev/null +++ b/boards/nordic/nrf52840_dongle_opensk/layout.ld @@ -0,0 +1,2 @@ +INCLUDE ../nrf52840_chip_layout.ld +INCLUDE ../../kernel_layout.ld diff --git a/boards/nordic/nrf52840_dongle_opensk/src/io.rs b/boards/nordic/nrf52840_dongle_opensk/src/io.rs new file mode 100644 index 0000000..e8bafa5 --- /dev/null +++ b/boards/nordic/nrf52840_dongle_opensk/src/io.rs @@ -0,0 +1,65 @@ +use core::fmt::Write; +use core::panic::PanicInfo; +use cortexm4; +use kernel::debug; +use kernel::debug::IoWrite; +use kernel::hil::led; +use kernel::hil::uart::{self, Configure}; +use nrf52840::gpio::Pin; + +use crate::CHIP; +use crate::PROCESSES; + +struct Writer { + initialized: bool, +} + +static mut WRITER: Writer = Writer { initialized: false }; + +impl Write for Writer { + fn write_str(&mut self, s: &str) -> ::core::fmt::Result { + self.write(s.as_bytes()); + Ok(()) + } +} + +impl IoWrite for Writer { + fn write(&mut self, buf: &[u8]) { + let uart = unsafe { &mut nrf52840::uart::UARTE0 }; + if !self.initialized { + self.initialized = true; + uart.configure(uart::Parameters { + baud_rate: 115200, + stop_bits: uart::StopBits::One, + parity: uart::Parity::None, + hw_flow_control: false, + width: uart::Width::Eight, + }); + } + for &c in buf { + unsafe { + uart.send_byte(c); + } + while !uart.tx_ready() {} + } + } +} + +#[cfg(not(test))] +#[no_mangle] +#[panic_handler] +/// Panic handler +pub unsafe extern "C" fn panic_fmt(pi: &PanicInfo) -> ! { + // The nRF52840 Dongle LEDs (see back of board) + const LED1_PIN: Pin = Pin::P0_06; + let led = &mut led::LedLow::new(&mut nrf52840::gpio::PORT[LED1_PIN]); + let writer = &mut WRITER; + debug::panic( + &mut [led], + writer, + pi, + &cortexm4::support::nop, + &PROCESSES, + &CHIP, + ) +} diff --git a/boards/nordic/nrf52840_dongle_opensk/src/main.rs b/boards/nordic/nrf52840_dongle_opensk/src/main.rs new file mode 100644 index 0000000..e0e292a --- /dev/null +++ b/boards/nordic/nrf52840_dongle_opensk/src/main.rs @@ -0,0 +1,311 @@ +//! Tock kernel for the Nordic Semiconductor nRF52840 dongle. +//! +//! It is based on nRF52840 SoC (Cortex M4 core with a BLE transceiver) with +//! many exported I/O and peripherals. + +#![no_std] +// Disable this attribute when documenting, as a workaround for +// https://github.com/rust-lang/rust/issues/62184. +#![cfg_attr(not(doc), no_main)] +#![feature(const_in_array_repeat_expressions)] +#![deny(missing_docs)] + +use kernel::common::dynamic_deferred_call::{DynamicDeferredCall, DynamicDeferredCallClientState}; +use kernel::component::Component; +#[allow(unused_imports)] +use kernel::{capabilities, create_capability, debug, debug_gpio, debug_verbose, static_init}; +use nrf52840::gpio::Pin; +use nrf52_components::{self, UartChannel, UartPins}; + +// The nRF52840 Dongle LEDs +const LED1_PIN: Pin = Pin::P0_06; +const LED2_R_PIN: Pin = Pin::P0_08; +const LED2_G_PIN: Pin = Pin::P1_09; +const LED2_B_PIN: Pin = Pin::P0_12; + +// The nRF52840 Dongle button +const BUTTON_PIN: Pin = Pin::P1_06; +const BUTTON_RST_PIN: Pin = Pin::P0_18; + +const UART_RTS: Option = Some(Pin::P0_13); +const UART_TXD: Pin = Pin::P0_15; +const UART_CTS: Option = Some(Pin::P0_17); +const UART_RXD: Pin = Pin::P0_20; + +// SPI pins not currently in use, but left here for convenience +const _SPI_MOSI: Pin = Pin::P1_01; +const _SPI_MISO: Pin = Pin::P1_02; +const _SPI_CLK: Pin = Pin::P1_04; + +/// UART Writer +pub mod io; + +// State for loading and holding applications. +// How should the kernel respond when a process faults. +const FAULT_RESPONSE: kernel::procs::FaultResponse = kernel::procs::FaultResponse::Panic; + +// Number of concurrent processes this platform supports. +const NUM_PROCS: usize = 8; + +static mut PROCESSES: [Option<&'static dyn kernel::procs::ProcessType>; NUM_PROCS] = + [None; NUM_PROCS]; + +// Static reference to chip for panic dumps +static mut CHIP: Option<&'static nrf52840::chip::Chip> = None; + +/// Dummy buffer that causes the linker to reserve enough space for the stack. +#[no_mangle] +#[link_section = ".stack_buffer"] +pub static mut STACK_MEMORY: [u8; 0x1000] = [0; 0x1000]; + +/// Supported drivers by the platform +pub struct Platform { + button: &'static capsules::button::Button<'static, nrf52840::gpio::GPIOPin<'static>>, + pconsole: &'static capsules::process_console::ProcessConsole< + 'static, + components::process_console::Capability, + >, + console: &'static capsules::console::Console<'static>, + gpio: &'static capsules::gpio::GPIO<'static, nrf52840::gpio::GPIOPin<'static>>, + led: &'static capsules::led::LED<'static, nrf52840::gpio::GPIOPin<'static>>, + rng: &'static capsules::rng::RngDriver<'static>, + ipc: kernel::ipc::IPC, + analog_comparator: &'static capsules::analog_comparator::AnalogComparator< + 'static, + nrf52840::acomp::Comparator<'static>, + >, + alarm: &'static capsules::alarm::AlarmDriver< + 'static, + capsules::virtual_alarm::VirtualMuxAlarm<'static, nrf52840::rtc::Rtc<'static>>, + >, +} + +impl kernel::Platform for Platform { + fn with_driver(&self, driver_num: usize, f: F) -> R + where + F: FnOnce(Option<&dyn kernel::Driver>) -> R, + { + match driver_num { + capsules::console::DRIVER_NUM => f(Some(self.console)), + capsules::gpio::DRIVER_NUM => f(Some(self.gpio)), + capsules::alarm::DRIVER_NUM => f(Some(self.alarm)), + capsules::led::DRIVER_NUM => f(Some(self.led)), + capsules::button::DRIVER_NUM => f(Some(self.button)), + capsules::rng::DRIVER_NUM => f(Some(self.rng)), + capsules::analog_comparator::DRIVER_NUM => f(Some(self.analog_comparator)), + kernel::ipc::DRIVER_NUM => f(Some(&self.ipc)), + _ => f(None), + } + } +} + +/// Entry point in the vector table called on hard reset. +#[no_mangle] +pub unsafe fn reset_handler() { + // Loads relocations and clears BSS + nrf52840::init(); + + let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(&PROCESSES)); + + // GPIOs + let gpio = components::gpio::GpioComponent::new( + board_kernel, + components::gpio_component_helper!( + nrf52840::gpio::GPIOPin, + // left side of the USB plug + 0 => &nrf52840::gpio::PORT[Pin::P0_13], + 1 => &nrf52840::gpio::PORT[Pin::P0_15], + 2 => &nrf52840::gpio::PORT[Pin::P0_17], + 3 => &nrf52840::gpio::PORT[Pin::P0_20], + 4 => &nrf52840::gpio::PORT[Pin::P0_22], + 5 => &nrf52840::gpio::PORT[Pin::P0_24], + 6 => &nrf52840::gpio::PORT[Pin::P1_00], + 7 => &nrf52840::gpio::PORT[Pin::P0_09], + 8 => &nrf52840::gpio::PORT[Pin::P0_10], + // right side of the USB plug + 9 => &nrf52840::gpio::PORT[Pin::P0_31], + 10 => &nrf52840::gpio::PORT[Pin::P0_29], + 11 => &nrf52840::gpio::PORT[Pin::P0_02], + 12 => &nrf52840::gpio::PORT[Pin::P1_15], + 13 => &nrf52840::gpio::PORT[Pin::P1_13], + 14 => &nrf52840::gpio::PORT[Pin::P1_10], + // Below the PCB + 15 => &nrf52840::gpio::PORT[Pin::P0_26], + 16 => &nrf52840::gpio::PORT[Pin::P0_04], + 17 => &nrf52840::gpio::PORT[Pin::P0_11], + 18 => &nrf52840::gpio::PORT[Pin::P0_14], + 19 => &nrf52840::gpio::PORT[Pin::P1_11], + 20 => &nrf52840::gpio::PORT[Pin::P1_07], + 21 => &nrf52840::gpio::PORT[Pin::P1_01], + 22 => &nrf52840::gpio::PORT[Pin::P1_04], + 23 => &nrf52840::gpio::PORT[Pin::P1_02] + ), + ) + .finalize(components::gpio_component_buf!(nrf52840::gpio::GPIOPin)); + + let button = components::button::ButtonComponent::new( + board_kernel, + components::button_component_helper!( + nrf52840::gpio::GPIOPin, + ( + &nrf52840::gpio::PORT[BUTTON_PIN], + kernel::hil::gpio::ActivationMode::ActiveLow, + kernel::hil::gpio::FloatingState::PullUp + ) + ), + ) + .finalize(components::button_component_buf!(nrf52840::gpio::GPIOPin)); + + let led = components::led::LedsComponent::new(components::led_component_helper!( + nrf52840::gpio::GPIOPin, + ( + &nrf52840::gpio::PORT[LED1_PIN], + kernel::hil::gpio::ActivationMode::ActiveLow + ), + ( + &nrf52840::gpio::PORT[LED2_R_PIN], + kernel::hil::gpio::ActivationMode::ActiveLow + ), + ( + &nrf52840::gpio::PORT[LED2_G_PIN], + kernel::hil::gpio::ActivationMode::ActiveLow + ), + ( + &nrf52840::gpio::PORT[LED2_B_PIN], + kernel::hil::gpio::ActivationMode::ActiveLow + ) + )) + .finalize(components::led_component_buf!(nrf52840::gpio::GPIOPin)); + + let chip = static_init!(nrf52840::chip::Chip, nrf52840::chip::new()); + CHIP = Some(chip); + + nrf52_components::startup::NrfStartupComponent::new( + false, + BUTTON_RST_PIN, + nrf52840::uicr::Regulator0Output::V3_0, + ) + .finalize(()); + + // Create capabilities that the board needs to call certain protected kernel + // functions. + let process_management_capability = + create_capability!(capabilities::ProcessManagementCapability); + let main_loop_capability = create_capability!(capabilities::MainLoopCapability); + let memory_allocation_capability = create_capability!(capabilities::MemoryAllocationCapability); + + let gpio_port = &nrf52840::gpio::PORT; + + // Configure kernel debug gpios as early as possible + kernel::debug::assign_gpios( + Some(&gpio_port[LED2_R_PIN]), + Some(&gpio_port[LED2_G_PIN]), + Some(&gpio_port[LED2_B_PIN]), + ); + + let rtc = &nrf52840::rtc::RTC; + rtc.start(); + let mux_alarm = components::alarm::AlarmMuxComponent::new(rtc) + .finalize(components::alarm_mux_component_helper!(nrf52840::rtc::Rtc)); + let alarm = components::alarm::AlarmDriverComponent::new(board_kernel, mux_alarm) + .finalize(components::alarm_component_helper!(nrf52840::rtc::Rtc)); + let uart_channel = UartChannel::Pins(UartPins::new(UART_RTS, UART_TXD, UART_CTS, UART_RXD)); + let channel = nrf52_components::UartChannelComponent::new(uart_channel, mux_alarm).finalize(()); + + 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); + + // Create a shared UART channel for the console and for kernel debug. + let uart_mux = + components::console::UartMuxComponent::new(channel, 115200, dynamic_deferred_caller) + .finalize(()); + + let pconsole = + components::process_console::ProcessConsoleComponent::new(board_kernel, uart_mux) + .finalize(()); + + // Setup the console. + let console = components::console::ConsoleComponent::new(board_kernel, uart_mux).finalize(()); + // Create the debugger object that handles calls to `debug!()`. + components::debug_writer::DebugWriterComponent::new(uart_mux).finalize(()); + + let rng = components::rng::RngComponent::new(board_kernel, &nrf52840::trng::TRNG).finalize(()); + + // Initialize AC using AIN5 (P0.29) as VIN+ and VIN- as AIN0 (P0.02) + // These are hardcoded pin assignments specified in the driver + let analog_comparator = components::analog_comparator::AcComponent::new( + &nrf52840::acomp::ACOMP, + components::acomp_component_helper!( + nrf52840::acomp::Channel, + &nrf52840::acomp::CHANNEL_AC0 + ), + ) + .finalize(components::acomp_component_buf!( + nrf52840::acomp::Comparator + )); + + nrf52_components::NrfClockComponent::new().finalize(()); + + let platform = Platform { + button, + pconsole, + console, + led, + gpio, + rng, + alarm, + analog_comparator, + ipc: kernel::ipc::IPC::new(board_kernel, &memory_allocation_capability), + }; + + platform.pconsole.start(); + debug!("Initialization complete. Entering main loop\r"); + debug!("{}", &nrf52840::ficr::FICR_INSTANCE); + + /// These symbols are defined in the linker script. + extern "C" { + /// Beginning of the ROM region containing app images. + static _sapps: u8; + /// End of the ROM region containing app images. + static _eapps: u8; + /// Beginning of the RAM region for app memory. + static mut _sappmem: u8; + /// End of the RAM region for app memory. + 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( + &platform, + chip, + Some(&platform.ipc), + scheduler, + &main_loop_capability, + ); +} diff --git a/boards/nordic/nrf52840_mdk_dfu/src/main.rs b/boards/nordic/nrf52840_mdk_dfu/src/main.rs index 1eccb0e..daaeaed 100644 --- a/boards/nordic/nrf52840_mdk_dfu/src/main.rs +++ b/boards/nordic/nrf52840_mdk_dfu/src/main.rs @@ -10,7 +10,6 @@ #![feature(const_in_array_repeat_expressions)] #![deny(missing_docs)] -use capsules::virtual_alarm::VirtualMuxAlarm; use kernel::common::dynamic_deferred_call::{DynamicDeferredCall, DynamicDeferredCallClientState}; use kernel::component::Component; #[allow(unused_imports)] @@ -33,10 +32,6 @@ const UART_TXD: Pin = Pin::P0_20; const UART_CTS: Option = Some(Pin::P0_03); const UART_RXD: Pin = Pin::P0_19; -// Constants related to the configuration of the 15.4 network stack -const SRC_MAC: u16 = 0xf00f; -const PAN_ID: u16 = 0xABCD; - /// UART Writer pub mod io; @@ -76,12 +71,6 @@ pub static mut STACK_MEMORY: [u8; 0x1000] = [0; 0x1000]; /// Supported drivers by the platform pub struct Platform { - ble_radio: &'static capsules::ble_advertising_driver::BLE< - 'static, - nrf52840::ble_radio::Radio<'static>, - VirtualMuxAlarm<'static, nrf52840::rtc::Rtc<'static>>, - >, - ieee802154_radio: &'static capsules::ieee802154::RadioDriver<'static>, button: &'static capsules::button::Button<'static, nrf52840::gpio::GPIOPin<'static>>, pconsole: &'static capsules::process_console::ProcessConsole< 'static, @@ -91,7 +80,6 @@ pub struct Platform { gpio: &'static capsules::gpio::GPIO<'static, nrf52840::gpio::GPIOPin<'static>>, led: &'static capsules::led::LED<'static, nrf52840::gpio::GPIOPin<'static>>, rng: &'static capsules::rng::RngDriver<'static>, - temp: &'static capsules::temperature::TemperatureSensor<'static>, ipc: kernel::ipc::IPC, analog_comparator: &'static capsules::analog_comparator::AnalogComparator< 'static, @@ -121,9 +109,6 @@ impl kernel::Platform for Platform { capsules::led::DRIVER_NUM => f(Some(self.led)), capsules::button::DRIVER_NUM => f(Some(self.button)), capsules::rng::DRIVER_NUM => f(Some(self.rng)), - capsules::ble_advertising_driver::DRIVER_NUM => f(Some(self.ble_radio)), - capsules::ieee802154::DRIVER_NUM => f(Some(self.ieee802154_radio)), - capsules::temperature::DRIVER_NUM => f(Some(self.temp)), capsules::analog_comparator::DRIVER_NUM => f(Some(self.analog_comparator)), nrf52840::nvmc::DRIVER_NUM => f(Some(self.nvmc)), capsules::usb::usb_ctap::DRIVER_NUM => f(Some(self.usb)), @@ -263,28 +248,6 @@ pub unsafe fn reset_handler() { // Create the debugger object that handles calls to `debug!()`. components::debug_writer::DebugWriterComponent::new(uart_mux).finalize(()); - let ble_radio = - nrf52_components::BLEComponent::new(board_kernel, &nrf52840::ble_radio::RADIO, mux_alarm) - .finalize(()); - - let (ieee802154_radio, _mux_mac) = components::ieee802154::Ieee802154Component::new( - board_kernel, - &nrf52840::ieee802154_radio::RADIO, - &nrf52840::aes::AESECB, - PAN_ID, - SRC_MAC, - ) - .finalize(components::ieee802154_component_helper!( - nrf52840::ieee802154_radio::Radio, - nrf52840::aes::AesECB<'static> - )); - - let temp = components::temperature::TemperatureComponent::new( - board_kernel, - &nrf52840::temperature::TEMP, - ) - .finalize(()); - let rng = components::rng::RngComponent::new(board_kernel, &nrf52840::trng::TRNG).finalize(()); // Initialize AC using AIN5 (P0.29) as VIN+ and VIN- as AIN0 (P0.02) @@ -355,14 +318,11 @@ pub unsafe fn reset_handler() { let platform = Platform { button, - ble_radio, - ieee802154_radio, pconsole, console, led, gpio, rng, - temp, alarm, analog_comparator, nvmc, diff --git a/boards/nordic/nrf52840dk_opensk/Cargo.toml b/boards/nordic/nrf52840dk_opensk/Cargo.toml new file mode 100644 index 0000000..9e72444 --- /dev/null +++ b/boards/nordic/nrf52840dk_opensk/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "nrf52840dk_opensk" +version = "0.1.0" +authors = ["Tock Project Developers "] +build = "build.rs" +edition = "2018" + +[dependencies] +components = { path = "../../components" } +cortexm4 = { path = "../../../arch/cortex-m4" } +capsules = { path = "../../../capsules" } +kernel = { path = "../../../kernel" } +nrf52840 = { path = "../../../chips/nrf52840" } +nrf52_components = { path = "../nrf52_components" } diff --git a/boards/nordic/nrf52840dk_opensk/Makefile b/boards/nordic/nrf52840dk_opensk/Makefile new file mode 100644 index 0000000..aec4fe1 --- /dev/null +++ b/boards/nordic/nrf52840dk_opensk/Makefile @@ -0,0 +1,31 @@ +# Makefile for building the tock kernel for the nRF development kit + +TARGET=thumbv7em-none-eabi +PLATFORM=nrf52840dk_opensk + +include ../../Makefile.common + +TOCKLOADER=tockloader + +# Where in the SAM4L flash to load the kernel with `tockloader` +KERNEL_ADDRESS=0x00000 + +# Upload programs over uart with tockloader +ifdef PORT + TOCKLOADER_GENERAL_FLAGS += --port $(PORT) +endif + +# Upload the kernel over JTAG +.PHONY: flash +flash: $(TOCK_ROOT_DIRECTORY)target/$(TARGET)/release/$(PLATFORM).bin + $(TOCKLOADER) $(TOCKLOADER_GENERAL_FLAGS) flash --address $(KERNEL_ADDRESS) --board nrf52dk --jlink $< + +# Upload the kernel over JTAG using OpenOCD +.PHONY: flash-openocd +flash-openocd: $(TOCK_ROOT_DIRECTORY)target/$(TARGET)/release/$(PLATFORM).bin + $(TOCKLOADER) $(TOCKLOADER_GENERAL_FLAGS) flash --address $(KERNEL_ADDRESS) --board nrf52dk --openocd $< + +# Upload the kernel over serial/bootloader +.PHONY: program +program: $(TOCK_ROOT_DIRECTORY)target/$(TARGET)/release/$(PLATFORM).hex + $(error Cannot program nRF52840DK over USB. Use \`make flash\` and JTAG) diff --git a/boards/nordic/nrf52840dk_opensk/README.md b/boards/nordic/nrf52840dk_opensk/README.md new file mode 100644 index 0000000..81c0fe2 --- /dev/null +++ b/boards/nordic/nrf52840dk_opensk/README.md @@ -0,0 +1,65 @@ +Platform-Specific Instructions: nRF52840-DK +=================================== + +This is an adapted nrf52840dk made to work with OpenSK. + +The [nRF52840 Development +Kit](https://www.nordicsemi.com/Software-and-Tools/Development-Kits/nRF52840-DK) is a platform +based around the nRF52840, an SoC with an ARM Cortex-M4 and a BLE +radio. The kit is Arduino shield compatible and includes several +buttons. + +## Getting Started + +First, follow the [Tock Getting Started guide](../../../doc/Getting_Started.md) + +JTAG is the preferred method to program. The development kit has an +integrated JTAG debugger, you simply need to [install JTAG +software](../../../doc/Getting_Started.md#loading-the-kernel-onto-a-board). + +## Programming the kernel +Once you have all software installed, you should be able to simply run +make flash in this directory to install a fresh kernel. + +## Programming user-level applications +You can program an application over USB using the integrated JTAG and `tockloader`: + +```bash +$ cd libtock-c/examples/ +$ make +$ tockloader install --jlink --board nrf52dk +``` + +The same options (`--jlink --board nrf52dk`) must be passed for other tockloader commands +such as `erase-apps` or `list`. + +Viewing console output on the nrf52840dk is slightly different from other boards. You must use +```bash +$ tockloader listen +``` +**followed by a press of the reset button** in order to view console output starting from the boot +sequence. Notably, you should not +pass the `--jlink` option to `tockloader listen`. + +## Console output + +This board supports two methods for writing messages to a console interface +(console driver for applications as well as debug statements in the kernel). + +By default, messages are written to a UART interface over the GPIO pins `P0.05` +to `P0.08` (see the [main.rs](src/main.rs) file). + +If you don't have any UART cables or want to use a different interface, there is +also a console over the Segger RTT protocol. This only requires a micro-USB +cable on the USB debugging port (the same used to flash Tock on the board), and +is enabled by setting the `USB_DEBUGGING` constant to `true` in the +[main.rs](src/main.rs) file. +This disables the UART interface. + +For instructions about how to receive RTT messages on the host, see the +[corresponding capsule](../../../capsules/src/segger_rtt.rs). + +## Debugging + +See the [nrf52dk README](../nrf52dk/README.md) for information about debugging +the nRF52840dk. diff --git a/boards/nordic/nrf52840dk_opensk/build.rs b/boards/nordic/nrf52840dk_opensk/build.rs new file mode 100644 index 0000000..1fdd492 --- /dev/null +++ b/boards/nordic/nrf52840dk_opensk/build.rs @@ -0,0 +1,4 @@ +fn main() { + println!("cargo:rerun-if-changed=layout.ld"); + println!("cargo:rerun-if-changed=../../kernel_layout.ld"); +} diff --git a/boards/nordic/nrf52840dk_opensk/jtag/gdbinit_pca10040.jlink b/boards/nordic/nrf52840dk_opensk/jtag/gdbinit_pca10040.jlink new file mode 100644 index 0000000..a4f2173 --- /dev/null +++ b/boards/nordic/nrf52840dk_opensk/jtag/gdbinit_pca10040.jlink @@ -0,0 +1,25 @@ +# +# +# +# J-LINK GDB SERVER initialization +# +# This connects to a GDB Server listening +# for commands on localhost at tcp port 2331 +target remote localhost:2331 +monitor speed 30 +file ../../../../target/thumbv7em-none-eabi/release/nrf52dk +monitor reset +# +# CPU core initialization (to be done by user) +# +# Set the processor mode +# monitor reg cpsr = 0xd3 +# Set auto JTAG speed +monitor speed auto +# Setup GDB FOR FASTER DOWNLOADS +set remote memory-write-packet-size 1024 +set remote memory-write-packet-size fixed +# tui enable +# layout split +# layout service_pending_interrupts +b reset_handler diff --git a/boards/nordic/nrf52840dk_opensk/jtag/jdbserver_pca10040.sh b/boards/nordic/nrf52840dk_opensk/jtag/jdbserver_pca10040.sh new file mode 100755 index 0000000..b4bee90 --- /dev/null +++ b/boards/nordic/nrf52840dk_opensk/jtag/jdbserver_pca10040.sh @@ -0,0 +1 @@ +JLinkGDBServer -device nrf52 -speed 1200 -if swd -AutoConnect 1 -port 2331 diff --git a/boards/nordic/nrf52840dk_opensk/layout.ld b/boards/nordic/nrf52840dk_opensk/layout.ld new file mode 100644 index 0000000..a8da375 --- /dev/null +++ b/boards/nordic/nrf52840dk_opensk/layout.ld @@ -0,0 +1,2 @@ +INCLUDE ../nrf52840_chip_layout.ld +INCLUDE ../../kernel_layout.ld diff --git a/boards/nordic/nrf52840dk_opensk/src/io.rs b/boards/nordic/nrf52840dk_opensk/src/io.rs new file mode 100644 index 0000000..88a697d --- /dev/null +++ b/boards/nordic/nrf52840dk_opensk/src/io.rs @@ -0,0 +1,102 @@ +use core::fmt::Write; +use core::panic::PanicInfo; +use cortexm4; +use kernel::debug; +use kernel::debug::IoWrite; +use kernel::hil::led; +use kernel::hil::uart::{self, Configure}; +use nrf52840::gpio::Pin; + +use crate::CHIP; +use crate::PROCESSES; + +enum Writer { + WriterUart(/* initialized */ bool), + WriterRtt(&'static capsules::segger_rtt::SeggerRttMemory<'static>), +} + +static mut WRITER: Writer = Writer::WriterUart(false); + +fn wait() { + for _ in 0..100 { + cortexm4::support::nop(); + } +} + +/// Set the RTT memory buffer used to output panic messages. +pub unsafe fn set_rtt_memory( + rtt_memory: &'static mut capsules::segger_rtt::SeggerRttMemory<'static>, +) { + WRITER = Writer::WriterRtt(rtt_memory); +} + +impl Write for Writer { + fn write_str(&mut self, s: &str) -> ::core::fmt::Result { + self.write(s.as_bytes()); + Ok(()) + } +} + +impl IoWrite for Writer { + fn write(&mut self, buf: &[u8]) { + match self { + Writer::WriterUart(ref mut initialized) => { + let uart = unsafe { &mut nrf52840::uart::UARTE0 }; + if !*initialized { + *initialized = true; + uart.configure(uart::Parameters { + baud_rate: 115200, + stop_bits: uart::StopBits::One, + parity: uart::Parity::None, + hw_flow_control: false, + width: uart::Width::Eight, + }); + } + for &c in buf { + unsafe { + uart.send_byte(c); + } + while !uart.tx_ready() {} + } + } + Writer::WriterRtt(rtt_memory) => { + let up_buffer = unsafe { &*rtt_memory.get_up_buffer_ptr() }; + let buffer_len = up_buffer.length.get(); + let buffer = unsafe { + core::slice::from_raw_parts_mut( + up_buffer.buffer.get() as *mut u8, + buffer_len as usize, + ) + }; + + let mut write_position = up_buffer.write_position.get(); + + for &c in buf { + buffer[write_position as usize] = c; + write_position = (write_position + 1) % buffer_len; + up_buffer.write_position.set(write_position); + wait(); + } + } + }; + } +} + +#[cfg(not(test))] +#[no_mangle] +#[panic_handler] +/// Panic handler +pub unsafe extern "C" fn panic_fmt(pi: &PanicInfo) -> ! { + // The nRF52840DK LEDs (see back of board) + const LED1_PIN: Pin = Pin::P0_13; + let led = &mut led::LedLow::new(&mut nrf52840::gpio::PORT[LED1_PIN]); + let writer = &mut WRITER; + debug::panic( + &mut [led], + writer, + pi, + &cortexm4::support::nop, + &PROCESSES, + &CHIP, + ) +} diff --git a/boards/nordic/nrf52840dk_opensk/src/main.rs b/boards/nordic/nrf52840dk_opensk/src/main.rs new file mode 100644 index 0000000..8e42380 --- /dev/null +++ b/boards/nordic/nrf52840dk_opensk/src/main.rs @@ -0,0 +1,396 @@ +//! Tock kernel for the Nordic Semiconductor nRF52840 development kit (DK). +//! +//! It is based on nRF52840 SoC (Cortex M4 core with a BLE transceiver) with +//! many exported I/O and peripherals. +//! +//! Pin Configuration +//! ------------------- +//! +//! ### `GPIO` +//! +//! | # | Pin | Ix | Header | Arduino | +//! |----|-------|----|--------|---------| +//! | 0 | P1.01 | 33 | P3 1 | D0 | +//! | 1 | P1.02 | 34 | P3 2 | D1 | +//! | 2 | P1.03 | 35 | P3 3 | D2 | +//! | 3 | P1.04 | 36 | P3 4 | D3 | +//! | 4 | P1.05 | 37 | P3 5 | D4 | +//! | 5 | P1.06 | 38 | P3 6 | D5 | +//! | 6 | P1.07 | 39 | P3 7 | D6 | +//! | 7 | P1.08 | 40 | P3 8 | D7 | +//! | 8 | P1.10 | 42 | P4 1 | D8 | +//! | 9 | P1.11 | 43 | P4 2 | D9 | +//! | 10 | P1.12 | 44 | P4 3 | D10 | +//! | 11 | P1.13 | 45 | P4 4 | D11 | +//! | 12 | P1.14 | 46 | P4 5 | D12 | +//! | 13 | P1.15 | 47 | P4 6 | D13 | +//! | 14 | P0.26 | 26 | P4 9 | D14 | +//! | 15 | P0.27 | 27 | P4 10 | D15 | +//! +//! ### `GPIO` / Analog Inputs +//! +//! | # | Pin | Header | Arduino | +//! |----|------------|--------|---------| +//! | 16 | P0.03 AIN1 | P2 1 | A0 | +//! | 17 | P0.04 AIN2 | P2 2 | A1 | +//! | 18 | P0.28 AIN4 | P2 3 | A2 | +//! | 19 | P0.29 AIN5 | P2 4 | A3 | +//! | 20 | P0.30 AIN6 | P2 5 | A4 | +//! | 21 | P0.31 AIN7 | P2 6 | A5 | +//! | 22 | P0.02 AIN0 | P4 8 | AVDD | +//! +//! ### Onboard Functions +//! +//! | Pin | Header | Function | +//! |-------|--------|----------| +//! | P0.05 | P6 3 | UART RTS | +//! | P0.06 | P6 4 | UART TXD | +//! | P0.07 | P6 5 | UART CTS | +//! | P0.08 | P6 6 | UART RXT | +//! | P0.11 | P24 1 | Button 1 | +//! | P0.12 | P24 2 | Button 2 | +//! | P0.13 | P24 3 | LED 1 | +//! | P0.14 | P24 4 | LED 2 | +//! | P0.15 | P24 5 | LED 3 | +//! | P0.16 | P24 6 | LED 4 | +//! | P0.18 | P24 8 | Reset | +//! | P0.19 | P24 9 | SPI CLK | +//! | P0.20 | P24 10 | SPI MOSI | +//! | P0.21 | P24 11 | SPI MISO | +//! | P0.24 | P24 14 | Button 3 | +//! | P0.25 | P24 15 | Button 4 | + +#![no_std] +// Disable this attribute when documenting, as a workaround for +// https://github.com/rust-lang/rust/issues/62184. +#![cfg_attr(not(doc), no_main)] +#![feature(const_in_array_repeat_expressions)] +#![deny(missing_docs)] + +use kernel::common::dynamic_deferred_call::{DynamicDeferredCall, DynamicDeferredCallClientState}; +use kernel::component::Component; +#[allow(unused_imports)] +use kernel::{capabilities, create_capability, debug, debug_gpio, debug_verbose, static_init}; +use nrf52840::gpio::Pin; +use nrf52_components::{self, UartChannel, UartPins}; + +// The nRF52840DK LEDs (see back of board) +const LED1_PIN: Pin = Pin::P0_13; +const LED2_PIN: Pin = Pin::P0_14; +const LED3_PIN: Pin = Pin::P0_15; +const LED4_PIN: Pin = Pin::P0_16; + +// The nRF52840DK buttons (see back of board) +const BUTTON1_PIN: Pin = Pin::P0_11; +const BUTTON2_PIN: Pin = Pin::P0_12; +const BUTTON3_PIN: Pin = Pin::P0_24; +const BUTTON4_PIN: Pin = Pin::P0_25; +const BUTTON_RST_PIN: Pin = Pin::P0_18; + +const UART_RTS: Option = Some(Pin::P0_05); +const UART_TXD: Pin = Pin::P0_06; +const UART_CTS: Option = Some(Pin::P0_07); +const UART_RXD: Pin = Pin::P0_08; + +const SPI_MOSI: Pin = Pin::P0_20; +const SPI_MISO: Pin = Pin::P0_21; +const SPI_CLK: Pin = Pin::P0_19; + +/// Debug Writer +pub mod io; + +// Whether to use UART debugging or Segger RTT (USB) debugging. +// - Set to false to use UART. +// - Set to true to use Segger RTT over USB. +const USB_DEBUGGING: bool = true; + +// State for loading and holding applications. +// How should the kernel respond when a process faults. +const FAULT_RESPONSE: kernel::procs::FaultResponse = kernel::procs::FaultResponse::Panic; + +// Number of concurrent processes this platform supports. +const NUM_PROCS: usize = 8; + +static mut PROCESSES: [Option<&'static dyn kernel::procs::ProcessType>; NUM_PROCS] = + [None; NUM_PROCS]; + +static mut CHIP: Option<&'static nrf52840::chip::Chip> = None; + +/// Dummy buffer that causes the linker to reserve enough space for the stack. +#[no_mangle] +#[link_section = ".stack_buffer"] +pub static mut STACK_MEMORY: [u8; 0x1000] = [0; 0x1000]; + +/// Supported drivers by the platform +pub struct Platform { + button: &'static capsules::button::Button<'static, nrf52840::gpio::GPIOPin<'static>>, + pconsole: &'static capsules::process_console::ProcessConsole< + 'static, + components::process_console::Capability, + >, + console: &'static capsules::console::Console<'static>, + gpio: &'static capsules::gpio::GPIO<'static, nrf52840::gpio::GPIOPin<'static>>, + led: &'static capsules::led::LED<'static, nrf52840::gpio::GPIOPin<'static>>, + rng: &'static capsules::rng::RngDriver<'static>, + ipc: kernel::ipc::IPC, + analog_comparator: &'static capsules::analog_comparator::AnalogComparator< + 'static, + nrf52840::acomp::Comparator<'static>, + >, + alarm: &'static capsules::alarm::AlarmDriver< + 'static, + capsules::virtual_alarm::VirtualMuxAlarm<'static, nrf52840::rtc::Rtc<'static>>, + >, +} + +impl kernel::Platform for Platform { + fn with_driver(&self, driver_num: usize, f: F) -> R + where + F: FnOnce(Option<&dyn kernel::Driver>) -> R, + { + match driver_num { + capsules::console::DRIVER_NUM => f(Some(self.console)), + capsules::gpio::DRIVER_NUM => f(Some(self.gpio)), + capsules::alarm::DRIVER_NUM => f(Some(self.alarm)), + capsules::led::DRIVER_NUM => f(Some(self.led)), + capsules::button::DRIVER_NUM => f(Some(self.button)), + capsules::rng::DRIVER_NUM => f(Some(self.rng)), + capsules::analog_comparator::DRIVER_NUM => f(Some(self.analog_comparator)), + kernel::ipc::DRIVER_NUM => f(Some(&self.ipc)), + _ => f(None), + } + } +} + +/// Entry point in the vector table called on hard reset. +#[no_mangle] +pub unsafe fn reset_handler() { + // Loads relocations and clears BSS + nrf52840::init(); + + let uart_channel = if USB_DEBUGGING { + // Initialize early so any panic beyond this point can use the RTT memory object. + let mut rtt_memory_refs = + components::segger_rtt::SeggerRttMemoryComponent::new().finalize(()); + + // XXX: This is inherently unsafe as it aliases the mutable reference to rtt_memory. This + // aliases reference is only used inside a panic handler, which should be OK, but maybe we + // should use a const reference to rtt_memory and leverage interior mutability instead. + self::io::set_rtt_memory(&mut *rtt_memory_refs.get_rtt_memory_ptr()); + + UartChannel::Rtt(rtt_memory_refs) + } else { + UartChannel::Pins(UartPins::new(UART_RTS, UART_TXD, UART_CTS, UART_RXD)) + }; + + let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(&PROCESSES)); + + let gpio = components::gpio::GpioComponent::new( + board_kernel, + components::gpio_component_helper!( + nrf52840::gpio::GPIOPin, + 0 => &nrf52840::gpio::PORT[Pin::P1_01], + 1 => &nrf52840::gpio::PORT[Pin::P1_02], + 2 => &nrf52840::gpio::PORT[Pin::P1_03], + 3 => &nrf52840::gpio::PORT[Pin::P1_04], + 4 => &nrf52840::gpio::PORT[Pin::P1_05], + 5 => &nrf52840::gpio::PORT[Pin::P1_06], + 6 => &nrf52840::gpio::PORT[Pin::P1_07], + 7 => &nrf52840::gpio::PORT[Pin::P1_08], + 8 => &nrf52840::gpio::PORT[Pin::P1_10], + 9 => &nrf52840::gpio::PORT[Pin::P1_11], + 10 => &nrf52840::gpio::PORT[Pin::P1_12], + 11 => &nrf52840::gpio::PORT[Pin::P1_13], + 12 => &nrf52840::gpio::PORT[Pin::P1_14], + 13 => &nrf52840::gpio::PORT[Pin::P1_15], + 14 => &nrf52840::gpio::PORT[Pin::P0_26], + 15 => &nrf52840::gpio::PORT[Pin::P0_27] + ), + ) + .finalize(components::gpio_component_buf!(nrf52840::gpio::GPIOPin)); + + let button = components::button::ButtonComponent::new( + board_kernel, + components::button_component_helper!( + nrf52840::gpio::GPIOPin, + ( + &nrf52840::gpio::PORT[BUTTON1_PIN], + kernel::hil::gpio::ActivationMode::ActiveLow, + kernel::hil::gpio::FloatingState::PullUp + ), //13 + ( + &nrf52840::gpio::PORT[BUTTON2_PIN], + kernel::hil::gpio::ActivationMode::ActiveLow, + kernel::hil::gpio::FloatingState::PullUp + ), //14 + ( + &nrf52840::gpio::PORT[BUTTON3_PIN], + kernel::hil::gpio::ActivationMode::ActiveLow, + kernel::hil::gpio::FloatingState::PullUp + ), //15 + ( + &nrf52840::gpio::PORT[BUTTON4_PIN], + kernel::hil::gpio::ActivationMode::ActiveLow, + kernel::hil::gpio::FloatingState::PullUp + ) //16 + ), + ) + .finalize(components::button_component_buf!(nrf52840::gpio::GPIOPin)); + + let led = components::led::LedsComponent::new(components::led_component_helper!( + nrf52840::gpio::GPIOPin, + ( + &nrf52840::gpio::PORT[LED1_PIN], + kernel::hil::gpio::ActivationMode::ActiveLow + ), + ( + &nrf52840::gpio::PORT[LED2_PIN], + kernel::hil::gpio::ActivationMode::ActiveLow + ), + ( + &nrf52840::gpio::PORT[LED3_PIN], + kernel::hil::gpio::ActivationMode::ActiveLow + ), + ( + &nrf52840::gpio::PORT[LED4_PIN], + kernel::hil::gpio::ActivationMode::ActiveLow + ) + )) + .finalize(components::led_component_buf!(nrf52840::gpio::GPIOPin)); + + let chip = static_init!(nrf52840::chip::Chip, nrf52840::chip::new()); + CHIP = Some(chip); + + nrf52_components::startup::NrfStartupComponent::new( + false, + BUTTON_RST_PIN, + nrf52840::uicr::Regulator0Output::DEFAULT, + ) + .finalize(()); + + // Create capabilities that the board needs to call certain protected kernel + // functions. + let process_management_capability = + create_capability!(capabilities::ProcessManagementCapability); + let main_loop_capability = create_capability!(capabilities::MainLoopCapability); + let memory_allocation_capability = create_capability!(capabilities::MemoryAllocationCapability); + let gpio_port = &nrf52840::gpio::PORT; + // Configure kernel debug gpios as early as possible + kernel::debug::assign_gpios( + Some(&gpio_port[LED1_PIN]), + Some(&gpio_port[LED2_PIN]), + Some(&gpio_port[LED3_PIN]), + ); + + let rtc = &nrf52840::rtc::RTC; + rtc.start(); + let mux_alarm = components::alarm::AlarmMuxComponent::new(rtc) + .finalize(components::alarm_mux_component_helper!(nrf52840::rtc::Rtc)); + let alarm = components::alarm::AlarmDriverComponent::new(board_kernel, mux_alarm) + .finalize(components::alarm_component_helper!(nrf52840::rtc::Rtc)); + + let channel = nrf52_components::UartChannelComponent::new(uart_channel, mux_alarm).finalize(()); + + 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); + + // Create a shared UART channel for the console and for kernel debug. + let uart_mux = + components::console::UartMuxComponent::new(channel, 115200, dynamic_deferred_caller) + .finalize(()); + + let pconsole = + components::process_console::ProcessConsoleComponent::new(board_kernel, uart_mux) + .finalize(()); + + // Setup the console. + let console = components::console::ConsoleComponent::new(board_kernel, uart_mux).finalize(()); + // Create the debugger object that handles calls to `debug!()`. + components::debug_writer::DebugWriterComponent::new(uart_mux).finalize(()); + + let rng = components::rng::RngComponent::new(board_kernel, &nrf52840::trng::TRNG).finalize(()); + + nrf52840::spi::SPIM0.configure( + nrf52840::pinmux::Pinmux::new(SPI_MOSI as u32), + nrf52840::pinmux::Pinmux::new(SPI_MISO as u32), + nrf52840::pinmux::Pinmux::new(SPI_CLK as u32), + ); + + // Initialize AC using AIN5 (P0.29) as VIN+ and VIN- as AIN0 (P0.02) + // These are hardcoded pin assignments specified in the driver + let analog_comparator = components::analog_comparator::AcComponent::new( + &nrf52840::acomp::ACOMP, + components::acomp_component_helper!( + nrf52840::acomp::Channel, + &nrf52840::acomp::CHANNEL_AC0 + ), + ) + .finalize(components::acomp_component_buf!( + nrf52840::acomp::Comparator + )); + + nrf52_components::NrfClockComponent::new().finalize(()); + + let platform = Platform { + button, + pconsole, + console, + led, + gpio, + rng, + alarm, + analog_comparator, + ipc: kernel::ipc::IPC::new(board_kernel, &memory_allocation_capability), + }; + + platform.pconsole.start(); + debug!("Initialization complete. Entering main loop\r"); + debug!("{}", &nrf52840::ficr::FICR_INSTANCE); + + /// These symbols are defined in the linker script. + extern "C" { + /// Beginning of the ROM region containing app images. + static _sapps: u8; + /// End of the ROM region containing app images. + static _eapps: u8; + /// Beginning of the RAM region for app memory. + static mut _sappmem: u8; + /// End of the RAM region for app memory. + 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( + &platform, + chip, + Some(&platform.ipc), + scheduler, + &main_loop_capability, + ); +} diff --git a/deploy.py b/deploy.py index 398c1da..4909ef5 100755 --- a/deploy.py +++ b/deploy.py @@ -84,9 +84,9 @@ OpenSKBoard = collections.namedtuple( ]) SUPPORTED_BOARDS = { - "nrf52840dk": + "nrf52840dk_opensk": OpenSKBoard( - path="third_party/tock/boards/nordic/nrf52840dk", + path="third_party/tock/boards/nordic/nrf52840dk_opensk", arch="thumbv7em-none-eabi", page_size=4096, kernel_address=0, @@ -103,9 +103,9 @@ SUPPORTED_BOARDS = { jlink_device="nrf52840_xxaa", nordic_dfu=False, ), - "nrf52840_dongle": + "nrf52840_dongle_opensk": OpenSKBoard( - path="third_party/tock/boards/nordic/nrf52840_dongle", + path="third_party/tock/boards/nordic/nrf52840_dongle_opensk", arch="thumbv7em-none-eabi", page_size=4096, kernel_address=0, diff --git a/docs/install.md b/docs/install.md index 166d5b5..487acc3 100644 --- a/docs/install.md +++ b/docs/install.md @@ -156,18 +156,18 @@ an OpenSK device with the correct certificate and private key. (_output may differ_): ```shell - $ ./deploy.py --board=nrf52840dk --opensk + $ ./deploy.py --board=nrf52840dk_opensk --opensk info: Updating rust toolchain to nightly-2020-02-03 info: syncing channel updates for 'nightly-2020-02-03-x86_64-unknown-linux-gnu' info: checking for self-updates info: component 'rust-std' for target 'thumbv7em-none-eabi' is up to date info: Rust toolchain up-to-date - info: Building Tock OS for board nrf52840dk + info: Building Tock OS for board nrf52840dk_opensk Compiling tock-registers v0.5.0 (./third_party/tock/libraries/tock-register-interface) Compiling tock-cells v0.1.0 (./third_party/tock/libraries/tock-cells) Compiling enum_primitive v0.1.0 (./third_party/tock/libraries/enum_primitive) Compiling tock_rt0 v0.1.0 (./third_party/tock/libraries/tock-rt0) - Compiling nrf52840dk v0.1.0 (./third_party/tock/boards/nordic/nrf52840dk) + Compiling nrf52840dk_opensk v0.1.0 (./third_party/tock/boards/nordic/nrf52840dk_opensk) Compiling kernel v0.1.0 (./third_party/tock/kernel) Compiling cortexm v0.1.0 (./third_party/tock/arch/cortex-m) Compiling nrf5x v0.1.0 (./third_party/tock/chips/nrf5x) @@ -184,7 +184,7 @@ an OpenSK device with the correct certificate and private key. info: Generating Tock TAB file for application/example ctap2 info: Erasing all installed applications All apps have been erased. - info: Flashing file third_party/tock/boards/nordic/nrf52840dk/target/thumbv7em-none-eabi/release/nrf52840dk.bin. + info: Flashing file third_party/tock/boards/nordic/nrf52840dk_opensk/target/thumbv7em-none-eabi/release/nrf52840dk_opensk.bin. info: Flashing padding application info: Installing Tock application ctap2 info: You're all set! @@ -270,7 +270,7 @@ our tool, `deploy.py` also supports other methods: left to the user. If your board is already flashed with Tock OS, you may skip installing it: -`./deploy.py --board=nrf52840dk --opensk --no-tockos` +`./deploy.py --board=nrf52840dk_opensk --opensk --no-tockos` For more options, we invite you to read the help of our `deploy.py` script by running `./deploy.py --help`. diff --git a/patches/tock/01-persistent-storage.patch b/patches/tock/01-persistent-storage.patch index 0c18aa3..86e1833 100644 --- a/patches/tock/01-persistent-storage.patch +++ b/patches/tock/01-persistent-storage.patch @@ -1,8 +1,8 @@ -diff --git a/boards/nordic/nrf52840_dongle/src/main.rs b/boards/nordic/nrf52840_dongle/src/main.rs -index fc53f59c8..d72d20482 100644 ---- a/boards/nordic/nrf52840_dongle/src/main.rs -+++ b/boards/nordic/nrf52840_dongle/src/main.rs -@@ -55,6 +55,11 @@ const NUM_PROCS: usize = 8; +diff --git a/boards/nordic/nrf52840_dongle_opensk/src/main.rs b/boards/nordic/nrf52840_dongle_opensk/src/main.rs +index e0e292a7e..b485a0997 100644 +--- a/boards/nordic/nrf52840_dongle_opensk/src/main.rs ++++ b/boards/nordic/nrf52840_dongle_opensk/src/main.rs +@@ -50,6 +50,11 @@ const NUM_PROCS: usize = 8; static mut PROCESSES: [Option<&'static dyn kernel::procs::ProcessType>; NUM_PROCS] = [None; NUM_PROCS]; @@ -14,7 +14,7 @@ index fc53f59c8..d72d20482 100644 // Static reference to chip for panic dumps static mut CHIP: Option<&'static nrf52840::chip::Chip> = None; -@@ -90,6 +95,7 @@ pub struct Platform { +@@ -78,6 +83,7 @@ pub struct Platform { 'static, capsules::virtual_alarm::VirtualMuxAlarm<'static, nrf52840::rtc::Rtc<'static>>, >, @@ -22,9 +22,9 @@ index fc53f59c8..d72d20482 100644 } impl kernel::Platform for Platform { -@@ -108,10 +114,30 @@ impl kernel::Platform for Platform { - capsules::ieee802154::DRIVER_NUM => f(Some(self.ieee802154_radio)), - capsules::temperature::DRIVER_NUM => f(Some(self.temp)), +@@ -93,10 +99,30 @@ impl kernel::Platform for Platform { + capsules::button::DRIVER_NUM => f(Some(self.button)), + capsules::rng::DRIVER_NUM => f(Some(self.rng)), capsules::analog_comparator::DRIVER_NUM => f(Some(self.analog_comparator)), + nrf52840::nvmc::DRIVER_NUM => f(Some(self.nvmc)), kernel::ipc::DRIVER_NUM => f(Some(&self.ipc)), @@ -53,7 +53,7 @@ index fc53f59c8..d72d20482 100644 } /// Entry point in the vector table called on hard reset. -@@ -120,7 +146,10 @@ pub unsafe fn reset_handler() { +@@ -105,7 +131,10 @@ pub unsafe fn reset_handler() { // Loads relocations and clears BSS nrf52840::init(); @@ -65,7 +65,7 @@ index fc53f59c8..d72d20482 100644 // GPIOs let gpio = components::gpio::GpioComponent::new( -@@ -286,6 +315,14 @@ pub unsafe fn reset_handler() { +@@ -249,6 +278,14 @@ pub unsafe fn reset_handler() { nrf52840::acomp::Comparator )); @@ -80,19 +80,19 @@ index fc53f59c8..d72d20482 100644 nrf52_components::NrfClockComponent::new().finalize(()); let platform = Platform { -@@ -300,6 +337,7 @@ pub unsafe fn reset_handler() { - temp, +@@ -260,6 +297,7 @@ pub unsafe fn reset_handler() { + rng, alarm, analog_comparator, + nvmc, ipc: kernel::ipc::IPC::new(board_kernel, &memory_allocation_capability), }; -diff --git a/boards/nordic/nrf52840dk/src/main.rs b/boards/nordic/nrf52840dk/src/main.rs -index 169f3d393..2ebb384d8 100644 ---- a/boards/nordic/nrf52840dk/src/main.rs -+++ b/boards/nordic/nrf52840dk/src/main.rs -@@ -123,6 +123,11 @@ const NUM_PROCS: usize = 8; +diff --git a/boards/nordic/nrf52840dk_opensk/src/main.rs b/boards/nordic/nrf52840dk_opensk/src/main.rs +index 8e4238018..ebee8704b 100644 +--- a/boards/nordic/nrf52840dk_opensk/src/main.rs ++++ b/boards/nordic/nrf52840dk_opensk/src/main.rs +@@ -114,6 +114,11 @@ const NUM_PROCS: usize = 8; static mut PROCESSES: [Option<&'static dyn kernel::procs::ProcessType>; NUM_PROCS] = [None; NUM_PROCS]; @@ -104,18 +104,18 @@ index 169f3d393..2ebb384d8 100644 static mut CHIP: Option<&'static nrf52840::chip::Chip> = None; /// Dummy buffer that causes the linker to reserve enough space for the stack. -@@ -158,6 +163,7 @@ pub struct Platform { +@@ -141,6 +146,7 @@ pub struct Platform { + 'static, capsules::virtual_alarm::VirtualMuxAlarm<'static, nrf52840::rtc::Rtc<'static>>, >, - nonvolatile_storage: &'static capsules::nonvolatile_storage_driver::NonvolatileStorage<'static>, + nvmc: &'static nrf52840::nvmc::SyscallDriver, } impl kernel::Platform for Platform { -@@ -177,10 +183,30 @@ impl kernel::Platform for Platform { - capsules::temperature::DRIVER_NUM => f(Some(self.temp)), +@@ -156,10 +162,30 @@ impl kernel::Platform for Platform { + capsules::button::DRIVER_NUM => f(Some(self.button)), + capsules::rng::DRIVER_NUM => f(Some(self.rng)), capsules::analog_comparator::DRIVER_NUM => f(Some(self.analog_comparator)), - capsules::nonvolatile_storage_driver::DRIVER_NUM => f(Some(self.nonvolatile_storage)), + nrf52840::nvmc::DRIVER_NUM => f(Some(self.nvmc)), kernel::ipc::DRIVER_NUM => f(Some(&self.ipc)), _ => f(None), @@ -143,7 +143,7 @@ index 169f3d393..2ebb384d8 100644 } /// Entry point in the vector table called on hard reset. -@@ -204,7 +230,10 @@ pub unsafe fn reset_handler() { +@@ -183,7 +209,10 @@ pub unsafe fn reset_handler() { UartChannel::Pins(UartPins::new(UART_RTS, UART_TXD, UART_CTS, UART_RXD)) }; @@ -155,7 +155,7 @@ index 169f3d393..2ebb384d8 100644 let gpio = components::gpio::GpioComponent::new( board_kernel, -@@ -411,6 +440,14 @@ pub unsafe fn reset_handler() { +@@ -334,6 +363,14 @@ pub unsafe fn reset_handler() { nrf52840::acomp::Comparator )); @@ -170,10 +170,10 @@ index 169f3d393..2ebb384d8 100644 nrf52_components::NrfClockComponent::new().finalize(()); let platform = Platform { -@@ -426,6 +463,7 @@ pub unsafe fn reset_handler() { +@@ -345,6 +382,7 @@ pub unsafe fn reset_handler() { + rng, alarm, analog_comparator, - nonvolatile_storage, + nvmc, ipc: kernel::ipc::IPC::new(board_kernel, &memory_allocation_capability), }; diff --git a/patches/tock/02-usb.patch b/patches/tock/02-usb.patch index 6220f6a..958ae8c 100644 --- a/patches/tock/02-usb.patch +++ b/patches/tock/02-usb.patch @@ -101,11 +101,11 @@ index 000000000..69e95c3c7 + usb_driver + } +} -diff --git a/boards/nordic/nrf52840_dongle/src/main.rs b/boards/nordic/nrf52840_dongle/src/main.rs -index d72d20482..118ea6d68 100644 ---- a/boards/nordic/nrf52840_dongle/src/main.rs -+++ b/boards/nordic/nrf52840_dongle/src/main.rs -@@ -45,6 +45,17 @@ const PAN_ID: u16 = 0xABCD; +diff --git a/boards/nordic/nrf52840_dongle_opensk/src/main.rs b/boards/nordic/nrf52840_dongle_opensk/src/main.rs +index b485a0997..f5d29d025 100644 +--- a/boards/nordic/nrf52840_dongle_opensk/src/main.rs ++++ b/boards/nordic/nrf52840_dongle_opensk/src/main.rs +@@ -40,6 +40,17 @@ const _SPI_CLK: Pin = Pin::P1_04; /// UART Writer pub mod io; @@ -123,7 +123,7 @@ index d72d20482..118ea6d68 100644 // State for loading and holding applications. // How should the kernel respond when a process faults. const FAULT_RESPONSE: kernel::procs::FaultResponse = kernel::procs::FaultResponse::Panic; -@@ -96,6 +107,11 @@ pub struct Platform { +@@ -84,6 +95,11 @@ pub struct Platform { capsules::virtual_alarm::VirtualMuxAlarm<'static, nrf52840::rtc::Rtc<'static>>, >, nvmc: &'static nrf52840::nvmc::SyscallDriver, @@ -135,15 +135,15 @@ index d72d20482..118ea6d68 100644 } impl kernel::Platform for Platform { -@@ -115,6 +131,7 @@ impl kernel::Platform for Platform { - capsules::temperature::DRIVER_NUM => f(Some(self.temp)), +@@ -100,6 +116,7 @@ impl kernel::Platform for Platform { + capsules::rng::DRIVER_NUM => f(Some(self.rng)), capsules::analog_comparator::DRIVER_NUM => f(Some(self.analog_comparator)), nrf52840::nvmc::DRIVER_NUM => f(Some(self.nvmc)), + capsules::usb::usb_ctap::DRIVER_NUM => f(Some(self.usb)), kernel::ipc::DRIVER_NUM => f(Some(&self.ipc)), _ => f(None), } -@@ -323,6 +340,21 @@ pub unsafe fn reset_handler() { +@@ -286,6 +303,21 @@ pub unsafe fn reset_handler() { ) ); @@ -165,7 +165,7 @@ index d72d20482..118ea6d68 100644 nrf52_components::NrfClockComponent::new().finalize(()); let platform = Platform { -@@ -338,6 +370,7 @@ pub unsafe fn reset_handler() { +@@ -298,6 +330,7 @@ pub unsafe fn reset_handler() { alarm, analog_comparator, nvmc, @@ -173,13 +173,13 @@ index d72d20482..118ea6d68 100644 ipc: kernel::ipc::IPC::new(board_kernel, &memory_allocation_capability), }; -diff --git a/boards/nordic/nrf52840dk/src/main.rs b/boards/nordic/nrf52840dk/src/main.rs -index 2ebb384d8..4a7bfffdd 100644 ---- a/boards/nordic/nrf52840dk/src/main.rs -+++ b/boards/nordic/nrf52840dk/src/main.rs -@@ -113,6 +113,17 @@ pub mod io; +diff --git a/boards/nordic/nrf52840dk_opensk/src/main.rs b/boards/nordic/nrf52840dk_opensk/src/main.rs +index ebee8704b..6a2f8715b 100644 +--- a/boards/nordic/nrf52840dk_opensk/src/main.rs ++++ b/boards/nordic/nrf52840dk_opensk/src/main.rs +@@ -104,6 +104,17 @@ pub mod io; // - Set to true to use Segger RTT over USB. - const USB_DEBUGGING: bool = false; + const USB_DEBUGGING: bool = true; +const VENDOR_ID: u16 = 0x1915; // Nordic Semiconductor +const PRODUCT_ID: u16 = 0x521f; // nRF52840 Dongle (PCA10059) @@ -195,9 +195,9 @@ index 2ebb384d8..4a7bfffdd 100644 // State for loading and holding applications. // How should the kernel respond when a process faults. const FAULT_RESPONSE: kernel::procs::FaultResponse = kernel::procs::FaultResponse::Panic; -@@ -164,6 +175,11 @@ pub struct Platform { +@@ -147,6 +158,11 @@ pub struct Platform { + capsules::virtual_alarm::VirtualMuxAlarm<'static, nrf52840::rtc::Rtc<'static>>, >, - nonvolatile_storage: &'static capsules::nonvolatile_storage_driver::NonvolatileStorage<'static>, nvmc: &'static nrf52840::nvmc::SyscallDriver, + usb: &'static capsules::usb::usb_ctap::CtapUsbSyscallDriver< + 'static, @@ -207,15 +207,15 @@ index 2ebb384d8..4a7bfffdd 100644 } impl kernel::Platform for Platform { -@@ -184,6 +200,7 @@ impl kernel::Platform for Platform { +@@ -163,6 +179,7 @@ impl kernel::Platform for Platform { + capsules::rng::DRIVER_NUM => f(Some(self.rng)), capsules::analog_comparator::DRIVER_NUM => f(Some(self.analog_comparator)), - capsules::nonvolatile_storage_driver::DRIVER_NUM => f(Some(self.nonvolatile_storage)), nrf52840::nvmc::DRIVER_NUM => f(Some(self.nvmc)), + capsules::usb::usb_ctap::DRIVER_NUM => f(Some(self.usb)), kernel::ipc::DRIVER_NUM => f(Some(&self.ipc)), _ => f(None), } -@@ -448,6 +465,21 @@ pub unsafe fn reset_handler() { +@@ -371,6 +388,21 @@ pub unsafe fn reset_handler() { ) ); @@ -237,9 +237,9 @@ index 2ebb384d8..4a7bfffdd 100644 nrf52_components::NrfClockComponent::new().finalize(()); let platform = Platform { -@@ -464,6 +496,7 @@ pub unsafe fn reset_handler() { +@@ -383,6 +415,7 @@ pub unsafe fn reset_handler() { + alarm, analog_comparator, - nonvolatile_storage, nvmc, + usb, ipc: kernel::ipc::IPC::new(board_kernel, &memory_allocation_capability), diff --git a/patches/tock/04-additional-boards.patch b/patches/tock/03-additional-boards.patch similarity index 66% rename from patches/tock/04-additional-boards.patch rename to patches/tock/03-additional-boards.patch index b4f0ba9..ff6055e 100644 --- a/patches/tock/04-additional-boards.patch +++ b/patches/tock/03-additional-boards.patch @@ -1,11 +1,14 @@ diff --git a/Cargo.toml b/Cargo.toml -index 83c4855..cfd63b1 100644 +index 83c48558e..75b1abacc 100644 --- a/Cargo.toml +++ b/Cargo.toml -@@ -13,6 +13,8 @@ members = [ +@@ -12,7 +12,11 @@ members = [ + "boards/imix", "boards/msp_exp432p401r", "boards/nordic/nrf52840dk", ++ "boards/nordic/nrf52840dk_opensk", "boards/nordic/nrf52840_dongle", ++ "boards/nordic/nrf52840_dongle_opensk", + "boards/nordic/nrf52840_dongle_dfu", + "boards/nordic/nrf52840_mdk_dfu", "boards/nordic/nrf52dk", diff --git a/patches/tock/03-usb-debugging.patch b/patches/tock/03-usb-debugging.patch deleted file mode 100644 index 2bebb33..0000000 --- a/patches/tock/03-usb-debugging.patch +++ /dev/null @@ -1,13 +0,0 @@ -diff --git a/boards/nordic/nrf52840dk/src/main.rs b/boards/nordic/nrf52840dk/src/main.rs -index 303a451..18fd331 100644 ---- a/boards/nordic/nrf52840dk/src/main.rs -+++ b/boards/nordic/nrf52840dk/src/main.rs -@@ -112,7 +112,7 @@ pub mod io; - // Whether to use UART debugging or Segger RTT (USB) debugging. - // - Set to false to use UART. - // - Set to true to use Segger RTT over USB. --const USB_DEBUGGING: bool = false; -+const USB_DEBUGGING: bool = true; - - const VENDOR_ID: u16 = 0x1915; // Nordic Semiconductor - const PRODUCT_ID: u16 = 0x521f; // nRF52840 Dongle (PCA10059) diff --git a/patches/tock/05-mpu-fix.patch b/patches/tock/04-mpu-fix.patch similarity index 100% rename from patches/tock/05-mpu-fix.patch rename to patches/tock/04-mpu-fix.patch diff --git a/patches/tock/06-update-uicr.patch b/patches/tock/05-update-uicr.patch similarity index 99% rename from patches/tock/06-update-uicr.patch rename to patches/tock/05-update-uicr.patch index 53ee945..0b518a9 100644 --- a/patches/tock/06-update-uicr.patch +++ b/patches/tock/05-update-uicr.patch @@ -1,5 +1,5 @@ diff --git a/chips/nrf52/src/uicr.rs b/chips/nrf52/src/uicr.rs -index 6bb6c86..3bb8b5a 100644 +index 6bb6c86b7..3bb8b5a7d 100644 --- a/chips/nrf52/src/uicr.rs +++ b/chips/nrf52/src/uicr.rs @@ -1,38 +1,45 @@ @@ -97,4 +97,3 @@ index 6bb6c86..3bb8b5a 100644 /// Setting of pins dedicated to NFC functionality: NFC antenna or GPIO NfcPins [ /// Setting pins dedicated to NFC functionality - diff --git a/patches/tock/07-firmware-protect.patch b/patches/tock/06-firmware-protect.patch similarity index 91% rename from patches/tock/07-firmware-protect.patch rename to patches/tock/06-firmware-protect.patch index 365b20a..84562e4 100644 --- a/patches/tock/07-firmware-protect.patch +++ b/patches/tock/06-firmware-protect.patch @@ -1,6 +1,6 @@ diff --git a/boards/components/src/firmware_protection.rs b/boards/components/src/firmware_protection.rs new file mode 100644 -index 0000000..58695af +index 000000000..58695af81 --- /dev/null +++ b/boards/components/src/firmware_protection.rs @@ -0,0 +1,70 @@ @@ -75,7 +75,7 @@ index 0000000..58695af + } +} diff --git a/boards/components/src/lib.rs b/boards/components/src/lib.rs -index 917497a..520408f 100644 +index 917497af4..520408fcb 100644 --- a/boards/components/src/lib.rs +++ b/boards/components/src/lib.rs @@ -9,6 +9,7 @@ pub mod console; @@ -86,11 +86,11 @@ index 917497a..520408f 100644 pub mod ft6x06; pub mod gpio; pub mod hd44780; -diff --git a/boards/nordic/nrf52840_dongle/src/main.rs b/boards/nordic/nrf52840_dongle/src/main.rs -index 118ea6d..76436f3 100644 ---- a/boards/nordic/nrf52840_dongle/src/main.rs -+++ b/boards/nordic/nrf52840_dongle/src/main.rs -@@ -112,6 +112,7 @@ pub struct Platform { +diff --git a/boards/nordic/nrf52840_dongle_opensk/src/main.rs b/boards/nordic/nrf52840_dongle_opensk/src/main.rs +index f5d29d025..051943867 100644 +--- a/boards/nordic/nrf52840_dongle_opensk/src/main.rs ++++ b/boards/nordic/nrf52840_dongle_opensk/src/main.rs +@@ -100,6 +100,7 @@ pub struct Platform { 'static, nrf52840::usbd::Usbd<'static>, >, @@ -98,7 +98,7 @@ index 118ea6d..76436f3 100644 } impl kernel::Platform for Platform { -@@ -132,6 +133,7 @@ impl kernel::Platform for Platform { +@@ -117,6 +118,7 @@ impl kernel::Platform for Platform { capsules::analog_comparator::DRIVER_NUM => f(Some(self.analog_comparator)), nrf52840::nvmc::DRIVER_NUM => f(Some(self.nvmc)), capsules::usb::usb_ctap::DRIVER_NUM => f(Some(self.usb)), @@ -106,7 +106,7 @@ index 118ea6d..76436f3 100644 kernel::ipc::DRIVER_NUM => f(Some(&self.ipc)), _ => f(None), } -@@ -355,6 +357,14 @@ pub unsafe fn reset_handler() { +@@ -318,6 +320,14 @@ pub unsafe fn reset_handler() { ) .finalize(components::usb_ctap_component_buf!(nrf52840::usbd::Usbd)); @@ -121,7 +121,7 @@ index 118ea6d..76436f3 100644 nrf52_components::NrfClockComponent::new().finalize(()); let platform = Platform { -@@ -371,6 +381,7 @@ pub unsafe fn reset_handler() { +@@ -331,6 +341,7 @@ pub unsafe fn reset_handler() { analog_comparator, nvmc, usb, @@ -129,11 +129,11 @@ index 118ea6d..76436f3 100644 ipc: kernel::ipc::IPC::new(board_kernel, &memory_allocation_capability), }; -diff --git a/boards/nordic/nrf52840dk/src/main.rs b/boards/nordic/nrf52840dk/src/main.rs -index b1d0d3c..3cfb38d 100644 ---- a/boards/nordic/nrf52840dk/src/main.rs -+++ b/boards/nordic/nrf52840dk/src/main.rs -@@ -180,6 +180,7 @@ pub struct Platform { +diff --git a/boards/nordic/nrf52840dk_opensk/src/main.rs b/boards/nordic/nrf52840dk_opensk/src/main.rs +index 6a2f8715b..7898562dd 100644 +--- a/boards/nordic/nrf52840dk_opensk/src/main.rs ++++ b/boards/nordic/nrf52840dk_opensk/src/main.rs +@@ -163,6 +163,7 @@ pub struct Platform { 'static, nrf52840::usbd::Usbd<'static>, >, @@ -141,15 +141,15 @@ index b1d0d3c..3cfb38d 100644 } impl kernel::Platform for Platform { -@@ -201,6 +202,7 @@ impl kernel::Platform for Platform { - capsules::nonvolatile_storage_driver::DRIVER_NUM => f(Some(self.nonvolatile_storage)), +@@ -180,6 +181,7 @@ impl kernel::Platform for Platform { + capsules::analog_comparator::DRIVER_NUM => f(Some(self.analog_comparator)), nrf52840::nvmc::DRIVER_NUM => f(Some(self.nvmc)), capsules::usb::usb_ctap::DRIVER_NUM => f(Some(self.usb)), + capsules::firmware_protection::DRIVER_NUM => f(Some(self.crp)), kernel::ipc::DRIVER_NUM => f(Some(&self.ipc)), _ => f(None), } -@@ -480,6 +482,14 @@ pub unsafe fn reset_handler() { +@@ -403,6 +405,14 @@ pub unsafe fn reset_handler() { ) .finalize(components::usb_ctap_component_buf!(nrf52840::usbd::Usbd)); @@ -164,8 +164,8 @@ index b1d0d3c..3cfb38d 100644 nrf52_components::NrfClockComponent::new().finalize(()); let platform = Platform { -@@ -497,6 +507,7 @@ pub unsafe fn reset_handler() { - nonvolatile_storage, +@@ -416,6 +426,7 @@ pub unsafe fn reset_handler() { + analog_comparator, nvmc, usb, + crp, @@ -173,7 +173,7 @@ index b1d0d3c..3cfb38d 100644 }; diff --git a/capsules/src/driver.rs b/capsules/src/driver.rs -index ae458b3..f536dad 100644 +index ae458b309..f536dad32 100644 --- a/capsules/src/driver.rs +++ b/capsules/src/driver.rs @@ -16,6 +16,7 @@ pub enum NUM { @@ -186,7 +186,7 @@ index ae458b3..f536dad 100644 Ipc = 0x10000, diff --git a/capsules/src/firmware_protection.rs b/capsules/src/firmware_protection.rs new file mode 100644 -index 0000000..8cf63d6 +index 000000000..8cf63d6a4 --- /dev/null +++ b/capsules/src/firmware_protection.rs @@ -0,0 +1,85 @@ @@ -276,7 +276,7 @@ index 0000000..8cf63d6 + } +} diff --git a/capsules/src/lib.rs b/capsules/src/lib.rs -index e4423fe..7538aad 100644 +index e4423fe05..7538aad18 100644 --- a/capsules/src/lib.rs +++ b/capsules/src/lib.rs @@ -22,6 +22,7 @@ pub mod crc; @@ -288,7 +288,7 @@ index e4423fe..7538aad 100644 pub mod ft6x06; pub mod fxos8700cq; diff --git a/chips/nrf52/src/uicr.rs b/chips/nrf52/src/uicr.rs -index 3bb8b5a..ea96cb2 100644 +index 3bb8b5a7d..ea96cb2ae 100644 --- a/chips/nrf52/src/uicr.rs +++ b/chips/nrf52/src/uicr.rs @@ -1,13 +1,14 @@ @@ -359,7 +359,7 @@ index 3bb8b5a..ea96cb2 100644 +} diff --git a/kernel/src/hil/firmware_protection.rs b/kernel/src/hil/firmware_protection.rs new file mode 100644 -index 0000000..de08246 +index 000000000..de0824646 --- /dev/null +++ b/kernel/src/hil/firmware_protection.rs @@ -0,0 +1,48 @@ @@ -412,7 +412,7 @@ index 0000000..de08246 + fn set_protection(&self, level: ProtectionLevel) -> ReturnCode; +} diff --git a/kernel/src/hil/mod.rs b/kernel/src/hil/mod.rs -index 4f42afa..83e7702 100644 +index 4f42afa02..83e7702ab 100644 --- a/kernel/src/hil/mod.rs +++ b/kernel/src/hil/mod.rs @@ -8,6 +8,7 @@ pub mod dac; @@ -423,4 +423,3 @@ index 4f42afa..83e7702 100644 pub mod flash; pub mod gpio; pub mod gpio_async; - diff --git a/reproduce_hashes.sh b/reproduce_hashes.sh index a97f10d..6fd97ec 100755 --- a/reproduce_hashes.sh +++ b/reproduce_hashes.sh @@ -27,7 +27,7 @@ echo "Building sha256sum tool..." cargo build --manifest-path third_party/tock/tools/sha256sum/Cargo.toml echo "Computing SHA-256 sums of the boards..." -for board in nrf52840dk nrf52840_dongle nrf52840_dongle_dfu nrf52840_mdk_dfu +for board in nrf52840dk_opensk nrf52840_dongle_opensk nrf52840_dongle_dfu nrf52840_mdk_dfu do BOARD=$board ./reproduce_board.sh done diff --git a/run_desktop_tests.sh b/run_desktop_tests.sh index 1359523..28fa740 100755 --- a/run_desktop_tests.sh +++ b/run_desktop_tests.sh @@ -69,16 +69,16 @@ cargo build --release --target=thumbv7em-none-eabi --features with_ctap1 ./third_party/tock/tools/sha256sum/target/debug/sha256sum target/thumbv7em-none-eabi/release/ctap2 echo "Checking that supported boards build properly..." -make -C third_party/tock/boards/nordic/nrf52840dk -make -C third_party/tock/boards/nordic/nrf52840_dongle +make -C third_party/tock/boards/nordic/nrf52840dk_opensk +make -C third_party/tock/boards/nordic/nrf52840_dongle_opensk echo "Checking that other boards build properly..." make -C third_party/tock/boards/nordic/nrf52840_dongle_dfu make -C third_party/tock/boards/nordic/nrf52840_mdk_dfu echo "Checking deployment of supported boards..." -./deploy.py --board=nrf52840dk --no-app --programmer=none -./deploy.py --board=nrf52840_dongle --no-app --programmer=none +./deploy.py --board=nrf52840dk_opensk --no-app --programmer=none +./deploy.py --board=nrf52840_dongle_opensk --no-app --programmer=none echo "Checking deployment of other boards..." ./deploy.py --board=nrf52840_dongle_dfu --no-app --programmer=none