Implemented first Hangar-Service
Dependencies: CalibrateMagneto QuaternionMath
Fork of SML2 by
Diff: I2CPeripheral.cpp
- Revision:
- 5:b9f2f62a8f90
- Parent:
- 1:c279bc3af90c
- Child:
- 7:604a8369b801
diff -r e759b8c756da -r b9f2f62a8f90 I2CPeripheral.cpp --- a/I2CPeripheral.cpp Fri Jan 23 13:00:46 2015 +0000 +++ b/I2CPeripheral.cpp Thu Feb 12 17:17:35 2015 +0000 @@ -18,19 +18,24 @@ } uint8_t I2CPeripheral::read_reg(const uint8_t reg) { - + uint8_t byte; + read_reg(reg, &byte, 1); + return byte; +} + +void I2CPeripheral::read_reg(const uint8_t reg, uint8_t* destination, const size_t nBytes) { mBus->start(); if (!mBus->write(mAddress | 0x00)) { WARN("No ACK after writing addr 0x%02x", mAddress); mBus->stop(); - return 0; + return; } if (!mBus->write(reg)) { WARN("No ACK after writing reg 0x%02x to addr 0x%02x", reg, mAddress); mBus->stop(); - return 0; + return; } // Generated repeated start @@ -38,11 +43,13 @@ if (!mBus->write(mAddress | 0x01)) { WARN("No ACK after writing addr 0x%02x after Sr", mAddress); mBus->stop(); - return 0; + return; } - const uint8_t byte = mBus->read(0); // don't acknowledge, only need 1 byte - mBus->stop(); + for (size_t i = 0; i < nBytes; i++) { + destination[i] = mBus->read(i < nBytes ? 1 : 0); // 0 signals to the chip that this is the last byte we're reading + //LOG("byte[%d] = %#x", i, destination[i]); + } - return byte; + mBus->stop(); } \ No newline at end of file