Firmware SDK
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
bc_tca9534a.c
1 #include <bc_tca9534a.h>
2 
3 #define BC_TCA9534A_REGISTER_INPUT_PORT 0x00
4 #define BC_TCA9534A_REGISTER_OUTPUT_PORT 0x01
5 #define BC_TCA9534A_REGISTER_POLARITY_INVERSION 0x02
6 #define BC_TCA9534A_REGISTER_CONFIGURATION 0x03
7 
8 bool bc_tca9534a_init(bc_tca9534a_t *self, bc_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  bc_i2c_init(self->_i2c_channel, BC_I2C_SPEED_400_KHZ);
16 
17  if (!bc_i2c_memory_read_8b(self->_i2c_channel, self->_i2c_address, BC_TCA9534A_REGISTER_CONFIGURATION, &self->_direction))
18  {
19  return false;
20  }
21 
22  if (!bc_i2c_memory_read_8b(self->_i2c_channel, self->_i2c_address, BC_TCA9534A_REGISTER_OUTPUT_PORT, &self->_output_port))
23  {
24  return false;
25  }
26 
27  return true;
28 }
29 
30 bool bc_tca9534a_read_port(bc_tca9534a_t *self, uint8_t *value)
31 {
32  return bc_i2c_memory_read_8b(self->_i2c_channel, self->_i2c_address, BC_TCA9534A_REGISTER_INPUT_PORT, value);
33 }
34 
35 bool bc_tca9534a_write_port(bc_tca9534a_t *self, uint8_t value)
36 {
37  if (!bc_i2c_memory_write_8b(self->_i2c_channel, self->_i2c_address, BC_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 (!bc_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 (!bc_tca9534a_write_port(self, port))
73  {
74  return false;
75  }
76 
77  return true;
78 }
79 
80 bool bc_tca9534a_get_port_direction(bc_tca9534a_t *self, uint8_t *direction)
81 {
82  *direction = self->_direction;
83 
84  return true;
85 }
86 
87 bool bc_tca9534a_set_port_direction(bc_tca9534a_t *self, uint8_t direction)
88 {
89  if (!bc_i2c_memory_write_8b(self->_i2c_channel, self->_i2c_address, BC_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 = BC_TCA9534A_PIN_DIRECTION_OUTPUT;
104  }
105  else
106  {
107  *direction = BC_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 == BC_TCA9534A_PIN_DIRECTION_INPUT)
120  {
121  port_direction |= 1 << (uint8_t) pin;
122  }
123 
124  if (!bc_tca9534a_set_port_direction(self, port_direction))
125  {
126  return false;
127  }
128 
129  return true;
130 }
I2C communication speed is 400 kHz.
Definition: bc_i2c.h:36
bool bc_tca9534a_set_port_direction(bc_tca9534a_t *self, uint8_t direction)
Set direction of all pins.
Definition: bc_tca9534a.c:87
bool bc_tca9534a_write_pin(bc_tca9534a_t *self, bc_tca9534a_pin_t pin, int value)
Write pin state.
Definition: bc_tca9534a.c:61
Pin state.
Definition: bc_tca9534a.h:42
bool bc_i2c_memory_read_8b(bc_i2c_channel_t channel, uint8_t device_address, uint32_t memory_address, uint8_t *data)
Memory read 1 byte from I2C channel.
Definition: bc_i2c.c:431
bool bc_tca9534a_write_port(bc_tca9534a_t *self, uint8_t value)
Write state to all pins.
Definition: bc_tca9534a.c:35
void bc_i2c_init(bc_i2c_channel_t channel, bc_i2c_speed_t speed)
Initialize I2C channel.
Definition: bc_i2c.c:54
bool bc_tca9534a_read_port(bc_tca9534a_t *self, uint8_t *value)
Read state of all pins.
Definition: bc_tca9534a.c:30
bool bc_tca9534a_read_pin(bc_tca9534a_t *self, bc_tca9534a_pin_t pin, int *value)
Read pin state.
Definition: bc_tca9534a.c:47
bool bc_tca9534a_get_port_direction(bc_tca9534a_t *self, uint8_t *direction)
Get direction of all pins.
Definition: bc_tca9534a.c:80
bool bc_tca9534a_init(bc_tca9534a_t *self, bc_i2c_channel_t i2c_channel, uint8_t i2c_address)
Initialize TCA9534A.
Definition: bc_tca9534a.c:8
bc_i2c_channel_t
I2C channels.
Definition: bc_i2c.h:15
bc_tca9534a_pin_t
Individual pin names.
Definition: bc_tca9534a.h:16
bool bc_tca9534a_get_pin_direction(bc_tca9534a_t *self, bc_tca9534a_pin_t pin, bc_tca9534a_pin_direction_t *direction)
Get pin direction.
Definition: bc_tca9534a.c:99
bool bc_tca9534a_set_pin_direction(bc_tca9534a_t *self, bc_tca9534a_pin_t pin, bc_tca9534a_pin_direction_t direction)
Set pin direction.
Definition: bc_tca9534a.c:113
bc_tca9534a_pin_direction_t
Pin direction.
Definition: bc_tca9534a.h:31
bool bc_i2c_memory_write_8b(bc_i2c_channel_t channel, uint8_t device_address, uint32_t memory_address, uint8_t data)
Memory write 1 byte to I2C channel.
Definition: bc_i2c.c:402