Implemented first Hangar-Service

Dependencies:   CalibrateMagneto QuaternionMath

Fork of SML2 by TobyRich GmbH

Committer:
pvaibhav
Date:
Wed Jan 14 15:44:34 2015 +0000
Revision:
1:c279bc3af90c
Parent:
0:943820483318
Child:
5:b9f2f62a8f90
Barometer and accelerometer are alive

Who changed what in which revision?

UserRevisionLine numberNew 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 1:c279bc3af90c 7 LOG("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 1:c279bc3af90c 16 ERR("Write failed, addr=0x%02x, reg=%02Xh, data=%02Xh", mAddress, 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 1:c279bc3af90c 21
pvaibhav 1:c279bc3af90c 22 mBus->start();
pvaibhav 1:c279bc3af90c 23
pvaibhav 1:c279bc3af90c 24 if (!mBus->write(mAddress | 0x00)) {
pvaibhav 1:c279bc3af90c 25 WARN("No ACK after writing addr 0x%02x", mAddress);
pvaibhav 0:943820483318 26 mBus->stop();
pvaibhav 0:943820483318 27 return 0;
pvaibhav 0:943820483318 28 }
pvaibhav 0:943820483318 29
pvaibhav 1:c279bc3af90c 30 if (!mBus->write(reg)) {
pvaibhav 1:c279bc3af90c 31 WARN("No ACK after writing reg 0x%02x to addr 0x%02x", reg, mAddress);
pvaibhav 1:c279bc3af90c 32 mBus->stop();
pvaibhav 1:c279bc3af90c 33 return 0;
pvaibhav 0:943820483318 34 }
pvaibhav 1:c279bc3af90c 35
pvaibhav 1:c279bc3af90c 36 // Generated repeated start
pvaibhav 1:c279bc3af90c 37 mBus->start();
pvaibhav 1:c279bc3af90c 38 if (!mBus->write(mAddress | 0x01)) {
pvaibhav 1:c279bc3af90c 39 WARN("No ACK after writing addr 0x%02x after Sr", mAddress);
pvaibhav 1:c279bc3af90c 40 mBus->stop();
pvaibhav 1:c279bc3af90c 41 return 0;
pvaibhav 1:c279bc3af90c 42 }
pvaibhav 1:c279bc3af90c 43
pvaibhav 1:c279bc3af90c 44 const uint8_t byte = mBus->read(0); // don't acknowledge, only need 1 byte
pvaibhav 1:c279bc3af90c 45 mBus->stop();
pvaibhav 1:c279bc3af90c 46
pvaibhav 1:c279bc3af90c 47 return byte;
pvaibhav 0:943820483318 48 }