ESPHome 2025.12.0-dev
Loading...
Searching...
No Matches
uicr.cpp
Go to the documentation of this file.
2
3#ifdef USE_NRF52_REG0_VOUT
4#include <zephyr/init.h>
5#include <hal/nrf_power.h>
6#include <zephyr/sys/printk.h>
7
8extern "C" {
9void nvmc_config(uint32_t mode);
10void nvmc_wait();
12}
13
14namespace esphome::nrf52 {
15
16enum class StatusFlags : uint8_t {
17 OK = 0x00,
18 NEED_RESET = 0x01,
19 NEED_ERASE = 0x02,
20};
21
23 a = static_cast<StatusFlags>(static_cast<uint8_t>(a) | static_cast<uint8_t>(b));
24 return a;
25}
26
27constexpr bool operator&(StatusFlags a, StatusFlags b) {
28 return (static_cast<uint8_t>(a) & static_cast<uint8_t>(b)) != 0;
29}
30
31static bool regout0_ok() {
32 return (NRF_UICR->REGOUT0 & UICR_REGOUT0_VOUT_Msk) == (USE_NRF52_REG0_VOUT << UICR_REGOUT0_VOUT_Pos);
33}
34
35static StatusFlags set_regout0() {
36 /* If the board is powered from USB (high voltage mode),
37 * GPIO output voltage is set to 1.8 volts by default.
38 */
39 if (!regout0_ok()) {
40 nvmc_config(NVMC_CONFIG_WEN_Wen);
41 NRF_UICR->REGOUT0 =
42 (NRF_UICR->REGOUT0 & ~((uint32_t) UICR_REGOUT0_VOUT_Msk)) | (USE_NRF52_REG0_VOUT << UICR_REGOUT0_VOUT_Pos);
43 nvmc_wait();
44 nvmc_config(NVMC_CONFIG_WEN_Ren);
46 }
47 return StatusFlags::OK;
48}
49
50#ifndef USE_BOOTLOADER_MCUBOOT
51// https://github.com/adafruit/Adafruit_nRF52_Bootloader/blob/6a9a6a3e6d0f86918e9286188426a279976645bd/lib/sdk11/components/libraries/bootloader_dfu/dfu_types.h#L61
52constexpr uint32_t BOOTLOADER_REGION_START = 0x000F4000;
53constexpr uint32_t BOOTLOADER_MBR_PARAMS_PAGE_ADDRESS = 0x000FE000;
54
55static bool bootloader_ok() {
56 return NRF_UICR->NRFFW[0] == BOOTLOADER_REGION_START && NRF_UICR->NRFFW[1] == BOOTLOADER_MBR_PARAMS_PAGE_ADDRESS;
57}
58
59static StatusFlags fix_bootloader() {
60 if (!bootloader_ok()) {
61 nvmc_config(NVMC_CONFIG_WEN_Wen);
62 NRF_UICR->NRFFW[0] = BOOTLOADER_REGION_START;
63 NRF_UICR->NRFFW[1] = BOOTLOADER_MBR_PARAMS_PAGE_ADDRESS;
64 nvmc_wait();
65 nvmc_config(NVMC_CONFIG_WEN_Ren);
66 return bootloader_ok() ? StatusFlags::NEED_RESET : StatusFlags::NEED_ERASE;
67 }
68 return StatusFlags::OK;
69}
70#endif
71
72#define BOOTLOADER_VERSION_REGISTER NRF_TIMER2->CC[0]
73
74static StatusFlags set_uicr() {
76#ifndef USE_BOOTLOADER_MCUBOOT
77 if (BOOTLOADER_VERSION_REGISTER <= 0x902) {
78#ifdef CONFIG_PRINTK
79 printk("cannot control regout0 for %#x\n", BOOTLOADER_VERSION_REGISTER);
80#endif
81 } else
82#endif
83 {
84 status |= set_regout0();
85 }
86#ifndef USE_BOOTLOADER_MCUBOOT
87 status |= fix_bootloader();
88#endif
89 return status;
90}
91
92static int board_esphome_init() {
93 StatusFlags status = set_uicr();
94
95#ifdef USE_NRF52_UICR_ERASE
96 if (status & StatusFlags::NEED_ERASE) {
97 nrfx_err_t ret = nrfx_nvmc_uicr_erase();
98 if (ret != NRFX_SUCCESS) {
99#ifdef CONFIG_PRINTK
100 printk("nrfx_nvmc_uicr_erase failed %d\n", ret);
101#endif
102 } else {
103 status |= set_uicr();
104 }
105 }
106#endif
107
108 if (status & StatusFlags::NEED_RESET) {
109 /* a reset is required for changes to take effect */
110 NVIC_SystemReset();
111 }
112
113 return 0;
114}
115} // namespace esphome::nrf52
116
117static int board_esphome_init() { return esphome::nrf52::board_esphome_init(); }
118
119SYS_INIT(board_esphome_init, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
120
121#endif
BedjetMode mode
BedJet operating mode.
uint8_t status
Definition bl0942.h:8
constexpr StatusFlags & operator|=(StatusFlags &a, StatusFlags b)
Definition uicr.cpp:22
constexpr bool operator&(StatusFlags a, StatusFlags b)
Definition uicr.cpp:27
constexpr uint32_t BOOTLOADER_MBR_PARAMS_PAGE_ADDRESS
Definition uicr.cpp:53
constexpr uint32_t BOOTLOADER_REGION_START
Definition uicr.cpp:52
void nvmc_config(uint32_t mode)
void nvmc_wait()
nrfx_err_t nrfx_nvmc_uicr_erase()
SYS_INIT(board_esphome_init, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT)