From 08768559d05d1a36dc915a0269d78e97528b2383 Mon Sep 17 00:00:00 2001 From: Ethan Zonca Date: Mon, 25 Jan 2016 14:12:13 -0500 Subject: [PATCH] Added nicer status LED handling: LED blinks for every tx/rx and guarantees at least as much off time as on time --- Inc/led.h | 10 ++++++++++ Makefile | 2 +- Src/can.c | 5 +++-- Src/led.c | 35 +++++++++++++++++++++++++++++++++++ Src/main.c | 31 ++++++++++++++++++++++--------- 5 files changed, 71 insertions(+), 12 deletions(-) create mode 100644 Inc/led.h create mode 100644 Src/led.c diff --git a/Inc/led.h b/Inc/led.h new file mode 100644 index 0000000..38d0e0a --- /dev/null +++ b/Inc/led.h @@ -0,0 +1,10 @@ +#ifndef _LED_H +#define _LED_H + + +#define LED_DURATION 50 + +void led_on(void); +void led_process(void); + +#endif diff --git a/Makefile b/Makefile index c47bc2e..3e766b0 100644 --- a/Makefile +++ b/Makefile @@ -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) diff --git a/Src/can.c b/Src/can.c index 1ba3636..04825bf 100644 --- a/Src/can.c +++ b/Src/can.c @@ -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; } diff --git a/Src/led.c b/Src/led.c new file mode 100644 index 0000000..762539f --- /dev/null +++ b/Src/led.c @@ -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(); + } +} + diff --git a/Src/main.c b/Src/main.c index adb3e43..55d1248 100644 --- a/Src/main.c +++ b/Src/main.c @@ -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