LPC SPIFILIB v1.01 bug in spifiProgram()

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

LPC SPIFILIB v1.01 bug in spifiProgram()

936 Views
arjanoskam
Contributor I

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.

Labels (3)
2 Replies

717 Views
victorjimenez
NXP TechSupport
NXP TechSupport

Hello Arjan,

The SPIFI Library provided in source is a simple example, it do not take into consideration of corner cases. We provided the source files so you can debug and make enhancements to the code according to what you need.

Hope it helps!

Victor.

 

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
Reply

717 Views
arjanoskam
Contributor I

Hi Victor,

NXP presents the SPIFI Library as a driver library, not as an example. Both on the website and in the LPC4088 manual, chapter 15.2:

Using the SPIFI, as described in this chapter, accomplished with a driver library

available from NXP Semiconductors.

Remark: The SPIFI software library with the SPIFI API are available on LPCWare.com.

And I think writing a buffer to an offset from a page boundary is hardly a corner case, in fact I would expect it happens a lot when using a file system.

Anyway, I ran into a problem and I fixed it to my satisfaction. I shared my solution with the community to help anybody who runs into the same problem, and maybe get some feedback or a better solution.

Kind regards,

Arjan