nrf52_components/
startup.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.

//! Component for starting up nrf52 platforms.
//! Contains 3 components, NrfStartupComponent, NrfClockComponent,
//! and UartChannelComponent, as well as two helper structs for
//! intializing Uart on Nordic boards.

use capsules_core::virtualizers::virtual_alarm::{MuxAlarm, VirtualMuxAlarm};
use core::mem::MaybeUninit;
use kernel::component::Component;
use nrf52::gpio::Pin;
use nrf52::uicr::Regulator0Output;
use segger::rtt::SeggerRtt;

pub struct NrfStartupComponent<'a> {
    nfc_as_gpios: bool,
    button_rst_pin: Pin,
    reg_vout: Regulator0Output,
    nvmc: &'a nrf52::nvmc::Nvmc,
}

impl<'a> NrfStartupComponent<'a> {
    pub fn new(
        nfc_as_gpios: bool,
        button_rst_pin: Pin,
        reg_vout: Regulator0Output,
        nvmc: &'a nrf52::nvmc::Nvmc,
    ) -> Self {
        Self {
            nfc_as_gpios,
            button_rst_pin,
            reg_vout,
            nvmc,
        }
    }
}

impl Component for NrfStartupComponent<'_> {
    type StaticInput = ();
    type Output = ();
    fn finalize(self, _s: Self::StaticInput) -> Self::Output {
        // Disable APPROTECT in software. This is required as of newer nRF52
        // hardware revisions. See
        // https://devzone.nordicsemi.com/nordic/nordic-blog/b/blog/posts/working-with-the-nrf52-series-improved-approtect.
        // If run on older HW revisions this function will do nothing.
        let approtect = nrf52::approtect::Approtect::new();
        approtect.sw_disable_approtect();

        // Make non-volatile memory writable and activate the reset button
        let uicr = nrf52::uicr::Uicr::new();

        // Check if we need to erase UICR memory to re-program it
        // This only needs to be done when a bit needs to be flipped from 0 to 1.
        let psel0_reset: u32 = uicr.get_psel0_reset_pin().map_or(0, |pin| pin as u32);
        let psel1_reset: u32 = uicr.get_psel1_reset_pin().map_or(0, |pin| pin as u32);
        let mut erase_uicr = ((!psel0_reset & (self.button_rst_pin as u32))
            | (!psel1_reset & (self.button_rst_pin as u32))
            | (!(uicr.get_vout() as u32) & (self.reg_vout as u32)))
            != 0;

        // Only enabling the NFC pin protection requires an erase.
        if self.nfc_as_gpios {
            erase_uicr |= !uicr.is_nfc_pins_protection_enabled();
        }

        // On new nRF52 variants we need to ensure that the APPROTECT field in UICR is
        // set to `HwDisable`.
        if uicr.is_ap_protect_enabled() {
            erase_uicr = true;
        }

        if erase_uicr {
            self.nvmc.erase_uicr();
        }

        self.nvmc.configure_writeable();
        while !self.nvmc.is_ready() {}

        let mut needs_soft_reset: bool = false;

        // Configure reset pins
        if uicr
            .get_psel0_reset_pin()
            .is_none_or(|pin| pin != self.button_rst_pin)
        {
            uicr.set_psel0_reset_pin(self.button_rst_pin);
            while !self.nvmc.is_ready() {}
            needs_soft_reset = true;
        }
        if uicr
            .get_psel1_reset_pin()
            .is_none_or(|pin| pin != self.button_rst_pin)
        {
            uicr.set_psel1_reset_pin(self.button_rst_pin);
            while !self.nvmc.is_ready() {}
            needs_soft_reset = true;
        }

        // Configure voltage regulator output
        if uicr.get_vout() != self.reg_vout {
            uicr.set_vout(self.reg_vout);
            while !self.nvmc.is_ready() {}
            needs_soft_reset = true;
        }

        // Check if we need to free the NFC pins for GPIO
        if self.nfc_as_gpios {
            uicr.set_nfc_pins_protection(true);
            while !self.nvmc.is_ready() {}
            needs_soft_reset = true;
        }

        // If APPROTECT was not already disabled, ensure it is set to disabled.
        if uicr.is_ap_protect_enabled() {
            uicr.disable_ap_protect();
            while !self.nvmc.is_ready() {}
            needs_soft_reset = true;
        }

        // Any modification of UICR needs a soft reset for the changes to be taken into account.
        if needs_soft_reset {
            unsafe {
                cortexm4::scb::reset();
            }
        }
    }
}

pub struct NrfClockComponent<'a> {
    clock: &'a nrf52::clock::Clock,
}

impl<'a> NrfClockComponent<'a> {
    pub fn new(clock: &'a nrf52::clock::Clock) -> Self {
        Self { clock }
    }
}

impl Component for NrfClockComponent<'_> {
    type StaticInput = ();
    type Output = ();
    fn finalize(self, _s: Self::StaticInput) -> Self::Output {
        // Start all of the clocks. Low power operation will require a better
        // approach than this.
        self.clock.low_stop();
        self.clock.high_stop();

        self.clock
            .low_set_source(nrf52::clock::LowClockSource::XTAL);
        self.clock.low_start();
        self.clock.high_start();
        while !self.clock.low_started() {}
        while !self.clock.high_started() {}
    }
}

#[macro_export]
macro_rules! uart_channel_component_static {
    ($A:ty $(,)?) => {{
        components::segger_rtt_component_static!($A)
    };};
}

/// Pins for the UART
#[derive(Debug)]
pub struct UartPins {
    rts: Option<Pin>,
    txd: Pin,
    cts: Option<Pin>,
    rxd: Pin,
}

impl UartPins {
    pub fn new(rts: Option<Pin>, txd: Pin, cts: Option<Pin>, rxd: Pin) -> Self {
        Self { rts, txd, cts, rxd }
    }
}

/// Uart chanel representation depends on whether USB debugging is
/// enabled.
pub enum UartChannel<'a> {
    Pins(UartPins),
    Rtt(components::segger_rtt::SeggerRttMemoryRefs<'a>),
}

pub struct UartChannelComponent {
    uart_channel: UartChannel<'static>,
    mux_alarm: &'static MuxAlarm<'static, nrf52::rtc::Rtc<'static>>,
    uarte0: &'static nrf52::uart::Uarte<'static>,
}

impl UartChannelComponent {
    pub fn new(
        uart_channel: UartChannel<'static>,
        mux_alarm: &'static MuxAlarm<'static, nrf52::rtc::Rtc<'static>>,
        uarte0: &'static nrf52::uart::Uarte<'static>,
    ) -> Self {
        Self {
            uart_channel,
            mux_alarm,
            uarte0,
        }
    }
}

impl Component for UartChannelComponent {
    type StaticInput = (
        &'static mut MaybeUninit<VirtualMuxAlarm<'static, nrf52::rtc::Rtc<'static>>>,
        &'static mut MaybeUninit<
            SeggerRtt<'static, VirtualMuxAlarm<'static, nrf52::rtc::Rtc<'static>>>,
        >,
    );
    type Output = &'static dyn kernel::hil::uart::Uart<'static>;

    fn finalize(self, s: Self::StaticInput) -> Self::Output {
        match self.uart_channel {
            UartChannel::Pins(uart_pins) => {
                unsafe {
                    self.uarte0.initialize(
                        nrf52::pinmux::Pinmux::new(uart_pins.txd as u32),
                        nrf52::pinmux::Pinmux::new(uart_pins.rxd as u32),
                        uart_pins.cts.map(|x| nrf52::pinmux::Pinmux::new(x as u32)),
                        uart_pins.rts.map(|x| nrf52::pinmux::Pinmux::new(x as u32)),
                    )
                };
                self.uarte0
            }
            UartChannel::Rtt(rtt_memory) => {
                let rtt =
                    components::segger_rtt::SeggerRttComponent::new(self.mux_alarm, rtt_memory)
                        .finalize(s);
                rtt
            }
        }
    }
}