Skip to content

MCUXpresso: Update LPC Flash driver program page function #8528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 9, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_LPC/flash_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment for FLASHIAP_CopyRamToFlash() says it can handle num_of_bytes:
"The addresses should be a 256 byte boundary and the number of bytes should be 256 | 512 | 1024 | 4096."
Here num_of_bytes can be any number aligned to 256. Its worth double checking whether 256|512|1024|4096 also includes other multiplies of 256... e.g. 768

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I modified the test to set the test_size to be page_size * 3, the tests passed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate it - thanks!

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;
}
}
}

Expand Down