Firmware SDK
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
bc_module_sensor.c
1 #include <bc_module_sensor.h>
2 #include <bc_tca9534a.h>
3 #include <bc_onewire.h>
4 
5 #define _BC_MODULE_SENSOR_INITIALIZED_STATE 0xff
6 #define _BC_MODULE_SENSOR_INITIALIZED_DIRECTION 0x00
7 #define _BC_MODULE_SENSOR_VDD (1 << 3)
8 #define _BC_MODULE_SENSOR_CH_A_56R (1 << 4)
9 #define _BC_MODULE_SENSOR_CH_A_4K7 (1 << 5)
10 #define _BC_MODULE_SENSOR_CH_B_4K7 (1 << 6)
11 #define _BC_MODULE_SENSOR_CH_B_56R (1 << 7)
12 
13 static struct
14 {
15  bool initialized;
16  bc_tca9534a_t tca9534a;
18  int onewire_init_semaphore;
19 
20 } _bc_module_sensor;
21 
22 static const bc_gpio_channel_t _bc_module_sensor_channel_gpio_lut[3] =
23 {
27 };
28 
30 {
31  if (!_bc_module_sensor.initialized)
32  {
33  if (!bc_tca9534a_init(&_bc_module_sensor.tca9534a, BC_I2C_I2C0, 0x3e))
34  {
35  return false;
36  }
37 
38  if (!bc_tca9534a_write_port(&_bc_module_sensor.tca9534a, _BC_MODULE_SENSOR_INITIALIZED_STATE))
39  {
40  return false;
41  }
42 
43  if (!bc_tca9534a_set_port_direction(&_bc_module_sensor.tca9534a, _BC_MODULE_SENSOR_INITIALIZED_DIRECTION))
44  {
45  return false;
46  }
47 
48  bc_gpio_init(_bc_module_sensor_channel_gpio_lut[BC_MODULE_SENSOR_CHANNEL_A]);
49  bc_gpio_init(_bc_module_sensor_channel_gpio_lut[BC_MODULE_SENSOR_CHANNEL_B]);
50  bc_gpio_init(_bc_module_sensor_channel_gpio_lut[BC_MODULE_SENSOR_CHANNEL_C]);
51 
52  _bc_module_sensor.initialized = true;
53  }
54 
55  return true;
56 }
57 
59 {
60  _bc_module_sensor.initialized = false;
61 }
62 
64 {
65  uint8_t port_actual;
66  uint8_t port_new;
67 
68  port_actual = _bc_module_sensor.tca9534a._output_port;
69 // bc_tca9534a_read_port(&_bc_module_sensor.tca9534a, &port_actual);
70 
71  port_new = port_actual;
72 
73  switch (channel)
74  {
76  {
77  port_new |= _BC_MODULE_SENSOR_CH_A_4K7 | _BC_MODULE_SENSOR_CH_A_56R;
78  break;
79  }
81  {
82  port_new |= _BC_MODULE_SENSOR_CH_B_4K7 | _BC_MODULE_SENSOR_CH_B_56R;
83  break;
84  }
86  default:
87  {
88  break;
89  }
90  }
91 
92  switch (pull)
93  {
95  {
96  // Done by OR mask above
97 
98  bc_gpio_set_pull(_bc_module_sensor_channel_gpio_lut[channel], BC_GPIO_PULL_NONE);
99 
100  break;
101  }
103  {
104  switch (channel)
105  {
107  {
108  port_new &= ~_BC_MODULE_SENSOR_CH_A_4K7;
109  break;
110  }
112  {
113  port_new &= ~_BC_MODULE_SENSOR_CH_B_4K7;
114  break;
115  }
117  default:
118  {
119  return false;
120  }
121  }
122  break;
123  }
125  {
126  switch (channel)
127  {
129  {
130  port_new &= ~_BC_MODULE_SENSOR_CH_A_56R;
131  break;
132  }
134  {
135  port_new &= ~_BC_MODULE_SENSOR_CH_B_56R;
136  break;
137  }
139  default:
140  {
141  return false;
142  }
143  }
144  break;
145  }
147  {
148  bc_gpio_set_pull(_bc_module_sensor_channel_gpio_lut[channel], BC_GPIO_PULL_UP);
149  break;
150  }
152  {
153  bc_gpio_set_pull(_bc_module_sensor_channel_gpio_lut[channel], BC_GPIO_PULL_DOWN);
154  break;
155  }
156  default:
157  {
158  return false;
159  }
160  }
161 
162  if (port_actual != port_new)
163  {
164  bc_gpio_set_pull(_bc_module_sensor_channel_gpio_lut[channel], BC_GPIO_PULL_NONE);
165  return bc_tca9534a_write_port(&_bc_module_sensor.tca9534a, port_new);
166  }
167 
168  return true;
169 }
170 
172 {
173  if (channel == BC_MODULE_SENSOR_CHANNEL_A)
174  {
175  uint8_t port_actual = _bc_module_sensor.tca9534a._output_port;
176 
177  if ((port_actual & _BC_MODULE_SENSOR_CH_A_4K7) == 0)
178  {
180  }
181 
182  if ((port_actual & _BC_MODULE_SENSOR_CH_A_56R) == 0)
183  {
185  }
186  }
187  if (channel == BC_MODULE_SENSOR_CHANNEL_B)
188  {
189  uint8_t port_actual = _bc_module_sensor.tca9534a._output_port;
190 
191  if ((port_actual & _BC_MODULE_SENSOR_CH_B_4K7) == 0)
192  {
194  }
195 
196  if ((port_actual & _BC_MODULE_SENSOR_CH_B_56R) == 0)
197  {
199  }
200  }
201 
202  return bc_gpio_get_pull(_bc_module_sensor_channel_gpio_lut[channel]);
203 }
204 
206 {
207  bc_gpio_set_mode(_bc_module_sensor_channel_gpio_lut[channel], mode);
208 }
209 
211 {
212  return bc_gpio_get_input(_bc_module_sensor_channel_gpio_lut[channel]);
213 }
214 
216 {
217  bc_gpio_set_output(_bc_module_sensor_channel_gpio_lut[channel], state);
218 }
219 
221 {
222  return bc_gpio_get_output(_bc_module_sensor_channel_gpio_lut[channel]);
223 }
224 
226 {
227  bc_gpio_toggle_output(_bc_module_sensor_channel_gpio_lut[channel]);
228 }
229 
231 {
232  uint8_t port_actual;
233  uint8_t port_new;
234 
236  {
237  return false;
238  }
239 
240  port_actual = _bc_module_sensor.tca9534a._output_port;
241 
242  if (on)
243  {
244  port_new = port_actual & ~_BC_MODULE_SENSOR_VDD;
245  }
246  else
247  {
248  port_new = port_actual | _BC_MODULE_SENSOR_VDD;
249  }
250 
251  if (port_actual != port_new)
252  {
253  return bc_tca9534a_write_port(&_bc_module_sensor.tca9534a, port_new);
254  }
255 
256  return true;
257 }
258 
260 {
261  if (!_bc_module_sensor.initialized)
262  {
264  }
265 
266  if (_bc_module_sensor.revision == BC_MODULE_SENSOR_REVISION_UNKNOWN)
267  {
268  if (!bc_tca9534a_set_pin_direction(&_bc_module_sensor.tca9534a, BC_TCA9534A_PIN_P1, BC_TCA9534A_PIN_DIRECTION_INPUT))
269  {
271  }
272 
273  uint8_t test_vector = 0x86;
274  int value_input;
275  int value_output;
276 
277  for (size_t i = 0; i < sizeof(test_vector); i++)
278  {
279  value_output = (test_vector >> i) & 0x01;
280 
281  if (!bc_tca9534a_write_pin(&_bc_module_sensor.tca9534a, BC_TCA9534A_PIN_P0, value_output))
282  {
284  }
285 
286  if (!bc_tca9534a_read_pin(&_bc_module_sensor.tca9534a, BC_TCA9534A_PIN_P1, &value_input))
287  {
289  }
290 
291  if (value_output != value_input)
292  {
293  _bc_module_sensor.revision = BC_MODULE_SENSOR_REVISION_R1_0;
294 
295  return _bc_module_sensor.revision;
296  }
297  }
298 
299  _bc_module_sensor.revision = BC_MODULE_SENSOR_REVISION_R1_1;
300  }
301 
302  return _bc_module_sensor.revision;
303 }
304 
306 {
307  bc_onewire_init(_bc_module_sensor_channel_gpio_lut[BC_MODULE_SENSOR_CHANNEL_B]);
308 }
309 
311 {
312  if (_bc_module_sensor.onewire_init_semaphore == 0)
313  {
315  {
316  if (!bc_module_sensor_set_vdd(1))
317  {
318  return false;
319  }
320  }
321  else
322  {
324  {
325  return false;
326  }
327  }
328 
330  {
331  return false;
332  }
333  }
334 
335  _bc_module_sensor.onewire_init_semaphore++;
336 
337  return true;
338 }
339 
341 {
342  _bc_module_sensor.onewire_init_semaphore--;
343 
344  if (_bc_module_sensor.onewire_init_semaphore == 0)
345  {
347  {
348  if (!bc_module_sensor_set_vdd(0))
349  {
350  return false;
351  }
352  }
353  else
354  {
356  {
357  return false;
358  }
359  }
360 
362  {
363  return false;
364  }
365  }
366 
367  return true;
368 }
GPIO channel P5.
Definition: bc_gpio.h:30
Channel has pull-up 4k7.
bool bc_module_sensor_set_pull(bc_module_sensor_channel_t channel, bc_module_sensor_pull_t pull)
Set pull of Sensor Module channel.
GPIO channel has no pull-up/pull-down.
Definition: bc_gpio.h:84
bool bc_tca9534a_set_port_direction(bc_tca9534a_t *self, uint8_t direction)
Set direction of all pins.
Definition: bc_tca9534a.c:87
int bc_gpio_get_output(bc_gpio_channel_t channel)
Get output state for GPIO channel.
Definition: bc_gpio.c:448
Channel has no pull.
GPIO channel has pull-up.
Definition: bc_gpio.h:87
bool bc_tca9534a_write_pin(bc_tca9534a_t *self, bc_tca9534a_pin_t pin, int state)
Write pin state.
Definition: bc_tca9534a.c:61
I2C channel I2C0.
Definition: bc_i2c.h:18
Pin state.
Definition: bc_tca9534a.h:42
void bc_module_sensor_deinit(void)
Deinitialize Sensor Module.
bool bc_tca9534a_write_port(bc_tca9534a_t *self, uint8_t state)
Write state to all pins.
Definition: bc_tca9534a.c:35
int bc_module_sensor_get_output(bc_module_sensor_channel_t channel)
Get output state for Sensor Module channel.
void bc_gpio_set_output(bc_gpio_channel_t channel, int state)
Set output state for GPIO channel.
Definition: bc_gpio.c:442
bc_module_sensor_mode_t
Sensor Module mode of operation.
bc_module_sensor_revision_t
Sensor Module hardware revision.
bc_module_sensor_revision_t bc_module_sensor_get_revision(void)
Get Sensor Module revision.
bc_gpio_pull_t bc_gpio_get_pull(bc_gpio_channel_t channel)
Get pull-up/pull-down configuration for GPIO channel.
Definition: bc_gpio.c:334
void bc_gpio_set_mode(bc_gpio_channel_t channel, bc_gpio_mode_t mode)
Set mode of operation for GPIO channel.
Definition: bc_gpio.c:340
void bc_gpio_set_pull(bc_gpio_channel_t channel, bc_gpio_pull_t pull)
Set pull-up/pull-down configuration for GPIO channel.
Definition: bc_gpio.c:313
bool bc_module_sensor_set_vdd(bool on)
Set VDD On / Off.
int bc_module_sensor_get_input(bc_module_sensor_channel_t channel)
Get input of Sensor Module channel.
void bc_module_sensor_toggle_output(bc_module_sensor_channel_t channel)
Toggle output state for Sensor Module channel.
bool bc_tca9534a_read_pin(bc_tca9534a_t *self, bc_tca9534a_pin_t pin, int *state)
Read pin state.
Definition: bc_tca9534a.c:47
bc_module_sensor_pull_t
Sensor module pull.
bool bc_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...
Hardware revision R1.0.
int bc_gpio_get_input(bc_gpio_channel_t channel)
Get input state for GPIO channel.
Definition: bc_gpio.c:436
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
void bc_onewire_init(bc_gpio_channel_t channel)
Initialize 1-Wire.
Definition: bc_onewire.c:35
bc_module_sensor_pull_t bc_module_sensor_get_pull(bc_module_sensor_channel_t channel)
Get pull-up/pull-down configuration for Sensor Module channel.
void bc_module_sensor_onewire_init(void)
Initialize 1-Wire on channel B.
void bc_module_sensor_set_mode(bc_module_sensor_channel_t channel, bc_module_sensor_mode_t mode)
Set output mode of Sensor Module channel.
Hardware revision unknown.
void bc_module_sensor_set_output(bc_module_sensor_channel_t channel, int state)
Set output state of Sensor Module channel.
GPIO channel has pull-down.
Definition: bc_gpio.h:90
Hardware revision R1.1.
void bc_gpio_toggle_output(bc_gpio_channel_t channel)
Toggle output state for GPIO channel.
Definition: bc_gpio.c:454
Channel has internal pull-down.
Channel has internal pull-up.
Channel has pull-up 56R.
bc_gpio_channel_t
GPIO channels.
Definition: bc_gpio.h:12
bc_module_sensor_channel_t
Sensor Module channels.
bool bc_module_sensor_init(void)
Initialize Sensor Module.
bool bc_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...
GPIO channel P4.
Definition: bc_gpio.h:27
void bc_gpio_init(bc_gpio_channel_t channel)
Initialize GPIO channel.
Definition: bc_gpio.c:301
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
GPIO channel P7.
Definition: bc_gpio.h:36