Skip to content

Commit

Permalink
flash/stm32h5x: support STM32H5x devices
Browse files Browse the repository at this point in the history
Change-Id: Id6ea9dc66849e374d17e5677b6a1099b61d6dc0b
Signed-off-by: Tarek BOCHKATI <tarek.bouchkati@gmail.com>
  • Loading branch information
tarek-bochkati committed Apr 28, 2023
1 parent c918b29 commit d4f1ae4
Show file tree
Hide file tree
Showing 10 changed files with 1,954 additions and 1 deletion.
5 changes: 4 additions & 1 deletion contrib/loaders/flash/stm32/Makefile
Expand Up @@ -12,7 +12,7 @@ OBJDUMP=$(CROSS_COMPILE)objdump
AFLAGS = -static -nostartfiles -mlittle-endian -Wa,-EL
CFLAGS = -c -mthumb -nostdlib -nostartfiles -Os -g -fPIC

all: stm32f1x.inc stm32f2x.inc stm32h7x.inc stm32l4x.inc stm32lx.inc
all: stm32f1x.inc stm32f2x.inc stm32h5x.inc stm32h7x.inc stm32l4x.inc stm32lx.inc

.PHONY: clean

Expand All @@ -22,6 +22,9 @@ all: stm32f1x.inc stm32f2x.inc stm32h7x.inc stm32l4x.inc stm32lx.inc
stm32l4x.elf: stm32l4x.c
$(CC) $(CFLAGS) -mcpu=cortex-m0plus -fstack-usage -Wa,-adhln=$(<:.c=.lst) $< -o $@

stm32h5x.elf: stm32h5x.c
$(CC) $(CFLAGS) -mcpu=cortex-m33 -fstack-usage -Wa,-adhln=$(<:.c=.lst) $< -o $@

%.lst: %.elf
$(OBJDUMP) -S $< > $@

Expand Down
92 changes: 92 additions & 0 deletions contrib/loaders/flash/stm32/stm32h5x.c
@@ -0,0 +1,92 @@
// SPDX-License-Identifier: GPL-2.0-or-later

/*
* Copyright (C) 2022 by Tarek BOCHKATI for STMicroelectronics
* <tarek.bouchkati@st.com>
*/

#define OPENOCD_CONTRIB_LOADERS_FLASH_STM32_STM32H5X

#include <stdint.h>
#include "../../../../src/flash/nor/stm32h5x.h"

static inline __attribute__((always_inline))
void copy_buffer_u32(uint32_t *dst, uint32_t *src, int len)
{
for (int i = 0; i < len; i++)
dst[i] = src[i];
}

/* this function is assumes that fifo_size is multiple of flash_word_size
* this condition is ensured by target_run_flash_async_algorithm
*/

void write(volatile struct stm32h5x_work_area *work_area,
uint8_t *fifo_end,
uint8_t *target_address,
uint32_t count)
{
volatile uint32_t *flash_sr = (uint32_t *)work_area->params.flash_sr_addr;
volatile uint32_t *flash_cr = (uint32_t *)work_area->params.flash_cr_addr;

/* optimization to avoid reading from memory each time */
uint8_t *rp_cache = work_area->fifo.rp;

/* fifo_start is used to wrap when we reach fifo_end */
uint8_t *fifo_start = rp_cache;

/* enable flash programming */
*flash_cr = FLASH_PG;

while (count) {
/* optimization to avoid reading from memory each time */
uint8_t *wp_cache = work_area->fifo.wp;
if (wp_cache == 0)
break; /* aborted by target_run_flash_async_algorithm */

int32_t fifo_size = wp_cache - rp_cache;
if (fifo_size < 0) {
/* consider the linear fifo, we will wrap later */
fifo_size = fifo_end - rp_cache;
}

/* wait for at least a flash word */
while (fifo_size >= work_area->params.flash_word_size) {
copy_buffer_u32((uint32_t *)target_address,
(uint32_t *)rp_cache,
work_area->params.flash_word_size / 4);

/* update target_address and rp_cache */
target_address += work_area->params.flash_word_size;
rp_cache += work_area->params.flash_word_size;

/* wait for the busy flag */
while (*flash_sr & FLASH_BSY)
;

if (*flash_sr & FLASH_ERROR) {
work_area->fifo.rp = 0; /* set rp to zero 0 on error */
goto write_end;
}

/* wrap if reach the fifo_end, and update rp in memory */
if (rp_cache >= fifo_end)
rp_cache = fifo_start;

/* flush the rp cache value,
* so target_run_flash_async_algorithm can fill the circular fifo */
work_area->fifo.rp = rp_cache;

/* update fifo_size and count */
fifo_size -= work_area->params.flash_word_size;
count--;
}
}

write_end:
/* disable flash programming */
*flash_cr = 0;

/* soft break the loader */
__asm("bkpt 0");
}
9 changes: 9 additions & 0 deletions contrib/loaders/flash/stm32/stm32h5x.inc
@@ -0,0 +1,9 @@
/* Autogenerated with ../../../../src/helper/bin2char.sh */
0x2d,0xe9,0xf0,0x43,0x02,0x24,0xd0,0xf8,0x00,0x80,0xd0,0xf8,0x04,0xe0,0xd0,0xf8,
0x74,0x90,0xce,0xf8,0x00,0x40,0x4c,0x46,0xe3,0xb1,0x05,0x6f,0xd5,0xb1,0x2d,0x1b,
0x00,0xd5,0x0d,0x1b,0x86,0x68,0xae,0x42,0xf6,0xd8,0x86,0x68,0x4f,0xea,0x96,0x0c,
0x00,0x26,0xb4,0x45,0x14,0xdc,0x86,0x68,0x32,0x44,0x86,0x68,0x34,0x44,0xd8,0xf8,
0x00,0x60,0x16,0xf0,0x01,0x06,0xfa,0xd1,0xd8,0xf8,0x00,0x70,0x17,0xf4,0x7e,0x0f,
0x0c,0xd0,0x46,0x67,0x00,0x23,0xce,0xf8,0x00,0x30,0x00,0xbe,0xbd,0xe8,0xf0,0x83,
0x54,0xf8,0x26,0x70,0x42,0xf8,0x26,0x70,0x01,0x36,0xe2,0xe7,0xa1,0x42,0x98,0xbf,
0x4c,0x46,0x44,0x67,0x86,0x68,0x01,0x3b,0xad,0x1b,0xd3,0xe7,
1 change: 1 addition & 0 deletions src/flash/nor/Makefile.am
Expand Up @@ -68,6 +68,7 @@ NOR_DRIVERS = \
%D%/stm32f2x.c \
%D%/stm32lx.c \
%D%/stm32l4x.c \
%D%/stm32h5x.c \
%D%/stm32h7x.c \
%D%/str7x.c \
%D%/str9x.c \
Expand Down
2 changes: 2 additions & 0 deletions src/flash/nor/drivers.c
Expand Up @@ -65,6 +65,7 @@ extern const struct flash_driver stm32f1x_flash;
extern const struct flash_driver stm32f2x_flash;
extern const struct flash_driver stm32lx_flash;
extern const struct flash_driver stm32l4x_flash;
extern const struct flash_driver stm32h5x_flash;
extern const struct flash_driver stm32h7x_flash;
extern const struct flash_driver stmqspi_flash;
extern const struct flash_driver stmsmi_flash;
Expand Down Expand Up @@ -141,6 +142,7 @@ static const struct flash_driver * const flash_drivers[] = {
&stm32f2x_flash,
&stm32lx_flash,
&stm32l4x_flash,
&stm32h5x_flash,
&stm32h7x_flash,
&stmsmi_flash,
&stmqspi_flash,
Expand Down

0 comments on commit d4f1ae4

Please sign in to comment.