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[2] =
23 {
26 };
27 
28 static const bc_tca9534a_pin_t _bc_module_sensor_channel_virtual_4k7_lut[2] =
29 {
30  [BC_MODULE_SENSOR_CHANNEL_A] = _BC_MODULE_SENSOR_CH_A_4K7,
31  [BC_MODULE_SENSOR_CHANNEL_B] = _BC_MODULE_SENSOR_CH_B_4K7
32 };
33 
34 static const bc_tca9534a_pin_t _bc_module_sensor_channel_virtual_56r_lut[2] =
35 {
36  [BC_MODULE_SENSOR_CHANNEL_A] = _BC_MODULE_SENSOR_CH_A_56R,
37  [BC_MODULE_SENSOR_CHANNEL_B] = _BC_MODULE_SENSOR_CH_B_56R
38 };
39 
41 {
42  if (!_bc_module_sensor.initialized)
43  {
44  if (!bc_tca9534a_init(&_bc_module_sensor.tca9534a, BC_I2C_I2C0, 0x3e))
45  {
46  return false;
47  }
48 
49  if (!bc_tca9534a_write_port(&_bc_module_sensor.tca9534a, _BC_MODULE_SENSOR_INITIALIZED_STATE))
50  {
51  return false;
52  }
53 
54  if (!bc_tca9534a_set_port_direction(&_bc_module_sensor.tca9534a, _BC_MODULE_SENSOR_INITIALIZED_DIRECTION))
55  {
56  return false;
57  }
58 
59  bc_gpio_init(_bc_module_sensor_channel_gpio_lut[BC_MODULE_SENSOR_CHANNEL_A]);
60  bc_gpio_init(_bc_module_sensor_channel_gpio_lut[BC_MODULE_SENSOR_CHANNEL_B]);
61 
62  _bc_module_sensor.initialized = true;
63  }
64 
65  return true;
66 }
67 
69 {
70  _bc_module_sensor.initialized = false;
71 }
72 
74 {
75  uint8_t port_actual;
76  uint8_t port_new;
77 
78  port_actual = _bc_module_sensor.tca9534a._output_port;
79 
80 // bc_tca9534a_read_port(&_bc_module_sensor.tca9534a, &port_actual);
81 
82  port_new = port_actual | _bc_module_sensor_channel_virtual_4k7_lut[channel] | _bc_module_sensor_channel_virtual_56r_lut[channel];
83 
84  switch (pull)
85  {
87  {
88  // Done by OR mask above
89 
90  bc_gpio_set_pull(_bc_module_sensor_channel_gpio_lut[channel], BC_GPIO_PULL_NONE);
91 
92  break;
93  }
95  {
96  port_new &= ~_bc_module_sensor_channel_virtual_4k7_lut[channel];
97  bc_gpio_set_pull(_bc_module_sensor_channel_gpio_lut[channel], BC_GPIO_PULL_NONE);
98 
99  break;
100  }
102  {
103  port_new &= ~_bc_module_sensor_channel_virtual_56r_lut[channel];
104  bc_gpio_set_pull(_bc_module_sensor_channel_gpio_lut[channel], BC_GPIO_PULL_NONE);
105 
106  break;
107  }
109  {
110  bc_gpio_set_pull(_bc_module_sensor_channel_gpio_lut[channel], BC_GPIO_PULL_UP);
111  break;
112  }
114  {
115  bc_gpio_set_pull(_bc_module_sensor_channel_gpio_lut[channel], BC_GPIO_PULL_DOWN);
116  break;
117  }
118  default:
119  {
120  return false;
121  }
122  }
123 
124  if (port_actual != port_new)
125  {
126  return bc_tca9534a_write_port(&_bc_module_sensor.tca9534a, port_new);
127  }
128 
129  return true;
130 }
131 
133 {
134  // TODO Implement better
135 
136  int weak;
137  int strong;
138  bc_gpio_pull_t internal;
139 
140  bc_tca9534a_read_pin(&_bc_module_sensor.tca9534a, _bc_module_sensor_channel_virtual_56r_lut[channel], &weak);
141  bc_tca9534a_read_pin(&_bc_module_sensor.tca9534a, _bc_module_sensor_channel_virtual_4k7_lut[channel], &strong);
142  internal = bc_gpio_get_pull(_bc_module_sensor_channel_gpio_lut[channel]);
143 
144  if (!weak)
145  {
147  }
148  else if (!strong)
149  {
151  }
152  else if (internal == BC_GPIO_PULL_UP)
153  {
155  }
156  else if (internal == BC_GPIO_PULL_DOWN)
157  {
158  return BC_GPIO_PULL_DOWN;
159  }
160  else
161  {
162  return BC_GPIO_PULL_NONE;
163  }
164 }
165 
167 {
168  bc_gpio_set_mode(_bc_module_sensor_channel_gpio_lut[channel], mode);
169 }
170 
172 {
173  return bc_gpio_get_input(_bc_module_sensor_channel_gpio_lut[channel]);
174 }
175 
177 {
178  bc_gpio_set_output(_bc_module_sensor_channel_gpio_lut[channel], state);
179 }
180 
182 {
183  return bc_gpio_get_output(_bc_module_sensor_channel_gpio_lut[channel]);
184 }
185 
187 {
188  bc_gpio_toggle_output(_bc_module_sensor_channel_gpio_lut[channel]);
189 }
190 
192 {
193  uint8_t port_actual;
194  uint8_t port_new;
195 
197  {
198  return false;
199  }
200 
201  port_actual = _bc_module_sensor.tca9534a._output_port;
202 
203  if (on)
204  {
205  port_new = port_actual & ~_BC_MODULE_SENSOR_VDD;
206  }
207  else
208  {
209  port_new = port_actual | _BC_MODULE_SENSOR_VDD;
210  }
211 
212  if (port_actual != port_new)
213  {
214  return bc_tca9534a_write_port(&_bc_module_sensor.tca9534a, port_new);
215  }
216 
217  return true;
218 }
219 
221 {
222  if (!_bc_module_sensor.initialized)
223  {
225  }
226 
227  if (_bc_module_sensor.revision == BC_MODULE_SENSOR_REVISION_UNKNOWN)
228  {
229  if (!bc_tca9534a_set_pin_direction(&_bc_module_sensor.tca9534a, BC_TCA9534A_PIN_P1, BC_TCA9534A_PIN_DIRECTION_INPUT))
230  {
232  }
233 
234  uint8_t test_vector = 0x86;
235  int value_input;
236  int value_output;
237 
238  for (size_t i = 0; i < sizeof(test_vector); i++)
239  {
240  value_output = (test_vector >> i) & 0x01;
241 
242  if (!bc_tca9534a_write_pin(&_bc_module_sensor.tca9534a, BC_TCA9534A_PIN_P0, value_output))
243  {
245  }
246 
247  if (!bc_tca9534a_read_pin(&_bc_module_sensor.tca9534a, BC_TCA9534A_PIN_P1, &value_input))
248  {
250  }
251 
252  if (value_output != value_input)
253  {
254  _bc_module_sensor.revision = BC_MODULE_SENSOR_REVISION_R1_0;
255 
256  return _bc_module_sensor.revision;
257  }
258  }
259 
260  _bc_module_sensor.revision = BC_MODULE_SENSOR_REVISION_R1_1;
261  }
262 
263  return _bc_module_sensor.revision;
264 }
265 
267 {
268  bc_onewire_init(_bc_module_sensor_channel_gpio_lut[BC_MODULE_SENSOR_CHANNEL_B]);
269 }
270 
272 {
273  if (_bc_module_sensor.onewire_init_semaphore == 0)
274  {
276  {
277  if (!bc_module_sensor_set_vdd(1))
278  {
279  return false;
280  }
281  }
282  else
283  {
285  {
286  return false;
287  }
288  }
289 
291  {
292  return false;
293  }
294  }
295 
296  _bc_module_sensor.onewire_init_semaphore++;
297 
298  return true;
299 }
300 
302 {
303  _bc_module_sensor.onewire_init_semaphore--;
304 
305  if (_bc_module_sensor.onewire_init_semaphore == 0)
306  {
308  {
309  if (!bc_module_sensor_set_vdd(0))
310  {
311  return false;
312  }
313  }
314  else
315  {
317  {
318  return false;
319  }
320  }
321 
323  {
324  return false;
325  }
326  }
327 
328  return true;
329 }
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.
bc_gpio_pull_t
GPIO pull-up/pull-down setting.
Definition: bc_gpio.h:81
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.
bc_tca9534a_pin_t
Individual pin names.
Definition: bc_tca9534a.h:16
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