1.1.3.4.10 DRV_MEMORY_AsyncWrite Function

C

void DRV_MEMORY_AsyncWrite
(
    const DRV_HANDLE handle,
    DRV_MEMORY_COMMAND_HANDLE *commandHandle,
    void *sourceBuffer,
    uint32_t blockStart,
    uint32_t nBlock
);

Summary

Writes data for the specified number of memory blocks in Asynchronous mode.

Description

This function schedules a non-blocking write operation for writing blocks of data into attached devices memory.

The function returns with a valid command 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_MEMORY_COMMAND_HANDLE_INVALID in the commandHandle argument under the following circumstances:

  • if a buffer object could not be allocated to the request

  • if the source buffer pointer is NULL

  • if the client opened the driver for read only

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

  • if the driver handle is invalid

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

If the requesting client has not registered any transfer handler callback with the driver, he can call DRV_MEMORY_CommandStatusGetGet() API to know the current status of the request.

Preconditions

DRV_MEMORY_Open() routine must have been called to obtain a valid opened device handle.

The memory address location which has to be written, must have been erased before using the DRV_MEMORY_xxxErase() routine.

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 programmed into media device memory
blockStartBlock start from where the data should be written to.
nBlockTotal number of blocks to be written.

Returns

The command handle is returned in the commandHandle argument. It will be DRV_MEMORY_COMMAND_HANDLE_INVALID if the request was not successful.

Example

#define BUFFER_SIZE 4096
uint8_t writeBuffer[BUFFER_SIZE];

// Use DRV_MEMORY_GeometryGet () to find the write region geometry.
uint32_t blockStart = 0x0;
uint32_t nBlock = BUFFER_SIZE / block_size; // block_size for write geometry
bool xfer_done = false;

DRV_MEMORY_COMMAND_HANDLE commandHandle;

// memoryHandle is the handle returned by the DRV_MEMORY_Open function.

// Event is received when the write request is completed.
void appTransferHandler
(
    DRV_MEMORY_EVENT event,
    DRV_MEMORY_COMMAND_HANDLE commandHandle,
    uintptr_t context
)
{
    switch(event)
    {
        case DRV_MEMORY_EVENT_COMMAND_COMPLETE:
        {
            xfer_done = true;
            break;
        }

        case DRV_MEMORY_EVENT_COMMAND_ERROR:
        {
            // Handle Error
            break;
        }

        default:
        {
            break;
        }
    }
}

DRV_MEMORY_TransferHandlerSet(memoryHandle, appTransferHandler, (uintptr_t)NULL);

DRV_MEMORY_AsyncErase(memoryHandle, &commandHandle, blockStart, nBlock);

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

// Wait for erase to be completed
while(!xfer_done);

DRV_MEMORY_AsyncWrite(memoryHandle, &commandHandle, &writeBuffer, blockStart, nBlock);

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

// Wait for write to be completed
while(!xfer_done);

Remarks

This API is supported in Both Bare-Metal and RTOS environment.