Skip to main content

nrf52/
crt1.rs

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 2022.
4
5use cortexm4f::{CortexM4F, CortexMVariant, initialize_ram_jump_to_main, scb, unhandled_interrupt};
6
7/*
8 * Adapted from crt1.c which was relicensed by the original author from
9 * GPLv3 to Apache 2.0.
10 * The original version of the file, under GPL can be found at
11 * https://github.com/SoftwareDefinedBuildings/stormport/blob/rebase0/tos/platforms/storm/stormcrt1.c
12 *
13 * Copyright 2016, Michael Andersen <m.andersen@eecs.berkeley.edu>
14 */
15
16extern "C" {
17    // _estack is not really a function, but it makes the types work
18    // You should never actually invoke it!!
19    fn _estack();
20}
21
22#[cfg_attr(
23    all(target_arch = "arm", target_os = "none"),
24    link_section = ".vectors"
25)]
26// used Ensures that the symbol is kept until the final binary
27#[cfg_attr(all(target_arch = "arm", target_os = "none"), used)]
28/// ARM Cortex M Vector Table
29pub static BASE_VECTORS: [unsafe extern "C" fn(); 16] = [
30    // Stack Pointer
31    _estack,
32    // Reset Handler
33    initialize_ram_jump_to_main,
34    // NMI
35    unhandled_interrupt,
36    // Hard Fault
37    CortexM4F::HARD_FAULT_HANDLER,
38    // Memory Management Fault
39    unhandled_interrupt,
40    // Bus Fault
41    unhandled_interrupt,
42    // Usage Fault
43    unhandled_interrupt,
44    // Reserved
45    unhandled_interrupt,
46    // Reserved
47    unhandled_interrupt,
48    // Reserved
49    unhandled_interrupt,
50    // Reserved
51    unhandled_interrupt,
52    // SVCall
53    CortexM4F::SVC_HANDLER,
54    // Reserved for Debug
55    unhandled_interrupt,
56    // Reserved
57    unhandled_interrupt,
58    // PendSv
59    unhandled_interrupt,
60    // SysTick
61    CortexM4F::SYSTICK_HANDLER,
62];
63
64#[cfg_attr(
65    all(target_arch = "arm", target_os = "none"),
66    link_section = ".vectors"
67)]
68// used Ensures that the symbol is kept until the final binary
69#[cfg_attr(all(target_arch = "arm", target_os = "none"), used)]
70pub static IRQS: [unsafe extern "C" fn(); 80] = [CortexM4F::GENERIC_ISR; 80];
71
72/// Apply fixes for various nRF52 errata
73///
74/// # Safety
75///
76/// Fixing these errata requires writing to various memory locations. These
77/// operations are safe as long as this is only run on an nRF52 MCU.
78pub(crate) unsafe fn fix_errata() {
79    // Apply early initialization workarounds for anomalies documented on
80    // 2015-12-11 nRF52832 Errata v1.2
81    // http://infocenter.nordicsemi.com/pdf/nRF52832_Errata_v1.2.pdf
82
83    // Workaround for Errata 12
84    // "COMP: Reference ladder not correctly callibrated" found at the Errate doc
85    core::ptr::write_volatile(
86        0x40013540i32 as *mut u32,
87        (core::ptr::read_volatile(0x10000324i32 as *mut u32) & 0x1f00u32) >> 8i32,
88    );
89
90    // Workaround for Errata 16
91    // "System: RAM may be corrupt on wakeup from CPU IDLE" found at the Errata doc
92    core::ptr::write_volatile(0x4007c074i32 as *mut u32, 3131961357u32);
93
94    // Workaround for Errata 31
95    // "CLOCK: Calibration values are not correctly loaded from FICR at reset"
96    // found at the Errata doc
97    core::ptr::write_volatile(
98        0x4000053ci32 as *mut u32,
99        (core::ptr::read_volatile(0x10000244i32 as *mut u32) & 0xe000u32) >> 13i32,
100    );
101
102    // Only needed for preview hardware
103    // // Workaround for Errata 32
104    // // "DIF: Debug session automatically enables TracePort pins" found at the Errata doc
105    // //    CoreDebug->DEMCR &= ~CoreDebug_DEMCR_TRCENA_Msk;
106    // *(0xe000edfcu32 as (*mut u32)) &= !0x01000000,
107
108    // Workaround for Errata 36
109    // "CLOCK: Some registers are not reset when expected" found at the Errata doc
110    //    NRF_CLOCK->EVENTS_DONE = 0;
111    //    NRF_CLOCK->EVENTS_CTTO = 0;
112    //    NRF_CLOCK->CTIV = 0;
113    // }
114
115    // Workaround for Errata 37
116    // "RADIO: Encryption engine is slow by default" found at the Errata document doc
117    core::ptr::write_volatile(0x400005a0i32 as *mut u32, 0x3u32);
118
119    // Workaround for Errata 57
120    // "NFCT: NFC Modulation amplitude" found at the Errata doc
121    core::ptr::write_volatile(0x40005610i32 as *mut u32, 0x5u32);
122    core::ptr::write_volatile(0x40005688i32 as *mut u32, 0x1u32);
123    core::ptr::write_volatile(0x40005618i32 as *mut u32, 0x0u32);
124    core::ptr::write_volatile(0x40005614i32 as *mut u32, 0x3fu32);
125
126    // Workaround for Errata 66
127    // "TEMP: Linearity specification not met with default settings" found at the Errata doc
128    //     NRF_TEMP->A0 = NRF_FICR->TEMP.A0;
129    //     NRF_TEMP->A1 = NRF_FICR->TEMP.A1;
130    //     NRF_TEMP->A2 = NRF_FICR->TEMP.A2;
131    //     NRF_TEMP->A3 = NRF_FICR->TEMP.A3;
132    //     NRF_TEMP->A4 = NRF_FICR->TEMP.A4;
133    //     NRF_TEMP->A5 = NRF_FICR->TEMP.A5;
134    //     NRF_TEMP->B0 = NRF_FICR->TEMP.B0;
135    //     NRF_TEMP->B1 = NRF_FICR->TEMP.B1;
136    //     NRF_TEMP->B2 = NRF_FICR->TEMP.B2;
137    //     NRF_TEMP->B3 = NRF_FICR->TEMP.B3;
138    //     NRF_TEMP->B4 = NRF_FICR->TEMP.B4;
139    //     NRF_TEMP->B5 = NRF_FICR->TEMP.B5;
140    //     NRF_TEMP->T0 = NRF_FICR->TEMP.T0;
141    //     NRF_TEMP->T1 = NRF_FICR->TEMP.T1;
142    //     NRF_TEMP->T2 = NRF_FICR->TEMP.T2;
143    //     NRF_TEMP->T3 = NRF_FICR->TEMP.T3;
144    //     NRF_TEMP->T4 = NRF_FICR->TEMP.T4;
145    // }
146
147    // Workaround for Errata 108
148    // "RAM: RAM content cannot be trusted upon waking up from System ON Idle
149    // or System OFF mode" found at the Errata doc
150    core::ptr::write_volatile(
151        0x40000ee4i32 as *mut u32,
152        core::ptr::read_volatile(0x10000258i32 as *mut u32) & 0x4fu32,
153    );
154}
155
156/// Explicitly tell the core where Tock's vector table is located.
157///
158/// If Tock is the
159/// only thing on the chip then this is effectively a no-op. If, however, there is
160/// a bootloader present then we want to ensure that the vector table is set
161/// correctly for Tock. The bootloader _may_ set this for us, but it may not
162/// so that any errors early in the Tock boot process trap back to the bootloader.
163/// To be safe we unconditionally set the vector table.
164pub(crate) fn initialize_vector_table() {
165    // # Safety
166    //
167    // The vector table must setup function pointers for the thumb core
168    // correctly. Because `BASE_VECTORS` is the correct data type this is
169    // safe.
170    unsafe {
171        scb::set_vector_table_offset(BASE_VECTORS.as_ptr().cast::<()>());
172    }
173}