Arjan Oskam

LPC SPIFILIB v1.01 bug in spifiProgram()

Discussion created by Arjan Oskam on Mar 27, 2018
Latest reply on Apr 9, 2018 by Arjan Oskam

While implementing a file system on a LPC4088 with MX25L6445E serial flash using LPCSPIFILIB v1.01, I found a bug in the spifiProgram() function.

 

The function programs a certain amount of bytes to an address in serial flash. However it doesn’t take into account the possibility that the start address to write may have an offset from the page boundary. Therefore it’s possible that data is written beyond the upper boundary of the page, effectively writing data to the start of the same page instead of the next page.

 

The solution is to subtract the page offset from the page size, in order to determine the amount of bytes that fir into the current page. This is the source code with my solution, which has been tested on a LPC4088 with a Macronix MX25L6445E serial flash:

 

/* Program the device with the passed buffer */
/* 2018-03-07 AO: Changed code so the 'uint32_t addr' parameter can also contain
* an address that starts at an offset from the page boundary. */                   
SPIFI_ERR_T spifiProgram(const SPIFI_HANDLE_T *pHandle, uint32_t addr, const uint32_t *writeBuff, uint32_t bytes)
{
    uint32_t sendBytes;
    SPIFI_ERR_T err = SPIFI_ERR_NONE;
    uint32_t PageOffset;
    uint32_t MaxSendBytes;

 

    /* Program using up to page size */
    while ((bytes > 0) && (err == SPIFI_ERR_NONE)) {
        PageOffset = addr & (pHandle->pInfoData->pageSize - 1); /* Assuming page size is always 2^x */
        MaxSendBytes = pHandle->pInfoData->pageSize - PageOffset;

 

        sendBytes = bytes;
        if (sendBytes > MaxSendBytes) {
            sendBytes = MaxSendBytes;
        }

 

        err = pHandle->pFamFx->pageProgram(pHandle, addr, writeBuff, sendBytes);
        addr += sendBytes;
        writeBuff += (sendBytes >> 2);
        bytes -= sendBytes;
    }

 

    return err;
}

 

I would appreciate your comments.

Outcomes