TARGET_STM: fix flash api 64bit address alignment on L4 and WB - #12086
Conversation
|
@ABOSTM, thank you for your changes. |
|
|
||
| /* HW needs an aligned address to program flash, which data parameters doesn't ensure */ | ||
| if ((uint32_t) data % 4 != 0) { // Data is not aligned, copy data in a temp buffer before programming it | ||
| if ((uint32_t) data % 8 != 0) { // Data is not aligned, copy data in a temp buffer before programming it |
There was a problem hiding this comment.
Change isn't wrong, but it would be nice to tidy a bit - get rid of the pointer aliasing and volatile. I'd rather this was
if ((uintptr_t) data % 8 != 0) {
while ((address < (StartAddress + size)) && (status == 0)) {
uint64_t data64 = 0;
for (int i = 0; i < 8; i++) {
data64 |= (uint64_t) data[i] << (i * 8);
}
}
Although I would have thought it should be possible in principle to just case the uint8_t * to a __packed uint64_t * to get the compiler to figure out how to do a potentially-unaligned load. Catch is I don't think either CMSIS or Mbed has the necessary compiler macros to do that portably.
CMSIS has __UNALIGNED_UINT32_READ, but not __UNALIGNED_UINT64_READ. :(
I guess you could just do
while ((address < (StartAddress + size)) && (status == 0)) {
uint32_t data_low = __UNALIGNED_UINT32_READ(data);
uint32_t data_high = __UNALIGNED_UINT32_READ(data + 4);
uint64_t data64 = ((uint64_t) data_high << 32) | data_low;
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, address, data64) == HAL_OK) {
which should be just the same or only a bit off having a direct 64-bit macro.
(And do that unconditionally - don't check for alignment - there's really no need to increase the code size to "speed-optimise" the aligned case).
There was a problem hiding this comment.
Hi
So let's wait for ARM-software/CMSIS_5#768 merge in mbed ?
Then, to be honest, as this looks as optimization, this will not come in our todo list very soon...
So any help and pull requests are welcomed!
Jerome
There was a problem hiding this comment.
Yeah, okay, we can revisit this when that appears, rather than go for a half-nice version.
|
CI started |
Test run: SUCCESSSummary: 11 of 11 test jobs passed |
Summary of changes
fix flash api 64bit address alignment on L4 and WB
Pull request type
Test results
No test available with misaligned address.