Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[STM32WLE]: Erase flash fails on second page #1305

Closed
5 tasks done
victor-dryad opened this issue Apr 18, 2023 · 18 comments
Closed
5 tasks done

[STM32WLE]: Erase flash fails on second page #1305

victor-dryad opened this issue Apr 18, 2023 · 18 comments

Comments

@victor-dryad
Copy link

victor-dryad commented Apr 18, 2023

  • Programmer/board type: [STLINK/V3SET]
  • Operating system an version: [Linux]
  • stlink tools version and/or git commit hash: [1.7.0-261-g1745bf5-dirty]
  • stlink commandline tool name: [st-flash]
  • Target chip (and board, if applicable): [STM32WLE]

Commandline output:

$ sudo st-flash --debug --freq=100k --serial 004C00293331510833323639 write ~/play/EN_Silva.bin 0x8008000
st-flash 1.7.0-261-g1745bf5-dirty
2023-04-19T10:08:30 DEBUG common.c: *** looking up stlink version ***
2023-04-19T10:08:30 DEBUG common.c: st vid         = 0x0483 (expect 0x0483)
2023-04-19T10:08:30 DEBUG common.c: stlink pid     = 0x374f
2023-04-19T10:08:30 DEBUG common.c: stlink version = 0x3
2023-04-19T10:08:30 DEBUG common.c: jtag version   = 0xc
2023-04-19T10:08:30 DEBUG common.c: swim version   = 0x1
2023-04-19T10:08:30 DEBUG common.c: stlink current mode: mass
2023-04-19T10:08:30 DEBUG usb.c: JTAG/SWD freq set to 100
2023-04-19T10:08:30 INFO usb.c: Unable to match requested speed 100 kHz, using 50 kHz
2023-04-19T10:08:30 DEBUG common.c: stlink current mode: mass
2023-04-19T10:08:30 DEBUG common.c: *** stlink_enter_swd_mode ***
2023-04-19T10:08:30 DEBUG common.c: Loading device parameters....
2023-04-19T10:08:30 DEBUG common.c: *** stlink_core_id ***
2023-04-19T10:08:30 DEBUG common.c: core_id = 0x6ba02477
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_read_debug32 0x410fc241 at 0xe000ed00
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_read_debug32 0x10016497 at 0xe0042000
2023-04-19T10:08:30 DEBUG chipid.c: detected chip_id parameters

2023-04-19T10:08:30 DEBUG chipid.c: # Device Type: STM32WLEx
2023-04-19T10:08:30 DEBUG chipid.c: # Reference Manual: RM0033
2023-04-19T10:08:30 DEBUG chipid.c: #
2023-04-19T10:08:30 DEBUG chipid.c: chip_id 0x497
2023-04-19T10:08:30 DEBUG chipid.c: flash_type 11
2023-04-19T10:08:30 DEBUG chipid.c: flash_size_reg 0x1fff75e0
2023-04-19T10:08:30 DEBUG chipid.c: flash_pagesize 0x800
2023-04-19T10:08:30 DEBUG chipid.c: sram_size 0x10000
2023-04-19T10:08:30 DEBUG chipid.c: bootrom_base 0x1fff0000
2023-04-19T10:08:30 DEBUG chipid.c: bootrom_size 0x7000
2023-04-19T10:08:30 DEBUG chipid.c: option_base 0x1fff7800
2023-04-19T10:08:30 DEBUG chipid.c: option_size 0x10
2023-04-19T10:08:30 DEBUG chipid.c: flags 2

2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_read_debug32 0xffff0100 at 0x1fff75e0
2023-04-19T10:08:30 INFO common.c: STM32WLEx: 64 KiB SRAM, 256 KiB flash in at least 2 KiB pages.
2023-04-19T10:08:30 DEBUG common.c: *** stlink_force_debug_mode ***
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_read_debug32 0x00001800 at 0xe004203c
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_write_debug32 0x00001800 to 0xe004203c
2023-04-19T10:08:30 DEBUG common.c: *** stlink_status ***
2023-04-19T10:08:30 DEBUG usb.c: core status: 01030003
2023-04-19T10:08:30 DEBUG common.c:   core status: halted
file /home/victor/play/EN_Silva.bin md5 checksum: d6dbf4ee312a850538c2dd92edb9e89, stlink checksum: 0x014d4616
2023-04-19T10:08:30 INFO common_flash.c: Attempting to write 211776 (0x33b40) bytes to stm32 address: 134250496 (0x8008000)
2023-04-19T10:08:30 DEBUG common.c: *** stlink_core_id ***
2023-04-19T10:08:30 DEBUG common.c: core_id = 0x6ba02477
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_read_debug32 0000000000 at 0x58004010
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_write_debug32 0x000003f8 to 0x58004010
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_read_debug32 0xc0000000 at 0x58004014
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_write_debug32 0x45670123 to 0x58004008
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_write_debug32 0xcdef89ab to 0x58004008
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_read_debug32 0x40000000 at 0x58004014
2023-04-19T10:08:30 DEBUG common_flash.c: Successfully unlocked flash
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_read_debug32 0x40000000 at 0x58004014
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_write_debug32 0x40000002 to 0x58004014
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_read_debug32 0x40000002 at 0x58004014
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_write_debug32 0x40000082 to 0x58004014
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_read_debug32 0x40000082 at 0x58004014
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_write_debug32 0x40010082 to 0x58004014
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_read_debug32 0x00050000 at 0x58004010
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_read_debug32 0x00050000 at 0x58004010
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_read_debug32 0x00050000 at 0x58004010
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_read_debug32 0x00050000 at 0x58004010
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_read_debug32 0x00050000 at 0x58004010
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_read_debug32 0x00050000 at 0x58004010
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_read_debug32 0x00050000 at 0x58004010
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_read_debug32 0x00050000 at 0x58004010
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_read_debug32 0x00050000 at 0x58004010
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_read_debug32 0000000000 at 0x58004010
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_read_debug32 0x40000082 at 0x58004014
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_write_debug32 0x40000080 to 0x58004014
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_read_debug32 0x40000080 at 0x58004014
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_write_debug32 0x40000081 to 0x58004014
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_read_debug32 0000000000 at 0x58004010
2023-04-19T10:08:30 DEBUG common_flash.c: 
read_flash_sr(sl, BANK_1): 0000000000 STM32WB_FLASH_SR_ERROR_MASK 0x3f8
2023-04-19T10:08:30 DEBUG common_flash.c: res & STM32WB_FLASH_SR_ERROR_MASK: 0000000000
-> Flash page at 0x8008000 erased (size: 0x800)
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_read_debug32 0000000000 at 0x58004010
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_write_debug32 0x000003f8 to 0x58004010
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_read_debug32 0x40000081 at 0x58004014
2023-04-19T10:08:30 DEBUG common_flash.c: Successfully unlocked flash
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_read_debug32 0x40000081 at 0x58004014
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_write_debug32 0x40000083 to 0x58004014
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_read_debug32 0x40000083 at 0x58004014
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_write_debug32 0x4000008b to 0x58004014
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_read_debug32 0x4000008b at 0x58004014
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_write_debug32 0x4001008b to 0x58004014
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_read_debug32 0x00000080 at 0x58004010
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_read_debug32 0x4000008b at 0x58004014
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_write_debug32 0x40000089 to 0x58004014
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_read_debug32 0x40000089 at 0x58004014
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_write_debug32 0x40000089 to 0x58004014
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_read_debug32 0x00000080 at 0x58004010
2023-04-19T10:08:30 DEBUG common_flash.c: 
read_flash_sr(sl, BANK_1): 0x00000080 STM32WB_FLASH_SR_ERROR_MASK 0x3f8
2023-04-19T10:08:30 DEBUG common_flash.c: res & STM32WB_FLASH_SR_ERROR_MASK: 0x00000080
2023-04-19T10:08:30 ERROR common_flash.c: Flash programming error: 0x00000080
2023-04-19T10:08:30 WARN common_flash.c: Failed to erase_flash_page(0x8008800) == -1
2023-04-19T10:08:30 ERROR common_flash.c: Failed to erase the flash prior to writing
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_read_debug32 0xffffffff at 0x08008004
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_write_reg
2023-04-19T10:08:30 DEBUG common.c: *** stlink_run ***
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_read_reg
2023-04-19T10:08:30 DEBUG read_write.c:  (16) ***
2023-04-19T10:08:30 DEBUG common.c: data_len = 8 0x8

 80 00 00 00 00 00 00 61
2023-04-19T10:08:30 DEBUG usb.c: r_idx (16) = 0x61000000
stlink_fwrite_flash() == -1
2023-04-19T10:08:30 DEBUG common.c: *** stlink_exit_debug_mode ***
2023-04-19T10:08:30 DEBUG read_write.c: *** stlink_write_debug32 0xa05f0000 to 0xe000edf0
2023-04-19T10:08:30 DEBUG common.c: *** stlink_close ***

@Nightwalker-87 Nightwalker-87 added this to To do in Release v1.8.0 via automation Apr 30, 2023
@Nightwalker-87 Nightwalker-87 added this to the v1.8.0 milestone Apr 30, 2023
@Nightwalker-87 Nightwalker-87 moved this from To do to In progress in Release v1.8.0 Apr 30, 2023
@Nightwalker-87
Copy link
Member

@victor-dryad : Just for a better understanding: What happens, if you change } while (addr < (base_addr + size)); to } while (addr <= (base_addr + size)); in common_flash.c line 1170?

@Nightwalker-87 Nightwalker-87 self-assigned this Apr 30, 2023
@victor-dryad
Copy link
Author

victor-dryad commented May 2, 2023

@victor-dryad : Just for a better understanding: What happens, if you change } while (addr < (base_addr + size)); to } while (addr <= (base_addr + size)); in common_flash.c line 1170?

Unfortunately looks no improvement.
Anything to add to improve the debug logging?

However one more thing fixed:

  • when I run st-flash let say - from scratch, I receive the fault on erase of first page (see log bellow)
  • when I flash using official tool, and then repeat it with st-flash - I receive the fault on erase of second page (as was reported)
  • it is the same with/without the correction in common_flash.c line 1170

The trials made under Ubuntu Mint 21.1
Actually, a final goal of me is to to use st-flash on a RaspPI. Later I will try the flashing there.
Therefor a suggestion:

  • Include in (debug) printout an info about OS version. And maybe any addition info about USB might be helpful?
$ sudo st-flash --debug --reset --freq=1000k --serial 004C00293331510833323639 write ~/play/EN_Silva.bin 0x8008000
st-flash 1.7.0-276-g800c861-dirty
2023-05-02T09:47:24 DEBUG common.c: *** looking up stlink version ***
2023-05-02T09:47:24 DEBUG common.c: st vid         = 0x0483 (expect 0x0483)
2023-05-02T09:47:24 DEBUG common.c: stlink pid     = 0x374f
2023-05-02T09:47:24 DEBUG common.c: stlink version = 0x3
2023-05-02T09:47:24 DEBUG common.c: jtag version   = 0xc
2023-05-02T09:47:24 DEBUG common.c: swim version   = 0x1
2023-05-02T09:47:24 DEBUG common.c: stlink current mode: mass
2023-05-02T09:47:24 DEBUG usb.c: JTAG/SWD freq set to 1000
2023-05-02T09:47:24 DEBUG common.c: stlink current mode: mass
2023-05-02T09:47:24 DEBUG common.c: *** stlink_enter_swd_mode ***
2023-05-02T09:47:24 DEBUG common.c: Loading device parameters....
2023-05-02T09:47:24 DEBUG common.c: *** stlink_core_id ***
2023-05-02T09:47:24 DEBUG common.c: core_id = 0x6ba02477
2023-05-02T09:47:24 DEBUG read_write.c: *** stlink_read_debug32 0x410fc241 at 0xe000ed00
2023-05-02T09:47:24 DEBUG read_write.c: *** stlink_read_debug32 0x10016497 at 0xe0042000
2023-05-02T09:47:24 DEBUG chipid.c: detected chip_id parameters

2023-05-02T09:47:24 DEBUG chipid.c: # Device Type: STM32WLEx
2023-05-02T09:47:24 DEBUG chipid.c: # Reference Manual: RM0033
2023-05-02T09:47:24 DEBUG chipid.c: #
2023-05-02T09:47:24 DEBUG chipid.c: chip_id 0x497
2023-05-02T09:47:24 DEBUG chipid.c: flash_type 11
2023-05-02T09:47:24 DEBUG chipid.c: flash_size_reg 0x1fff75e0
2023-05-02T09:47:24 DEBUG chipid.c: flash_pagesize 0x800
2023-05-02T09:47:24 DEBUG chipid.c: sram_size 0x10000
2023-05-02T09:47:24 DEBUG chipid.c: bootrom_base 0x1fff0000
2023-05-02T09:47:24 DEBUG chipid.c: bootrom_size 0x7000
2023-05-02T09:47:24 DEBUG chipid.c: option_base 0x1fff7800
2023-05-02T09:47:24 DEBUG chipid.c: option_size 0x10
2023-05-02T09:47:24 DEBUG chipid.c: flags 2

2023-05-02T09:47:24 DEBUG read_write.c: *** stlink_read_debug32 0xffff0100 at 0x1fff75e0
2023-05-02T09:47:24 INFO common.c: STM32WLEx: 64 KiB SRAM, 256 KiB flash in at least 2 KiB pages.
2023-05-02T09:47:24 DEBUG common.c: *** stlink_force_debug_mode ***
2023-05-02T09:47:24 DEBUG read_write.c: *** stlink_read_debug32 0x00001800 at 0xe004203c
2023-05-02T09:47:24 DEBUG read_write.c: *** stlink_write_debug32 0x00001800 to 0xe004203c
2023-05-02T09:47:24 DEBUG common.c: *** stlink_status ***
2023-05-02T09:47:24 DEBUG usb.c: core status: 01030003
2023-05-02T09:47:24 DEBUG common.c:   core status: halted
file /home/victor/play/EN_Silva.bin md5 checksum: d6dbf4ee312a850538c2dd92edb9e89, stlink checksum: 0x014d4616
2023-05-02T09:47:24 INFO common_flash.c: Attempting to write 211776 (0x33b40) bytes to stm32 address: 134250496 (0x8008000)
2023-05-02T09:47:24 DEBUG common.c: *** stlink_core_id ***
2023-05-02T09:47:24 DEBUG common.c: core_id = 0x6ba02477
2023-05-02T09:47:24 DEBUG read_write.c: *** stlink_read_debug32 0x00000080 at 0x58004010
2023-05-02T09:47:24 DEBUG read_write.c: *** stlink_write_debug32 0x000003f8 to 0x58004010
2023-05-02T09:47:24 DEBUG read_write.c: *** stlink_read_debug32 0x40000089 at 0x58004014
2023-05-02T09:47:24 DEBUG common_flash.c: Successfully unlocked flash
2023-05-02T09:47:24 DEBUG read_write.c: *** stlink_read_debug32 0x40000089 at 0x58004014
2023-05-02T09:47:24 DEBUG read_write.c: *** stlink_write_debug32 0x4000008b to 0x58004014
2023-05-02T09:47:24 DEBUG read_write.c: *** stlink_read_debug32 0x4000008b at 0x58004014
2023-05-02T09:47:24 DEBUG read_write.c: *** stlink_write_debug32 0x40000083 to 0x58004014
2023-05-02T09:47:24 DEBUG read_write.c: *** stlink_read_debug32 0x40000083 at 0x58004014
2023-05-02T09:47:24 DEBUG read_write.c: *** stlink_write_debug32 0x40010083 to 0x58004014
2023-05-02T09:47:24 DEBUG read_write.c: *** stlink_read_debug32 0x00000080 at 0x58004010
2023-05-02T09:47:24 DEBUG read_write.c: *** stlink_read_debug32 0x40000083 at 0x58004014
2023-05-02T09:47:24 DEBUG read_write.c: *** stlink_write_debug32 0x40000081 to 0x58004014
2023-05-02T09:47:24 DEBUG read_write.c: *** stlink_read_debug32 0x40000081 at 0x58004014
2023-05-02T09:47:24 DEBUG read_write.c: *** stlink_write_debug32 0x40000081 to 0x58004014
2023-05-02T09:47:24 DEBUG read_write.c: *** stlink_read_debug32 0x00000080 at 0x58004010
2023-05-02T09:47:24 ERROR common_flash.c: Flash programming error: 0x00000080
2023-05-02T09:47:24 WARN common_flash.c: Failed to erase_flash_page(0x8008000) == -1
2023-05-02T09:47:24 ERROR common_flash.c: Failed to erase the flash prior to writing
2023-05-02T09:47:24 DEBUG read_write.c: *** stlink_read_debug32 0xffffffff at 0x08008004
2023-05-02T09:47:24 DEBUG read_write.c: *** stlink_write_reg
2023-05-02T09:47:24 DEBUG common.c: *** stlink_run ***
2023-05-02T09:47:24 DEBUG read_write.c: *** stlink_read_reg
2023-05-02T09:47:24 DEBUG read_write.c:  (16) ***
2023-05-02T09:47:24 DEBUG common.c: data_len = 8 0x8
 80 00 00 00 03 00 00 61
2023-05-02T09:47:24 DEBUG usb.c: r_idx (16) = 0x61000003
stlink_fwrite_flash() == -1
2023-05-02T09:47:24 DEBUG common.c: *** stlink_exit_debug_mode ***
2023-05-02T09:47:24 DEBUG read_write.c: *** stlink_write_debug32 0xa05f0000 to 0xe000edf0
2023-05-02T09:47:24 DEBUG common.c: *** stlink_close ***

@Nightwalker-87
Copy link
Member

Actually, the OS info in detail is not too important, apart from the system type (windows, linux) because behaviour within different flavours of debian based distributions is usually very similar, if not the same. Also we do test the toolset on debian based systems regularly. More detailed USB debug info appears not to be trivial to implement from my point of view. Here we mostly rely on the external libusb library.

I'll check the log above in more detail soon.
For now I recommend to rollback the suggested change and recompile, in order to preserve the original state for further review.

@Nightwalker-87
Copy link
Member

related to #1310

@Nightwalker-87 Nightwalker-87 moved this from In progress to Review in progress in Release v1.8.0 May 16, 2023
@Ant-ON
Copy link
Collaborator

Ant-ON commented May 29, 2023

@victor-dryad Flash programming error: 0x00000080 is mean FLASH_SR = 0x00000080. Bits are set in the error:
Bit 6 SIZERR: Size error
Bit 4 WRPERR: Write protection error

Perhaps is write protection set?

@victor-dryad
Copy link
Author

victor-dryad commented Jun 2, 2023

Kind of blind research:
common_flash.c stlink_erase_flash_page, second else-if branch for STM32_FLASH_TYPE_WB_WL.
from line 1096 the code is replaced with additional logging and one hack:

      uint32_t flash_page =
          ((flashaddr - STM32_FLASH_BASE) / (uint32_t)(sl->flash_pgsz));
      stlink_read_debug32(sl, FLASH_WB_CR, &val);

      ILOG("\n\nstlink_erase_flash_page: \n");
      ILOG("stlink_read_debug32(sl, FLASH_WB_CR, &val) val=%#x\n", val);

      val &= ~(0xFF << 3); // Clear previously set page number (if any)
      val |= ((flash_page & 0xFF) << 3);
      uint32_t val1 = val;
      val = 0x40000002;

      ILOG("flash_page(%#x)=(flashaddr (%#x) - STM32_FLASH_BASE (%#x)) / (uint32_t)(sl->flash_pgsz) (%#x)\n",
           (flashaddr - STM32_FLASH_BASE) / (uint32_t)(sl->flash_pgsz), flashaddr, STM32_FLASH_BASE, (uint32_t)(sl->flash_pgsz));

      // sec 3.10.5 - PNB[7:0] is offset by 3.
      val &= ~(0xFF << 3); // Clear previously set page number (if any)

      ILOG("val(%#x) &= ~(0xFF << 3)\n", val);

      val |= ((flash_page & 0xFF) << 3);

      ILOG("val(%#x) |= ((flash_page & 0xFF) << 3)\n", val);

      ILOG("stlink_write_debug32(sl, FLASH_WB_CR (%#x), val(%#x instead of %#x))\n\n", FLASH_WB_CR, val, val1);

      stlink_write_debug32(sl, FLASH_WB_CR, val);

This hack solves the issue in my case.
The hack:
I set a value which was read first time, then a new value (read on next steps) is ignored.

st-flash 1.7.0-282-g5e85fd0-dirty
2023-06-02T14:15:38 INFO common.c: STM32WLEx: 64 KiB SRAM, 256 KiB flash in at least 2 KiB pages.
file binaries/bootloader_v0.2_sensor.bin md5 checksum: 7887f024efd120a350dd74f4b6d289aa, stlink checksum: 0x001d2262
2023-06-02T14:15:38 INFO common_flash.c: Attempting to write 18736 (0x4930) bytes to stm32 address: 134217728 (0x8000000)
2023-06-02T14:15:38 INFO common_flash.c: 

stlink_erase_flash_page: 
2023-06-02T14:15:38 INFO common_flash.c: stlink_read_debug32(sl, FLASH_WB_CR, &val) val=0x40000002
2023-06-02T14:15:38 INFO common_flash.c: flash_page(0)=(flashaddr (0x8000000) - STM32_FLASH_BASE (0x8000000)) / (uint32_t)(sl->flash_pgsz) (0x800)
2023-06-02T14:15:38 INFO common_flash.c: val(0x40000002) &= ~(0xFF << 3)
2023-06-02T14:15:38 INFO common_flash.c: val(0x40000002) |= ((flash_page & 0xFF) << 3)
2023-06-02T14:15:38 INFO common_flash.c: stlink_write_debug32(sl, FLASH_WB_CR (0x58004014), val(0x40000002 instead of 0x40000002))

-> Flash page at 0x8000000 erased (size: 0x800)
2023-06-02T14:15:38 INFO common_flash.c: 

stlink_erase_flash_page: 
2023-06-02T14:15:38 INFO common_flash.c: stlink_read_debug32(sl, FLASH_WB_CR, &val) val=0x40000003
2023-06-02T14:15:38 INFO common_flash.c: flash_page(0x1)=(flashaddr (0x8000800) - STM32_FLASH_BASE (0x8000000)) / (uint32_t)(sl->flash_pgsz) (0x800)
2023-06-02T14:15:38 INFO common_flash.c: val(0x40000002) &= ~(0xFF << 3)
2023-06-02T14:15:38 INFO common_flash.c: val(0x4000000a) |= ((flash_page & 0xFF) << 3)
2023-06-02T14:15:38 INFO common_flash.c: stlink_write_debug32(sl, FLASH_WB_CR (0x58004014), val(0x4000000a instead of 0x4000000b))

-> Flash page at 0x8000800 erased (size: 0x800)
2023-06-02T14:15:38 INFO common_flash.c: 

stlink_erase_flash_page: 
2023-06-02T14:15:38 INFO common_flash.c: stlink_read_debug32(sl, FLASH_WB_CR, &val) val=0x4000000b
2023-06-02T14:15:38 INFO common_flash.c: flash_page(0x2)=(flashaddr (0x8001000) - STM32_FLASH_BASE (0x8000000)) / (uint32_t)(sl->flash_pgsz) (0x800)
2023-06-02T14:15:38 INFO common_flash.c: val(0x40000002) &= ~(0xFF << 3)
2023-06-02T14:15:38 INFO common_flash.c: val(0x40000012) |= ((flash_page & 0xFF) << 3)
2023-06-02T14:15:38 INFO common_flash.c: stlink_write_debug32(sl, FLASH_WB_CR (0x58004014), val(0x40000012 instead of 0x40000013))

-> Flash page at 0x8001000 erased (size: 0x800)
2023-06-02T14:15:38 INFO common_flash.c: 

stlink_erase_flash_page: 
2023-06-02T14:15:38 INFO common_flash.c: stlink_read_debug32(sl, FLASH_WB_CR, &val) val=0x40000013
2023-06-02T14:15:38 INFO common_flash.c: flash_page(0x3)=(flashaddr (0x8001800) - STM32_FLASH_BASE (0x8000000)) / (uint32_t)(sl->flash_pgsz) (0x800)
2023-06-02T14:15:38 INFO common_flash.c: val(0x40000002) &= ~(0xFF << 3)
2023-06-02T14:15:38 INFO common_flash.c: val(0x4000001a) |= ((flash_page & 0xFF) << 3)
2023-06-02T14:15:38 INFO common_flash.c: stlink_write_debug32(sl, FLASH_WB_CR (0x58004014), val(0x4000001a instead of 0x4000001b))

-> Flash page at 0x8001800 erased (size: 0x800)
2023-06-02T14:15:38 INFO common_flash.c: 

stlink_erase_flash_page: 
2023-06-02T14:15:38 INFO common_flash.c: stlink_read_debug32(sl, FLASH_WB_CR, &val) val=0x4000001b
2023-06-02T14:15:38 INFO common_flash.c: flash_page(0x4)=(flashaddr (0x8002000) - STM32_FLASH_BASE (0x8000000)) / (uint32_t)(sl->flash_pgsz) (0x800)
2023-06-02T14:15:38 INFO common_flash.c: val(0x40000002) &= ~(0xFF << 3)
2023-06-02T14:15:38 INFO common_flash.c: val(0x40000022) |= ((flash_page & 0xFF) << 3)
2023-06-02T14:15:38 INFO common_flash.c: stlink_write_debug32(sl, FLASH_WB_CR (0x58004014), val(0x40000022 instead of 0x40000023))

-> Flash page at 0x8002000 erased (size: 0x800)
2023-06-02T14:15:38 INFO common_flash.c: 

stlink_erase_flash_page: 
2023-06-02T14:15:38 INFO common_flash.c: stlink_read_debug32(sl, FLASH_WB_CR, &val) val=0x40000023
2023-06-02T14:15:38 INFO common_flash.c: flash_page(0x5)=(flashaddr (0x8002800) - STM32_FLASH_BASE (0x8000000)) / (uint32_t)(sl->flash_pgsz) (0x800)
2023-06-02T14:15:38 INFO common_flash.c: val(0x40000002) &= ~(0xFF << 3)
2023-06-02T14:15:38 INFO common_flash.c: val(0x4000002a) |= ((flash_page & 0xFF) << 3)
2023-06-02T14:15:38 INFO common_flash.c: stlink_write_debug32(sl, FLASH_WB_CR (0x58004014), val(0x4000002a instead of 0x4000002b))

-> Flash page at 0x8002800 erased (size: 0x800)
2023-06-02T14:15:38 INFO common_flash.c: 

stlink_erase_flash_page: 
2023-06-02T14:15:38 INFO common_flash.c: stlink_read_debug32(sl, FLASH_WB_CR, &val) val=0x4000002b
2023-06-02T14:15:38 INFO common_flash.c: flash_page(0x6)=(flashaddr (0x8003000) - STM32_FLASH_BASE (0x8000000)) / (uint32_t)(sl->flash_pgsz) (0x800)
2023-06-02T14:15:38 INFO common_flash.c: val(0x40000002) &= ~(0xFF << 3)
2023-06-02T14:15:38 INFO common_flash.c: val(0x40000032) |= ((flash_page & 0xFF) << 3)
2023-06-02T14:15:38 INFO common_flash.c: stlink_write_debug32(sl, FLASH_WB_CR (0x58004014), val(0x40000032 instead of 0x40000033))

-> Flash page at 0x8003000 erased (size: 0x800)
2023-06-02T14:15:38 INFO common_flash.c: 

stlink_erase_flash_page: 
2023-06-02T14:15:38 INFO common_flash.c: stlink_read_debug32(sl, FLASH_WB_CR, &val) val=0x40000033
2023-06-02T14:15:38 INFO common_flash.c: flash_page(0x7)=(flashaddr (0x8003800) - STM32_FLASH_BASE (0x8000000)) / (uint32_t)(sl->flash_pgsz) (0x800)
2023-06-02T14:15:38 INFO common_flash.c: val(0x40000002) &= ~(0xFF << 3)
2023-06-02T14:15:38 INFO common_flash.c: val(0x4000003a) |= ((flash_page & 0xFF) << 3)
2023-06-02T14:15:38 INFO common_flash.c: stlink_write_debug32(sl, FLASH_WB_CR (0x58004014), val(0x4000003a instead of 0x4000003b))

-> Flash page at 0x8003800 erased (size: 0x800)
2023-06-02T14:15:38 INFO common_flash.c: 

stlink_erase_flash_page: 
2023-06-02T14:15:38 INFO common_flash.c: stlink_read_debug32(sl, FLASH_WB_CR, &val) val=0x4000003b
2023-06-02T14:15:38 INFO common_flash.c: flash_page(0x8)=(flashaddr (0x8004000) - STM32_FLASH_BASE (0x8000000)) / (uint32_t)(sl->flash_pgsz) (0x800)
2023-06-02T14:15:38 INFO common_flash.c: val(0x40000002) &= ~(0xFF << 3)
2023-06-02T14:15:38 INFO common_flash.c: val(0x40000042) |= ((flash_page & 0xFF) << 3)
2023-06-02T14:15:38 INFO common_flash.c: stlink_write_debug32(sl, FLASH_WB_CR (0x58004014), val(0x40000042 instead of 0x40000043))

-> Flash page at 0x8004000 erased (size: 0x800)
2023-06-02T14:15:38 INFO common_flash.c: 

stlink_erase_flash_page: 
2023-06-02T14:15:38 INFO common_flash.c: stlink_read_debug32(sl, FLASH_WB_CR, &val) val=0x40000043
2023-06-02T14:15:38 INFO common_flash.c: flash_page(0x9)=(flashaddr (0x8004800) - STM32_FLASH_BASE (0x8000000)) / (uint32_t)(sl->flash_pgsz) (0x800)
2023-06-02T14:15:38 INFO common_flash.c: val(0x40000002) &= ~(0xFF << 3)
2023-06-02T14:15:38 INFO common_flash.c: val(0x4000004a) |= ((flash_page & 0xFF) << 3)
2023-06-02T14:15:38 INFO common_flash.c: stlink_write_debug32(sl, FLASH_WB_CR (0x58004014), val(0x4000004a instead of 0x4000004b))

-> Flash page at 0x8004800 erased (size: 0x800)

2023-06-02T14:15:38 INFO flash_loader.c: Starting Flash write for WB/G0/G4/L5/U5
  9/  9 pages written
2023-06-02T14:15:40 INFO common_flash.c: Starting verification of write complete
2023-06-02T14:15:40 INFO common_flash.c: Flash written and verified! jolly good!

@Nightwalker-87
Copy link
Member

@victor-dryad When looking at the if-clause in L1071 in common_flash.c (current testing branch), we find a (very) similar procedure for STM32_FLASH_TYPE_G0, STM32_FLASH_TYPE_G4, STM32_FLASH_TYPE_WB_WL. Thus I assume all related boards should be affected respectively.

@victor-dryad
Copy link
Author

@victor-dryad When looking at the if-clause in L1071 in common_flash.c (current testing branch), we find a (very) similar procedure for STM32_FLASH_TYPE_G0, STM32_FLASH_TYPE_G4, STM32_FLASH_TYPE_WB_WL. Thus I assume all related boards should be affected respectively.

Talking about testing branch:
here is the else-if peace of code I mentioned:

} else if (sl->flash_type == STM32_FLASH_TYPE_WB_WL) {

As far as I can see,
the code for another boards differ.

@Nightwalker-87
Copy link
Member

Nightwalker-87 commented Jun 9, 2023

Well, they are similar - only the HEX-values differ.

@victor-dryad
Copy link
Author

Well, they are similar - only the HEX-values differ.

A way to create a mask is (may be almost) the same. the values differ.
Where can I read about these registers and meaning of bits to set/clear?

@Nightwalker-87
Copy link
Member

... the respective reference manuals? I'd try the one for the ST32WLE device you have at hand (section 3.10.5) and then extend research to STM32G491 or any G0 device (section 3.7.5), taking into account related sections of interest.

@Nightwalker-87
Copy link
Member

Nightwalker-87 commented Jun 19, 2023

@victor-dryad I still don't see the point that is actually leading to this. 😞
We apparently have a workaround, yes, but it's not what I'd add to the code, if we don't understand this behaviour...
@Ant-ON Any idea?

@Nightwalker-87
Copy link
Member

We need more contributors here with similar hardware for further investigation and testing.

@Nightwalker-87 Nightwalker-87 pinned this issue Jun 28, 2023
@Nightwalker-87
Copy link
Member

Nightwalker-87 commented Jun 28, 2023

@victor-dryad You may give it another try with the latest testing branch and without any custom patches from your side, though there were no specific fixes for this problem. However it may be useful to generate another output with a recent commit to see if there are any effects regarding the received output. Please make sure to run
sudo make uninstall && sudo make clean && sudo make install
to ensure a proper clean-up before additional testing.
It would be great, if the output could go to pastebin.com.

@Nightwalker-87
Copy link
Member

Nightwalker-87 commented Jul 20, 2023

2023-05-02T09:47:24 DEBUG chipid.c: flags 2

--> This is wrong: The WLE does not have a dual bank flash (per data sheet) and thus the related code necessary is not implemented. This option needs to be disabled in /config/chips/WLEx.chip, but I'm unsure yet, if it has an effect on this issue.

@BrentVerhaegen
Copy link

BrentVerhaegen commented Jul 22, 2023

Experiencing the same issue on a NUCLEO-WL55JC1 with STM32WL55 MCU
Pulled the latest develop branch and build stlink from there.

st-flash --reset --debug write build/nucleo-wl55jc1-test.bin 0x08000000
st-flash 1.7.0-296-gf7fb0a6-dirty
2023-07-22T21:05:22 DEBUG common.c: *** looking up stlink version ***
2023-07-22T21:05:22 DEBUG common.c: st vid         = 0x0483 (expect 0x0483)
2023-07-22T21:05:22 DEBUG common.c: stlink pid     = 0x374e
2023-07-22T21:05:22 DEBUG common.c: stlink version = 0x3
2023-07-22T21:05:22 DEBUG common.c: jtag version   = 0x7
2023-07-22T21:05:22 DEBUG common.c: swim version   = 0x0
2023-07-22T21:05:22 DEBUG common.c: stlink current mode: mass
2023-07-22T21:05:22 DEBUG usb.c: JTAG/SWD freq set to 0
2023-07-22T21:05:22 DEBUG common.c: stlink current mode: mass
2023-07-22T21:05:22 DEBUG common.c: *** stlink_enter_swd_mode ***
2023-07-22T21:05:22 DEBUG common.c: Loading device parameters....
2023-07-22T21:05:22 DEBUG common.c: *** stlink_core_id ***
2023-07-22T21:05:22 DEBUG common.c: core_id = 0x6ba02477
2023-07-22T21:05:22 DEBUG read_write.c: *** stlink_read_debug32  0x410fc241 at 0xe000ed00
2023-07-22T21:05:22 DEBUG read_write.c: *** stlink_read_debug32  0x10036497 at 0xe0042000
2023-07-22T21:05:22 DEBUG chipid.c: detected chip_id parameters

2023-07-22T21:05:22 DEBUG chipid.c: # Device Type: STM32WLEx
2023-07-22T21:05:22 DEBUG chipid.c: # Reference Manual: RM0033
2023-07-22T21:05:22 DEBUG chipid.c: #
2023-07-22T21:05:22 DEBUG chipid.c: chip_id 0x497
2023-07-22T21:05:22 DEBUG chipid.c: flash_type 11
2023-07-22T21:05:22 DEBUG chipid.c: flash_size_reg 0x1fff75e0
2023-07-22T21:05:22 DEBUG chipid.c: flash_pagesize 0x800
2023-07-22T21:05:22 DEBUG chipid.c: sram_size 0x10000
2023-07-22T21:05:22 DEBUG chipid.c: bootrom_base 0x1fff0000
2023-07-22T21:05:22 DEBUG chipid.c: bootrom_size 0x7000
2023-07-22T21:05:22 DEBUG chipid.c: option_base 0x1fff7800
2023-07-22T21:05:22 DEBUG chipid.c: option_size 0x10
2023-07-22T21:05:22 DEBUG chipid.c: flags 2

2023-07-22T21:05:22 DEBUG read_write.c: *** stlink_read_debug32  0xffff0100 at 0x1fff75e0
2023-07-22T21:05:22 INFO common.c: STM32WLEx: 64 KiB SRAM, 256 KiB flash in at least 2 KiB pages.
2023-07-22T21:05:22 DEBUG common.c: *** stlink_force_debug_mode ***
2023-07-22T21:05:22 DEBUG read_write.c: *** stlink_read_debug32  0x00001800 at 0xe004203c
2023-07-22T21:05:22 DEBUG read_write.c: *** stlink_write_debug32 0x00001800 to 0xe004203c
2023-07-22T21:05:22 DEBUG common.c: *** stlink_status ***
2023-07-22T21:05:22 DEBUG usb.c: core status: 01030003
2023-07-22T21:05:22 DEBUG common.c:   core status: halted
file build/nucleo-wl55jc1-test.bin md5 checksum: 34ce78ef829cb8d053b66c29660878f, stlink checksum: 0x0017b69e
2023-07-22T21:05:22 INFO common_flash.c: Attempting to write 14880 (0x3a20) bytes to stm32 address: 134217728 (0x8000000)
2023-07-22T21:05:22 DEBUG common.c: *** stlink_core_id ***
2023-07-22T21:05:22 DEBUG common.c: core_id = 0x6ba02477
2023-07-22T21:05:22 DEBUG read_write.c: *** stlink_read_debug32  0x00000080 at 0x58004010
2023-07-22T21:05:22 DEBUG read_write.c: *** stlink_write_debug32 0x000003f8 to 0x58004010
2023-07-22T21:05:22 DEBUG read_write.c: *** stlink_read_debug32  0x40000001 at 0x58004014
2023-07-22T21:05:22 DEBUG common_flash.c: Successfully unlocked flash
2023-07-22T21:05:22 DEBUG read_write.c: *** stlink_read_debug32  0x40000001 at 0x58004014
2023-07-22T21:05:22 DEBUG read_write.c: *** stlink_write_debug32 0x40000003 to 0x58004014
2023-07-22T21:05:22 DEBUG read_write.c: *** stlink_read_debug32  0x40000003 at 0x58004014
2023-07-22T21:05:22 DEBUG read_write.c: *** stlink_write_debug32 0x40000003 to 0x58004014
2023-07-22T21:05:22 DEBUG read_write.c: *** stlink_read_debug32  0x40000003 at 0x58004014
2023-07-22T21:05:22 DEBUG read_write.c: *** stlink_write_debug32 0x40010003 to 0x58004014
2023-07-22T21:05:22 DEBUG read_write.c: *** stlink_read_debug32  0x00000080 at 0x58004010
2023-07-22T21:05:22 DEBUG read_write.c: *** stlink_read_debug32  0x40000003 at 0x58004014
2023-07-22T21:05:22 DEBUG read_write.c: *** stlink_write_debug32 0x40000001 to 0x58004014
2023-07-22T21:05:22 DEBUG read_write.c: *** stlink_read_debug32  0x40000001 at 0x58004014
2023-07-22T21:05:22 DEBUG read_write.c: *** stlink_write_debug32 0x40000001 to 0x58004014
2023-07-22T21:05:22 DEBUG read_write.c: *** stlink_read_debug32  0x00000080 at 0x58004010
2023-07-22T21:05:22 ERROR common_flash.c: Flash programming error: 0x00000080
2023-07-22T21:05:22 WARN common_flash.c: Failed to erase_flash_page(0x8000000) == -1
2023-07-22T21:05:22 ERROR common_flash.c: Failed to erase the flash prior to writing
2023-07-22T21:05:22 DEBUG read_write.c: *** stlink_read_debug32  0xffffffff at 0x08000004
2023-07-22T21:05:22 DEBUG read_write.c: *** stlink_write_reg
2023-07-22T21:05:22 DEBUG common.c: *** stlink_run ***
2023-07-22T21:05:22 DEBUG read_write.c: *** stlink_read_reg
2023-07-22T21:05:22 DEBUG read_write.c:  (16) ***
2023-07-22T21:05:22 DEBUG common.c: data_len = 8 0x8
 80 00 00 00 03 00 00 01
2023-07-22T21:05:22 DEBUG usb.c: r_idx (16) = 0x01000003
stlink_fwrite_flash() == -1
2023-07-22T21:05:22 DEBUG common.c: *** stlink_exit_debug_mode ***
2023-07-22T21:05:22 DEBUG read_write.c: *** stlink_write_debug32 0xa05f0000 to 0xe000edf0
2023-07-22T21:05:22 DEBUG common.c: *** stlink_close ***

The data between 0x0800 0000 and 0x0800 0800 did get ereased succesfully. The data in other memory regions wasn't erased.

I've tried removing the dualbank flag in /config/chips/WLEx.chip as suggested by @Nightwalker-87 , but this didn't change anything.

Unfortunately I'm not capable to help debug this issue, but I'm more than happy to test or try any suggestions on the hardware that I have.

@Nightwalker-87 Nightwalker-87 moved this from Review in progress to In progress in Release v1.8.0 Sep 1, 2023
@Nightwalker-87
Copy link
Member

@victor-dryad I believe to have found a bug in common_flash.c L99ff.
The following block is obviously misplaced and belongs to the respective case for the H7 addressed earlier:

    if (sl->chip_flags & CHIP_F_HAS_DUAL_BANK) {
      cr2_reg = FLASH_H7_CR2;
    }
    cr_lock_shift = FLASH_H7_CR_LOCK;
    cr_mask = ~(1u << FLASH_H7_CR_SER)

@Nightwalker-87
Copy link
Member

Commit 7dcb130 fixes the obvious bug above.
Apart from this we are not able to verify or further investigate due to missing feedback and contribution.
Against this background I am closing this ticket now as resolved.
Please open a new ticket should the problem persist.

Release v1.8.0 automation moved this from Review in progress to Done Dec 24, 2023
@stlink-org stlink-org locked as resolved and limited conversation to collaborators Dec 24, 2023
@Nightwalker-87 Nightwalker-87 unpinned this issue Dec 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
No open projects
Development

No branches or pull requests

4 participants