139 const uint32_t jumpAddress = *(__IO uint32_t*)(
APP_ADDRESS + 4U);
141 const uint32_t stackPointer = *(__IO uint32_t*)
APP_ADDRESS;
144 if ((jumpAddress <
APP_ADDRESS) || (jumpAddress == 0xFFFFFFFFU))
150 if ((stackPointer < SRAM_BASE) ||
151 (stackPointer > (SRAM_BASE + SRAM1_SIZE_MAX + SRAM2_SIZE)))
172 uint32_t jumpAddress = *(__IO uint32_t*)(
APP_ADDRESS + 4U);
250 const uint32_t appSize = f_size(&file);
251 LogPrint(
"Size of application on the SD card: %u bytes\n", appSize);
254 LogPrint(
"Error: Application on SD card is too large.\n");
258 (void)f_close(&file);
262 LogPrint(
"Error: Application file cannot be opened.\n");
271 uint32_t calculatedCrc = 0U;
272 uint32_t providedCrc = 0U;
281 if (providedCrc == calculatedCrc)
283 LogPrint(
"CRC matches. CRC value: 0x%x\n", calculatedCrc);
288 LogPrint(
"Provided application CRC: 0x%08x\n", providedCrc);
289 LogPrint(
"Calculated application CRC: 0x%08x\n", calculatedCrc);
302 LogPrint(
"Starting programming...\n");
309 uint32_t bytesFlashed = 0;
331 status = f_read(&file,
readBuffer, FLASH_PAGE_SIZE, &bytesRead);
334 LogPrint(
"Error: Application file read error at: %u byte\n",
335 (uint32_t)(file.fptr));
342 (uint32_t)bytesRead) != (uint32_t)bytesRead)
344 LogPrint(
"Error: Programming error at: %u byte\n",
349 bytesFlashed += bytesRead;
354 }
while (bytesRead > 0U);
357 (void)f_close(&file);
361 LogPrint(
"Programming successful. Flashed: %u bytes\n",
368 LogPrint(
"Error: Application file cannot be opened.\n");
381 LogPrint(
"Starting verification...\n");
407 uint32_t dataChunkFromFile = 0U;
408 if (FR_OK != f_read(&file, &dataChunkFromFile,
409 sizeof(dataChunkFromFile), &bytesRead))
411 LogPrint(
"Error: Application file read error at: %u byte\n",
412 (uint32_t)(file.fptr));
419 uint32_t dataChunkFromFlash = 0U;
420 (void)
FlashRead((uint8_t*)&dataChunkFromFlash, addressToVerify,
421 sizeof(dataChunkFromFlash));
422 if (dataChunkFromFlash != dataChunkFromFile)
424 LogPrint(
"Error: Verification error at: 0x%x\n",
430 addressToVerify += bytesRead;
432 }
while (bytesRead > 0U);
435 (void)f_close(&file);
439 LogPrint(
"Verification successful. Verified: %u bytes\n",
445 LogPrint(
"Error: Application file cannot be opened.\n");
457 uint32_t calculatedCrc = 0U;
467 if (FR_OK != f_read(&file,
readBuffer, FLASH_PAGE_SIZE, &bytesRead))
469 LogPrint(
"Error: Application file read error at: %u byte\n",
470 (uint32_t)(file.fptr));
478 }
while (bytesRead > 0U);
479 (void)f_close(&file);
483 LogPrint(
"Error: Application file cannot be opened.\n");
489 *crc = calculatedCrc;
503 if (FR_OK == f_read(&file,
readBuffer, 8U, &bytesRead))
507 *crc = strtoul((
const char*)
readBuffer, NULL, 16);
515 "Error: CRC cannot be converted to numerical value.\n");
520 LogPrint(
"Error: CRC file cannot be read.\n");
522 (void)f_close(&file);
526 LogPrint(
"Error: CRC file cannot be opened.\n");
This file contains the board-specific GPIO pin definitions, peripheral configurations,...
This file contains the bootloader configuration parameters, function prototypes, and other required d...
This file contains the CRC peripheral driver function prototypes for initialization,...
This file contains the MCU internal flash driver function prototypes for read, write,...
bool BootloaderUpdateFirmware(void)
This function performs the complete application update procedure.
static bool CheckApplicationCrc(void)
Validate the integrity of the application binary by comparing a CRC-32 value calculated over the file...
bool BootloaderCheckForApplication(void)
This function checks whether a valid application exists in flash.
static bool CalculateCrcOfFile(uint32_t *const crc)
Calculate the CRC-32 checksum of the application binary file on the SD card using the hardware CRC pe...
static bool CheckApplicationSize(void)
Check whether the application binary on the SD card fits into the available flash memory region desig...
static uint8_t readBuffer[FLASH_PAGE_SIZE]
static bool ReadCrcFromFile(uint32_t *const crc)
Read the expected CRC-32 value from a dedicated CRC file on the SD card and convert the hexadecimal s...
static bool VerifyApplication(void)
Verify the programmed flash contents against the original application binary file on the SD card by p...
void BootloaderJumpToApplication(void)
This function performs the jump to the user application in flash.
void BootloaderJumpToSysMem(void)
This function performs the jump to the microcontroller System Memory (ST built-in bootloader).
static bool UpdateApplication(void)
Erase the application flash region and program it with the contents of the application binary file fr...
bool SdCardMount(void)
Mount the FatFs file system on the SD card by linking the disk I/O driver and mounting the default lo...
void SdCardUnMount(void)
Unmount the FatFs file system from the SD card and unlink the disk I/O driver.
void SetVectorTableLocation(const uint32_t address)
Set the vector table location to the specified address.
uint32_t CrcAccumulate(const uint8_t *const data, const uint32_t size)
Calculate the CRC value over the provided buffer.
void CrcDeInit(void)
De-initialize the CRC peripheral.
void CrcInit(void)
Initialize the CRC peripheral.
uint32_t FlashRead(uint8_t *const data, const uint32_t address, const uint32_t length)
Read data from the microcontroller flash.
#define SYSTEM_MEMORY_ADDRESS
void FlashRemapMemoryToSystemFlash(void)
Remap the microcontroller flash memory to the built-in system flash memory.
uint32_t FlashWrite(const uint8_t *const data, const uint32_t address, const uint32_t length)
Write data into the microcontroller flash.
void FlashClearErrorFlags(void)
Clear all flash error flags.
void LedGreen1Off(void)
Turn off the 1st green LED (LD2).
void LedGreen1Toggle(void)
Toggle the 1st green LED (LD2).
void LedGreen2On(void)
Turn on the 2nd green LED (LD3).
void LedGreen2Off(void)
Turn off the 2nd green LED (LD3).
void LogPrint(const char *format,...)
Print a formatted log message over the UART interface.
#define assert_param(expr)
The assert_param macro is used for function parameter check.
This file contains the LED driver function prototypes for controlling the green LEDs on the STM32L496...
This file contains the UART-based logging function prototypes for the debug output.
This file includes the microcontroller-specific HAL header files.
This file contains the SD card disk I/O driver interface for the FatFs middleware.
This file contains the SD card FatFs file system mount and unmount function prototypes.
This file contains the SDMMC peripheral driver function prototypes for SD card initialization,...
This file contains the SysTick timer initialization and de-initialization function prototypes.