22-11-2022, 12:59 PM
I've previously been working with STM32F411 but recently switched to STM32G473, so I need to learn new things.
STM32G473 have dual-bank FLASH, configurable via the option bytes as single- or dual-bank. Factory default is dual-bank.
I will use singe-bank mode and therefore need to somehow program the option bytes.
My first attempt was to embed the option bytes via the source code into the .hex file, to be flashed when writing/updating the firmware on the CPU. I was able to do this with the help of the linker script. STM32CubeProgrammer will take the generated .hex file and write the option bytes along with the rest of the firmware. This works. I copy/paste the code here in case someone else have use for it:
In the source code:
In the linker script:
BUT, unfortunately this does not work with EBlink 4.7 in EmBitz 2.50:
And I guess the reason is in EBlink/scripts/stmicro/stm32gx.script
I've looked briefly at the script code and I would not know where to start, if I try to implement this myself.
Are there any plans of implementing this anytime soon?
What are my alternatives? Suggestions are appreciated as this is a new CPU for me and I'm learning.
At the moment I'm in development, and can set the options with STM32CubeProgrammer, but eventually I will be getting factory empty CPU's and by then I need to get them flashed as conveniently as possible, that was why I initially tried to embed the option bytes directly in the .hex file.
STM32G473 have dual-bank FLASH, configurable via the option bytes as single- or dual-bank. Factory default is dual-bank.
I will use singe-bank mode and therefore need to somehow program the option bytes.
My first attempt was to embed the option bytes via the source code into the .hex file, to be flashed when writing/updating the firmware on the CPU. I was able to do this with the help of the linker script. STM32CubeProgrammer will take the generated .hex file and write the option bytes along with the rest of the firmware. This works. I copy/paste the code here in case someone else have use for it:
In the source code:
Code:
#include <stdint.h>
#include "stm32g4xx_hal.h"
/// User and read protection option bytes
#define OPTR_CONTENT \
(1 << 31) |
OB_IRH_ENABLE |
OB_NRST_MODE_INPUT_ONLY |
...
/// Option byte array (single-bank or bank 1) located at the correct address in memory based on the section definition in the linker script
uint32_t application_option_bytes_singlebank_or_bank1[6 * 2] __attribute__((section(".option_bytes_bank1_section"))) =
{
(uint32_t)(OPTR_CONTENT), ~(uint32_t)(OPTR_CONTENT),
(uint32_t)(PCROP1_START_CONTENT), ~(uint32_t)(PCROP1_START_CONTENT),
(uint32_t)(PCROP1_END_CONTENT), ~(uint32_t)(PCROP1_END_CONTENT),
(uint32_t)(WRP1A_CONTENT), ~(uint32_t)(WRP1A_CONTENT),
(uint32_t)(WRP2A_CONTENT), ~(uint32_t)(WRP2A_CONTENT),
(uint32_t)(SEC1_CONTENT), ~(uint32_t)(SEC1_CONTENT),
};
/// Option byte array (bank 2) located at the correct address in memory based on the section definition in the linker script
uint32_t application_option_bytes_bank2[5 * 2] __attribute__((section(".option_bytes_bank2_section"))) =
{
(uint32_t)(PCROP2_START_CONTENT), ~(uint32_t)(PCROP2_START_CONTENT),
(uint32_t)(PCROP2_END_CONTENT), ~(uint32_t)(PCROP2_END_CONTENT),
(uint32_t)(WRP1B_CONTENT), ~(uint32_t)(WRP1B_CONTENT),
(uint32_t)(WRP2B_CONTENT), ~(uint32_t)(WRP2B_CONTENT),
(uint32_t)(SEC2_CONTENT), ~(uint32_t)(SEC2_CONTENT),
};
In the linker script:
Code:
MEMORY
{
...
OPT_B1 (rw) : ORIGIN = 0x1FFF7800, LENGTH = 48 /* Options bytes single-bank or bank 1 */
OPT_B2 (rw) : ORIGIN = 0x1FFFF808, LENGTH = 40 /* Options bytes bank 2 */
...
}
SECTIONS
{
...
/* Options bytes single-bank or bank 1 */
.option_bytes_bank1_section :
{
KEEP(*(.option_bytes_bank1_section*))
} > OPT_B1
/* Options bytes bank 2 */
.option_bytes_bank2_section :
{
KEEP(*(.option_bytes_bank2_section*))
} > OPT_B2
...
}
BUT, unfortunately this does not work with EBlink 4.7 in EmBitz 2.50:
Quote:EBlink version 4.7-[13] by Gerard Zagema
Interface USB# : 54FF6A066684565547522187
Interface type : STlink/V2
STlink connect : Under reset
Target voltage : 3.25V
Interface speed: 4000KHz
Target detected: Cortex-M4 (r0p1) with FPv4_SP
HW breakpoints : 6
HW watchpoints : 4
Fault unwind : Active break (level 2)
STmicro family : STM32G47/48xx
Detected FLASH : 0x80000
Configured RAM : 0x20000
Reset: system
Try to flash a non-flash region! Address 0x1FFF7800
Try to flash a non-flash region! Address 0x1FFFF808
Checking current flash content
Flash programming done
Flash verify done
Flasher starts target
Reset: system
And I guess the reason is in EBlink/scripts/stmicro/stm32gx.script
Quote://///////////////////////////////////////////////////
//
// STM32Gx
//
// ToDo: - option programming
//
I've looked briefly at the script code and I would not know where to start, if I try to implement this myself.
Are there any plans of implementing this anytime soon?
What are my alternatives? Suggestions are appreciated as this is a new CPU for me and I'm learning.
At the moment I'm in development, and can set the options with STM32CubeProgrammer, but eventually I will be getting factory empty CPU's and by then I need to get them flashed as conveniently as possible, that was why I initially tried to embed the option bytes directly in the .hex file.