I2C-Library for various sensors

Committer:
JOEV
Date:
Wed Jul 31 13:38:00 2019 +0000
Revision:
0:782435d2cc06
abcdef

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JOEV 0:782435d2cc06 1 /**
JOEV 0:782435d2cc06 2 * @brief Implementation of TWI_Master class.
JOEV 0:782435d2cc06 3 *
JOEV 0:782435d2cc06 4 * @file twi_master.cpp
JOEV 0:782435d2cc06 5 * @author Joel von Rotz
JOEV 0:782435d2cc06 6 * @date 18.07.2018
JOEV 0:782435d2cc06 7 */
JOEV 0:782435d2cc06 8 #include "mbed.h"
JOEV 0:782435d2cc06 9 #include "twi_master.h"
JOEV 0:782435d2cc06 10
JOEV 0:782435d2cc06 11 /**
JOEV 0:782435d2cc06 12 * @brief Construct a new TWI_Master object. The given parameters should be all the necessary things to start
JOEV 0:782435d2cc06 13 * interacting with the other libraries.
JOEV 0:782435d2cc06 14 *
JOEV 0:782435d2cc06 15 * @param sda SDA-Pin
JOEV 0:782435d2cc06 16 * @param scl SCL-Pin
JOEV 0:782435d2cc06 17 * @param frequency I2C-frequency (default: 400kHz)
JOEV 0:782435d2cc06 18 */
JOEV 0:782435d2cc06 19 TWI_Master::TWI_Master(PinName sda, PinName scl, uint32_t frequency) :
JOEV 0:782435d2cc06 20 m_i2c_sensor(sda, scl)
JOEV 0:782435d2cc06 21 {
JOEV 0:782435d2cc06 22 m_i2c_sensor.frequency(frequency);
JOEV 0:782435d2cc06 23 }
JOEV 0:782435d2cc06 24
JOEV 0:782435d2cc06 25 /**
JOEV 0:782435d2cc06 26 * @brief Writes data into specified address (changes every bit)
JOEV 0:782435d2cc06 27 *
JOEV 0:782435d2cc06 28 * @param slave_address register-address which will be written to
JOEV 0:782435d2cc06 29 * @param data 8-bit value that overwrites the register-value
JOEV 0:782435d2cc06 30 */
JOEV 0:782435d2cc06 31 void TWI_Master::i2c_write(uint8_t i2c_address, uint8_t slave_address, const uint8_t data)
JOEV 0:782435d2cc06 32 {
JOEV 0:782435d2cc06 33 char writeArray[2] = {slave_address, data};
JOEV 0:782435d2cc06 34 m_i2c_sensor.write(i2c_address, writeArray, 2, false);
JOEV 0:782435d2cc06 35 }
JOEV 0:782435d2cc06 36
JOEV 0:782435d2cc06 37 /**
JOEV 0:782435d2cc06 38 * @brief Writes bits by masking the necessary bits. The data variables will be normally masked with the mask
JOEV 0:782435d2cc06 39 * <code><strong>(data & mask)</strong></code>, the register will be masked by the inverted mask <code><strong>(register_value & ~mask)</strong></code>.
JOEV 0:782435d2cc06 40 * What will be written to the register will be <code><strong> new_register_data = (data | register_value) </strong></code>.
JOEV 0:782435d2cc06 41 *
JOEV 0:782435d2cc06 42 * @param slave_address register-address that will be edited
JOEV 0:782435d2cc06 43 * @param data 8-bit value that edit the editable bits (which are determined by the mask)
JOEV 0:782435d2cc06 44 * @param mask Bits that want to be changed, all the other bits will be saved.
JOEV 0:782435d2cc06 45 */
JOEV 0:782435d2cc06 46 void TWI_Master::i2c_writeBits(uint8_t i2c_address, uint8_t slave_address, const uint8_t data, uint8_t mask)
JOEV 0:782435d2cc06 47 {
JOEV 0:782435d2cc06 48 char current_value;
JOEV 0:782435d2cc06 49 char writeArray[2] = {slave_address, data};
JOEV 0:782435d2cc06 50
JOEV 0:782435d2cc06 51 //Get current value of desired register and mask it with mask-value
JOEV 0:782435d2cc06 52 current_value = i2c_read(i2c_address, slave_address);
JOEV 0:782435d2cc06 53 current_value &= ~mask;
JOEV 0:782435d2cc06 54
JOEV 0:782435d2cc06 55 //Combine Data with the new data (additionaly masking the data) and send it to the Sensor
JOEV 0:782435d2cc06 56 writeArray[1] = current_value | (data & mask);
JOEV 0:782435d2cc06 57
JOEV 0:782435d2cc06 58 m_i2c_sensor.write(i2c_address, writeArray, 2, false);
JOEV 0:782435d2cc06 59 }
JOEV 0:782435d2cc06 60
JOEV 0:782435d2cc06 61 /**
JOEV 0:782435d2cc06 62 * @brief Reads multiple registers
JOEV 0:782435d2cc06 63 *
JOEV 0:782435d2cc06 64 * @param slave_address the start register-address
JOEV 0:782435d2cc06 65 * @param data array, which the functions saves the data to
JOEV 0:782435d2cc06 66 * @param length the length of the read-sequence
JOEV 0:782435d2cc06 67 */
JOEV 0:782435d2cc06 68 void TWI_Master::i2c_readSeries(uint8_t i2c_address, uint8_t slave_address, uint8_t *data, uint8_t length)
JOEV 0:782435d2cc06 69 {
JOEV 0:782435d2cc06 70 char *data_array;
JOEV 0:782435d2cc06 71 data_array = reinterpret_cast<char*>(data);
JOEV 0:782435d2cc06 72
JOEV 0:782435d2cc06 73 const char temp_address = slave_address;
JOEV 0:782435d2cc06 74
JOEV 0:782435d2cc06 75 m_i2c_sensor.write(i2c_address, &temp_address, 1, true);
JOEV 0:782435d2cc06 76 m_i2c_sensor.read(i2c_address, data_array,length, false);
JOEV 0:782435d2cc06 77 }
JOEV 0:782435d2cc06 78
JOEV 0:782435d2cc06 79 /**
JOEV 0:782435d2cc06 80 * @brief Reads one register from the specified register-address.
JOEV 0:782435d2cc06 81 *
JOEV 0:782435d2cc06 82 * @param slave_address the desired register-address for reading
JOEV 0:782435d2cc06 83 * @return uint8_t returns read data
JOEV 0:782435d2cc06 84 */
JOEV 0:782435d2cc06 85 uint8_t TWI_Master::i2c_read(uint8_t i2c_address, uint8_t slave_address)
JOEV 0:782435d2cc06 86 {
JOEV 0:782435d2cc06 87 char data;
JOEV 0:782435d2cc06 88 const char temp_address = slave_address;
JOEV 0:782435d2cc06 89 m_i2c_sensor.write(i2c_address, &temp_address, 1, true);
JOEV 0:782435d2cc06 90 m_i2c_sensor.read(i2c_address, &data, 1, false);
JOEV 0:782435d2cc06 91 return data;
JOEV 0:782435d2cc06 92 }
JOEV 0:782435d2cc06 93
JOEV 0:782435d2cc06 94 /**
JOEV 0:782435d2cc06 95 * @brief Reads one register from the specified address and masks it with
JOEV 0:782435d2cc06 96 * the given mask.
JOEV 0:782435d2cc06 97 *
JOEV 0:782435d2cc06 98 * @param slave_address the desired register-address for reading
JOEV 0:782435d2cc06 99 * @param mask the mask for the desired bits
JOEV 0:782435d2cc06 100 * @return uint8_t returns the masked data
JOEV 0:782435d2cc06 101 */
JOEV 0:782435d2cc06 102 uint8_t TWI_Master::i2c_readBits(uint8_t i2c_address, uint8_t slave_address, uint8_t mask)
JOEV 0:782435d2cc06 103 {
JOEV 0:782435d2cc06 104 char data;
JOEV 0:782435d2cc06 105 const char temp_address = slave_address;
JOEV 0:782435d2cc06 106 m_i2c_sensor.write(i2c_address, &temp_address, 1, true);
JOEV 0:782435d2cc06 107 m_i2c_sensor.read(i2c_address, &data, 1, false);
JOEV 0:782435d2cc06 108 return data & mask;
JOEV 0:782435d2cc06 109 }
JOEV 0:782435d2cc06 110