AT45_SendCommand
Default mainpageat91libmemoriesspi-flashAT45_SendCommand
Description Source Call Graph
Start Line: 140
unsigned char AT45_SendCommand(At45 *pAt45, unsigned char cmd, unsigned char cmdSize, unsigned char *pData, unsigned int dataSize, unsigned int address, SpidCallback callback, void *pArgument)
{
    SpidCmd *pCommand;
    const At45Desc *pDesc = pAt45->pDesc;
    unsigned int dfAddress = 0;

    // Sanity checks
    ASSERT(pAt45, "AT45_Command: pAt45 is 0.\n\r");
    ASSERT(pDesc || (cmd == AT45_STATUS_READ),
           "AT45_Command: Device has no descriptor, only STATUS_READ command allowed\n\r");

    // Check if the SPI driver is available
    if (AT45_IsBusy(pAt45)) {

        return AT45_ERROR_LOCK;
    }

    // Compute command pattern
    pAt45->pCmdBuffer[0] = cmd;

    // Add address bytes if necessary
    if (cmdSize > 1) {

        ASSERT(pDesc, "AT45_Command: No descriptor for dataflash.\n\r");
        if (!configuredBinaryPage) {
            dfAddress =
                ((address / (pDesc->pageSize)) << pDesc->pageOffset)
                 + (address % (pDesc->pageSize));
        }
        else {
            dfAddress = address;
        }
        // Write address bytes
        if (pDesc->pageNumber >= 16384) {

            pAt45->pCmdBuffer[1] = ((dfAddress & 0x0F000000) >> 24);
            pAt45->pCmdBuffer[2] = ((dfAddress & 0x00FF0000) >> 16);
            pAt45->pCmdBuffer[3] = ((dfAddress & 0x0000FF00) >> 8);
            pAt45->pCmdBuffer[4] = ((dfAddress & 0x000000FF) >> 0);
    
            if ((cmd != AT45_CONTINUOUS_READ) && (cmd != AT45_PAGE_READ)) {
    
                cmdSize++;
            }
        }
        else {

            pAt45->pCmdBuffer[1] = ((dfAddress & 0x00FF0000) >> 16);
            pAt45->pCmdBuffer[2] = ((dfAddress & 0x0000FF00) >> 8);
            pAt45->pCmdBuffer[3] = ((dfAddress & 0x000000FF) >> 0);
        }
    }

    // Update the SPI Transfer descriptors
    pCommand = &(pAt45->command);
    pCommand->cmdSize = cmdSize;
    pCommand->pData = pData;
    pCommand->dataSize = dataSize;
    pCommand->callback = callback;
    pCommand->pArgument = pArgument;

    // Send Command and data through the SPI
    if (SPID_SendCommand(pAt45->pSpid, pCommand)) {

        return AT45_ERROR_SPI;
    }

    return 0;
}