/** * @brief Reads a block of data from the EEPROM. * @param pBuffer : pointer to the buffer that receives the data read * from the EEPROM. * @param ReadAddr : EEPROM's internal address to read from. * @param NumByteToRead : number of bytes to read from the EEPROM. * @retval None */ void I2C_EE_BufferRead(uint8_t* pBuffer, uint16_t ReadAddr, uint16_t NumByteToRead) { I2C_START(); I2C_SEND_DATA(I2C_EEPROM_ADDRESS|EE_CMD_WRITE); I2C_WAIT_ACK(); #ifdef EE_M24C08 I2C_SEND_DATA(ReadAddr); I2C_WAIT_ACK(); #else I2C_SEND_DATA((uint8_t)((ReadAddr & 0xFF00) >> 8)); I2C_WAIT_ACK(); I2C_SEND_DATA((uint8_t)(ReadAddr & 0x00FF)); I2C_WAIT_ACK(); #endif I2C_START(); I2C_SEND_DATA(I2C_EEPROM_ADDRESS|EE_CMD_READ); I2C_WAIT_ACK(); while (NumByteToRead) { if (NumByteToRead == 1) { *pBuffer =I2C_READ(); I2C_NO_ACK(); I2C_STOP(); } else { *pBuffer =I2C_READ(); I2C_ACK(); pBuffer++; } NumByteToRead--; } }
int I2C_read(unsigned char slave_addr, unsigned char reg_addr, unsigned char length, unsigned char *data) { I2C_START(); I2C_SendByte(slave_addr << 1 | I2C_Direction_Transmitter); I2C_SendByte(reg_addr); I2C_START(); I2C_SendByte(slave_addr << 1 | I2C_Direction_Receiver); while (length) { if (length==1) *data =I2C_ReceiveByte(); else *data =I2C_ReceiveByte_WithACK(); data++; length--; } I2C_STOP(); return 0x00; }
void I2C_ReadBuf(uint8_t sla, uint8_t adr, uint8_t len, uint8_t *buf) { I2C_START(); I2C_SendByte(sla); I2C_SendByte(adr); I2C_START(); I2C_SendByte(sla+1); while (len--) *buf++ = I2C_ReadByte(len ? ACK:NACK); I2C_STOP(); }
// I2C模拟读出一个字节 uint8_t I2C_ReadByte(uint8_t DeviceAddr, uint8_t address) { uint8_t i; I2C_START(); I2C_SendByte(DeviceAddr); I2C_SendByte(address); I2C_START(); I2C_SendByte(DeviceAddr + 1); i = I2C_ReceiveByte(); I2C_STOP(); return i; }
uint8_t I2C_read_volume() { uint8_t volume ; I2C_START(); I2C_SEND_DATA(I2C_CAT5137_ADDRESS|EE_CMD_WRITE); I2C_WAIT_ACK(); I2C_SEND_DATA(0); I2C_WAIT_ACK(); I2C_START(); I2C_SEND_DATA(I2C_CAT5137_ADDRESS|EE_CMD_READ); I2C_WAIT_ACK(); volume = I2C_READ(); I2C_NO_ACK(); I2C_STOP(); return volume ; }
uint8_t LM75A_I2C_SingleRead(uint8_t index) { uint8_t tmp; I2C_START(LM75A_I2C_PORT); //Start I2C_WAIT_READY(LM75A_I2C_PORT); LM75A_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk; //clear flag I2C_SET_DATA(LM75A_I2C_PORT, LM75A_I2C_SLA); //send slave address+W I2C_SET_CONTROL_REG(LM75A_I2C_PORT, I2C_SI); I2C_WAIT_READY(LM75A_I2C_PORT); LM75A_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk; //clear flag I2C_SET_DATA(LM75A_I2C_PORT, index); //send index I2C_SET_CONTROL_REG(LM75A_I2C_PORT, I2C_SI); I2C_WAIT_READY(LM75A_I2C_PORT); LM75A_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk; //clear flag I2C_SET_CONTROL_REG(LM75A_I2C_PORT, I2C_STA | I2C_SI); //Start I2C_WAIT_READY(LM75A_I2C_PORT); LM75A_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk; //clear flag I2C_SET_DATA(LM75A_I2C_PORT, (LM75A_I2C_SLA+1)); //send slave address+R I2C_SET_CONTROL_REG(LM75A_I2C_PORT, I2C_SI); I2C_WAIT_READY(LM75A_I2C_PORT); LM75A_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk; //clear flag I2C_SET_CONTROL_REG(LM75A_I2C_PORT, I2C_SI); I2C_WAIT_READY(LM75A_I2C_PORT); LM75A_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk; //clear flag tmp = I2C_GET_DATA(LM75A_I2C_PORT); //read data I2C_SET_CONTROL_REG(LM75A_I2C_PORT, I2C_SI|I2C_STO);//Stop return tmp; }
uint8_t MPU6050_I2C_SingleRead(uint8_t index) { uint8_t tmp; I2C_START(MPU6050_I2C_PORT); //Start I2C_WAIT_READY(MPU6050_I2C_PORT); MPU6050_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk; //clear flag I2C_SET_DATA(MPU6050_I2C_PORT, MPU6050_I2C_SLA); //send slave address+W I2C_SET_CONTROL_REG(MPU6050_I2C_PORT, I2C_SI); I2C_WAIT_READY(MPU6050_I2C_PORT); MPU6050_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk; //clear flag I2C_SET_DATA(MPU6050_I2C_PORT, index); //send index I2C_SET_CONTROL_REG(MPU6050_I2C_PORT, I2C_SI); I2C_WAIT_READY(MPU6050_I2C_PORT); MPU6050_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk; //clear flag I2C_SET_CONTROL_REG(MPU6050_I2C_PORT, I2C_STA | I2C_SI); //Start I2C_WAIT_READY(MPU6050_I2C_PORT); MPU6050_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk; //clear flag I2C_SET_DATA(MPU6050_I2C_PORT, (MPU6050_I2C_SLA+1)); //send slave address+R I2C_SET_CONTROL_REG(MPU6050_I2C_PORT, I2C_SI); I2C_WAIT_READY(MPU6050_I2C_PORT); MPU6050_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk; //clear flag I2C_SET_CONTROL_REG(MPU6050_I2C_PORT, I2C_SI); I2C_WAIT_READY(MPU6050_I2C_PORT); MPU6050_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk; //clear flag tmp = I2C_GET_DATA(MPU6050_I2C_PORT); //read data I2C_STOP(MPU6050_I2C_PORT); //STOP return tmp; }
// I2C模拟写入一个字节(无需寄存器地址) void I2C_NoAddr_WriteByte(uint8_t DeviceAddr, uint8_t data) { I2C_START(); I2C_SendByte(DeviceAddr); I2C_SendByte(data); I2C_STOP(); }
// I2C模拟读出两个字节 uint16_t I2C_Read_2Bytes(uint8_t DeviceAddr, uint8_t address) { uint8_t data_temp1,data_temp2; uint16_t data_16; I2C_START(); I2C_SendByte(DeviceAddr); I2C_SendByte(address); I2C_START(); I2C_SendByte(DeviceAddr + 1); data_temp1 = I2C_ReceiveByte_WithACK(); data_temp2 = I2C_ReceiveByte(); I2C_STOP(); data_16 = (data_temp1<<8) | data_temp2; return data_16; }
void I2C_SendBuf(uint8_t sla, uint8_t adr, uint8_t len, uint8_t *buf) { I2C_START(); I2C_SendByte(sla); I2C_SendByte(adr); while (len--) I2C_SendByte(*buf++); I2C_STOP(); }
// I2C模拟写一个字节 void I2C_WriteByte(uint8_t DeviceAddr, uint8_t address, uint8_t data) { I2C_START(); I2C_SendByte(DeviceAddr); I2C_SendByte(address); I2C_SendByte(data); I2C_STOP(); }
// I2C模拟读出三个字节 uint32_t I2C_Read_3Bytes(uint8_t DeviceAddr, uint8_t address) { uint8_t data_temp1, data_temp2, data_temp3; uint32_t data_32; I2C_START(); I2C_SendByte(DeviceAddr); I2C_SendByte(address); I2C_START(); I2C_SendByte(DeviceAddr + 1); data_temp1 = I2C_ReceiveByte_WithACK(); data_temp2 = I2C_ReceiveByte_WithACK(); data_temp3 = I2C_ReceiveByte(); I2C_STOP(); data_32 = (data_temp1<<16) | (data_temp2<<8) | data_temp3; return data_32; }
/** * @brief Wait for EEPROM Standby state * @param None * @retval None */ void I2C_EE_WaitEepromStandbyState(void) { do { I2C_START(); I2C_SEND_DATA(I2C_EEPROM_ADDRESS|EE_CMD_WRITE); } while (0 == I2C_WAIT_ACK()); I2C_STOP(); }
void I2C_set_volume( register uint8_t volume ) { I2C_START(); I2C_SEND_DATA(I2C_CAT5137_ADDRESS|EE_CMD_WRITE); I2C_WAIT_ACK(); I2C_SEND_DATA(0); I2C_WAIT_ACK(); I2C_SEND_DATA(volume); I2C_WAIT_ACK(); I2C_STOP(); }
// I2C模拟读出多个字节 u8 i2cread(u8 dev_addr, u8 reg_addr, u8 i2c_len, u8 *i2c_data_buf) { I2C_START(); I2C_SendByte(dev_addr << 1 | I2C_Direction_Transmitter); I2C_SendByte(reg_addr); I2C_START(); I2C_SendByte(dev_addr << 1 | I2C_Direction_Receiver); while (i2c_len) { if (i2c_len == 1) *i2c_data_buf = I2C_ReceiveByte(); else *i2c_data_buf = I2C_ReceiveByte_WithACK(); i2c_data_buf++; i2c_len--; } I2C_STOP(); return 0x00; }
// I2C模拟写入多个字节 u8 i2cwrite(u8 dev_addr, u8 reg_addr, u8 i2c_len, u8 *i2c_data_buf) { u8 i; I2C_START(); I2C_SendByte(dev_addr << 1 | I2C_Direction_Transmitter); I2C_SendByte(reg_addr); for (i = 0; i < i2c_len; i++) I2C_SendByte(i2c_data_buf[i]); I2C_STOP(); return 0x00; }
/************************************************************************* Issues a start condition and sends address and transfer direction. return 0 = device accessible, 1= failed to access device *************************************************************************/ unsigned char lib_i2c_start(unsigned char address) { // Send start I2C_START(I2C); if (!I2C_WAIT_READY_ERROR(I2C)) return 3; // transmit address byte + direction if (lib_i2c_write(address)) // Master Transmit Address ACK return 2; return 0; } /* lib_i2c_start */
int sleepMode(void){ unsigned char ret = -1; ret = I2C_START(TemperatureSensorAddress); if(ret != 0) return ret; I2C_WRITE(0x01); // write on confiuration register I2C_WRITE(0xC0); // Put SuhtDown mode, start one shot convertion I2C_STOP(); return 0; }
int WakeUpMode(void){ unsigned char ret = -1; ret = I2C_START(TemperatureSensorAddress); if(ret != 0) return ret; I2C_WRITE(0x01); // CONTROLS/STATUS REGISTER I2C_WRITE(0x40); //WakeUP I2C_STOP(); return 0; }
int I2C_write(unsigned char slave_addr, unsigned char reg_addr, unsigned char length, unsigned char const *data) { uint8_t i; I2C_START(); I2C_SendByte(slave_addr << 1 | I2C_Direction_Transmitter); I2C_SendByte(reg_addr); for (i=0;i<length;i++) I2C_SendByte(data[i]); I2C_STOP(); return 0x00; }
/*---------------------------------------------------------------------------------------------------------*/ void I2C_MasterRx(uint32_t u32Status) { if(u32Status == 0x08) /* START has been transmitted and prepare SLA+W */ { I2C0->DAT = g_u8DeviceAddr << 1; /* Write SLA+W to Register I2CDAT */ I2C_SET_CONTROL_REG(I2C0, I2C_CTL_SI); } else if(u32Status == 0x18) /* SLA+W has been transmitted and ACK has been received */ { I2C0->DAT = g_au8MstTxData[g_u8MstDataLen++]; I2C_SET_CONTROL_REG(I2C0, I2C_CTL_SI); } else if(u32Status == 0x20) /* SLA+W has been transmitted and NACK has been received */ { I2C_STOP(I2C0); I2C_START(I2C0); } else if(u32Status == 0x28) /* DATA has been transmitted and ACK has been received */ { if(g_u8MstDataLen != 2) { I2C0->DAT = g_au8MstTxData[g_u8MstDataLen++]; I2C_SET_CONTROL_REG(I2C0, I2C_CTL_SI); } else { I2C_SET_CONTROL_REG(I2C0, I2C_CTL_STA_SI); } } else if(u32Status == 0x10) /* Repeat START has been transmitted and prepare SLA+R */ { I2C0->DAT = ((g_u8DeviceAddr << 1) | 0x01); /* Write SLA+R to Register I2CDAT */ I2C_SET_CONTROL_REG(I2C0, I2C_CTL_SI); } else if(u32Status == 0x40) /* SLA+R has been transmitted and ACK has been received */ { I2C_SET_CONTROL_REG(I2C0, I2C_CTL_SI); } else if(u32Status == 0x58) /* DATA has been received and NACK has been returned */ { g_u8MstRxData = I2C0->DAT; I2C_SET_CONTROL_REG(I2C0, I2C_CTL_STO_SI); g_u8MstEndFlag = 1; } else { /* TO DO */ printf("Status 0x%x is NOT processed\n", u32Status); } }
// odczyt danych z pamiêci EEPROM void EI2C_read_buf(u08 device, u16 subAddr, u16 len, u08 *buf) { while (len--) { I2C_START(); i2cPutbyte(device | ((subAddr>>8)<<1) ); i2cPutbyte(subAddr); I2C_REP_START(); i2cPutbyte(device + 1); *buf++ = i2cGetbyte( NACK ); I2C_STOP(); subAddr++; } }
// zapis danych do pamiêci EEPROM void EI2C_write_buf(u08 device, u16 subAddr, u16 len, u08 *buf) { while (len--) { I2C_START(); // ustawienie 9 bitu adresu pamiêci EEPROM w ramach // sprzêtowego adresu urz¹dzenia na pozycji bitu 1 (nr.2) i2cPutbyte( device | ((subAddr>>8)<<1) ); i2cPutbyte(subAddr); i2cPutbyte(*buf++); I2C_STOP(); _delay_ms(5); // oczekiwanie na zapis subAddr++; } }
int configureTemperature(void){ int t; I2C_INIT(); t=I2C_START(TemperatureSensorAddress+I2C_WRITE_FLAG); if(t!=0) return t; I2C_WRITE(0x04); // CONTROLS/STATUS REGISTER I2C_WRITE(0xE0); // 14bits resolution & Timeout Off0xE0 I2C_WRITE(0x01); // CONTROLS/STATUS REGISTER I2C_WRITE(0x40); I2C_STOP(); return 0; }
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop) { if (quantity > BUFFER_LENGTH) quantity = BUFFER_LENGTH; int readed = 0; int timeout_=TIMEOUT; while(timeout_--) { /* Send start */ I2C_START(i2c); I2C_WAIT_READY(i2c); /* Send control byte */ I2C_SET_DATA(i2c, (address<<1)+1); I2C_SET_CONTROL_REG(i2c, I2C_SI | I2C_AA); I2C_WAIT_READY(i2c); if(I2C_GET_STATUS(i2c)!=0x40) { /* Send stop */ I2C_SET_CONTROL_REG(i2c, I2C_STO | I2C_SI); continue; } readed = 0; while (readed < quantity){ /* Read data */ if((readed+1)==quantity) I2C_SET_CONTROL_REG(i2c, I2C_SI); else I2C_SET_CONTROL_REG(i2c, I2C_SI | I2C_AA); I2C_WAIT_READY(i2c); rxBuffer[readed++] = I2C_GET_DATA(i2c); }; if(sendStop==true){ /* Send stop */ I2C_SET_CONTROL_REG(i2c, I2C_STO | I2C_SI); }else I2C_SET_CONTROL_REG(i2c, I2C_SI); break; } // set rx buffer iterator vars rxBufferIndex = 0; rxBufferLength = readed; return readed; }
// Send a n-byte data frame to an address void i2cm_send(uint8_t slave_addr, uint8_t n, const uint8_t* data) { int i; I2C_START(); // Slave address + Write bit (0) I2C_SEND(slave_addr<<1); // Data for( i=0; i<n; i++ ) { I2C_SEND(data[i]); } // Stop and wait I2C_STOP(); wait_4cyc(100); }
/** * @brief Writes one byte to the I2C EEPROM. * @param pBuffer : pointer to the buffer containing the data to be * written to the EEPROM. * @param WriteAddr : EEPROM's internal address to write to. * @retval None */ void I2C_EE_ByteWrite(uint8_t* pBuffer, uint16_t WriteAddr) { I2C_START(); I2C_SEND_DATA(I2C_EEPROM_ADDRESS|EE_CMD_WRITE); I2C_WAIT_ACK(); #ifdef EE_M24C08 I2C_SEND_DATA(WriteAddr); I2C_WAIT_ACK(); #else I2C_SEND_DATA((uint8_t)((WriteAddr&0xFF00)>>8) ); I2C_WAIT_ACK(); I2C_SEND_DATA((uint8_t)(WriteAddr&0xFF)); I2C_WAIT_ACK(); #endif I2C_SEND_DATA(*pBuffer); I2C_WAIT_ACK(); I2C_STOP(); }
void DAC8571_Write_DATA(uint16_t DATA) { uint8_t DATA_H,DATA_L; DATA_H=(DATA>>8); DATA_L=(DATA&0xff); I2C_START(); Delay_us(1); writebyte(0x98); Delay_us(1); writebyte(0x10); Delay_us(1); writebyte(DATA_H); Delay_us(1); writebyte(DATA_L); Delay_us(1); I2C_STOP(); }
// Receive one data byte from an address, -1 on error int i2cm_rcv_single(uint8_t slave_addr) { I2C_START(); // Slave address + Read bit (1) I2C_SEND((slave_addr<<1)+1); // No ACK, no data if( TW_STATUS != TW_MR_SLA_ACK ) { I2C_STOP(); wait_4cyc(100); return -1; } // First byte: is there something to transmit? I2C_ACK(); return TWDR; }
uint16_t LM75A_I2C_DoubleRead(uint8_t index) { uint8_t msb, lsb; uint16_t tmp; I2C_START(LM75A_I2C_PORT); //Start I2C_WAIT_READY(LM75A_I2C_PORT); LM75A_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk; //clear flag I2C_SET_DATA(LM75A_I2C_PORT, LM75A_I2C_SLA); //send slave address+W I2C_SET_CONTROL_REG(LM75A_I2C_PORT, I2C_SI); I2C_WAIT_READY(LM75A_I2C_PORT); LM75A_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk; //clear flag I2C_SET_DATA(LM75A_I2C_PORT, index); //send index I2C_SET_CONTROL_REG(LM75A_I2C_PORT, I2C_SI); I2C_WAIT_READY(LM75A_I2C_PORT); LM75A_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk; //clear flag I2C_SET_CONTROL_REG(LM75A_I2C_PORT, I2C_STA | I2C_SI); //Start I2C_WAIT_READY(LM75A_I2C_PORT); LM75A_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk; //clear flag I2C_SET_DATA(LM75A_I2C_PORT, (LM75A_I2C_SLA+1)); //send slave address+R I2C_SET_CONTROL_REG(LM75A_I2C_PORT, I2C_SI); I2C_WAIT_READY(LM75A_I2C_PORT); LM75A_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk; //clear flag I2C_SET_CONTROL_REG(LM75A_I2C_PORT, I2C_SI); I2C_WAIT_READY(LM75A_I2C_PORT); LM75A_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk; //clear flag msb = I2C_GET_DATA(LM75A_I2C_PORT); //read data I2C_SET_CONTROL_REG(LM75A_I2C_PORT, I2C_SI); I2C_WAIT_READY(LM75A_I2C_PORT); LM75A_I2C_PORT->INTSTS |= I2C_INTSTS_INTSTS_Msk; //clear flag lsb = I2C_GET_DATA(LM75A_I2C_PORT); //read data I2C_SET_CONTROL_REG(LM75A_I2C_PORT, I2C_SI|I2C_STO);//Stop tmp = msb; tmp = tmp<<8 | lsb; return tmp; }