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_WriteCalibration(uint16_t value) { uint16_t tempValue = ChangeByteOrder(value); return I2cWrite(CURRENT_MONITOR_DEVICE_ADDRESS, CURRENT_MONITOR_REGISTER_ADDRESS_CALIBRATION, EI2cSubAddressMode_OneByte, (uint8_t*)&tempValue, 2); }
/****************************************************** 函数描述: I2C 设备写操作函数 参数描述: chn: I2C 通道号, 系统使用两路I2C 通道0 为主控板 通道1 为远端 chipType: I2C 设备地址,I2C为共享总线,每一个总线上 的设备都有对应的地址 addr: 需要访问的寄存器偏移 value: 写入寄存器的值 函数返回值 无 *******************************************************/ void dev_I2C_Write(uint32_t chn,uint32_t chipType,uint8_t addr,uint8_t value) { if (chn > 1) { printf ("\r\n the I2C channel error ! channel = 0x%0x\n", chn); return; } I2cWrite((unsigned char)chn, (unsigned char)chipType, (unsigned long)(addr & 0xFF), (unsigned char)value); }
/*--------------------------------------------------------------------------------------------------*/ 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 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); }
/************************************************************* ** : 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); }
/************************************************************* ** : RTCWrite ** : set RTC ** : INPUT RTCAddr ** [BCD7] = sec,min,hour,day,mon,year,week ** ** : pmj ** : 2006-9-20 **************************************************************/ void RTCWrite(unsigned char *RTCAddr) { unsigned char buff[20]; unsigned char i,bbc[2]; unsigned char err; #if 0 // soft rtc start 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); #endif #if 1 sys_date.year = 0x08; sys_date.month = 0xb0; sys_date.day = 0x17; sys_date.hour = 0x16; sys_date.minute = 0x35; sys_date.second = 0x16; softMin = b2h(0x35); softSec = b2h(0x16); #endif softMS = 0; // soft rtc end buff[0] = 0; buff[2] = 0; buff[1] = 0x20; #if 0 for(i = 0; i < 4; i++) { buff[i+3] = *(RTCAddr+i); } buff[7] = (*(RTCAddr+4)) >> 5; if(buff[7] == 7) buff[7] = 0; buff[8] = (*(RTCAddr+4))&0x1f; buff[9] = *(RTCAddr+5); #endif #if 1 buff[3] = _time_chk.sec;//0x18; buff[4] = _time_chk.min;//0x35; // 分 buff[5] = _time_chk.hr;//0x16; //16时 buff[6] = _time_chk.day;//0x31; //31日 buff[7] = _time_chk.wkd;//(0x72) >> 5; // 星期三 // if(buff[7] == 7) buff[7] = 0; buff[8] = _time_chk.mth;//0x72&0x1f; // 12月 buff[9] = _time_chk.yr;//0x08;// year #else buff[3] = 0x18; buff[4] = 0x35; // 分 buff[5] = 0x16; //16时 buff[6] = 0x31; //31日 buff[7] = (0x72) >> 5; // 星期三 if(buff[7] == 7) buff[7] = 0; buff[8] = 0x72&0x1f; // 12月 buff[9] = 0x08; // year #endif for(i = 10; i < 14; i++) { buff[i] = 0x80; } buff[14] = 0x80; buff[15] = 0; buff[16] = 0x00; while(1) { if(I2cWrite(0xa2, &buff[0], 17) != 0) {error_i2();} else break; } buff[0] = 0; buff[1] = 0; buff[2] = 0; while(1) { if(I2cWrite(0xa2, &buff[0], 3) != 0) {error_i2();} else break; } }