/* 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 . * */ /** BusVoodoo global definitions and methods (code) * @file busvoodoo_global.c * @author King Kévin * @date 2018 * @note peripherals used: time @ref busvoodoo_led_timer */ /* standard libraries */ #include // standard integer types #include // standard utilities #include // string utilities #include // math utilities /* STM32 (including CM3) libraries */ #include // interrupt handler #include // general purpose input output library #include // real-time control clock library #include // ADC utilities #include // DAC utilities #include // 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 /** @defgroup busvoodoo_led_timer timer used to blink LED * @{ */ #define BUSVOODOO_LED_TIMER 5 /**< timer peripheral */ /** @} */ /** blue LED status */ static volatile bool busvoodoo_global_led_blue = false; /** red LED status */ static volatile bool busvoodoo_global_led_red = false; /** hardware version voltages, calculated from divider ratios, starting with version A */ 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_off(ADC1); // switch off ADC while configuring it 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(ADC1); // enable internal voltage reference adc_enable_external_trigger_regular(ADC1, ADC_CR2_EXTSEL_SWSTART); // use software trigger to start conversion adc_set_single_conversion_mode(ADC1); // we only convert one channel after another 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_calibration(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; ibusvoodoo_version_voltages[i]-0.2 && version_voltage5.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; } uint8_t channels[2] = {ADC_CHANNEL17, ADC_CHANNEL17}; // voltages to convert: internal and internal (as fallback) switch (channel) { // set desired channel case BUSVOODOO_5V_CHANNEL: channels[1] = ADC_CHANNEL(BUSVOODOO_5V_CHANNEL); break; case BUSVOODOO_3V3_CHANNEL: channels[1] = ADC_CHANNEL(BUSVOODOO_3V3_CHANNEL); break; case BUSVOODOO_LV_CHANNEL: channels[1] = ADC_CHANNEL(BUSVOODOO_LV_CHANNEL); break; case BUSVOODOO_HV_CHANNEL: channels[1] = ADC_CHANNEL(BUSVOODOO_HV_CHANNEL); break; case BUSVOODOO_HW_VERSION_CHANNEL: channels[1] = ADC_CHANNEL(BUSVOODOO_HW_VERSION_CHANNEL); break; default: // unknown channel break; } adc_set_regular_sequence(ADC1, LENGTH(channels), channels); // set channels to convert adc_enable_discontinuous_mode_regular(ADC1, LENGTH(channels)); // convert all channels uint16_t values[LENGTH(channels)] = {0}; // to store converted values ADC_SR(ADC1) = 0; // reset flags adc_start_conversion_regular(ADC1); // start conversion for individual channels for (uint8_t channel_i=0; channel_i4.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 } /** update LED status according to LED flags */ static void busvoodoo_led_update(void) { if (busvoodoo_global_led_blue && busvoodoo_global_led_red) { // both LEDs should be on led_blink(0.01, 0.5); // enable both LEDs (alternating at 100Hz) } else if (busvoodoo_global_led_blue) { // only blue LED should be on led_blue(); // enable only blue LED } else if (busvoodoo_global_led_red) { // only red LED should be on led_red(); // enable only red LED } else { // no LED should be on led_off(); // disable both LEDs } } void busvoodoo_led_blue_pulse(uint16_t ms) { timer_disable_counter(TIM(BUSVOODOO_LED_TIMER)); // disable counter while changing LEDs to avoid coherence errors (at the cost of time precision) if (ms>UINT16_MAX/2) { // enforce maximum ms = UINT16_MAX/2; } if (0==ms) { // disable LED busvoodoo_global_led_blue = false; // remember we disabled the blue LED } else { busvoodoo_global_led_blue = true; // remember the blue LED should be on timer_set_oc_value(TIM(BUSVOODOO_LED_TIMER), TIM_OC1, timer_get_counter(TIM(BUSVOODOO_LED_TIMER))+ms*2); // use capture 1 to set LED timer timer_clear_flag(TIM(BUSVOODOO_LED_TIMER), TIM_SR_CC1IF); // clear flag before enabling timer_enable_irq(TIM(BUSVOODOO_LED_TIMER), TIM_DIER_CC1IE); // enable capture 1 for blue LED } busvoodoo_led_update(); // update LED status timer_enable_counter(TIM(BUSVOODOO_LED_TIMER)); // re-enable timer } void busvoodoo_led_red_pulse(uint16_t ms) { timer_disable_counter(TIM(BUSVOODOO_LED_TIMER)); // disable counter while changing LEDs to avoid coherence errors (at the cost of time precision) if (ms>UINT16_MAX/2) { // enforce maximum ms = UINT16_MAX/2; } if (0==ms) { // disable LED busvoodoo_global_led_red = false; // remember we disabled the blue LED } else { busvoodoo_global_led_red = true; // remember the blue LED should be on timer_set_oc_value(TIM(BUSVOODOO_LED_TIMER), TIM_OC2, timer_get_counter(TIM(BUSVOODOO_LED_TIMER))+ms*2); // use capture 1 to set LED timer timer_clear_flag(TIM(BUSVOODOO_LED_TIMER), TIM_SR_CC2IF); // clear flag before enabling timer_enable_irq(TIM(BUSVOODOO_LED_TIMER), TIM_DIER_CC2IE); // enable capture 2 for red LED } busvoodoo_led_update(); // update LED status timer_enable_counter(TIM(BUSVOODOO_LED_TIMER)); // re-enable timer } 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'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 */ /** switch 3V3 and 5V power rails on/off * @param[in] argument string: "on" to switch on, "off" to switch off, NULL to get status */ 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); } } /** set lV linear drop-out voltage regulator voltage * @param[in] argument voltage to set (0 to switch off, NULL to get voltage) */ 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); } } } /** set HV step-up voltage regulator voltage * @param[in] argument voltage to set (0 to switch off, NULL to get voltage) */ 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); } } } /** display I/O and RS/CAN connector pinouts * @param[in] argument not used */ 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=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=0; i--) { if (NULL==busvoodoo_global_pinout_io[i*2]) { printf("x"); // x stands for pin not used for (int16_t j=0; j0) { 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; j0) { printf(" "); // print space between the pin names } } printf("|\n"); // display bottom line printf("+"); for (uint16_t i=0; i