in shakti/bsp/drivers/i2c/i2c_driver.c [68:144]
int config_i2c(i2c_struct * instance, unsigned char prescale_div, unsigned char scl_div)
{
unsigned char temp = 0;
log_debug("\tI2C: Initializing the Controller\n");
if(prescale_div != instance->prescale )
{
instance->prescale = prescale_div;
#ifdef DEBUG
temp = instance->prescale;
if((temp | 0x00) != prescale_div)
{
log_error("\t Failed to write Prescale division Written Value: 0x%x; read Value: 0x%x\n", prescale_div, instance->prescale);
return -ENXIO;
}
else
{
log_debug("\tPrescaler successfully initalized\n");
}
#endif
}
if(scl_div != instance->scl )
{
instance->scl = scl_div; //Setting the I2C clock value to be 1, which will set the clock for module and prescaler clock
#ifdef DEBUG
temp = instance->scl;
/* Just reading the written value to see if all is well -- Compiler should not optimize this load!!! Compiler can just optimize the store to pointer address followed by load pointer to a register to just an immediate load to the register since clock register is not used anywhere -- but the purpose is lost. Don't give compiler optimizations */
if((temp | 0x00) != scl_div)
{
log_error("\tClock initialization failed Write Value: 0x%x; read Value: 0x%x\n", scl_div, temp);
return -ENXIO;
}
else
{
log_debug("\tClock successfully initalized\n");
}
#endif
}
/* S1=0x80 S0 selected, serial interface off */
log_debug("\tClearing the status register. \n");
instance->control = I2C_PIN;
// Reading set control Register Value to ensure sanctity
log_debug("\tReading Status Register \n");
temp = instance->control;
//Check whether the status register is cleared or not.
if((temp & 0x7f) != 0){
log_error("\tDevice Not Recognized\n");
return -ENXIO;
}
log_debug("\tWaiting for a specified time\n ");
waitfor(900); //1 Second software wait -- Should be 900000 but setting to 900 now since simulation is already slow
log_debug("\tDone Waiting \n ");
log_info("\nControl: %x; Status: %x", instance->control, instance->status);
/* Enable Serial Interface */
instance->control = I2C_IDLE;
waitfor(900); //1 Second software wait -- Should be 900000 but setting to 900 now since simulation is already slow
temp = instance->status;
/* Check to see if I2C is really in Idle and see if we can access the status register -- If not something wrong in initialization. This also verifies if Control is properly written since zero bit will be initialized to zero*/
if(temp != (I2C_PIN | I2C_BB)){
log_error("\n\tInitialization failed; Status Reg: %x\n", temp);
return -ENXIO;
}
log_info("\tI2C Initialization success\n");
return 0;
}