stm32/flash: Fix writing final words to flash on H5 and H7 MCUs.

The calculations `num_word32 / 4` and `num_word32 / 8` were rounding down
the number of words to program to flash, and therefore possibly truncating
the data (eg mboot could miss writing the final few words of the firmware).

That's fixed in this commit by adding extra logic to program any remaining
words.  And the logic for H5 and H7 is combined.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George 2024-04-28 15:35:38 +10:00
parent 64f28dc1eb
commit d3fe0a06e8

View File

@ -24,6 +24,7 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
#include <string.h>
#include "py/mpconfig.h" #include "py/mpconfig.h"
#include "py/misc.h" #include "py/misc.h"
#include "py/mphal.h" #include "py/mphal.h"
@ -450,28 +451,38 @@ int flash_write(uint32_t flash_dest, const uint32_t *src, uint32_t num_word32) {
#endif #endif
} }
#elif defined(STM32H5) #elif defined(STM32H5) || defined(STM32H7)
// program the flash 128 bits (4 words) at a time #if defined(STM32H5)
for (int i = 0; i < num_word32 / 4; i++) { static const unsigned int WORD32_PER_FLASHWORD = 4;
status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_QUADWORD, flash_dest, (uint64_t)(uint32_t)src); static const unsigned int PROGRAM_TYPE = FLASH_TYPEPROGRAM_QUADWORD;
if (status != HAL_OK) { #else
break; static const unsigned int WORD32_PER_FLASHWORD = FLASH_NB_32BITWORD_IN_FLASHWORD;
static const unsigned int PROGRAM_TYPE = FLASH_TYPEPROGRAM_FLASHWORD;
#endif
if (flash_dest % (WORD32_PER_FLASHWORD * sizeof(uint32_t))) {
// The flash_dest address is not aligned correctly.
status = HAL_ERROR;
} else {
// Program the flash WORD32_PER_FLASHWORD words at a time.
for (int i = 0; i < num_word32 / WORD32_PER_FLASHWORD; i++) {
status = HAL_FLASH_Program(PROGRAM_TYPE, flash_dest, (uint32_t)src);
if (status != HAL_OK) {
break;
}
flash_dest += WORD32_PER_FLASHWORD * sizeof(uint32_t);
src += WORD32_PER_FLASHWORD;
} }
flash_dest += 16;
src += 4;
}
#elif defined(STM32H7) // Program any remaining words from src.
// Additional bytes beyond that are programmed to 0xff.
// program the flash 256 bits at a time if (num_word32 % WORD32_PER_FLASHWORD) {
for (int i = 0; i < num_word32 / 8; i++) { uint32_t buf[WORD32_PER_FLASHWORD];
status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD, flash_dest, (uint64_t)(uint32_t)src); memset(buf, 0xff, sizeof(buf));
if (status != HAL_OK) { memcpy(buf, src, (num_word32 % WORD32_PER_FLASHWORD) * sizeof(uint32_t));
break; status = HAL_FLASH_Program(PROGRAM_TYPE, flash_dest, (uint32_t)buf);
} }
flash_dest += 32;
src += 8;
} }
#else #else