static void ICACHE_FLASH_ATTR tcn75_read_all(void *arg) { int i; uint8 dtmp1, dtmp2; // set in one shot mode i2c_init(); for (i=0; i < 8; i++) { uint8_t addr = TCN_BASE_ADDR | i; if (!(present&(1<<i))) { continue; } i2c_start(); i2c_writeByte(addr << 1); if (!i2c_check_ack()) { i2c_stop(); data[i] |= INVALID_READING; continue; } i2c_writeByte(TCN_TA); if (!i2c_check_ack()) { i2c_stop(); data[i] |= INVALID_READING; continue; } i2c_stop(); i2c_start(); // now read i2c_writeByte((addr<<1)|1); // i2c read if (!i2c_check_ack()) { i2c_stop(); data[i] |= INVALID_READING; continue; } dtmp1=i2c_readByte(); //read MSB i2c_send_ack(1); dtmp2 = i2c_readByte(); //read LSB i2c_send_ack(0); //NACK READY FOR STOP data[i]= ((dtmp1<<8)|dtmp2); i2c_stop(); } }
uint16_t I2C_ReadADC(uint8_t write_addr, uint8_t read_addr, uint8_t channel) { uint8_t a2d_val_high; uint8_t a2d_val_low; int ack; lockset(lock); ack = i2c_startpoll(&i2c_bus, write_addr); ack = i2c_writeByte(&i2c_bus, ADC_CHANNEL[channel]); ack = i2c_startpoll(&i2c_bus, read_addr); a2d_val_high = i2c_readByte(&i2c_bus, 0); a2d_val_low = i2c_readByte(&i2c_bus, 1); i2c_stop(&i2c_bus); lockclr(lock); return (((uint16_t) a2d_val_high) << 8) | (uint16_t) a2d_val_low; }
/****************************************************************************** * FunctionName : user_mvh3004_burst_read * Description : burst read mvh3004's internal data * Parameters : uint8 addr - mvh3004's address * uint8 *pData - data point to put read data * uint16 len - read length * Returns : bool - true or false *******************************************************************************/ LOCAL bool ICACHE_FLASH_ATTR peri_iaq_single_burst_read(uint8 addr, uint8 *pData, uint16 len) { uint8 ack; uint16 i; i2c_start(); i2c_writeByte(addr); ack = i2c_check_ack(); PRINTF("the first ack is:%d\n",ack); if (ack==0) { os_printf("addr1 not ack when tx write cmd \n"); i2c_stop(); return false; } i2c_writeByte(0x52); ack = i2c_check_ack(); PRINTF("the second ack is:%d\n",ack); if (ack==0) { os_printf("not ack when write 0x52 \n"); i2c_stop(); return false; } i2c_start(); i2c_writeByte(addr + 1); ack = i2c_check_ack(); PRINTF("the third ack is:%d\n",ack); if (ack==0) { os_printf("addr2 not ack when tx write cmd \n"); i2c_stop(); return false; } os_delay_us(1); for (i = 0; i < len; i++) { pData[i] = i2c_readByte(); if(i==3) i2c_send_ack(0); else i2c_send_ack(1); os_delay_us(1); PRINTF("bytes_readed:%d\n", pData[i]); } i2c_stop(); return true; }
unsigned short read_value(i2c* bus, int address, int regAddr, int sb) { i2c_start(bus); i2c_writeByte(bus, address); i2c_writeByte(bus, regAddr); i2c_start(bus); i2c_writeByte(bus, address+1); char b1 = i2c_readByte(bus, 0); char b2 = i2c_readByte(bus, 1); unsigned short val; if (sb == 0) val = combine(b1, b2); else val = combine(b2, b1); i2c_stop(bus); return val; }
/* Function sht21_read_raw_value * * */ uint16_t ICACHE_FLASH_ATTR sht21_read_raw(uint8_t reg) { if(!sht21_writeCommand(SHT21_ADDR_W, reg)) return 999; os_delay_us(20); sht21_readCommand(SHT21_ADDR_R); uint8_t msb = i2c_readByte(); i2c_send_ack(1); uint8_t lsb = i2c_readByte(); i2c_send_ack(0); i2c_stop(); uint16_t rawH = msb << 8 | lsb; rawH = rawH & 0xFFFC; return rawH; }
unsigned short read_from_register(i2c* bus, int address, int regAddr) { i2c_start(bus); i2c_writeByte(bus, address); i2c_writeByte(bus, regAddr); i2c_start(bus); i2c_writeByte(bus, address+1); unsigned short val = i2c_readByte(bus, 1); i2c_stop(bus); return val; }
int16_t ICACHE_FLASH_ATTR SHT21_GetVal(uint8 mode) { i2c_start(); //Start i2c i2c_writeByte(SHT21_ADDRESS); if (!i2c_check_ack()) { //os_printf("-%s-%s slave not ack... return \r\n", __FILE__, __func__); i2c_stop(); return(0); } if (mode==GET_SHT_TEMPERATURE) i2c_writeByte(TRIGGER_TEMP_MEASURE_NOHOLD); else if (mode==GET_SHT_HUMIDITY) i2c_writeByte(TRIGGER_HUMD_MEASURE_NOHOLD); else return 0; if (!i2c_check_ack()) { //os_printf("-%s-%s slave not ack... return \r\n", __FILE__, __func__); i2c_stop(); return(0); } os_delay_us(20); i2c_stop(); os_delay_us(70000); uint8 ack = 0; while (!ack) { i2c_start(); i2c_writeByte(SHT21_ADDRESS+1); ack = i2c_check_ack(); if (!ack) i2c_stop(); } //os_printf("-%s-%s get ack \r\n", __FILE__, __func__); uint8 msb = i2c_readByte(); i2c_send_ack(1); uint8 lsb = i2c_readByte(); i2c_send_ack(0); i2c_stop(); uint16_t _rv = msb << 8; _rv += lsb; _rv &= ~0x0003; //os_printf("-%s-%s RAW:%d \r\n", __FILE__, __func__,_rv); float rv = _rv; if (mode==GET_SHT_TEMPERATURE) { rv *= 175.72; rv /= 65536; rv -= 46.85; } else if (mode==GET_SHT_HUMIDITY) { rv *= 125.0; rv /= 65536; rv -= 6.0; } return (int16_t)(rv*10); }
int mpu6050_readTempDataRaw(void){ int tempRaw = (int) i2c_readByte(MPU6050_ADDRESS, MPU6050_RA_TEMP_OUT_H) << 8 | i2c_readByte(MPU6050_ADDRESS, MPU6050_RA_TEMP_OUT_L); return tempRaw; }
void mpu6050_readGyroDataRaw(int *xData, int *yData, int *zData){ *xData = (int) i2c_readByte(MPU6050_ADDRESS, MPU6050_RA_GYRO_XOUT_H) << 8 | i2c_readByte(MPU6050_ADDRESS, MPU6050_RA_GYRO_XOUT_L); *yData = (int) i2c_readByte(MPU6050_ADDRESS, MPU6050_RA_GYRO_YOUT_H) << 8 | i2c_readByte(MPU6050_ADDRESS, MPU6050_RA_GYRO_YOUT_L); *zData = (int) i2c_readByte(MPU6050_ADDRESS, MPU6050_RA_GYRO_ZOUT_H) << 8 | i2c_readByte(MPU6050_ADDRESS, MPU6050_RA_GYRO_ZOUT_L); return; }
void mpu6050_readAccelerometerDataRaw(int *xData, int *yData, int *zData){ *xData = (int) i2c_readByte(MPU6050_ADDRESS, MPU6050_RA_ACCEL_XOUT_H) << 8 | i2c_readByte(MPU6050_ADDRESS, MPU6050_RA_ACCEL_XOUT_L); *yData = (int) i2c_readByte(MPU6050_ADDRESS, MPU6050_RA_ACCEL_YOUT_H) << 8 | i2c_readByte(MPU6050_ADDRESS, MPU6050_RA_ACCEL_YOUT_L); *zData = (int) i2c_readByte(MPU6050_ADDRESS, MPU6050_RA_ACCEL_ZOUT_H) << 8 | i2c_readByte(MPU6050_ADDRESS, MPU6050_RA_ACCEL_ZOUT_L); return; }