stm32f1/lib/busvoodoo_global.c

493 lines
24 KiB
C

/* 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/>.
*
*/
/** BusVoodoo global definitions and methods (code)
* @file busvoodoo_global.c
* @author King Kévin <kingkevin@cuvoodoo.info>
* @date 2018
* @note peripherals used: time @ref busvoodoo_led_timer
*/
/* standard libraries */
#include <stdint.h> // standard integer types
#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
/** @defgroup busvoodoo_led_timer timer used to blink LED
* @{
*/
#define BUSVOODOO_LED_TIMER 5 /**< timer peripheral */
/** @} */
/** blue LED status */
volatile bool busvoodoo_global_led_blue = false;
/** red LED status */
volatile bool busvoodoo_global_led_red = false;
const char* busvoodoo_io_names[13] = {"I2C_SMBA/SPI_NSS/I2S_WS", "SDIO_CMD", "USART_CTS/SPI_SCK/I2S_CK", "SDIO_D3/UART_RX", "I2C_SDA/USART_RX", "SDIO_D0", "SPI_MOSI/I2S_SD", "SDIO_CK/USART_CK", "I2C_SCL/USART_TX", "SDIO_D1", "I2S_MCK", "USART_RTS/SPI_MISO", "SDIO_D2/UART_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;
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 pin alternate function (for communication)
busvoodoo_safe_state(); // switch off all outputs
// check if this BusVoodoo is a full version
rcc_periph_clock_enable(RCC_ADC12_IN(BUSVOODOO_12V_CHANNEL)); // enable clock for GPIO domain for 12V channel
gpio_set(ADC12_IN_PORT(BUSVOODOO_12V_CHANNEL), ADC12_IN_PIN(BUSVOODOO_12V_CHANNEL)); // pull ADC 12V high
gpio_set_mode(ADC12_IN_PORT(BUSVOODOO_12V_CHANNEL), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, ADC12_IN_PIN(BUSVOODOO_12V_CHANNEL)); // set 12V channel as digital input with pull-up capabilities
// on a full version (fully populated board) the ADC 12V signal will be pulled low
if (gpio_get(ADC12_IN_PORT(BUSVOODOO_12V_CHANNEL), ADC12_IN_PIN(BUSVOODOO_12V_CHANNEL))) { // check is ADC 12V is pulled low
busvoodoo_full = false;
} else {
busvoodoo_full = true;
}
// setup ADC to measure the 5V, 3.3V, xV, and 12V power rails voltages
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_XV_CHANNEL)); // enable clock for GPIO domain for xV channel
gpio_set_mode(ADC12_IN_PORT(BUSVOODOO_XV_CHANNEL), GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, ADC12_IN_PIN(BUSVOODOO_XV_CHANNEL)); // set xV channel as analogue input for the ADC
rcc_periph_clock_enable(RCC_ADC12_IN(BUSVOODOO_12V_CHANNEL)); // enable clock for GPIO domain for 12V channel
gpio_set_mode(ADC12_IN_PORT(BUSVOODOO_12V_CHANNEL), GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, ADC12_IN_PIN(BUSVOODOO_12V_CHANNEL)); // set 12V 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
uint8_t channels[] = {ADC_CHANNEL17, ADC_CHANNEL(BUSVOODOO_5V_CHANNEL), ADC_CHANNEL(BUSVOODOO_3V3_CHANNEL), ADC_CHANNEL(BUSVOODOO_XV_CHANNEL), ADC_CHANNEL(BUSVOODOO_12V_CHANNEL)}; // voltages to convert: internal, 5V, 3.3V, xV, 12V
adc_set_regular_sequence(ADC1, LENGTH(channels), channels); // set channels to convert
adc_enable_discontinuous_mode_regular(ADC1, LENGTH(channels)); // convert all channels
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
// setup DAC to control xV and 12V voltage outputs
gpio_set_mode(GPIO(BUSVOODOO_XVCTL_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, GPIO(BUSVOODOO_XVCTL_PIN)); // set xV pin as analog (the DAC will use it as output)
rcc_periph_clock_enable(RCC_DAC); // enable clock for DAC domain
dac_disable(BUSVOODOO_XVCTL_CHANNEL); // disable output to configure it properly
dac_buffer_enable(BUSVOODOO_XVCTL_CHANNEL); // enable output buffer to be able to drive larger loads (should be per default)
if (busvoodoo_full) {
gpio_set_mode(GPIO(BUSVOODOO_12VCTL_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, GPIO(BUSVOODOO_12VCTL_PIN)); // set 12V pin as analog (the DAC will use it as output)
dac_disable(BUSVOODOO_12VCTL_CHANNEL); // disable output to configure it properly
dac_buffer_enable(BUSVOODOO_12VCTL_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 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), (rcc_ahb_frequency/2000)-1); // set prescaler to have 2kHz ticks (the prescaler is not large enough for 1kHz ticks)
timer_set_period(TIM(BUSVOODOO_LED_TIMER), 0xffff); // set period to maximum
nvic_enable_irq(NVIC_TIM_IRQ(BUSVOODOO_LED_TIMER)); // enable interrupts for this timer
}
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_XVEN_PORT), GPIO(BUSVOODOO_XVEN_PIN)); // disable xV voltage regulator
gpio_set_mode(GPIO(BUSVOODOO_XVEN_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(BUSVOODOO_XVEN_PIN)); // set pin as output (push-pull, pulled low for safety)
gpio_set(GPIO(BUSVOODOO_12VEN_PORT), GPIO(BUSVOODOO_12VEN_PIN)); // disable 12V voltage regulator
gpio_set_mode(GPIO(BUSVOODOO_12VEN_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO(BUSVOODOO_12VEN_PIN)); // set pin as output (open-drain pulled high to disable the pMOS)
// set DAC channel back to analog
gpio_set_mode(GPIO(BUSVOODOO_XVCTL_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, GPIO(BUSVOODOO_XVCTL_PIN)); // set xV pin as analog
gpio_set_mode(GPIO(BUSVOODOO_12VCTL_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, GPIO(BUSVOODOO_12VCTL_PIN)); // set 12V 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 all RS-232 and some RS-485 signals (put back to input floating)
gpio_set_mode(GPIO(BUSVOODOO_RS232_TX_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_RS232_TX_PIN));
gpio_set_mode(GPIO(BUSVOODOO_RS232_RX_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_RS232_RX_PIN));
gpio_set_mode(GPIO(BUSVOODOO_RS232_RTS_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_RS232_RTS_PIN));
gpio_set_mode(GPIO(BUSVOODOO_RS232_CTS_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_RS232_CTS_PIN));
gpio_set(GPIO(BUSVOODOO_RS232_EN_PORT), GPIO(BUSVOODOO_RS232_EN_PIN)); // set high to disable receiver
gpio_clear(GPIO(BUSVOODOO_RS232_SHDN_PORT), GPIO(BUSVOODOO_RS232_SHDN_PIN)); // set low to disable transmitter
// disable all CAN and some RS-458 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));
gpio_set_mode(GPIO(BUSVOODOO_CAN_RX_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_CAN_RX_PIN));
gpio_set_mode(GPIO(BUSVOODOO_CAN_EN_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_CAN_EN_PIN));
gpio_set_mode(GPIO(BUSVOODOO_CAN_S_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_CAN_S_PIN));
}
}
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_XV_CHANNEL && channel!=BUSVOODOO_12V_CHANNEL) { // check channel
return NAN;
}
uint16_t channels[5] = {0}; // to start converted values: internal reference 1.2V, 5V rail, 3.3V rail, xV rail, 12V rail
adc_start_conversion_regular(ADC1); // start conversion to get first voltage
for (uint8_t channel_i=0; channel_i<LENGTH(channels); channel_i++) { // get all conversions
while (!adc_eoc(ADC1)); // wait until conversion finished
channels[channel_i] = adc_read_regular(ADC1); // read voltage value (clears flag)
}
float to_return = NAN; // voltage to return
switch (channel) { // get converter value and calculate according to the voltage divider on this channel
case BUSVOODOO_5V_CHANNEL:
to_return = channels[1]/(10.0/(10.0+10.0));
break;
case BUSVOODOO_3V3_CHANNEL:
to_return = channels[2]/(10.0/(10.0+10.0));
break;
case BUSVOODOO_XV_CHANNEL:
to_return = channels[3]/(10.0/(10.0+10.0));
break;
case BUSVOODOO_12V_CHANNEL:
to_return = channels[4]/(1.5/(10.0+1.5));
break;
default: // unknown channel
to_return = NAN;
break;
}
if (!isnan(to_return)) {
to_return *= 1.2/channels[0]; // calculate voltage from converted values using internal 1.2V voltage reference
}
return to_return;
}
float busvoodoo_vreg_set(uint8_t channel, float voltage)
{
if (channel!=BUSVOODOO_XV_CHANNEL && channel!=BUSVOODOO_12V_CHANNEL) { // check channel
return NAN;
}
if (BUSVOODOO_12V_CHANNEL==channel && !busvoodoo_full) { // the 12V voltage regulator is only present on the full version
return NAN;
}
float volt = NAN; // common variable for voltages
if (BUSVOODOO_XV_CHANNEL==channel) { // set voltage on xV low drop-out voltage regulator
if (voltage<=0.3) { // disable voltage regulator
gpio_clear(GPIO(BUSVOODOO_XVEN_PORT), GPIO(BUSVOODOO_XVEN_PIN)); // disable xV voltage regulator
dac_disable(BUSVOODOO_XVCTL_CHANNEL); // disable xV 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 xV 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_XV_SET(voltage)/volt*4095; // DAC value corresponding to the voltage
dac_load_data_buffer_single(dac_set, RIGHT12, BUSVOODOO_XVCTL_CHANNEL); // set output so the voltage regulator is set to 2.5V
dac_software_trigger(BUSVOODOO_XVCTL_CHANNEL); // transfer the value to the DAC
dac_enable(BUSVOODOO_XVCTL_CHANNEL); // enable DAC
gpio_set(GPIO(BUSVOODOO_XVEN_PORT), GPIO(BUSVOODOO_XVEN_PIN)); // enable xV voltage regulator
}
}
sleep_ms(10); // let voltage settle
volt = busvoodoo_vreg_get(BUSVOODOO_XV_CHANNEL); // get xV voltage to return
} else if (BUSVOODOO_12V_CHANNEL==channel && busvoodoo_full) {// set voltage on 12V boost voltage regulator
if (voltage<3.29) { // disable voltage regulator
gpio_set(GPIO(BUSVOODOO_12VEN_PORT), GPIO(BUSVOODOO_12VEN_PIN)); // disable 12V voltage regulator
dac_disable(BUSVOODOO_12VCTL_CHANNEL); // disable 12V 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_12V_SET(voltage)/volt*4095; // DAC value corresponding to the voltage
dac_load_data_buffer_single(dac_set, RIGHT12, BUSVOODOO_12VCTL_CHANNEL); // set output so the voltage regulator is set to desired output voltage
dac_software_trigger(BUSVOODOO_12VCTL_CHANNEL); // transfer the value to the DAC
dac_enable(BUSVOODOO_12VCTL_CHANNEL); // enable DAC
gpio_clear(GPIO(BUSVOODOO_12VEN_PORT), GPIO(BUSVOODOO_12VEN_PIN)); // enable 12V voltage regulator
}
sleep_ms(100); // let the voltage regulator start and voltage settle
volt = busvoodoo_vreg_get(BUSVOODOO_12V_CHANNEL); // get 12V voltage
} else { // don't know about other voltage regulators
volt = NAN;
}
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_XV_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_blink(0, 1); // enable only blue LED
} else if (busvoodoo_global_led_red) { // only red LED should be on
led_blink(0, 0); // enable only red LED
} else { // no LED should be on
led_off(); // disable both LEDs
}
}
void busvoodoo_led_blue(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(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
}
/* 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 xV 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_XVEN_PORT), GPIO(BUSVOODOO_XVEN_PIN))) { // xV voltage regulator used
printf("adjustable voltage regulator used");
} else {
printf("external voltage input");
}
float voltage = busvoodoo_vreg_get(BUSVOODOO_XV_CHANNEL); // get xV voltage
// print xV 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("xV rail switched off");
} else {
printf("xV rail set to %.2fV", voltage);
}
voltage = busvoodoo_vreg_set(BUSVOODOO_XV_CHANNEL, voltage); // set xV voltage
// print xV voltage
if (voltage < 0.1) {
printf(": 0.00V\n");
} else {
printf(": %.2fV\n", voltage);
}
}
}
/** set 12V 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_12VEN_PORT), GPIO(BUSVOODOO_12VEN_PIN)) ? "off" : "on");
float voltage = busvoodoo_vreg_get(BUSVOODOO_12V_CHANNEL); // get 12V voltage
// print xV 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_vreg_set(BUSVOODOO_12V_CHANNEL, voltage); // set 12V voltage
// print xV voltage
if (voltage < 0.1) {
printf(": 0.00V\n");
} else {
printf(": %.2fV\n", voltage);
}
}
}
/** list of supported commands */
const struct menu_command_t busvoodoo_global_commands[] = {
{
'p',
"power",
"switch 3V3 and 5V power rails on/off",
MENU_ARGUMENT_STRING,
"[on|off]",
&busvoodoo_global_power,
},
{
'l',
"lV",
"set voltage on low voltage power rail (0, 0.3-4.8, 5V)",
MENU_ARGUMENT_FLOAT,
"[voltage]",
&busvoodoo_global_lv,
},
{
'H',
"HV",
"set voltage on high voltage power rail (0, 3.3-24V)",
MENU_ARGUMENT_FLOAT,
"[voltage]",
&busvoodoo_global_hv,
},
};
/** interrupt service routine called on LED timeout */
void TIM_ISR(BUSVOODOO_LED_TIMER)(void)
{
if (timer_get_flag(TIM(BUSVOODOO_LED_TIMER), TIM_SR_CC1IF)) { // blue LED timeout
timer_clear_flag(TIM(BUSVOODOO_LED_TIMER), TIM_SR_CC1IF); // clear flag
timer_disable_irq(TIM(BUSVOODOO_LED_TIMER), TIM_DIER_CC1IE); // disable capture 1 for blue LED
busvoodoo_global_led_blue = false; // remember blue LED should be disabled
busvoodoo_led_update(); // update LED status
} else if (timer_get_flag(TIM(BUSVOODOO_LED_TIMER), TIM_SR_CC2IF)) { // red LED timeout
timer_clear_flag(TIM(BUSVOODOO_LED_TIMER), TIM_SR_CC2IF); // clear flag
timer_disable_irq(TIM(BUSVOODOO_LED_TIMER), TIM_DIER_CC2IE); // disable capture 2 for red LED
busvoodoo_global_led_red = false; // remember red LED should be disabled
busvoodoo_led_update(); // update LED status
}
}