Skip to content

EraseFlash fails if address exceeds 0xFFFF #5

Closed
@bessman

Description

@bessman

EraseFlash returns BAD_ADDRESS when given an address over 0xFFFF. This is due to this check 1:

// check to make sure page is aligned here.
if ( (eraseAddress & FLASH_ERASE_PAGE_MASK) != eraseAddress)
{
    goodErase = false;
}

FLASH_ERASE_PAGE_MASK is 2:

#define FLASH_ERASE_PAGE_MASK  (~((FLASH_ERASE_PAGE_SIZE_IN_INSTRUCTIONS*2) - 1))

and FLASH_ERASE_PAGE_SIZE_IN_INSTRUCTIONS is 3:

#define FLASH_ERASE_PAGE_SIZE_IN_INSTRUCTIONS 1024U

1024U is an unsigned int, which is 16-bits wide in the PIC24. Therefore, when eraseAddress is greater than 0xFFFF (the largest value which fits in an unsigned int), the check incorrectly concludes that the address is misaligned.

Defining FLASH_ERASE_PAGE_SIZE_IN_INSTRUCTIONS as 1024UL should fix this.

In practice, the impact of this bug is low; if the entire program area is erased at once, the bug does not trigger. I only noticed it when I tried to erase one page at a time.

Footnotes

  1. https://github.com/fossasia/pslab-bootloader/blob/5e1616dc1366fb7e08c73000c62aba77432c1327/src/mcc_generated_files/boot/boot_process.c#L282

  2. https://github.com/fossasia/pslab-bootloader/blob/5e1616dc1366fb7e08c73000c62aba77432c1327/src/mcc_generated_files/boot/boot_process.c#L69

  3. https://github.com/fossasia/pslab-bootloader/blob/5e1616dc1366fb7e08c73000c62aba77432c1327/src/mcc_generated_files/memory/flash.h#L31

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions