bool i2c_data_verify(alt_u32 scl_base, alt_u32 sda_base, alt_u8 ControlAddr){ bool bPass; const alt_8 DeviceAddr = 0xA0; alt_u8 OrgData, TestData, Data; TestData = alt_nticks(); if (TestData == 0) TestData = 0x12; bPass = I2C_Read(scl_base, sda_base, DeviceAddr, ControlAddr, &OrgData); if (bPass) // write bPass = I2C_Write(scl_base, sda_base, DeviceAddr, ControlAddr, TestData); if (bPass) // read bPass = I2C_Read(scl_base, sda_base, DeviceAddr, ControlAddr, &Data); if (bPass && (Data != TestData)) // verify bPass = FALSE; // restore if (bPass) // write back bPass = I2C_Write(scl_base, sda_base, DeviceAddr, ControlAddr, OrgData); if (bPass) // read bPass = I2C_Read(scl_base, sda_base, DeviceAddr, ControlAddr, &Data); if (bPass && (Data != OrgData)) // verify bPass = FALSE; return bPass; }
/******************************************************************************* * @brief mpu6050 Tx. * @param DestAddr: To * @param pData: The data Tx * @param DataLen: The number of data need to Tx * @retval Ref to system.h - StatusTypeDef. */ void Mpu6050ReadTemp(unsigned char DevID, char *pTemp) { unsigned char buf[2] = {0, 0}; I2C_Read(DevID, TEMP_OUT_L, 1, &buf[0]); I2C_Read(DevID, TEMP_OUT_H, 1, &buf[1]); *pTemp = (*(short *)buf) / 340 + 36.53; }
/****************************************************************************** ** Function name: EE_Read ** ** Description: Reads a word from EEPROM (Uses I2CRead) ** ** Parameters: Address to read from ** Returned value: Data at address ** ******************************************************************************/ uint32_t EE_Read (uint16_t _EEadd) { uint32_t retDATA = 0; retDATA = I2C_Read(_EEadd+3); retDATA = (retDATA << 8) + I2C_Read(_EEadd+2); retDATA = (retDATA << 8) + I2C_Read(_EEadd+1); retDATA = (retDATA << 8) + I2C_Read(_EEadd+0); return retDATA; }
uint8_t MPU9050_Read(mpu9050_t *mpu9050) { //const TickType_t xTickToWait = 10; uint8_t status; I2C_Read(MPUSLVADDR, INT_STATUS, &status, 1); I2C_Read(MPUSLVADDR, ACCEL_XOUT_H, (uint8_t *)&mpu9050->accel.x, sizeof(triaxial_t)); I2C_Read(MPUSLVADDR, GYRO_XOUT_H, (uint8_t *)&mpu9050->gyro.x, sizeof(triaxial_t)); return status; }
uint8_t HMC_Read(HMC_Data_t *hmc_data) { uint8_t DataBuf[6]; float tmp, r; I2C_Read(HMC_SLV_ADDR, DOUT_X_MSB, DataBuf, 6); hmc_data->direct.x = (DataBuf[0] << 8 | DataBuf[1]); hmc_data->direct.y = (DataBuf[2] << 8 | DataBuf[3]); hmc_data->direct.z = (DataBuf[4] << 8 | DataBuf[5]); r = sqrt((double)hmc_data->direct.x*(double)hmc_data->direct.x +(double)hmc_data->direct.y*(double)hmc_data->direct.y +(double)hmc_data->direct.z*(double)hmc_data->direct.z); tmp = (double)hmc_data->direct.x/r; tmp = acos(tmp); hmc_data->angle.x = tmp*180.0/3.1415926; tmp = (double)hmc_data->direct.y/r; tmp = acos(tmp); hmc_data->angle.y = tmp*180.0/3.1415926; tmp = (double)hmc_data->direct.z/r; tmp = acos(tmp); hmc_data->angle.z = tmp*180.0/3.1415926; return pdTRUE; }
// Generic function to read a value from a single ADXL345 register uint8_t readADXL345Register (uint8_t reg, uint8_t *valp) { uint8_t r; r = I2C_Start(); if (r == TW_START) { // outputStringToUART0("\n\r setADXL345Register: about to write address \n\r"); r = I2C_Write(ADXL345_ADDR_WRITE); // address the device, say we are going to write if (r == TW_MT_SLA_ACK) { // outputStringToUART0("\n\r setADXL345Register: about to write data \n\r"); r = I2C_Write(reg); // tell the device the register we are going to want if (r == TW_MT_DATA_ACK) { r = I2C_Start(); // restart, preparatory to reading r = I2C_Write(ADXL345_ADDR_READ); // address the device, say we are going to read // outputStringToUART0("\n\r setADXL345Register: about to read value \n\r"); *valp = I2C_Read(0); // do NACK, since this is the last and only byte // r = I2C_Write(val); // set the value // outputStringToUART0("\n\r setADXL345Register: about to Stop \n\r"); I2C_Stop(); // outputStringToUART0("\n\r setADXL345Register: Stop completed \n\r"); return I2C_OK; } else { // could not write data to device I2C_Stop(); return errNoI2CDataAck; } } else { // could not address device I2C_Stop(); return errNoI2CAddressAck; } } else { // could not START return errNoI2CStart; } };
unsigned char BusyXLCD(void) { OLATB_curr |= 0xC0; // RW and RS are on clockLCDLow(); I2C_Write(EXPANDER_IODIRB, 0x1E); // data bus is now an input I2C_Write(EXPANDER_OLATB, OLATB_curr); DelayFor18TCY(); clockLCDHigh(); // Clock in the command with E DelayFor18TCY(); busy = I2C_Read(EXPANDER_GPIOB); clockLCDLow(); // Reset clock line DelayFor18TCY(); clockLCDHigh(); // Clock out other nibble DelayFor18TCY(); clockLCDLow(); OLATB_curr &= 0xBF; // Reset control line I2C_Write(EXPANDER_OLATB, OLATB_curr); I2C_Write(EXPANDER_IODIRB, 0x00); // set data bus back to output // busy bit is high? if (busy & 0x02) { return 1; } // Busy bit is low return 0; }
/* Read GPIO Digital Inputs */ void ReadGPIO(void) { int fd; unsigned char buff[5]; unsigned char data[5]; buff[0]=0x03; data[0]=0x00; /* Open I2C-BUS */ I2C_Open(&fd, 0x21); /* Write register */ I2C_Send(&fd, buff,1 ); /* Read the ADC */ I2C_Read(&fd, data, 1); printf("GPIO: 0x%02x\n", data[0]); /* Close I2C-BUS */ I2C_Close(&fd); }
/* Read Board Firmware Version */ void ReadSV(void) { int fd; unsigned char buff[5]; unsigned char data[5]; buff[0]=0x21; data[0]=0x00; /* Open I2C-BUS */ I2C_Open(&fd, 0x21); /* Write register */ I2C_Send(&fd, buff,1 ); /* Read the ADC */ I2C_Read(&fd, data, 1); printf("Firmware Version: %d.0.%2d\n", data[0]>>4,data[0]&0x0f); /* Close I2C-BUS */ I2C_Close(&fd); }
/* Read Board ID */ void ReadID(void) { int fd; unsigned char buff[5]; unsigned char data[5]; buff[0]=0x20; data[0]=0x00; /* Open I2C-BUS */ I2C_Open(&fd, 0x21); /* Write register */ I2C_Send(&fd, buff,1 ); /* Read ID */ I2C_Read(&fd, data, 1); printf("ID: 0x%x\n", data[0]); /* Close I2C-BUS */ I2C_Close(&fd); }
//--------------- Reads data from device - single location char MAX44000_ReadRegister(char rAddr) { tmp_data[0] = rAddr; I2C_Start(); // issue I2C start signal I2C_Write(MAX44000_I2C_Adr,tmp_data,1,_I2C_END_MODE_RESTART); I2C_Read (MAX44000_I2C_Adr,tmp_data,1,_I2C_END_MODE_STOP); return tmp_data[0]; }
/*---------------------------------------------------------------------------* * Routine: Temperature_Get *---------------------------------------------------------------------------* * Description: * Read the value of the ADT7420 and return the temperature in Celcius. * Inputs: * void * Outputs: * uint16_t -- temperature with 4 bits of fraction and 12 bits of * integer. *---------------------------------------------------------------------------*/ uint16_t Temperature_Get(void) { uint8_t target_reg; uint8_t target_data[2] = {0x00, 0x00}; uint16_t temp = 0; uint32_t timeout = MSTimerGet(); I2C_Request r; r.iAddr = ADT7420_ADDR>>1; r.iSpeed = 100; r.iWriteData = &target_reg; r.iWriteLength = 1; r.iReadData = target_data; r.iReadLength = 2; I2C_Write(&r, 0); while ((I2C_IsBusy()) && (MSTimerDelta(timeout) < 10)) {} I2C_Read(&r, 0); while ((I2C_IsBusy()) && (MSTimerDelta(timeout) < 10)) {} /* Convert the device measurement into a decimal number and insert into a temporary string to be displayed */ temp = (target_data[0] << 8) + target_data[1]; // temp = temp >> 3; return temp; }
bool MPL3115A2_readBytes_(uint8_t address, uint8_t subAddress, uint8_t count, uint8_t * dest, bool loop) { int i=0; for(;i<5;i++) { if(I2C_Write(address, (unsigned char*)&subAddress, 1, 0)) break; // if written length > 0 if (!loop) break; } if (i>=5) return false; for(i=0;i<5;i++) { if(I2C_Read(address, dest, count, 1)) break; // if read length > 0 if (!loop) break; } if (i<5) return true; return false; }
/*---------------------------------------------------------------------------* * Routine: EEPROM_Seq_Read *---------------------------------------------------------------------------* * Description: * Read the value of the address and return the data . * Inputs: * void * Outputs: * uint16_t -- temperature with 4 bits of fraction and 12 bits of * integer. *---------------------------------------------------------------------------*/ int16_t EEPROM_Seq_Read(uint16_t addr,uint8_t *pdata, uint16_t r_lenth) { uint8_t target_address[2]; uint32_t timeout = MSTimerGet(); I2C_Request r; int16_t result = 0; target_address[0] = addr & 0xFF00; target_address[1] = addr & 0x00FF; r.iAddr = EEPROM_ADDR >> 1; r.iSpeed = 100; r.iWriteData = target_address; r.iWriteLength = 2; r.iReadData = pdata; r.iReadLength = r_lenth; I2C_Write(&r, 0); while ((I2C_IsBusy()) && (MSTimerDelta(timeout) < 10)) {} I2C_Read(&r, 0); while ((I2C_IsBusy()) && (MSTimerDelta(timeout) < 10)) {} result = 1; return result; }
/** * @brief Reads from gyro * @param Pointer to data structure * @retval I2C success/error code */ I2C_Returntype Gyr_Read(Vector* a) { //This uses the pointer looparound I2C_Returntype r=I2C_Read((uint8_t*)a,6,GYR_ADD,GYR_DATA); Flipbytes(&(a->x)); //Fixed the swapped endianess Flipbytes(&(a->y)); Flipbytes(&(a->z)); return r; }
/*---------------------------------------------------------------------------* * Routine: Accelerometer_Get *---------------------------------------------------------------------------* * Description: * Read the value of the ADT7420 and return the LightSensor in Lux. * Inputs: * void * Outputs: * uint16_t -- LightSensor with 4 bits of fraction and 12 bits of * integer. *---------------------------------------------------------------------------*/ int16_t *Accelerometer_Get(void) { uint8_t target_reg, acc_axis; uint8_t target_data[2] = {0x00, 0x00}; uint32_t timeout = MSTimerGet(); I2C_Request r; //Accelerometer_Init(); for(acc_axis=0; acc_axis<3; acc_axis++) { target_reg = acc_reg_addr[acc_axis]; r.iAddr = ACCEL_ADDR>>1; r.iSpeed = 100; r.iWriteData = &target_reg; r.iWriteLength = 1; r.iReadData = target_data; r.iReadLength = 2; I2C_Write(&r, 0); while ((I2C_IsBusy()) && (MSTimerDelta(timeout) < 10)) {} I2C_Read(&r, 0); while ((I2C_IsBusy()) && (MSTimerDelta(timeout) < 10)) {} /* Convert the device measurement into a decimal number and insert into a temporary string to be displayed */ gAccData[acc_axis] = (target_data[1] << 8) + target_data[0]; } return gAccData; }
/******************************************************************************* * Outline : EEPROM_Read * Description : This function writes the given contents to the * EEPROM, at the given location. * Argument : offset -- Offset byte from start of EEPROM * aData -- Pointer to bytes to write to EEPROM * aSize -- number of bytes to write to EEPROM * Return value : 0 = success, else failure *******************************************************************************/ uint8_t EEPROM_Read(uint16_t offset, uint8_t *aData, uint16_t aSize) { uint8_t writeData[2]; uint32_t timeout = MSTimerGet(); I2C_Request r; writeData[0] = (uint8_t)offset<<8; writeData[1] = (uint8_t)offset; r.iAddr = EEPROM_ADDR>>1; r.iSpeed = 100; r.iWriteData = writeData; r.iWriteLength = 2; r.iReadData = aData; r.iReadLength = aSize; I2C_Start(); I2C_Write(&r, 0); while ((I2C_IsBusy()) && (MSTimerDelta(timeout) < EEPROM_TIMEOUT)) {} I2C_Read(&r, 0); while ((I2C_IsBusy()) && (MSTimerDelta(timeout) < EEPROM_TIMEOUT)) {} return 0; }
/** * @brief Main program * @param None * @retval None */ int main() { int i; /*System clock configuration*/ SystemInit(); /* Configure UART2 */ S_UART_Init(115200); conf.scl = I2C_PA_9; conf.sda = I2C_PA_10; I2C_Init(&conf); //============ Write ============== I2C_Write(&conf, 0xa0, &Transmit_Data[0], MAX_SIZE); delay_function(4000); //========= Read ============= //Write memory address I2C_Write(&conf, 0xA0, &Transmit_Data[0], 1); delay_function(4000); //Read data I2C_Read(&conf, 0xA0, &Recv_Data[0], MAX_SIZE - 1); printf("Recv data : "); for(i=0; i<MAX_SIZE - 1; i++) { printf("0x%x ", Recv_Data[i]); } printf("\r\n"); }
uint8_t MPL3115A2_read8(uint8_t a) { int i=0; uint8_t out; for(;i<5;i++) { if(I2C_Write(MPL3115A2_ADDRESS, (unsigned char*)&a, 1, 0)) break; // if written length > 0 } if (i>=5) return 0xff; // failed for(i=0;i<5;i++) { if(I2C_Read(MPL3115A2_ADDRESS, &out, 1, 0)) break; // if read length > 0 } if (i<5) return out; return 0xff; }
/* Use this function to find module i2c address */ void BusScan(void) { int fd; unsigned char buff[5]; unsigned char data[5]; unsigned char adrs; for (adrs = 1; adrs < 129; adrs++) { buff[0]=0x20; data[0]=0x00; /* Open I2C-BUS */ I2C_Open(&fd, adrs); if (1) { /* Write register */ I2C_Send(&fd, buff,1 ); /* Read the ADC */ I2C_Read(&fd, data, 1); if (data[0]==0x23) printf("Found with I2c address 0x%02x \n", adrs); /* Close I2C-BUS */ I2C_Close(&fd); } } }
uint8_t HMC_GetStatus(void) { uint8_t status; I2C_Read(HMC_SLV_ADDR, HMC_STATUS, &status, 1); return status; }
/****************************************************************************** * @brief Single Read a Register. * * @param reg - Register Address. * * @return rxBuffer[0] - Read Data. ******************************************************************************/ int AD7156_ReadReg(int reg) { unsigned char rxBuffer[1] = {0x00}; I2C_Read(I2C_BASEADDR, AD7156_I2C_ADDR, reg, 1, rxBuffer); return(rxBuffer[0]); }
/* Get a temperatire reading over I2C from the TC74 sensor */ uint8_t GetTemp(void){ uint8_t temp; I2C_Start(); I2C_Write( 0x91 ); temp = I2C_Read( 0x01); I2C_Stop(); return temp; }
/* Set Factory I2C device address */ void Set_Factory(void){ int fd; unsigned char buff[5]; unsigned char data[5]; unsigned char adrs; for (adrs = 0; adrs < 129; adrs++) { buff[0]=0x20; data[0]=0x00; /* Open I2C-BUS */ I2C_Open(&fd, adrs); if (1) { /* Write register */ I2C_Send(&fd, buff,1 ); /* Read the ADC */ I2C_Read(&fd, data, 1); if (data[0]==0x23) { /* Close I2C-BUS */ I2C_Close(&fd); printf("Found with I2c address 0x%02x \n", adrs); buff[0]=0xF0; buff[1]=0x21; /* value: * New Address to be setted * / /* Open I2C-BUS */ I2C_Open(&fd, adrs); /* Write register */ I2C_Send(&fd, buff,2 ); /* Close I2C-BUS */ I2C_Close(&fd); printf("Reverted to 0x21\n"); } /* Close I2C-BUS */ I2C_Close(&fd); } } }
/*************************************************************************************************** void RTC_GetTime(uint8_t *ptr_hour_u8,uint8_t *ptr_min_u8,uint8_t *ptr_sec_u8) **************************************************************************************************** * I/P Arguments: uint8_t *,uint8_t *,uint8_t *-->pointers to get the hh,mm,ss. * Return value : none * description :This function is used to get the Time(hh,mm,ss) from Ds1307 RTC. Note: The time read from Ds1307 will be of BCD format, like 0x12,0x39,0x26 for 12hr,39min and 26sec. ***************************************************************************************************/ void RTC_GetTime(uint8_t *ptr_hour_u8,uint8_t *ptr_min_u8,uint8_t *ptr_sec_u8) { I2C_Start(); // Start I2C communication I2C_Write(C_Ds1307WriteMode_U8); // connect to DS1307 by sending its ID on I2c Bus I2C_Write(C_Ds1307SecondRegAddress_U8); // Request Sec RAM address at 00H I2C_Stop(); // Stop I2C communication after selecting Sec Register I2C_Start(); // Start I2C communication I2C_Write(C_Ds1307ReadMode_U8); // connect to DS1307(Read mode) by sending its ID *ptr_sec_u8 = I2C_Read(1); // read second and return Positive ACK *ptr_min_u8 = I2C_Read(1); // read minute and return Positive ACK *ptr_hour_u8 = I2C_Read(0); // read hour and return Negative/No ACK I2C_Stop(); // Stop I2C communication after reading the Time }
float readAuxBatteryVoltage ( void ) { I2C_Init(); // We bit-bang an I2C master on a couple pins from the programming port I2C_Start(); I2C_Write(0x6C); // Write address of device I2C_Write(0x02); // register 2 is VCELL, which is the voltage of the cell I2C_Stop(); I2C_Start(); I2C_Write(0x6D); // Read address of device // Read VCELL register uint8_t highByte = I2C_Read(1); uint8_t lowByte = I2C_Read(0); // only the high nibble of the low byte is used I2C_Stop(); // printf("High/Low: %d/%d\n", highByte, lowByte); uint16_t total = ((uint16_t)highByte * 16) + (lowByte / 16); // shift the high byte up 4 bits, and the low byte down 4 bits float voltage = (float)total * 0.00125; // the total is in units of 1.25 mV, so convert it to volts // printf("Total: %d\n", total); return voltage; }
//----------------------------------------------------------- void I2C_Read_Buf(uint8_t address_mem, uint8_t* data, uint8_t data_len) { uint8_t i=0; for(i=0;i<data_len;i++) { data[i]=I2C_Read(TMR_ADDR,address_mem+i); _delay_us(100); } }
void DS1307_GetTime(unsigned char *h_ptr,unsigned char *m_ptr,unsigned char *s_ptr) { I2C_Start(); // Start I2C communication I2C_Write(DS1307_ID); // connect to DS1307 by sending its ID on I2c Bus I2C_Write(SEC_ADDRESS); // Request Sec RAM address at 00H I2C_Stop(); // Stop I2C communication after selecting Sec Register I2C_Start(); // Start I2C communication I2C_Write(0xD1); // connect to DS1307( under Read mode) //by sending its ID on I2c Bus *s_ptr = I2C_Read(1); // read second and return Positive ACK *m_ptr = I2C_Read(1); // read minute and return Positive ACK *h_ptr = I2C_Read(0); // read hour and return Negative/No ACK I2C_Stop(); // Stop I2C communication after reading the Time }
/**************************************************************************//** * @brief Reads data from the selected EEPROM device * * @param eepromAddr - I2C address of the EEPROM device * @param pData - Buffer to store the read data * @param size - Number of bytes to read * * @return Returns -1 in case of error, 0 for success ******************************************************************************/ int32_t EEPROM_Read(uint8_t i2cAddr, uint8_t eepromAddr, uint8_t* pData, uint16_t size) { uint32_t ret; ret = I2C_Read(i2cAddr, eepromAddr, size, pData); return (size != ret ? -1 : 0); }
/*************************************************************************************************** void RTC_GetDate(uint8_t *ptr_day_u8,uint8_t *ptr_month_u8,uint8_t *ptr_year_u8) **************************************************************************************************** * I/P Arguments: uint8_t *,uint8_t *,uint8_t *-->pointers to get the y,m,d. * Return value : none * description :This function is used to get the Date(d,m,y) from Ds1307 RTC. Note: The date read from Ds1307 will be of BCD format, like 0x15,0x08,0x47 for 15th day,8th month and 47th year. ***************************************************************************************************/ void RTC_GetDate(uint8_t *ptr_day_u8,uint8_t *ptr_month_u8,uint8_t *ptr_year_u8) { I2C_Start(); // Start I2C communication I2C_Write(C_Ds1307WriteMode_U8); // connect to DS1307 by sending its ID on I2c Bus I2C_Write(C_Ds1307DateRegAddress_U8); // Request DAY RAM address at 04H I2C_Stop(); // Stop I2C communication after selecting DAY Register I2C_Start(); // Start I2C communication I2C_Write(C_Ds1307ReadMode_U8); // connect to DS1307(Read mode) by sending its ID *ptr_day_u8 = I2C_Read(1); // read Day and return Positive ACK *ptr_month_u8 = I2C_Read(1); // read Month and return Positive ACK *ptr_year_u8 = I2C_Read(0); // read Year and return Negative/No ACK I2C_Stop(); // Stop I2C communication after reading the Date }