CuVoodoo STM32F1 firmware template
busvoodoo_global.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  */
22 /* standard libraries */
23 #include <stdint.h> // standard integer types
24 #include <stdlib.h> // standard utilities
25 #include <string.h> // string utilities
26 #include <math.h> // math utilities
27 
28 /* STM32 (including CM3) libraries */
29 #include <libopencm3/cm3/nvic.h> // interrupt handler
30 #include <libopencm3/stm32/gpio.h> // general purpose input output library
31 #include <libopencm3/stm32/rcc.h> // real-time control clock library
32 #include <libopencm3/stm32/adc.h> // ADC utilities
33 #include <libopencm3/stm32/dac.h> // DAC utilities
34 #include <libopencm3/stm32/timer.h> // timer utilities
35 
36 /* own libraries */
37 #include "global.h" // board definitions
38 #include "menu.h" // command definitions
39 #include "print.h" // print utilities
40 #include "busvoodoo_global.h" // BusVoodoo definitions
41 
45 #define BUSVOODOO_LED_TIMER 1
49 static volatile uint32_t busvoodoo_global_led_blue_timeout = 0;
50 
51 static volatile bool busvoodoo_global_led_blue_timer = false;
53 static volatile uint32_t busvoodoo_global_led_red_timeout = 0;
55 static volatile bool busvoodoo_global_led_red_timer = false;
57 static volatile bool busvoodoo_global_led_blinking = false;
58 
60 static const float busvoodoo_version_voltages[] = {100.0/(10.0+100.0)*3.3}; // version A start with revision 27
61 
62 const char* busvoodoo_global_pinout_io[10] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
63 const char* busvoodoo_global_pinout_rscan[5] = {NULL, NULL, NULL, NULL, NULL};
64 
65 const char* busvoodoo_io_names[13] = {"I2C_SMBA/SPI_NSS/I2S_WS/UART1_CK", "SDIO_CMD", "UART1_CTS/SPI_SCK/I2S_CK", "SDIO_D3/UART2_RX", "I2C_SDA/UART1_RX", "SDIO_D0", "SPI_MOSI/I2S_SD", "SDIO_CK", "I2C_SCL/UART1_TX", "SDIO_D1", "I2S_MCK", "UART1_RTS/SPI_MISO", "SDIO_D2/UART2_TX"};
66 const uint32_t busvoodoo_io_ports[13] = {GPIOB, GPIOD, GPIOB, GPIOC, GPIOB, GPIOC, GPIOB, GPIOC, GPIOB, GPIOC, GPIOC, GPIOB, GPIOC};
67 const uint32_t busvoodoo_io_pins[13] = {GPIO12, GPIO2, GPIO13, GPIO11, GPIO11, GPIO8, GPIO15, GPIO12, GPIO10, GPIO9, GPIO6, GPIO14, GPIO10};
68 const uint8_t busvoodoo_io_groups[13] = {6, 6, 4, 4, 1, 1, 5, 5, 2, 2, 3, 3, 3};
69 
70 bool busvoodoo_full = false;
71 char busvoodoo_version = '0';
73 
74 void busvoodoo_setup(void)
75 {
76  // enable all GPIO domains since we use pins on all ports
77  rcc_periph_clock_enable(RCC_GPIOA); // enable clock for all GPIO domains
78  rcc_periph_clock_enable(RCC_GPIOB); // enable clock for all GPIO domains
79  rcc_periph_clock_enable(RCC_GPIOC); // enable clock for all GPIO domains
80  rcc_periph_clock_enable(RCC_GPIOD); // enable clock for all GPIO domains
81  rcc_periph_clock_enable(RCC_AFIO); // enable clock for alternate function (for communication)
82  busvoodoo_safe_state(); // put pins in safe state (for common light version)
83 
84  // check if this BusVoodoo is a full flavor
85  rcc_periph_clock_enable(RCC_ADC12_IN(BUSVOODOO_HV_CHANNEL)); // enable clock for GPIO domain for HV channel
87  gpio_set_mode(ADC12_IN_PORT(BUSVOODOO_HV_CHANNEL), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, ADC12_IN_PIN(BUSVOODOO_HV_CHANNEL)); // set HV channel as digital input with pull-up capabilities
88  // on a full version (fully populated board) the ADC HV signal will be pulled low
89  if (gpio_get(ADC12_IN_PORT(BUSVOODOO_HV_CHANNEL), ADC12_IN_PIN(BUSVOODOO_HV_CHANNEL))) { // check is ADC HV is pulled low
90  busvoodoo_full = false;
91  } else {
92  busvoodoo_full = true;
93  busvoodoo_safe_state(); // also put the full version pins in safe state
94  }
95 
96  // setup ADC to measure the 5V, 3.3V, LV, and HV power rails voltages, and hardware version channel
97  rcc_periph_clock_enable(RCC_ADC12_IN(BUSVOODOO_5V_CHANNEL)); // enable clock for GPIO domain for 5V channel
98  gpio_set_mode(ADC12_IN_PORT(BUSVOODOO_5V_CHANNEL), GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, ADC12_IN_PIN(BUSVOODOO_5V_CHANNEL)); // set 5V channel as analogue input for the ADC
99  rcc_periph_clock_enable(RCC_ADC12_IN(BUSVOODOO_3V3_CHANNEL)); // enable clock for GPIO domain for 3.3V channel
100  gpio_set_mode(ADC12_IN_PORT(BUSVOODOO_3V3_CHANNEL), GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, ADC12_IN_PIN(BUSVOODOO_3V3_CHANNEL)); // set 3.3V channel as analogue input for the ADC
101  rcc_periph_clock_enable(RCC_ADC12_IN(BUSVOODOO_LV_CHANNEL)); // enable clock for GPIO domain for LV channel
102  gpio_set_mode(ADC12_IN_PORT(BUSVOODOO_LV_CHANNEL), GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, ADC12_IN_PIN(BUSVOODOO_LV_CHANNEL)); // set LV channel as analogue input for the ADC
103  rcc_periph_clock_enable(RCC_ADC12_IN(BUSVOODOO_HV_CHANNEL)); // enable clock for GPIO domain for HV channel
104  gpio_set_mode(ADC12_IN_PORT(BUSVOODOO_HV_CHANNEL), GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, ADC12_IN_PIN(BUSVOODOO_HV_CHANNEL)); // set HV channel as analogue input for the ADC
105  rcc_periph_clock_enable(RCC_ADC12_IN(BUSVOODOO_HW_VERSION_CHANNEL)); // enable clock for GPIO domain for hardware version channel
106  gpio_set_mode(ADC12_IN_PORT(BUSVOODOO_HW_VERSION_CHANNEL), GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, ADC12_IN_PIN(BUSVOODOO_HW_VERSION_CHANNEL)); // set hardware version channel as analogue input for the ADC
107  rcc_periph_clock_enable(RCC_ADC1); // enable clock for ADC domain
108  adc_power_off(ADC1); // switch off ADC while configuring it
109  adc_disable_scan_mode(ADC1); // ensure scan mode is disabled
110  adc_disable_discontinuous_mode_regular(ADC1); // ensure discontinuous mode is not used
111  adc_set_single_conversion_mode(ADC1); // ensure continuous mode is not used (that's not the same as discontinuous)
112  adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_28DOT5CYC); // use 28.5 cycles to sample (long enough to be stable)
113  adc_enable_temperature_sensor(); // enable internal voltage reference
114  adc_power_on(ADC1); // switch on ADC
115  sleep_us(1); // wait t_stab for the ADC to stabilize
116  adc_reset_calibration(ADC1); // remove previous non-calibration
117  adc_calibrate(ADC1); // calibrate ADC for less accuracy errors
118 
119  // find out version of the board
120  gpio_set_mode(GPIO(BUSVOODOO_HW_VERSION_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO(BUSVOODOO_HW_VERSION_PIN)); // use pull up and down to check if a voltage divider is present on the pin
122  bool version_up = (0!=gpio_get(GPIO(BUSVOODOO_HW_VERSION_PORT), GPIO(BUSVOODOO_HW_VERSION_PIN))); // check if the signal is still up
123  gpio_clear(GPIO(BUSVOODOO_HW_VERSION_PORT), GPIO(BUSVOODOO_HW_VERSION_PIN)); // pull down
124  bool version_down = (0==gpio_get(GPIO(BUSVOODOO_HW_VERSION_PORT), GPIO(BUSVOODOO_HW_VERSION_PIN))); // check if the signal is still down
125  gpio_set_mode(ADC12_IN_PORT(BUSVOODOO_HW_VERSION_CHANNEL), GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, ADC12_IN_PIN(BUSVOODOO_HW_VERSION_CHANNEL)); // put back to analog input
126  // get version
127  if (version_up && version_down) { // no voltage divider on pin
128  busvoodoo_version = '0'; // the pin is floating only for version 0 (= revision 18)
129  } else { // voltage divider on pin
130  float version_voltage = busvoodoo_vreg_get(BUSVOODOO_HW_VERSION_CHANNEL); // measure hardware version voltage
131  for (uint8_t i=0; i<LENGTH(busvoodoo_version_voltages); i++) { // go through expected version voltages
132  if (version_voltage>busvoodoo_version_voltages[i]-0.2 && version_voltage<busvoodoo_version_voltages[i]+0.2) { // verify if voltage matches
133  busvoodoo_version = 'A'+i; // remember version name for matching voltage
134  break; // stop searching
135  }
136  }
137  }
138 
139  // setup DAC to control LV and HV voltage outputs
140  gpio_set_mode(GPIO(BUSVOODOO_LVCTL_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, GPIO(BUSVOODOO_LVCTL_PIN)); // set LV pin as analog (the DAC will use it as output)
141  rcc_periph_clock_enable(RCC_DAC); // enable clock for DAC domain
142  dac_disable(BUSVOODOO_LVCTL_CHANNEL); // disable output to configure it properly
143  dac_buffer_enable(BUSVOODOO_LVCTL_CHANNEL); // enable output buffer to be able to drive larger loads (should be per default)
144  if (busvoodoo_full) {
145  gpio_set_mode(GPIO(BUSVOODOO_HVCTL_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, GPIO(BUSVOODOO_HVCTL_PIN)); // set HV pin as analog (the DAC will use it as output)
146  dac_disable(BUSVOODOO_HVCTL_CHANNEL); // disable output to configure it properly
147  dac_buffer_enable(BUSVOODOO_HVCTL_CHANNEL); // enable output buffer to be able to drive larger loads (should be per default)
148  }
149  dac_set_trigger_source(DAC_CR_TSEL1_SW); // use software to trigger the voltage change
150  dac_set_trigger_source(DAC_CR_TSEL2_SW); // use software to trigger the voltage change
151 
152  // enable timer for LED pulsing or blinking
153  rcc_periph_clock_enable(RCC_TIM(BUSVOODOO_LED_TIMER)); // enable clock for timer domain
154  timer_reset(TIM(BUSVOODOO_LED_TIMER)); // reset timer configuration
155  timer_set_mode(TIM(BUSVOODOO_LED_TIMER), TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); // configure timer to up counting mode
156  timer_set_prescaler(TIM(BUSVOODOO_LED_TIMER), 3296-1); // set prescaler to allow 3/3 seconds PWM output (72MHz/2^16/3296=0.33Hz)
157  timer_set_oc_mode(TIM(BUSVOODOO_LED_TIMER), TIM_OC1, TIM_OCM_PWM1); // use PWM output compare mode (for blinking)
158  timer_disable_oc_output(TIM(BUSVOODOO_LED_TIMER), TIM_OC1); // disable output compare output (for now)
159  timer_enable_break_main_output(TIM(BUSVOODOO_LED_TIMER)); // required to enable timer 1, even when no dead time is used
160  timer_set_period(TIM(BUSVOODOO_LED_TIMER), (rcc_ahb_frequency/3296)/1000); // set period to 1 ms for pulsing
161  nvic_enable_irq(NVIC_TIM1_UP_IRQ); // enable interrupt for timer 1 to catch update event when overflowing
162  // disable LEDs and reset state
163  gpio_set_mode(GPIO(LED_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(LED_PIN)); // set LED pin to floating to disable both LEDs
164  busvoodoo_global_led_blue_timeout = 0; // no timeout needed
165  busvoodoo_global_led_blue_timer = false; // no timeout needed
166  busvoodoo_global_led_red_timeout = 0; // no timeout needed
167  busvoodoo_global_led_red_timer = false; // no timeout needed
168  busvoodoo_global_led_blinking = false; // start in pulse mode
169 }
170 
172 {
173  // disable voltage outputs
174  gpio_set(GPIO(BUSVOODOO_VOUTEN_PORT), GPIO(BUSVOODOO_VOUTEN_PIN)); // disable 5V and 3.3V output on connector
175  gpio_set_mode(GPIO(BUSVOODOO_VOUTEN_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO(BUSVOODOO_VOUTEN_PIN)); // set pin as output (open-drain pulled high to disable the pMOS)
176  gpio_clear(GPIO(BUSVOODOO_LVEN_PORT), GPIO(BUSVOODOO_LVEN_PIN)); // disable LV voltage regulator
177  gpio_set_mode(GPIO(BUSVOODOO_LVEN_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(BUSVOODOO_LVEN_PIN)); // set pin as output (push-pull, pulled low for safety)
178  gpio_set(GPIO(BUSVOODOO_HVEN_PORT), GPIO(BUSVOODOO_HVEN_PIN)); // disable HV voltage regulator
179  gpio_set_mode(GPIO(BUSVOODOO_HVEN_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO(BUSVOODOO_HVEN_PIN)); // set pin as output (open-drain pulled high to disable the pMOS)
180 
181  // set DAC channel back to analog
182  gpio_set_mode(GPIO(BUSVOODOO_LVCTL_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, GPIO(BUSVOODOO_LVCTL_PIN)); // set LV pin as analog
183  gpio_set_mode(GPIO(BUSVOODOO_HVCTL_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, GPIO(BUSVOODOO_HVCTL_PIN)); // set HV pin as analog
184 
185  // disable embedded pull-ups
186  gpio_primary_remap(AFIO_MAPR_SWJ_CFG_JTAG_OFF_SW_ON, 0); // disable JTAG (but keep SWD) so to use the underlying GPIOs (PA15, PB3, PB4)
187  gpio_set(GPIO(BUSVOODOO_5VPULLUP_PORT), GPIO(BUSVOODOO_5VPULLUP_PIN)); // set pin high to disable 5V embedded pull-up
188  gpio_set_mode(GPIO(BUSVOODOO_5VPULLUP_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO(BUSVOODOO_5VPULLUP_PIN)); // set pin as output (open-drain pulled high to disable the pMOS)
189  gpio_set(GPIO(BUSVOODOO_OEPULLUP_PORT), GPIO(BUSVOODOO_OEPULLUP_PIN)); // set pin high to disable embedded pull-up bus switch
190  gpio_set_mode(GPIO(BUSVOODOO_OEPULLUP_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO(BUSVOODOO_OEPULLUP_PIN)); // set pin as output (open-drain pulled high to disable the bus switch)
191 
192  // disable all signal I/O outputs
193  for (uint8_t pin=0; pin<LENGTH(busvoodoo_io_ports) && pin<LENGTH(busvoodoo_io_pins); pin++) {
194  gpio_set_mode(busvoodoo_io_ports[pin], GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, busvoodoo_io_pins[pin]); // set pin back to input (floating)
195  }
196 
197  if (busvoodoo_full) {
198  // disable RS-232 signals
199  gpio_set_mode(GPIO(BUSVOODOO_RS232_TX_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_RS232_TX_PIN)); // set pin to floating
200  gpio_set_mode(GPIO(BUSVOODOO_RS232_RX_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_RS232_RX_PIN)); // set pin to floating
201  gpio_set_mode(GPIO(BUSVOODOO_RS232_RTS_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_RS232_RTS_PIN)); // set pin to floating
202  gpio_set_mode(GPIO(BUSVOODOO_RS232_CTS_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_RS232_CTS_PIN)); // set pin to floating
203  gpio_set(GPIO(BUSVOODOO_RS232_EN_PORT), GPIO(BUSVOODOO_RS232_EN_PIN)); // set high to disable receiver
204  gpio_set_mode(GPIO(BUSVOODOO_RS232_EN_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO(BUSVOODOO_RS232_EN_PIN)); // use external pull-up resistor to set high by default
205  gpio_clear(GPIO(BUSVOODOO_RS232_SHDN_PORT), GPIO(BUSVOODOO_RS232_SHDN_PIN)); // set low to disable transmitter
206  gpio_set_mode(GPIO(BUSVOODOO_RS232_SHDN_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(BUSVOODOO_RS232_SHDN_PIN)); // there is also an external pull-down resistor to disable per default
207 
208  // disable RS-485 signals (RS and TX are shared with RS-232)
209  gpio_set(GPIO(BUSVOODOO_RS485_RE_PORT), GPIO(BUSVOODOO_RS485_RE_PIN)); // set high to disable receiver
210  gpio_set_mode(GPIO(BUSVOODOO_RS485_RE_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO(BUSVOODOO_RS485_RE_PIN)); // use external pull-up resistor to set high by default
211  gpio_clear(GPIO(BUSVOODOO_RS485_DE_PORT), GPIO(BUSVOODOO_RS485_DE_PIN)); // set low to disable transmitter
212  gpio_set_mode(GPIO(BUSVOODOO_RS485_DE_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(BUSVOODOO_RS485_DE_PIN)); // there is also an external pull-down resistor to disable per default
213  gpio_set_mode(GPIO(BUSVOODOO_RS485_TX_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_RS485_TX_PIN)); // set pin to floating
214  gpio_set_mode(GPIO(BUSVOODOO_RS485_RX_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_RS485_RX_PIN)); // set pin to floating
215 
216 #if BUSVOODOO_HARDWARE_VERSION!=0
217  // disable CAN transceiver and signals (put back to input floating)
218  gpio_set_mode(GPIO(BUSVOODOO_CAN_TX_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_CAN_TX_PIN)); // set pin to floating
219  gpio_set_mode(GPIO(BUSVOODOO_CAN_RX_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_CAN_RX_PIN)); // set pin to floating
220  gpio_set(GPIO(BUSVOODOO_CAN_EN_PORT), GPIO(BUSVOODOO_CAN_EN_PIN)); // set high to power off transceiver
221  gpio_set_mode(GPIO(BUSVOODOO_CAN_EN_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO(BUSVOODOO_CAN_EN_PIN)); // use external pull-up resistor to set high by default
222 #endif
223  }
224 }
225 
227 {
228  if (on) { // we need to switch on Vout
229  gpio_clear(GPIO(BUSVOODOO_VOUTEN_PORT), GPIO(BUSVOODOO_VOUTEN_PIN)); // enable Vout
230  } else {
231  gpio_set(GPIO(BUSVOODOO_VOUTEN_PORT), GPIO(BUSVOODOO_VOUTEN_PIN)); // disable Vout
232  }
233 
234  bool to_return = true;
235  sleep_ms(1); // wait a bit for voltage to settle
236  float voltage = busvoodoo_vreg_get(BUSVOODOO_5V_CHANNEL); // get 5V power rail voltage
237  if (voltage<4.0 || voltage>5.5) {
238  to_return = false; // voltage output is not ok
239  }
240  voltage = busvoodoo_vreg_get(BUSVOODOO_3V3_CHANNEL); // get 3.3V power rail voltage
241  if (voltage<3.0 || voltage>3.6) {
242  to_return = true; // voltage output is not ok
243  }
244  return to_return;
245 }
246 
247 float busvoodoo_vreg_get(uint8_t channel)
248 {
249  if (channel!=BUSVOODOO_5V_CHANNEL && channel!=BUSVOODOO_3V3_CHANNEL && channel!=BUSVOODOO_LV_CHANNEL && channel!=BUSVOODOO_HV_CHANNEL && channel!=BUSVOODOO_HW_VERSION_CHANNEL) { // check channel
250  return NAN;
251  }
252 
253  // start by reading the internal voltage
254  uint8_t channels[1] = {ADC_CHANNEL17}; // voltages to convert: internal
255  adc_set_regular_sequence(ADC1, LENGTH(channels), channels); // set channel to convert
256  ADC_SR(ADC1) = 0; // reset flags
257  adc_start_conversion_direct(ADC1); // start conversion (without using trigger)
258  while (!adc_eoc(ADC1)); // wait until conversion finished
259  uint16_t internal_value = adc_read_regular(ADC1); // read voltage value (clears flag)
260 
261  // read desired voltage
262  switch (channel) {
264  channels[0] = ADC_CHANNEL(BUSVOODOO_5V_CHANNEL);
265  break;
267  channels[0] = ADC_CHANNEL(BUSVOODOO_3V3_CHANNEL);
268  break;
270  channels[0] = ADC_CHANNEL(BUSVOODOO_LV_CHANNEL);
271  break;
273  channels[0] = ADC_CHANNEL(BUSVOODOO_HV_CHANNEL);
274  break;
277  break;
278  default: // unknown channel
279  return NAN;
280  }
281  adc_set_regular_sequence(ADC1, LENGTH(channels), channels); // set channel to convert
282  ADC_SR(ADC1) = 0; // reset flags
283  adc_start_conversion_direct(ADC1); // start conversion (without using trigger)
284  while (!adc_eoc(ADC1)); // wait until conversion finished
285  uint16_t desired_value = adc_read_regular(ADC1); // read voltage value (clears flag)
286 
287  // calculate desired voltage
288  float to_return = NAN; // voltage to return
289  switch (channel) { // get converted value and calculate according to the voltage divider on this channel
291  to_return = desired_value/(10.0/(10.0+10.0));
292  break;
294  to_return = desired_value/(10.0/(10.0+10.0));
295  break;
297  to_return = desired_value/(10.0/(10.0+10.0));
298  break;
300  to_return = desired_value/(1.5/(10.0+1.5));
301  break;
303  to_return = desired_value;
304  break;
305  default: // unknown channel
306  to_return = NAN;
307  break;
308  }
309  if (!isnan(to_return)) {
310  to_return *= 1.2/internal_value; // calculate voltage from converted values using internal 1.2V voltage reference
311  }
312  return to_return;
313 }
314 
315 float busvoodoo_lv_set(float voltage)
316 {
317  float volt = NAN; // common variable for voltages
318  if (voltage<=0.3) { // disable voltage regulator
319  gpio_clear(GPIO(BUSVOODOO_LVEN_PORT), GPIO(BUSVOODOO_LVEN_PIN)); // disable LV voltage regulator
320  dac_disable(BUSVOODOO_LVCTL_CHANNEL); // disable LV control
321  } else { // enable voltage regulator
322  if (voltage>4.85) { // use the 5V directly
323  gpio_clear(GPIO(BUSVOODOO_5VPULLUP_PORT), GPIO(BUSVOODOO_5VPULLUP_PIN)); // put 5V on LV line
324  } else { // use adjustable voltage regulator (5.0V rail minus LDO and diodes)
325  gpio_set(GPIO(BUSVOODOO_5VPULLUP_PORT), GPIO(BUSVOODOO_5VPULLUP_PIN)); // disable 5V input
326  volt = busvoodoo_vreg_get(BUSVOODOO_3V3_CHANNEL); // get reference voltage
327  if (isnan(voltage)) {
328  return NAN;
329  }
330  uint16_t dac_set = BUSVOODOO_LV_SET(voltage)/volt*4095; // DAC value corresponding to the voltage
331  dac_load_data_buffer_single(dac_set, RIGHT12, BUSVOODOO_LVCTL_CHANNEL); // set output so the voltage regulator is set to 2.5V
332  dac_software_trigger(BUSVOODOO_LVCTL_CHANNEL); // transfer the value to the DAC
333  dac_enable(BUSVOODOO_LVCTL_CHANNEL); // enable DAC
334  gpio_set(GPIO(BUSVOODOO_LVEN_PORT), GPIO(BUSVOODOO_LVEN_PIN)); // enable LV voltage regulator
335  }
336  }
337  sleep_ms(50); // let voltage settle
338  volt = busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL); // get LV voltage to return
339 
340  return volt; // return measured voltage
341 }
342 
343 float busvoodoo_hv_set(float voltage)
344 {
345  if (!busvoodoo_full) { // the HV voltage regulator is only present on the full version
346  return NAN;
347  }
348 
349  float volt = NAN; // common variable for voltages
350  if (voltage<3.29) { // disable voltage regulator
351  gpio_set(GPIO(BUSVOODOO_HVEN_PORT), GPIO(BUSVOODOO_HVEN_PIN)); // disable HV voltage regulator
352  dac_disable(BUSVOODOO_HVCTL_CHANNEL); // disable HV control
353  } else {
354  if (voltage>24.0) { // enforce upper voltage limit (diodes limit is 30V, ADC input limit is 25V)
355  voltage = 24.0;
356  }
357  volt = busvoodoo_vreg_get(BUSVOODOO_3V3_CHANNEL); // get reference voltage
358  if (isnan(voltage)) {
359  return NAN;
360  }
361  uint16_t dac_set = BUSVOODOO_HV_SET(voltage)/volt*4095; // DAC value corresponding to the voltage
362  dac_load_data_buffer_single(dac_set, RIGHT12, BUSVOODOO_HVCTL_CHANNEL); // set output so the voltage regulator is set to desired output voltage
363  dac_software_trigger(BUSVOODOO_HVCTL_CHANNEL); // transfer the value to the DAC
364  dac_enable(BUSVOODOO_HVCTL_CHANNEL); // enable DAC
365  gpio_clear(GPIO(BUSVOODOO_HVEN_PORT), GPIO(BUSVOODOO_HVEN_PIN)); // enable HV voltage regulator
366  }
367  sleep_ms(100); // let the voltage regulator start and voltage settle
368  volt = busvoodoo_vreg_get(BUSVOODOO_HV_CHANNEL); // get HV voltage
369 
370  return volt; // return measured voltage
371 }
372 
374 {
375  if (on) { // enable embedded pull-ups
376  gpio_clear(GPIO(BUSVOODOO_OEPULLUP_PORT), GPIO(BUSVOODOO_OEPULLUP_PIN)); // switch on embedded pull-ups
377  } else { // disable embedded pull-ups
378  gpio_set(GPIO(BUSVOODOO_OEPULLUP_PORT), GPIO(BUSVOODOO_OEPULLUP_PIN)); // switch off embedded pull-up
379  }
380  return busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL); // set voltage on adjustable voltage regulator to be used by the embedded pull-ups
381 }
382 
384 static void busvoodoo_leds_update(void)
385 {
386  // handle LED timer
387  if (busvoodoo_global_led_blue_timer && 0==busvoodoo_global_led_blue_timeout) { // timer reached timeout
388  busvoodoo_global_led_blue_timer = false; // timer it not required anymore
389  }
390  if (busvoodoo_global_led_red_timer && 0==busvoodoo_global_led_red_timeout) { // timer reached timeout
391  busvoodoo_global_led_red_timer = false; // timer it not required anymore
392  }
393  if (!busvoodoo_global_led_blue_timer && !busvoodoo_global_led_red_timer) { // timer is not needed anymore
394  timer_disable_counter(TIM(BUSVOODOO_LED_TIMER)); // disable timer
395  timer_disable_irq(TIM(BUSVOODOO_LED_TIMER), TIM_DIER_UIE); // disable overflow interrupt used as tick
396  }
397  // drive right LED
398  if (busvoodoo_global_led_blue_timeout>0 && busvoodoo_global_led_red_timeout>0) { // both LEDs should be on
399  timer_set_period(TIM(BUSVOODOO_LED_TIMER), (rcc_ahb_frequency/3296)/1000); // ensure timer is set period to 1 ms for pulsing
400  timer_set_oc_value(TIM(BUSVOODOO_LED_TIMER), TIM_OC1, rcc_ahb_frequency/3296/1000/2); // set 50% PWM duty cycle
401  timer_enable_oc_output(TIM(BUSVOODOO_LED_TIMER), TIM_OC1); // enable PWM output
402  gpio_set_mode(GPIO(LED_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO(LED_PIN)); // allow PWM to drive pin
403  timer_enable_counter(TIM(BUSVOODOO_LED_TIMER)); // ensure the timer is enabled (interrupt should be disabled if not required)
404  } else if (busvoodoo_global_led_blue_timeout>0) { // only blue LED should be on
405  timer_disable_oc_output(TIM(BUSVOODOO_LED_TIMER), TIM_OC1); // disable PWM output
406  gpio_set(GPIO(LED_PORT), GPIO(LED_PIN)); // switch only blue LED on
407  gpio_set_mode(GPIO(LED_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(LED_PIN)); // set LED pin as push-pull to drive either LED
408  } else if (busvoodoo_global_led_red_timeout>0) { // only red LED should be on
409  timer_disable_oc_output(TIM(BUSVOODOO_LED_TIMER), TIM_OC1); // disable PWM output
410  gpio_clear(GPIO(LED_PORT), GPIO(LED_PIN)); // switch only red LED on
411  gpio_set_mode(GPIO(LED_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(LED_PIN)); // set LED pin as push-pull to drive either LED
412  } else { // no LED should be on
413  timer_disable_oc_output(TIM(BUSVOODOO_LED_TIMER), TIM_OC1); // disable PWM output
414  gpio_set_mode(GPIO(LED_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(LED_PIN)); // set LED pin to floating to disable both LEDs
415  }
416  busvoodoo_global_led_blinking = false; // setting the LEDs forced blinking mode exit
417 }
418 
420 {
421  busvoodoo_global_led_blue_timer = false; // there it no timeout of this LED
422  busvoodoo_global_led_blue_timeout = 1; // still enable LED
423  busvoodoo_leds_update(); // update LED status
424 }
425 
427 {
428  busvoodoo_global_led_blue_timer = false; // there it no timeout of this LED
429  busvoodoo_global_led_blue_timeout = 0; // disable LED
430  busvoodoo_leds_update(); // update LED status
431 }
432 
434 {
435  busvoodoo_global_led_red_timer = false; // there it no timeout of this LED
436  busvoodoo_global_led_red_timeout = 1; // still enable LED
437  busvoodoo_leds_update(); // update LED status
438 }
439 
441 {
442  busvoodoo_global_led_red_timer = false; // there it no timeout of this LED
443  busvoodoo_global_led_red_timeout = 0; // disable LED
444  busvoodoo_leds_update(); // update LED status
445 }
446 
448 {
449  busvoodoo_global_led_blue_timer = false; // there it no timeout of this LED
450  busvoodoo_global_led_blue_timeout = 0; // disable LED
451  busvoodoo_global_led_red_timer = false; // there it no timeout of this LED
452  busvoodoo_global_led_red_timeout = 0; // disable LED
453  busvoodoo_leds_update(); // update LED status
454 }
455 
457 static void busvoodoo_led_pulse_setup(void)
458 {
459  if (!busvoodoo_global_led_blinking) { // we are in the blink mode, reconfigure to pulse mode
460  timer_disable_oc_output(TIM(BUSVOODOO_LED_TIMER), TIM_OC1); // disable PWM output
461  timer_set_period(TIM(BUSVOODOO_LED_TIMER), (rcc_ahb_frequency/3296)/1000); // set period to 1 ms for pulsing
462  busvoodoo_global_led_blinking = false; // remember we quite the PWM/blinking mode
463  }
464  timer_enable_irq(TIM(BUSVOODOO_LED_TIMER), TIM_DIER_UIE); // enable overflow interrupt used as tick
465  timer_enable_counter(TIM(BUSVOODOO_LED_TIMER)); // ensure the timer is enabled
466 }
467 
468 void busvoodoo_led_blue_pulse(uint32_t ms)
469 {
470  if (0==ms) { // disable LED
471  busvoodoo_global_led_blue_timer = false; // no need to use the timer
472  busvoodoo_global_led_blue_timeout = 0; // disable blue LED
473  } else {
474  busvoodoo_global_led_blue_timer = true; // use the timer
475  busvoodoo_global_led_blue_timeout = ms; // disable blue LED
476  busvoodoo_led_pulse_setup(); // start timer
477  }
478  busvoodoo_leds_update(); // update LED status
479 }
480 
481 void busvoodoo_led_red_pulse(uint32_t ms)
482 {
483  if (0==ms) { // disable LED
484  busvoodoo_global_led_red_timer = false; // no need to use the timer
485  busvoodoo_global_led_red_timeout = 0; // disable blue LED
486  } else {
487  busvoodoo_global_led_red_timer = true; // use the timer
488  busvoodoo_global_led_red_timeout = ms; // disable blue LED
489  busvoodoo_led_pulse_setup(); // start timer
490  }
491  busvoodoo_leds_update(); // update LED status
492 }
493 
495 void tim1_up_isr(void)
496 {
497  if (timer_get_flag(TIM(BUSVOODOO_LED_TIMER), TIM_SR_UIF)) { // tick occurred
498  timer_clear_flag(TIM(BUSVOODOO_LED_TIMER), TIM_SR_UIF); // clear flag
499  if (busvoodoo_global_led_blue_timer && busvoodoo_global_led_blue_timeout>0) { // timeout for blue LED is running
500  busvoodoo_global_led_blue_timeout--; // decrement remaining timeout
501  }
502  if (busvoodoo_global_led_red_timer && busvoodoo_global_led_red_timeout>0) { // timeout for red LED is running
503  busvoodoo_global_led_red_timeout--; // decrement remaining timeout
504  }
505  if (0==busvoodoo_global_led_blue_timeout || 0==busvoodoo_global_led_red_timeout) { // a timeout occured
506  busvoodoo_leds_update(); // update LED status
507  }
508  }
509 }
510 
511 void busvoodoo_leds_blink(double period, double duty)
512 {
513  if (period<0.0 || period>6.0 || duty<0.0 || duty>1.0) { // input argument out of bounds
514  return; // do nothing
515  }
516  timer_disable_counter(TIM(BUSVOODOO_LED_TIMER)); // disable timer while reconfiguring
517  timer_disable_irq(TIM(BUSVOODOO_LED_TIMER), TIM_DIER_UIE); // disable overflow interrupt used for pulsing
518  if (busvoodoo_global_led_blue_timer) { // switch off LED when pulsing
519  busvoodoo_global_led_blue_timer = false; // stop pulse
520  busvoodoo_global_led_blue_timeout = 0; // switch off
521  }
522  if (busvoodoo_global_led_blue_timer) { // switch off LED when pulsing
523  busvoodoo_global_led_blue_timer = false; // stop pulse
524  busvoodoo_global_led_blue_timeout = 0; // switch off
525  }
526  if (0.0==period) { // no blinking
527  if (duty>=0.5) { // only enable blue LED
529  } else { // only enable red LED
531  }
533  } else {
534  gpio_set_mode(GPIO(LED_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO(LED_PIN)); // set LED pin to alternate function for PWM
535  timer_set_counter(TIM(BUSVOODOO_LED_TIMER), 0); // reset counter
536  timer_set_period(TIM(BUSVOODOO_LED_TIMER), 0xffff*(period/6.0)); // set period
537  timer_set_oc_value(TIM(BUSVOODOO_LED_TIMER), TIM_OC1, 0xffff*(period/6.0)*duty); // PWM duty cycle
538  timer_enable_counter(TIM(BUSVOODOO_LED_TIMER)); // enable timer to start blinking
539  }
540 }
541 
546 {
547  bool power_led_on = false; // to calculate the final state of the power LED
548  power_led_on |= !gpio_get(GPIO(BUSVOODOO_VOUTEN_PORT), GPIO(BUSVOODOO_VOUTEN_PIN)); // check power rails output
549  power_led_on |= !gpio_get(GPIO(BUSVOODOO_5VPULLUP_PORT), GPIO(BUSVOODOO_5VPULLUP_PIN)); // check 5V output on LV pin
550  power_led_on |= gpio_get(GPIO(BUSVOODOO_LVEN_PORT), GPIO(BUSVOODOO_LVEN_PIN)); // check if low-voltage regulator is on
551  if (!busvoodoo_full) {
552  power_led_on |= !gpio_get(GPIO(BUSVOODOO_HVEN_PORT), GPIO(BUSVOODOO_HVEN_PIN)); // check if high-voltage regulator is on
553  }
554  if (power_led_on) {
555  busvoodoo_led_red_on(); // switch on red LED to indicate one of the power output is on
556  } else {
557  busvoodoo_led_red_off(); // switch off red LED to indicate no power output is on
558  }
559 }
560 
561 bool busvoodoo_global_actions(char* actions, bool perform, bool (*action_handler)(const char* action, uint32_t repetition, bool perform))
562 {
563  char* action_start = actions; // start of the current action
564  bool last_action = false; // is the current action the last one
565  while ('\0'!=*action_start && !last_action) {
566  // find end of action
567  char* action_end = action_start+1;
568  if ('"'==*action_start || '\''==*action_start) { // start of string
569  while ('\0'!=*action_end && *action_end!=*action_start) {
570  action_end++;
571  }
572  if (*action_end!=*action_start) { // action not ended correctly
573  return false;
574  }
575  action_end++; // go the end of action
576  } else { // just look for a separation
577  while ('\0'!=*action_end && ':'!=*action_end && ' '!=*action_end && ','!=*action_end) {
578  action_end++;
579  }
580  }
581  // find start of next action
582  char *separation = action_end; // position of separation to next action
583  while ('\0'!=*separation && ' '!=*separation && ','!=*separation) { // find separation or end
584  separation++;
585  }
586  if ('\0'==*separation) {
587  last_action = true; // remember we reached the end of the string
588  } else {
589  *separation = '\0'; // end the action to form a string
590  }
591  // get multiplier
592  uint32_t multiplier = 1; // the number of times the action should be performed
593  if (separation>action_end) { // there is something after the action
594  if (':'==*action_end) { // multiplier sign found
595  if (separation==action_end+1) { // no digit present
596  return false; // malformed action
597  }
598  for (char* digit=action_end+1; digit<separation; digit++) { // check if all the characters are digits
599  if (*digit<'0' || *digit>'9') { // not a digit
600  return false; // malformed string
601  }
602  }
603  multiplier = strtol(action_end+1, NULL, 10); // parse multiplier number
604  } else { // unknown sign after action
605  return false; // malformed action
606  }
607  }
608  // perform action
609  *action_end = '\0'; // end action string
610  if (!(*action_handler)(action_start, multiplier, perform)) { // perform action
611  return false; // action if malformed
612  }
613  // go to next action
614  if (!last_action) {
615  action_start = separation+1;
616  }
617  }
618  return true; // all went well
619 }
620 
621 /* command handlers */
622 
627 {
628  float voltage;
629  if (NULL==argument || 0==strlen(argument)) {
630  if (gpio_get(GPIO(BUSVOODOO_VOUTEN_PORT), GPIO(BUSVOODOO_VOUTEN_PIN))) { // check if power rails are switch on (enable low)
631  goto power_off;
632  } else {
633  goto power_on;
634  }
635  } else if (0==strcmp(argument, "on")) {
636  if (busvoodoo_vout_switch(true)) { // switch power rail on
637  printf("power rails switched on\n");
638  } else {
639  printf("power rails switched on but malfunctioning\n");
640  }
641 power_on:
642  voltage = busvoodoo_vreg_get(BUSVOODOO_5V_CHANNEL); // get 5V power rail voltage
643  printf("5V power rail: %.2fV\n", voltage);
644  voltage = busvoodoo_vreg_get(BUSVOODOO_3V3_CHANNEL); // get 3.3V power rail voltage
645  printf("3V3 power rail: %.2fV\n", voltage);
646  } else if (0==strcmp(argument, "off")) {
647  busvoodoo_vout_switch(false); // switch power rail off
648  printf("power rails switched off\n");
649 power_off:
650  printf("5V power rail: off\n");
651  printf("3V3 power rail: off\n");
652  } else {
653  printf("option malformed: %s\n", argument);
654  }
655  busvoodoo_global_power_led_update(); // update power output LED
656 }
657 
661 static void busvoodoo_global_lv(void* argument)
662 {
663  if (NULL==argument) {
664  if (!gpio_get(GPIO(BUSVOODOO_5VPULLUP_PORT), GPIO(BUSVOODOO_5VPULLUP_PIN))) { // 5V input enabled
665  printf("5V power rail used");
666  } else if (gpio_get(GPIO(BUSVOODOO_LVEN_PORT), GPIO(BUSVOODOO_LVEN_PIN))) { // LV voltage regulator used
667  printf("adjustable voltage regulator used");
668  } else {
669  printf("external voltage input");
670  }
671  float voltage = busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL); // get LV voltage
672  // print LV voltage
673  if (voltage < 0.1) {
674  printf(": 0.00V\n");
675  } else {
676  printf(": %.2fV\n", voltage);
677  }
678  } else {
679  double voltage = *((double*)argument); // get desired voltage
680  if (0==voltage) {
681  printf("LV rail switched off");
682  } else {
683  printf("LV rail set to %.2fV", voltage);
684  }
685  voltage = busvoodoo_lv_set(voltage); // set LV voltage
686  // print LV voltage
687  if (voltage < 0.1) {
688  printf(": 0.00V\n");
689  } else {
690  printf(": %.2fV\n", voltage);
691  }
692  }
693  busvoodoo_global_power_led_update(); // update power output LED
694 }
695 
699 static void busvoodoo_global_hv(void* argument)
700 {
701  if (!busvoodoo_full) {
702  printf("function not available on BusVoodoo light");
703  return;
704  }
705  if (NULL==argument) {
706  printf("high voltage regulator switched %s: ", gpio_get(GPIO(BUSVOODOO_HVEN_PORT), GPIO(BUSVOODOO_HVEN_PIN)) ? "off" : "on");
707  float voltage = busvoodoo_vreg_get(BUSVOODOO_HV_CHANNEL); // get HV voltage
708  // print LV voltage
709  if (voltage < 0.1) {
710  printf("0.00V\n");
711  } else {
712  printf("%.2fV\n", voltage);
713  }
714  } else {
715  double voltage = *((double*)argument); // get desired voltage
716  printf("high voltage rail ");
717  if (voltage<=3.3) {
718  printf("switched off");
719  } else {
720  printf("set to %.2fV", voltage);
721  }
722  voltage = busvoodoo_hv_set(voltage); // set HV voltage
723  // print HV voltage
724  if (voltage < 0.1) {
725  printf(": 0.00V\n");
726  } else {
727  printf(": %.2fV\n", voltage);
728  }
729  }
730  busvoodoo_global_power_led_update(); // update power output LED
731 }
732 
737 {
738  (void)argument; // argument is not used
739  bool no_pinout = true; // it no pinout has been displays
740 
741  // display RS/CAN connector pinout
742  if (busvoodoo_full) { // only display on full version of the board
743  bool pin_used = false; // if no pins are used
744  for (uint8_t i=0; i<LENGTH(busvoodoo_global_pinout_rscan); i++) { // verify if one of the pins is used
745  pin_used |= (NULL!=busvoodoo_global_pinout_rscan[i]); // verify if pin is used
746  }
747  if (pin_used) {
748  // count the space used to display the pins
749  uint8_t space = 0; // no integer overflow protected (it's only cosmetic)
750  for (uint8_t i=0; i<LENGTH(busvoodoo_global_pinout_rscan); i++) {
751  if (NULL==busvoodoo_global_pinout_rscan[i]) {
752  space += 1; // only x will be shown
753  } else {
754  space += strlen(busvoodoo_global_pinout_rscan[i]); // add size of pin name
755  }
756  }
757  space += LENGTH(busvoodoo_global_pinout_rscan)-1; // add the spaces between the names
758  // display pinout
759  printf("RS/CAN connector pinout:\n");
760  // display top line
761  printf("+");
762  for (uint8_t i=0; i<space; i++) {
763  printf("-");
764  }
765  printf("+\n");
766  // display pin names
767  printf("|");
768  for (int8_t i=LENGTH(busvoodoo_global_pinout_rscan)-1; i>=0; i--) {
769  if (NULL==busvoodoo_global_pinout_rscan[i]) {
770  printf("x"); // x stands for pin not used
771  } else {
772  printf("%s", busvoodoo_global_pinout_rscan[i]); // print pin name
773  }
774  if (i>0) {
775  printf(" "); // print space between the pin names
776  }
777  }
778  printf("|\n");
779  // display bottom line
780  printf("+");
781  for (uint8_t i=0; i<space; i++) {
782  printf("-");
783  }
784  printf("+\n");
785  no_pinout = false; // remember a pinout has been shown
786  }
787  }
788 
789  // display I/O connector pinout
790  bool pin_used = false; // if no pins are used
791  for (uint8_t i=0; i<LENGTH(busvoodoo_global_pinout_io); i++) { // verify if one of the pins is used
792  pin_used |= (NULL!=busvoodoo_global_pinout_io[i]); // verify if pin is used
793  }
794  if (pin_used) {
795  // count the space used to display the pins (no integer overflow protected, it's only cosmetic)
796  uint8_t spaces[5] = {0}; // maximum spaces used by top and bottom pins
797  for (uint8_t i=0; i<LENGTH(spaces); i++) {
798  if (NULL==busvoodoo_global_pinout_io[i*2]) {
799  spaces[i] = 1; // only x will be shown
800  } else {
801  spaces[i] = strlen(busvoodoo_global_pinout_io[i*2]); // remember size of pin name
802  }
803  if (NULL==busvoodoo_global_pinout_io[i*2+1]) {
804  if (spaces[i]<1) {
805  spaces[i] = 1; // only x will be shown
806  }
807  } else {
808  if (spaces[i]<strlen(busvoodoo_global_pinout_io[i*2+1])) {
809  spaces[i] = strlen(busvoodoo_global_pinout_io[i*2+1]); // remember bigger size of pin name
810  }
811  }
812  }
813  // display pinout
814  printf("I/O connector pinout:\n");
815  // display top line
816  printf("+");
817  for (uint16_t i=0; i<(uint16_t)(spaces[4]+spaces[3]+1); i++) {
818  printf("-");
819  }
820  for (uint16_t i=0; i<(uint16_t)(spaces[2]+2); i++) {
821  printf(" ");
822  }
823  for (uint16_t i=0; i<(uint16_t)(spaces[1]+spaces[0]+1); i++) {
824  printf("-");
825  }
826  printf("+\n");
827  // display top pin names
828  printf("|");
829  for (int8_t i=4; i>=0; i--) {
830  if (NULL==busvoodoo_global_pinout_io[i*2]) {
831  printf("x"); // x stands for pin not used
832  for (int16_t j=0; j<spaces[i]-1; j++) {
833  printf(" "); // pad to match to bottom pin
834  }
835  } else {
836  printf("%s", busvoodoo_global_pinout_io[i*2]); // print pin name
837  for (int16_t j=0; j+strlen(busvoodoo_global_pinout_io[i*2])<spaces[i]; j++) {
838  printf(" "); // pad to match to bottom pin
839  }
840  }
841  if (i>0) {
842  printf(" "); // print space between the pin names
843  }
844  }
845  printf("|\n");
846  // display bottom pin names
847  printf("|");
848  for (int8_t i=4; i>=0; i--) {
849  if (NULL==busvoodoo_global_pinout_io[i*2+1]) {
850  printf("x"); // x stands for pin not used
851  for (int16_t j=0; j<spaces[i]-1; j++) {
852  printf(" "); // pad to match to bottom pin
853  }
854  } else {
855  printf("%s", busvoodoo_global_pinout_io[i*2+1]); // print pin name
856  for (int16_t j=0; j+strlen(busvoodoo_global_pinout_io[i*2+1])<spaces[i]; j++) {
857  printf(" "); // pad to match to bottom pin
858  }
859  }
860  if (i>0) {
861  printf(" "); // print space between the pin names
862  }
863  }
864  printf("|\n");
865  // display bottom line
866  printf("+");
867  for (uint16_t i=0; i<spaces[4]+1+spaces[3]+1+spaces[2]+1+spaces[1]+1+spaces[0]; i++) {
868  printf("-");
869  }
870  printf("+\n");
871  no_pinout = false; // remember a pinout has been shown
872  }
873 
874  // in case nothing has been displayed
875  if (no_pinout) {
876  printf("no pins are used\n");
877  }
878 }
879 
881  {
882  .shortcut = 'p',
883  .name = "pinout",
884  .command_description = "show connector pinout",
885  .argument = MENU_ARGUMENT_NONE,
886  .argument_description = NULL,
887  .command_handler = &busvoodoo_global_pinout,
888  },
889  {
890  .shortcut = 'P',
891  .name = "power",
892  .command_description = "switch 3V3 and 5V power rails on/off, or read internal voltages",
893  .argument = MENU_ARGUMENT_STRING,
894  .argument_description = "[on|off]",
895  .command_handler = &busvoodoo_global_power,
896  },
897  {
898  .shortcut = 'L',
899  .name = "LV",
900  .command_description = "set voltage on low voltage power rail (0, 0.3-4.8, 5V), or read voltage on pin",
901  .argument = MENU_ARGUMENT_FLOAT,
902  .argument_description = "[voltage]",
903  .command_handler = &busvoodoo_global_lv,
904  },
905 };
906 
908 
910  {
911  .shortcut = 'H',
912  .name = "HV",
913  .command_description = "set voltage on high voltage power rail (0, 3.3-24V), or read voltage on pin",
914  .argument = MENU_ARGUMENT_FLOAT,
915  .argument_description = "[voltage]",
916  .command_handler = &busvoodoo_global_hv,
917  },
918 };
919 
void busvoodoo_leds_off(void)
switch off blue and red LEDs
#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
static void busvoodoo_global_power_led_update(void)
updates the red power LED status
#define BUSVOODOO_HVCTL_CHANNEL
DAC channel to control HV output voltage.
command menu entry
Definition: menu.h:31
const struct menu_command_t busvoodoo_global_commands[]
list of supported commands for base BusVoodoo
static void busvoodoo_global_power(void *argument)
switch 3V3 and 5V power rails on/off
BusVoodoo global definitions and methods (API)
#define BUSVOODOO_HVEN_PIN
high voltage (HV) enable pin (active low)
#define BUSVOODOO_HW_VERSION_CHANNEL
ADC to identify hardware version.
static void busvoodoo_global_lv(void *argument)
set LV linear drop-out voltage regulator voltage
static volatile bool busvoodoo_global_led_blinking
if the LEDs are in a blinking pattern
void busvoodoo_led_blue_pulse(uint32_t ms)
pulse blue LED for short duration
#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_HW_VERSION_PORT
pin to identify hardware version
static void busvoodoo_global_hv(void *argument)
set HV step-up voltage regulator voltage
#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...
#define BUSVOODOO_HVCTL_PORT
pin to control HV output voltage
#define BUSVOODOO_HVEN_PORT
high voltage (HV) enable pin (active low)
#define BUSVOODOO_RS232_CTS_PORT
RS-232 Clear-To-Send input pin.
float busvoodoo_lv_set(float voltage)
set voltage on low voltage adjustable voltage regulator
#define BUSVOODOO_LVEN_PIN
low voltage (LV) enable pin (active high)
const char * busvoodoo_global_pinout_rscan[5]
RS/CAN connector pinout.
static volatile uint32_t busvoodoo_global_led_red_timeout
number of remaining milliseconds the red LED should stay on
#define BUSVOODOO_LV_SET(x)
voltage to output for the DAC to set the desired LV output voltage (based on resistor values on the L...
const uint32_t busvoodoo_io_pins[13]
pin of individual signals
#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
#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:102
#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)
#define RCC_TIM(x)
get RCC for timer based on TIM identifier
Definition: global.h:109
const uint32_t busvoodoo_io_ports[13]
port of individual signals
#define BUSVOODOO_RS485_TX_PORT
RS-485 Transmit output pin.
void busvoodoo_led_blue_off(void)
switch off blue LED
#define BUSVOODOO_OEPULLUP_PIN
bus switch output enable pin to enable embedded pull-ups (active low)
#define ADC12_IN_PORT(x)
get port based on ADC12_IN identifier
Definition: global.h:215
#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
char shortcut
short command code (0 if not available)
Definition: menu.h:32
#define ADC12_IN_PIN(x)
get pin based on ADC12_IN identifier
Definition: global.h:233
void busvoodoo_leds_blink(double period, double duty)
set LED blinking pattern
void busvoodoo_led_blue_on(void)
switch on blue LED
#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
#define BUSVOODOO_RS232_EN_PIN
RS-232 pin to enable receiver (active low, pulled up)
float busvoodoo_vreg_get(uint8_t channel)
read voltage from power rail
const char * busvoodoo_io_names[13]
I/O individual signal names.
char busvoodoo_global_string[64]
shared string buffer, i.e.
#define BUSVOODOO_5VPULLUP_PIN
5V pull-up enable pin (active low)
const uint8_t busvoodoo_global_commands_nb
number supported commands for base BusVoodoo
#define BUSVOODOO_VOUTEN_PORT
voltage output (5V and 3.3V) enable pin (active low)
bool busvoodoo_global_actions(char *actions, bool perform, bool(*action_handler)(const char *action, uint32_t repetition, bool perform))
parse and perform actions
void sleep_ms(uint32_t duration)
go to sleep for some milliseconds
Definition: global.c:117
static void busvoodoo_leds_update(void)
update LED status according to LED flags
#define BUSVOODOO_VOUTEN_PIN
voltage output (5V and 3.3V) enable pin (active low)
const uint8_t busvoodoo_global_full_commands_nb
number supported commands for BusVoodoo full only
#define BUSVOODOO_LVCTL_CHANNEL
DAC channel to control LV output voltage.
#define BUSVOODOO_HW_VERSION_PIN
pin to identify hardware version
#define BUSVOODOO_RS232_RX_PORT
RS-232 Receive input pin.
void busvoodoo_led_red_pulse(uint32_t ms)
pulse red LED for short duration
#define BUSVOODOO_RS485_DE_PIN
RS-485 pin to enable transmitter (active high, pulled low)
void tim1_up_isr(void)
interrupt service routine called on LED timeout
static volatile bool busvoodoo_global_led_red_timer
if the timer for the red LED is enabled
#define BUSVOODOO_LED_TIMER
timer peripheral ID
#define BUSVOODOO_LVEN_PORT
low voltage (LV) enable pin (active high)
float busvoodoo_embedded_pullup(bool on)
enable embedded pull-up resistors
void busvoodoo_led_red_off(void)
switch off red LED
#define BUSVOODOO_RS232_RTS_PORT
RS-232 Request-To-Send output pin.
static volatile bool busvoodoo_global_led_blue_timer
if the timer for the blue LED is enabled
static void busvoodoo_global_pinout(void *argument)
display I/O and RS/CAN connector pinouts
#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 ADC_CHANNEL(x)
get channel based on ADC12_IN identifier
Definition: global.h:269
#define BUSVOODOO_OEPULLUP_PORT
bus switch output enable pin to enable embedded pull-ups (active low)
#define BUSVOODOO_5VPULLUP_PORT
5V pull-up enable pin (active low)
void busvoodoo_led_red_on(void)
switch on red LED
#define BUSVOODOO_RS232_RX_PIN
RS-232 Receive input pin.
#define TIM(x)
get TIM based on TIM identifier
Definition: global.h:107
#define BUSVOODOO_RS485_RX_PIN
RS-485 Receive input pin.
const struct menu_command_t busvoodoo_global_full_commands[]
list of supported commands for BusVoodoo full only
static const float busvoodoo_version_voltages[]
hardware version voltages, calculated from divider ratios, starting with version A ...
#define BUSVOODOO_RS485_TX_PIN
RS-485 Transmit output pin.
static void busvoodoo_led_pulse_setup(void)
setup the timer for pulsing LEDs
void busvoodoo_safe_state(void)
set safe state by disabling all outputs
char busvoodoo_version
version of the hardware board
void busvoodoo_setup(void)
setup BusVoodoo board
static volatile uint32_t busvoodoo_global_led_blue_timeout
number of remaining milliseconds the blue LED should stay on
#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 BUSVOODOO_HVCTL_PIN
pin to control HV output voltage
#define RCC_ADC12_IN(x)
get RCC based on ADC12_IN identifier
Definition: global.h:251