forked from achamaikin/CCSModuleSW30Web
added WS2812 support
This commit is contained in:
@@ -332,8 +332,9 @@ PC5.GPIOParameters=GPIO_Label
|
||||
PC5.GPIO_Label=LOCK_B
|
||||
PC5.Locked=true
|
||||
PC5.Signal=GPIO_Output
|
||||
PC9.GPIOParameters=GPIO_Label
|
||||
PC9.GPIOParameters=GPIO_Speed,GPIO_Label
|
||||
PC9.GPIO_Label=LED_DATA
|
||||
PC9.GPIO_Speed=GPIO_SPEED_FREQ_HIGH
|
||||
PC9.Locked=true
|
||||
PC9.Signal=GPIO_Output
|
||||
PD0.Locked=true
|
||||
|
||||
@@ -41,3 +41,5 @@ void LED_Task();
|
||||
void LED_Write();
|
||||
void LED_Init();
|
||||
void LED_SetColor(RGB_Cycle_t *color);
|
||||
void RGB_SetColor(RGB_t *color);
|
||||
void WS2812_SetColor(RGB_t *color);
|
||||
|
||||
+5
-5
@@ -54,15 +54,15 @@ void MX_GPIO_Init(void)
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(GPIOD, RELAY_DC_Pin|USART2_DIR_Pin, GPIO_PIN_RESET);
|
||||
|
||||
/*Configure GPIO pin : DBG1_Pin */
|
||||
GPIO_InitStruct.Pin = DBG1_Pin;
|
||||
/*Configure GPIO pins : DBG1_Pin LED_DATA_Pin */
|
||||
GPIO_InitStruct.Pin = DBG1_Pin|LED_DATA_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
||||
HAL_GPIO_Init(DBG1_GPIO_Port, &GPIO_InitStruct);
|
||||
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : RELAY_CP_Pin LOCK_A_Pin LOCK_B_Pin LED_DATA_Pin */
|
||||
GPIO_InitStruct.Pin = RELAY_CP_Pin|LOCK_A_Pin|LOCK_B_Pin|LED_DATA_Pin;
|
||||
/*Configure GPIO pins : RELAY_CP_Pin LOCK_A_Pin LOCK_B_Pin */
|
||||
GPIO_InitStruct.Pin = RELAY_CP_Pin|LOCK_A_Pin|LOCK_B_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
|
||||
@@ -6,6 +6,15 @@
|
||||
#include "tim.h"
|
||||
#include <string.h>
|
||||
|
||||
/* Второй светодиод в цепочке WS2812 */
|
||||
static RGB_t ws2812_led1 = { .R = 0, .G = 0, .B = 0 };
|
||||
|
||||
#define WS2812_ERROR_BLINK_MS 100
|
||||
#define WS2812_ERROR_BLINK_PAUSE 30
|
||||
|
||||
/* Яркость обоих WS2812 на плате, 0..255 */
|
||||
#define WS2812_BRIGHTNESS 60
|
||||
|
||||
RGB_State_t LED_State;
|
||||
RGB_Cycle_t LED_Cycle;
|
||||
|
||||
@@ -179,12 +188,149 @@ void interpolateColors(RGB_t* color1, RGB_t* color2, uint16_t a, uint16_t b, RGB
|
||||
}
|
||||
|
||||
|
||||
#pragma GCC push_options
|
||||
#pragma GCC optimize("O2")
|
||||
|
||||
|
||||
#define WS2812_T0H_NOP 25
|
||||
#define WS2812_T0L_NOP 52
|
||||
#define WS2812_T1H_NOP 52
|
||||
#define WS2812_T1L_NOP 25
|
||||
|
||||
#define _WS2812_DELAY_NOPS(n) __asm volatile(".rept " #n "\nnop\n.endr" ::: "memory")
|
||||
#define WS2812_DELAY_NOPS(n) _WS2812_DELAY_NOPS(n)
|
||||
|
||||
static void ws2812_send_pixel(uint8_t r, uint8_t g, uint8_t b)
|
||||
{
|
||||
uint32_t tmp = ~(((uint32_t)g << 16) | ((uint32_t)r << 8) | (uint32_t)b);
|
||||
|
||||
for (int i = 0; i < 24; i++) {
|
||||
LED_DATA_GPIO_Port->BSRR = LED_DATA_Pin;
|
||||
if (tmp & (1U << 23)) {
|
||||
WS2812_DELAY_NOPS(WS2812_T0H_NOP);
|
||||
LED_DATA_GPIO_Port->BRR = LED_DATA_Pin;
|
||||
WS2812_DELAY_NOPS(WS2812_T0L_NOP);
|
||||
} else {
|
||||
WS2812_DELAY_NOPS(WS2812_T1H_NOP);
|
||||
LED_DATA_GPIO_Port->BRR = LED_DATA_Pin;
|
||||
WS2812_DELAY_NOPS(WS2812_T1L_NOP);
|
||||
}
|
||||
tmp <<= 1;
|
||||
}
|
||||
}
|
||||
|
||||
static void ws2812_update(RGB_t *led0, RGB_t *led1)
|
||||
{
|
||||
uint32_t primask = __get_PRIMASK();
|
||||
|
||||
__disable_irq();
|
||||
ws2812_send_pixel(led0->R, led0->G, led0->B);
|
||||
ws2812_send_pixel(led1->R, led1->G, led1->B);
|
||||
__set_PRIMASK(primask);
|
||||
}
|
||||
|
||||
#pragma GCC pop_options
|
||||
|
||||
static RGB_t RGB_ScaleBrightness(const RGB_t *color)
|
||||
{
|
||||
RGB_t out = {
|
||||
.R = (uint8_t)((uint16_t)color->R * WS2812_BRIGHTNESS / 255),
|
||||
.G = (uint8_t)((uint16_t)color->G * WS2812_BRIGHTNESS / 255),
|
||||
.B = (uint8_t)((uint16_t)color->B * WS2812_BRIGHTNESS / 255),
|
||||
};
|
||||
return out;
|
||||
}
|
||||
|
||||
/*
|
||||
* Моргание красным на втором WS2812 при chargingError > 0.
|
||||
* Логика как Slave-LB24 LED_Task: N вспышек = код ошибки, пауза 30×100 ms.
|
||||
* Вызывается из LED_Task каждые 20 ms; шаг FSM — 100 ms.
|
||||
*/
|
||||
static void LED_Ws2812ErrorBlinkUpdate(void)
|
||||
{
|
||||
static uint8_t err_counter;
|
||||
static uint8_t led_state;
|
||||
static uint8_t led_pause;
|
||||
static uint32_t blink_tick;
|
||||
static uint32_t flash_until;
|
||||
static CONN_Error_t last_error = CONN_NO_ERROR;
|
||||
|
||||
const CONN_Error_t err = CONN.chargingError;
|
||||
const uint32_t now = HAL_GetTick();
|
||||
|
||||
if (err == CONN_NO_ERROR) {
|
||||
err_counter = 0;
|
||||
led_state = 0;
|
||||
led_pause = 0;
|
||||
flash_until = 0;
|
||||
last_error = CONN_NO_ERROR;
|
||||
ws2812_led1.R = 0;
|
||||
ws2812_led1.G = 0;
|
||||
ws2812_led1.B = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
if (err != last_error) {
|
||||
err_counter = 0;
|
||||
led_state = 0;
|
||||
led_pause = 0;
|
||||
flash_until = 0;
|
||||
last_error = err;
|
||||
}
|
||||
|
||||
if (now < flash_until) {
|
||||
ws2812_led1.R = 255;
|
||||
ws2812_led1.G = 0;
|
||||
ws2812_led1.B = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
ws2812_led1.R = 0;
|
||||
ws2812_led1.G = 0;
|
||||
ws2812_led1.B = 0;
|
||||
|
||||
if ((now - blink_tick) < WS2812_ERROR_BLINK_MS) {
|
||||
return;
|
||||
}
|
||||
blink_tick = now;
|
||||
|
||||
uint8_t led_flash = 0;
|
||||
|
||||
if (led_pause > 0) {
|
||||
led_pause--;
|
||||
} else if (err_counter < (uint8_t)err) {
|
||||
if (led_state == 0) {
|
||||
led_state = 1;
|
||||
} else {
|
||||
led_flash = 1;
|
||||
led_state = 0;
|
||||
err_counter++;
|
||||
}
|
||||
} else {
|
||||
err_counter = 0;
|
||||
led_pause = WS2812_ERROR_BLINK_PAUSE;
|
||||
}
|
||||
|
||||
if (led_flash) {
|
||||
flash_until = now + WS2812_ERROR_BLINK_MS;
|
||||
ws2812_led1.R = 255;
|
||||
ws2812_led1.G = 0;
|
||||
ws2812_led1.B = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void RGB_SetColor(RGB_t *color){
|
||||
htim4.Instance->CCR2 = color->R * 100 / 255;
|
||||
htim4.Instance->CCR3 = color->G * 100 / 255;
|
||||
htim4.Instance->CCR4 = color->B * 100 / 255;
|
||||
}
|
||||
|
||||
void WS2812_SetColor(RGB_t *color){
|
||||
RGB_t led0 = RGB_ScaleBrightness(color);
|
||||
RGB_t led1 = RGB_ScaleBrightness(&ws2812_led1);
|
||||
ws2812_update(&led0, &led1);
|
||||
}
|
||||
|
||||
void LED_SetColor(RGB_Cycle_t *color){
|
||||
memcpy(&LED_Cycle, color, sizeof(RGB_Cycle_t));
|
||||
}
|
||||
@@ -196,6 +342,7 @@ void LED_Init(){
|
||||
HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_3);
|
||||
HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_4);
|
||||
RGB_SetColor(&color);
|
||||
WS2812_SetColor(&color);
|
||||
}
|
||||
|
||||
void LED_Task(){
|
||||
@@ -239,6 +386,8 @@ void LED_Task(){
|
||||
default:
|
||||
LED_State.state = LED_RISING;
|
||||
}
|
||||
LED_Ws2812ErrorBlinkUpdate();
|
||||
RGB_SetColor(&LED_State.color);
|
||||
WS2812_SetColor(&LED_State.color);
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
Binary file not shown.
+3404
-3341
File diff suppressed because it is too large
Load Diff
+20710
-20089
File diff suppressed because it is too large
Load Diff
+1213
-1183
File diff suppressed because it is too large
Load Diff
+3404
-3340
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,11 @@
|
||||
../Core/Src/rgb_controller.c:102:6:LED_Write 18
|
||||
../Core/Src/rgb_controller.c:164:6:interpolateColors 3
|
||||
../Core/Src/rgb_controller.c:182:6:RGB_SetColor 1
|
||||
../Core/Src/rgb_controller.c:188:6:LED_SetColor 1
|
||||
../Core/Src/rgb_controller.c:193:6:LED_Init 1
|
||||
../Core/Src/rgb_controller.c:201:6:LED_Task 10
|
||||
../Core/Src/rgb_controller.c:111:6:LED_Write 18
|
||||
../Core/Src/rgb_controller.c:173:6:interpolateColors 3
|
||||
../Core/Src/rgb_controller.c:203:13:ws2812_send_pixel 3
|
||||
../Core/Src/rgb_controller.c:222:13:ws2812_update 1
|
||||
../Core/Src/rgb_controller.c:234:14:RGB_ScaleBrightness 1
|
||||
../Core/Src/rgb_controller.c:249:13:LED_Ws2812ErrorBlinkUpdate 9
|
||||
../Core/Src/rgb_controller.c:322:6:RGB_SetColor 1
|
||||
../Core/Src/rgb_controller.c:328:6:WS2812_SetColor 1
|
||||
../Core/Src/rgb_controller.c:334:6:LED_SetColor 1
|
||||
../Core/Src/rgb_controller.c:339:6:LED_Init 1
|
||||
../Core/Src/rgb_controller.c:348:6:LED_Task 10
|
||||
|
||||
Reference in New Issue
Block a user