nrf52/uicr.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//! User information configuration registers
6//!
7//! Minimal implementation to support activation of the reset button on
8//! nRF52-DK.
9
10use crate::ficr;
11use enum_primitive::cast::FromPrimitive;
12use kernel::utilities::StaticRef;
13use tock_registers_v0::interfaces::{ReadWriteable, Readable, Writeable};
14use tock_registers_v0::registers::ReadWrite;
15use tock_registers_v0::{register_bitfields, register_structs};
16
17use crate::gpio::Pin;
18
19register_structs! {
20 pub UicrRegisters {
21 /// Mapping of the nRESET function (see POWER chapter for details)
22 /// - Address: 0x200 - 0x204
23 (0x00 => pselreset0: ReadWrite<u32, Pselreset::Register>),
24 /// Mapping of the nRESET function (see POWER chapter for details)
25 /// - Address: 0x204 - 0x208
26 (0x04 => pselreset1: ReadWrite<u32, Pselreset::Register>),
27 /// Access Port protection
28 /// - Address: 0x208 - 0x20c
29 (0x08 => approtect: ReadWrite<u32, ApProtect::Register>),
30 /// Setting of pins dedicated to NFC functionality: NFC antenna or GPIO
31 /// - Address: 0x20c - 0x210
32 (0x0C => nfcpins: ReadWrite<u32, NfcPins::Register>),
33 (0x10 => _reserved1),
34 /// External circuitry to be supplied from VDD pin.
35 /// - Address: 0x300 - 0x304
36 (0x100 => extsupply: ReadWrite<u32, ExtSupply::Register>),
37 /// GPIO reference voltage
38 /// - Address: 0x304 - 0x308
39 (0x104 => regout0: ReadWrite<u32, RegOut::Register>),
40 (0x108 => @END),
41 }
42}
43
44register_bitfields! [u32,
45 /// Task register
46 Pselreset [
47 /// GPIO number Px.nn onto which Reset is exposed
48 PIN OFFSET(0) NUMBITS(5) [],
49 /// GPIO port number Pn.xx onto with Reset is exposed
50 PORT OFFSET(5) NUMBITS(1) [],
51 /// Connection
52 CONNECTION OFFSET(31) NUMBITS(1) [
53 DISCONNECTED = 1,
54 CONNECTED = 0
55 ]
56 ],
57 /// Access port protection
58 ApProtect [
59 /// Ready event
60 PALL OFFSET(0) NUMBITS(8) [
61 /// Enable
62 ENABLED = 0x00,
63 /// Disable for later nRF52 variants
64 HWDISABLE = 0x5a,
65 /// Disable
66 DISABLED = 0xff
67 ]
68 ],
69 /// Setting of pins dedicated to NFC functionality: NFC antenna or GPIO
70 NfcPins [
71 /// Setting pins dedicated to NFC functionality
72 PROTECT OFFSET(0) NUMBITS(1) [
73 /// Operation as GPIO pins. Same protection as normal GPIO pins
74 DISABLED = 0,
75 /// Operation as NFC antenna pins. Configures the protection for
76 /// NFC operation
77 NFC = 1
78 ]
79 ],
80 /// Enable external circuitry to be supplied from VDD pin
81 ExtSupply [
82 /// Enable external circuitry to be supplied from VDD pin
83 EXTSUPPLY OFFSET(0) NUMBITS(1) [
84 /// No current can be drawn from the VDD pin
85 DISABLED = 0,
86 /// It is allowed to supply external circuitry from the VDD pin
87 ENABLED = 1
88 ]
89 ],
90 /// GPIO reference voltage / external output supply voltage
91 RegOut [
92 /// Output voltage from REG0 regulator stage
93 VOUT OFFSET(0) NUMBITS(3) [
94 V1_8 = 0,
95 V2_1 = 1,
96 V2_4 = 2,
97 V2_7 = 3,
98 V3_0 = 4,
99 V3_3 = 5,
100 DEFAULT = 7
101 ]
102 ]
103];
104
105pub struct Uicr<'a> {
106 registers: StaticRef<UicrRegisters>,
107 ficr: &'a ficr::Ficr,
108}
109
110#[derive(Copy, Clone, PartialEq)]
111/// Output voltage from REG0 regulator stage.
112///
113/// The value is board dependent (e.g. the nRF52840dk board uses 1.8V
114/// whereas the nRF52840-Dongle requires 3.0V to light its LEDs).
115/// When a chip is out of the factory or fully erased, the default value (7)
116/// will output 1.8V.
117pub enum Regulator0Output {
118 V1_8 = 0,
119 V2_1 = 1,
120 V2_4 = 2,
121 V2_7 = 3,
122 V3_0 = 4,
123 V3_3 = 5,
124 DEFAULT = 7,
125}
126
127impl From<u32> for Regulator0Output {
128 fn from(val: u32) -> Self {
129 match val & 7 {
130 0 => Regulator0Output::V1_8,
131 1 => Regulator0Output::V2_1,
132 2 => Regulator0Output::V2_4,
133 3 => Regulator0Output::V2_7,
134 4 => Regulator0Output::V3_0,
135 5 => Regulator0Output::V3_3,
136 7 => Regulator0Output::DEFAULT,
137 _ => Regulator0Output::DEFAULT, // Invalid value, fall back to DEFAULT
138 }
139 }
140}
141
142impl<'a> Uicr<'a> {
143 pub const fn new(registers: StaticRef<UicrRegisters>, ficr: &'a ficr::Ficr) -> Uicr<'a> {
144 Uicr { registers, ficr }
145 }
146
147 pub fn set_psel0_reset_pin(&self, pin: Pin) {
148 self.registers.pselreset0.set(pin as u32);
149 }
150
151 pub fn get_psel0_reset_pin(&self) -> Option<Pin> {
152 Pin::from_u32(self.registers.pselreset0.get())
153 }
154
155 pub fn set_psel1_reset_pin(&self, pin: Pin) {
156 self.registers.pselreset1.set(pin as u32);
157 }
158
159 pub fn get_psel1_reset_pin(&self) -> Option<Pin> {
160 Pin::from_u32(self.registers.pselreset1.get())
161 }
162
163 pub fn set_vout(&self, vout: Regulator0Output) {
164 self.registers.regout0.modify(RegOut::VOUT.val(vout as u32));
165 }
166
167 pub fn get_vout(&self) -> Regulator0Output {
168 Regulator0Output::from(self.registers.regout0.read(RegOut::VOUT))
169 }
170
171 pub fn set_nfc_pins_protection(&self, protected: bool) {
172 if protected {
173 self.registers.nfcpins.write(NfcPins::PROTECT::NFC);
174 } else {
175 self.registers.nfcpins.write(NfcPins::PROTECT::DISABLED);
176 }
177 }
178
179 pub fn is_nfc_pins_protection_enabled(&self) -> bool {
180 self.registers.nfcpins.matches_all(NfcPins::PROTECT::NFC)
181 }
182
183 pub fn is_ap_protect_enabled(&self) -> bool {
184 // We need to understand the variant of this nRF52 chip to correctly
185 // implement this function. Newer versions use a different value to
186 // indicate disabled.
187 let disabled_val = if self.ficr.has_updated_approtect_logic() {
188 ApProtect::PALL::HWDISABLE
189 } else {
190 ApProtect::PALL::DISABLED
191 };
192
193 // Here we compare to the correct DISABLED value because any other value
194 // should enable the protection.
195 !self.registers.approtect.matches_all(disabled_val)
196 }
197
198 pub fn set_ap_protect(&self) {
199 self.registers.approtect.write(ApProtect::PALL::ENABLED);
200 }
201
202 /// Disable the access port protection in the UICR register. This is stored
203 /// in flash and is persistent. This behavior can also be accomplished
204 /// outside of tock by running `nrfjprog --recover`.
205 pub fn disable_ap_protect(&self) {
206 // We need to understand the variant of this nRF52 chip to correctly
207 // implement this function.
208 if self.ficr.has_updated_approtect_logic() {
209 // Newer revisions of the chip require setting the APPROTECT
210 // register to `HwDisable`.
211 self.registers.approtect.write(ApProtect::PALL::HWDISABLE);
212 } else {
213 // All other revisions just use normal disable.
214 self.registers.approtect.write(ApProtect::PALL::DISABLED);
215 }
216 }
217}