1 #include <twr_tca9534a.h>
3 #define TWR_TCA9534A_REGISTER_INPUT_PORT 0x00
4 #define TWR_TCA9534A_REGISTER_OUTPUT_PORT 0x01
5 #define TWR_TCA9534A_REGISTER_POLARITY_INVERSION 0x02
6 #define TWR_TCA9534A_REGISTER_CONFIGURATION 0x03
10 memset(
self, 0,
sizeof(*
self));
12 self->_i2c_channel = i2c_channel;
13 self->_i2c_address = i2c_address;
17 if (!
twr_i2c_memory_read_8b(self->_i2c_channel, self->_i2c_address, TWR_TCA9534A_REGISTER_CONFIGURATION, &self->_direction))
22 if (!
twr_i2c_memory_read_8b(self->_i2c_channel, self->_i2c_address, TWR_TCA9534A_REGISTER_OUTPUT_PORT, &self->_output_port))
32 return twr_i2c_memory_read_8b(self->_i2c_channel, self->_i2c_address, TWR_TCA9534A_REGISTER_INPUT_PORT, value);
42 self->_output_port = value;
56 *value = ((port >> (uint8_t) pin) & 0x01);
63 uint8_t port =
self->_output_port;
65 port &= ~(1 << (uint8_t) pin);
69 port |= 1 << (uint8_t) pin;
82 *direction =
self->_direction;
89 if (!
twr_i2c_memory_write_8b(self->_i2c_channel, self->_i2c_address, TWR_TCA9534A_REGISTER_CONFIGURATION, direction))
94 self->_direction = direction;
101 if (((self->_direction >> (uint8_t) pin) & 1) == 0)
103 *direction = TWR_TCA9534A_PIN_DIRECTION_OUTPUT;
107 *direction = TWR_TCA9534A_PIN_DIRECTION_INPUT;
115 uint8_t port_direction =
self->_direction;
117 port_direction &= ~(1 << (uint8_t) pin);
119 if (direction == TWR_TCA9534A_PIN_DIRECTION_INPUT)
121 port_direction |= 1 << (uint8_t) pin;
void twr_i2c_init(twr_i2c_channel_t channel, twr_i2c_speed_t speed)
Initialize I2C channel.
bool twr_i2c_memory_write_8b(twr_i2c_channel_t channel, uint8_t device_address, uint32_t memory_address, uint8_t data)
Memory write 1 byte to I2C channel.
bool twr_i2c_memory_read_8b(twr_i2c_channel_t channel, uint8_t device_address, uint32_t memory_address, uint8_t *data)
Memory read 1 byte from I2C channel.
twr_i2c_channel_t
I2C channels.
@ TWR_I2C_SPEED_400_KHZ
I2C communication speed is 400 kHz.
bool twr_tca9534a_read_port(twr_tca9534a_t *self, uint8_t *value)
Read state of all pins.
bool twr_tca9534a_get_port_direction(twr_tca9534a_t *self, uint8_t *direction)
Get direction of all pins.
bool twr_tca9534a_write_port(twr_tca9534a_t *self, uint8_t value)
Write state to all pins.
twr_tca9534a_pin_direction_t
Pin direction.
bool twr_tca9534a_get_pin_direction(twr_tca9534a_t *self, twr_tca9534a_pin_t pin, twr_tca9534a_pin_direction_t *direction)
Get pin direction.
bool twr_tca9534a_read_pin(twr_tca9534a_t *self, twr_tca9534a_pin_t pin, int *value)
Read pin state.
bool twr_tca9534a_set_pin_direction(twr_tca9534a_t *self, twr_tca9534a_pin_t pin, twr_tca9534a_pin_direction_t direction)
Set pin direction.
twr_tca9534a_pin_t
Individual pin names.
bool twr_tca9534a_init(twr_tca9534a_t *self, twr_i2c_channel_t i2c_channel, uint8_t i2c_address)
Initialize TCA9534A.
bool twr_tca9534a_set_port_direction(twr_tca9534a_t *self, uint8_t direction)
Set direction of all pins.
bool twr_tca9534a_write_pin(twr_tca9534a_t *self, twr_tca9534a_pin_t pin, int value)
Write pin state.