Jump to main content

##

# 1.1.8.4.1 DRV_SPI_Initialize Function

## C

    SYS_MODULE_OBJ DRV_SPI_Initialize
    (
        const SYS_MODULE_INDEX index,
        const SYS_MODULE_INIT * const init
    )

## Summary

Initializes the SPI instance for the specified driver index.

## Description

This routine initializes the SPI driver instance for the specified driver index, making it ready for clients to open and use it. The initialization data is specified by the init parameter. The initialization may fail if the number of driver objects allocated are insufficient or if the specified driver instance is already initialized. The driver instance index is independent of the SPI module ID. For example, driver instance 0 can be assigned to SPI2.

## Precondition

None.

## Parameters

Param | Description
--- | ---
index | Identifier for the instance to be initialized
init | Pointer to the init data structure containing any data necessary to initialize the driver.

## Returns

If successful, returns a valid handle to a driver instance object. Otherwise, returns SYS_MODULE_OBJ_INVALID.

## Example

    // The following code snippet shows an example SPI driver initialization.

    SYS_MODULE_OBJ objectHandle;

    const DRV_SPI_PLIB_INTERFACE drvSPI0PlibAPI = {

        // SPI PLIB Setup
        .setup = (DRV_SPI_PLIB_SETUP)SPI0_TransferSetup,

        // SPI PLIB WriteRead function
        .writeRead = (DRV_SPI_PLIB_WRITE_READ)SPI0_WriteRead,

        // SPI PLIB Transfer Status function
        .isBusy = (DRV_SPI_PLIB_IS_BUSY)SPI0_IsBusy,

        // SPI PLIB Callback Register
        .callbackRegister = (DRV_SPI_PLIB_CALLBACK_REGISTER)SPI0_CallbackRegister,
    };

    const DRV_SPI_INIT drvSPI0InitData = {

        // SPI PLIB API
        .spiPlib = &drvSPI0PlibAPI,

        .remapDataBits = drvSPI0remapDataBits,
        .remapClockPolarity = drvSPI0remapClockPolarity,
        .remapClockPhase = drvSPI0remapClockPhase,

        /// SPI Number of clients
        .numClients = DRV_SPI_CLIENTS_NUMBER_IDX0,

        // SPI Client Objects Pool
        .clientObjPool = (uintptr_t)&drvSPI0ClientObjPool[0],

        // DMA Channel for Transmit
        .dmaChannelTransmit = DRV_SPI_XMIT_DMA_CH_IDX0,

        // DMA Channel for Receive
        .dmaChannelReceive = DRV_SPI_RCV_DMA_CH_IDX0,

        // SPI Transmit Register
        .spiTransmitAddress = (void *)&(SPI0_REGS->SPI_TDR),

        // SPI Receive Register
        .spiReceiveAddress = (void *)&(SPI0_REGS->SPI_RDR),

        // Interrupt source is DMA

        .interruptSource = XDMAC_IRQn,

        // SPI Queue Size
        .queueSize = DRV_SPI_QUEUE_SIZE_IDX0,

        // SPI Transfer Objects Pool
        .transferObjPool = (uintptr_t)&drvSPI0TransferObjPool[0],
    };

    objectHandle = DRV_SPI_Initialize(DRV_SPI_INDEX_0,(SYS_MODULE_INIT*)&drvSPI0InitData);

    if (objectHandle == SYS_MODULE_OBJ_INVALID)
    {
        // Handle error
    }

## Remarks

- This routine must be called before any other SPI routine is called.
- This routine must only be called once during system initialization.
- This routine will NEVER block for hardware access.