static char i2cs_recv(void) { I2C_WAIT(); while( TW_STATUS != TW_SR_DATA_ACK ) { I2C_ACK(); } char c = TWDR; I2C_ACK(); return c; }
/** * @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--; } }
static void i2cs_send(char c) { I2C_WAIT(); while( TW_STATUS != TW_ST_SLA_ACK && TW_STATUS != TW_ST_DATA_ACK ) { I2C_ACK(); } I2C_SEND(c); }
/* @brief Receive data from an I2C slave. * * Parameters for the '>' command: * - I2C slave address (u8) * - data size (u8) or 0 * * If the data size is 0, the value is read from the first data byte and is the * number of bytes to read after this first one. This is used to read protocol * replies from the slave. */ static void cmd_i2c_recv(void) { const uint8_t addr = init_cmd_i2c(); if( addr == 0 ) { return; // reply sent by init_cmd_i2c() } uint8_t size = recv_u8(); // poll the slave do I2CM_START(); while( TW_STATUS != TW_START ); // slave address + Read bit (1) I2C_SEND((addr<<1)+1); #ifndef DISABLE_STRICT_CHECKS if( TW_STATUS != TW_MR_SLA_ACK && TW_STATUS != TW_MR_SLA_NACK ) { I2CM_STOP(); goto slave_i2c_error; } #endif if( size == 0 ) { // read the size in the first byte I2C_ACK(); size = TWDR; reply_success(size+1); send_u8(size); } else { reply_success(size); } while( size-- != 1 ) { I2C_ACK(); const uint8_t c = TWDR; send_u8(c); } I2C_NACK(); send_u8( TWDR ); I2CM_STOP(); return; #ifndef DISABLE_STRICT_CHECKS slave_i2c_error: reply_error(STATUS_I2C_ERROR); return; #endif }
// Receive a frame from an address // Slave must respect the i2c_ryder protocol : first byte tells if there are // data to transmit or not // Return number of read bytes uint8_t i2cm_rcv(uint8_t slave_addr, uint8_t n, uint8_t* data) { int i; 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 0; } // First byte: is there something to transmit? I2C_ACK(); if( TWDR == 0 ) { I2C_NACK(); I2C_STOP(); wait_4cyc(100); return 0; } // Read all data for( i=0; i<n; i++ ) { I2C_ACK(); data[i] = TWDR; } I2C_NACK(); I2C_STOP(); wait_4cyc(100); return 1; }
void i2cs_init(uint8_t slave_addr) { unsigned int i; for( i=0; i<sizeof(i2cs_recv_buf); i++ ) i2cs_recv_buf[i] = 0; i2cs_recv_size = 0; for( i=0; i<sizeof(i2cs_send_buf); i++ ) i2cs_send_buf[i] = 0; i2cs_send_size = 0; TWAR = slave_addr << 1; I2C_ACK(); }
unsigned long DS1672_Value(void) { unsigned long value; int t; unsigned long v; I2C_Init(100,1000000); I2C_Start(); t=I2C_TX(0xD1); if(t!=0) return 0xffffffff; t=I2C_RX(); if(t==-1) return 0xffffffff; value=t; I2C_ACK(); t=I2C_RX(); if(t==-1) return 0xffffffff; v = t; value+=v<<8; I2C_ACK(); t=I2C_RX(); if(t==-1) return 0xffffffff; v = t; value+=v<<16; I2C_ACK(); t=I2C_RX(); if(t==-1) return 0xffffffff; v = t; value+=v<<24; I2C_NACK(); I2C_Stop(); return value; }
// 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; }
static void I2C_SEND(uint8_t d) { TWDR = (d); I2C_ACK(); }