/*FUNCTION********************************************************************** * * Function Name : I2C_DRV_SlaveInit * Description : initializes the I2C module. * This function will save the application callback info, turn on the clock of * I2C instance, setup according to user configuration. * *END**************************************************************************/ void I2C_DRV_SlaveInit(uint32_t instance, const i2c_slave_user_config_t * userConfigPtr, i2c_slave_state_t * slave) { assert(slave); assert(instance < HW_I2C_INSTANCE_COUNT); uint32_t baseAddr = g_i2cBaseAddr[instance]; /* Exit if current instance is already initialized. */ if (g_i2cStatePtr[instance]) { return; } /* Init driver instance structure */ memset(slave, 0, sizeof(i2c_slave_state_t)); slave->slaveListening = userConfigPtr->slaveListening; slave->slaveCallback = userConfigPtr->slaveCallback; slave->callbackParam = userConfigPtr->callbackParam; /* Enable clock for I2C.*/ CLOCK_SYS_EnableI2cClock(instance); /* Init instance to known state. */ I2C_HAL_Init(baseAddr); /* Set slave address.*/ I2C_HAL_SetAddress7bit(baseAddr, userConfigPtr->address); /* Save runtime structure pointer.*/ g_i2cStatePtr[instance] = slave; /* Create Event for irqSync */ OSA_EventCreate(&slave->irqEvent, kEventAutoClear); #if FSL_FEATURE_I2C_HAS_START_STOP_DETECT /* Enable I2C START&STOP signal detect interrupt in the peripheral.*/ if(userConfigPtr->startStopDetect) { I2C_HAL_SetStartStopIntCmd(baseAddr,true); } #endif #if FSL_FEATURE_I2C_HAS_STOP_DETECT /* Enable STOP signal detect interrupt in the peripheral.*/ if(userConfigPtr->stopDetect) { I2C_HAL_SetStopIntCmd(baseAddr,true); } #endif /* Enable I2C interrupt as default if setup slave listening mode */ I2C_HAL_SetIntCmd(baseAddr, slave->slaveListening); /* Enable I2C interrupt from NVIC */ INT_SYS_EnableIRQ(g_i2cIrqId[instance]); /* Enable the peripheral operation.*/ I2C_HAL_Enable(baseAddr); }
/*FUNCTION********************************************************************** * * Function Name : I2C_DRV_SlaveDeinit * Description : Shuts down the I2C slave driver. * This function will clear the control register and turn off the clock to the * module. * *END**************************************************************************/ void I2C_DRV_SlaveDeinit(uint32_t instance) { assert(instance < HW_I2C_INSTANCE_COUNT); uint32_t baseAddr = g_i2cBaseAddr[instance]; i2c_slave_state_t * i2cSlaveState = (i2c_slave_state_t *)g_i2cStatePtr[instance]; #if FSL_FEATURE_I2C_HAS_START_STOP_DETECT /* Disable I2C START&STOP signal detect interrupt in the peripheral.*/ I2C_HAL_SetStartStopIntCmd(baseAddr,false); #endif #if FSL_FEATURE_I2C_HAS_STOP_DETECT /* Disable STOP signal detect interrupt in the peripheral.*/ I2C_HAL_SetStopIntCmd(baseAddr,false); #endif /* Disable I2C interrupt. */ I2C_HAL_SetIntCmd(baseAddr, false); /* Turn off I2C.*/ I2C_HAL_Disable(baseAddr); /* Disable clock for I2C.*/ CLOCK_SYS_DisableI2cClock(instance); /* Disable I2C NVIC interrupt */ INT_SYS_DisableIRQ(g_i2cIrqId[instance]); /* Destroy sema. */ OSA_EventDestroy(&i2cSlaveState->irqEvent); /* Clear runtime structure poniter.*/ g_i2cStatePtr[instance] = NULL; }
/*FUNCTION********************************************************************** * * Function Name : I2C_DRV_SlaveDeinit * Description : Shuts down the I2C slave driver. * This function will clear the control register and turn off the clock to the * module. * *END*/ i2c_status_t I2C_DRV_SlaveDeinit(uint32_t instance) { assert(instance < I2C_INSTANCE_COUNT); /** Exit if current instance is already de-initialized or is gated.*/ if ((!g_i2cStatePtr[instance]) || (!CLOCK_SYS_GetI2cGateCmd(instance))) { return kStatus_I2C_Fail; } I2C_Type * base = g_i2cBase[instance]; i2c_slave_state_t * i2cSlaveState = (i2c_slave_state_t *)g_i2cStatePtr[instance]; #if FSL_FEATURE_I2C_HAS_START_STOP_DETECT /** Disable I2C START&STOP signal detect interrupt in the peripheral.*/ I2C_HAL_SetStartStopIntCmd(base,false); #endif #if FSL_FEATURE_I2C_HAS_STOP_DETECT /** Disable STOP signal detect interrupt in the peripheral.*/ I2C_HAL_SetStopIntCmd(base,false); #endif /** Disable I2C interrupt. */ I2C_HAL_SetIntCmd(base, false); /** Turn off I2C.*/ I2C_HAL_Disable(base); /** Disable clock for I2C.*/ CLOCK_SYS_DisableI2cClock(instance); /** Disable I2C NVIC interrupt */ INT_SYS_DisableIRQ(g_i2cIrqId[instance]); /** Destroy sema. */ OSA_EventDestroy(&i2cSlaveState->irqEvent); /** Clear runtime structure poniter.*/ g_i2cStatePtr[instance] = NULL; return kStatus_I2C_Success; }