I finally found the right code here : https://github.com/T-K-233/Notes/blob/main/stm32/misc/changing-stm32-default-boot-option.md
I have modified it , in order to allow to clear or to set the nSWBOOT0 bit .
It is Arduino code , I hope it isn't an issue ; 2 things one need to watch out for :
- avoid unwanted call to the function (that modifies the nSWBOOT0 bit , or anything in the flash domain)
- avoid to catch the function in an infinite loop , this may destroy the device
#define LED PC6
String ze_string;
void setup() {
Serial.begin(115200); // USB CDC
pinMode(LED, OUTPUT);
while (!Serial) { // wait for USB CDC → ok
delay(50);
}
// if not cleard , clear the nSWBOOT0 in the setup , in order to allow normal CAN operation
Serial.print("FLASH->OPTR before setup: ");
Serial.println(FLASH->OPTR, HEX);
if (FLASH->OPTR |= FLASH_OPTR_nSWBOOT0) { // is nSWBOOT0 set ?
set_clear_nSWBOOT0(false); // then clear it
digitalWrite(LED, HIGH);
}
Serial.print("FLASH->OPTR after setup , must be FBEFF8AA : ");
Serial.println(FLASH->OPTR, HEX);
}
void loop() {
while (Serial.available()) {
ze_string = Serial.readString(); // read the incoming data as string
Serial.print(" Serial: ");
Serial.println(ze_string);
if (ze_string.equals("Please dear STM32 , would you be so kind and allow BOOT0 pin to be in use , thank you !")) {
if ((FLASH->OPTR & FLASH_OPTR_nSWBOOT0) == 0) { // isn't nSWBOOT0 set ?
set_clear_nSWBOOT0(true); // then set it
Serial.println(FLASH->OPTR, HEX);
Serial.println(" : FFEFF8AA → BOOT0 pin in use , please cycle STM32 before uploading in DFU mode");
digitalWrite(LED, LOW);
}
}
}
Serial.print("i"); /// debug
Serial.println(FLASH->OPTR, HEX); /// debug
delay(500); /// debug
}
// https://github.com/T-K-233/Notes/blob/main/stm32/misc/changing-stm32-default-boot-option.md
void set_clear_nSWBOOT0(bool set_nSWBOOT0) {
// 1. Unlock the FLASH_CR with the LOCK clearing sequence
// Check that no Flash memory operation is on going by checking the BSY bit in the Flash status register (FLASH_SR).
while (__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY)) {}
HAL_FLASH_Unlock();
// 2. Unlock the FLASH Option Byte with the LOCK clearing sequence
// Check that no Flash memory operation is on going by checking the BSY bit in the Flash status register (FLASH_SR).
while (__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY)) {}
HAL_FLASH_OB_Unlock();
// 3. program OPTR
if (set_nSWBOOT0) { // → set nSWBOOT0 bit
if (FLASH->OPTR | FLASH_OPTR_nSWBOOT0) // is nSWBOOT0 set ?
FLASH->OPTR |= FLASH_OPTR_nSWBOOT0; // default to boot according BOOT0 pin
} else { // → clear nSWBOOT0 bit
if (FLASH->OPTR | FLASH_OPTR_nSWBOOT0 == 0) // isn't nSWBOOT0 set ?
FLASH->OPTR &= ~FLASH_OPTR_nSWBOOT0; // default to boot from flash
}
// 4. Set the Options Start bit OPTSTRT in the Flash control register (FLASH_CR).
SET_BIT(FLASH->CR, FLASH_CR_OPTSTRT);
// 4.1 clear status register
SET_BIT(FLASH->SR, FLASH_SR_OPTVERR | FLASH_SR_RDERR);
// 5. Wait for the BSY bit to be cleared.
while (__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY)) {}
// 6. Lock Flash
// If LOCK is set by software, OPTLOCK is automatically set too
HAL_FLASH_Lock();
// 7. reload the new settings
// seems this line will cause error when put before FLASH_Lock(), which will then corrupt all Flash settings
// so putting it here
// can also comment out this line and just power cycle to update the flash settings
HAL_FLASH_OB_Launch();
}