diff options
| author | Dario Nieuwenhuis <[email protected]> | 2024-03-21 23:01:45 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-03-21 23:01:45 +0000 |
| commit | 7cf5cf45801e784a4f085c47efc30b1212961837 (patch) | |
| tree | 8a931f10eccb2136593bb151b025e519a34f1eca | |
| parent | ee1aa80e3063de35a6f68c918a18b506d7f49539 (diff) | |
| parent | 10597e3b4d18818d322be95e1a57656855f99852 (diff) | |
Merge pull request #2725 from DeepSOIC/regout0
nRF52840/config: add dcdc voltage parameter
| -rw-r--r-- | embassy-nrf/src/lib.rs | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/embassy-nrf/src/lib.rs b/embassy-nrf/src/lib.rs index 718f229a3..3457dd933 100644 --- a/embassy-nrf/src/lib.rs +++ b/embassy-nrf/src/lib.rs | |||
| @@ -225,10 +225,31 @@ pub mod config { | |||
| 225 | /// Config for the first stage DCDC (VDDH -> VDD), if disabled LDO will be used. | 225 | /// Config for the first stage DCDC (VDDH -> VDD), if disabled LDO will be used. |
| 226 | #[cfg(feature = "nrf52840")] | 226 | #[cfg(feature = "nrf52840")] |
| 227 | pub reg0: bool, | 227 | pub reg0: bool, |
| 228 | /// Configure the voltage of the first stage DCDC. It is stored in non-volatile memory (UICR.REGOUT0 register); pass None to not touch it. | ||
| 229 | #[cfg(feature = "nrf52840")] | ||
| 230 | pub reg0_voltage: Option<Reg0Voltage>, | ||
| 228 | /// Config for the second stage DCDC (VDD -> DEC4), if disabled LDO will be used. | 231 | /// Config for the second stage DCDC (VDD -> DEC4), if disabled LDO will be used. |
| 229 | pub reg1: bool, | 232 | pub reg1: bool, |
| 230 | } | 233 | } |
| 231 | 234 | ||
| 235 | /// Output voltage setting for REG0 regulator stage. | ||
| 236 | #[cfg(feature = "nrf52840")] | ||
| 237 | pub enum Reg0Voltage { | ||
| 238 | /// 1.8 V | ||
| 239 | _1V8 = 0, | ||
| 240 | /// 2.1 V | ||
| 241 | _2V1 = 1, | ||
| 242 | /// 2.4 V | ||
| 243 | _2V4 = 2, | ||
| 244 | /// 2.7 V | ||
| 245 | _2V7 = 3, | ||
| 246 | /// 3.0 V | ||
| 247 | _3V0 = 4, | ||
| 248 | /// 3.3 V | ||
| 249 | _3v3 = 5, | ||
| 250 | //ERASED = 7, means 1.8V | ||
| 251 | } | ||
| 252 | |||
| 232 | /// Settings for enabling the built in DCDC converters. | 253 | /// Settings for enabling the built in DCDC converters. |
| 233 | #[cfg(feature = "_nrf5340-app")] | 254 | #[cfg(feature = "_nrf5340-app")] |
| 234 | pub struct DcdcConfig { | 255 | pub struct DcdcConfig { |
| @@ -279,6 +300,8 @@ pub mod config { | |||
| 279 | dcdc: DcdcConfig { | 300 | dcdc: DcdcConfig { |
| 280 | #[cfg(feature = "nrf52840")] | 301 | #[cfg(feature = "nrf52840")] |
| 281 | reg0: false, | 302 | reg0: false, |
| 303 | #[cfg(feature = "nrf52840")] | ||
| 304 | reg0_voltage: None, | ||
| 282 | reg1: false, | 305 | reg1: false, |
| 283 | }, | 306 | }, |
| 284 | #[cfg(feature = "_nrf5340-app")] | 307 | #[cfg(feature = "_nrf5340-app")] |
| @@ -337,6 +360,7 @@ mod consts { | |||
| 337 | pub const UICR_PSELRESET2: *mut u32 = 0x10001204 as *mut u32; | 360 | pub const UICR_PSELRESET2: *mut u32 = 0x10001204 as *mut u32; |
| 338 | pub const UICR_NFCPINS: *mut u32 = 0x1000120C as *mut u32; | 361 | pub const UICR_NFCPINS: *mut u32 = 0x1000120C as *mut u32; |
| 339 | pub const UICR_APPROTECT: *mut u32 = 0x10001208 as *mut u32; | 362 | pub const UICR_APPROTECT: *mut u32 = 0x10001208 as *mut u32; |
| 363 | pub const UICR_REGOUT0: *mut u32 = 0x10001304 as *mut u32; | ||
| 340 | pub const APPROTECT_ENABLED: u32 = 0x0000_0000; | 364 | pub const APPROTECT_ENABLED: u32 = 0x0000_0000; |
| 341 | pub const APPROTECT_DISABLED: u32 = 0x0000_005a; | 365 | pub const APPROTECT_DISABLED: u32 = 0x0000_005a; |
| 342 | } | 366 | } |
| @@ -493,6 +517,21 @@ pub fn init(config: config::Config) -> Peripherals { | |||
| 493 | } | 517 | } |
| 494 | } | 518 | } |
| 495 | 519 | ||
| 520 | #[cfg(feature = "nrf52840")] | ||
| 521 | unsafe { | ||
| 522 | if let Some(value) = config.dcdc.reg0_voltage { | ||
| 523 | let value = value as u32; | ||
| 524 | let res = uicr_write_masked(consts::UICR_REGOUT0, value, 0b00000000_00000000_00000000_00000111); | ||
| 525 | needs_reset |= res == WriteResult::Written; | ||
| 526 | if res == WriteResult::Failed { | ||
| 527 | warn!( | ||
| 528 | "Failed to set regulator voltage, as UICR is already programmed to some other setting, and can't be changed without erasing it.\n\ | ||
| 529 | To fix this, erase UICR manually, for example using `probe-rs erase` or `nrfjprog --eraseuicr`." | ||
| 530 | ); | ||
| 531 | } | ||
| 532 | } | ||
| 533 | } | ||
| 534 | |||
| 496 | if needs_reset { | 535 | if needs_reset { |
| 497 | cortex_m::peripheral::SCB::sys_reset(); | 536 | cortex_m::peripheral::SCB::sys_reset(); |
| 498 | } | 537 | } |
