A small compact Library for the I2C IO-Expander TCA9538

Committer:
joelvonrotz
Date:
Tue Jul 30 13:44:13 2019 +0000
Revision:
0:b6e63b82937d
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
joelvonrotz 0:b6e63b82937d 1 /**
joelvonrotz 0:b6e63b82937d 2 * @file TCA9538.cpp
joelvonrotz 0:b6e63b82937d 3 * @author Joel von Rotz (joel.vonrotz@maxonmotor.com)
joelvonrotz 0:b6e63b82937d 4 * @brief
joelvonrotz 0:b6e63b82937d 5 * @version 0.1
joelvonrotz 0:b6e63b82937d 6 * @date 2019-07-30
joelvonrotz 0:b6e63b82937d 7 *
joelvonrotz 0:b6e63b82937d 8 * @copyright Copyright (c) 2019, maxon motor ag - all rights reserved
joelvonrotz 0:b6e63b82937d 9 *
joelvonrotz 0:b6e63b82937d 10 */
joelvonrotz 0:b6e63b82937d 11 #include "TCA9538.h"
joelvonrotz 0:b6e63b82937d 12
joelvonrotz 0:b6e63b82937d 13 /**
joelvonrotz 0:b6e63b82937d 14 * @brief Construct a new TCA9538 object
joelvonrotz 0:b6e63b82937d 15 *
joelvonrotz 0:b6e63b82937d 16 * @param i2c_device Reference of a mbed I2C-object
joelvonrotz 0:b6e63b82937d 17 * @param address Based on the given address-configurations in the datasheet
joelvonrotz 0:b6e63b82937d 18 */
joelvonrotz 0:b6e63b82937d 19 TCA9538::TCA9538(I2C& i2c_device, uint8_t address) :
joelvonrotz 0:b6e63b82937d 20 m_i2c_address(address << 1),
joelvonrotz 0:b6e63b82937d 21 m_i2c_device(i2c_device)
joelvonrotz 0:b6e63b82937d 22 {
joelvonrotz 0:b6e63b82937d 23
joelvonrotz 0:b6e63b82937d 24 }
joelvonrotz 0:b6e63b82937d 25
joelvonrotz 0:b6e63b82937d 26 /**
joelvonrotz 0:b6e63b82937d 27 * @brief Configures the desired Register
joelvonrotz 0:b6e63b82937d 28 *
joelvonrotz 0:b6e63b82937d 29 * The register of the IO-expander can be configured by calling this set-function with
joelvonrotz 0:b6e63b82937d 30 * a register-value and the value, which will be written onto the register.
joelvonrotz 0:b6e63b82937d 31 *
joelvonrotz 0:b6e63b82937d 32 * Possible values for the registers are:
joelvonrotz 0:b6e63b82937d 33 *
joelvonrotz 0:b6e63b82937d 34 * - TCA9538::INPUT
joelvonrotz 0:b6e63b82937d 35 * - TCA9538::OUTPUT
joelvonrotz 0:b6e63b82937d 36 * - TCA9538::POLARITY
joelvonrotz 0:b6e63b82937d 37 * - TCA9538::CONFIG
joelvonrotz 0:b6e63b82937d 38 *
joelvonrotz 0:b6e63b82937d 39 * @param index the desired register to be changed
joelvonrotz 0:b6e63b82937d 40 * @param value the value written on the given register
joelvonrotz 0:b6e63b82937d 41 * @return true no success
joelvonrotz 0:b6e63b82937d 42 * @return false success
joelvonrotz 0:b6e63b82937d 43 */
joelvonrotz 0:b6e63b82937d 44 bool TCA9538::set(tca9538_registers_t index, uint8_t value)
joelvonrotz 0:b6e63b82937d 45 {
joelvonrotz 0:b6e63b82937d 46 bool status;
joelvonrotz 0:b6e63b82937d 47
joelvonrotz 0:b6e63b82937d 48 if(index == INPUT)
joelvonrotz 0:b6e63b82937d 49 {
joelvonrotz 0:b6e63b82937d 50 return 1;
joelvonrotz 0:b6e63b82937d 51 }
joelvonrotz 0:b6e63b82937d 52
joelvonrotz 0:b6e63b82937d 53 char data[2] = {index, value};
joelvonrotz 0:b6e63b82937d 54 status = m_i2c_device.write(m_i2c_address, data, 2);
joelvonrotz 0:b6e63b82937d 55 return status;
joelvonrotz 0:b6e63b82937d 56 }
joelvonrotz 0:b6e63b82937d 57
joelvonrotz 0:b6e63b82937d 58 /**
joelvonrotz 0:b6e63b82937d 59 * @brief Returns Value of desired Register
joelvonrotz 0:b6e63b82937d 60 *
joelvonrotz 0:b6e63b82937d 61 * Returns the value from the given register. The parameter value is the same
joelvonrotz 0:b6e63b82937d 62 * as in TCA9538::set(tca9538_registers_t index, uint8_t value).
joelvonrotz 0:b6e63b82937d 63 *
joelvonrotz 0:b6e63b82937d 64 * @param index Register to be read from
joelvonrotz 0:b6e63b82937d 65 * @return uint8_t The Value of the desired Register
joelvonrotz 0:b6e63b82937d 66 */
joelvonrotz 0:b6e63b82937d 67 uint8_t TCA9538::get(tca9538_registers_t index)
joelvonrotz 0:b6e63b82937d 68 {
joelvonrotz 0:b6e63b82937d 69 bool status;
joelvonrotz 0:b6e63b82937d 70 char value;
joelvonrotz 0:b6e63b82937d 71 char char_index = index;
joelvonrotz 0:b6e63b82937d 72
joelvonrotz 0:b6e63b82937d 73 status = m_i2c_device.write(m_i2c_address, &char_index, 1, true);
joelvonrotz 0:b6e63b82937d 74 status |= m_i2c_device.read(m_i2c_address, &value, 1, false);
joelvonrotz 0:b6e63b82937d 75 return status;
joelvonrotz 0:b6e63b82937d 76 }