A I2C-Dev like library which can be used with different kinds of sensors.
Used in:
- BNO055 - Link
i2c_master.cpp@0:0af6c7cf0ed3, 2019-07-11 (annotated)
- Committer:
- joelvonrotz
- Date:
- Thu Jul 11 09:35:55 2019 +0000
- Revision:
- 0:0af6c7cf0ed3
I2C Master library;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
joelvonrotz | 0:0af6c7cf0ed3 | 1 | #include "mbed.h" |
joelvonrotz | 0:0af6c7cf0ed3 | 2 | #include "i2c_master.h" |
joelvonrotz | 0:0af6c7cf0ed3 | 3 | |
joelvonrotz | 0:0af6c7cf0ed3 | 4 | I2C_Master::I2C_Master(PinName sda, PinName scl, uint32_t frequency) : |
joelvonrotz | 0:0af6c7cf0ed3 | 5 | m_i2c_sensor(sda, scl) |
joelvonrotz | 0:0af6c7cf0ed3 | 6 | { |
joelvonrotz | 0:0af6c7cf0ed3 | 7 | |
joelvonrotz | 0:0af6c7cf0ed3 | 8 | m_i2c_sensor.frequency(frequency); |
joelvonrotz | 0:0af6c7cf0ed3 | 9 | } |
joelvonrotz | 0:0af6c7cf0ed3 | 10 | |
joelvonrotz | 0:0af6c7cf0ed3 | 11 | void I2C_Master::i2c_write(uint8_t i2c_address, uint8_t slave_address, const uint8_t data) |
joelvonrotz | 0:0af6c7cf0ed3 | 12 | { |
joelvonrotz | 0:0af6c7cf0ed3 | 13 | char writeArray[2] = {slave_address, data}; |
joelvonrotz | 0:0af6c7cf0ed3 | 14 | m_i2c_sensor.write(i2c_address, writeArray, 2, false); |
joelvonrotz | 0:0af6c7cf0ed3 | 15 | } |
joelvonrotz | 0:0af6c7cf0ed3 | 16 | |
joelvonrotz | 0:0af6c7cf0ed3 | 17 | void I2C_Master::i2c_writeBits(uint8_t i2c_address, uint8_t slave_address, const uint8_t data, uint8_t mask) |
joelvonrotz | 0:0af6c7cf0ed3 | 18 | { |
joelvonrotz | 0:0af6c7cf0ed3 | 19 | char current_value; |
joelvonrotz | 0:0af6c7cf0ed3 | 20 | char writeArray[2] = {slave_address, data}; |
joelvonrotz | 0:0af6c7cf0ed3 | 21 | |
joelvonrotz | 0:0af6c7cf0ed3 | 22 | //Get current value of desired register and mask it with mask-value |
joelvonrotz | 0:0af6c7cf0ed3 | 23 | current_value = i2c_read(i2c_address, slave_address); |
joelvonrotz | 0:0af6c7cf0ed3 | 24 | current_value &= ~mask; |
joelvonrotz | 0:0af6c7cf0ed3 | 25 | |
joelvonrotz | 0:0af6c7cf0ed3 | 26 | //Combine Data with the new data (additionaly masking the data) and send it to the Sensor |
joelvonrotz | 0:0af6c7cf0ed3 | 27 | writeArray[1] = current_value | (data & mask); |
joelvonrotz | 0:0af6c7cf0ed3 | 28 | |
joelvonrotz | 0:0af6c7cf0ed3 | 29 | m_i2c_sensor.write(i2c_address, writeArray, 2, false); |
joelvonrotz | 0:0af6c7cf0ed3 | 30 | } |
joelvonrotz | 0:0af6c7cf0ed3 | 31 | |
joelvonrotz | 0:0af6c7cf0ed3 | 32 | void I2C_Master::i2c_readSeries(uint8_t i2c_address, uint8_t slave_address, uint8_t *data, uint8_t length) |
joelvonrotz | 0:0af6c7cf0ed3 | 33 | { |
joelvonrotz | 0:0af6c7cf0ed3 | 34 | char *data_array; |
joelvonrotz | 0:0af6c7cf0ed3 | 35 | data_array = reinterpret_cast<char*>(data); |
joelvonrotz | 0:0af6c7cf0ed3 | 36 | |
joelvonrotz | 0:0af6c7cf0ed3 | 37 | const char temp_address = slave_address; |
joelvonrotz | 0:0af6c7cf0ed3 | 38 | |
joelvonrotz | 0:0af6c7cf0ed3 | 39 | m_i2c_sensor.write(i2c_address, &temp_address, 1, true); |
joelvonrotz | 0:0af6c7cf0ed3 | 40 | m_i2c_sensor.read(i2c_address, data_array,length, false); |
joelvonrotz | 0:0af6c7cf0ed3 | 41 | } |
joelvonrotz | 0:0af6c7cf0ed3 | 42 | |
joelvonrotz | 0:0af6c7cf0ed3 | 43 | uint8_t I2C_Master::i2c_read(uint8_t i2c_address, uint8_t slave_address) |
joelvonrotz | 0:0af6c7cf0ed3 | 44 | { |
joelvonrotz | 0:0af6c7cf0ed3 | 45 | char data; |
joelvonrotz | 0:0af6c7cf0ed3 | 46 | const char temp_address = slave_address; |
joelvonrotz | 0:0af6c7cf0ed3 | 47 | m_i2c_sensor.write(i2c_address, &temp_address, 1, true); |
joelvonrotz | 0:0af6c7cf0ed3 | 48 | m_i2c_sensor.read(i2c_address, &data, 1, false); |
joelvonrotz | 0:0af6c7cf0ed3 | 49 | return data; |
joelvonrotz | 0:0af6c7cf0ed3 | 50 | } |
joelvonrotz | 0:0af6c7cf0ed3 | 51 | |
joelvonrotz | 0:0af6c7cf0ed3 | 52 | uint8_t I2C_Master::i2c_readBits(uint8_t i2c_address, uint8_t slave_address, uint8_t mask) |
joelvonrotz | 0:0af6c7cf0ed3 | 53 | { |
joelvonrotz | 0:0af6c7cf0ed3 | 54 | char data; |
joelvonrotz | 0:0af6c7cf0ed3 | 55 | const char temp_address = slave_address; |
joelvonrotz | 0:0af6c7cf0ed3 | 56 | m_i2c_sensor.write(i2c_address, &temp_address, 1, true); |
joelvonrotz | 0:0af6c7cf0ed3 | 57 | m_i2c_sensor.read(i2c_address, &data, 1, false); |
joelvonrotz | 0:0af6c7cf0ed3 | 58 | return data & mask; |
joelvonrotz | 0:0af6c7cf0ed3 | 59 | } |
joelvonrotz | 0:0af6c7cf0ed3 | 60 |