Skip to content

Commit

Permalink
Merge pull request #1330 from andars/fix-some-stm32l0-flashing-issues
Browse files Browse the repository at this point in the history
Fixed some flashing issues on STM32L0.
  • Loading branch information
Nightwalker-87 committed Sep 1, 2023
2 parents 99925c6 + 1861b8d commit f3fcaf2
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
5 changes: 5 additions & 0 deletions flashloaders/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ stm32f0.o: stm32f0.s
stm32vl.o: stm32f0.s
$(CC) stm32f0.s $(CFLAGS_ARMV7_M) -o stm32vl.o

# separate rule for STM32Lx.
# Built for ARMv6-M target to be compatible with both Cortex M0 and Cortex M3.
stm32lx.o: stm32lx.s
$(CC) stm32lx.s $(CFLAGS_ARMV6_M) -o stm32lx.o

# generic rule for all other ARMv7-M
%.o: %.s
$(CC) $< $(CFLAGS_ARMV7_M) -o $@
Expand Down
4 changes: 2 additions & 2 deletions flashloaders/stm32lx.s
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ loop:
str r4, [r1]

# increment address
add r0, r0, #4
add r1, r1, #4
adds r0, r0, #4
adds r1, r1, #4

# loop if count > 0
subs r2, r2, #4
Expand Down
15 changes: 7 additions & 8 deletions src/stlink-lib/flash_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ static const uint8_t loader_code_stm32f0[] = {
0x14, 0x00, 0x00, 0x00
};

// flashloaders/stm32lx.s
// flashloaders/stm32lx.s -- compiled for armv6-m for compatibility with both
// armv6-m cores (STM32L0) and armv7-m cores (STM32L1)
static const uint8_t loader_code_stm32lx[] = {
0x04, 0x68, 0x0c, 0x60,
0x00, 0xf1, 0x04, 0x00,
0x01, 0xf1, 0x04, 0x01,
0x04, 0x3a, 0xf7, 0xdc,
0x04, 0x30, 0x04, 0x31,
0x04, 0x3a, 0xf9, 0xdc,
0x00, 0xbe, 0x00, 0x00
};

Expand Down Expand Up @@ -428,12 +428,11 @@ int32_t stlink_flash_loader_run(stlink_t *sl, flash_loader_t* fl, stm32_addr_t t
#define L1_WRITE_BLOCK_SIZE 0x80
#define L0_WRITE_BLOCK_SIZE 0x40

int32_t stm32l1_write_half_pages(stlink_t *sl, stm32_addr_t addr, uint8_t *base, uint32_t len, uint32_t pagesize) {
int32_t stm32l1_write_half_pages(stlink_t *sl, flash_loader_t *fl, stm32_addr_t addr, uint8_t *base, uint32_t len, uint32_t pagesize) {
uint32_t count, off;
uint32_t num_half_pages = len / pagesize;
uint32_t val;
uint32_t flash_regs_base = get_stm32l0_flash_base(sl);
flash_loader_t fl;
bool use_loader = true;
int32_t ret = 0;

Expand All @@ -448,7 +447,7 @@ int32_t stm32l1_write_half_pages(stlink_t *sl, stm32_addr_t addr, uint8_t *base,

for (count = 0; count < num_half_pages; count++) {
if (use_loader) {
ret = stlink_flash_loader_run(sl, &fl, addr + count * pagesize, base + count * pagesize, pagesize);
ret = stlink_flash_loader_run(sl, fl, addr + count * pagesize, base + count * pagesize, pagesize);
if (ret && count == 0) {
/* It seems that stm32lx devices have a problem when it is blank */
WLOG("Failed to use flash loader, fallback to soft write\n");
Expand Down Expand Up @@ -770,7 +769,7 @@ int32_t stlink_flashloader_write(stlink_t *sl, flash_loader_t *fl, stm32_addr_t
off = 0;

if (len > pagesize) {
if (stm32l1_write_half_pages(sl, addr, base, len, pagesize)) {
if (stm32l1_write_half_pages(sl, fl, addr, base, len, pagesize)) {
return (-1);
} else {
off = (size_t)(len / pagesize) * pagesize;
Expand Down
2 changes: 1 addition & 1 deletion src/stlink-lib/flash_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ int32_t stlink_flash_loader_run(stlink_t *sl, flash_loader_t* fl, stm32_addr_t t

/* === Functions from old header file flashloader.h === */

int32_t stm32l1_write_half_pages(stlink_t *sl, stm32_addr_t addr, uint8_t *base, uint32_t len, uint32_t pagesize);
int32_t stm32l1_write_half_pages(stlink_t *sl, flash_loader_t *fl, stm32_addr_t addr, uint8_t *base, uint32_t len, uint32_t pagesize);
// static void set_flash_cr_pg(stlink_t *sl, uint32_t bank);
// static void set_dma_state(stlink_t *sl, flash_loader_t *fl, int32_t bckpRstr);
int32_t stlink_flashloader_start(stlink_t *sl, flash_loader_t *fl);
Expand Down

0 comments on commit f3fcaf2

Please sign in to comment.