busvoodoo: add led blinking with timeout

This commit is contained in:
King Kévin 2018-01-24 21:28:45 +01:00
parent 513bee9298
commit a91161981c
2 changed files with 100 additions and 0 deletions

View File

@ -16,6 +16,7 @@
* @file busvoodoo_global.c
* @author King Kévin <kingkevin@cuvoodoo.info>
* @date 2018
* @note peripherals used: time @ref busvoodoo_led_timer
*/
/* standard libraries */
@ -24,10 +25,12 @@
#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
@ -35,6 +38,17 @@
#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};
@ -97,6 +111,14 @@ void busvoodoo_setup(void)
}
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)
@ -267,6 +289,60 @@ float busvoodoo_embedded_pullup(bool on)
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
*/
@ -399,3 +475,18 @@ const struct menu_command_t busvoodoo_global_commands[] = {
},
};
/** 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
}
}

View File

@ -16,6 +16,7 @@
* @file busvoodoo_global.h
* @author King Kévin <kingkevin@cuvoodoo.info>
* @date 2018
* @note peripherals used: time @ref busvoodoo_led_timer
*/
/** @defgroup busvoodoo_voltages pin to control voltage regulators and pull-ups definitions
@ -138,3 +139,11 @@ float busvoodoo_vreg_set(uint8_t channel, float voltage);
* @return voltage applied on pull-up resistors, or NaN if channel is invalid (or error happened)
*/
float busvoodoo_embedded_pullup(bool on);
/** enable blue LED for short duration
* @param[in] ms duration in ms (0-32768)
*/
void busvoodoo_led_blue(uint16_t ms);
/** enable red LED for short duration
* @param[in] ms duration in ms (0-32768)
*/
void busvoodoo_led_red(uint16_t ms);