From 68114ed82b100cec0c476f18c49f7119e8f0ed98 Mon Sep 17 00:00:00 2001 From: Mahesh Mahadevan Date: Tue, 23 Oct 2018 14:51:29 -0500 Subject: [PATCH] MCUXpresso: Update LPC Flash driver program page function Handle the case where the number of bytes to write is not aligned to page size Signed-off-by: Mahesh Mahadevan --- .../TARGET_LPC/flash_api.c | 39 ++++++++++++++++--- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_LPC/flash_api.c b/targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_LPC/flash_api.c index ccd89b06a6..ca573045e1 100644 --- a/targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_LPC/flash_api.c +++ b/targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_LPC/flash_api.c @@ -58,7 +58,7 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, { uint32_t n; uint32_t sector_number; - + uint32_t num_of_bytes = size; uint32_t status; int32_t ret = -1; @@ -80,10 +80,39 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, status = FLASHIAP_PrepareSectorForWrite(sector_number, sector_number); if (status == kStatus_FLASHIAP_Success) { - status = FLASHIAP_CopyRamToFlash(address, (uint32_t *)data, - FSL_FEATURE_SYSCON_FLASH_PAGE_SIZE_BYTES, SystemCoreClock); - if (status == kStatus_FLASHIAP_Success) { - ret = 0; + /* Check if the number of bytes to write is aligned to page size */ + if (size % FSL_FEATURE_SYSCON_FLASH_PAGE_SIZE_BYTES) { + uint8_t page_buffer[FSL_FEATURE_SYSCON_FLASH_PAGE_SIZE_BYTES] = { 0 }; + uint32_t remaining_bytes = 0; + + /* Find the number of pages and remaining bytes */ + num_of_bytes = FSL_FEATURE_SYSCON_FLASH_PAGE_SIZE_BYTES * (size / FSL_FEATURE_SYSCON_FLASH_PAGE_SIZE_BYTES); + remaining_bytes = (size - num_of_bytes); + + /* Copy the remaining bytes into a temp buffer whose size is page-aligned */ + memcpy(page_buffer, data + num_of_bytes, remaining_bytes); + + if (num_of_bytes) { + /* First write page size aligned bytes of data */ + status = FLASHIAP_CopyRamToFlash(address, (uint32_t *)data, num_of_bytes, SystemCoreClock); + if (status == kStatus_FLASHIAP_Success) { + /* Prepare the next write for the remaining data */ + status = FLASHIAP_PrepareSectorForWrite(sector_number, sector_number); + } + } + + /* Write the remaining data */ + if (status == kStatus_FLASHIAP_Success) { + status = FLASHIAP_CopyRamToFlash((address + num_of_bytes), (uint32_t *)page_buffer, FSL_FEATURE_SYSCON_FLASH_PAGE_SIZE_BYTES, SystemCoreClock); + if (status == kStatus_FLASHIAP_Success) { + ret = 0; + } + } + } else { + status = FLASHIAP_CopyRamToFlash(address, (uint32_t *)data, num_of_bytes, SystemCoreClock); + if (status == kStatus_FLASHIAP_Success) { + ret = 0; + } } }