Skip to content

Commit b353136

Browse files
Cruz Monrreal IICruz Monrreal II
authored andcommitted
Merge branch 'Fix_LPC_Flash_Driver' of ssh://github.com/NXPmicro/mbed into rollup
2 parents 6ebc5be + 48d4a45 commit b353136

File tree

1 file changed

+34
-5
lines changed
  • targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_LPC

1 file changed

+34
-5
lines changed

targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_LPC/flash_api.c

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
5858
{
5959
uint32_t n;
6060
uint32_t sector_number;
61-
61+
uint32_t num_of_bytes = size;
6262
uint32_t status;
6363
int32_t ret = -1;
6464

@@ -80,10 +80,39 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
8080

8181
status = FLASHIAP_PrepareSectorForWrite(sector_number, sector_number);
8282
if (status == kStatus_FLASHIAP_Success) {
83-
status = FLASHIAP_CopyRamToFlash(address, (uint32_t *)data,
84-
FSL_FEATURE_SYSCON_FLASH_PAGE_SIZE_BYTES, SystemCoreClock);
85-
if (status == kStatus_FLASHIAP_Success) {
86-
ret = 0;
83+
/* Check if the number of bytes to write is aligned to page size */
84+
if (size % FSL_FEATURE_SYSCON_FLASH_PAGE_SIZE_BYTES) {
85+
uint8_t page_buffer[FSL_FEATURE_SYSCON_FLASH_PAGE_SIZE_BYTES] = { 0 };
86+
uint32_t remaining_bytes = 0;
87+
88+
/* Find the number of pages and remaining bytes */
89+
num_of_bytes = FSL_FEATURE_SYSCON_FLASH_PAGE_SIZE_BYTES * (size / FSL_FEATURE_SYSCON_FLASH_PAGE_SIZE_BYTES);
90+
remaining_bytes = (size - num_of_bytes);
91+
92+
/* Copy the remaining bytes into a temp buffer whose size is page-aligned */
93+
memcpy(page_buffer, data + num_of_bytes, remaining_bytes);
94+
95+
if (num_of_bytes) {
96+
/* First write page size aligned bytes of data */
97+
status = FLASHIAP_CopyRamToFlash(address, (uint32_t *)data, num_of_bytes, SystemCoreClock);
98+
if (status == kStatus_FLASHIAP_Success) {
99+
/* Prepare the next write for the remaining data */
100+
status = FLASHIAP_PrepareSectorForWrite(sector_number, sector_number);
101+
}
102+
}
103+
104+
/* Write the remaining data */
105+
if (status == kStatus_FLASHIAP_Success) {
106+
status = FLASHIAP_CopyRamToFlash((address + num_of_bytes), (uint32_t *)page_buffer, FSL_FEATURE_SYSCON_FLASH_PAGE_SIZE_BYTES, SystemCoreClock);
107+
if (status == kStatus_FLASHIAP_Success) {
108+
ret = 0;
109+
}
110+
}
111+
} else {
112+
status = FLASHIAP_CopyRamToFlash(address, (uint32_t *)data, num_of_bytes, SystemCoreClock);
113+
if (status == kStatus_FLASHIAP_Success) {
114+
ret = 0;
115+
}
87116
}
88117
}
89118

0 commit comments

Comments
 (0)