Skip to main content

nrf52/
chip.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
5//! Top-level chip definition for the nRF52 microcontroller.
6
7use core::fmt::Write;
8use cortexm4f::{CortexM4F, CortexMVariant, nvic};
9use kernel::platform::chip::InterruptService;
10use kernel::utilities::StaticRef;
11
12//
13// Peripheral Registers Instantiations
14//
15
16const AESECB_BASE: StaticRef<crate::aes::AesEcbRegisters> =
17    unsafe { StaticRef::new(0x4000E000 as *const crate::aes::AesEcbRegisters) };
18
19const APPROTECT_BASE: StaticRef<crate::approtect::ApprotectRegisters> =
20    unsafe { StaticRef::new(0x40000000 as *const crate::approtect::ApprotectRegisters) };
21
22const CLOCK_BASE: StaticRef<crate::clock::ClockRegisters> =
23    unsafe { StaticRef::new(0x40000000 as *const crate::clock::ClockRegisters) };
24
25const COMP_BASE: StaticRef<crate::acomp::CompRegisters> =
26    unsafe { StaticRef::new(0x40013000 as *const crate::acomp::CompRegisters) };
27
28pub const FICR_BASE: StaticRef<crate::ficr::FicrRegisters> =
29    unsafe { StaticRef::new(0x10000000 as *const crate::ficr::FicrRegisters) };
30
31const NVMC_BASE: StaticRef<crate::nvmc::NvmcRegisters> =
32    unsafe { StaticRef::new(0x4001E400 as *const crate::nvmc::NvmcRegisters) };
33
34const POWER_BASE: StaticRef<crate::power::PowerRegisters> =
35    unsafe { StaticRef::new(0x40000000 as *const crate::power::PowerRegisters) };
36
37const PWM0_BASE: StaticRef<crate::pwm::PwmRegisters> =
38    unsafe { StaticRef::new(0x4001C000 as *const crate::pwm::PwmRegisters) };
39
40const RADIO_BASE: StaticRef<crate::ble_radio::RadioRegisters> =
41    unsafe { StaticRef::new(0x40001000 as *const crate::ble_radio::RadioRegisters) };
42
43const RTC1_BASE: StaticRef<crate::rtc::RtcRegisters> =
44    unsafe { StaticRef::new(0x40011000 as *const crate::rtc::RtcRegisters) };
45
46const SAADC_BASE: StaticRef<crate::adc::AdcRegisters> =
47    unsafe { StaticRef::new(0x40007000 as *const crate::adc::AdcRegisters) };
48
49const SPIM0_BASE: StaticRef<crate::spi::SpimRegisters> =
50    unsafe { StaticRef::new(0x40003000 as *const crate::spi::SpimRegisters) };
51
52const SPIM2_BASE: StaticRef<crate::spi::SpimRegisters> =
53    unsafe { StaticRef::new(0x40023000 as *const crate::spi::SpimRegisters) };
54
55const TEMP_BASE: StaticRef<crate::temperature::TempRegisters> =
56    unsafe { StaticRef::new(0x4000C000 as *const crate::temperature::TempRegisters) };
57
58const TIMER0_BASE: StaticRef<crate::timer::TimerRegisters> =
59    unsafe { StaticRef::new(0x40008000 as *const crate::timer::TimerRegisters) };
60
61const TIMER1_BASE: StaticRef<crate::timer::TimerRegisters> =
62    unsafe { StaticRef::new(0x40009000 as *const crate::timer::TimerRegisters) };
63
64const TIMER2_BASE: StaticRef<crate::timer::TimerRegisters> =
65    unsafe { StaticRef::new(0x4000A000 as *const crate::timer::TimerRegisters) };
66
67const TWI1_BASE: StaticRef<crate::i2c::TwiRegisters> =
68    unsafe { StaticRef::new(0x40004000 as *const crate::i2c::TwiRegisters) };
69
70const UICR_BASE: StaticRef<crate::uicr::UicrRegisters> =
71    unsafe { StaticRef::new(0x10001200 as *const crate::uicr::UicrRegisters) };
72
73const RNG_BASE: StaticRef<crate::trng::RngRegisters> =
74    unsafe { StaticRef::new(0x4000D000 as *const crate::trng::RngRegisters) };
75
76pub struct NRF52<'a, I: InterruptService + 'a> {
77    mpu: cortexm4f::mpu::MPU,
78    userspace_kernel_boundary: cortexm4f::syscall::SysCall,
79    interrupt_service: &'a I,
80}
81
82impl<'a, I: InterruptService + 'a> NRF52<'a, I> {
83    pub fn new(interrupt_service: &'a I) -> Self {
84        // SAFETY: All nRF52 chips are Cortex-M4F cores with valid MPU hardware.
85        let mpu = unsafe { cortexm4f::mpu::new() };
86
87        Self {
88            mpu,
89            userspace_kernel_boundary: cortexm4f::syscall::SysCall::new(),
90            interrupt_service,
91        }
92    }
93}
94
95/// This struct, when initialized, instantiates all peripheral drivers for the nrf52.
96///
97/// If a board wishes to use only a subset of these peripherals, this
98/// should not be used or imported, and a modified version should be
99/// constructed manually in main.rs.
100pub struct Nrf52DefaultPeripherals<'a> {
101    pub acomp: crate::acomp::Comparator<'a>,
102    pub ecb: crate::aes::AesECB<'a>,
103    pub pwr_clk: crate::power::Power<'a>,
104    pub ble_radio: crate::ble_radio::Radio<'a>,
105    pub trng: crate::trng::Trng<'a>,
106    pub rtc: crate::rtc::Rtc<'a>,
107    pub temp: crate::temperature::Temp<'a>,
108    pub timer0: crate::timer::TimerAlarm<'a>,
109    pub timer1: crate::timer::TimerAlarm<'a>,
110    pub timer2: crate::timer::Timer,
111    pub uarte0: crate::uarte::Uarte<'a>,
112    pub spim0: crate::spi::SPIM<'a>,
113    pub twi1: crate::i2c::TWI<'a>,
114    pub spim2: crate::spi::SPIM<'a>,
115    pub adc: crate::adc::Adc<'a>,
116    pub nvmc: crate::nvmc::Nvmc,
117    pub clock: crate::clock::Clock,
118    pub pwm0: crate::pwm::Pwm,
119    pub uicr: crate::uicr::Uicr<'a>,
120    pub approtect: crate::approtect::Approtect<'a>,
121    pub ficr: &'a crate::ficr::Ficr,
122}
123
124impl<'a> Nrf52DefaultPeripherals<'a> {
125    /// Create default peripherals for an nRF52-based microcontroller.
126    ///
127    /// # Safety
128    ///
129    /// Some of these peripherals use DMA. As such, the default peripherals must
130    /// be unique. This requires:
131    ///
132    /// - This constructor must be called at most once.
133    /// - There must not be additional instances of the DMA-enabled peripheral
134    ///   drivers.
135    /// - There must not be any other code that accesses the DMA buffer and
136    ///   length registers of the DMA-enabled peripherals.
137    pub unsafe fn new(ficr: &'a crate::ficr::Ficr, aes_ecb_buffer: &'static mut [u8; 48]) -> Self {
138        // SAFETY: See function-level doc.
139        let aes_registers = unsafe { crate::aes::AesEcbRegistersManager::new(AESECB_BASE) };
140
141        // SAFETY: See function-level doc.
142        let uarte0_registers = unsafe { crate::uarte::UarteRegistersManager::new_uarte0() };
143
144        Self {
145            acomp: crate::acomp::Comparator::new(COMP_BASE),
146            ecb: crate::aes::AesECB::new(aes_registers, aes_ecb_buffer),
147            pwr_clk: crate::power::Power::new(POWER_BASE),
148            ble_radio: crate::ble_radio::Radio::new(RADIO_BASE),
149            trng: crate::trng::Trng::new(RNG_BASE),
150            rtc: crate::rtc::Rtc::new(RTC1_BASE),
151            temp: crate::temperature::Temp::new(TEMP_BASE),
152            timer0: crate::timer::TimerAlarm::new(TIMER0_BASE),
153            timer1: crate::timer::TimerAlarm::new(TIMER1_BASE),
154            timer2: crate::timer::Timer::new(TIMER2_BASE),
155            uarte0: crate::uarte::Uarte::new(uarte0_registers),
156            spim0: crate::spi::SPIM::new(SPIM0_BASE),
157            spim2: crate::spi::SPIM::new(SPIM2_BASE),
158            twi1: crate::i2c::TWI::new(TWI1_BASE),
159            // Default to 3.3 V VDD reference.
160            adc: crate::adc::Adc::new(SAADC_BASE, 3300),
161            nvmc: crate::nvmc::Nvmc::new(NVMC_BASE),
162            clock: crate::clock::Clock::new(CLOCK_BASE),
163            pwm0: crate::pwm::Pwm::new(PWM0_BASE),
164            uicr: crate::uicr::Uicr::new(UICR_BASE, ficr),
165            approtect: crate::approtect::Approtect::new(APPROTECT_BASE, ficr),
166            ficr,
167        }
168    }
169    // Necessary for setting up circular dependencies
170    pub fn init(&'static self) {
171        kernel::deferred_call::DeferredCallClient::register(&self.nvmc);
172    }
173}
174impl kernel::platform::chip::InterruptService for Nrf52DefaultPeripherals<'_> {
175    fn service_interrupt(&self, interrupt: u32) -> bool {
176        match interrupt {
177            crate::peripheral_interrupts::COMP => self.acomp.handle_interrupt(),
178            crate::peripheral_interrupts::ECB => self.ecb.handle_interrupt(),
179            crate::peripheral_interrupts::POWER_CLOCK => self.pwr_clk.handle_interrupt(),
180            crate::peripheral_interrupts::RADIO => match self.ble_radio.is_enabled() {
181                false => (),
182                true => self.ble_radio.handle_interrupt(),
183            },
184            crate::peripheral_interrupts::RNG => self.trng.handle_interrupt(),
185            crate::peripheral_interrupts::RTC1 => self.rtc.handle_interrupt(),
186            crate::peripheral_interrupts::TEMP => self.temp.handle_interrupt(),
187            crate::peripheral_interrupts::TIMER0 => self.timer0.handle_interrupt(),
188            crate::peripheral_interrupts::TIMER1 => self.timer1.handle_interrupt(),
189            crate::peripheral_interrupts::TIMER2 => self.timer2.handle_interrupt(),
190            crate::peripheral_interrupts::UART0 => self.uarte0.handle_interrupt(),
191            crate::peripheral_interrupts::SPI0_TWI0 => self.spim0.handle_interrupt(),
192            crate::peripheral_interrupts::SPI1_TWI1 => self.twi1.handle_interrupt(),
193            crate::peripheral_interrupts::SPIM2_SPIS2_SPI2 => self.spim2.handle_interrupt(),
194            crate::peripheral_interrupts::ADC => self.adc.handle_interrupt(),
195            _ => return false,
196        }
197        true
198    }
199}
200
201impl<'a, I: InterruptService + 'a> kernel::platform::chip::Chip for NRF52<'a, I> {
202    type MPU = cortexm4f::mpu::MPU;
203    type UserspaceKernelBoundary = cortexm4f::syscall::SysCall;
204    type ThreadIdProvider = cortexm4f::thread_id::CortexMThreadIdProvider;
205
206    fn init() {
207        // # Safety
208        //
209        // We are setting up the nRF52 chip, so these memory locations are valid
210        // on the nRF52 and this is OK to call.
211        unsafe {
212            crate::crt1::fix_errata();
213        }
214        crate::crt1::initialize_vector_table();
215
216        // # Safety
217        //
218        // Need to enable interrupts.
219        nvic::enable_all();
220    }
221
222    fn mpu(&self) -> &Self::MPU {
223        &self.mpu
224    }
225
226    fn userspace_kernel_boundary(&self) -> &Self::UserspaceKernelBoundary {
227        &self.userspace_kernel_boundary
228    }
229
230    fn service_pending_interrupts(&self) {
231        while let Some(interrupt) = nvic::next_pending() {
232            if !self.interrupt_service.service_interrupt(interrupt) {
233                panic!("unhandled interrupt {}", interrupt);
234            }
235            let n = nvic::Nvic::new(interrupt);
236            n.clear_pending();
237            n.enable();
238        }
239    }
240
241    fn has_pending_interrupts(&self) -> bool {
242        nvic::has_pending()
243    }
244
245    fn sleep(&self) {
246        let cap = kernel::create_capability!(kernel::capabilities::CpuStateControlCapability);
247        cortexm4f::support::wfi(&cap);
248    }
249
250    fn with_interrupts_disabled<F, R>(&self, f: F) -> R
251    where
252        F: FnOnce() -> R,
253    {
254        cortexm4f::support::with_interrupts_disabled(f)
255    }
256
257    unsafe fn print_state(_this: Option<&Self>, write: &mut dyn Write) {
258        CortexM4F::print_cortexm_state(write);
259    }
260}