1.2.4.2 Using The Library

The PLC PVDD Monitor service must be used from within the PLC application to monitor whether PLC transmissions are allowed.

PLC PVDD Monitor service requires a callback to be set in order to notify PVDD Threshold events to the user.

Example application to monitor PVDD voltage to allow PLC transmissions

APP_PLC_DATA appPlc;

static void APP_PLC_PVDDMonitorCb( SRV_PVDDMON_CMP_MODE cmpMode, uintptr_t context )
{
    (void)context;
    
    if (cmpMode == SRV_PVDDMON_CMP_MODE_OUT)
    {
        /* PLC Transmission is not permitted */
        DRV_PLC_PHY_EnableTX(appPlc.drvPl360Handle, false);
        appPlc.pvddMonTxEnable = false;
        /* Restart PVDD Monitor to check when VDD is within the comparison window */
        SRV_PVDDMON_Restart(SRV_PVDDMON_CMP_MODE_IN);
    }
    else
    {
        /* PLC Transmission is permitted again */
        DRV_PLC_PHY_EnableTX(appPlc.drvPl360Handle, true);
        appPlc.pvddMonTxEnable = true;
        /* Restart PVDD Monitor to check when VDD is out of the comparison window */
        SRV_PVDDMON_Restart(SRV_PVDDMON_CMP_MODE_OUT);
    }
}

void APP_PLC_Initialize ( void )
{
    /* Set PVDD Monitor tracking data */
    appPlc.pvddMonTxEnable = true;
}

void APP_PLC_Tasks ( void )
{   
    /* Check the application's current state. */
    switch ( appPlc.state )
    {
        case APP_PLC_STATE_IDLE:
        {
            /* Initialize PLC driver */
            appPlc.state = APP_PLC_STATE_INIT;
        }
        break;

        case APP_PLC_STATE_INIT:
        {
            /* Open PLC driver */
            appPlc.drvPl360Handle = DRV_PLC_PHY_Open(DRV_PLC_PHY_INDEX_0, NULL);

            if (appPlc.drvPl360Handle != DRV_HANDLE_INVALID)
            {
                appPlc.state = APP_PLC_STATE_OPEN;
            }
            else
            {
                appPlc.state = APP_PLC_STATE_ERROR;
            }
            break;
        }

        case APP_PLC_STATE_OPEN:
        {
            /* Check PLC transceiver */
            if (DRV_PLC_PHY_Status(DRV_PLC_PHY_INDEX_0) == SYS_STATUS_READY)
            {
                /* Configure PLC callbacks */
                DRV_PLC_PHY_DataCfmCallbackRegister(appPlc.drvPl360Handle, APP_PLC_DataCfmCb, NULL);
                DRV_PLC_PHY_DataIndCallbackRegister(appPlc.drvPl360Handle, APP_PLC_DataIndCb, NULL);
                
                /* Apply PLC initial configuration */
                APP_PLC_SetInitialConfiguration();
                
                /* Enable PLC Transmission */
                DRV_PLC_PHY_EnableTX(appPlc.drvPl360Handle, true);

                /* Enable PLC PVDD Monitor Service */
                SRV_PVDDMON_RegisterCallback(APP_PLC_PVDDMonitorCb, 0);
                SRV_PVDDMON_Start(SRV_PVDDMON_CMP_MODE_OUT);
                
                /* Set PLC state to transmit a frame */
                appPlc.state = APP_PLC_STATE_TX_REQUEST;
            }
        }
        break;
    }
}

bool APP_PLC_SendData ( uint8_t* pData, uint16_t length )
{
    if (appPlc.state == APP_PLC_STATE_WAITING)
    {
        if (appPlc.pvddMonTxEnable)
        {
            if ((length > 0) && (length <= APP_PLC_BUFFER_SIZE))
            {
                /* Set data length in Tx Parameters structure */
                /* It should be equal or less than Maximum Data Length (see _get_max_psdu_len) */
                /* Otherwise DRV_PLC_PHY_TX_RESULT_INV_LENGTH will be reported in Tx Confirm */
                appPlcTx.pl360Tx.dataLength = length;
                memcpy(appPlcTx.pDataTx, pData, length);

                appPlc.plcTxState = APP_PLC_TX_STATE_WAIT_TX_CFM;

                DRV_PLC_PHY_TxRequest(appPlc.drvPl360Handle, &appPlcTx.pl360Tx);

                /* Set PLC state */
                if (appPlc.plcTxState == APP_PLC_TX_STATE_WAIT_TX_CFM)
                {
                    appPlc.state = APP_PLC_STATE_WAITING_TX_CFM;
                    return true;
                }
            }
        }
    }
    
    return false;
}