components/esp-cryptoauthlib/cryptoauthlib/lib/hal/hal_pic32mz2048efm_i2c.c
ATCA_STATUS hal_i2c_wake(ATCAIface iface)
{
    DRV_I2C_Object *tempI2cObj;
    ATCAIfaceCfg *cfg = atgetifacecfg(iface);
    //int bus = cfg->atcai2c.bus;
    //int retries = cfg->rx_retries;
    uint32_t bdrt = cfg->atcai2c.baud;
    uint8_t data[4];
    DRV_I2C_BUFFER_EVENT Rx_Transaction = DRV_I2C_BUFFER_EVENT_ERROR;

    data[0] = 0x00;

    if (bdrt != 100000)   // if not already at 100KHz, change it
    {
        uint32_t backupBaudRate;
        DRV_I2C_INIT *tempi2cDriverInit;

        DRV_I2C_Close(drvI2CMasterHandle);

        /* DeInitialize Drivers */
        tempI2cObj = (DRV_I2C_Object*)(cfg->cfg_data);
        DRV_I2C_Deinitialize(tempI2cObj->i2cDriverInstance);

        /* Re-Initialize Drivers for ECC508A sleep configuration - I2C speed at 100KHZ */
        tempi2cDriverInit = (DRV_I2C_INIT*)tempI2cObj->i2cDriverInit;
        backupBaudRate = tempi2cDriverInit->baudRate;
        tempi2cDriverInit->baudRate = 100000;

        tempI2cObj->i2cDriverInstance = DRV_I2C_Initialize(tempI2cObj->i2cDriverInstanceIndex, (SYS_MODULE_INIT*)tempi2cDriverInit);
        drvI2CMasterHandle1 = DRV_I2C_Open(tempI2cObj->i2cDriverInstanceIndex, DRV_IO_INTENT_NONBLOCKING | DRV_IO_INTENT_READWRITE);
        write_bufHandle = DRV_I2C_Transmit(drvI2CMasterHandle1, cfg->atcai2c.slave_address, &data, 1, NULL);
        if ( ((DRV_I2C_BUFFER_HANDLE)(-1)) == write_bufHandle)
        {
            while (1)
            {
                ;
            }
        }

        atca_delay_us(cfg->wake_delay); // wait tWHI + tWLO which is configured based on device type and configuration structure
        read_bufHandle = DRV_I2C_Receive(drvI2CMasterHandle1, cfg->atcai2c.slave_address, &data, 4, NULL);
        do
        {
            Rx_Transaction = DRV_I2C_TransferStatusGet(drvI2CMasterHandle1, read_bufHandle);
        }
        while (Rx_Transaction != DRV_I2C_BUFFER_EVENT_COMPLETE && Rx_Transaction != DRV_I2C_BUFFER_EVENT_ERROR);

        DRV_I2C_Close(drvI2CMasterHandle1);

        /* DeInitialize Drivers */
        DRV_I2C_Deinitialize(tempI2cObj->i2cDriverInstance);

        /* Re-Initialize Drivers back for ECC508A default configuration*/
        tempi2cDriverInit->baudRate = backupBaudRate;
        tempI2cObj->i2cDriverInstance = DRV_I2C_Initialize(tempI2cObj->i2cDriverInstanceIndex, (SYS_MODULE_INIT*)tempi2cDriverInit);
        drvI2CMasterHandle = DRV_I2C_Open(tempI2cObj->i2cDriverInstanceIndex, DRV_IO_INTENT_NONBLOCKING | DRV_IO_INTENT_READWRITE);
    }
    else
    {
        DRV_I2C_BUFFER_EVENT statusevent;
        // Send the wake by writing to an address of 0x00
        write_bufHandle = DRV_I2C_Transmit(drvI2CMasterHandle, cfg->atcai2c.slave_address, &data, 1, NULL);
        if ( ((DRV_I2C_BUFFER_HANDLE)(-1)) == write_bufHandle)
        {
            while (1)
            {
                ;
            }
        }
        atca_delay_us(cfg->wake_delay); // wait tWHI + tWLO which is configured based on device type and configuration structure
        read_bufHandle = DRV_I2C_Receive(drvI2CMasterHandle, cfg->atcai2c.slave_address, &data, 4, NULL);
        do
        {
            Rx_Transaction = DRV_I2C_TransferStatusGet(drvI2CMasterHandle, read_bufHandle);
        }
        while (Rx_Transaction != DRV_I2C_BUFFER_EVENT_COMPLETE && Rx_Transaction != DRV_I2C_BUFFER_EVENT_ERROR);
    }

    return hal_check_wake(data, 4);
}
/** \brief wake up CryptoAuth device using I2C bus
 * \param[in] iface  interface to logical device to wakeup
 * \return ATCA_SUCCESS on success, otherwise an error code.
 */
\brief wake up CryptoAuth device using I2C bus
\param[in] iface  interface to logical device to wakeup
\return ATCA_SUCCESS on success, otherwise an error code.
\ brief wake up CryptoAuth device using I2C bus \ param [ in ] iface interface to logical device to wakeup \ return ATCA_SUCCESS on success otherwise an error code .
ATCA_STATUS hal_i2c_wake(ATCAIface iface)
{
    DRV_I2C_Object *tempI2cObj;
    ATCAIfaceCfg *cfg = atgetifacecfg(iface);
    uint32_t bdrt = cfg->atcai2c.baud;
    uint8_t data[4];
    DRV_I2C_BUFFER_EVENT Rx_Transaction = DRV_I2C_BUFFER_EVENT_ERROR;
    data[0] = 0x00;
    if (bdrt != 100000)   
    {
        uint32_t backupBaudRate;
        DRV_I2C_INIT *tempi2cDriverInit;
        DRV_I2C_Close(drvI2CMasterHandle);
        tempI2cObj = (DRV_I2C_Object*)(cfg->cfg_data);
        DRV_I2C_Deinitialize(tempI2cObj->i2cDriverInstance);
        tempi2cDriverInit = (DRV_I2C_INIT*)tempI2cObj->i2cDriverInit;
        backupBaudRate = tempi2cDriverInit->baudRate;
        tempi2cDriverInit->baudRate = 100000;
        tempI2cObj->i2cDriverInstance = DRV_I2C_Initialize(tempI2cObj->i2cDriverInstanceIndex, (SYS_MODULE_INIT*)tempi2cDriverInit);
        drvI2CMasterHandle1 = DRV_I2C_Open(tempI2cObj->i2cDriverInstanceIndex, DRV_IO_INTENT_NONBLOCKING | DRV_IO_INTENT_READWRITE);
        write_bufHandle = DRV_I2C_Transmit(drvI2CMasterHandle1, cfg->atcai2c.slave_address, &data, 1, NULL);
        if ( ((DRV_I2C_BUFFER_HANDLE)(-1)) == write_bufHandle)
        {
            while (1)
            {
                ;
            }
        }
        atca_delay_us(cfg->wake_delay); 
        read_bufHandle = DRV_I2C_Receive(drvI2CMasterHandle1, cfg->atcai2c.slave_address, &data, 4, NULL);
        do
        {
            Rx_Transaction = DRV_I2C_TransferStatusGet(drvI2CMasterHandle1, read_bufHandle);
        }
        while (Rx_Transaction != DRV_I2C_BUFFER_EVENT_COMPLETE && Rx_Transaction != DRV_I2C_BUFFER_EVENT_ERROR);
        DRV_I2C_Close(drvI2CMasterHandle1);
        DRV_I2C_Deinitialize(tempI2cObj->i2cDriverInstance);
        tempi2cDriverInit->baudRate = backupBaudRate;
        tempI2cObj->i2cDriverInstance = DRV_I2C_Initialize(tempI2cObj->i2cDriverInstanceIndex, (SYS_MODULE_INIT*)tempi2cDriverInit);
        drvI2CMasterHandle = DRV_I2C_Open(tempI2cObj->i2cDriverInstanceIndex, DRV_IO_INTENT_NONBLOCKING | DRV_IO_INTENT_READWRITE);
    }
    else
    {
        DRV_I2C_BUFFER_EVENT statusevent;
        write_bufHandle = DRV_I2C_Transmit(drvI2CMasterHandle, cfg->atcai2c.slave_address, &data, 1, NULL);
        if ( ((DRV_I2C_BUFFER_HANDLE)(-1)) == write_bufHandle)
        {
            while (1)
            {
                ;
            }
        }
        atca_delay_us(cfg->wake_delay); 
        read_bufHandle = DRV_I2C_Receive(drvI2CMasterHandle, cfg->atcai2c.slave_address, &data, 4, NULL);
        do
        {
            Rx_Transaction = DRV_I2C_TransferStatusGet(drvI2CMasterHandle, read_bufHandle);
        }
        while (Rx_Transaction != DRV_I2C_BUFFER_EVENT_COMPLETE && Rx_Transaction != DRV_I2C_BUFFER_EVENT_ERROR);
    }
    return hal_check_wake(data, 4);
}
ATCA_STATUS hal_i2c_wake ( ATCAIface iface ) { DRV_I2C_Object * tempI2cObj ; ATCAIfaceCfg * cfg = atgetifacecfg ( iface ) ; uint32_t bdrt = cfg -> atcai2c . baud ; uint8_t data [ 4 ] ; DRV_I2C_BUFFER_EVENT Rx_Transaction = DRV_I2C_BUFFER_EVENT_ERROR ; data [ 0 ] = 0x00 ; if ( bdrt != 100000 ) { uint32_t backupBaudRate ; DRV_I2C_INIT * tempi2cDriverInit ; DRV_I2C_Close ( drvI2CMasterHandle ) ; tempI2cObj = ( DRV_I2C_Object * ) ( cfg -> cfg_data ) ; DRV_I2C_Deinitialize ( tempI2cObj -> i2cDriverInstance ) ; tempi2cDriverInit = ( DRV_I2C_INIT * ) tempI2cObj -> i2cDriverInit ; backupBaudRate = tempi2cDriverInit -> baudRate ; tempi2cDriverInit -> baudRate = 100000 ; tempI2cObj -> i2cDriverInstance = DRV_I2C_Initialize ( tempI2cObj -> i2cDriverInstanceIndex , ( SYS_MODULE_INIT * ) tempi2cDriverInit ) ; drvI2CMasterHandle1 = DRV_I2C_Open ( tempI2cObj -> i2cDriverInstanceIndex , DRV_IO_INTENT_NONBLOCKING | DRV_IO_INTENT_READWRITE ) ; write_bufHandle = DRV_I2C_Transmit ( drvI2CMasterHandle1 , cfg -> atcai2c . slave_address , & data , 1 , NULL ) ; if ( ( ( DRV_I2C_BUFFER_HANDLE ) ( -1 ) ) == write_bufHandle ) { while ( 1 ) { ; } } atca_delay_us ( cfg -> wake_delay ) ; read_bufHandle = DRV_I2C_Receive ( drvI2CMasterHandle1 , cfg -> atcai2c . slave_address , & data , 4 , NULL ) ; do { Rx_Transaction = DRV_I2C_TransferStatusGet ( drvI2CMasterHandle1 , read_bufHandle ) ; } while ( Rx_Transaction != DRV_I2C_BUFFER_EVENT_COMPLETE && Rx_Transaction != DRV_I2C_BUFFER_EVENT_ERROR ) ; DRV_I2C_Close ( drvI2CMasterHandle1 ) ; DRV_I2C_Deinitialize ( tempI2cObj -> i2cDriverInstance ) ; tempi2cDriverInit -> baudRate = backupBaudRate ; tempI2cObj -> i2cDriverInstance = DRV_I2C_Initialize ( tempI2cObj -> i2cDriverInstanceIndex , ( SYS_MODULE_INIT * ) tempi2cDriverInit ) ; drvI2CMasterHandle = DRV_I2C_Open ( tempI2cObj -> i2cDriverInstanceIndex , DRV_IO_INTENT_NONBLOCKING | DRV_IO_INTENT_READWRITE ) ; } else { DRV_I2C_BUFFER_EVENT statusevent ; write_bufHandle = DRV_I2C_Transmit ( drvI2CMasterHandle , cfg -> atcai2c . slave_address , & data , 1 , NULL ) ; if ( ( ( DRV_I2C_BUFFER_HANDLE ) ( -1 ) ) == write_bufHandle ) { while ( 1 ) { ; } } atca_delay_us ( cfg -> wake_delay ) ; read_bufHandle = DRV_I2C_Receive ( drvI2CMasterHandle , cfg -> atcai2c . slave_address , & data , 4 , NULL ) ; do { Rx_Transaction = DRV_I2C_TransferStatusGet ( drvI2CMasterHandle , read_bufHandle ) ; } while ( Rx_Transaction != DRV_I2C_BUFFER_EVENT_COMPLETE && Rx_Transaction != DRV_I2C_BUFFER_EVENT_ERROR ) ; } return hal_check_wake ( data , 4 ) ; }
\brief wake up CryptoAuth device using I2C bus
 \param[in] iface  interface to logical device to wakeup
 \return ATCA_SUCCESS on success, otherwise an error code.
\ brief wake up CryptoAuth device using I2C bus \ param [ in ] iface interface to logical device to wakeup \ return ATCA_SUCCESS on success otherwise an error code .
//int bus = cfg->atcai2c.bus; //int retries = cfg->rx_retries; // if not already at 100KHz, change it /* DeInitialize Drivers */ /* Re-Initialize Drivers for ECC508A sleep configuration - I2C speed at 100KHZ */ // wait tWHI + tWLO which is configured based on device type and configuration structure /* DeInitialize Drivers */ /* Re-Initialize Drivers back for ECC508A default configuration*/ // Send the wake by writing to an address of 0x00 // wait tWHI + tWLO which is configured based on device type and configuration structure