Added nicer status LED handling: LED blinks for every tx/rx and guarantees at least as much off time as on time

This commit is contained in:
Ethan Zonca 2016-01-25 14:12:13 -05:00
parent 2c648e49ee
commit 08768559d0
5 changed files with 71 additions and 12 deletions

10
Inc/led.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef _LED_H
#define _LED_H
#define LED_DURATION 50
void led_on(void);
void led_process(void);
#endif

View File

@ -11,7 +11,7 @@
BUILD_NUMBER ?= 0
# SOURCES: list of sources in the user application
SOURCES = main.c usbd_conf.c usbd_cdc_if.c usb_device.c usbd_desc.c stm32f0xx_hal_msp.c stm32f0xx_it.c system_stm32f0xx.c can.c slcan.c
SOURCES = main.c usbd_conf.c usbd_cdc_if.c usb_device.c usbd_desc.c stm32f0xx_hal_msp.c stm32f0xx_it.c system_stm32f0xx.c can.c slcan.c led.c
# TARGET: name of the user application
TARGET = CANtact-b$(BUILD_NUMBER)

View File

@ -1,5 +1,6 @@
#include "stm32f0xx_hal.h"
#include "can.h"
#include "led.h"
CAN_HandleTypeDef hcan;
CAN_FilterConfTypeDef filter;
@ -113,7 +114,7 @@ uint32_t can_tx(CanTxMsgTypeDef *tx_msg, uint32_t timeout) {
hcan.pTxMsg = tx_msg;
status = HAL_CAN_Transmit(&hcan, timeout);
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_1);
led_on();
return status;
}
@ -124,7 +125,7 @@ uint32_t can_rx(CanRxMsgTypeDef *rx_msg, uint32_t timeout) {
status = HAL_CAN_Receive(&hcan, CAN_FIFO0, timeout);
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_1);
led_on();
return status;
}

35
Src/led.c Normal file
View File

@ -0,0 +1,35 @@
//
// LED: Handles blinking of status light
//
#include "stm32f0xx_hal.h"
#include "led.h"
static uint32_t led_laston = 0;
static uint32_t led_lastoff = 0;
// Attempt to turn on status LED
void led_on(void)
{
// Make sure the LED has been off for at least LED_DURATION before turning on again
// This prevents a solid status LED on a busy canbus
if(led_laston == 0 && HAL_GetTick() - led_lastoff > LED_DURATION)
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, 1);
led_laston = HAL_GetTick();
}
}
// Process time-based LED events
void led_process(void)
{
// If LED has been on for long enough, turn it off
if(led_laston > 0 && HAL_GetTick() - led_laston > LED_DURATION)
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, 0);
led_laston = 0;
led_lastoff = HAL_GetTick();
}
}

View File

@ -40,9 +40,10 @@
#include "usbd_cdc_if.h"
#include "can.h"
#include "slcan.h"
#include "led.h"
//#define INTERNAL_OSCILLATOR
#define EXTERNAL_OSCILLATOR
#define INTERNAL_OSCILLATOR
//#define EXTERNAL_OSCILLATOR
/* USER CODE END Includes */
@ -65,6 +66,7 @@ static void led_init(void);
/* USER CODE END 0 */
volatile int i=0;
int main(void)
{
@ -98,7 +100,11 @@ int main(void)
// blink red LED for test
uint32_t count;
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_SET);
for (count=0; count < 200000; count++) { __asm("nop");}
HAL_Delay(100);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_RESET);
HAL_Delay(100);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_SET);
HAL_Delay(100);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_RESET);
// loop forever
@ -106,13 +112,16 @@ int main(void)
uint32_t status;
uint8_t msg_buf[SLCAN_MTU];
for (;;) {
while (!is_can_msg_pending());
status = can_rx(&rx_msg, 3);
if (status == HAL_OK) {
status = slcan_parse_frame(&msg_buf, &rx_msg);
CDC_Transmit_FS(msg_buf, status);
}
while (!is_can_msg_pending())
led_process();
status = can_rx(&rx_msg, 3);
if (status == HAL_OK) {
status = slcan_parse_frame(&msg_buf, &rx_msg);
CDC_Transmit_FS(msg_buf, status);
}
led_process();
}
/* USER CODE END 3 */
@ -177,6 +186,10 @@ void SystemClock_Config(void)
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
__SYSCFG_CLK_ENABLE();
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}
/** Configure pins as