Implemented first Hangar-Service
Dependencies: CalibrateMagneto QuaternionMath
Fork of SML2 by
I2CPeripheral.cpp@0:943820483318, 2015-01-13 (annotated)
- Committer:
- pvaibhav
- Date:
- Tue Jan 13 11:23:01 2015 +0000
- Revision:
- 0:943820483318
- Child:
- 1:c279bc3af90c
Initial commit with working LED and motor drivers.
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 | 0:943820483318 | 6 | I2CPeripheral::I2CPeripheral(I2C &i2c, const uint8_t address) : mBus(&i2c), mAddress(address) { |
pvaibhav | 0:943820483318 | 7 | INFO("Initialised with ADDR=0x%02X", mAddress); |
pvaibhav | 0:943820483318 | 8 | } |
pvaibhav | 0:943820483318 | 9 | |
pvaibhav | 0:943820483318 | 10 | void I2CPeripheral::write_reg(const uint8_t reg, const uint8_t val) |
pvaibhav | 0:943820483318 | 11 | { |
pvaibhav | 0:943820483318 | 12 | char data[2]; |
pvaibhav | 0:943820483318 | 13 | data[0] = reg; |
pvaibhav | 0:943820483318 | 14 | data[1] = val; |
pvaibhav | 0:943820483318 | 15 | if (mBus->write(mAddress, data, 2)) { |
pvaibhav | 0:943820483318 | 16 | ERR("Write failed, addr=0x%02x, reg=%02Xh, data=%02Xh", reg, val); |
pvaibhav | 0:943820483318 | 17 | } |
pvaibhav | 0:943820483318 | 18 | } |
pvaibhav | 0:943820483318 | 19 | |
pvaibhav | 0:943820483318 | 20 | uint8_t I2CPeripheral::read_reg(const uint8_t reg) { |
pvaibhav | 0:943820483318 | 21 | uint8_t byte; |
pvaibhav | 0:943820483318 | 22 | if (mBus->write(mAddress, (const char*)®, 1, true)) { |
pvaibhav | 0:943820483318 | 23 | ERR("Can't write reg=0x%02x to read from", reg); |
pvaibhav | 0:943820483318 | 24 | mBus->stop(); |
pvaibhav | 0:943820483318 | 25 | return 0; |
pvaibhav | 0:943820483318 | 26 | } |
pvaibhav | 0:943820483318 | 27 | |
pvaibhav | 0:943820483318 | 28 | if (mBus->read(mAddress, (char*)&byte, 1)) { |
pvaibhav | 0:943820483318 | 29 | ERR("Can't read reg=0x%02x", reg); |
pvaibhav | 0:943820483318 | 30 | } |
pvaibhav | 0:943820483318 | 31 | return byte; |
pvaibhav | 0:943820483318 | 32 | } |