Bremen Team - Hangar / SML2

Dependencies:   CalibrateMagneto QuaternionMath

Fork of SML2 by TobyRich GmbH

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers I2CPeripheral.cpp Source File

I2CPeripheral.cpp

00001 #include "I2CPeripheral.h"
00002 #include "mbed.h"
00003 #define DEBUG "I2CPeripheral"
00004 #include "Logger.h"
00005 
00006 I2CPeripheral::I2CPeripheral(I2C &i2c, const uint8_t address) : mBus(&i2c), mAddress(address)
00007 {
00008     LOG("Initialised with ADDR=0x%02X", mAddress);
00009 }
00010 
00011 void I2CPeripheral::write_reg(const uint8_t reg, const uint8_t val)
00012 {
00013     char data[2];
00014     data[0] = reg;
00015     data[1] = val;
00016     if (mBus->write(mAddress, data, 2)) {
00017         ERR("Write failed, addr=0x%02x, reg=%02Xh, data=%02Xh", mAddress, reg, val);
00018     }
00019 }
00020 
00021 uint8_t I2CPeripheral::read_reg(const uint8_t reg)
00022 {
00023     uint8_t byte;
00024     read_reg(reg, &byte, 1);
00025     return byte;
00026 }
00027 
00028 void I2CPeripheral::read_reg(const uint8_t reg, uint8_t* destination, const size_t nBytes)
00029 {
00030     // Note about error checking - this function runs in a tight inner loop at 200 Hz or higher.
00031     // Therefore, checking for success and showing error message was removed.
00032     mBus->write(mAddress, (const char*)&reg, 1, true);
00033     // For reasons not known to me, the read() function also seems to require repeated start 'true'
00034     mBus->read(mAddress, (char*)destination, nBytes, true);
00035     mBus->stop();
00036 }