From ecb914fe139956cc7ca66291b48cb9c3492150e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20J=C3=A4ger?= Date: Mon, 7 Jun 2021 13:11:34 +0200 Subject: [PATCH] Fix STM32G4 bootloader triggering The MPU must be disabled to allow writing the option bytes. Also replacing flash_write_protection_set with direct register access because it will be deprecated in Zephyr v2.6. --- boards/arm/mppt_2420_hc/mppt_2420_hc_0_2_0.conf | 1 - boards/arm/mppt_2420_hc/mppt_2420_hc_defconfig | 1 + src/hardware.cpp | 11 +++++++---- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/boards/arm/mppt_2420_hc/mppt_2420_hc_0_2_0.conf b/boards/arm/mppt_2420_hc/mppt_2420_hc_0_2_0.conf index b85a808..a956d33 100644 --- a/boards/arm/mppt_2420_hc/mppt_2420_hc_0_2_0.conf +++ b/boards/arm/mppt_2420_hc/mppt_2420_hc_0_2_0.conf @@ -3,7 +3,6 @@ CONFIG_FLASH=y CONFIG_FLASH_PAGE_LAYOUT=y CONFIG_FLASH_LOG_LEVEL_WRN=y -#CONFIG_MPU_ALLOW_FLASH_WRITE=y CONFIG_SPI=y CONFIG_SPI_NOR=y diff --git a/boards/arm/mppt_2420_hc/mppt_2420_hc_defconfig b/boards/arm/mppt_2420_hc/mppt_2420_hc_defconfig index 6bb24ee..e1fdb80 100644 --- a/boards/arm/mppt_2420_hc/mppt_2420_hc_defconfig +++ b/boards/arm/mppt_2420_hc/mppt_2420_hc_defconfig @@ -55,6 +55,7 @@ CONFIG_UART_CONSOLE=y # Enable MPU CONFIG_ARM_MPU=y +CONFIG_MPU_ALLOW_FLASH_WRITE=y CONFIG_PWM=y diff --git a/src/hardware.cpp b/src/hardware.cpp index 4e07fa7..26e5d7d 100644 --- a/src/hardware.cpp +++ b/src/hardware.cpp @@ -20,7 +20,6 @@ #include #include -#include #include LOG_MODULE_REGISTER(hardware, CONFIG_HW_LOG_LEVEL); @@ -36,8 +35,12 @@ void start_stm32_bootloader() k_sleep(K_MSEC(100)); // wait for capacitor at BOOT0 pin to charge up reset_device(); #elif defined(CONFIG_SOC_SERIES_STM32G4X) - const struct device *flash = device_get_binding(DT_LABEL(DT_NODELABEL(flash0))); - flash_write_protection_set(flash, false); + if ((FLASH->CR & FLASH_CR_OPTLOCK) != 0U) { + /* Authorizes the Option Byte register programming */ + FLASH->OPTKEYR = FLASH_OPTKEY1; + FLASH->OPTKEYR = FLASH_OPTKEY2; + } + // Set proper bits for booting the embedded bootloader (see table // 5 in section 2.6.1 in document RM0440) @@ -60,7 +63,7 @@ void start_stm32_bootloader() // If OBL_LAUNCH did not reset (it should), we'll force it by // first locking back the flash registers and going for a reboot. - flash_write_protection_set(flash, true); + FLASH->CR |= FLASH_CR_OPTLOCK; reset_device(); #endif }