char I2C_read(char writeDataLength, char readDataLength, char* writeData) { char j = readDataLength; I2C_START; // do start transition // send the data writeDataLength--; //decrement this to begin with, as the last byte (second addr) will be sent after the repeated start while (writeDataLength--) i2cPutbyte(*writeData++); //repeated start HDEL; I2C_SDL_HI; //prepare for the repeated start HDEL; I2C_SCL_HI; // do a repeated START I2C_START; i2cPutbyte(*writeData); // Send the last byte in the write data array // receive data bytes while (j--) buffer_REPLY[IN_REPORT_HEADER_LENGTH + readDataLength - 1 - j] = i2cGetbyte(j == 0); I2C_SDL_LO; // clear data line and I2C_STOP; // send STOP transition return ERROR_I2C_NOERROR; }
uint8_t i2c_Receive_Status(uint8_t device) { I2C_START; // do start transition i2cPutbyte(device | READ); // send DEVICE address and read return(i2cGetbyte(1)); // read status byte }
// 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++; } }
//! Retrieve a byte sequence on the I2C bus void i2c_Receive(uint8_t device, uint8_t subAddr, uint8_t length, uint8_t *dataxx) { int j = length; uint8_t *p = dataxx; I2C_START; // do start transition i2cPutbyte(device); // send DEVICE address i2cPutbyte(subAddr); // and the subaddress HDEL; I2C_SCL_HI; // do a repeated START I2C_START; // transition i2cPutbyte(device | READ); // resend DEVICE, with READ bit set // receive data bytes while (j--) *p++ = i2cGetbyte(j == 0); I2C_SDA_LO; // clear data line and I2C_STOP; // send STOP transition }
void instrCall_I2C(char instruction, char* datain, char datainLength) { switch (instruction) { case I2C_WRITE: { I2C_error = I2C_write(datain[0], &datain[1]); break; } case I2C_READ: { I2C_read(datain[0], datain[1], &datain[2]); setReplyInherentData(S_I2C, I2C_READ, datain[1], 0x00); break; } case I2C_SETUP: { I2C_error = I2C_setup(); break; } case I2C_SEND_START: { I2C_error = ERROR_I2C_NOERROR; I2C_SCL_LO; QDEL; I2C_SDL_HI; QDEL; I2C_SCL_HI; QDEL; //ensure the lines are in the correct state before issuing the start I2C_START; break; } case I2C_SEND_STOP: { I2C_STOP; break; } case I2C_SEND_BYTES: { char ack = 0; // send the data char writeDataLength = datain[0]; char* writeData = &datain[1]; while (writeDataLength--) ack |= i2cPutbyte(*writeData++); I2C_SDL_LO; // clear data line if (ack == 0) //all bytes acked I2C_error = ERROR_I2C_NOERROR; else I2C_error = ERROR_I2C_NOACK; break; } case I2C_READ_BYTES: { char readDataLength = datain[0]; char j = readDataLength; while (j--) buffer_REPLY[IN_REPORT_HEADER_LENGTH + readDataLength - 1 - j] = i2cGetbyte(j == 0); I2C_SDL_HI; // clear data line and setReplyInherentData(S_I2C, I2C_READ_BYTES, readDataLength, 0x00); break; } case I2C_CHECK_ERROR: { setReply(S_I2C, I2C_CHECK_ERROR, 1, 0x00, &I2C_error); I2C_error = ERROR_I2C_NOERROR; //reset the error code break; } case I2C_BUS_SCAN: { I2C_busScan(datain[0], datain[1], datain[2]); break; } } }