Author Topic: ST 32F417 veneer code (or something quite different)  (Read 4456 times)

0 Members and 1 Guest are viewing this topic.

Offline peter-hTopic starter

  • Super Contributor
  • ***
  • Posts: 6173
  • Country: gb
  • Doing electronics since the 1960s...
ST 32F417 veneer code (or something quite different)
« on: August 20, 2022, 09:57:35 am »
I am having some weird problems where I am calling a function in a boot block (bottom 32k i.e. 0x8000000 to 0x8008000) from a boot loader that's been relocated into RAM at these addresses



Sometimes it runs, sometimes it just gets skipped. The function calls do nothing. I even see this when stepping in Cube, although if I step in the disassembly mode then all the code is correctly stepped. Weird!

I can see what the veneer is e.g.
https://developer.arm.com/documentation/dui0489/i/arm-and-thumb-instructions/bl

but how can it fail? Some interesting comments here
https://stackoverflow.com/questions/39477993/arm-cortex-a7-avoid-memory-veneers

and it is what I am seeing. Literally, the code being called with the "long call" is as if it wasn't getting called.

And I can't move the functions being called to the boot loader because it would make it too big.

I discovered this only when doing some testing, uploading new firmware using my http server and then using a reboot function there, which calls this well known code

Code: [Select]
static inline void L_reboot(void)
{
// Ensure all outstanding memory accesses including buffered write are completed before reset
__ASM volatile ("dsb 0xF":::"memory");
// Keep priority group unchanged
SCB->AIRCR  = (uint32_t)((0x5FAUL << SCB_AIRCR_VECTKEY_Pos) |
                           (SCB->AIRCR & SCB_AIRCR_PRIGROUP_Msk) |
                            SCB_AIRCR_SYSRESETREQ_Msk    );
__ASM volatile ("dsb 0xF":::"memory");
// wait until reset
for(;;)
{
__NOP();
}
}

The issue happens very reliably after the board is restarted with the above, but not if it is restarted with debugger (presumably that does a hardware reset). I don't know if that is any kind of clue.

Lots of stuff is simply broken, like the functions for turning a LED on/off (see above screenshot) which are just GPIO stuff. The LED is not changing. But the code is there, in the disassembly.

Thank you for any ideas. Spent a day on this already, and I am down to watching GPIO pulses with a scope, and (mostly) can't see any. Basically long calls to functions are getting skipped, but otherwise the code is not crashing.
« Last Edit: August 21, 2022, 08:54:39 pm by peter-h »
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 

Offline wek

  • Frequent Contributor
  • **
  • Posts: 591
  • Country: sk
Re: ST 32F417 veneer code
« Reply #1 on: August 20, 2022, 10:09:33 am »
Quote
I am calling a function in a boot block (bottom 32k i.e. 0x8000000 to 0x8008000)

You did not show us calls to 0x0800'xxxx, but calls to RAM (bl 0x2001f688).

JW
 

Offline peter-hTopic starter

  • Super Contributor
  • ***
  • Posts: 6173
  • Country: gb
  • Doing electronics since the 1960s...
Re: ST 32F417 veneer code
« Reply #2 on: August 20, 2022, 10:33:00 am »
It turns out that I have been down this rabbit hole a while ago, but in the other direction: calling a RAM-resident loader from the boot block



I can't see addresses in the calls to FLASH. The disassembly of the veneers looks like this



If I step through it I can see the function in FLASH:



I tried this format of long_call (this function is in the boot block) but looking at the disassembly it does not prevent it getting a veneer. Maybe the attribute syntax is wrong?



The problem is that if long calls were failing, this doesn't explain why this code has been running for over a year, with the loader calling lots of boot block functions...

Could there be something in the CPU which makes it support or not support long calls, according to how it has been reset?

After much trial and error and trying out various code samples on e.g. stackexchange (which were obviously never tested, or were for some other compiler) I found that the way to get rid of veneers is to put the long_call attribute in the prototypes (in the .h file):

« Last Edit: August 20, 2022, 10:58:35 am by peter-h »
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 

Offline wek

  • Frequent Contributor
  • **
  • Posts: 591
  • Country: sk
Re: ST 32F417 veneer code
« Reply #3 on: August 20, 2022, 11:07:07 am »
ldr.w pc,[pc] picks the word immediately after this instruction and stores it into PC, in other words, jumps to address given by that word. So, the two "instructions" after ldr.w pc,[pc] are not instructions, and if you'd look at the values at that place in RAM, you'd see the target address.

IMO you are after a red herring and your problem is elsewhere. Try to describe the symptoms without making premature conclusions.

JW
 

Offline peter-hTopic starter

  • Super Contributor
  • ***
  • Posts: 6173
  • Country: gb
  • Doing electronics since the 1960s...
Re: ST 32F417 veneer code
« Reply #4 on: August 20, 2022, 11:27:19 am »
Indeed; removing the veneers did not solve the problem.

It is a mystery; some calls from the ram resident boot loader are just not taking place, although if I step them in the debugger in the disassembly mode, they happen ok.

Statistically, the problem depends on how the CPU was reset.

I don't know how to troubleshoot this.
« Last Edit: August 20, 2022, 11:43:49 am by peter-h »
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 

Offline wek

  • Frequent Contributor
  • **
  • Posts: 591
  • Country: sk
Re: ST 32F417 veneer code
« Reply #5 on: August 20, 2022, 12:01:16 pm »
> It is a mystery; some calls from the ram resident boot loader are just not taking place

That's already a conclusion which may be false; the reason for what you observe may be something else.

> Statistically, the problem depends on how the CPU was reset.

That may influence what's mapped at address 0, among other things. What's the value of VTOR register, btw.?

> I don't know how to troubleshoot this.

As in any other case: observe behaviour, formulate hypotheses, construct experiments to prove or disprove them.

JW
 

Offline peter-hTopic starter

  • Super Contributor
  • ***
  • Posts: 6173
  • Country: gb
  • Doing electronics since the 1960s...
Re: ST 32F417 veneer code
« Reply #6 on: August 20, 2022, 12:46:50 pm »
At this point, VTOR is not at the default base; it is at 0x200 (512-aligned as required) above the boot block i.e. at 0x8008200. But should VTOR matter? Interrupts, RTOS are disabled.

I did actually wonder whether I have interrupts enabled. They should not be because this stuff is running after a reset (a reset carried out with the code above, and a reset should reset VTOR, disable interrupts, etc). But maybe that software reset doesn't?

I have added
Code: [Select]
// This should not be needed
RCC->CIR = 0x00000000; // disable interrupts
SCB->VTOR = FLASH_BASE; // set VT location to default
to the software reset, and before the entry to the boot loader.

But it makes no difference. It does change the nature of how it hangs. Instead of crashing with an invalid opcode (which is directly the result of a function which writes to the FLASH not getting called) it hangs in a delay routine which uses CYCCNT. It now looks like the CPU is running very very slowly.
« Last Edit: August 20, 2022, 01:12:45 pm by peter-h »
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 

Offline wek

  • Frequent Contributor
  • **
  • Posts: 591
  • Country: sk
Re: ST 32F417 veneer code
« Reply #7 on: August 20, 2022, 01:19:10 pm »
> RCC->CIR = 0x00000000;      // disable interrupts

This disables only the particular interrupts generated by RCC, is this what you intended.

Nonetheless, this does not matter as software reset resets among other things NVIC so effectively disables all interrupts which can be disabled. It also resets VTOR so it does not matter how do you set it before software reset.

Software reset is as good as "pushbutton" reset, both cause so called system reset (read first part of RCC chapter). Only a select few things are not affected by this reset - the backup domain (which is not germane here probably), some status bits in RCC, some sleep-related bits, some (most?) of the Debug stuff. These (except backup domain) are reset by power-up reset only.

SYSCFG_MEMRMP.MEM_MODE (i.e. mapping of memory at address 0) *is* affected, but it has its own rules (a.k.a. bootloader entry, AN2606 etc. - read Boot configuration subchapter).

The debugger may do whatever it is pleased to do, and rarely is it concisely documented.

> It now looks like the CPU is running very very slowly.

Then investigate this. Output sysclk to MCO. Check RCC settings (this means "read out the RCC registers content and check", not "look at CubeMX's clock tree picture"). If you've clicked clock setup in CubeMX (because it's so easy and fast), make sure you've handled every case where it returns with failure.

JW
« Last Edit: August 20, 2022, 01:23:04 pm by wek »
 

Offline peter-hTopic starter

  • Super Contributor
  • ***
  • Posts: 6173
  • Country: gb
  • Doing electronics since the 1960s...
Re: ST 32F417 veneer code
« Reply #8 on: August 20, 2022, 09:29:47 pm »
The slow clock was not the case... It is ok now. I am not using MX. A lot of code was originally generated with MX (by the other guy who was doing it part-time) but not much of that left now. Also I rarely use HAL code myself.

Am I right that the veneer code is always ok, with no side effects or issues? I am pretty damn sure I had to use the long call attribute on that long call/jump to the boot loader. I spent ages on that one.

I still have the issue that sometimes it all runs perfectly while at other times, and always after that software reset, it doesn't. The reason it doesn't is because a particular function (in the boot block, but called from the boot loader) which does some data manipulation, is simply not executed. So the data written to the CPU FLASH is garbage, 100% consistent garbage... It is exactly as if that function call was commented out.
« Last Edit: August 20, 2022, 09:32:01 pm by peter-h »
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 

Offline peter-hTopic starter

  • Super Contributor
  • ***
  • Posts: 6173
  • Country: gb
  • Doing electronics since the 1960s...
Re: ST 32F417 veneer code
« Reply #9 on: August 21, 2022, 01:53:21 pm »
After many more hours I am finding that the soft reset definitely does not work like a hard reset.

It is hard to narrow down the way it differs though. One manifestation is that SPI2 is not running properly; wrong data is fetched from a serial FLASH. That could be all kinds of things of course.

I looked up SYSCFG_MEMRMP.MEM_MODE but I am not using ST's boot loader. BOOT0 & BOOT1 are both 0.

Maybe I should have implemented a spare GPIO to drive NRST (with suitable safeguards of course, but GPIOs are supposed to power up as inputs so this should work). Unfortunately it needs >20us so would need a hardware one-shot.

Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 

Offline langwadt

  • Super Contributor
  • ***
  • Posts: 5855
  • Country: dk
Re: ST 32F417 veneer code
« Reply #10 on: August 21, 2022, 04:51:26 pm »
After many more hours I am finding that the soft reset definitely does not work like a hard reset.

It is hard to narrow down the way it differs though. One manifestation is that SPI2 is not running properly; wrong data is fetched from a serial FLASH. That could be all kinds of things of course.

I looked up SYSCFG_MEMRMP.MEM_MODE but I am not using ST's boot loader. BOOT0 & BOOT1 are both 0.

Maybe I should have implemented a spare GPIO to drive NRST (with suitable safeguards of course, but GPIOs are supposed to power up as inputs so this should work). Unfortunately it needs >20us so would need a hardware one-shot.

is the flash also connected to HW reset, so it is actually the flash that needs a reset?

instead of doing SW reset you try to use could use the watchdog



 

Offline wek

  • Frequent Contributor
  • **
  • Posts: 591
  • Country: sk
Re: ST 32F417 veneer code
« Reply #11 on: August 21, 2022, 05:58:48 pm »
What do you mean by hardware reset, exactly?

Software reset - and WD reset, too - generates a pulse on NRST, that's why it's in effect identical to pushbutton reset. Except if you prevent that pulse  e.g. by using a push-pull output reset generator to NRST.

Power-on reset (generated by the internal POR circuit) is different, I discussed that already above.

JW
« Last Edit: August 21, 2022, 06:00:33 pm by wek »
 

Offline peter-hTopic starter

  • Super Contributor
  • ***
  • Posts: 6173
  • Country: gb
  • Doing electronics since the 1960s...
Re: ST 32F417 veneer code
« Reply #12 on: August 21, 2022, 08:15:18 pm »
This is turning out to be something weird.

The issue is between

a) code loaded into the target via the STLINK V2/V3 debugger
b) code loaded into the target via a firmware update feature

And it is not the obvious thing (item b) being duff). The firmware update route has been exhaustively tested over more than a year, but not since Cube was updated to v10, and the GCC tools updated with it.

This is the post build batch file I am using, to generate the firmware update file:

Code: [Select]
arm-none-eabi-objcopy -S --strip-all -R .bss -R .main_heap -R .ccmram -O binary yyy.elf zzz.dat

I know with 100% certainty that zzz.dat used to be identical to the content of the CPU FLASH. There is/was ~350k of code running fine.

But now, not quite, and this is why it took so many hours to home in on it. The following examples are: cpu flash and then the file

It starts off nicely




and starts to break around here (line 1, halfway along)




In terms of the resulting code, it is still about 80% there. Bloody confusing...

Does anyone have any ideas?

I haven't done a "diff" properly because I don't actually have a file of the CPU FLASH. Yes, I could write a bit of code on my board and write one out. But fairly clearly the first difference is at offset 488 hex which is 110 decimal.

The flash size (before the FFs start) is 377964 whereas the above zzz.dat file is 372748.

I have no idea of what command Cube is using to drive the debugger but I see this

Code: [Select]
make all
arm-none-eabi-size   KDE485.elf
   text    data     bss     dec     hex filename
 354696   69480   57584 481760   759e0 yyy.elf
Finished building: default.size.stdout

The reset issue may still be there, but all the time I have different data in the firmware-update file (which is where the problem is confined to) I don't have a hope of a working system, obviously.

Quote
Software reset - and WD reset, too - generates a pulse on NRST, that's why it's in effect identical to pushbutton reset. Except if you prevent that pulse  e.g. by using a push-pull output reset generator to NRST.

That's useful to know!

UPDATE: I wrote out a file of the actual CPU FLASH (the board has a 2MB FatFS filesystem) and treating that as a firmware update does work!

So what has changed in the bloody GCC objcopy, making the above command line produce a small % of garbage?

Code: [Select]
arm-none-eabi-objcopy -S --strip-all -R .bss -R .main_heap -R .ccmram -O binary out.elf out.dat
I can extract a firmware update file out of a running target by writing out the CPU FLASH into a file, but it is a pretty weird way to go about it.
« Last Edit: August 22, 2022, 06:33:19 am by peter-h »
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 

Offline wek

  • Frequent Contributor
  • **
  • Posts: 591
  • Country: sk
Re: ST 32F417 veneer code (or something quite different)
« Reply #13 on: August 22, 2022, 06:50:09 am »
Look at the mapfile, what's around the address in question.

JW
 

Offline peter-hTopic starter

  • Super Contributor
  • ***
  • Posts: 6173
  • Country: gb
  • Doing electronics since the 1960s...
Re: ST 32F417 veneer code (or something quite different)
« Reply #14 on: August 22, 2022, 08:09:06 am »
At exactly 0x488 is this



which is this



The question is what could have changed? The batch file is invoking the GCC executable which is in the project path, set up by Cube. It isn't some old version which might be incompatible with a new compiler.

I have written code to readily generate the firmware upgrade file on the target, which works 100%, but it is a strange way to go about it. It would be useful to find what command Cube uses to produce the binary which goes to the debugger, but I can't see the command anywhere.

Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 

Offline wek

  • Frequent Contributor
  • **
  • Posts: 591
  • Country: sk
Re: ST 32F417 veneer code (or something quite different)
« Reply #15 on: August 22, 2022, 08:26:07 am »
Are you sure the binary comes from the same elf the mapfile comes from?
Try to disassemble the elf using objdump -h -S

> It would be useful to find what command Cube uses to produce the binary which goes to the debugger, but I can't see the command anywhere.

Debugger (gdb) uses elf.

JW
 

Offline peter-hTopic starter

  • Super Contributor
  • ***
  • Posts: 6173
  • Country: gb
  • Doing electronics since the 1960s...
Re: ST 32F417 veneer code (or something quite different)
« Reply #16 on: August 22, 2022, 09:44:02 am »
The two files actually agree; at 0x488 is the _init_switches function.

This is the -S dump



The get_hex_switch is a misread; that function is the next one below, at 0x4c4.

Quote
Debugger (gdb) uses elf.

I am sure it does, since .elf is the only output file, but the .elf is 4.6MB, so 4.3MB is somehow stripped out on the way to the CPU FLASH. After various discussions here a long time ago I had been successfully using the command

Code: [Select]
arm-none-eabi-objcopy -S --strip-all -R .bss -R .main_heap -R .ccmram -O binary out.elf out.dat
but bizzarely this no longer works. It still produces a file of very close to the right size.





« Last Edit: August 22, 2022, 09:47:57 am by peter-h »
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 

Offline wek

  • Frequent Contributor
  • **
  • Posts: 591
  • Country: sk
Re: ST 32F417 veneer code (or something quite different)
« Reply #17 on: August 22, 2022, 10:54:14 am »
Quote
Quote
Debugger (gdb) uses elf.
I am sure it does, since .elf is the only output file, but the .elf is 4.6MB, so 4.3MB is somehow stripped out on the way to the CPU FLASH.
That's mostly debug information (dwarf).

If the binary matches objdump, then you objcopy/objdump from a different .elf than you debug/program from.

JW
 

Offline peter-hTopic starter

  • Super Contributor
  • ***
  • Posts: 6173
  • Country: gb
  • Doing electronics since the 1960s...
Re: ST 32F417 veneer code (or something quite different)
« Reply #18 on: August 22, 2022, 01:04:56 pm »
Sure, plus Cube runs this

Code: [Select]
Finished building target: xxx.elf
 
arm-none-eabi-size   xxx.elf
   text    data     bss     dec     hex filename
 352540   69480   57584 479604   75174 xxx.elf
Finished building: default.size.stdout
 
arm-none-eabi-objdump -h -S  xxx.elf  > "xxx.list"
Finished building: xxx.list
 
arm-none-eabi-objcopy  -O binary  xxx.elf  "xxx.bin"
Finished building: xxx.bin
 

where the xxx.bin (131MB) doesn't appear to get used for anything. The .list is a disassembly listing, as you mentioned.

I did indeed have an orphaned .elf file, from about a month ago, caused by me having renamed the project, and the batch process producing the firmware update was picking up the old .elf file.

But that doesn't explain the corrupted data. That orphaned .elf would have still contained running code. Especially in the boot loader which I had not touched for ages and where the corruption starts. That corrupted code could not have been produced by any compiler+linker.

All I can think is that an updated objcopy.exe was not compatible with an older .elf.

Thank you for your help :)

Regarding the veneers, I won't go over this again (takes hours of testing) but I spent some hours a year ago on how to transfer control to the boot loader (which is in RAM) and there was certainly no way to get a veneer or anything else to work automatically. The long-call attribute was the only way I found. Yes, some function calls in the boot loader have been apparently successfully calling functions in the boot block without the long-call but I don't think the debugger handles stepping into these correctly. Only in assembler stepping mode does that work right. So I have given all boot block functions (which for obvious reasons are exported only to the boot loader, not to the rest of the product which may get updated without the boot loader getting updated) the long-call attrib in their prototypes.

This stuff is all easy once you know it, and it takes years to learn it.


« Last Edit: August 22, 2022, 01:18:09 pm by peter-h »
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 

Offline wek

  • Frequent Contributor
  • **
  • Posts: 591
  • Country: sk
Re: ST 32F417 veneer code (or something quite different)
« Reply #19 on: August 22, 2022, 01:24:17 pm »
An easy and quick test is to delete the elf from which you generate the binary. Recompilation will regenerate it.

[EDIT] There may be another thing: if the bootloader loads the binary from the serial FLASH into the mcu FLASH, then regardless of how do you overwrite it in mcu FLASH, it gets reloaded from the serial FLASH once the bootloader runs. Timestamps/CRC inside the binary would reveal the difference.

JW
« Last Edit: August 22, 2022, 01:28:14 pm by wek »
 

Offline peter-hTopic starter

  • Super Contributor
  • ***
  • Posts: 6173
  • Country: gb
  • Doing electronics since the 1960s...
Re: ST 32F417 veneer code (or something quite different)
« Reply #20 on: August 22, 2022, 02:25:30 pm »
Quote
An easy and quick test is to delete the elf from which you generate the binary. Recompilation will regenerate it.

I deleted the old elf, and edited the batch file which generates the firmware upgrade file to use the new elf. That was easy and obvious. A silly mistake.

The current elf was always being generated (at every debugger run) but it wasn't getting picked up for the firmware upgrade file. My project has a firmware upgrade facility, via a file upload.

Quote
There may be another thing: if the bootloader loads the binary from the serial FLASH into the mcu FLASH, then regardless of how do you overwrite it in mcu FLASH, it gets reloaded from the serial FLASH once the bootloader runs. Timestamps/CRC inside the binary would reveal the difference.

The process used to process the firmware upgrade file and eventually end up with it in the CPU FLASH  is a bit long. There is CRC checking involved (a post build step appends a crc32 to the file) and other stuff. But this wasn't the problem. The file which came out of objcopy had the duff bytes in it. So this is nothing to do with my board. So the CPU FLASH also had the duff bytes in it.

All I can think of is the GCC toolkit update to v10.something which came with Cube v9 or 10. Also Cube got a very fast update from 10.0 to 10.1 which is unusual...

The lesson is to retest everything after anything major, which I normally do, but one can't retest really absolutely everything. I suppose the one benefit of spending a lot of time on a product is that by the time it is released, it should be fairly solid.
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->