A I2C-Dev like library which can be used with different kinds of sensors.
Used in:
- BNO055 - Link
Revision 0:0af6c7cf0ed3, committed 2019-07-11
- Comitter:
- joelvonrotz
- Date:
- Thu Jul 11 09:35:55 2019 +0000
- Commit message:
- I2C Master library;
Changed in this revision
i2c_master.cpp | Show annotated file Show diff for this revision Revisions of this file |
i2c_master.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 0af6c7cf0ed3 i2c_master.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/i2c_master.cpp Thu Jul 11 09:35:55 2019 +0000 @@ -0,0 +1,60 @@ +#include "mbed.h" +#include "i2c_master.h" + +I2C_Master::I2C_Master(PinName sda, PinName scl, uint32_t frequency) : + m_i2c_sensor(sda, scl) +{ + + m_i2c_sensor.frequency(frequency); +} + +void I2C_Master::i2c_write(uint8_t i2c_address, uint8_t slave_address, const uint8_t data) +{ + char writeArray[2] = {slave_address, data}; + m_i2c_sensor.write(i2c_address, writeArray, 2, false); +} + +void I2C_Master::i2c_writeBits(uint8_t i2c_address, uint8_t slave_address, const uint8_t data, uint8_t mask) +{ + char current_value; + char writeArray[2] = {slave_address, data}; + + //Get current value of desired register and mask it with mask-value + current_value = i2c_read(i2c_address, slave_address); + current_value &= ~mask; + + //Combine Data with the new data (additionaly masking the data) and send it to the Sensor + writeArray[1] = current_value | (data & mask); + + m_i2c_sensor.write(i2c_address, writeArray, 2, false); +} + +void I2C_Master::i2c_readSeries(uint8_t i2c_address, uint8_t slave_address, uint8_t *data, uint8_t length) +{ + char *data_array; + data_array = reinterpret_cast<char*>(data); + + const char temp_address = slave_address; + + m_i2c_sensor.write(i2c_address, &temp_address, 1, true); + m_i2c_sensor.read(i2c_address, data_array,length, false); +} + +uint8_t I2C_Master::i2c_read(uint8_t i2c_address, uint8_t slave_address) +{ + char data; + const char temp_address = slave_address; + m_i2c_sensor.write(i2c_address, &temp_address, 1, true); + m_i2c_sensor.read(i2c_address, &data, 1, false); + return data; +} + +uint8_t I2C_Master::i2c_readBits(uint8_t i2c_address, uint8_t slave_address, uint8_t mask) +{ + char data; + const char temp_address = slave_address; + m_i2c_sensor.write(i2c_address, &temp_address, 1, true); + m_i2c_sensor.read(i2c_address, &data, 1, false); + return data & mask; +} +
diff -r 000000000000 -r 0af6c7cf0ed3 i2c_master.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/i2c_master.h Thu Jul 11 09:35:55 2019 +0000 @@ -0,0 +1,31 @@ +/** + * @file i2c.h + * @author Joel von Rotz (joel.vonrotz@maxonmotor.com) + * @brief + * @version 0.1 + * @date 2019-05-14 + * + * @copyright Copyright (c) 2019, maxon motor ag, All Rights Reserved + * + */ +#ifndef _I2C_H +#define _I2C_H +#include "mbed.h" + +class I2C_Master +{ +public: + I2C_Master(PinName sda, PinName scl, uint32_t frequency = 400000); + void i2c_write(uint8_t i2c_address, uint8_t slave_address, const uint8_t data); + void i2c_writeBits(uint8_t i2c_address, uint8_t slave_address, const uint8_t data, uint8_t mask); + uint8_t i2c_readBits(uint8_t i2c_address, uint8_t slave_address, uint8_t mask); + void i2c_readSeries(uint8_t i2c_address, uint8_t slave_address, uint8_t *data, uint8_t length); + uint8_t i2c_read(uint8_t i2c_address, uint8_t slave_address); + +private: + I2C m_i2c_sensor; + uint32_t m_i2c_frequency; +}; + +#endif +