Updated WS2812 driver to control manualy when buffer is transfered to DMA, enabled CAN as well
This commit is contained in:
parent
0a5c96959c
commit
7de9180f80
|
@ -14,10 +14,8 @@ extern "C" {
|
|||
#include <Def/SCREEN_Def.h>
|
||||
|
||||
|
||||
#define GPS_ENABLED 0
|
||||
#define CAN_ENABLED 0
|
||||
#define CAN_ENABLED 1
|
||||
#define RGB_ENABLED 1
|
||||
#define UART_ENABLED 0
|
||||
|
||||
#define CAN_SETUP_ID 0x580
|
||||
|
||||
|
|
|
@ -6,13 +6,14 @@
|
|||
* @author Benas Brazdziunas
|
||||
*/
|
||||
|
||||
#include "WS2812/WS2812.hpp"
|
||||
#include "WS2812.hpp"
|
||||
|
||||
|
||||
static WS2812_RGB_t WS2812_LED_BUF[WS2812_LED_N];
|
||||
static uint32_t WS2812_TIM_BUF[WS2812_BUFLEN];
|
||||
static uint8_t WS2812_BRIGHTNESS = 5;
|
||||
static bool WS2812_INVERT_ORDER = true;
|
||||
static bool WS2812_DMA_READY = false;
|
||||
|
||||
TIM_HandleTypeDef WS2812_PWM_DRIVER;
|
||||
DMA_HandleTypeDef WS2812_PWM_DMA;
|
||||
|
@ -21,12 +22,14 @@ extern void DMA1_Stream1_IRQHandler(void);
|
|||
|
||||
void initWS2812()
|
||||
{
|
||||
ConfigureTimerPeripheral();
|
||||
ConfigureTimerGPIO();
|
||||
ConfigureTimerChannel();
|
||||
|
||||
HAL_TIM_PWM_Start_DMA(&WS2812_PWM_DRIVER, WS2812_PWM_TIM_CH, (uint32_t *)WS2812_TIM_BUF, WS2812_BUFLEN);
|
||||
uint32_t freq = HAL_RCC_GetHCLKFreq();
|
||||
ConfigureTimerGPIO();
|
||||
ConfigureTimerPeripheral();
|
||||
ConfigureDMA();
|
||||
ConfigureTimerChannel();
|
||||
//HAL_TIM_PWM_Start(&WS2812_PWM_DRIVER, WS2812_PWM_TIM_CH);
|
||||
HAL_TIM_PWM_Start_DMA(&WS2812_PWM_DRIVER, WS2812_PWM_TIM_CH, (uint32_t *)WS2812_TIM_BUF, WS2812_BUFLEN);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -88,7 +91,9 @@ void setWS2812Brightness(uint8_t num)
|
|||
*/
|
||||
void updateWS2812()
|
||||
{
|
||||
uint32_t pos = WS2812_INVERT_ORDER ? (WS2812_LED_N * 24) : 0;
|
||||
while(!WS2812_DMA_READY);
|
||||
|
||||
uint32_t pos = WS2812_INVERT_ORDER ? (WS2812_LED_N * 24) : 0;
|
||||
|
||||
for (uint32_t num = 0; num < WS2812_LED_N; num++)
|
||||
{
|
||||
|
@ -132,6 +137,7 @@ void updateWS2812()
|
|||
WS2812_TIM_BUF[pos--] = ((led.green & 0x01) != 0) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
|
||||
|
||||
} else {
|
||||
|
||||
// Col:Green , Bit:7..0
|
||||
WS2812_TIM_BUF[pos++] = ((led.green & 0x80) != 0) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
|
||||
WS2812_TIM_BUF[pos++] = ((led.green & 0x40) != 0) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
|
||||
|
@ -162,17 +168,17 @@ void updateWS2812()
|
|||
WS2812_TIM_BUF[pos++] = ((led.blue & 0x02) != 0) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
|
||||
WS2812_TIM_BUF[pos++] = ((led.blue & 0x01) != 0) ? WS2812_DUTYCYCLE_1 : WS2812_DUTYCYCLE_0;
|
||||
}
|
||||
|
||||
}
|
||||
HAL_TIM_PWM_Start_DMA(&WS2812_PWM_DRIVER, WS2812_PWM_TIM_CH, (uint32_t *)WS2812_TIM_BUF, WS2812_BUFLEN);
|
||||
WS2812_DMA_READY = false;
|
||||
}
|
||||
|
||||
void ConfigureTimerPeripheral()
|
||||
{
|
||||
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
|
||||
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
||||
TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
|
||||
|
||||
__HAL_RCC_TIM2_CLK_ENABLE();
|
||||
|
||||
WS2812_PWM_DRIVER.Instance = WS2812_PWM_TIMER;
|
||||
WS2812_PWM_DRIVER.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
WS2812_PWM_DRIVER.Init.RepetitionCounter = 0;
|
||||
|
@ -186,6 +192,7 @@ void ConfigureTimerPeripheral()
|
|||
return;
|
||||
}
|
||||
|
||||
|
||||
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
|
||||
if (HAL_TIM_ConfigClockSource(&WS2812_PWM_DRIVER, &sClockSourceConfig) != HAL_OK)
|
||||
{
|
||||
|
@ -202,6 +209,8 @@ void ConfigureTimerPeripheral()
|
|||
|
||||
void ConfigureTimerGPIO(void)
|
||||
{
|
||||
|
||||
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
|
||||
|
@ -240,7 +249,6 @@ void ConfigureDMA(void)
|
|||
|
||||
/* Enable DMA clock */
|
||||
__HAL_RCC_DMA1_CLK_ENABLE();
|
||||
|
||||
/*##- 3- Configure DMA #####################################################*/
|
||||
/*********************** Configure DMA parameters ***************************/
|
||||
WS2812_PWM_DMA.Instance = DMA1_Stream1;
|
||||
|
@ -251,22 +259,24 @@ void ConfigureDMA(void)
|
|||
WS2812_PWM_DMA.Init.MemInc = DMA_MINC_ENABLE;
|
||||
WS2812_PWM_DMA.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
|
||||
WS2812_PWM_DMA.Init.MemDataAlignment = DMA_PDATAALIGN_WORD;
|
||||
WS2812_PWM_DMA.Init.Mode = DMA_CIRCULAR;
|
||||
WS2812_PWM_DMA.Init.Priority = DMA_PRIORITY_VERY_HIGH;
|
||||
WS2812_PWM_DMA.Init.Mode = DMA_NORMAL;
|
||||
WS2812_PWM_DMA.Init.Priority = DMA_PRIORITY_LOW;
|
||||
WS2812_PWM_DMA.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
|
||||
|
||||
/* Associate the DMA handle */
|
||||
__HAL_LINKDMA(&WS2812_PWM_DRIVER, hdma[TIM_DMA_ID_CC3], WS2812_PWM_DMA);
|
||||
|
||||
/* Initialize DMA handle */
|
||||
//
|
||||
// /* Initialize DMA handle */
|
||||
HAL_DMA_Init(WS2812_PWM_DRIVER.hdma[TIM_DMA_ID_CC3]);
|
||||
|
||||
/* NVIC configuration for DMA transfer complete interrupt */
|
||||
HAL_NVIC_SetPriority(DMA1_Stream1_IRQn, 5, 0);
|
||||
//
|
||||
// /* NVIC configuration for DMA transfer complete interrupt */
|
||||
HAL_NVIC_SetPriority(DMA1_Stream1_IRQn, 0, 0);
|
||||
HAL_NVIC_EnableIRQ(DMA1_Stream1_IRQn);
|
||||
//
|
||||
}
|
||||
|
||||
void DMA1_Stream1_IRQHandler(void)
|
||||
{
|
||||
WS2812_DMA_READY = true;
|
||||
HAL_DMA_IRQHandler(WS2812_PWM_DRIVER.hdma[TIM_DMA_ID_CC3]);
|
||||
}
|
||||
|
|
|
@ -18,30 +18,29 @@ extern "C"
|
|||
#include "stm32f4xx_hal.h"
|
||||
|
||||
|
||||
#define CONCAT_SYMBOLS(s1, s2) s1##s2
|
||||
#define CONCAT_EXPANDED_SYMBOLS(s1, s2) CONCAT_SYMBOLS(s1, s2)
|
||||
|
||||
#define WS2812_LED_N 22 // Number of LEDs
|
||||
#define WS2812_PORT GPIOA
|
||||
#define WS2812_PIN 2
|
||||
#define WS2812_TIM_N 2 // timer, 1-11
|
||||
#define WS2812_TIM_CH 3 // timer channel, 0-3
|
||||
|
||||
#define WS2812_PWM_FREQUENCY (HAL_RCC_GetHCLKFreq() / 2) /**< Clock frequency of PWM, must be valid with respect to system clock! */
|
||||
#define WS2812_PWM_PERIOD (WS2812_PWM_FREQUENCY / 800000)
|
||||
|
||||
#define WS2812_DUTYCYCLE_0 (WS2812_PWM_FREQUENCY / (1000000000 / 220))
|
||||
#define WS2812_DUTYCYCLE_1 (WS2812_PWM_FREQUENCY / (1000000000 / 580))
|
||||
|
||||
#define CONCAT_SYMBOLS(s1, s2) s1##s2
|
||||
#define CONCAT_EXPANDED_SYMBOLS(s1, s2) CONCAT_SYMBOLS(s1, s2)
|
||||
|
||||
#define WS2812_PWM_TIMER CONCAT_EXPANDED_SYMBOLS(TIM, WS2812_TIM_N)
|
||||
#define WS2812_PWM_TIM_CH CONCAT_EXPANDED_SYMBOLS(TIM_CHANNEL_, WS2812_TIM_CH)
|
||||
#define WS2812_PWM_DRIVER CONCAT_EXPANDED_SYMBOLS(htim, WS2812_TIM_N)
|
||||
#define WS2812_PWM_PIN CONCAT_EXPANDED_SYMBOLS(GPIO_PIN_, WS2812_PIN)
|
||||
|
||||
|
||||
|
||||
#define WS2812_BUFLEN ((WS2812_LED_N + 4) * 24)
|
||||
|
||||
#define WS2812_PWM_FREQUENCY (HAL_RCC_GetHCLKFreq() / 2)
|
||||
#define WS2812_PWM_PERIOD (WS2812_PWM_FREQUENCY / 800000)
|
||||
|
||||
#define WS2812_DUTYCYCLE_0 (WS2812_PWM_FREQUENCY / (1000000000 / 300))
|
||||
#define WS2812_DUTYCYCLE_1 (WS2812_PWM_FREQUENCY / (1000000000 / 700))
|
||||
|
||||
|
||||
typedef struct {
|
||||
uint8_t red;
|
||||
uint8_t green;
|
||||
|
@ -56,7 +55,6 @@ void setWS2812One(uint32_t num, WS2812_RGB_t rgb_col);
|
|||
void setWS2812All(WS2812_RGB_t rgb_col);
|
||||
void setWS2812Brightness(uint8_t num);
|
||||
|
||||
//void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);
|
||||
void DMA1_Stream1_IRQHandler(void);
|
||||
|
||||
void updateWS2812();
|
||||
|
@ -66,6 +64,7 @@ void ConfigureTimerGPIO(void);
|
|||
void ConfigureTimerChannel(void);
|
||||
void ConfigureDMA(void);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
|
@ -946,8 +946,8 @@ void Start_CAN_Task(void *argument)
|
|||
Current_Status.PRES_UNIT = kPa;
|
||||
Current_Status.TEMP_UNIT = C;
|
||||
Current_Status.SPEED_UNIT = Kmh;
|
||||
HAL_GPIO_WritePin(CAN1_S0_GPIO_Port, CAN1_S0_Pin, SET);
|
||||
HAL_GPIO_WritePin(CAN2_S0_GPIO_Port, CAN2_S0_Pin, SET);
|
||||
//HAL_GPIO_WritePin(CAN1_S0_GPIO_Port, CAN1_S0_Pin, SET);
|
||||
//HAL_GPIO_WritePin(CAN2_S0_GPIO_Port, CAN2_S0_Pin, SET);
|
||||
|
||||
//HAL_GPIO_WritePin(LED_CAN1_GPIO_Port, LED_CAN1_Pin, SET);
|
||||
|
||||
|
@ -1528,11 +1528,13 @@ void Start_ALERT_Task(void *argument)
|
|||
void Start_RGB_Task(void *argument)
|
||||
{
|
||||
/* USER CODE BEGIN Start_RGB_Task */
|
||||
|
||||
setWS2812Brightness(5);
|
||||
/* Infinite loop */
|
||||
for (;;) {
|
||||
if (RGB_ENABLED) {
|
||||
Current_Status.RPM = Current_Status.RPM >= PROTECTION_RPM_HIGH ? 0 : Current_Status.RPM;
|
||||
Current_Status.RPM = Current_Status.RPM + 100;
|
||||
//Current_Status.RPM = Current_Status.RPM >= PROTECTION_RPM_HIGH ? 0 : Current_Status.RPM;
|
||||
//Current_Status.RPM = Current_Status.RPM + 100;
|
||||
|
||||
Current_Status.ENGINE_PROTECTION = Current_Status.RPM >= PROTECTION_RPM_HIGH ? 1 : 0;
|
||||
|
||||
|
|
Loading…
Reference in New Issue