Firmware SDK
twr_tca9534a.c
1 #include <twr_tca9534a.h>
2 
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
7 
8 bool twr_tca9534a_init(twr_tca9534a_t *self, twr_i2c_channel_t i2c_channel, uint8_t i2c_address)
9 {
10  memset(self, 0, sizeof(*self));
11 
12  self->_i2c_channel = i2c_channel;
13  self->_i2c_address = i2c_address;
14 
15  twr_i2c_init(self->_i2c_channel, TWR_I2C_SPEED_400_KHZ);
16 
17  if (!twr_i2c_memory_read_8b(self->_i2c_channel, self->_i2c_address, TWR_TCA9534A_REGISTER_CONFIGURATION, &self->_direction))
18  {
19  return false;
20  }
21 
22  if (!twr_i2c_memory_read_8b(self->_i2c_channel, self->_i2c_address, TWR_TCA9534A_REGISTER_OUTPUT_PORT, &self->_output_port))
23  {
24  return false;
25  }
26 
27  return true;
28 }
29 
30 bool twr_tca9534a_read_port(twr_tca9534a_t *self, uint8_t *value)
31 {
32  return twr_i2c_memory_read_8b(self->_i2c_channel, self->_i2c_address, TWR_TCA9534A_REGISTER_INPUT_PORT, value);
33 }
34 
35 bool twr_tca9534a_write_port(twr_tca9534a_t *self, uint8_t value)
36 {
37  if (!twr_i2c_memory_write_8b(self->_i2c_channel, self->_i2c_address, TWR_TCA9534A_REGISTER_OUTPUT_PORT, value))
38  {
39  return false;
40  }
41 
42  self->_output_port = value;
43 
44  return true;
45 }
46 
48 {
49  uint8_t port;
50 
51  if (!twr_tca9534a_read_port(self, &port))
52  {
53  return false;
54  }
55 
56  *value = ((port >> (uint8_t) pin) & 0x01);
57 
58  return true;
59 }
60 
62 {
63  uint8_t port = self->_output_port;
64 
65  port &= ~(1 << (uint8_t) pin);
66 
67  if (value != 0)
68  {
69  port |= 1 << (uint8_t) pin;
70  }
71 
72  if (!twr_tca9534a_write_port(self, port))
73  {
74  return false;
75  }
76 
77  return true;
78 }
79 
80 bool twr_tca9534a_get_port_direction(twr_tca9534a_t *self, uint8_t *direction)
81 {
82  *direction = self->_direction;
83 
84  return true;
85 }
86 
87 bool twr_tca9534a_set_port_direction(twr_tca9534a_t *self, uint8_t direction)
88 {
89  if (!twr_i2c_memory_write_8b(self->_i2c_channel, self->_i2c_address, TWR_TCA9534A_REGISTER_CONFIGURATION, direction))
90  {
91  return false;
92  }
93 
94  self->_direction = direction;
95 
96  return true;
97 }
98 
100 {
101  if (((self->_direction >> (uint8_t) pin) & 1) == 0)
102  {
103  *direction = TWR_TCA9534A_PIN_DIRECTION_OUTPUT;
104  }
105  else
106  {
107  *direction = TWR_TCA9534A_PIN_DIRECTION_INPUT;
108  }
109 
110  return true;
111 }
112 
114 {
115  uint8_t port_direction = self->_direction;
116 
117  port_direction &= ~(1 << (uint8_t) pin);
118 
119  if (direction == TWR_TCA9534A_PIN_DIRECTION_INPUT)
120  {
121  port_direction |= 1 << (uint8_t) pin;
122  }
123 
124  if (!twr_tca9534a_set_port_direction(self, port_direction))
125  {
126  return false;
127  }
128 
129  return true;
130 }
void twr_i2c_init(twr_i2c_channel_t channel, twr_i2c_speed_t speed)
Initialize I2C channel.
Definition: twr_i2c.c:57
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.
Definition: twr_i2c.c:408
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.
Definition: twr_i2c.c:437
twr_i2c_channel_t
I2C channels.
Definition: twr_i2c.h:16
@ TWR_I2C_SPEED_400_KHZ
I2C communication speed is 400 kHz.
Definition: twr_i2c.h:36
bool twr_tca9534a_read_port(twr_tca9534a_t *self, uint8_t *value)
Read state of all pins.
Definition: twr_tca9534a.c:30
bool twr_tca9534a_get_port_direction(twr_tca9534a_t *self, uint8_t *direction)
Get direction of all pins.
Definition: twr_tca9534a.c:80
bool twr_tca9534a_write_port(twr_tca9534a_t *self, uint8_t value)
Write state to all pins.
Definition: twr_tca9534a.c:35
twr_tca9534a_pin_direction_t
Pin direction.
Definition: twr_tca9534a.h:32
bool twr_tca9534a_get_pin_direction(twr_tca9534a_t *self, twr_tca9534a_pin_t pin, twr_tca9534a_pin_direction_t *direction)
Get pin direction.
Definition: twr_tca9534a.c:99
bool twr_tca9534a_read_pin(twr_tca9534a_t *self, twr_tca9534a_pin_t pin, int *value)
Read pin state.
Definition: twr_tca9534a.c:47
bool twr_tca9534a_set_pin_direction(twr_tca9534a_t *self, twr_tca9534a_pin_t pin, twr_tca9534a_pin_direction_t direction)
Set pin direction.
Definition: twr_tca9534a.c:113
twr_tca9534a_pin_t
Individual pin names.
Definition: twr_tca9534a.h:17
bool twr_tca9534a_init(twr_tca9534a_t *self, twr_i2c_channel_t i2c_channel, uint8_t i2c_address)
Initialize TCA9534A.
Definition: twr_tca9534a.c:8
bool twr_tca9534a_set_port_direction(twr_tca9534a_t *self, uint8_t direction)
Set direction of all pins.
Definition: twr_tca9534a.c:87
bool twr_tca9534a_write_pin(twr_tca9534a_t *self, twr_tca9534a_pin_t pin, int value)
Write pin state.
Definition: twr_tca9534a.c:61
Pin state.
Definition: twr_tca9534a.h:43