1.1.7.4.9 DRV_SDSPI_AsyncWrite Function

C

void DRV_SDSPI_AsyncWrite(
    const DRV_HANDLE handle,
    DRV_SDSPI_COMMAND_HANDLE* commandHandle,
    void* sourceBuffer,
    uint32_t blockStart,
    uint32_t nBlocks
)

Summary

Writes blocks of data starting at the specified address of the SD Card.

Description

This function performs a non-blocking write operation to write blocks of data to the SD Card. The function returns with a valid buffer handle in the commandHandle argument if the write request was scheduled successfully. The function adds the request to the hardware instance queue and returns immediately. While the request is in the queue, the application buffer is owned by the driver and should not be modified. The function returns DRV_SDSPI_COMMAND_HANDLE_INVALID in the commandHandle argument under the following circumstances:

  • if the driver handle is invalid

  • if the source buffer pointer is NULL

  • if the number of blocks to be written is either zero or more than the number of blocks actually available

  • if the SD card is write-protected

  • if there was an error during the SD card write operation

If the requesting client registered an event callback with the driver, the driver will issue a DRV_SDSPI_EVENT_COMMAND_COMPLETE event if the buffer was processed successfully or DRV_SDSPI_EVENT_COMMAND_ERROR event if the buffer was not processed successfully.

Precondition

The DRV_SDSPI_Initialize routine must have been called for the specified SDSPI driver instance.

DRV_SDSPI_Open routine must have been called to obtain a valid opened device handle.

Parameters

ParamDescription
handleA valid open-instance handle, returned from the driver's open function
commandHandlePointer to an argument that will contain the return buffer handle
sourceBufferThe source buffer containing data to be written to the SD Card.
blockStartStarting block address of SD Card where the writes should begin.
nBlockTotal number of blocks to be written.

Returns

The buffer handle is returned in the commandHandle argument. It will be DRV_SDSPI_COMMAND_HANDLE_INVALID if the request was not queued successfully.

Example

uint8_t CACHE_ALIGN myBuffer[MY_BUFFER_SIZE];

// address should be block aligned.
uint32_t blockStart = 0x00;
uint32_t nBlock = 2;
DRV_SDSPI_COMMAND_HANDLE commandHandle;
MY_APP_OBJ myAppObj;

// Event is received when
// the buffer is processed.

void APP_SDSPIEventHandler(
    DRV_SDSPI_EVENT event,
    DRV_SDSPI_COMMAND_HANDLE commandHandle,
    uintptr_t contextHandle
)
{
    // contextHandle points to myAppObj.
    
    switch(event)
    {
        case DRV_SDSPI_EVENT_COMMAND_COMPLETE:
        {
            // This means the data was transferred successfully
            break;
        }
        
        case DRV_SDSPI_EVENT_COMMAND_ERROR:
        {
            // Error handling here
            break;
        }
        
        default:
        {
            break;
        }
    }
}

// mySDSPIHandle is the handle returned
// by the DRV_SDSPI_Open function.

// Client registers an event handler with driver
DRV_SDSPI_EventHandlerSet(mySDSPIHandle, APP_SDSPIEventHandler, (uintptr_t)&myAppObj);

DRV_SDSPI_AsyncWrite(mySDSPIHandle, &commandHandle, &myBuffer[0], blockStart, nBlock);

if(commandHandle == DRV_SDSPI_COMMAND_HANDLE_INVALID)
{
    // Error handling here
}

Remarks

None.