/* Writes a single byte (data) into address */ void i2cWriteRegister(uint8_t i2c_7bit_address, uint8_t address, uint8_t data) { i2cSendStart(); i2cSendWriteAddress(i2c_7bit_address); i2cSendData(address); // write register address i2cSendData(data); i2cSendStop(); }
/* Writes num_to_send bytes of data starting at address */ void i2cWriteRegisters(uint8_t i2c_7bit_address, uint8_t address, uint8_t num_to_send, const uint8_t *data) { i2cSendStart(); i2cSendWriteAddress(i2c_7bit_address); i2cSendData(address); // write register address for (int j=0; j<num_to_send; j++) { i2cSendData(data[j]); } i2cSendStop(); }
/** * Implementing the CHAR dev write API. * * Note, this doesn't implement ulFlags specific options yet! **/ static BT_ERROR i2cWrite(BT_HANDLE hI2C, BT_u16 usDevice, BT_u8 *pucSource, BT_u32 ulLength) { BT_ERROR Error = BT_ERR_NONE; i2cStart(hI2C); Error = i2cSendAddress(hI2C, usDevice, BT_I2C_WRITE_ACCESS); if (Error) goto err_out; Error = i2cSendData(hI2C, pucSource, ulLength); if (Error) goto err_out; Error = i2cStop(hI2C); return Error; err_out: i2cStop(hI2C); return Error; }
// read a single byte from address and return it as a byte uint8_t i2cReadRegister(uint8_t i2c_7bit_address, uint8_t address) { uint8_t data; i2cSendStart(); i2cSendWriteAddress(i2c_7bit_address); i2cSendData(address); // write register address i2cSendRepeatedStart(); // repeated start i2cSendReadAddress(i2c_7bit_address); i2cReceiveByte(0); data = i2cGetReceivedByte(); // Get result i2cSendStop(); cbi(TWCR, TWEN); // Disable TWI sbi(TWCR, TWEN); // Enable TWI return data; }
// Read num_to_read registers sequentially, starting at address // into the dest byte arra void i2cReadRegisters(uint8_t i2c_7bit_address, uint8_t address, uint8_t num_to_read, uint8_t * dest) { i2cSendStart(); i2cSendWriteAddress(i2c_7bit_address); i2cSendData(address); // write register address i2cSendRepeatedStart(); // repeated start i2cSendReadAddress(i2c_7bit_address); int j; for (j=0; j<num_to_read-1; j++) { i2cReceiveByte(1); dest[j] = i2cGetReceivedByte(); // Get data } i2cReceiveByte(0); dest[j] = i2cGetReceivedByte(); // Get data i2cSendStop(); cbi(TWCR, TWEN); // Disable TWI sbi(TWCR, TWEN); // Enable TWI }
/** * Sendet Daten zu einem I2C-Gerät, das zusätzlich ein * Befehls-Byte benötigt. * * @param [in] adr * die I2C-Adresse des gewünschten Gerätes * @param [in] cmd * der gewünschte Befehl (Laut Datenblatt vom Slave) * @param [in] data * die zu sendenden Daten * @param [in] length * die Anzahl Bytes, die gesendet werden. * * @return * EC_SUCCESS falls der Slave immer mit ACK geantwortet hat * EC_I2C_NAK falls kein ACK empf. wurde. * EC_I2C_NO_ANSWER falls I2C-Gerät nicht geantwortet hat */ tError i2cWriteCmdData(uint8 adr, uint8 cmd, uint8 *data, uint8 length) { tError result; // @ToDo implement function code result = i2cStart(adr, TRUE); // init communication for write if (result != EC_SUCCESS) { return result; } result = i2cSendByte(cmd); // send the command byte if (result != EC_SUCCESS) { return result; } result = i2cSendData(data, length); // send the data byte if (result != EC_SUCCESS) { return result; } i2cStop(); return EC_SUCCESS; }