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

initial l5 support #1247

Closed
wants to merge 12 commits into from
Closed

initial l5 support #1247

wants to merge 12 commits into from

Conversation

Evidlo
Copy link

@Evidlo Evidlo commented May 18, 2022

Made more progress. I looked for anywhere STM32_FLASH_TYPE_G4 was defined and made an equivalent section for STM32_FLASH_TYPE_L5. I crossed referenced all the G0/G4 definitions in stm32flash.h and the associated reference manual and found equivalent values in the L5 manual. I wasn't able to find where the value for STM32Gx_FLASH_SR_ERROR_MASK was taken from in the reference manual, so I just used the same value for L5.

Most of the registers/offsets in the reference manual are prefixed with "NS" (non-secure), which I've followed with my variable names. I don't know if other STM32's distinguish NS vs PD addresses in this way, but I can remove the NS prefix if we want to keep a simple naming convention.

@Evidlo
Copy link
Author

Evidlo commented May 18, 2022

I'm having trouble getting the flash to erase. st-flash is detecting nonzero bytes during the write step. I think the flash isn't being erased at all, because my blink program restarts as soon as I power-cycle the board despite the write step failing.

[evan@blackbox ~] /home/evan/resources/stlink/build/Release/bin/st-flash write /tmp/arduino_build_451074/blink.ino.bin 0x8000000
st-flash 1.7.0-186-gc4762e6-dirty
Failed to parse flash type or unrecognized flash type

detected chip_id parametres

# Device Type: STM32L552
# Reference Manual: RM0438
#
chip_id 0x472
flash_type 10
flash_size_reg 0xbfa05e0
flash_pagesize 0x1000
sram_size 0x40000
bootrom_base 0xbf90000
bootrom_size 0x8000
option_base 0x0
option_size 0x0
flags 0

2022-05-17T22:09:30 INFO common.c: STM32L552: 256 KiB SRAM, 512 KiB flash in at least 4 KiB pages.
file /tmp/arduino_build_451074/blink.ino.bin md5 checksum: 11eea395bf81feee318bcef406d42c, stlink checksum: 0x001e2964
2022-05-17T22:09:30 INFO common_flash.c: Attempting to write 20568 (0x5058) bytes to stm32 address: 134217728 (0x8000000)
-> Flash page at 0x8000000 erased (size: 0x1000)
-> Flash page at 0x8001000 erased (size: 0x1000)
-> Flash page at 0x8002000 erased (size: 0x1000)
-> Flash page at 0x8003000 erased (size: 0x1000)
-> Flash page at 0x8004000 erased (size: 0x1000)
-> Flash page at 0x8005000 erased (size: 0x1000)

2022-05-17T22:09:30 INFO flashloader.c: Starting Flash write for WB/L5/G0/G4
  4/  5 pages written
2022-05-17T22:09:34 ERROR common_flash.c: Flash memory contains a non-erased value
2022-05-17T22:09:34 ERROR common_flash.c: Flash programming error: 0x00000080
stlink_fwrite_flash() == -1

src/common_flash.c Outdated Show resolved Hide resolved
@Ant-ON
Copy link
Collaborator

Ant-ON commented May 18, 2022

You can also add page size reduction if DBANK (for 512K flash device and DB256K for 256K) is used. Example (if (sl->chip_flags & CHIP_F_HAS_DUAL_BANK && sl->flash_type == STM32_FLASH_TYPE_L5_U5) ...):

stlink/src/common.c

Lines 280 to 287 in 951859c

if (sl->chip_id == STM32_CHIPID_G4_CAT3) {
uint32_t flash_optr;
stlink_read_debug32(sl, STM32Gx_FLASH_OPTR, &flash_optr);
if (!(flash_optr & (1 << STM32G4_FLASH_OPTR_DBANK))) {
sl->flash_pgsz <<= 1;
}
}

And small fix page erase code

    } else if (sl->flash_type == STM32_FLASH_TYPE_L5_U5) {
      uint32_t flash_page;
      stlink_read_debug32(sl, STM32L5_FLASH_NSCR, &val);
      if (sl->flash_pgsz == 0x800 && offset >= sl->flash_size/2) {
        flash_page = (flashaddr - STM32_FLASH_BASE - sl->flash_size/2) / 
                                 (uint32_t)(sl->flash_pgsz));
        // set bank 2
        val |= (1 << STM32L5_FLASH_NSCR_NSBKER);
      } else {
        flash_page = (flashaddr - STM32_FLASH_BASE) / 
                                 (uint32_t)(sl->flash_pgsz));
        // clear bank selection / set bank 1
        val &= ~(1 << STM32L5_FLASH_NSCR_NSBKER);
      }

      // sec 6.9.9
      val &= ~(0x7F << 3);
      val |= ((flash_page & 0x7F) << 3) | (1 << STM32L5_FLASH_NSCR_NSPER);
      stlink_write_debug32(sl, STM32L5_FLASH_NSCR, val);
    }

inc/stm32flash.h Outdated Show resolved Hide resolved
inc/stm32flash.h Outdated Show resolved Hide resolved
@Ant-ON
Copy link
Collaborator

Ant-ON commented May 18, 2022

@Evidlo RM0438 pg. 183 have note:

At power-on reset or a system reset, the main regulator voltage range 2 is selected by
default. Consequently, the voltage scaling range must be programmed to range 0 or range 1
via VOS[1:0] bits in the PWR_CR1 register prior to any Flash erase and programming
operation.

I would add the following code to the start of the unlock_flash_if function

#define STM32L5_PWR_CR1 0x40007000
#define STM32L5_PWR_CR1_VOS 9 /* Voltage scaling range selection */

...
  if (sl->flash_type == STM32_FLASH_TYPE_L5_U5) {
    // Set the voltage scaling range to the range 0 to perform flash operations
    // RM0438 pg. 183
   uint32_t mask = (0x3 << STM32L5_PWR_CR1_VOS);
    stlink_read_debug32(sl, STM32L5_PWR_CR1, &val);
    if (val & mask) {
      val &= ~mask;
      stlink_write_debug32(sl, STM32L5_PWR_CR1, val);
    }
  }

@Evidlo
Copy link
Author

Evidlo commented May 18, 2022

@Ant-ON

Why bother reading the value of the VOS bits? Why not just zero them out every time?

@Ant-ON
Copy link
Collaborator

Ant-ON commented May 19, 2022

@Evidlo You are right. It looks somehow without strong logic. It is necessary to write the condition differently (I made a mistake, sorry) so the idea becomes clear:

...
if ((val & mask) > (0x1 << STM32L5_PWR_CR1_VOS)) {
...

I wanted to make as few changes to the system as possible. st-flash has the ability to connect without reset (--hot-plug option)

ps Have you checked flash the firmware? Did it work?

@Nightwalker-87
Copy link
Member

@Evidlo: Please correct the detected errors. Can we also draw this to a close?

@Evidlo
Copy link
Author

Evidlo commented Jun 5, 2022

I've made the changes, but erasing still doesn't work and there are nonzero values in the flash:

[evan@blackbox src] /home/evan/resources/stlink/build/Release/bin/st-flash write /tmp/arduino_build_678130/blink.ino.bin 0x8000000
st-flash 1.7.0-193-ga23ef65-dirty
Failed to parse flash type or unrecognized flash type UNKNOWN from file /usr/local/stlink/chips/unknown_device.chip

detected chip_id parametres

# Device Type: STM32L552
# Reference Manual: RM0438
#
chip_id 0x472
flash_type 10
flash_size_reg 0xbfa05e0
flash_pagesize 0x1000
sram_size 0x40000
bootrom_base 0xbf90000
bootrom_size 0x8000
option_base 0x0
option_size 0x0
flags 0

2022-06-04T21:39:05 INFO common.c: STM32L552: 256 KiB SRAM, 512 KiB flash in at least 4 KiB pages.
file /tmp/arduino_build_678130/blink.ino.bin md5 checksum: 1888c7fea96a4b962dc1fb93fda4aaf, stlink checksum: 0x001e34a6
2022-06-04T21:39:05 INFO common_flash.c: Attempting to write 20584 (0x5068) bytes to stm32 address: 134217728 (0x8000000)
-> Flash page at 0x8000000 erased (size: 0x1000)
-> Flash page at 0x8001000 erased (size: 0x1000)
-> Flash page at 0x8002000 erased (size: 0x1000)
-> Flash page at 0x8003000 erased (size: 0x1000)
-> Flash page at 0x8004000 erased (size: 0x1000)
-> Flash page at 0x8005000 erased (size: 0x1000)

2022-06-04T21:39:05 INFO flashloader.c: Starting Flash write for WB/L5/G0/G4
  4/  5 pages written
2022-06-04T21:39:09 ERROR common_flash.c: Flash memory contains a non-erased value
2022-06-04T21:39:09 ERROR common_flash.c: Flash programming error: 0x00000080
stlink_fwrite_flash() == -1

@Nightwalker-87
Copy link
Member

Nightwalker-87 commented Jun 5, 2022

@Evidlo: Much better now, but some issue on macos remains as can be seen below.
It's only a -Werror,-Wshorten-64-to-32 so it should not be too complicated to resolve this as well, I think.

inc/stm32flash.h Outdated Show resolved Hide resolved
@Ant-ON
Copy link
Collaborator

Ant-ON commented Jun 14, 2022

@Evidlo I seem to have found the problem. Can you fix and try flash?

@Evidlo
Copy link
Author

Evidlo commented Jun 15, 2022

@Ant-ON, I've made the changes, but I'm getting an identical error message.

Also an unrelated problem is that the --reset argument doesn't seem to work, but running the reset command separately before writing does work.

--reset argument
[evan@blackbox ~] ~/resources/stlink/build/Release/bin/st-flash --reset write ~/resources/eduplsb/fw/blink.ino.bin 0x8000000
st-flash 1.7.0-197-g3d67d23
Failed to parse flash type or unrecognized flash type UNKNOWN from file /usr/local/stlink/chips/unknown_device.chip

detected chip_id parametres

# Device Type: STM32L552
# Reference Manual: RM0438
#
chip_id 0x472
flash_type 10
flash_size_reg 0xbfa05e0
flash_pagesize 0x1000
sram_size 0x40000
bootrom_base 0xbf90000
bootrom_size 0x8000
option_base 0x0
option_size 0x0
flags 0

2022-06-15T16:26:35 INFO common.c: STM32L552: 256 KiB SRAM, 512 KiB flash in at least 4 KiB pages.
file /home/evan/resources/eduplsb/fw/blink.ino.bin md5 checksum: 1888c7fea96a4b962dc1fb93fda4aaf, stlink checksum: 0x001e34a6
2022-06-15T16:26:35 INFO common_flash.c: Attempting to write 20584 (0x5068) bytes to stm32 address: 134217728 (0x8000000)
2022-06-15T16:26:35 ERROR common_flash.c: Flash programming error: 0x00000080
2022-06-15T16:26:35 WARN common_flash.c: Failed to erase_flash_page(0x8000000) == -1
2022-06-15T16:26:35 ERROR common_flash.c: Failed to erase the flash prior to writing
stlink_fwrite_flash() == -1
reset command
[evan@blackbox ~] ~/resources/stlink/build/Release/bin/st-flash reset
st-flash 1.7.0-197-g3d67d23
Failed to parse flash type or unrecognized flash type UNKNOWN from file /usr/local/stlink/chips/unknown_device.chip

detected chip_id parametres

# Device Type: STM32L552
# Reference Manual: RM0438
#
chip_id 0x472
flash_type 10
flash_size_reg 0xbfa05e0
flash_pagesize 0x1000
sram_size 0x40000
bootrom_base 0xbf90000
bootrom_size 0x8000
option_base 0x0
option_size 0x0
flags 0

2022-06-15T16:24:29 INFO common.c: STM32L552: 256 KiB SRAM, 512 KiB flash in at least 4 KiB pages.
2022-06-15T16:24:29 WARN common.c: NRST is not connected
[evan@blackbox ~] ~/resources/stlink/build/Release/bin/st-flash write ~/resources/eduplsb/fw/blink.ino.bin 0x8000000
st-flash 1.7.0-197-g3d67d23
Failed to parse flash type or unrecognized flash type UNKNOWN from file /usr/local/stlink/chips/unknown_device.chip

detected chip_id parametres

# Device Type: STM32L552
# Reference Manual: RM0438
#
chip_id 0x472
flash_type 10
flash_size_reg 0xbfa05e0
flash_pagesize 0x1000
sram_size 0x40000
bootrom_base 0xbf90000
bootrom_size 0x8000
option_base 0x0
option_size 0x0
flags 0

2022-06-15T16:24:33 INFO common.c: STM32L552: 256 KiB SRAM, 512 KiB flash in at least 4 KiB pages.
file /home/evan/resources/eduplsb/fw/blink.ino.bin md5 checksum: 1888c7fea96a4b962dc1fb93fda4aaf, stlink checksum: 0x001e34a6
2022-06-15T16:24:33 INFO common_flash.c: Attempting to write 20584 (0x5068) bytes to stm32 address: 134217728 (0x8000000)
-> Flash page at 0x8000000 erased (size: 0x1000)
-> Flash page at 0x8001000 erased (size: 0x1000)
-> Flash page at 0x8002000 erased (size: 0x1000)
-> Flash page at 0x8003000 erased (size: 0x1000)
-> Flash page at 0x8004000 erased (size: 0x1000)
-> Flash page at 0x8005000 erased (size: 0x1000)

2022-06-15T16:24:34 INFO flashloader.c: Starting Flash write for WB/L5/G0/G4
  5/  5 pages written
2022-06-15T16:24:38 ERROR common_flash.c: Flash memory contains a non-erased value
2022-06-15T16:24:38 ERROR common_flash.c: Flash programming error: 0x00000080
stlink_fwrite_flash() == -1

@Evidlo
Copy link
Author

Evidlo commented Jun 15, 2022

@Ant-ON If you'd like it, I can also give you guest access to a machine that has the board connected if you give me your ssh pubkey.

@Nightwalker-87
Copy link
Member

@Evidlo What is the current state here? Is there any reasonable perspective to draw this topic to a close?

@Evidlo
Copy link
Author

Evidlo commented Jun 27, 2022

@Nightwalker-87 Flashing is still not working.

@Evidlo
Copy link
Author

Evidlo commented Aug 26, 2022

@Ant-ON Any additional ideas for how to debug this?

@Ant-ON
Copy link
Collaborator

Ant-ON commented Aug 29, 2022

@Evidlo For starters, I think it's worth checking the memory erasing process. To do this, we will erase the memory and read its content:

st-flash erase 0x8000000 0x6000
st-flash read ./temp.bin 0x8000000 0x6000

If the erasing process works correctly, then the temp.bin file will have filled by 0xFF bytes

@Ant-ON
Copy link
Collaborator

Ant-ON commented Aug 29, 2022

@Evidlo To fix the reset after the flashing, you need to change the reset code in the src/st-flash/flash.c file to

    if (o.reset) {
        stlink_reset(sl, RESET_AUTO);
        stlink_run(sl, RUN_NORMAL);
    }

Copy link
Member

@Nightwalker-87 Nightwalker-87 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please finally address the mentioned topic(s) from the discussion.

@Nightwalker-87 Nightwalker-87 removed their request for review October 15, 2022 19:29
@Nightwalker-87
Copy link
Member

This attempt failed to come to an end within a reasonable timespan.
Accordingly this PR is finally closed.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants