/************************************************* Function: StorageGetParam Descroption: Input: 1.Index 2.Info 3.Data 4.THData Output: Return: Other: *************************************************/ INT8U StorageGetParam(INT8U Index, PINFO_PARAM Info, INT8U* Data, INT8U* THData) { unsigned int SubAddr = ADDRESS_DATA(Index); if(NULL == Info || NULL == Data || NULL == THData) { return FALSE; } I2cRead(I2C_ADDR_W, SubAddr, (char*)Info, sizeof(INFO_PARAM)); SubAddr += sizeof(INFO_PARAM); I2cRead(I2C_ADDR_W, SubAddr, (char*)Data, (Info->Num*16)); SubAddr += (Info->Num*16); I2cRead(I2C_ADDR_W, SubAddr, (char*)THData, (Info->Num)); return TRUE; }
/*--------------------------------------------------------------------------------------------------*/ unsigned char GLAM_EEPROMReadBytes(unsigned char reg_start, unsigned char bytes, unsigned char *buffer){ unsigned char data, i; signed char error=0; if( reg_start+bytes >128 ) return (2); cli(); error += I2cStartWait(); /* send I2C start with deley */ error += I2cWriteWait(GLAM_EEPROM_ADDRESS); /* send 24AA01 address byte */ error += I2cWriteWait(reg_start); /* write first address byte for miltiple read */ error += I2cStartWait(); /* send I2C start with deley */ error += I2cWriteWait(GLAM_EEPROM_ADDRESS|0x01);/* send 24AA01 address byte and ¸read bit */ for(i=0 ; i<bytes ; i++){ /* read number of bytes */ error += I2cRead(&data); /* read one byte */ *(buffer+i) = data; /* store one byte */ if((i+1) == bytes){ error += I2cSendNak(); /* send NAK to 24AA01 to finish read */ I2cStop(); /* send stop to 24AA01 */ break; /* break for */ } else error += I2cSendAck(); /* send ACK to 24AA01 slave to read next byte */ } sei(); if(error) /* error occurred? */ return (1); /* return error (1) -> communication failed */ return (0); /* return successfully */ }
int I2cSearch() { int i, ret; uint8_t send, recive; send = 0; //7bitアドレス printf(" "); for(i=0; i<0x10; i++) printf("%2X ", i); for(i=0; i<1<<8; i++) { if( i%0x10 == 0 ) printf("\n %2X ", i); I2cSetSlaveAddr(i); I2cWrite(&send, 1); ret = I2cRead(&recive, 1); if( ret != 0 ) { printf("-- "); continue; } printf("%2x ", i); //for(i=0; i<1; i++) // I2cDprintf("recive[%d] %d\n", i, recive[i]); DelayMicroSecond(100); } printf("\n"); }
uint32_t get_ir_sen(void) { uint32_t buf[10]; buf[0] = 0; I2cWrite(SENSOR_BUS, 0x90, buf, 2); return I2cRead(SENSOR_BUS, 0x90, 1); }
EN_RESULT CurrentMonitor_ReadConfig(uint16_t* pConfig) { uint16_t tempValue; return I2cRead(CURRENT_MONITOR_DEVICE_ADDRESS, CURRENT_MONITOR_REGISTER_ADDRESS_CONFIG, EI2cSubAddressMode_OneByte, 2, (uint8_t*)&tempValue); *pConfig = ChangeByteOrder(tempValue); }
EN_RESULT CurrentMonitor_ReadCalibration(uint16_t* pValue) { uint16_t tempValue; EN_RETURN_IF_FAILED(I2cRead(CURRENT_MONITOR_DEVICE_ADDRESS, CURRENT_MONITOR_REGISTER_ADDRESS_CALIBRATION, EI2cSubAddressMode_OneByte, 2, (uint8_t*)&tempValue)); *pValue = ChangeByteOrder(tempValue); return EN_SUCCESS; }
/************************************************* Function: StorageGetParamName Descroption: Input: 1.Index 2.Info Output: Return: Other: *************************************************/ INT8U StorageGetParamName(INT8U Index, PINFO_PARAM Info) { unsigned int SubAddr = ADDRESS_DATA(Index); if(NULL == Info) { return FALSE; } I2cRead(I2C_ADDR_W, SubAddr, (char*)Info, sizeof(INFO_PARAM)); return TRUE; }
EN_RESULT CurrentMonitor_ReadVSense(uint16_t* pVSense) { uint16_t tempValue; EN_RETURN_IF_FAILED(I2cRead(CURRENT_MONITOR_DEVICE_ADDRESS, CURRENT_MONITOR_REGISTER_ADDRESS_VSENS, EI2cSubAddressMode_OneByte, 2, (uint8_t*)&tempValue)); // uV. One ADC bit is 10 micro volt *pVSense = ChangeByteOrder(tempValue)*10; // in micro volt return EN_SUCCESS; }
void dev_I2C_Read(uint32_t chn,uint32_t chipType,uint8_t addr,uint8_t *value) { unsigned char r_value; if (chn > 1) { printf ("\r\n the I2C channel error ! channel = 0x%0x\n", chn); return; } I2cRead((unsigned char)chn, (unsigned char)chipType, (unsigned long)(addr & 0xFF), &r_value); *value = r_value; }
EN_RESULT CurrentMonitor_ReadVBus(uint16_t* pValue) { uint16_t tempValue; EN_RETURN_IF_FAILED(I2cRead(CURRENT_MONITOR_DEVICE_ADDRESS, CURRENT_MONITOR_REGISTER_ADDRESS_VBUS, EI2cSubAddressMode_OneByte, 2, (uint8_t*)&tempValue)); // mV. Calculate /2 to cut off the last bit *pValue = ChangeByteOrder(tempValue) / 2; return EN_SUCCESS; }
EN_RESULT CurrentMonitor_ReadPower(uint16_t* pValue) { uint16_t tempValue; EN_RETURN_IF_FAILED(I2cRead(CURRENT_MONITOR_DEVICE_ADDRESS, CURRENT_MONITOR_REGISTER_ADDRESS_POWER, EI2cSubAddressMode_OneByte, 2, (uint8_t*)&tempValue)); // mW. Calculate *20 because the Power LSB is 20*Current LSB *pValue = ChangeByteOrder(tempValue) * 20; return EN_SUCCESS; }
void i2cReadWrite(void) { DWORD slaveId = 0x98; BYTE byData; logMsg("i2cTest star\n", 0, 0, 0, 0, 0, 0); dev_I2cInit(); logMsg("i2cTest init\n", 0, 0, 0, 0, 0, 0); I2cWrite(0, slaveId, 0x09, 0x61); byData = 0; I2cRead(0, slaveId, 0x09, &byData); logMsg("i2cTest byData%d\n", byData, 0, 0, 0, 0, 0); }
/************************************************* Function: StorageInit Description: Input: Output: Return: Others: *************************************************/ void StorageInit(void) { INT8U Init = I2CRecvByte(I2C_ADDR_W,ADDRESS_INIT); if(Init == 0) { mSysParam.Init = 1; mSysParam.BeepEnable = 1; mSysParam.DataNum = 0; I2CWrite(I2C_ADDR_W, ADDRESS_INIT, (unsigned char*)(&mSysParam), sizeof(SYS_PARAM)); } else { I2cRead(I2C_ADDR_W, ADDRESS_INIT, (unsigned char*)(&mSysParam), sizeof(SYS_PARAM)); } StorageInitInfo(); }
/************************************************************* ** : RTCRead ** : read RTC ** : INPUT RTCAddr ** [BCD7] = sec,min,hour,day,mon,year,week ** ** : pmj ** : 2006-9-20 **************************************************************/ void RTCRead(unsigned char *RTCAddr) { unsigned char buff[7]; unsigned char buffr = 0x02; unsigned char err; buff[0] = 0; buff[1] = 0; buff[2] = 0; DISICNT = 0x3FFF; /* disable interrupts */ while(1) { if(I2cWrite(0xa2, &buff[0], 3) != 0) {error_i2();} else break; } while(1) { if(I2cRead(0xa2, buff, &buffr, 1, 7) != 0) {error_i2();} else break; } DISICNT = 0x0000; /* enable interrupts */ buff[4] = buff[4]&0x07; // if(buff[4] == 0) buff[4] = 0x07; buff[4] = buff[4] << 5; *RTCAddr = buff[0]&0x7f; //sec *(RTCAddr+1) = buff[1]&0x7f; //min *(RTCAddr+2) = buff[2]&0x3f; //hour *(RTCAddr+3) = buff[3]&0x3f; //day *(RTCAddr+4) = (buff[5]&0x1f)|buff[4]; //week-mon *(RTCAddr+5) = buff[6]; //year // soft rtc sys_date.year = *(RTCAddr+5); sys_date.month = *(RTCAddr+4); sys_date.day = *(RTCAddr+3); sys_date.hour = *(RTCAddr+2); sys_date.minute = *(RTCAddr+1); sys_date.second = *RTCAddr; softMin = b2h(*(RTCAddr+1)); softSec = b2h(*RTCAddr); softMS = 0; // soft rtc end }
int I2cTransfer(uint8_t *tbuf, unsigned int tlen, uint8_t *rbuf, unsigned int rlen) { int result; if( tbuf != NULL ) { result = I2cWrite(tbuf, tlen); if( result != 0 ) return result; } if( rbuf != NULL ) { result = I2cRead(rbuf, rlen); if( result != 0 ) return result; } return 0; }
/*函数名称:HwCtlI2cValue **函数参数: **函数功能:I2C读写操作汇总 **函数返回: **修 改 人: **修改日期: */ static void HwCtlI2cValue(i2ccfg_t *pi2c_cfg) { EnterCriticalSection(&gHwInfo.CriticalSectionHwI2c); switch (pi2c_cfg->flag) { case I2C_WRITE: I2cWrite(pi2c_cfg); break; case I2C_READ: I2cRead(pi2c_cfg); break; default: break; } LeaveCriticalSection(&gHwInfo.CriticalSectionHwI2c); }