uint8_t I2C::read(uint8_t address, uint8_t registerAddress, uint8_t numberBytes, uint8_t *dataBuffer) { bytesAvailable = 0; bufferIndex = 0; if(numberBytes == 0){numberBytes++;} nack = numberBytes - 1; returnStatus = 0; returnStatus = start(); if(returnStatus){return(returnStatus);} returnStatus = sendAddress(SLA_W(address)); if(returnStatus) { if(returnStatus == 1){return(2);} return(returnStatus); } returnStatus = sendByte(registerAddress); if(returnStatus) { if(returnStatus == 1){return(3);} return(returnStatus); } returnStatus = start(); if(returnStatus) { if(returnStatus == 1){return(4);} return(returnStatus); } returnStatus = sendAddress(SLA_R(address)); if(returnStatus) { if(returnStatus == 1){return(5);} return(returnStatus); } for(uint8_t i = 0; i < numberBytes; i++) { if( i == nack ) { returnStatus = receiveByte(0); if(returnStatus == 1){return(6);} if(returnStatus != MR_DATA_NACK){return(returnStatus);} } else { returnStatus = receiveByte(1); if(returnStatus == 1){return(6);} if(returnStatus != MR_DATA_ACK){return(returnStatus);} } dataBuffer[i] = TWDR; bytesAvailable = i+1; totalBytes = i+1; } returnStatus = stop(); if(returnStatus) { if(returnStatus == 1){return(7);} return(returnStatus); } return(returnStatus); }
bool read_registers_uint16_le(uint8_t addr, uint8_t reg, uint16_t* data, uint8_t size) { QASSERT(s_is_initialized); QASSERT(s_is_locked); QASSERT(data && size > 0); if (!data || size == 0) { return 0; } uint8_t* data_ptr8 = reinterpret_cast<uint8_t*>(data) + 1; //the data_ptr8 will advance like this //1, 0, 3, 2, 5, 4, 7, 6, 9, 8 if (!_start()) goto error; if (!_send_address(SLA_W(addr))) goto error; if (!_send_byte(reg)) goto error; if (!_start()) goto error; if (!_send_address(SLA_R(addr))) goto error; { uint8_t i = size - 1; while (i > 0) { if (!_receive_byte_ack()) goto error; *data_ptr8 = TWDR; data_ptr8 -= 1; if (!_receive_byte_ack()) goto error; *data_ptr8 = TWDR; data_ptr8 += 3; --i; } } //last bytes { if (!_receive_byte_ack()) goto error; *data_ptr8 = TWDR; data_ptr8 -= 1; if (!_receive_byte()) goto error; *data_ptr8 = TWDR; //data_ptr8 += 3; } if (!_stop()) goto error; return true; error: s_lockup_count++; return false; }
bool read_registers(uint8_t addr, uint8_t reg, uint8_t* data, uint8_t size) { QASSERT(s_is_initialized); QASSERT(s_is_locked); QASSERT(size > 0); if (size == 0) { return 0; } if (!_start()) goto error; if (!_send_address(SLA_W(addr))) goto error; if (!_send_byte(reg)) goto error; if (!_start()) goto error; if (!_send_address(SLA_R(addr))) goto error; if (data) { uint8_t i = size - 1; while (i > 0) { if (!_receive_byte_ack()) goto error; *data++ = TWDR; --i; } //last item { if (!_receive_byte()) goto error; *data = TWDR; } } else { uint8_t i = size - 1; while (i > 0) { if (!_receive_byte_ack()) goto error; s_i2c_null_sink = TWDR; --i; } //last item { if (!_receive_byte()) goto error; s_i2c_null_sink = TWDR; } } if (!_stop()) goto error; return true; error: s_lockup_count++; return false; }
bool read(uint8_t addr, uint8_t* data, uint8_t size) { QASSERT(s_is_initialized); QASSERT(s_is_locked); QASSERT(size > 0); if (size == 0) { return 0; } if (!_start()) goto error; if (!_send_address(SLA_R(addr))) goto error; if (data) { for (int8_t i = size - 1; i > 0; i--) //write size - 1 butes { if (!_receive_byte_ack()) goto error; *data++ = TWDR; } //last item { if (!_receive_byte()) goto error; *data = TWDR; } } else { for (int8_t i = size - 1; i > 0; i--) //write size - 1 butes { if (!_receive_byte_ack()) goto error; s_i2c_null_sink = TWDR; } //last item { if (!_receive_byte()) goto error; s_i2c_null_sink = TWDR; } } if (!_stop()) goto error; return true; error: s_lockup_count++; return false; }