/** * @brief This function send device address and register address to I2C slave. * @param DeviceAddr: I2C slave address * @param RegAddr: slave device's register address, usually before read/write slave device's register, * i2c master should first send it. * @return * @arg TRUE: success * @arg FALSE:fail */ bool I2cSendAddr(void* I2cMasterHandle, uint8_t SlaveAddr, uint8_t RegAddr, uint8_t RwFlag) { I2cStart(I2cMasterHandle); if(!I2cWriteByte(I2cMasterHandle, SlaveAddr)) { /* Retry */ I2cStart(I2cMasterHandle); if(!I2cWriteByte(I2cMasterHandle, SlaveAddr)) { I2cStop(I2cMasterHandle); return FALSE; } } if(!I2cWriteByte(I2cMasterHandle, RegAddr)) { I2cStop(I2cMasterHandle); return FALSE; } if(RwFlag == IIC_READ) { I2cStart(I2cMasterHandle); if(!I2cWriteByte(I2cMasterHandle, SlaveAddr | IIC_READ)) { I2cStop(I2cMasterHandle); return FALSE; } } return TRUE; }
void i2c_send(uint8_t sla, uint8_t _twi_len, bool_t* finished) { i2c_debug = 0x32; twi_len = _twi_len; twi_sla = I2C_TRANSMIT | sla; twi_end = finished; I2cStart(); }
uint32_t I2cEngine(uint8_t module) { i2c_bus[module].master_state = I2C_IDLE; i2c_bus[module].read_index = 0; i2c_bus[module].write_index = 0; if(I2cStart(module) != TRUE) { I2cStop(module); return (FALSE); } while(i2c_bus[module].master_state != DATA_NACK) { continue; } I2cStop(module); return (TRUE); }
/*--------------------------------------------------------------------------------------------------*/ unsigned char GLAM_EEPROMWriteBytes(unsigned char reg_start, unsigned char bytes, unsigned char *buffer){ unsigned char i; signed char error=0; if( reg_start+bytes >120) return (2); /* it is not allowed modify configuration data */ cli(); error += I2cStart(); /* send I2C start with deley */ error += I2cWrite(GLAM_EEPROM_ADDRESS); /* send 24AA01 address byte and write bit */ error += I2cWrite(reg_start); /* write first address byte for miltiple read */ for(i=0 ; i<bytes ; i++){ /* read number of bytes */ error += I2cWrite(*(buffer+i)); /* read one byte */ if((i+1) == bytes){ I2cStop(); /* send stop to 24AA01 */ break; /* break for */ } } sei(); if(error) /* error occurred? */ return (1); /* return error (1) -> communication failed */ return (0); /* return successfully */ }
void i2c_get(uint8_t sla, uint8_t _twi_len, bool_t* finished) { twi_len = _twi_len; twi_sla = I2C_RECEIVE | sla; twi_end = finished; I2cStart(); }
/*============================================================================*/ GERR I2cReadWrite(BOOL mode, BYTE ChipAddress, BYTE *Data, DWORD NbData) { #if 0 BYTE i=0, ack; I2cStart(); /* Start the I2C sequence */ I2cByteWrite( (BYTE)(ChipAddress+(mode==READ)) ); /* Send Slave chip address */ ack = I2cAckGet(); /* Read Acknowledge bit */ while((i < NbData) && ack) { if(mode==READ) { Data[i] = I2cByteRead(); /* Read data from the slave*/ I2cAckSet(i==(NbData-1)); /* Write Acknowledge bit */ } else { I2cByteWrite(Data[i]); /* Write data to the salve */ ack=I2cAckGet(); /* Read Acknowledge bit */ } i++; } I2cStop(); /* Stop the I2C sequence */ if(!ack) { if (mode==READ) for (i=0; i<NbData; i++) Data[i]=0xFF; return -1; } else return 0; #else GERR ferr; char readData[4]; char data[2]; #ifdef PN2020WT_2 if (mode == READ) { ferr = GD_I2C_Read(&pData->handleI2C, ChipAddress, readReg, 1, Data, NbData); if (ferr != GD_OK) { return GD_ERR_FE_I2C; } } else { ferr = GD_I2C_Write(&pData->handleI2C, ChipAddress, Data, NbData); if (ferr != GD_OK) { return GD_ERR_FE_I2C; } readReg[0] = Data[0]; } return GD_OK; #else if (mode == READ) { ferr = GD_I2C_Read(&pgData->handleI2C, ChipAddress, readReg, 1, Data, NbData); if (ferr != GD_OK) { return GD_ERR_FE_I2C; } } else { ferr = GD_I2C_Write(&pgData->handleI2C, ChipAddress, Data, NbData); if (ferr != GD_OK) { return GD_ERR_FE_I2C; } readReg[0] = Data[0]; } return GD_OK; #endif #endif }