/** * Internal helper, send EEPROM write control byte and address. This is a * prolog for a few complete sequences. This resulting in setting the internal * address counter in EEPROM. * * @param address EEPROM address. * @return 0 - success, error otherwise. */ unsigned char eeprom_helper_writeAddressSequence(unsigned int address) { i2c_start(); // Control byte for writing i2c_transmitByte(CONTROL_BYTE_WRITE); if (!i2c_check_ack()) { i2c_stop(); return EEPROM_NO_ACK; } // MSB of the address i2c_transmitByte((unsigned char)((address & 0xFF00) >> 8)); if (!i2c_check_ack()) { i2c_stop(); return EEPROM_NO_ACK; } // LSB of the address i2c_transmitByte((unsigned char)(address & 0xFF)); if (!i2c_check_ack()) { i2c_stop(); return EEPROM_NO_ACK; } return EEPROM_OK; }
/** * Write a sequence of bytes from the buffer to EEPROM starting the address * specified (up to 64 bytes). * * @param address starting address in the EEPROM. * @param buffer data to write. * @param len length of the buffer. * @return 0 - success, error code otherwise. */ unsigned char eeprom_writePage(unsigned int address, unsigned char* buffer, unsigned char len) { if (len > 64) return EEPROM_WRONG_LEN; unsigned char error = eeprom_helper_writeAddressSequence(address); // Now send bytes to EEPROM addressed by the internal counter. for (unsigned char i = 0; i < len; i++) { // EEPROM should acknowledge each time if (!i2c_check_ack()) { i2c_stop(); return EEPROM_NO_ACK; } i2c_transmitByte(buffer[i]); } // EEPROM should acknowledge each time if (!i2c_check_ack()) { i2c_stop(); return EEPROM_NO_ACK; } // End of writing, let EEPROM to flush from internal buffer to storage. i2c_stop(); return EEPROM_OK; }
/****************************************************************************** * FunctionName : oled_write_reg * Description : write the register of the i2c oled. * Parameters : uint8_t reg_addr: the register address. * uint8_t val: the write value. * Returns : bool : the write result, 0:failed, 1:success. *******************************************************************************/ static bool ICACHE_FLASH_ATTR oled_write_reg(uint8_t reg_addr,uint8_t val) { i2c_start(); i2c_writeByte(OLED_ADDRESS); if(!i2c_check_ack()) { i2c_stop(); return 0; } i2c_writeByte(reg_addr); if(!i2c_check_ack()) { i2c_stop(); return 0; } i2c_writeByte(val&0xff); if(!i2c_check_ack()) { i2c_stop(); return 0; } i2c_stop(); if(reg_addr==0x00) oledstat=true; return 1; }
static int ICACHE_FLASH_ATTR tcn75_set_cfg(int idx, uint8_t cfgval) { uint8_t addr = TCN_BASE_ADDR | idx; int rc = 0; // set in one shot mode i2c_start(); i2c_writeByte(addr << 1); if (!i2c_check_ack()) { goto out_err; } i2c_writeByte(TCN_CFG); if (!i2c_check_ack()) { goto out_err; } // place all in powerdown mode i2c_writeByte(cfgval); if (!i2c_check_ack()) { goto out_err; } rc = 1; out_err: i2c_stop(); return rc; }
/****************************************************************************** * 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; }
/** * Read bytes addressed by internal EEPROM internally incremented counter. * * @param buffer for successful operation contains bytes read from EEPROM. * @param len buffer size. * @return 0 - success, error code otherwise. */ unsigned char eeprom_readCurrentAddress(unsigned char* buffer, unsigned char len) { i2c_start(); // Control byte for reading, should be followed by ACK from EEPROM. i2c_transmitByte(CONTROL_BYTE_READ); // Now EEPROM sends bytes addressed by the internal counter. for (unsigned char i = 0; i < len; i++) { // EEPROM should acknowledge each time if (!i2c_check_ack()) { i2c_stop(); return EEPROM_NO_ACK; } buffer[i] = i2c_receiveByte(); } // Finally master won't acknowledge and then generate a Stop condition. i2c_send_ack(NACK); i2c_stop(); return EEPROM_OK; }
/* Function sht21_writeCommand * * */ bool ICACHE_FLASH_ATTR sht21_writeCommand(uint8_t addr, uint8_t cmd) { i2c_start(); i2c_writeByte(addr); if(i2c_check_ack()){ i2c_writeByte(cmd); if(i2c_check_ack()){ i2c_stop(); return TRUE; } } else { i2c_stop(); return FALSE; } return FALSE; }
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(); } }
/* Function sht21_readCommand * TODO: Finish it! * */ uint32_t ICACHE_FLASH_ATTR sht21_readCommand(uint8_t addr) { while (!i2c_check_ack()) { i2c_start(); i2c_writeByte(addr); if (!i2c_check_ack) i2c_stop(); } return 0; }
bool ICACHE_FLASH_ATTR SHT21_SoftReset() { //Soft reset the SHT21 i2c_start(); i2c_writeByte(SHT21_ADDRESS); if (!i2c_check_ack()) { //os_printf("-%s-%s slave not ack... return \r\n", __FILE__, __func__); i2c_stop(); return(0); } i2c_writeByte(SOFT_RESET); if (!i2c_check_ack()) { //os_printf("-%s-%s slave not ack... return \r\n", __FILE__, __func__); i2c_stop(); return(0); } i2c_stop(); return(1); }
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); }