busvoodoo_global.c
Go to the documentation of this file.
/* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* standard libraries */
#include <stdint.h> // standard integer types
#include <stdlib.h> // standard utilities
#include <string.h> // string utilities
#include <math.h> // math utilities
/* STM32 (including CM3) libraries */
#include <libopencm3/cm3/nvic.h> // interrupt handler
#include <libopencm3/stm32/gpio.h> // general purpose input output library
#include <libopencm3/stm32/rcc.h> // real-time control clock library
#include <libopencm3/stm32/adc.h> // ADC utilities
#include <libopencm3/stm32/dac.h> // DAC utilities
#include <libopencm3/stm32/timer.h> // timer utilities
/* own libraries */
#include "global.h" // board definitions
#include "menu.h" // command definitions
#include "print.h" // print utilities
#include "busvoodoo_global.h" // BusVoodoo definitions
#define BUSVOODOO_LED_TIMER 1
static volatile uint32_t busvoodoo_global_led_blue_timeout = 0;
static volatile bool busvoodoo_global_led_blue_timer = false;
static volatile uint32_t busvoodoo_global_led_red_timeout = 0;
static volatile bool busvoodoo_global_led_red_timer = false;
static volatile bool busvoodoo_global_led_blinking = false;
static const float busvoodoo_version_voltages[] = {100.0/(10.0+100.0)*3.3}; // version A start with revision 27
const char* busvoodoo_global_pinout_io[10] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
const char* busvoodoo_global_pinout_rscan[5] = {NULL, NULL, NULL, NULL, NULL};
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"};
const uint32_t busvoodoo_io_ports[13] = {GPIOB, GPIOD, GPIOB, GPIOC, GPIOB, GPIOC, GPIOB, GPIOC, GPIOB, GPIOC, GPIOC, GPIOB, GPIOC};
const uint32_t busvoodoo_io_pins[13] = {GPIO12, GPIO2, GPIO13, GPIO11, GPIO11, GPIO8, GPIO15, GPIO12, GPIO10, GPIO9, GPIO6, GPIO14, GPIO10};
const uint8_t busvoodoo_io_groups[13] = {6, 6, 4, 4, 1, 1, 5, 5, 2, 2, 3, 3, 3};
bool busvoodoo_full = false;
char busvoodoo_version = '0';
char busvoodoo_global_string[64];
void busvoodoo_setup(void)
{
// enable all GPIO domains since we use pins on all ports
rcc_periph_clock_enable(RCC_GPIOA); // enable clock for all GPIO domains
rcc_periph_clock_enable(RCC_GPIOB); // enable clock for all GPIO domains
rcc_periph_clock_enable(RCC_GPIOC); // enable clock for all GPIO domains
rcc_periph_clock_enable(RCC_GPIOD); // enable clock for all GPIO domains
rcc_periph_clock_enable(RCC_AFIO); // enable clock for alternate function (for communication)
busvoodoo_safe_state(); // put pins in safe state (for common light version)
// check if this BusVoodoo is a full flavor
rcc_periph_clock_enable(RCC_ADC12_IN(BUSVOODOO_HV_CHANNEL)); // enable clock for GPIO domain for HV channel
gpio_set(ADC12_IN_PORT(BUSVOODOO_HV_CHANNEL), ADC12_IN_PIN(BUSVOODOO_HV_CHANNEL)); // pull ADC HV high
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
// on a full version (fully populated board) the ADC HV signal will be pulled low
if (gpio_get(ADC12_IN_PORT(BUSVOODOO_HV_CHANNEL), ADC12_IN_PIN(BUSVOODOO_HV_CHANNEL))) { // check is ADC HV is pulled low
busvoodoo_full = false;
} else {
busvoodoo_full = true;
busvoodoo_safe_state(); // also put the full version pins in safe state
}
// setup ADC to measure the 5V, 3.3V, LV, and HV power rails voltages, and hardware version channel
rcc_periph_clock_enable(RCC_ADC12_IN(BUSVOODOO_5V_CHANNEL)); // enable clock for GPIO domain for 5V channel
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
rcc_periph_clock_enable(RCC_ADC12_IN(BUSVOODOO_3V3_CHANNEL)); // enable clock for GPIO domain for 3.3V channel
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
rcc_periph_clock_enable(RCC_ADC12_IN(BUSVOODOO_LV_CHANNEL)); // enable clock for GPIO domain for LV channel
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
rcc_periph_clock_enable(RCC_ADC12_IN(BUSVOODOO_HV_CHANNEL)); // enable clock for GPIO domain for HV channel
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
rcc_periph_clock_enable(RCC_ADC12_IN(BUSVOODOO_HW_VERSION_CHANNEL)); // enable clock for GPIO domain for hardware version channel
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
rcc_periph_clock_enable(RCC_ADC1); // enable clock for ADC domain
adc_power_off(ADC1); // switch off ADC while configuring it
adc_disable_scan_mode(ADC1); // ensure scan mode is disabled
adc_disable_discontinuous_mode_regular(ADC1); // ensure discontinuous mode is not used
adc_set_single_conversion_mode(ADC1); // ensure continuous mode is not used (that's not the same as discontinuous)
adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_28DOT5CYC); // use 28.5 cycles to sample (long enough to be stable)
adc_enable_temperature_sensor(); // enable internal voltage reference
adc_power_on(ADC1); // switch on ADC
sleep_us(1); // wait t_stab for the ADC to stabilize
adc_reset_calibration(ADC1); // remove previous non-calibration
adc_calibrate(ADC1); // calibrate ADC for less accuracy errors
// find out version of the board
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
gpio_set(GPIO(BUSVOODOO_HW_VERSION_PORT), GPIO(BUSVOODOO_HW_VERSION_PIN)); // pull up
bool version_up = (0!=gpio_get(GPIO(BUSVOODOO_HW_VERSION_PORT), GPIO(BUSVOODOO_HW_VERSION_PIN))); // check if the signal is still up
gpio_clear(GPIO(BUSVOODOO_HW_VERSION_PORT), GPIO(BUSVOODOO_HW_VERSION_PIN)); // pull down
bool version_down = (0==gpio_get(GPIO(BUSVOODOO_HW_VERSION_PORT), GPIO(BUSVOODOO_HW_VERSION_PIN))); // check if the signal is still down
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
// get version
if (version_up && version_down) { // no voltage divider on pin
busvoodoo_version = '0'; // the pin is floating only for version 0 (= revision 18)
} else { // voltage divider on pin
float version_voltage = busvoodoo_vreg_get(BUSVOODOO_HW_VERSION_CHANNEL); // measure hardware version voltage
for (uint8_t i=0; i<LENGTH(busvoodoo_version_voltages); i++) { // go through expected version voltages
if (version_voltage>busvoodoo_version_voltages[i]-0.2 && version_voltage<busvoodoo_version_voltages[i]+0.2) { // verify if voltage matches
busvoodoo_version = 'A'+i; // remember version name for matching voltage
break; // stop searching
}
}
}
// setup DAC to control LV and HV voltage outputs
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)
rcc_periph_clock_enable(RCC_DAC); // enable clock for DAC domain
dac_disable(BUSVOODOO_LVCTL_CHANNEL); // disable output to configure it properly
dac_buffer_enable(BUSVOODOO_LVCTL_CHANNEL); // enable output buffer to be able to drive larger loads (should be per default)
if (busvoodoo_full) {
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)
dac_disable(BUSVOODOO_HVCTL_CHANNEL); // disable output to configure it properly
dac_buffer_enable(BUSVOODOO_HVCTL_CHANNEL); // enable output buffer to be able to drive larger loads (should be per default)
}
dac_set_trigger_source(DAC_CR_TSEL1_SW); // use software to trigger the voltage change
dac_set_trigger_source(DAC_CR_TSEL2_SW); // use software to trigger the voltage change
// enable timer for LED pulsing or blinking
rcc_periph_clock_enable(RCC_TIM(BUSVOODOO_LED_TIMER)); // enable clock for timer domain
timer_reset(TIM(BUSVOODOO_LED_TIMER)); // reset timer configuration
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
timer_set_prescaler(TIM(BUSVOODOO_LED_TIMER), 3296-1); // set prescaler to allow 3/3 seconds PWM output (72MHz/2^16/3296=0.33Hz)
timer_set_oc_mode(TIM(BUSVOODOO_LED_TIMER), TIM_OC1, TIM_OCM_PWM1); // use PWM output compare mode (for blinking)
timer_disable_oc_output(TIM(BUSVOODOO_LED_TIMER), TIM_OC1); // disable output compare output (for now)
timer_enable_break_main_output(TIM(BUSVOODOO_LED_TIMER)); // required to enable timer 1, even when no dead time is used
timer_set_period(TIM(BUSVOODOO_LED_TIMER), (rcc_ahb_frequency/3296)/1000); // set period to 1 ms for pulsing
nvic_enable_irq(NVIC_TIM1_UP_IRQ); // enable interrupt for timer 1 to catch update event when overflowing
// disable LEDs and reset state
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
busvoodoo_global_led_blue_timeout = 0; // no timeout needed
busvoodoo_global_led_blue_timer = false; // no timeout needed
busvoodoo_global_led_red_timeout = 0; // no timeout needed
busvoodoo_global_led_red_timer = false; // no timeout needed
busvoodoo_global_led_blinking = false; // start in pulse mode
}
void busvoodoo_safe_state(void)
{
// disable voltage outputs
gpio_set(GPIO(BUSVOODOO_VOUTEN_PORT), GPIO(BUSVOODOO_VOUTEN_PIN)); // disable 5V and 3.3V output on connector
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)
gpio_clear(GPIO(BUSVOODOO_LVEN_PORT), GPIO(BUSVOODOO_LVEN_PIN)); // disable LV voltage regulator
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)
gpio_set(GPIO(BUSVOODOO_HVEN_PORT), GPIO(BUSVOODOO_HVEN_PIN)); // disable HV voltage regulator
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)
// set DAC channel back to analog
gpio_set_mode(GPIO(BUSVOODOO_LVCTL_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, GPIO(BUSVOODOO_LVCTL_PIN)); // set LV pin as analog
gpio_set_mode(GPIO(BUSVOODOO_HVCTL_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, GPIO(BUSVOODOO_HVCTL_PIN)); // set HV pin as analog
// disable embedded pull-ups
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)
gpio_set(GPIO(BUSVOODOO_5VPULLUP_PORT), GPIO(BUSVOODOO_5VPULLUP_PIN)); // set pin high to disable 5V embedded pull-up
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)
gpio_set(GPIO(BUSVOODOO_OEPULLUP_PORT), GPIO(BUSVOODOO_OEPULLUP_PIN)); // set pin high to disable embedded pull-up bus switch
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)
// disable all signal I/O outputs
for (uint8_t pin=0; pin<LENGTH(busvoodoo_io_ports) && pin<LENGTH(busvoodoo_io_pins); pin++) {
gpio_set_mode(busvoodoo_io_ports[pin], GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, busvoodoo_io_pins[pin]); // set pin back to input (floating)
}
if (busvoodoo_full) {
// disable RS-232 signals
gpio_set_mode(GPIO(BUSVOODOO_RS232_TX_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_RS232_TX_PIN)); // set pin to floating
gpio_set_mode(GPIO(BUSVOODOO_RS232_RX_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_RS232_RX_PIN)); // set pin to floating
gpio_set_mode(GPIO(BUSVOODOO_RS232_RTS_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_RS232_RTS_PIN)); // set pin to floating
gpio_set_mode(GPIO(BUSVOODOO_RS232_CTS_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_RS232_CTS_PIN)); // set pin to floating
gpio_set(GPIO(BUSVOODOO_RS232_EN_PORT), GPIO(BUSVOODOO_RS232_EN_PIN)); // set high to disable receiver
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
gpio_clear(GPIO(BUSVOODOO_RS232_SHDN_PORT), GPIO(BUSVOODOO_RS232_SHDN_PIN)); // set low to disable transmitter
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
// disable RS-485 signals (RS and TX are shared with RS-232)
gpio_set(GPIO(BUSVOODOO_RS485_RE_PORT), GPIO(BUSVOODOO_RS485_RE_PIN)); // set high to disable receiver
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
gpio_clear(GPIO(BUSVOODOO_RS485_DE_PORT), GPIO(BUSVOODOO_RS485_DE_PIN)); // set low to disable transmitter
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
gpio_set_mode(GPIO(BUSVOODOO_RS485_TX_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_RS485_TX_PIN)); // set pin to floating
gpio_set_mode(GPIO(BUSVOODOO_RS485_RX_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_RS485_RX_PIN)); // set pin to floating
#if BUSVOODOO_HARDWARE_VERSION!=0
// disable CAN transceiver and signals (put back to input floating)
gpio_set_mode(GPIO(BUSVOODOO_CAN_TX_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_CAN_TX_PIN)); // set pin to floating
gpio_set_mode(GPIO(BUSVOODOO_CAN_RX_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_CAN_RX_PIN)); // set pin to floating
gpio_set(GPIO(BUSVOODOO_CAN_EN_PORT), GPIO(BUSVOODOO_CAN_EN_PIN)); // set high to power off transceiver
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
#endif
}
}
bool busvoodoo_vout_switch(bool on)
{
if (on) { // we need to switch on Vout
gpio_clear(GPIO(BUSVOODOO_VOUTEN_PORT), GPIO(BUSVOODOO_VOUTEN_PIN)); // enable Vout
} else {
gpio_set(GPIO(BUSVOODOO_VOUTEN_PORT), GPIO(BUSVOODOO_VOUTEN_PIN)); // disable Vout
}
bool to_return = true;
sleep_ms(1); // wait a bit for voltage to settle
float voltage = busvoodoo_vreg_get(BUSVOODOO_5V_CHANNEL); // get 5V power rail voltage
if (voltage<4.0 || voltage>5.5) {
to_return = false; // voltage output is not ok
}
voltage = busvoodoo_vreg_get(BUSVOODOO_3V3_CHANNEL); // get 3.3V power rail voltage
if (voltage<3.0 || voltage>3.6) {
to_return = true; // voltage output is not ok
}
return to_return;
}
float busvoodoo_vreg_get(uint8_t channel)
{
if (channel!=BUSVOODOO_5V_CHANNEL && channel!=BUSVOODOO_3V3_CHANNEL && channel!=BUSVOODOO_LV_CHANNEL && channel!=BUSVOODOO_HV_CHANNEL && channel!=BUSVOODOO_HW_VERSION_CHANNEL) { // check channel
return NAN;
}
// start by reading the internal voltage
uint8_t channels[1] = {ADC_CHANNEL17}; // voltages to convert: internal
adc_set_regular_sequence(ADC1, LENGTH(channels), channels); // set channel to convert
ADC_SR(ADC1) = 0; // reset flags
adc_start_conversion_direct(ADC1); // start conversion (without using trigger)
while (!adc_eoc(ADC1)); // wait until conversion finished
uint16_t internal_value = adc_read_regular(ADC1); // read voltage value (clears flag)
// read desired voltage
switch (channel) {
case BUSVOODOO_5V_CHANNEL:
channels[0] = ADC_CHANNEL(BUSVOODOO_5V_CHANNEL);
break;
case BUSVOODOO_3V3_CHANNEL:
channels[0] = ADC_CHANNEL(BUSVOODOO_3V3_CHANNEL);
break;
case BUSVOODOO_LV_CHANNEL:
channels[0] = ADC_CHANNEL(BUSVOODOO_LV_CHANNEL);
break;
case BUSVOODOO_HV_CHANNEL:
channels[0] = ADC_CHANNEL(BUSVOODOO_HV_CHANNEL);
break;
case BUSVOODOO_HW_VERSION_CHANNEL:
channels[0] = ADC_CHANNEL(BUSVOODOO_HW_VERSION_CHANNEL);
break;
default: // unknown channel
return NAN;
}
adc_set_regular_sequence(ADC1, LENGTH(channels), channels); // set channel to convert
ADC_SR(ADC1) = 0; // reset flags
adc_start_conversion_direct(ADC1); // start conversion (without using trigger)
while (!adc_eoc(ADC1)); // wait until conversion finished
uint16_t desired_value = adc_read_regular(ADC1); // read voltage value (clears flag)
// calculate desired voltage
float to_return = NAN; // voltage to return
switch (channel) { // get converted value and calculate according to the voltage divider on this channel
case BUSVOODOO_5V_CHANNEL:
to_return = desired_value/(10.0/(10.0+10.0));
break;
case BUSVOODOO_3V3_CHANNEL:
to_return = desired_value/(10.0/(10.0+10.0));
break;
case BUSVOODOO_LV_CHANNEL:
to_return = desired_value/(10.0/(10.0+10.0));
break;
case BUSVOODOO_HV_CHANNEL:
to_return = desired_value/(1.5/(10.0+1.5));
break;
case BUSVOODOO_HW_VERSION_CHANNEL:
to_return = desired_value;
break;
default: // unknown channel
to_return = NAN;
break;
}
if (!isnan(to_return)) {
to_return *= 1.2/internal_value; // calculate voltage from converted values using internal 1.2V voltage reference
}
return to_return;
}
float busvoodoo_lv_set(float voltage)
{
float volt = NAN; // common variable for voltages
if (voltage<=0.3) { // disable voltage regulator
gpio_clear(GPIO(BUSVOODOO_LVEN_PORT), GPIO(BUSVOODOO_LVEN_PIN)); // disable LV voltage regulator
dac_disable(BUSVOODOO_LVCTL_CHANNEL); // disable LV control
} else { // enable voltage regulator
if (voltage>4.85) { // use the 5V directly
gpio_clear(GPIO(BUSVOODOO_5VPULLUP_PORT), GPIO(BUSVOODOO_5VPULLUP_PIN)); // put 5V on LV line
} else { // use adjustable voltage regulator (5.0V rail minus LDO and diodes)
gpio_set(GPIO(BUSVOODOO_5VPULLUP_PORT), GPIO(BUSVOODOO_5VPULLUP_PIN)); // disable 5V input
volt = busvoodoo_vreg_get(BUSVOODOO_3V3_CHANNEL); // get reference voltage
if (isnan(voltage)) {
return NAN;
}
uint16_t dac_set = BUSVOODOO_LV_SET(voltage)/volt*4095; // DAC value corresponding to the voltage
dac_load_data_buffer_single(dac_set, RIGHT12, BUSVOODOO_LVCTL_CHANNEL); // set output so the voltage regulator is set to 2.5V
dac_software_trigger(BUSVOODOO_LVCTL_CHANNEL); // transfer the value to the DAC
dac_enable(BUSVOODOO_LVCTL_CHANNEL); // enable DAC
gpio_set(GPIO(BUSVOODOO_LVEN_PORT), GPIO(BUSVOODOO_LVEN_PIN)); // enable LV voltage regulator
}
}
sleep_ms(50); // let voltage settle
volt = busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL); // get LV voltage to return
return volt; // return measured voltage
}
float busvoodoo_hv_set(float voltage)
{
if (!busvoodoo_full) { // the HV voltage regulator is only present on the full version
return NAN;
}
float volt = NAN; // common variable for voltages
if (voltage<3.29) { // disable voltage regulator
gpio_set(GPIO(BUSVOODOO_HVEN_PORT), GPIO(BUSVOODOO_HVEN_PIN)); // disable HV voltage regulator
dac_disable(BUSVOODOO_HVCTL_CHANNEL); // disable HV control
} else {
if (voltage>24.0) { // enforce upper voltage limit (diodes limit is 30V, ADC input limit is 25V)
voltage = 24.0;
}
volt = busvoodoo_vreg_get(BUSVOODOO_3V3_CHANNEL); // get reference voltage
if (isnan(voltage)) {
return NAN;
}
uint16_t dac_set = BUSVOODOO_HV_SET(voltage)/volt*4095; // DAC value corresponding to the voltage
dac_load_data_buffer_single(dac_set, RIGHT12, BUSVOODOO_HVCTL_CHANNEL); // set output so the voltage regulator is set to desired output voltage
dac_software_trigger(BUSVOODOO_HVCTL_CHANNEL); // transfer the value to the DAC
dac_enable(BUSVOODOO_HVCTL_CHANNEL); // enable DAC
gpio_clear(GPIO(BUSVOODOO_HVEN_PORT), GPIO(BUSVOODOO_HVEN_PIN)); // enable HV voltage regulator
}
sleep_ms(100); // let the voltage regulator start and voltage settle
volt = busvoodoo_vreg_get(BUSVOODOO_HV_CHANNEL); // get HV voltage
return volt; // return measured voltage
}
float busvoodoo_embedded_pullup(bool on)
{
if (on) { // enable embedded pull-ups
gpio_clear(GPIO(BUSVOODOO_OEPULLUP_PORT), GPIO(BUSVOODOO_OEPULLUP_PIN)); // switch on embedded pull-ups
} else { // disable embedded pull-ups
gpio_set(GPIO(BUSVOODOO_OEPULLUP_PORT), GPIO(BUSVOODOO_OEPULLUP_PIN)); // switch off embedded pull-up
}
return busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL); // set voltage on adjustable voltage regulator to be used by the embedded pull-ups
}
static void busvoodoo_leds_update(void)
{
// handle LED timer
if (busvoodoo_global_led_blue_timer && 0==busvoodoo_global_led_blue_timeout) { // timer reached timeout
busvoodoo_global_led_blue_timer = false; // timer it not required anymore
}
if (busvoodoo_global_led_red_timer && 0==busvoodoo_global_led_red_timeout) { // timer reached timeout
busvoodoo_global_led_red_timer = false; // timer it not required anymore
}
if (!busvoodoo_global_led_blue_timer && !busvoodoo_global_led_red_timer) { // timer is not needed anymore
timer_disable_counter(TIM(BUSVOODOO_LED_TIMER)); // disable timer
timer_disable_irq(TIM(BUSVOODOO_LED_TIMER), TIM_DIER_UIE); // disable overflow interrupt used as tick
}
// drive right LED
if (busvoodoo_global_led_blue_timeout>0 && busvoodoo_global_led_red_timeout>0) { // both LEDs should be on
timer_set_period(TIM(BUSVOODOO_LED_TIMER), (rcc_ahb_frequency/3296)/1000); // ensure timer is set period to 1 ms for pulsing
timer_set_oc_value(TIM(BUSVOODOO_LED_TIMER), TIM_OC1, rcc_ahb_frequency/3296/1000/2); // set 50% PWM duty cycle
timer_enable_oc_output(TIM(BUSVOODOO_LED_TIMER), TIM_OC1); // enable PWM output
gpio_set_mode(GPIO(LED_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO(LED_PIN)); // allow PWM to drive pin
timer_enable_counter(TIM(BUSVOODOO_LED_TIMER)); // ensure the timer is enabled (interrupt should be disabled if not required)
} else if (busvoodoo_global_led_blue_timeout>0) { // only blue LED should be on
timer_disable_oc_output(TIM(BUSVOODOO_LED_TIMER), TIM_OC1); // disable PWM output
gpio_set(GPIO(LED_PORT), GPIO(LED_PIN)); // switch only blue LED on
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
} else if (busvoodoo_global_led_red_timeout>0) { // only red LED should be on
timer_disable_oc_output(TIM(BUSVOODOO_LED_TIMER), TIM_OC1); // disable PWM output
gpio_clear(GPIO(LED_PORT), GPIO(LED_PIN)); // switch only red LED on
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
} else { // no LED should be on
timer_disable_oc_output(TIM(BUSVOODOO_LED_TIMER), TIM_OC1); // disable PWM output
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
}
busvoodoo_global_led_blinking = false; // setting the LEDs forced blinking mode exit
}
void busvoodoo_led_blue_on(void)
{
busvoodoo_global_led_blue_timer = false; // there it no timeout of this LED
busvoodoo_global_led_blue_timeout = 1; // still enable LED
busvoodoo_leds_update(); // update LED status
}
void busvoodoo_led_blue_off(void)
{
busvoodoo_global_led_blue_timer = false; // there it no timeout of this LED
busvoodoo_global_led_blue_timeout = 0; // disable LED
busvoodoo_leds_update(); // update LED status
}
void busvoodoo_led_red_on(void)
{
busvoodoo_global_led_red_timer = false; // there it no timeout of this LED
busvoodoo_global_led_red_timeout = 1; // still enable LED
busvoodoo_leds_update(); // update LED status
}
void busvoodoo_led_red_off(void)
{
busvoodoo_global_led_red_timer = false; // there it no timeout of this LED
busvoodoo_global_led_red_timeout = 0; // disable LED
busvoodoo_leds_update(); // update LED status
}
void busvoodoo_leds_off(void)
{
busvoodoo_global_led_blue_timer = false; // there it no timeout of this LED
busvoodoo_global_led_blue_timeout = 0; // disable LED
busvoodoo_global_led_red_timer = false; // there it no timeout of this LED
busvoodoo_global_led_red_timeout = 0; // disable LED
busvoodoo_leds_update(); // update LED status
}
static void busvoodoo_led_pulse_setup(void)
{
if (!busvoodoo_global_led_blinking) { // we are in the blink mode, reconfigure to pulse mode
timer_disable_oc_output(TIM(BUSVOODOO_LED_TIMER), TIM_OC1); // disable PWM output
timer_set_period(TIM(BUSVOODOO_LED_TIMER), (rcc_ahb_frequency/3296)/1000); // set period to 1 ms for pulsing
busvoodoo_global_led_blinking = false; // remember we quite the PWM/blinking mode
}
timer_enable_irq(TIM(BUSVOODOO_LED_TIMER), TIM_DIER_UIE); // enable overflow interrupt used as tick
timer_enable_counter(TIM(BUSVOODOO_LED_TIMER)); // ensure the timer is enabled
}
void busvoodoo_led_blue_pulse(uint32_t ms)
{
if (0==ms) { // disable LED
busvoodoo_global_led_blue_timer = false; // no need to use the timer
busvoodoo_global_led_blue_timeout = 0; // disable blue LED
} else {
busvoodoo_global_led_blue_timer = true; // use the timer
busvoodoo_global_led_blue_timeout = ms; // disable blue LED
busvoodoo_led_pulse_setup(); // start timer
}
busvoodoo_leds_update(); // update LED status
}
void busvoodoo_led_red_pulse(uint32_t ms)
{
if (0==ms) { // disable LED
busvoodoo_global_led_red_timer = false; // no need to use the timer
busvoodoo_global_led_red_timeout = 0; // disable blue LED
} else {
busvoodoo_global_led_red_timer = true; // use the timer
busvoodoo_global_led_red_timeout = ms; // disable blue LED
busvoodoo_led_pulse_setup(); // start timer
}
busvoodoo_leds_update(); // update LED status
}
void tim1_up_isr(void)
{
if (timer_get_flag(TIM(BUSVOODOO_LED_TIMER), TIM_SR_UIF)) { // tick occurred
timer_clear_flag(TIM(BUSVOODOO_LED_TIMER), TIM_SR_UIF); // clear flag
if (busvoodoo_global_led_blue_timer && busvoodoo_global_led_blue_timeout>0) { // timeout for blue LED is running
busvoodoo_global_led_blue_timeout--; // decrement remaining timeout
}
if (busvoodoo_global_led_red_timer && busvoodoo_global_led_red_timeout>0) { // timeout for red LED is running
busvoodoo_global_led_red_timeout--; // decrement remaining timeout
}
if (0==busvoodoo_global_led_blue_timeout || 0==busvoodoo_global_led_red_timeout) { // a timeout occured
busvoodoo_leds_update(); // update LED status
}
}
}
void busvoodoo_leds_blink(double period, double duty)
{
if (period<0.0 || period>6.0 || duty<0.0 || duty>1.0) { // input argument out of bounds
return; // do nothing
}
timer_disable_counter(TIM(BUSVOODOO_LED_TIMER)); // disable timer while reconfiguring
timer_disable_irq(TIM(BUSVOODOO_LED_TIMER), TIM_DIER_UIE); // disable overflow interrupt used for pulsing
if (busvoodoo_global_led_blue_timer) { // switch off LED when pulsing
busvoodoo_global_led_blue_timer = false; // stop pulse
busvoodoo_global_led_blue_timeout = 0; // switch off
}
if (busvoodoo_global_led_blue_timer) { // switch off LED when pulsing
busvoodoo_global_led_blue_timer = false; // stop pulse
busvoodoo_global_led_blue_timeout = 0; // switch off
}
if (0.0==period) { // no blinking
if (duty>=0.5) { // only enable blue LED
busvoodoo_global_led_blue_timeout = 1;
} else { // only enable red LED
busvoodoo_global_led_red_timeout = 1;
}
busvoodoo_leds_update();
} else {
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
timer_set_counter(TIM(BUSVOODOO_LED_TIMER), 0); // reset counter
timer_set_period(TIM(BUSVOODOO_LED_TIMER), 0xffff*(period/6.0)); // set period
timer_set_oc_value(TIM(BUSVOODOO_LED_TIMER), TIM_OC1, 0xffff*(period/6.0)*duty); // PWM duty cycle
timer_enable_counter(TIM(BUSVOODOO_LED_TIMER)); // enable timer to start blinking
}
}
static void busvoodoo_global_power_led_update(void)
{
bool power_led_on = false; // to calculate the final state of the power LED
power_led_on |= !gpio_get(GPIO(BUSVOODOO_VOUTEN_PORT), GPIO(BUSVOODOO_VOUTEN_PIN)); // check power rails output
power_led_on |= !gpio_get(GPIO(BUSVOODOO_5VPULLUP_PORT), GPIO(BUSVOODOO_5VPULLUP_PIN)); // check 5V output on LV pin
power_led_on |= gpio_get(GPIO(BUSVOODOO_LVEN_PORT), GPIO(BUSVOODOO_LVEN_PIN)); // check if low-voltage regulator is on
if (!busvoodoo_full) {
power_led_on |= !gpio_get(GPIO(BUSVOODOO_HVEN_PORT), GPIO(BUSVOODOO_HVEN_PIN)); // check if high-voltage regulator is on
}
if (power_led_on) {
busvoodoo_led_red_on(); // switch on red LED to indicate one of the power output is on
} else {
busvoodoo_led_red_off(); // switch off red LED to indicate no power output is on
}
}
bool busvoodoo_global_actions(char* actions, bool perform, bool (*action_handler)(const char* action, uint32_t repetition, bool perform))
{
char* action_start = actions; // start of the current action
bool last_action = false; // is the current action the last one
while ('\0'!=*action_start && !last_action) {
// find end of action
char* action_end = action_start+1;
if ('"'==*action_start || '\''==*action_start) { // start of string
while ('\0'!=*action_end && *action_end!=*action_start) {
action_end++;
}
if (*action_end!=*action_start) { // action not ended correctly
return false;
}
action_end++; // go the end of action
} else { // just look for a separation
while ('\0'!=*action_end && ':'!=*action_end && ' '!=*action_end && ','!=*action_end) {
action_end++;
}
}
// find start of next action
char *separation = action_end; // position of separation to next action
while ('\0'!=*separation && ' '!=*separation && ','!=*separation) { // find separation or end
separation++;
}
if ('\0'==*separation) {
last_action = true; // remember we reached the end of the string
} else {
*separation = '\0'; // end the action to form a string
}
// get multiplier
uint32_t multiplier = 1; // the number of times the action should be performed
if (separation>action_end) { // there is something after the action
if (':'==*action_end) { // multiplier sign found
if (separation==action_end+1) { // no digit present
return false; // malformed action
}
for (char* digit=action_end+1; digit<separation; digit++) { // check if all the characters are digits
if (*digit<'0' || *digit>'9') { // not a digit
return false; // malformed string
}
}
multiplier = strtol(action_end+1, NULL, 10); // parse multiplier number
} else { // unknown sign after action
return false; // malformed action
}
}
// perform action
*action_end = '\0'; // end action string
if (!(*action_handler)(action_start, multiplier, perform)) { // perform action
return false; // action if malformed
}
// go to next action
if (!last_action) {
action_start = separation+1;
}
}
return true; // all went well
}
/* command handlers */
static void busvoodoo_global_power(void* argument)
{
float voltage;
if (NULL==argument || 0==strlen(argument)) {
if (gpio_get(GPIO(BUSVOODOO_VOUTEN_PORT), GPIO(BUSVOODOO_VOUTEN_PIN))) { // check if power rails are switch on (enable low)
goto power_off;
} else {
goto power_on;
}
} else if (0==strcmp(argument, "on")) {
if (busvoodoo_vout_switch(true)) { // switch power rail on
printf("power rails switched on\n");
} else {
printf("power rails switched on but malfunctioning\n");
}
power_on:
voltage = busvoodoo_vreg_get(BUSVOODOO_5V_CHANNEL); // get 5V power rail voltage
printf("5V power rail: %.2fV\n", voltage);
voltage = busvoodoo_vreg_get(BUSVOODOO_3V3_CHANNEL); // get 3.3V power rail voltage
printf("3V3 power rail: %.2fV\n", voltage);
} else if (0==strcmp(argument, "off")) {
busvoodoo_vout_switch(false); // switch power rail off
printf("power rails switched off\n");
power_off:
printf("5V power rail: off\n");
printf("3V3 power rail: off\n");
} else {
printf("option malformed: %s\n", argument);
}
busvoodoo_global_power_led_update(); // update power output LED
}
static void busvoodoo_global_lv(void* argument)
{
if (NULL==argument) {
if (!gpio_get(GPIO(BUSVOODOO_5VPULLUP_PORT), GPIO(BUSVOODOO_5VPULLUP_PIN))) { // 5V input enabled
printf("5V power rail used");
} else if (gpio_get(GPIO(BUSVOODOO_LVEN_PORT), GPIO(BUSVOODOO_LVEN_PIN))) { // LV voltage regulator used
printf("adjustable voltage regulator used");
} else {
printf("external voltage input");
}
float voltage = busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL); // get LV voltage
// print LV voltage
if (voltage < 0.1) {
printf(": 0.00V\n");
} else {
printf(": %.2fV\n", voltage);
}
} else {
double voltage = *((double*)argument); // get desired voltage
if (0==voltage) {
printf("LV rail switched off");
} else {
printf("LV rail set to %.2fV", voltage);
}
voltage = busvoodoo_lv_set(voltage); // set LV voltage
// print LV voltage
if (voltage < 0.1) {
printf(": 0.00V\n");
} else {
printf(": %.2fV\n", voltage);
}
}
busvoodoo_global_power_led_update(); // update power output LED
}
static void busvoodoo_global_hv(void* argument)
{
if (!busvoodoo_full) {
printf("function not available on BusVoodoo light");
return;
}
if (NULL==argument) {
printf("high voltage regulator switched %s: ", gpio_get(GPIO(BUSVOODOO_HVEN_PORT), GPIO(BUSVOODOO_HVEN_PIN)) ? "off" : "on");
float voltage = busvoodoo_vreg_get(BUSVOODOO_HV_CHANNEL); // get HV voltage
// print LV voltage
if (voltage < 0.1) {
printf("0.00V\n");
} else {
printf("%.2fV\n", voltage);
}
} else {
double voltage = *((double*)argument); // get desired voltage
printf("high voltage rail ");
if (voltage<=3.3) {
printf("switched off");
} else {
printf("set to %.2fV", voltage);
}
voltage = busvoodoo_hv_set(voltage); // set HV voltage
// print HV voltage
if (voltage < 0.1) {
printf(": 0.00V\n");
} else {
printf(": %.2fV\n", voltage);
}
}
busvoodoo_global_power_led_update(); // update power output LED
}
static void busvoodoo_global_pinout(void* argument)
{
(void)argument; // argument is not used
bool no_pinout = true; // it no pinout has been displays
// display RS/CAN connector pinout
if (busvoodoo_full) { // only display on full version of the board
bool pin_used = false; // if no pins are used
for (uint8_t i=0; i<LENGTH(busvoodoo_global_pinout_rscan); i++) { // verify if one of the pins is used
pin_used |= (NULL!=busvoodoo_global_pinout_rscan[i]); // verify if pin is used
}
if (pin_used) {
// count the space used to display the pins
uint8_t space = 0; // no integer overflow protected (it's only cosmetic)
for (uint8_t i=0; i<LENGTH(busvoodoo_global_pinout_rscan); i++) {
if (NULL==busvoodoo_global_pinout_rscan[i]) {
space += 1; // only x will be shown
} else {
space += strlen(busvoodoo_global_pinout_rscan[i]); // add size of pin name
}
}
space += LENGTH(busvoodoo_global_pinout_rscan)-1; // add the spaces between the names
// display pinout
printf("RS/CAN connector pinout:\n");
// display top line
printf("+");
for (uint8_t i=0; i<space; i++) {
printf("-");
}
printf("+\n");
// display pin names
printf("|");
for (int8_t i=LENGTH(busvoodoo_global_pinout_rscan)-1; i>=0; i--) {
if (NULL==busvoodoo_global_pinout_rscan[i]) {
printf("x"); // x stands for pin not used
} else {
printf("%s", busvoodoo_global_pinout_rscan[i]); // print pin name
}
if (i>0) {
printf(" "); // print space between the pin names
}
}
printf("|\n");
// display bottom line
printf("+");
for (uint8_t i=0; i<space; i++) {
printf("-");
}
printf("+\n");
no_pinout = false; // remember a pinout has been shown
}
}
// display I/O connector pinout
bool pin_used = false; // if no pins are used
for (uint8_t i=0; i<LENGTH(busvoodoo_global_pinout_io); i++) { // verify if one of the pins is used
pin_used |= (NULL!=busvoodoo_global_pinout_io[i]); // verify if pin is used
}
if (pin_used) {
// count the space used to display the pins (no integer overflow protected, it's only cosmetic)
uint8_t spaces[5] = {0}; // maximum spaces used by top and bottom pins
for (uint8_t i=0; i<LENGTH(spaces); i++) {
if (NULL==busvoodoo_global_pinout_io[i*2]) {
spaces[i] = 1; // only x will be shown
} else {
spaces[i] = strlen(busvoodoo_global_pinout_io[i*2]); // remember size of pin name
}
if (NULL==busvoodoo_global_pinout_io[i*2+1]) {
if (spaces[i]<1) {
spaces[i] = 1; // only x will be shown
}
} else {
if (spaces[i]<strlen(busvoodoo_global_pinout_io[i*2+1])) {
spaces[i] = strlen(busvoodoo_global_pinout_io[i*2+1]); // remember bigger size of pin name
}
}
}
// display pinout
printf("I/O connector pinout:\n");
// display top line
printf("+");
for (uint16_t i=0; i<(uint16_t)(spaces[4]+spaces[3]+1); i++) {
printf("-");
}
for (uint16_t i=0; i<(uint16_t)(spaces[2]+2); i++) {
printf(" ");
}
for (uint16_t i=0; i<(uint16_t)(spaces[1]+spaces[0]+1); i++) {
printf("-");
}
printf("+\n");
// display top pin names
printf("|");
for (int8_t i=4; i>=0; i--) {
if (NULL==busvoodoo_global_pinout_io[i*2]) {
printf("x"); // x stands for pin not used
for (int16_t j=0; j<spaces[i]-1; j++) {
printf(" "); // pad to match to bottom pin
}
} else {
printf("%s", busvoodoo_global_pinout_io[i*2]); // print pin name
for (int16_t j=0; j+strlen(busvoodoo_global_pinout_io[i*2])<spaces[i]; j++) {
printf(" "); // pad to match to bottom pin
}
}
if (i>0) {
printf(" "); // print space between the pin names
}
}
printf("|\n");
// display bottom pin names
printf("|");
for (int8_t i=4; i>=0; i--) {
if (NULL==busvoodoo_global_pinout_io[i*2+1]) {
printf("x"); // x stands for pin not used
for (int16_t j=0; j<spaces[i]-1; j++) {
printf(" "); // pad to match to bottom pin
}
} else {
printf("%s", busvoodoo_global_pinout_io[i*2+1]); // print pin name
for (int16_t j=0; j+strlen(busvoodoo_global_pinout_io[i*2+1])<spaces[i]; j++) {
printf(" "); // pad to match to bottom pin
}
}
if (i>0) {
printf(" "); // print space between the pin names
}
}
printf("|\n");
// display bottom line
printf("+");
for (uint16_t i=0; i<spaces[4]+1+spaces[3]+1+spaces[2]+1+spaces[1]+1+spaces[0]; i++) {
printf("-");
}
printf("+\n");
no_pinout = false; // remember a pinout has been shown
}
// in case nothing has been displayed
if (no_pinout) {
printf("no pins are used\n");
}
}
const struct menu_command_t busvoodoo_global_commands[] = {
{
.shortcut = 'p',
.name = "pinout",
.command_description = "show connector pinout",
.argument = MENU_ARGUMENT_NONE,
.argument_description = NULL,
.command_handler = &busvoodoo_global_pinout,
},
{
.shortcut = 'P',
.name = "power",
.command_description = "switch 3V3 and 5V power rails on/off, or read internal voltages",
.argument = MENU_ARGUMENT_STRING,
.argument_description = "[on|off]",
.command_handler = &busvoodoo_global_power,
},
{
.shortcut = 'L',
.name = "LV",
.command_description = "set voltage on low voltage power rail (0, 0.3-4.8, 5V), or read voltage on pin",
.argument = MENU_ARGUMENT_FLOAT,
.argument_description = "[voltage]",
.command_handler = &busvoodoo_global_lv,
},
};
const uint8_t busvoodoo_global_commands_nb = LENGTH(busvoodoo_global_commands);
const struct menu_command_t busvoodoo_global_full_commands[] = {
{
.shortcut = 'H',
.name = "HV",
.command_description = "set voltage on high voltage power rail (0, 3.3-24V), or read voltage on pin",
.argument = MENU_ARGUMENT_FLOAT,
.argument_description = "[voltage]",
.command_handler = &busvoodoo_global_hv,
},
};
const uint8_t busvoodoo_global_full_commands_nb = LENGTH(busvoodoo_global_full_commands);
busvoodoo_leds_off
void busvoodoo_leds_off(void)
switch off blue and red LEDs
Definition: busvoodoo_global.c:447
BUSVOODOO_5V_CHANNEL
#define BUSVOODOO_5V_CHANNEL
ADC channel to measure 3.3V rail.
Definition: busvoodoo_global.h:43
busvoodoo_hv_set
float busvoodoo_hv_set(float voltage)
set voltage on high voltage adjustable voltage regulator
Definition: busvoodoo_global.c:343
busvoodoo_global_power_led_update
static void busvoodoo_global_power_led_update(void)
updates the red power LED status
Definition: busvoodoo_global.c:545
BUSVOODOO_HVCTL_CHANNEL
#define BUSVOODOO_HVCTL_CHANNEL
DAC channel to control HV output voltage.
Definition: busvoodoo_global.h:57
menu_command_t
command menu entry
Definition: menu.h:32
busvoodoo_global_commands
const struct menu_command_t busvoodoo_global_commands[]
list of supported commands for base BusVoodoo
Definition: busvoodoo_global.c:880
busvoodoo_global_power
static void busvoodoo_global_power(void *argument)
switch 3V3 and 5V power rails on/off
Definition: busvoodoo_global.c:626
busvoodoo_global.h
BusVoodoo global definitions and methods (API)
BUSVOODOO_HVEN_PIN
#define BUSVOODOO_HVEN_PIN
high voltage (HV) enable pin (active low)
Definition: busvoodoo_global.h:34
BUSVOODOO_HW_VERSION_CHANNEL
#define BUSVOODOO_HW_VERSION_CHANNEL
ADC to identify hardware version.
Definition: busvoodoo_global.h:108
busvoodoo_global_lv
static void busvoodoo_global_lv(void *argument)
set LV linear drop-out voltage regulator voltage
Definition: busvoodoo_global.c:661
busvoodoo_global_led_blinking
static volatile bool busvoodoo_global_led_blinking
if the LEDs are in a blinking pattern
Definition: busvoodoo_global.c:57
busvoodoo_led_blue_pulse
void busvoodoo_led_blue_pulse(uint32_t ms)
pulse blue LED for short duration
Definition: busvoodoo_global.c:468
BUSVOODOO_LVCTL_PORT
#define BUSVOODOO_LVCTL_PORT
pin to control LV output voltage
Definition: busvoodoo_global.h:51
busvoodoo_vout_switch
bool busvoodoo_vout_switch(bool on)
switch 3V3 and 5V power outputs on I/O connector
Definition: busvoodoo_global.c:226
BUSVOODOO_RS232_EN_PORT
#define BUSVOODOO_RS232_EN_PORT
RS-232 pin to enable receiver (active low, pulled up)
Definition: busvoodoo_global.h:64
BUSVOODOO_HW_VERSION_PORT
#define BUSVOODOO_HW_VERSION_PORT
pin to identify hardware version
Definition: busvoodoo_global.h:106
busvoodoo_global_hv
static void busvoodoo_global_hv(void *argument)
set HV step-up voltage regulator voltage
Definition: busvoodoo_global.c:699
BUSVOODOO_HV_SET
#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...
Definition: busvoodoo_global.h:58
BUSVOODOO_HVCTL_PORT
#define BUSVOODOO_HVCTL_PORT
pin to control HV output voltage
Definition: busvoodoo_global.h:55
BUSVOODOO_HVEN_PORT
#define BUSVOODOO_HVEN_PORT
high voltage (HV) enable pin (active low)
Definition: busvoodoo_global.h:33
BUSVOODOO_RS232_CTS_PORT
#define BUSVOODOO_RS232_CTS_PORT
RS-232 Clear-To-Send input pin.
Definition: busvoodoo_global.h:70
busvoodoo_lv_set
float busvoodoo_lv_set(float voltage)
set voltage on low voltage adjustable voltage regulator
Definition: busvoodoo_global.c:315
BUSVOODOO_LVEN_PIN
#define BUSVOODOO_LVEN_PIN
low voltage (LV) enable pin (active high)
Definition: busvoodoo_global.h:32
busvoodoo_global_pinout_rscan
const char * busvoodoo_global_pinout_rscan[5]
RS/CAN connector pinout.
Definition: busvoodoo_global.c:63
busvoodoo_global_led_red_timeout
static volatile uint32_t busvoodoo_global_led_red_timeout
number of remaining milliseconds the red LED should stay on
Definition: busvoodoo_global.c:53
BUSVOODOO_LV_SET
#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...
Definition: busvoodoo_global.h:54
busvoodoo_io_pins
const uint32_t busvoodoo_io_pins[13]
pin of individual signals
Definition: busvoodoo_global.c:67
BUSVOODOO_RS232_TX_PORT
#define BUSVOODOO_RS232_TX_PORT
RS-232 Transmit output pin.
Definition: busvoodoo_global.h:72
global.h
global definitions and methods (API)
menu_command_t::argument
enum menu_argument_t argument
what kind of argument it accepts
Definition: menu.h:36
GPIO
#define GPIO(x)
get GPIO based on GPIO identifier
Definition: global.h:103
BUSVOODOO_RS485_RX_PORT
#define BUSVOODOO_RS485_RX_PORT
RS-485 Receive input pin.
Definition: busvoodoo_global.h:87
sleep_us
void sleep_us(uint32_t duration)
go to sleep for some microseconds
Definition: global.c:102
BUSVOODOO_RS485_RE_PORT
#define BUSVOODOO_RS485_RE_PORT
RS-485 pin to enable receiver (active low, pulled up)
Definition: busvoodoo_global.h:81
busvoodoo_full
bool busvoodoo_full
is the BusVoodoo board fully populated (with HV voltage regulator, RS-232, RS-485, CAN transceiver on the back side)
Definition: busvoodoo_global.c:70
RCC_TIM
#define RCC_TIM(x)
get RCC for timer based on TIM identifier
Definition: global.h:109
busvoodoo_io_ports
const uint32_t busvoodoo_io_ports[13]
port of individual signals
Definition: busvoodoo_global.c:66
BUSVOODOO_RS485_TX_PORT
#define BUSVOODOO_RS485_TX_PORT
RS-485 Transmit output pin.
Definition: busvoodoo_global.h:85
busvoodoo_led_blue_off
void busvoodoo_led_blue_off(void)
switch off blue LED
Definition: busvoodoo_global.c:426
BUSVOODOO_OEPULLUP_PIN
#define BUSVOODOO_OEPULLUP_PIN
bus switch output enable pin to enable embedded pull-ups (active low)
Definition: busvoodoo_global.h:30
ADC12_IN_PORT
#define ADC12_IN_PORT(x)
get port based on ADC12_IN identifier
Definition: global.h:215
BUSVOODOO_RS232_CTS_PIN
#define BUSVOODOO_RS232_CTS_PIN
RS-232 Clear-To-Send input pin.
Definition: busvoodoo_global.h:71
BUSVOODOO_LV_CHANNEL
#define BUSVOODOO_LV_CHANNEL
ADC channel to measure LV rail.
Definition: busvoodoo_global.h:44
BUSVOODOO_RS232_TX_PIN
#define BUSVOODOO_RS232_TX_PIN
RS-232 Transmit output pin.
Definition: busvoodoo_global.h:73
BUSVOODOO_RS485_DE_PORT
#define BUSVOODOO_RS485_DE_PORT
RS-485 pin to enable transmitter (active high, pulled low)
Definition: busvoodoo_global.h:83
BUSVOODOO_RS232_RTS_PIN
#define BUSVOODOO_RS232_RTS_PIN
RS-232 Request-To-Send output pin.
Definition: busvoodoo_global.h:69
busvoodoo_io_groups
const uint8_t busvoodoo_io_groups[13]
which I/O pin (group) does the signal belong to
Definition: busvoodoo_global.c:68
menu_command_t::shortcut
char shortcut
short command code (0 if not available)
Definition: menu.h:33
ADC12_IN_PIN
#define ADC12_IN_PIN(x)
get pin based on ADC12_IN identifier
Definition: global.h:233
busvoodoo_leds_blink
void busvoodoo_leds_blink(double period, double duty)
set LED blinking pattern
Definition: busvoodoo_global.c:511
busvoodoo_led_blue_on
void busvoodoo_led_blue_on(void)
switch on blue LED
Definition: busvoodoo_global.c:419
BUSVOODOO_HV_CHANNEL
#define BUSVOODOO_HV_CHANNEL
ADC channel to measure HV rail.
Definition: busvoodoo_global.h:45
busvoodoo_global_pinout_io
const char * busvoodoo_global_pinout_io[10]
I/O connector pinout.
Definition: busvoodoo_global.c:62
BUSVOODOO_LVCTL_PIN
#define BUSVOODOO_LVCTL_PIN
pin to control LV output voltage
Definition: busvoodoo_global.h:52
BUSVOODOO_RS232_EN_PIN
#define BUSVOODOO_RS232_EN_PIN
RS-232 pin to enable receiver (active low, pulled up)
Definition: busvoodoo_global.h:65
busvoodoo_vreg_get
float busvoodoo_vreg_get(uint8_t channel)
read voltage from power rail
Definition: busvoodoo_global.c:247
busvoodoo_io_names
const char * busvoodoo_io_names[13]
I/O individual signal names.
Definition: busvoodoo_global.c:65
busvoodoo_global_string
char busvoodoo_global_string[64]
shared string buffer, i.e.
Definition: busvoodoo_global.c:72
BUSVOODOO_5VPULLUP_PIN
#define BUSVOODOO_5VPULLUP_PIN
5V pull-up enable pin (active low)
Definition: busvoodoo_global.h:28
busvoodoo_global_commands_nb
const uint8_t busvoodoo_global_commands_nb
number supported commands for base BusVoodoo
Definition: busvoodoo_global.c:907
BUSVOODOO_VOUTEN_PORT
#define BUSVOODOO_VOUTEN_PORT
voltage output (5V and 3.3V) enable pin (active low)
Definition: busvoodoo_global.h:35
busvoodoo_global_actions
bool busvoodoo_global_actions(char *actions, bool perform, bool(*action_handler)(const char *action, uint32_t repetition, bool perform))
parse and perform actions
Definition: busvoodoo_global.c:561
sleep_ms
void sleep_ms(uint32_t duration)
go to sleep for some milliseconds
Definition: global.c:117
busvoodoo_leds_update
static void busvoodoo_leds_update(void)
update LED status according to LED flags
Definition: busvoodoo_global.c:384
printf
size_t printf(const char *format,...)
print format string on user output
Definition: print.c:448
BUSVOODOO_VOUTEN_PIN
#define BUSVOODOO_VOUTEN_PIN
voltage output (5V and 3.3V) enable pin (active low)
Definition: busvoodoo_global.h:36
busvoodoo_global_full_commands_nb
const uint8_t busvoodoo_global_full_commands_nb
number supported commands for BusVoodoo full only
Definition: busvoodoo_global.c:920
BUSVOODOO_LVCTL_CHANNEL
#define BUSVOODOO_LVCTL_CHANNEL
DAC channel to control LV output voltage.
Definition: busvoodoo_global.h:53
BUSVOODOO_HW_VERSION_PIN
#define BUSVOODOO_HW_VERSION_PIN
pin to identify hardware version
Definition: busvoodoo_global.h:107
BUSVOODOO_RS232_RX_PORT
#define BUSVOODOO_RS232_RX_PORT
RS-232 Receive input pin.
Definition: busvoodoo_global.h:74
busvoodoo_led_red_pulse
void busvoodoo_led_red_pulse(uint32_t ms)
pulse red LED for short duration
Definition: busvoodoo_global.c:481
print.h
printing utilities to replace the large printf from the standard library (API)
BUSVOODOO_RS485_DE_PIN
#define BUSVOODOO_RS485_DE_PIN
RS-485 pin to enable transmitter (active high, pulled low)
Definition: busvoodoo_global.h:84
tim1_up_isr
void tim1_up_isr(void)
interrupt service routine called on LED timeout
Definition: busvoodoo_global.c:495
busvoodoo_global_led_red_timer
static volatile bool busvoodoo_global_led_red_timer
if the timer for the red LED is enabled
Definition: busvoodoo_global.c:55
BUSVOODOO_LED_TIMER
#define BUSVOODOO_LED_TIMER
timer peripheral ID
Definition: busvoodoo_global.c:45
BUSVOODOO_LVEN_PORT
#define BUSVOODOO_LVEN_PORT
low voltage (LV) enable pin (active high)
Definition: busvoodoo_global.h:31
busvoodoo_embedded_pullup
float busvoodoo_embedded_pullup(bool on)
enable embedded pull-up resistors
Definition: busvoodoo_global.c:373
busvoodoo_led_red_off
void busvoodoo_led_red_off(void)
switch off red LED
Definition: busvoodoo_global.c:440
BUSVOODOO_RS232_RTS_PORT
#define BUSVOODOO_RS232_RTS_PORT
RS-232 Request-To-Send output pin.
Definition: busvoodoo_global.h:68
busvoodoo_global_led_blue_timer
static volatile bool busvoodoo_global_led_blue_timer
if the timer for the blue LED is enabled
Definition: busvoodoo_global.c:51
busvoodoo_global_pinout
static void busvoodoo_global_pinout(void *argument)
display I/O and RS/CAN connector pinouts
Definition: busvoodoo_global.c:736
LENGTH
#define LENGTH(x)
get the length of an array
Definition: global.h:26
BUSVOODOO_RS485_RE_PIN
#define BUSVOODOO_RS485_RE_PIN
RS-485 pin to enable receiver (active low, pulled up)
Definition: busvoodoo_global.h:82
ADC_CHANNEL
#define ADC_CHANNEL(x)
get channel based on ADC12_IN identifier
Definition: global.h:269
BUSVOODOO_OEPULLUP_PORT
#define BUSVOODOO_OEPULLUP_PORT
bus switch output enable pin to enable embedded pull-ups (active low)
Definition: busvoodoo_global.h:29
BUSVOODOO_5VPULLUP_PORT
#define BUSVOODOO_5VPULLUP_PORT
5V pull-up enable pin (active low)
Definition: busvoodoo_global.h:27
busvoodoo_led_red_on
void busvoodoo_led_red_on(void)
switch on red LED
Definition: busvoodoo_global.c:433
BUSVOODOO_RS232_RX_PIN
#define BUSVOODOO_RS232_RX_PIN
RS-232 Receive input pin.
Definition: busvoodoo_global.h:75
TIM
#define TIM(x)
get TIM based on TIM identifier
Definition: global.h:107
BUSVOODOO_RS485_RX_PIN
#define BUSVOODOO_RS485_RX_PIN
RS-485 Receive input pin.
Definition: busvoodoo_global.h:88
busvoodoo_global_full_commands
const struct menu_command_t busvoodoo_global_full_commands[]
list of supported commands for BusVoodoo full only
Definition: busvoodoo_global.c:909
busvoodoo_version_voltages
static const float busvoodoo_version_voltages[]
hardware version voltages, calculated from divider ratios, starting with version A ...
Definition: busvoodoo_global.c:60
BUSVOODOO_RS485_TX_PIN
#define BUSVOODOO_RS485_TX_PIN
RS-485 Transmit output pin.
Definition: busvoodoo_global.h:86
busvoodoo_led_pulse_setup
static void busvoodoo_led_pulse_setup(void)
setup the timer for pulsing LEDs
Definition: busvoodoo_global.c:457
busvoodoo_safe_state
void busvoodoo_safe_state(void)
set safe state by disabling all outputs
Definition: busvoodoo_global.c:171
busvoodoo_version
char busvoodoo_version
version of the hardware board
Definition: busvoodoo_global.c:71
busvoodoo_setup
void busvoodoo_setup(void)
setup BusVoodoo board
Definition: busvoodoo_global.c:74
busvoodoo_global_led_blue_timeout
static volatile uint32_t busvoodoo_global_led_blue_timeout
number of remaining milliseconds the blue LED should stay on
Definition: busvoodoo_global.c:49
BUSVOODOO_RS232_SHDN_PORT
#define BUSVOODOO_RS232_SHDN_PORT
RS-232 pin to enable transmitter (active high, pulled low)
Definition: busvoodoo_global.h:66
BUSVOODOO_3V3_CHANNEL
#define BUSVOODOO_3V3_CHANNEL
ADC channel to measure 5V rail.
Definition: busvoodoo_global.h:42
BUSVOODOO_RS232_SHDN_PIN
#define BUSVOODOO_RS232_SHDN_PIN
RS-232 pin to enable transmitter (active high, pulled low)
Definition: busvoodoo_global.h:67
BUSVOODOO_HVCTL_PIN
#define BUSVOODOO_HVCTL_PIN
pin to control HV output voltage
Definition: busvoodoo_global.h:56
RCC_ADC12_IN
#define RCC_ADC12_IN(x)
get RCC based on ADC12_IN identifier
Definition: global.h:251