AT26D_Write
Default mainpageat91libmemoriesspi-flashAT26D_Write
Description Source Call Graph
Start Line: 290
unsigned char AT26D_Write(At26 *pAt26, unsigned char *pData, unsigned int size, unsigned int address)
{
    unsigned int pageSize;
    unsigned int writeSize;
    unsigned char error;
    unsigned char status;

    SANITY_CHECK(pAt26);
    SANITY_CHECK(pData);

    // Retrieve device page size
    pageSize = AT26_PageSize(pAt26);

    // Program one page after the other
    while (size > 0) {
        // Compute number of bytes to program in page
        writeSize = min(size, pageSize - (address % pageSize));

        // Enable critical write operation
        AT26D_EnableWrite(pAt26);
     
         // Program page
          error = AT26_SendCommand(pAt26, AT26_BYTE_PAGE_PROGRAM, 4,
                           pData, writeSize, address, 0, 0);
        ASSERT(!error, "-F- AT26_WritePage: Failed to issue command.\n\r");
        // Wait for transfer to finish
        AT26D_Wait(pAt26);
        // Poll the Serial flash status register until the operation is achieved
        AT26D_WaitReady(pAt26);

        // Make sure that write was without error
        status = AT26D_ReadStatus(pAt26);
        if ((status & AT26_STATUS_EPE) == AT26_STATUS_EPE_ERROR) {

            return AT26_ERROR_PROGRAM;
        }
        pData += writeSize;
        size -= writeSize;
        address += writeSize;
    }

    return 0;
}