2024-10-16 23:51:49 -07:00
|
|
|
/*
|
|
|
|
* rgbled.c
|
|
|
|
*
|
|
|
|
* using TIM2 CH1-CH3
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ch32v20x.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "rtc.h"
|
|
|
|
|
|
|
|
#include "port_pwr.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define RED 0
|
|
|
|
#define GRN 1
|
|
|
|
#define BLU 2
|
|
|
|
|
2024-10-22 23:06:18 -07:00
|
|
|
#define BRT_RED 80
|
|
|
|
#define BRT_GRN 32
|
|
|
|
#define BRT_BLU 36
|
|
|
|
#define BRT_OFF 0
|
2024-10-16 23:51:49 -07:00
|
|
|
|
|
|
|
#define RGBLED_TIM TIM2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static uint8_t flash_timeout[3];
|
|
|
|
static uint8_t flash[3] = {0};
|
|
|
|
static uint8_t state[3] = {0};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void rgbled_set()
|
|
|
|
{
|
|
|
|
uint8_t i;
|
|
|
|
|
|
|
|
// flash counters
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
if (!flash[i]) {
|
|
|
|
flash[i] = flash_timeout[i];
|
|
|
|
} else flash[i]--;
|
|
|
|
|
|
|
|
switch (flash_timeout[i]) {
|
|
|
|
// always on
|
|
|
|
case 0x00: {
|
|
|
|
state[i] = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// always off
|
|
|
|
case 0xff: {
|
|
|
|
state[i] = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// standard
|
|
|
|
default: {
|
|
|
|
if (flash[i] == flash_timeout[i]) {
|
|
|
|
state[i] ^= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-22 23:06:18 -07:00
|
|
|
RGBLED_TIM->CH1CVR = state[BLU] ? BRT_BLU : BRT_OFF;
|
|
|
|
RGBLED_TIM->CH2CVR = state[GRN] ? BRT_GRN : BRT_OFF;
|
|
|
|
RGBLED_TIM->CH3CVR = state[RED] ? BRT_RED : BRT_OFF;
|
2024-10-16 23:51:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void rgbled_update()
|
|
|
|
{
|
|
|
|
// VCR flash if clock isn't set
|
2024-10-22 23:06:18 -07:00
|
|
|
switch (rtc_state) {
|
|
|
|
case RTC_STATE_CLOCK_NOT_SET: {
|
|
|
|
flash_timeout[BLU] = 48;
|
2024-10-16 23:51:49 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
flash_timeout[BLU] = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
flash_timeout[GRN] = gat_pwr_state() ? 0 : 0xff;
|
2024-10-22 23:06:18 -07:00
|
|
|
flash_timeout[RED] = gat_oc_state() ? 16 : 0xff;
|
|
|
|
|
|
|
|
rgbled_set();
|
2024-10-16 23:51:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void rgbled_init()
|
|
|
|
{
|
|
|
|
TIM_TimeBaseInitTypeDef timer ={0};
|
|
|
|
TIM_OCInitTypeDef pwm = {0};
|
|
|
|
|
|
|
|
timer.TIM_Period = (1 << 10) - 1; // 10-bit
|
|
|
|
timer.TIM_Prescaler = 0;
|
|
|
|
timer.TIM_ClockDivision = TIM_CKD_DIV1;
|
|
|
|
timer.TIM_CounterMode = TIM_CounterMode_Up;
|
|
|
|
TIM_TimeBaseInit(RGBLED_TIM, &timer);
|
|
|
|
|
|
|
|
pwm.TIM_OCMode = TIM_OCMode_PWM1;
|
|
|
|
pwm.TIM_OutputState = TIM_OutputState_Enable;
|
2024-10-22 23:06:18 -07:00
|
|
|
pwm.TIM_Pulse = BRT_OFF;
|
|
|
|
pwm.TIM_OCPolarity = TIM_OCPolarity_Low;
|
|
|
|
|
|
|
|
TIM_OC1PreloadConfig(RGBLED_TIM, TIM_OCPreload_Disable);
|
|
|
|
TIM_OC2PreloadConfig(RGBLED_TIM, TIM_OCPreload_Disable);
|
|
|
|
TIM_OC3PreloadConfig(RGBLED_TIM, TIM_OCPreload_Disable);
|
|
|
|
TIM_ARRPreloadConfig(RGBLED_TIM, ENABLE);
|
|
|
|
|
2024-10-16 23:51:49 -07:00
|
|
|
TIM_OC1Init(RGBLED_TIM, &pwm);
|
|
|
|
TIM_OC2Init(RGBLED_TIM, &pwm);
|
|
|
|
TIM_OC3Init(RGBLED_TIM, &pwm);
|
|
|
|
TIM_CtrlPWMOutputs(RGBLED_TIM, ENABLE);
|
|
|
|
|
2024-10-22 23:06:18 -07:00
|
|
|
RGBLED_TIM->CNT = 0;
|
|
|
|
TIM_Cmd(RGBLED_TIM, ENABLE);
|
2024-10-16 23:51:49 -07:00
|
|
|
}
|