Implemented first Hangar-Service
Dependencies: CalibrateMagneto QuaternionMath
Fork of SML2 by
I2CPeripheral.cpp@46:fd5a62296b12, 2015-05-27 (annotated)
- Committer:
- pvaibhav
- Date:
- Wed May 27 13:01:43 2015 +0000
- Revision:
- 46:fd5a62296b12
- Parent:
- 7:604a8369b801
Code reformatted
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pvaibhav | 0:943820483318 | 1 | #include "I2CPeripheral.h" |
pvaibhav | 0:943820483318 | 2 | #include "mbed.h" |
pvaibhav | 0:943820483318 | 3 | #define DEBUG "I2CPeripheral" |
pvaibhav | 0:943820483318 | 4 | #include "Logger.h" |
pvaibhav | 0:943820483318 | 5 | |
pvaibhav | 7:604a8369b801 | 6 | I2CPeripheral::I2CPeripheral(I2C &i2c, const uint8_t address) : mBus(&i2c), mAddress(address) |
pvaibhav | 7:604a8369b801 | 7 | { |
pvaibhav | 1:c279bc3af90c | 8 | LOG("Initialised with ADDR=0x%02X", mAddress); |
pvaibhav | 0:943820483318 | 9 | } |
pvaibhav | 0:943820483318 | 10 | |
pvaibhav | 0:943820483318 | 11 | void I2CPeripheral::write_reg(const uint8_t reg, const uint8_t val) |
pvaibhav | 0:943820483318 | 12 | { |
pvaibhav | 0:943820483318 | 13 | char data[2]; |
pvaibhav | 0:943820483318 | 14 | data[0] = reg; |
pvaibhav | 0:943820483318 | 15 | data[1] = val; |
pvaibhav | 0:943820483318 | 16 | if (mBus->write(mAddress, data, 2)) { |
pvaibhav | 1:c279bc3af90c | 17 | ERR("Write failed, addr=0x%02x, reg=%02Xh, data=%02Xh", mAddress, reg, val); |
pvaibhav | 0:943820483318 | 18 | } |
pvaibhav | 0:943820483318 | 19 | } |
pvaibhav | 0:943820483318 | 20 | |
pvaibhav | 7:604a8369b801 | 21 | uint8_t I2CPeripheral::read_reg(const uint8_t reg) |
pvaibhav | 7:604a8369b801 | 22 | { |
pvaibhav | 5:b9f2f62a8f90 | 23 | uint8_t byte; |
pvaibhav | 5:b9f2f62a8f90 | 24 | read_reg(reg, &byte, 1); |
pvaibhav | 5:b9f2f62a8f90 | 25 | return byte; |
pvaibhav | 5:b9f2f62a8f90 | 26 | } |
pvaibhav | 5:b9f2f62a8f90 | 27 | |
pvaibhav | 7:604a8369b801 | 28 | void I2CPeripheral::read_reg(const uint8_t reg, uint8_t* destination, const size_t nBytes) |
pvaibhav | 7:604a8369b801 | 29 | { |
pvaibhav | 7:604a8369b801 | 30 | // Note about error checking - this function runs in a tight inner loop at 200 Hz or higher. |
pvaibhav | 7:604a8369b801 | 31 | // Therefore, checking for success and showing error message was removed. |
pvaibhav | 7:604a8369b801 | 32 | mBus->write(mAddress, (const char*)®, 1, true); |
pvaibhav | 7:604a8369b801 | 33 | // For reasons not known to me, the read() function also seems to require repeated start 'true' |
pvaibhav | 7:604a8369b801 | 34 | mBus->read(mAddress, (char*)destination, nBytes, true); |
pvaibhav | 5:b9f2f62a8f90 | 35 | mBus->stop(); |
pvaibhav | 0:943820483318 | 36 | } |