Skip to content

Commit d9d81ee

Browse files
authored
Merge pull request #8 from bitcraze/evoggy/bor-disable
Disable BOR in option bytes if enabled.
2 parents 7a39c1c + 02d19ff commit d9d81ee

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

Core/Src/main.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,31 @@ static uint16_t convertAdcToMilliamps(uint8_t adc_8bit, uint16_t sense_resistor_
9898
/* Private user code ---------------------------------------------------------*/
9999
/* USER CODE BEGIN 0 */
100100

101+
/**
102+
* @brief Disable Brown-Out Reset (BOR) if it is currently enabled in the option bytes.
103+
*/
104+
static void disable_bor_if_enabled(void) {
105+
if (FLASH->OPTR & FLASH_OPTR_BOR_EN) {
106+
FLASH_OBProgramInitTypeDef ob_cfg = {0};
107+
108+
HAL_FLASH_Unlock();
109+
HAL_FLASH_OB_Unlock();
110+
111+
ob_cfg.OptionType = OPTIONBYTE_USER;
112+
ob_cfg.USERType = OB_USER_BOR_EN;
113+
ob_cfg.USERConfig = OB_BOR_DISABLE;
114+
115+
HAL_FLASHEx_OBProgram(&ob_cfg);
116+
117+
/* Launch option byte reload - this triggers a system reset */
118+
HAL_FLASH_OB_Launch();
119+
120+
/* Should not reach here as OB_Launch resets the MCU */
121+
HAL_FLASH_OB_Lock();
122+
HAL_FLASH_Lock();
123+
}
124+
}
125+
101126
/* USER CODE END 0 */
102127

103128
/**
@@ -117,6 +142,9 @@ int main(void)
117142

118143
/* USER CODE BEGIN Init */
119144

145+
/* Disable BOR in option bytes if it is currently enabled */
146+
disable_bor_if_enabled();
147+
120148
/* USER CODE END Init */
121149

122150
/* Configure the system clock */

0 commit comments

Comments
 (0)