Jump to main content

# 1.2.6.4.1 SRV_USI_Initialize Function

## C

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

## Summary

Initializes the specified USI service instance.

## Description

This routine initializes the specified USI service instance, making it ready for clients to open and use. The initialization data is specified by the `init` parameter.

The initialization may fail if the number of instances allocated are insufficient or if the specified instance is already initialized.

The USI service instance index is independent of the peripheral instance it is associated with. For example, USI service instance 0 can be assigned to UART peripheral instance 2.

## Parameters

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

## Returns

If successful, returns a valid USI instance object. Otherwise, returns SYS_MODULE_OBJ_INVALID.

## Example

    static uint8_t CACHE_ALIGN srvUSI0ReadBuffer[SRV_USI0_RD_BUF_SIZE] = {0};
    static uint8_t CACHE_ALIGN srvUSI0WriteBuffer[SRV_USI0_WR_BUF_SIZE] = {0};

    /* Declared in USI USART service implementation (srv_usi_usart.c) */
    extern const SRV_USI_DEV_DESC srvUSIUSARTDevDesc;

    const SRV_USI_USART_INTERFACE srvUsi0InitDataUART2 = {
        .readCallbackRegister = (USI_USART_PLIB_READ_CALLBACK_REG)UART2_ReadCallbackRegister,
        .readData = (USI_USART_PLIB_WRRD)UART2_Read,
        .writeData = (USI_USART_PLIB_WRRD)UART2_Write,
        .writeIsBusy = (USI_USART_PLIB_WRITE_ISBUSY)UART2_WriteIsBusy,
    };

    const USI_USART_INIT_DATA srvUsi0InitData = {
        .plib = (void*)&srvUsi0InitDataUART2,
        .pRdBuffer = (void*)srvUSI0ReadBuffer,
        .rdBufferSize = SRV_USI0_RD_BUF_SIZE,
    };

    const SRV_USI_INIT srvUSI0Init =
    {
        .deviceInitData = (const void * const)&srvUsi0InitData,
        .consDevDesc = &srvUSIUSARTDevDesc,
        .deviceIndex = 0,
        .pWrBuffer = srvUSI0WriteBuffer,
        .wrBufferSize = SRV_USI0_WR_BUF_SIZE
    };

    static uint8_t CACHE_ALIGN srvUSI1ReadBuffer[SRV_USI1_RD_BUF_SIZE] = {0};
    static uint8_t CACHE_ALIGN srvUSI1WriteBuffer[SRV_USI1_WR_BUF_SIZE] = {0};

    static uint8_t CACHE_ALIGN srvUSI1CDCReadBuffer[128] = {0};

    /* Declared in USI CDC service implementation (srv_usi_cdc.c) */
    extern const SRV_USI_DEV_DESC srvUSICDCDevDesc;

    const USI_CDC_INIT_DATA srvUsi1InitData = {
        .cdcInstanceIndex = 0,
        .usiReadBuffer = srvUSI1ReadBuffer,
        .usiBufferSize = SRV_USI1_RD_BUF_SIZE,
        .cdcReadBuffer = srvUSI1CDCReadBuffer,
        .cdcBufferSize = 128
    };

    const SRV_USI_INIT srvUSI1Init =
    {
        .deviceInitData = (const void * const)&srvUsi1InitData,
        .consDevDesc = &srvUSICDCDevDesc,
        .deviceIndex = 0,
        .pWrBuffer = srvUSI1WriteBuffer,
        .wrBufferSize = SRV_USI1_WR_BUF_SIZE
    };

    SYS_MODULE_OBJ  objSrvUSI0;
    SYS_MODULE_OBJ  objSrvUSI1;

    objSrvUSI0 = SRV_USI_Initialize(SRV_USI_INDEX_0, (SYS_MODULE_INIT *)&srvUSI0Init);
    if (objSrvUSI0 == SYS_MODULE_OBJ_INVALID)
    {
        // Handle error
    }

    objSrvUSI1 = SRV_USI_Initialize(SRV_USI_INDEX_1, (SYS_MODULE_INIT *)&srvUSI1Init);
    if (objSrvUSI1 == SYS_MODULE_OBJ_INVALID)
    {
        // Handle error
    }

## Remarks

his routine must be called before any other USI routine is called. This routine should only during system initialization.

Rev: G