CuVoodoo STM32F1 firmware template
busvoodoo_hiz.c
Go to the documentation of this file.
1 /* This program is free software: you can redistribute it and/or modify
2  * it under the terms of the GNU General Public License as published by
3  * the Free Software Foundation, either version 3 of the License, or
4  * (at your option) any later version.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program. If not, see <http://www.gnu.org/licenses/>.
13  *
14  */
20 /* standard libraries */
21 #include <stdint.h> // standard integer types
22 #include <math.h> // math utilities
23 
24 /* STM32 (including CM3) libraries */
25 #include <libopencmsis/core_cm3.h> // Cortex M3 utilities
26 #include <libopencm3/stm32/gpio.h> // general purpose input output library
27 #include <libopencm3/stm32/rcc.h> // real-time control clock library
28 #include <libopencm3/stm32/dbgmcu.h> // debug utilities
29 #include <libopencm3/stm32/desig.h> // design utilities
30 #include <libopencm3/stm32/dac.h> // DAC utilities
31 
32 /* own libraries */
33 #include "global.h" // board definitions
34 #include "print.h" // printing utilities
35 #include "menu.h" // menu definitions
36 #include "busvoodoo_global.h" // BusVoodoo definitions
37 #include "busvoodoo_oled.h" // OLED utilities
38 #include "busvoodoo_hiz.h" // own definitions
39 
40 #define BUSVOODOO_LV_DEFAULT (0.8*(1+30.0/10.0))
41 #define BUSVOODOO_LV_TEST 2.5
42 #define BUSVOODOO_HV_DEFAULT (1.25*(1+100.0/10.0))
43 #define BUSVOODOO_HV_TEST 12.0
50 static bool busvoodoo_hiz_setup(char** prefix, const char* line)
51 {
52  (void)line; // no configuration is required
53  *prefix = "HiZ"; // set command line prefix
54  led_blue(); // switch blue LED on to show we are ready
55  busvoodoo_oled_text_left(*prefix); // set mode title on OLED display
56  const char* pinout_io[10] = {"GND", "5V", "3V3", "LV", NULL, NULL, NULL, NULL, NULL, NULL}; // HiZ mode I/O pinout
57  for (uint8_t i=0; i<LENGTH(pinout_io) && i<LENGTH(busvoodoo_global_pinout_io); i++) {
58  busvoodoo_global_pinout_io[i] = pinout_io[i]; // set pin names
59  }
60  if (busvoodoo_full) {
61  const char* pinout_rscan[5] = {"HV", NULL, NULL, NULL, NULL}; // HiZ mode RS/CAN pinout
62  for (uint8_t i=0; i<LENGTH(pinout_rscan) && i<LENGTH(busvoodoo_global_pinout_rscan); i++) {
63  busvoodoo_global_pinout_rscan[i] = pinout_rscan[i]; // set pin names
64  }
65  }
66  busvoodoo_oled_text_pinout(busvoodoo_global_pinout_io, true); // set pinout on display
67  busvoodoo_oled_update(); // update display to show text and pinout
68  return true;
69 }
70 
73 static void busvoodoo_hiz_exit(void)
74 {
75  // there is nothing to do
76 }
77 
81 static bool busvoodoo_hiz_test_self(void)
82 {
83  bool to_return = false; // success of the self-test
84  busvoodoo_safe_state(); // start from a safe state
85 
86  // get device information
87  // get device identifier (DEV_ID)
88  // 0x412: low-density, 16-32 kB flash
89  // 0x410: medium-density, 64-128 kB flash
90  // 0x414: high-density, 256-512 kB flash
91  // 0x430: XL-density, 768-1024 kB flash
92  // 0x418: connectivity
93  if (0==(DBGMCU_IDCODE&DBGMCU_IDCODE_DEV_ID_MASK)) { // can't read IDCODE for verification
94  // this is a known issue document in STM32F10xxC/D/E Errata sheet, without workaround
95  } else if (0x414!=(DBGMCU_IDCODE&DBGMCU_IDCODE_DEV_ID_MASK)) {
96  printf("this (DEV_ID=%03x) is not a high-density device: a wrong micro-controller might have been used\n", (DBGMCU_IDCODE&DBGMCU_IDCODE_DEV_ID_MASK));
97  }
98  // ensure flash size is ok
99  if (0xffff==DESIG_FLASH_SIZE) {
100  printf("unknown flash size: this is probably a defective micro-controller\n");
101  }
102 
103  // check 5V power rail
104  float voltage = busvoodoo_vreg_get(BUSVOODOO_5V_CHANNEL); // get 5V power rail voltage
105  if (voltage<4.0) {
106  printf("5V power rail voltage is too low: %.2fV\n", voltage);
107 #if DEBUG
108  while (true);
109 #else
110  goto error;
111 #endif
112  } else if (voltage>5.5) {
113  printf("5V power rail voltage is too high: %.2fV\n", voltage);
114 #if DEBUG
115  while (true);
116 #else
117  goto error;
118 #endif
119  }
120 
121  // check 3.3V power rail
122  voltage = busvoodoo_vreg_get(BUSVOODOO_3V3_CHANNEL); // get 3.3V power rail voltage
123  if (voltage<3.0) {
124  printf("3.3V power rail voltage is too low: %.2fV\n", voltage);
125 #if DEBUG
126  while (true);
127 #else
128  goto error;
129 #endif
130  } else if (voltage>3.6) {
131  printf("3.3V power rail voltage is too high: %.2fV\n", voltage);
132 #if DEBUG
133  while (true);
134 #else
135  goto error;
136 #endif
137  }
138 
139  // test 5V and 3.3V outputs
140  busvoodoo_vout_switch(true); // enable Vout
141  voltage = busvoodoo_vreg_get(BUSVOODOO_5V_CHANNEL); // get 5V power rail voltage
142  if (voltage<4.0) {
143  printf("5V power rail voltage is too low when 5V output is enabled: %.2fV\n", voltage);
144 #if DEBUG
145  while (true);
146 #else
147  goto error;
148 #endif
149  } else if (voltage>5.5) {
150  printf("5V power rail voltage is too high when 5V output is enabled: %.2fV\n", voltage);
151 #if DEBUG
152  while (true);
153 #else
154  goto error;
155 #endif
156  }
157  voltage = busvoodoo_vreg_get(BUSVOODOO_3V3_CHANNEL); // get 3.3V power rail voltage
158  if (voltage<3.0) {
159  printf("3.3V power rail voltage is too low when 3V3 output is enabled: %.2fV\n", voltage);
160 #if DEBUG
161  while (true);
162 #else
163  goto error;
164 #endif
165  } else if (voltage>3.6) {
166  printf("3.3V power rail voltage is too high when 3V3 is enabled: %.2fV\n", voltage);
167 #if DEBUG
168  while (true);
169 #else
170  goto error;
171 #endif
172  }
173  busvoodoo_vout_switch(false); // disable Vout
174 
175  // check LV voltage regulator
176  voltage = busvoodoo_lv_set(0); // disable LV voltage regulator
177  if (voltage>0.2) { // ensure the output is at 0V when the regulator is not enabled
178  printf("LV voltage is %.2fV instead of 0V when the regulator is disabled\n", voltage);
179 #if DEBUG
180  while (true);
181 #else
182  goto error;
183 #endif
184  }
185  gpio_set(GPIO(BUSVOODOO_LVEN_PORT), GPIO(BUSVOODOO_LVEN_PIN)); // enable LV voltage regulator
186  sleep_ms(5); // let the voltage regulator start and voltage settle
187  voltage = busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL); // get LV voltage
188  // without being driven it should be around the default voltage
189  if (voltage<BUSVOODOO_LV_DEFAULT-0.2) {
190  printf("LV voltage is lower (%.2fV) than expected (%.2fV) when the regulator is enabled\n", voltage, BUSVOODOO_LV_DEFAULT);
191 #if DEBUG
192  while (true);
193 #else
194  goto error;
195 #endif
196  } else if (voltage>BUSVOODOO_LV_DEFAULT+0.2) {
197  printf("LV voltage is higher (%.2fV) than expected (%.2fV) when the regulator is enabled\n", voltage, BUSVOODOO_LV_DEFAULT);
198 #if DEBUG
199  while (true);
200 #else
201  goto error;
202 #endif
203  }
204 
205  // check if we can control LV
206  voltage = busvoodoo_lv_set(BUSVOODOO_LV_TEST); // get LV voltage
207  if (voltage<BUSVOODOO_LV_TEST-0.2) {
208  printf("LV voltage is lower (%.2fV) than set (%.2fV)\n", voltage, BUSVOODOO_LV_TEST);
209 #if DEBUG
210  while (true);
211 #else
212  goto error;
213 #endif
214  } else if (voltage>BUSVOODOO_LV_TEST+0.2) {
215  printf("LV voltage is highed (%.2fV) than set (%.2fV)\n", voltage, BUSVOODOO_LV_TEST);
216 #if DEBUG
217  while (true);
218 #else
219  goto error;
220 #endif
221  }
222  busvoodoo_lv_set(0); // disable LV voltage regulator
223 
224  // check HV voltage regulator
225  if (busvoodoo_full) {
226  voltage = busvoodoo_hv_set(0); // disable HV voltage regulator
227  if (voltage>0.2) { // ensure the output is at 0V when the regulator is not enabled
228  printf("HV voltage is %.2fV instead of 0V when the regulator is disabled\n", voltage);
229 #if DEBUG
230  while (true);
231 #else
232  goto error;
233 #endif
234  }
235  gpio_clear(GPIO(BUSVOODOO_HVEN_PORT), GPIO(BUSVOODOO_HVEN_PIN)); // enable HV voltage regulator
236  sleep_ms(10); // let the voltage regulator start and voltage settle
237  voltage = busvoodoo_vreg_get(BUSVOODOO_HV_CHANNEL); // get HV voltage
238  // without being driven it should be around the default voltage
239  if (voltage<BUSVOODOO_HV_DEFAULT-0.3) {
240  printf("HV voltage is lower (%.2fV) than expected (%.2fV) when regulator is enabled\n", voltage, BUSVOODOO_HV_DEFAULT);
241 #if DEBUG
242  while (true);
243 #else
244  goto error;
245 #endif
246  } else if (voltage>BUSVOODOO_HV_DEFAULT+0.3) {
247  printf("HV voltage is higher (%.2fV) than expected (%.2fV) when regulator is enabled\n", voltage, BUSVOODOO_HV_DEFAULT);
248 #if DEBUG
249  while (true);
250 #else
251  goto error;
252 #endif
253  }
254 
255  // check if we can control HV voltage regulator
256  voltage = busvoodoo_hv_set(BUSVOODOO_HV_TEST); // set voltage on HV voltage regulator
257  if (voltage<BUSVOODOO_HV_TEST-0.3) {
258  printf("HV voltage is lower (%.2fV) than set (%.2fV)\n", voltage, BUSVOODOO_HV_TEST);
259 #if DEBUG
260  while (true);
261 #else
262  goto error;
263 #endif
264  } else if (voltage>BUSVOODOO_HV_TEST+0.3) {
265  printf("HV voltage is higher (%.2fV) than set (%.2fV)\n", voltage, BUSVOODOO_HV_TEST);
266 #if DEBUG
267  while (true);
268 #else
269  goto error;
270 #endif
271  }
272  voltage = busvoodoo_hv_set(0); // disable HV voltage regulator
273  }
274 
275  // pull all pins down and ensure they are low
276  for (uint8_t pin=0; pin<LENGTH(busvoodoo_io_ports) && pin<LENGTH(busvoodoo_io_pins); pin++) {
277  gpio_set_mode(busvoodoo_io_ports[pin], GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, busvoodoo_io_pins[pin]); // set pin to input
278  gpio_clear(busvoodoo_io_ports[pin], busvoodoo_io_pins[pin]); // pull down so it's not floating
279  }
280  for (uint8_t pin=0; pin<LENGTH(busvoodoo_io_ports) && pin<LENGTH(busvoodoo_io_pins) && pin<LENGTH(busvoodoo_io_names); pin++) {
281  if (gpio_get(busvoodoo_io_ports[pin], busvoodoo_io_pins[pin])) { // ensure it really is low
282  printf("signal %s is high although it is pulled low (internal)\n", busvoodoo_io_names[pin]); // warn user about the error
283 #if DEBUG
284  while (true);
285 #else
286  goto error;
287 #endif
288  }
289  }
290 
291  // pull all pins up and ensure they are high
292  for (uint8_t pin=0; pin<LENGTH(busvoodoo_io_ports) && pin<LENGTH(busvoodoo_io_pins); pin++) {
293  gpio_set_mode(busvoodoo_io_ports[pin], GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, busvoodoo_io_pins[pin]); // set pin to input
294  gpio_set(busvoodoo_io_ports[pin], busvoodoo_io_pins[pin]); // pull up using internal pull-up
295  }
296  for (uint8_t pin=0; pin<LENGTH(busvoodoo_io_ports) && pin<LENGTH(busvoodoo_io_pins) && pin<LENGTH(busvoodoo_io_names); pin++) {
297  if (!gpio_get(busvoodoo_io_ports[pin], busvoodoo_io_pins[pin])) { // ensure it really is high
298  printf("signal %s is low although it is pulled up (internal)\n", busvoodoo_io_names[pin]); // warn user about the error
299 #if DEBUG
300  while (true);
301 #else
302  goto error;
303 #endif
304  }
305  }
306 
307  // set individual pin high and ensure only pins in the same group are at the same level
308  for (uint8_t pin=0; pin<LENGTH(busvoodoo_io_ports) && pin<LENGTH(busvoodoo_io_pins); pin++) {
309  gpio_set_mode(busvoodoo_io_ports[pin], GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, busvoodoo_io_pins[pin]); // set pin to input
310  gpio_clear(busvoodoo_io_ports[pin], busvoodoo_io_pins[pin]); // pull down to ensure it is not high by accident
311  }
312  for (uint8_t pin1=0; pin1<LENGTH(busvoodoo_io_ports) && pin1<LENGTH(busvoodoo_io_pins) && pin1<LENGTH(busvoodoo_io_groups) && pin1<LENGTH(busvoodoo_io_names); pin1++) {
313  gpio_set_mode(busvoodoo_io_ports[pin1], GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, busvoodoo_io_pins[pin1]); // set button pin to output
314  gpio_set(busvoodoo_io_ports[pin1], busvoodoo_io_pins[pin1]); // set pin high
315  for (uint8_t pin2=0; pin2<LENGTH(busvoodoo_io_ports) && pin2<LENGTH(busvoodoo_io_pins) && pin2<LENGTH(busvoodoo_io_groups) && pin2<LENGTH(busvoodoo_io_names); pin2++) {
316  if (busvoodoo_io_groups[pin1]==busvoodoo_io_groups[pin2] && !gpio_get(busvoodoo_io_ports[pin2], busvoodoo_io_pins[pin2])) {
317  printf("signal %s of I/O-%u is low while it should be set high by signal %s of I/O-%u\n", busvoodoo_io_names[pin2], busvoodoo_io_groups[pin2], busvoodoo_io_names[pin1], busvoodoo_io_groups[pin1]); // warn user about the error
318 #if DEBUG
319  while (true);
320 #else
321  goto error;
322 #endif
323  } else if (busvoodoo_io_groups[pin1]!=busvoodoo_io_groups[pin2] && gpio_get(busvoodoo_io_ports[pin2], busvoodoo_io_pins[pin2])) {
324  printf("signal %s of I/O-%u is high while it should not be set high by signal %s of I/O-%u\n", busvoodoo_io_names[pin2], busvoodoo_io_groups[pin2], busvoodoo_io_names[pin1], busvoodoo_io_groups[pin1]); // warn user about the error
325 #if DEBUG
326  while (true);
327 #else
328  goto error;
329 #endif
330  }
331  }
332  gpio_set_mode(busvoodoo_io_ports[pin1], GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, busvoodoo_io_pins[pin1]); // set pin back to input
333  gpio_clear(busvoodoo_io_ports[pin1], busvoodoo_io_pins[pin1]); // pull pin back down
334  }
335  // set individual pin low and ensure only pins in the same group are at the same level
336  for (uint8_t pin=0; pin<LENGTH(busvoodoo_io_ports) && pin<LENGTH(busvoodoo_io_pins); pin++) {
337  gpio_set_mode(busvoodoo_io_ports[pin], GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, busvoodoo_io_pins[pin]); // set pin to input
338  gpio_set(busvoodoo_io_ports[pin], busvoodoo_io_pins[pin]); // pull up to ensure it is not low by accident
339  }
340  for (uint8_t pin1=0; pin1<LENGTH(busvoodoo_io_ports) && pin1<LENGTH(busvoodoo_io_pins) && pin1<LENGTH(busvoodoo_io_groups) && pin1<LENGTH(busvoodoo_io_names); pin1++) {
341  gpio_set_mode(busvoodoo_io_ports[pin1], GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, busvoodoo_io_pins[pin1]); // set button pin to output
342  gpio_clear(busvoodoo_io_ports[pin1], busvoodoo_io_pins[pin1]); // set pin low
343  for (uint8_t pin2=0; pin2<LENGTH(busvoodoo_io_ports) && pin2<LENGTH(busvoodoo_io_pins) && pin2<LENGTH(busvoodoo_io_groups) && pin2<LENGTH(busvoodoo_io_names); pin2++) {
344  if (busvoodoo_io_groups[pin1]==busvoodoo_io_groups[pin2] && gpio_get(busvoodoo_io_ports[pin2], busvoodoo_io_pins[pin2])) {
345  printf("signal %s of I/O-%u is high while it should be set low by signal %s of I/O-%u\n", busvoodoo_io_names[pin2], busvoodoo_io_groups[pin2], busvoodoo_io_names[pin1], busvoodoo_io_groups[pin1]); // warn user about the error
346 #if DEBUG
347  while (true);
348 #else
349  goto error;
350 #endif
351  } else if (busvoodoo_io_groups[pin1]!=busvoodoo_io_groups[pin2] && !gpio_get(busvoodoo_io_ports[pin2], busvoodoo_io_pins[pin2])) {
352  printf("signal %s of I/O-%u is low while it should not be set low by signal %s of I/O-%u\n", busvoodoo_io_names[pin2], busvoodoo_io_groups[pin2], busvoodoo_io_names[pin1], busvoodoo_io_groups[pin1]); // warn user about the error
353 #if DEBUG
354  while (true);
355 #else
356  goto error;
357 #endif
358  }
359  }
360  gpio_set_mode(busvoodoo_io_ports[pin1], GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, busvoodoo_io_pins[pin1]); // set pin back to input
361  gpio_set(busvoodoo_io_ports[pin1], busvoodoo_io_pins[pin1]); // pull pin back up
362  }
363 
364  // test 5V pull-up
365  for (uint8_t pin=0; pin<LENGTH(busvoodoo_io_ports) && pin<LENGTH(busvoodoo_io_pins); pin++) {
366  gpio_set_mode(busvoodoo_io_ports[pin], GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, busvoodoo_io_pins[pin]); // set pin to input
367  gpio_clear(busvoodoo_io_ports[pin], busvoodoo_io_pins[pin]); // pull down to ensure it is not high by accident
368  }
369  busvoodoo_lv_set(5.0); // set LV voltage regulator to use the 5.0V power rail
370  voltage = busvoodoo_embedded_pullup(true); // enable 5V pull-up
371  if (voltage<4.0) {
372  printf("5V power rail voltage is too low when used to pull up: %.2fV\n", voltage);
373 #if DEBUG
374  while (true);
375 #else
376  goto error;
377 #endif
378  } else if (voltage>5.5) {
379  printf("5V power rail voltage is too high when used to pull up: %.2fV\n", voltage);
380 #if DEBUG
381  while (true);
382 #else
383  goto error;
384 #endif
385  }
386  for (uint8_t pin=0; pin<LENGTH(busvoodoo_io_ports) && pin<LENGTH(busvoodoo_io_pins) && pin<LENGTH(busvoodoo_io_names); pin++) {
387  if (!gpio_get(busvoodoo_io_ports[pin], busvoodoo_io_pins[pin])) { // ensure it really is high
388  printf("signal %s is low although it is pulled up by 5V (embedded)\n", busvoodoo_io_names[pin]); // warn user about the error
389 #if DEBUG
390  while (true);
391 #else
392  goto error;
393 #endif
394  }
395  }
396  busvoodoo_embedded_pullup(false); // disable pull-ups
397  voltage = busvoodoo_lv_set(0); // disable LV voltage regulator
398 
399  // test LV pull-up set to 3.3V
400  for (uint8_t pin=0; pin<LENGTH(busvoodoo_io_ports) && pin<LENGTH(busvoodoo_io_pins); pin++) {
401  gpio_set_mode(busvoodoo_io_ports[pin], GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, busvoodoo_io_pins[pin]); // set pin to input
402  gpio_clear(busvoodoo_io_ports[pin], busvoodoo_io_pins[pin]); // pull down to ensure it is not high by accident
403  }
404  busvoodoo_lv_set(3.3); // set LV voltage regulator
405  voltage = busvoodoo_embedded_pullup(3.3); // enable pull-up with adjustable regulator
406  if (voltage<BUSVOODOO_LV_DEFAULT-0.2) {
407  printf("LV voltage is lower (%.2fV) than expected (%.2fV) when used to pull up\n", voltage, BUSVOODOO_LV_DEFAULT);
408 #if DEBUG
409  while (true);
410 #else
411  goto error;
412 #endif
413  } else if (voltage>BUSVOODOO_LV_DEFAULT+0.2) {
414  printf("LV voltage is higher (%.2fV) than expected (%.2fV) when used to pull up\n", voltage, BUSVOODOO_LV_DEFAULT);
415 #if DEBUG
416  while (true);
417 #else
418  goto error;
419 #endif
420  }
421  for (uint8_t pin=0; pin<LENGTH(busvoodoo_io_ports) && pin<LENGTH(busvoodoo_io_pins) && pin<LENGTH(busvoodoo_io_names); pin++) {
422  if (!gpio_get(busvoodoo_io_ports[pin], busvoodoo_io_pins[pin])) { // ensure it really is high
423  printf("signal %s is low although it is pulled up by LV (embedded)\n", busvoodoo_io_names[pin]); // warn user about the error
424 #if DEBUG
425  while (true);
426 #else
427  goto error;
428 #endif
429  }
430  }
431  busvoodoo_lv_set(0); // disable LV voltage regulator
432  busvoodoo_embedded_pullup(false); // disable pull-ups
433 
434  // test RS-485 transceiver
435  if (busvoodoo_full) {
436  // configure transceiver
437  gpio_set(GPIO(BUSVOODOO_RS485_DE_PORT), GPIO(BUSVOODOO_RS485_DE_PIN)); // enable transmitter
438  gpio_clear(GPIO(BUSVOODOO_RS485_RE_PORT), GPIO(BUSVOODOO_RS485_RE_PIN)); // enable receiver
439  gpio_set_mode(GPIO(BUSVOODOO_RS485_TX_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(BUSVOODOO_RS485_TX_PIN)); // set TX as output
440  gpio_set_mode(GPIO(BUSVOODOO_RS485_RX_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO(BUSVOODOO_RS485_RX_PIN)); // set RX as input with pull up/down
441  // test low signal
442  gpio_set(GPIO(BUSVOODOO_RS485_RX_PORT), GPIO(BUSVOODOO_RS485_RX_PIN)); // pull RX up
443  gpio_clear(GPIO(BUSVOODOO_RS485_TX_PORT), GPIO(BUSVOODOO_RS485_TX_PIN)); // set TX low
444  sleep_us(100); // let voltage settle
445  if (gpio_get(GPIO(BUSVOODOO_RS485_RX_PORT), GPIO(BUSVOODOO_RS485_RX_PIN))) { // test if RX is still high
446  printf("RS-485 RX is high while TX is set low\n"); // warn user about the error
447 #if DEBUG
448  while (true);
449 #else
450  goto error;
451 #endif
452  }
453  // test high signal
454  gpio_clear(GPIO(BUSVOODOO_RS485_RX_PORT), GPIO(BUSVOODOO_RS485_RX_PIN)); // pull RX down
455  gpio_set(GPIO(BUSVOODOO_RS485_TX_PORT), GPIO(BUSVOODOO_RS485_TX_PIN)); // set TX high
456  sleep_us(100); // let voltage settle
457  if (0==gpio_get(GPIO(BUSVOODOO_RS485_RX_PORT), GPIO(BUSVOODOO_RS485_RX_PIN))) { // test if RX is still low
458  printf("RS-485 RX is low while TX is set high\n"); // warn user about the error
459 #if DEBUG
460  while (true);
461 #else
462  goto error;
463 #endif
464  }
465  // release transceiver
466  gpio_set(GPIO(BUSVOODOO_RS485_RE_PORT), GPIO(BUSVOODOO_RS485_RE_PIN)); // set high to disable receiver
467  gpio_clear(GPIO(BUSVOODOO_RS485_DE_PORT), GPIO(BUSVOODOO_RS485_DE_PIN)); // set low to disable transmitter
468  gpio_set_mode(GPIO(BUSVOODOO_RS485_TX_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_RS485_TX_PIN)); // set pin to floating
469  gpio_set_mode(GPIO(BUSVOODOO_RS485_RX_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_RS485_RX_PIN)); // set pin to floating
470  }
471 
472  // test CAN transceiver
473  if (busvoodoo_full) {
474  // configure transceiver
475  gpio_clear(GPIO(BUSVOODOO_CAN_EN_PORT), GPIO(BUSVOODOO_CAN_EN_PIN)); // pull low to power transceiver
476  gpio_set_mode(GPIO(BUSVOODOO_CAN_TX_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO(BUSVOODOO_CAN_TX_PIN)); // set TX as output (there is a pull-up resistor to 5V)
477  gpio_set_mode(GPIO(BUSVOODOO_CAN_RX_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO(BUSVOODOO_CAN_RX_PIN)); // set RX as input with pull up/down
478  // test high signal
479  gpio_clear(GPIO(BUSVOODOO_CAN_RX_PORT), GPIO(BUSVOODOO_CAN_RX_PIN)); // pull RX down
480  gpio_set(GPIO(BUSVOODOO_CAN_TX_PORT), GPIO(BUSVOODOO_CAN_TX_PIN)); // set TX high
481  sleep_us(100); // let voltage settle
482  if (0==gpio_get(GPIO(BUSVOODOO_CAN_RX_PORT), GPIO(BUSVOODOO_CAN_RX_PIN))) { // test if RX is still low
483  printf("CAN RX is low while TX is set high\n"); // warn user about the error
484 #if DEBUG
485  while (true);
486 #else
487  goto error;
488 #endif
489  }
490  // test low signal
491  gpio_set(GPIO(BUSVOODOO_CAN_RX_PORT), GPIO(BUSVOODOO_CAN_RX_PIN)); // pull RX up
492  gpio_clear(GPIO(BUSVOODOO_CAN_TX_PORT), GPIO(BUSVOODOO_CAN_TX_PIN)); // set TX low
493  sleep_us(100); // let voltage settle
494  if (gpio_get(GPIO(BUSVOODOO_CAN_RX_PORT), GPIO(BUSVOODOO_CAN_RX_PIN))) { // test if RX is still high
495  printf("CAN RX is high while TX is set low\n"); // warn user about the error
496 #if DEBUG
497  while (true);
498 #else
499  goto error;
500 #endif
501  }
502  // test dominant timeout
503  sleep_ms(10); // after 5 ms the output should be switched back from dominant to recessive respite TX being low
504  if (!gpio_get(GPIO(BUSVOODOO_CAN_RX_PORT), GPIO(BUSVOODOO_CAN_RX_PIN))) { // test if RX is now low
505  printf("CAN RX is low while the output should be back to recessive\n"); // warn user about the error
506 #if DEBUG
507  while (true);
508 #else
509  goto error;
510 #endif
511  }
512  // release transceiver
513  gpio_set(GPIO(BUSVOODOO_CAN_EN_PORT), GPIO(BUSVOODOO_CAN_EN_PIN)); // set high to power off transceiver
514  gpio_set_mode(GPIO(BUSVOODOO_CAN_TX_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_CAN_TX_PIN)); // set pin to floating
515  gpio_set_mode(GPIO(BUSVOODOO_CAN_RX_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_CAN_RX_PIN)); // set pin to floating
516  }
517 
518  to_return = true; // all tests are successful
519 #if DEBUG
520 #else
521 error:
522 #endif
523  busvoodoo_safe_state(); // set back to safe state
524  if (!to_return) {
525  printf("the test procedure has been aborted for safety reasons\n");
526  }
527 
528  return to_return;
529 }
530 
534 static bool busvoodoo_hiz_test_pins(void)
535 {
536  bool to_return = false; // test result to return
537  busvoodoo_safe_state(); // start from safe state with all outputs switched off
538 
539  const char* lv_to = "connect I/O pin 4 to "; // most outputs will be tested using LV ADC
540  char* pinout[10] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}; // pinout to display
541  busvoodoo_oled_text_right("pins test"); // display test on display
542  busvoodoo_oled_update(); // update screen
543 
544  // test GND on pin 1 by shorting LV_CTL to ground (through 40k resistor) and measure short
545  gpio_set(GPIO(BUSVOODOO_LVCTL_PORT), GPIO(BUSVOODOO_LVCTL_PIN)); // set pin high
546  gpio_set_mode(GPIO(BUSVOODOO_LVCTL_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(BUSVOODOO_LVCTL_PIN)); // set LV control pin as output
547  printf("%sI/O pin 1\n", lv_to); // ask user for action
548  led_red(); // notify user to perform action
549  pinout[3] = "O"; // set main testing pin
550  pinout[0] = "O"; // set target testing pin
551  busvoodoo_oled_text_pinout((const char**)pinout, true); // display pins to user
552  busvoodoo_oled_update(); // update screen
553  do {
554  sleep_ms(100); // wait for user to make connection
555  } while (busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL)>0.2 && !user_input_available); // wait until pin is shorted to ground
556  led_blue(); // notify user test is running
557  if (user_input_available) { // user interruption
558  goto error;
559  }
560  gpio_clear(GPIO(BUSVOODOO_LVCTL_PORT), GPIO(BUSVOODOO_LVCTL_PIN)); // set pin low
561  gpio_set_mode(GPIO(BUSVOODOO_LVCTL_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(BUSVOODOO_LVCTL_PIN)); // set LV control pin as output
562  sleep_ms(100); // wait for voltage to settle an debounce
564  printf("I/O pin 4 is high while it should be low and shorted to ground\n");
565 #if DEBUG
566  while (true);
567 #else
568  goto error;
569 #endif
570  }
571  gpio_set_mode(GPIO(BUSVOODOO_LVCTL_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, GPIO(BUSVOODOO_LVCTL_PIN)); // set LV control pin back to analog input for DAC
572  pinout[0] = NULL; // clear pin to test
573 
574  // test 5V output on pin 2
575  gpio_clear(GPIO(BUSVOODOO_VOUTEN_PORT), GPIO(BUSVOODOO_VOUTEN_PIN)); // enable Vout
576  printf("%sI/O pin 2\n", lv_to);
577  led_red(); // notify user to perform action
578  pinout[1] = "O"; // set target testing pin
579  busvoodoo_oled_text_pinout((const char**)pinout, true); // display pins to user
580  busvoodoo_oled_update(); // update screen
581  do {
582  sleep_ms(100); // wait for user to make connection
583  } while (busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL)<0.2 && !user_input_available); // wait until pin is connected
584  led_blue(); // notify user test is running
585  if (user_input_available) { // user interruption
586  goto error;
587  }
588  gpio_set(GPIO(BUSVOODOO_VOUTEN_PORT), GPIO(BUSVOODOO_VOUTEN_PIN)); // disable Vout
589  sleep_ms(100); // wait for voltage to settle and debounce
591  printf("5V output is high while the power output should be switched off\n");
592 #if DEBUG
593  while (true);
594 #else
595  goto error;
596 #endif
597  }
598  pinout[1] = NULL; // clear pin to test
599 
600  // test 3.3V output on pin 3
601  gpio_clear(GPIO(BUSVOODOO_VOUTEN_PORT), GPIO(BUSVOODOO_VOUTEN_PIN)); // enable Vout
602  printf("%sI/O pin 3\n", lv_to);
603  led_red(); // notify user to perform action
604  pinout[2] = "O"; // set target testing pin
605  busvoodoo_oled_text_pinout((const char**)pinout, true); // display pins to user
606  busvoodoo_oled_update(); // update screen
607  do {
608  sleep_ms(100); // wait for user to make connection
609  } while ((busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL)<0.2 || busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL)>3.5) && !user_input_available); // wait until pin is connected
610  led_blue(); // notify user test is running
611  if (user_input_available) { // user interruption
612  goto error;
613  }
614  gpio_set(GPIO(BUSVOODOO_VOUTEN_PORT), GPIO(BUSVOODOO_VOUTEN_PIN)); // disable Vout
615  sleep_ms(100); // wait for voltage to settle and debounce
617  printf("3V3 output is high while the power output should be switched off\n");
618 #if DEBUG
619  while (true);
620 #else
621  goto error;
622 #endif
623  }
624  pinout[2] = NULL; // clear pin to test
625 
626  // test I/O pins
627  for (uint8_t io=1; io<=6; io++) { // test each I/O pin
628  for (uint8_t pin=0; pin<LENGTH(busvoodoo_io_ports) && pin<LENGTH(busvoodoo_io_pins) && pin<LENGTH(busvoodoo_io_groups); pin++) { // look for a pin mapped on this I/O
629  if (busvoodoo_io_groups[pin]==io) {
630  gpio_set(busvoodoo_io_ports[pin], busvoodoo_io_pins[pin]); // set pin high
631  gpio_set_mode(busvoodoo_io_ports[pin], GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, busvoodoo_io_pins[pin]); // set pin to output
632  printf("%sI/O pin %u\n", lv_to, io+4);
633  led_red(); // notify user to perform action
634  pinout[io+3] = "O"; // set target testing pin
635  busvoodoo_oled_text_pinout((const char**)pinout, true); // display pins to user
636  busvoodoo_oled_update(); // update screen
637  do {
638  sleep_ms(100); // wait for user to make connection
639  } while (busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL)<0.2 && !user_input_available); // wait until pin is connected
640  led_blue(); // notify user test is running
641  if (user_input_available) { // user interruption
642  goto error;
643  }
644  gpio_clear(busvoodoo_io_ports[pin], busvoodoo_io_pins[pin]); // set pin low
645  sleep_ms(100); // wait for voltage to settle and debounce
647  printf("I/O pin %u is high while it should be low\n", io+4);
648 #if DEBUG
649  while (true);
650 #else
651  goto error;
652 #endif
653  }
654  gpio_set_mode(busvoodoo_io_ports[pin], GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, busvoodoo_io_pins[pin]); // set pin back to input
655  pinout[io+3] = NULL; // clear pin to test
656  break; // stop looking for pin
657  }
658  }
659  }
660 
661  if (busvoodoo_full) {
662  pinout[3] = NULL; // reset testing pin
663 
664  // test HV output on RS/CAN pin 1
665  double voltage = busvoodoo_vreg_get(BUSVOODOO_3V3_CHANNEL); // get reference voltage
666  uint16_t dac_set = BUSVOODOO_HV_SET(5.0)/voltage*4095; // DAC value corresponding to the voltage
667  dac_load_data_buffer_single(dac_set, RIGHT12, BUSVOODOO_HVCTL_CHANNEL); // set output so the voltage regulator is set to desired output voltage
668  dac_software_trigger(BUSVOODOO_HVCTL_CHANNEL); // transfer the value to the DAC
669  dac_enable(BUSVOODOO_HVCTL_CHANNEL); // enable DAC
670  gpio_clear(GPIO(BUSVOODOO_HVEN_PORT), GPIO(BUSVOODOO_HVEN_PIN)); // enable HV voltage regulator
671  printf("%sRS/CAN pin 1\n", lv_to);
672  led_red(); // notify user to perform action
673  pinout[0] = "O"; // set target testing pin
674  busvoodoo_oled_text_pinout((const char**)pinout, false); // display pins to user
675  busvoodoo_oled_update(); // update screen
676  do {
677  sleep_ms(100); // wait for user to make connection
678  } while (busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL)<0.2 && !user_input_available); // wait until pin is connected
679  led_blue(); // notify user test is running
680  if (user_input_available) { // user interruption
681  goto error;
682  }
683  gpio_set(GPIO(BUSVOODOO_HVEN_PORT), GPIO(BUSVOODOO_HVEN_PIN)); // disable HV voltage regulator
684  dac_disable(BUSVOODOO_HVCTL_CHANNEL); // disable HV control
685  sleep_ms(100); // wait for voltage to settle (and debounce)
687  printf("HV output is high while voltage regulator should be switched off\n");
688 #if DEBUG
689  while (true);
690 #else
691  goto error;
692 #endif
693  }
694  pinout[0] = NULL; // clear pin to test
695 
696  // test RS-485 output A
697  gpio_set(GPIO(BUSVOODOO_RS485_DE_PORT), GPIO(BUSVOODOO_RS485_DE_PIN)); // enable transmitter
698  gpio_set_mode(GPIO(BUSVOODOO_RS485_TX_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(BUSVOODOO_RS485_TX_PIN)); // set TX as output
699  gpio_set(GPIO(BUSVOODOO_RS485_TX_PORT), GPIO(BUSVOODOO_RS485_TX_PIN)); // set TX high -> A will be high
700  printf("%sRS/CAN pin 3\n", lv_to);
701  led_red(); // notify user to perform action
702  pinout[4] = "O"; // set target testing pin
703  busvoodoo_oled_text_pinout((const char**)pinout, false); // display pins to user
704  busvoodoo_oled_update(); // update screen
705  do {
706  sleep_ms(100); // wait for user to make connection
707  } while (busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL)<2.0 && !user_input_available); // wait until pin is connected
708  led_blue(); // notify user test is running
709  if (user_input_available) { // user interruption
710  goto error;
711  }
712  gpio_clear(GPIO(BUSVOODOO_RS485_TX_PORT), GPIO(BUSVOODOO_RS485_TX_PIN)); // set TX low
713  sleep_ms(100); // wait for voltage to settle (and debounce)
715  printf("RS-485 output A is high while it should be low\n");
716 #if DEBUG
717  while (true);
718 #else
719  goto error;
720 #endif
721  }
722  gpio_clear(GPIO(BUSVOODOO_RS485_DE_PORT), GPIO(BUSVOODOO_RS485_DE_PIN)); // set low to disable transmitter
723  gpio_set_mode(GPIO(BUSVOODOO_RS485_TX_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_RS485_TX_PIN)); // set pin to floating
724  pinout[4] = NULL; // clear pin to test
725 
726  // test CAN output L
727  // configure transceiver
728  gpio_clear(GPIO(BUSVOODOO_CAN_EN_PORT), GPIO(BUSVOODOO_CAN_EN_PIN)); // pull low to power transceiver
729  gpio_set_mode(GPIO(BUSVOODOO_CAN_TX_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO(BUSVOODOO_CAN_TX_PIN)); // set TX as output (there is a pull-up resistor to 5V)
730  // test recessive output
731  gpio_set(GPIO(BUSVOODOO_CAN_TX_PORT), GPIO(BUSVOODOO_CAN_TX_PIN)); // set TX high
732  sleep_us(100); // let voltage settle
733  voltage = busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL); // get output voltage
734  if (voltage<0.5) { // normally the lower limit is 2V, put pin 4 pulls it down because it is not strongly driven
735  printf("CAN L output is low while it should be at recessive 2.5V\n");
736 #if DEBUG
737  while (true);
738 #else
739  goto error;
740 #endif
741  }
742  if (voltage>3.0) { // normally the upper limit is 3V
743  printf("CAN L output is high while it should be at recessive 2.5V\n");
744 #if DEBUG
745  while (true);
746 #else
747  goto error;
748 #endif
749  }
750  // test dominant output
751  gpio_clear(GPIO(BUSVOODOO_CAN_TX_PORT), GPIO(BUSVOODOO_CAN_TX_PIN)); // set TX low
752  sleep_us(100); // let voltage settle
753  voltage = busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL); // get output voltage
754  if (voltage>2.5) {
755  printf("CAN L output is high while it should at dominant <2.25V\n");
756 #if DEBUG
757  while (true);
758 #else
759  goto error;
760 #endif
761  }
762  // release transceiver
763  gpio_set(GPIO(BUSVOODOO_CAN_EN_PORT), GPIO(BUSVOODOO_CAN_EN_PIN)); // set high to power off transceiver
764  gpio_set_mode(GPIO(BUSVOODOO_CAN_TX_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_CAN_TX_PIN)); // set pin to floating
765 
766  // test RS-485 port pin B
767  gpio_set(GPIO(BUSVOODOO_RS485_DE_PORT), GPIO(BUSVOODOO_RS485_DE_PIN)); // enable transmitter
768  gpio_set_mode(GPIO(BUSVOODOO_RS485_TX_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(BUSVOODOO_RS485_TX_PIN)); // set TX as output
769  gpio_clear(GPIO(BUSVOODOO_RS485_TX_PORT), GPIO(BUSVOODOO_RS485_TX_PIN)); // set TX low -> B will be high
770  printf("%sRS/CAN pin 4\n", lv_to);
771  led_red(); // notify user to perform action
772  pinout[6] = "O"; // set target testing pin
773  busvoodoo_oled_text_pinout((const char**)pinout, false); // display pins to user
774  busvoodoo_oled_update(); // update screen
775  do {
776  sleep_ms(100); // wait for user to make connection
777  } while (busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL)<2.0 && !user_input_available); // wait until pin is connected
778  led_blue(); // notify user test is running
779  if (user_input_available) { // user interruption
780  goto error;
781  }
782  gpio_set(GPIO(BUSVOODOO_RS485_TX_PORT), GPIO(BUSVOODOO_RS485_TX_PIN)); // set TX high
783  sleep_ms(100); // wait for voltage to settle (and debounce)
785  printf("RS-485 output B is high while it should be low\n");
786 #if DEBUG
787  while (true);
788 #else
789  goto error;
790 #endif
791  }
792  gpio_clear(GPIO(BUSVOODOO_RS485_DE_PORT), GPIO(BUSVOODOO_RS485_DE_PIN)); // set low to disable transmitter
793  gpio_set_mode(GPIO(BUSVOODOO_RS485_TX_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_RS485_TX_PIN)); // set pin to floating
794  pinout[6] = NULL; // clear pin to test
795 
796  // test CAN output H
797  // configure transceiver
798  gpio_clear(GPIO(BUSVOODOO_CAN_EN_PORT), GPIO(BUSVOODOO_CAN_EN_PIN)); // pull low to power transceiver
799  gpio_set_mode(GPIO(BUSVOODOO_CAN_TX_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO(BUSVOODOO_CAN_TX_PIN)); // set TX as output (there is a pull-up resistor to 5V)
800  // test recessive output
801  gpio_set(GPIO(BUSVOODOO_CAN_TX_PORT), GPIO(BUSVOODOO_CAN_TX_PIN)); // set TX high
802  sleep_us(100); // let voltage settle
803  voltage = busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL); // get output voltage
804  if (voltage<0.5) { // normally the lower limit is 2V, put pin 4 pulls it down because it is not strongly driven
805  printf("CAN H output is low while it should be at recessive 2.5V\n");
806 #if DEBUG
807  while (true);
808 #else
809  goto error;
810 #endif
811  }
812  if (voltage>3.0) { // normally the upper limit is 3V
813  printf("CAN H output is high while it should be at recessive 2.5V\n");
814 #if DEBUG
815  while (true);
816 #else
817  goto error;
818 #endif
819  }
820  // test dominant output
821  gpio_clear(GPIO(BUSVOODOO_CAN_TX_PORT), GPIO(BUSVOODOO_CAN_TX_PIN)); // set TX low
822  sleep_us(100); // let voltage settle
823  voltage = busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL); // get output voltage
824  if (voltage<2.5) {
825  printf("CAN H output is low while it dominant >2.75V\n");
826  printf("%.02f\n", voltage);
827 #if DEBUG
828  while (true);
829 #else
830  goto error;
831 #endif
832  }
833  // release transceiver
834  gpio_set(GPIO(BUSVOODOO_CAN_EN_PORT), GPIO(BUSVOODOO_CAN_EN_PIN)); // set high to power off transceiver
835  gpio_set_mode(GPIO(BUSVOODOO_CAN_TX_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_CAN_TX_PIN)); // set pin to floating
836 
837  // test RS-232 port (with itself)
838  rcc_periph_clock_enable(RCC_GPIO(BUSVOODOO_RS232_EN_PORT)); // enable clock for GPIO domain
839  gpio_clear(GPIO(BUSVOODOO_RS232_EN_PORT), GPIO(BUSVOODOO_RS232_EN_PIN)); // set low to enable receiver
840  gpio_set_mode(GPIO(BUSVOODOO_RS232_EN_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO(BUSVOODOO_RS232_EN_PIN)); // set pin as output (open-drain pulled high to disable receiver)
841  rcc_periph_clock_enable(RCC_GPIO(BUSVOODOO_RS232_SHDN_PORT)); // enable clock for GPIO domain
842  gpio_set(GPIO(BUSVOODOO_RS232_SHDN_PORT), GPIO(BUSVOODOO_RS232_SHDN_PIN)); // set high to enable transmitter
843  gpio_set_mode(GPIO(BUSVOODOO_RS232_SHDN_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(BUSVOODOO_RS232_SHDN_PIN)); // set pin as output (push-pull pulled low to disable transmitter)
844  rcc_periph_clock_enable(RCC_GPIO(BUSVOODOO_RS232_TX_PORT)); // enable clock for GPIO
845  gpio_set_mode(GPIO(BUSVOODOO_RS232_TX_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(BUSVOODOO_RS232_TX_PIN)); // set pin as output (push-pull)
846  rcc_periph_clock_enable(RCC_GPIO(BUSVOODOO_RS232_RX_PORT)); // enable clock for GPIO
847  gpio_set_mode(GPIO(BUSVOODOO_RS232_RX_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO(BUSVOODOO_RS232_RX_PIN)); // set pin as input (with pull resistors)
848  // start by setting low since unconnected (pulled to ground by 3 kO) is considered as high
849  gpio_clear(GPIO(BUSVOODOO_RS232_TX_PORT), GPIO(BUSVOODOO_RS232_TX_PIN)); // set low
850  gpio_set(GPIO(BUSVOODOO_RS232_RX_PORT), GPIO(BUSVOODOO_RS232_RX_PIN)); // pull high to avoid false negative
851  sleep_ms(5);
852  printf("connect RS/CAN pin 2 to RS/CAN pin 3\n");
853  led_red(); // notify user to perform action
854  pinout[2] = "O"; // set target testing pin
855  pinout[4] = "O"; // set target testing pin
856  busvoodoo_oled_text_pinout((const char**)pinout, false); // display pins to user
857  busvoodoo_oled_update(); // update screen
858  do {
859  sleep_ms(100); // wait for user to make connection
860  } while (gpio_get(GPIO(BUSVOODOO_RS232_RX_PORT), GPIO(BUSVOODOO_RS232_RX_PIN)) && !user_input_available); // wait until pin is connected
861  led_blue(); // notify user test is running
862  if (user_input_available) { // user interruption
863  goto error;
864  }
865  gpio_set(GPIO(BUSVOODOO_RS232_TX_PORT), GPIO(BUSVOODOO_RS232_TX_PIN)); // set high
866  gpio_clear(GPIO(BUSVOODOO_RS232_RX_PORT), GPIO(BUSVOODOO_RS232_RX_PIN)); // pull low to avoid false negative
867  sleep_ms(100); // wait for voltage to settle and debounce
868  if (!gpio_get(GPIO(BUSVOODOO_RS232_RX_PORT), GPIO(BUSVOODOO_RS232_RX_PIN))) { // check if RX is set low by TX
869  printf("RS-232 RX is high while it should be set low by RS-232 TX\n");
870 #if DEBUG
871  while (true);
872 #else
873  goto error;
874 #endif
875  }
876  gpio_set_mode(GPIO(BUSVOODOO_RS232_TX_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_RS232_TX_PIN)); // free pin
877  gpio_set_mode(GPIO(BUSVOODOO_RS232_RX_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_RS232_RX_PIN)); // free pin
878  pinout[2] = NULL; // clear pin to test
879  pinout[4] = NULL; // clear pin to test
880 
881  rcc_periph_clock_enable(RCC_GPIO(BUSVOODOO_RS232_RTS_PORT)); // enable clock for GPIO
882  gpio_set_mode(GPIO(BUSVOODOO_RS232_RTS_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(BUSVOODOO_RS232_RTS_PIN)); // set pin as output (push-pull)
883  rcc_periph_clock_enable(RCC_GPIO(BUSVOODOO_RS232_CTS_PORT)); // enable clock for GPIO
884  gpio_set_mode(GPIO(BUSVOODOO_RS232_CTS_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO(BUSVOODOO_RS232_CTS_PIN)); // set pin as input (with pull resistors)
885  // start by setting low since unconnected (pulled to ground by 3 kO) is considered as high
886  gpio_clear(GPIO(BUSVOODOO_RS232_RTS_PORT), GPIO(BUSVOODOO_RS232_RTS_PIN)); // set low
887  gpio_set(GPIO(BUSVOODOO_RS232_CTS_PORT), GPIO(BUSVOODOO_RS232_CTS_PIN)); // pull high to avoid false negative
888  printf("connect RS/CAN pin 4 to RS/CAN pin 5\n");
889  led_red(); // notify user to perform action
890  pinout[6] = "O"; // set target testing pin
891  pinout[8] = "O"; // set target testing pin
892  busvoodoo_oled_text_pinout((const char**)pinout, false); // display pins to user
893  busvoodoo_oled_update(); // update screen
894  do {
895  sleep_ms(100); // wait for user to make connection
896  } while (gpio_get(GPIO(BUSVOODOO_RS232_CTS_PORT), GPIO(BUSVOODOO_RS232_CTS_PIN)) && !user_input_available); // wait until pin is connected
897  led_blue(); // notify user test is running
898  if (user_input_available) { // user interruption
899  goto error;
900  }
902  gpio_clear(GPIO(BUSVOODOO_RS232_CTS_PORT), GPIO(BUSVOODOO_RS232_CTS_PIN)); // pull low to avoid false negative
903  sleep_ms(100); // wait for voltage to settle an debounce
904  if (!gpio_get(GPIO(BUSVOODOO_RS232_CTS_PORT), GPIO(BUSVOODOO_RS232_CTS_PIN))) { // check if CTS is set high by RTS
905  printf("RS-232 CTS is high while it should be set low by RS-232 RTS\n");
906 #if DEBUG
907  while (true);
908 #else
909  goto error;
910 #endif
911  }
912  gpio_set_mode(GPIO(BUSVOODOO_RS232_RTS_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_RS232_RTS_PIN)); // free pin
913  gpio_set_mode(GPIO(BUSVOODOO_RS232_CTS_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_RS232_CTS_PIN)); // free pin
914  gpio_set(GPIO(BUSVOODOO_RS232_EN_PORT), GPIO(BUSVOODOO_RS232_EN_PIN)); // set high to disable receiver
915  gpio_clear(GPIO(BUSVOODOO_RS232_SHDN_PORT), GPIO(BUSVOODOO_RS232_SHDN_PIN)); // set low to disable transmitter
916  pinout[6] = NULL; // clear pin to test
917  pinout[8] = NULL; // clear pin to test
918  }
919 
920  to_return = true; // all tests passed
921 error:
922  busvoodoo_safe_state(); // go back to safe state
923  busvoodoo_oled_text_left("HiZ"); // reset mode text
925  busvoodoo_oled_update(); // update display to show text and pinout
926  if (user_input_available) { // user interrupted the test
927  printf("user interrupted the test\n");
928  to_return = true; // we don't consider this as error
929  }
930  if (!to_return) { // test has not been completed
931  printf("the test procedure has been aborted for safety reasons\n");
932  }
933 
934  return to_return;
935 }
936 
937 // command handlers
938 
944 {
945  (void)argument; // we won't use the argument
946  RCC_CSR |= RCC_CSR_RMVF; // clear reset flags
947  scb_reset_core(); // reset core (the bootloader will interpret it as starting into DFU)
948  while (true); // wait for the reset to happen
949 }
950 
955 static void busvoodoo_hiz_version(void* argument)
956 {
957  (void)argument; // we won't use the argument
958  printf("BusVoodoo flavor: %s\n", busvoodoo_full ? "full" : "light");
959  printf("hardware version: %c\n", busvoodoo_version);
960  printf("firmware date: %04u-%02u-%02u\n", BUILD_YEAR, BUILD_MONTH, BUILD_DAY);
961  printf("device ID: %08x%08x%08x\n", DESIG_UNIQUE_ID0, DESIG_UNIQUE_ID1, DESIG_UNIQUE_ID2);
962 }
963 
964 
969 {
970  (void)argument; // we won't use the argument
971  printf("performing self-test\n");
972  printf("remove all cables from connectors and press space to start (or any other key to abort)\n");
973  led_red(); // show red LED to indicate test started
974  if (' '==user_input_get()) { // space entered
975  if (busvoodoo_hiz_test_self()) { // perform self-test
976  led_blue(); // show blue to indicate test passed
977  printf("self-test succeeded\n"); // notify user
978  } else {
979  led_blink(0.5, 0.5); // show error on LEDs
980  printf("self-test failed\n"); // notify user
981  }
982  } else {
983  printf("self-test aborted\n");
984  }
985 }
986 
991 {
992  (void)argument; // we won't use the argument
993  printf("performing pins test\n");
994  printf("remove all cables from connectors and press space to start (or any other key to abort)\n");
995  led_red(); // show red LED to indicate test started
996  if (' '==user_input_get()) { // space entered
997  if (busvoodoo_hiz_test_pins()) { // perform pin test
998  led_blue(); // show blue OK LED
999  printf("pins test succeeded\n"); // notify user
1000  } else {
1001  led_blink(0.5, 0.5); // show error on LEDs
1002  printf("pins test failed\n"); // notify user
1003  }
1004  } else {
1005  printf("pins test aborted\n");
1006  }
1007 }
1008 
1010 static const struct menu_command_t busvoodoo_hiz_commands[] = {
1011  {
1012  'v',
1013  "version",
1014  "show hardware and firmware version",
1016  NULL,
1018  },
1019  {
1020  'b',
1021  "bootloader",
1022  "reboot into DFU bootloader",
1024  NULL,
1026  },
1027  {
1028  's',
1029  "self-test",
1030  "perform board self-test",
1032  NULL,
1034  },
1035  {
1036  't',
1037  "pins-test",
1038  "perform connector pins test",
1040  NULL,
1042  },
1043 };
1044 
1046  "hiz",
1047  "High Impedance (Z)",
1052 };
#define BUSVOODOO_5V_CHANNEL
ADC channel to measure 3.3V rail.
float busvoodoo_hv_set(float voltage)
set voltage on high voltage adjustable voltage regulator
#define BUSVOODOO_LV_TEST
target LV output voltage to test if we can set control the LV voltage regulator
Definition: busvoodoo_hiz.c:41
static void busvoodoo_hiz_bootloader(void *argument)
switch to DFU bootloader
#define BUSVOODOO_HVCTL_CHANNEL
DAC channel to control HV output voltage.
command menu entry
Definition: menu.h:31
static const struct menu_command_t busvoodoo_hiz_commands[]
HiZ menu commands.
BusVoodoo global definitions and methods (API)
#define BUSVOODOO_HVEN_PIN
high voltage (HV) enable pin (active low)
#define RCC_GPIO(x)
get RCC for GPIO based on GPIO identifier
Definition: global.h:105
void busvoodoo_oled_text_pinout(const char *pins[10], bool io_connector)
draw pin names on bottom (blue) part in display buffer
#define BUSVOODOO_LVCTL_PORT
pin to control LV output voltage
bool busvoodoo_vout_switch(bool on)
switch 3V3 and 5V power outputs on I/O connector
#define BUSVOODOO_RS232_EN_PORT
RS-232 pin to enable receiver (active low, pulled up)
#define BUSVOODOO_HV_SET(x)
voltage to output for the DAC to set the desired HV output voltage (based on resistor values on the H...
static bool busvoodoo_hiz_test_pins(void)
test if signals are soldered correctly to the connector pins
#define BUSVOODOO_HVEN_PORT
high voltage (HV) enable pin (active low)
#define BUSVOODOO_RS232_CTS_PORT
RS-232 Clear-To-Send input pin.
void busvoodoo_oled_update(void)
update OLED display RAM with current display buffer
float busvoodoo_lv_set(float voltage)
set voltage on low voltage adjustable voltage regulator
struct busvoodoo_mode_t busvoodoo_hiz_mode
HiZ mode interface definition.
#define BUSVOODOO_LVEN_PIN
low voltage (LV) enable pin (active high)
const char * busvoodoo_global_pinout_rscan[5]
RS/CAN connector pinout.
const uint32_t busvoodoo_io_pins[13]
pin of individual signals
static bool busvoodoo_hiz_test_self(void)
perform self tests
Definition: busvoodoo_hiz.c:81
#define BUSVOODOO_RS232_TX_PORT
RS-232 Transmit output pin.
global definitions and methods (API)
enum menu_argument_t argument
what kind of argument it accepts
Definition: menu.h:35
static void busvoodoo_hiz_exit(void)
exit HiZ mode
Definition: busvoodoo_hiz.c:73
#define GPIO(x)
get GPIO based on GPIO identifier
Definition: global.h:103
#define BUSVOODOO_RS485_RX_PORT
RS-485 Receive input pin.
void sleep_us(uint32_t duration)
go to sleep for some microseconds
Definition: global.c:144
static void busvoodoo_hiz_command_test_self(void *argument)
command to perform board self-test
#define BUSVOODOO_HV_DEFAULT
default (when not driven) HV voltage regulator output voltage based on R1 and R2
Definition: busvoodoo_hiz.c:42
#define BUSVOODOO_CAN_EN_PORT
signal to power CAN transceiver (active low, pulled high)
#define BUSVOODOO_RS485_RE_PORT
RS-485 pin to enable receiver (active low, pulled up)
bool busvoodoo_full
is the BusVoodoo board fully populated (with HV voltage regulator, RS-232, RS-485, CAN transceiver on the back side)
static void busvoodoo_hiz_version(void *argument)
show BusVoodoo version
const uint32_t busvoodoo_io_ports[13]
port of individual signals
#define BUSVOODOO_RS485_TX_PORT
RS-485 Transmit output pin.
#define BUSVOODOO_RS232_CTS_PIN
RS-232 Clear-To-Send input pin.
#define BUSVOODOO_LV_CHANNEL
ADC channel to measure LV rail.
#define BUSVOODOO_RS232_TX_PIN
RS-232 Transmit output pin.
#define BUSVOODOO_RS485_DE_PORT
RS-485 pin to enable transmitter (active high, pulled low)
#define BUSVOODOO_RS232_RTS_PIN
RS-232 Request-To-Send output pin.
const uint8_t busvoodoo_io_groups[13]
which I/O pin (group) does the signal belong to
#define BUSVOODOO_CAN_RX_PIN
CAN Receive input pin.
#define BUILD_MONTH
build month as number if known, or 0 if unknown
Definition: global.h:94
#define BUSVOODOO_HV_CHANNEL
ADC channel to measure HV rail.
const char * busvoodoo_global_pinout_io[10]
I/O connector pinout.
#define BUSVOODOO_LVCTL_PIN
pin to control LV output voltage
char user_input_get(void)
get user input
Definition: global.c:187
#define BUSVOODOO_RS232_EN_PIN
RS-232 pin to enable receiver (active low, pulled up)
static bool busvoodoo_hiz_setup(char **prefix, const char *line)
setup HiZ mode
Definition: busvoodoo_hiz.c:50
float busvoodoo_vreg_get(uint8_t channel)
read voltage from power rail
const char * busvoodoo_io_names[13]
I/O individual signal names.
#define BUSVOODOO_VOUTEN_PORT
voltage output (5V and 3.3V) enable pin (active low)
void sleep_ms(uint32_t duration)
go to sleep for some milliseconds
Definition: global.c:159
#define BUSVOODOO_CAN_TX_PIN
CAN Transmit output pin.
#define BUSVOODOO_VOUTEN_PIN
voltage output (5V and 3.3V) enable pin (active low)
#define BUSVOODOO_RS232_RX_PORT
RS-232 Receive input pin.
#define BUSVOODOO_RS485_DE_PIN
RS-485 pin to enable transmitter (active high, pulled low)
void busvoodoo_oled_text_left(char *text)
draw mode text on top (yellow) left side in display buffer
#define BUSVOODOO_LVEN_PORT
low voltage (LV) enable pin (active high)
float busvoodoo_embedded_pullup(bool on)
enable embedded pull-up resistors
#define BUSVOODOO_HV_TEST
target HV output voltage to test if we can set control the HV voltage regulator
Definition: busvoodoo_hiz.c:43
#define BUSVOODOO_RS232_RTS_PORT
RS-232 Request-To-Send output pin.
#define LENGTH(x)
get the length of an array
Definition: global.h:26
#define BUSVOODOO_RS485_RE_PIN
RS-485 pin to enable receiver (active low, pulled up)
#define BUSVOODOO_CAN_RX_PORT
CAN Receive input pin.
void busvoodoo_oled_text_right(char *text)
draw mode text on top (yellow) right side in display buffer
#define BUSVOODOO_RS232_RX_PIN
RS-232 Receive input pin.
#define BUSVOODOO_LV_DEFAULT
default (when not driven) LV voltage regulator output voltage based on R1 and R2
Definition: busvoodoo_hiz.c:40
BusVoodoo high impedance (HiZ) default mode (API)
#define BUSVOODOO_RS485_RX_PIN
RS-485 Receive input pin.
library to show BusVoodoo mode information on SSD1306 OLED display: name, activity, pinout (API)
static void busvoodoo_hiz_command_test_pins(void *argument)
command to perform pins test
volatile bool user_input_available
flag set when user input is available
Definition: global.c:37
#define BUSVOODOO_RS485_TX_PIN
RS-485 Transmit output pin.
void busvoodoo_safe_state(void)
set safe state by disabling all outputs
#define BUILD_YEAR
build year as number if known, or 0 if unknown
Definition: global.h:92
char busvoodoo_version
version of the hardware board
BusVoodoo mode interface.
#define BUSVOODOO_RS232_SHDN_PORT
RS-232 pin to enable transmitter (active high, pulled low)
#define BUSVOODOO_3V3_CHANNEL
ADC channel to measure 5V rail.
#define BUSVOODOO_RS232_SHDN_PIN
RS-232 pin to enable transmitter (active high, pulled low)
#define BUILD_DAY
build day as number if known, or 0 if unknown
Definition: global.h:96
#define BUSVOODOO_CAN_TX_PORT
CAN Transmit output pin.
#define BUSVOODOO_CAN_EN_PIN
signal to power CAN transceiver (active low, pulled high)