Firmware SDK
twr_module_sensor.c
1 #include <twr_module_sensor.h>
2 #include <twr_tca9534a.h>
3 #include <twr_onewire_gpio.h>
4 #include <twr_log.h>
5 #include <twr_error.h>
6 
7 #define _TWR_MODULE_SENSOR_INITIALIZED_STATE 0xff
8 #define _TWR_MODULE_SENSOR_INITIALIZED_DIRECTION 0x00
9 #define _TWR_MODULE_SENSOR_VDD (1 << 3)
10 #define _TWR_MODULE_SENSOR_CH_A_56R (1 << 4)
11 #define _TWR_MODULE_SENSOR_CH_A_4K7 (1 << 5)
12 #define _TWR_MODULE_SENSOR_CH_B_4K7 (1 << 6)
13 #define _TWR_MODULE_SENSOR_CH_B_56R (1 << 7)
14 
15 static struct
16 {
17  bool initialized;
18  twr_tca9534a_t tca9534a;
20  int onewire_power_semaphore;
21  bool onewire_initialized;
22  twr_onewire_t onewire;
23 
24 } _twr_module_sensor = {
25  .initialized = false,
26  .onewire_initialized = false
27 };
28 
29 static const twr_gpio_channel_t _twr_module_sensor_channel_gpio_lut[3] =
30 {
34 };
35 
37 {
38  if (!_twr_module_sensor.initialized)
39  {
40  if (!twr_tca9534a_init(&_twr_module_sensor.tca9534a, TWR_I2C_I2C0, 0x3e))
41  {
42  return false;
43  }
44 
45  if (!twr_tca9534a_write_port(&_twr_module_sensor.tca9534a, _TWR_MODULE_SENSOR_INITIALIZED_STATE))
46  {
47  return false;
48  }
49 
50  if (!twr_tca9534a_set_port_direction(&_twr_module_sensor.tca9534a, _TWR_MODULE_SENSOR_INITIALIZED_DIRECTION))
51  {
52  return false;
53  }
54 
55  twr_gpio_init(_twr_module_sensor_channel_gpio_lut[TWR_MODULE_SENSOR_CHANNEL_A]);
56  twr_gpio_init(_twr_module_sensor_channel_gpio_lut[TWR_MODULE_SENSOR_CHANNEL_B]);
57  twr_gpio_init(_twr_module_sensor_channel_gpio_lut[TWR_MODULE_SENSOR_CHANNEL_C]);
58 
59  _twr_module_sensor.initialized = true;
60  }
61 
62  return true;
63 }
64 
66 {
67  _twr_module_sensor.initialized = false;
68 }
69 
71 {
72  uint8_t port_actual;
73  uint8_t port_new;
74 
75  port_actual = _twr_module_sensor.tca9534a._output_port;
76 // twr_tca9534a_read_port(&_twr_module_sensor.tca9534a, &port_actual);
77 
78  port_new = port_actual;
79 
80  switch (channel)
81  {
83  {
84  port_new |= _TWR_MODULE_SENSOR_CH_A_4K7 | _TWR_MODULE_SENSOR_CH_A_56R;
85  break;
86  }
88  {
89  port_new |= _TWR_MODULE_SENSOR_CH_B_4K7 | _TWR_MODULE_SENSOR_CH_B_56R;
90  break;
91  }
93  default:
94  {
95  break;
96  }
97  }
98 
99  switch (pull)
100  {
102  {
103  // Done by OR mask above
104 
105  twr_gpio_set_pull(_twr_module_sensor_channel_gpio_lut[channel], TWR_GPIO_PULL_NONE);
106 
107  break;
108  }
110  {
111  switch (channel)
112  {
114  {
115  port_new &= ~_TWR_MODULE_SENSOR_CH_A_4K7;
116  break;
117  }
119  {
120  port_new &= ~_TWR_MODULE_SENSOR_CH_B_4K7;
121  break;
122  }
124  default:
125  {
126  return false;
127  }
128  }
129  break;
130  }
132  {
133  switch (channel)
134  {
136  {
137  port_new &= ~_TWR_MODULE_SENSOR_CH_A_56R;
138  break;
139  }
141  {
142  port_new &= ~_TWR_MODULE_SENSOR_CH_B_56R;
143  break;
144  }
146  default:
147  {
148  return false;
149  }
150  }
151  break;
152  }
154  {
155  twr_gpio_set_pull(_twr_module_sensor_channel_gpio_lut[channel], TWR_GPIO_PULL_UP);
156  break;
157  }
159  {
160  twr_gpio_set_pull(_twr_module_sensor_channel_gpio_lut[channel], TWR_GPIO_PULL_DOWN);
161  break;
162  }
163  default:
164  {
165  return false;
166  }
167  }
168 
169  if (port_actual != port_new)
170  {
171  twr_gpio_set_pull(_twr_module_sensor_channel_gpio_lut[channel], TWR_GPIO_PULL_NONE);
172  return twr_tca9534a_write_port(&_twr_module_sensor.tca9534a, port_new);
173  }
174 
175  return true;
176 }
177 
179 {
180  if (channel == TWR_MODULE_SENSOR_CHANNEL_A)
181  {
182  uint8_t port_actual = _twr_module_sensor.tca9534a._output_port;
183 
184  if ((port_actual & _TWR_MODULE_SENSOR_CH_A_4K7) == 0)
185  {
187  }
188 
189  if ((port_actual & _TWR_MODULE_SENSOR_CH_A_56R) == 0)
190  {
192  }
193  }
194  if (channel == TWR_MODULE_SENSOR_CHANNEL_B)
195  {
196  uint8_t port_actual = _twr_module_sensor.tca9534a._output_port;
197 
198  if ((port_actual & _TWR_MODULE_SENSOR_CH_B_4K7) == 0)
199  {
201  }
202 
203  if ((port_actual & _TWR_MODULE_SENSOR_CH_B_56R) == 0)
204  {
206  }
207  }
208 
209  return (twr_module_sensor_pull_t) twr_gpio_get_pull(_twr_module_sensor_channel_gpio_lut[channel]);
210 }
211 
213 {
214  twr_gpio_set_mode(_twr_module_sensor_channel_gpio_lut[channel], (twr_gpio_mode_t) mode);
215 }
216 
218 {
219  return twr_gpio_get_input(_twr_module_sensor_channel_gpio_lut[channel]);
220 }
221 
223 {
224  twr_gpio_set_output(_twr_module_sensor_channel_gpio_lut[channel], state);
225 }
226 
228 {
229  return twr_gpio_get_output(_twr_module_sensor_channel_gpio_lut[channel]);
230 }
231 
233 {
234  twr_gpio_toggle_output(_twr_module_sensor_channel_gpio_lut[channel]);
235 }
236 
238 {
239  uint8_t port_actual;
240  uint8_t port_new;
241 
243  {
244  return false;
245  }
246 
247  port_actual = _twr_module_sensor.tca9534a._output_port;
248 
249  if (on)
250  {
251  port_new = port_actual & ~_TWR_MODULE_SENSOR_VDD;
252  }
253  else
254  {
255  port_new = port_actual | _TWR_MODULE_SENSOR_VDD;
256  }
257 
258  if (port_actual != port_new)
259  {
260  return twr_tca9534a_write_port(&_twr_module_sensor.tca9534a, port_new);
261  }
262 
263  return true;
264 }
265 
267 {
268  if (!_twr_module_sensor.initialized)
269  {
271  }
272 
273  if (_twr_module_sensor.revision == TWR_MODULE_SENSOR_REVISION_UNKNOWN)
274  {
275  if (!twr_tca9534a_set_pin_direction(&_twr_module_sensor.tca9534a, TWR_TCA9534A_PIN_P1, TWR_TCA9534A_PIN_DIRECTION_INPUT))
276  {
278  }
279 
280  uint8_t test_vector = 0x86;
281  int value_input;
282  int value_output;
283 
284  for (size_t i = 0; i < sizeof(test_vector); i++)
285  {
286  value_output = (test_vector >> i) & 0x01;
287 
288  if (!twr_tca9534a_write_pin(&_twr_module_sensor.tca9534a, TWR_TCA9534A_PIN_P0, value_output))
289  {
291  }
292 
293  if (!twr_tca9534a_read_pin(&_twr_module_sensor.tca9534a, TWR_TCA9534A_PIN_P1, &value_input))
294  {
296  }
297 
298  if (value_output != value_input)
299  {
300  _twr_module_sensor.revision = TWR_MODULE_SENSOR_REVISION_R1_0;
301 
302  return _twr_module_sensor.revision;
303  }
304  }
305 
306  _twr_module_sensor.revision = TWR_MODULE_SENSOR_REVISION_R1_1;
307  }
308 
309  return _twr_module_sensor.revision;
310 }
311 
313 {
314  if (!_twr_module_sensor.onewire_initialized)
315  {
316  twr_onewire_gpio_init(&_twr_module_sensor.onewire, TWR_GPIO_P5);
317 
318  _twr_module_sensor.onewire_initialized = true;
319  }
320 
321  return &_twr_module_sensor.onewire;
322 }
323 
325 {
326  if (++_twr_module_sensor.onewire_power_semaphore == 1)
327  {
329  {
331  {
333  {
334  return false;
335  }
336  break;
337  }
339  {
340  twr_gpio_set_mode(_twr_module_sensor_channel_gpio_lut[TWR_MODULE_SENSOR_CHANNEL_A], TWR_GPIO_MODE_ANALOG);
341 
343  {
344  return false;
345  }
346  break;
347  }
349  default:
350  {
351  return false;
352  }
353  }
354 
356  {
357  return false;
358  }
359  }
360  return true;
361 }
362 
364 {
365  if (_twr_module_sensor.onewire_power_semaphore < 1) twr_error(TWR_ERROR_ERROR_UNLOCK);
366 
367  if (--_twr_module_sensor.onewire_power_semaphore == 0)
368  {
370  {
372  {
374  {
375  return false;
376  }
377  break;
378  }
380  {
382  {
383  return false;
384  }
385  break;
386  }
388  default:
389  {
390  return false;
391  }
392  }
393 
395  {
396  return false;
397  }
398  }
399 
400  return true;
401 }
twr_gpio_pull_t twr_gpio_get_pull(twr_gpio_channel_t channel)
Get pull-up/pull-down configuration for GPIO channel.
Definition: twr_gpio.c:361
void twr_gpio_set_output(twr_gpio_channel_t channel, int state)
Set output state for GPIO channel.
Definition: twr_gpio.c:471
void twr_gpio_set_pull(twr_gpio_channel_t channel, twr_gpio_pull_t pull)
Set pull-up/pull-down configuration for GPIO channel.
Definition: twr_gpio.c:340
void twr_gpio_init(twr_gpio_channel_t channel)
Initialize GPIO channel.
Definition: twr_gpio.c:325
twr_gpio_mode_t
GPIO mode of operation.
Definition: twr_gpio.h:103
twr_gpio_channel_t
GPIO channels.
Definition: twr_gpio.h:13
void twr_gpio_toggle_output(twr_gpio_channel_t channel)
Toggle output state for GPIO channel.
Definition: twr_gpio.c:483
int twr_gpio_get_output(twr_gpio_channel_t channel)
Get output state for GPIO channel.
Definition: twr_gpio.c:477
int twr_gpio_get_input(twr_gpio_channel_t channel)
Get input state for GPIO channel.
Definition: twr_gpio.c:465
void twr_gpio_set_mode(twr_gpio_channel_t channel, twr_gpio_mode_t mode)
Set mode of operation for GPIO channel.
Definition: twr_gpio.c:367
@ TWR_GPIO_MODE_ANALOG
GPIO channel operates in analog mode.
Definition: twr_gpio.h:114
@ TWR_GPIO_P5
GPIO channel P5, A5, DAC1.
Definition: twr_gpio.h:30
@ TWR_GPIO_P4
GPIO channel P4, A4, DAC0.
Definition: twr_gpio.h:27
@ TWR_GPIO_P7
GPIO channel P7, A6.
Definition: twr_gpio.h:36
@ TWR_GPIO_PULL_NONE
GPIO channel has no pull-up/pull-down.
Definition: twr_gpio.h:90
@ TWR_GPIO_PULL_DOWN
GPIO channel has pull-down.
Definition: twr_gpio.h:96
@ TWR_GPIO_PULL_UP
GPIO channel has pull-up.
Definition: twr_gpio.h:93
@ TWR_I2C_I2C0
I2C channel I2C0.
Definition: twr_i2c.h:18
twr_module_sensor_revision_t twr_module_sensor_get_revision(void)
Get Sensor Module revision.
void twr_module_sensor_set_mode(twr_module_sensor_channel_t channel, twr_module_sensor_mode_t mode)
Set output mode of Sensor Module channel.
int twr_module_sensor_get_output(twr_module_sensor_channel_t channel)
Get output state for Sensor Module channel.
void twr_module_sensor_toggle_output(twr_module_sensor_channel_t channel)
Toggle output state for Sensor Module channel.
twr_module_sensor_revision_t
Sensor Module hardware revision.
int twr_module_sensor_get_input(twr_module_sensor_channel_t channel)
Get input of Sensor Module channel.
twr_module_sensor_pull_t twr_module_sensor_get_pull(twr_module_sensor_channel_t channel)
Get pull-up/pull-down configuration for Sensor Module channel.
twr_onewire_t * twr_module_sensor_get_onewire(void)
Initialize and get Instance 1-Wire for channel B.
void twr_module_sensor_set_output(twr_module_sensor_channel_t channel, int state)
Set output state of Sensor Module channel.
bool twr_module_sensor_onewire_power_down(void)
Semaphore for 1Wire Power down: for R1.1 set VDD Off, for R1.0 pull none on channel A.
bool twr_module_sensor_onewire_power_up(void)
Semaphore for 1Wire Power up: for R1.1 set VDD On, for R1.0 pull up 56R on channel A.
bool twr_module_sensor_set_vdd(bool on)
Set VDD On / Off.
bool twr_module_sensor_init(void)
Initialize Sensor Module.
twr_module_sensor_mode_t
Sensor Module mode of operation.
twr_module_sensor_pull_t
Sensor module pull.
bool twr_module_sensor_set_pull(twr_module_sensor_channel_t channel, twr_module_sensor_pull_t pull)
Set pull of Sensor Module channel.
twr_module_sensor_channel_t
Sensor Module channels.
void twr_module_sensor_deinit(void)
Deinitialize Sensor Module.
@ TWR_MODULE_SENSOR_REVISION_UNKNOWN
Hardware revision unknown.
@ TWR_MODULE_SENSOR_REVISION_R1_0
Hardware revision R1.0.
@ TWR_MODULE_SENSOR_REVISION_R1_1
Hardware revision R1.1.
@ TWR_MODULE_SENSOR_PULL_UP_4K7
Channel has pull-up 4k7.
@ TWR_MODULE_SENSOR_PULL_DOWN_INTERNAL
Channel has internal pull-down.
@ TWR_MODULE_SENSOR_PULL_UP_INTERNAL
Channel has internal pull-up.
@ TWR_MODULE_SENSOR_PULL_NONE
Channel has no pull.
@ TWR_MODULE_SENSOR_PULL_UP_56R
Channel has pull-up 56R.
@ TWR_MODULE_SENSOR_CHANNEL_B
Channel B.
@ TWR_MODULE_SENSOR_CHANNEL_A
Channel A.
@ TWR_MODULE_SENSOR_CHANNEL_C
Channel C.
struct twr_onewire_t twr_onewire_t
1-Wire instance
Definition: twr_onewire.h:14
void twr_onewire_gpio_init(twr_onewire_t *onewire, twr_gpio_channel_t channel)
Initialize 1-Wire.
bool twr_tca9534a_write_port(twr_tca9534a_t *self, uint8_t state)
Write state to all pins.
Definition: twr_tca9534a.c:35
bool twr_tca9534a_read_pin(twr_tca9534a_t *self, twr_tca9534a_pin_t pin, int *state)
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
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 state)
Write pin state.
Definition: twr_tca9534a.c:61
Pin state.
Definition: twr_tca9534a.h:43