LPT26x-HSF-4MB-Hilink_14.2..../application/ws63/user_main/spotlight/spotlight_main.c
2025-05-13 22:00:58 +08:00

121 lines
3.1 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "hsf.h"
#if defined(CONFIG_PWM_SUPPORT_LPM)
#include "pm_veto.h"
#endif
#include "pinctrl.h"
#include "gpio.h"
#include "pwm.h"
// #include "tcxo.h"
#include "spotlight.h"
#define BRIGHTNESS_PIN GPIO_00
#define CCT_PIN GPIO_04
#define SWITCH_PIN GPIO_13
#define BRIGHTNESS_PWM_CHANNEL 0
#define CCT_PWM_CHANNEL 4
#define CONFIG_PWM_GROUP_ID 0
static errcode_t pwm_sample_callback(uint8_t channel)
{
e_printf("PWM %d, cycle done. \r\n", channel);
return ERRCODE_SUCC;
}
static void gpio_init(pin_t pin)
{
/* PINCTRL init. */
uapi_pin_init();
uapi_pin_set_mode(pin, HAL_PIO_FUNC_GPIO);
uapi_gpio_set_dir(pin, GPIO_DIRECTION_OUTPUT);
uapi_pin_set_ds(pin, PIN_DS_3);
uapi_pin_set_pull(pin, PIN_PULL_TYPE_DOWN);
uapi_gpio_set_val(pin, GPIO_LEVEL_LOW);
}
static void pwm_sys_init() {
uapi_pwm_deinit();
uapi_pwm_init();
}
static void pwm_init(pin_t pin, pin_t pin1, uint8_t channel, uint channel1)
{
pwm_config_t cfg_no_repeat = {
0,
0,
0,
0,
true
};
uint32_t frequency = uapi_pwm_get_frequency(channel);
e_printf("pwm基础时钟频率为: %dHZ\r\n", frequency);
// 计算1KHz PWM所需的周期值高电平+低电平)
uint32_t total_period = frequency / 1000;
// 50%占空比,高低电平时间相等
cfg_no_repeat.high_time = total_period / 2;
cfg_no_repeat.low_time = total_period / 2;
e_printf("设置1KHz PWM50%%占空比,周期值: %d\r\n", total_period);
uapi_pin_set_mode(pin, PIN_MODE_3); //set pin as pwm output
uapi_pin_set_mode(pin1, PIN_MODE_3); //set pin as pwm output
uapi_pwm_open(channel, &cfg_no_repeat);
uapi_pwm_open(channel1, &cfg_no_repeat);
// uapi_tcxo_delay_ms((uint32_t)TEST_TCXO_DELAY_1000MS);
// uapi_pwm_unregister_interrupt(channel);
// uapi_pwm_register_interrupt(channel, pwm_sample_callback);
#ifdef CONFIG_PWM_USING_V151
uint8_t channel_id[2] = {channel, channel1};
/* channel_id can also choose to configure multiple channels, and the third parameter also needs to be adjusted
accordingly. */
uapi_pwm_set_group(CONFIG_PWM_GROUP_ID, channel_id, 2);
#else
uapi_pwm_start(channel);
#endif
}
void pwm_start()
{
#ifdef CONFIG_PWM_USING_V151
/* Here you can also call the uapi_pwm_start interface to open each channel individually. */
uapi_pwm_start_group(CONFIG_PWM_GROUP_ID);
#endif
}
void pwm_sys_deinit(void)
{
// uapi_tcxo_delay_ms((uint32_t)TEST_TCXO_DELAY_1000MS);
#ifdef CONFIG_PWM_USING_V151
uapi_pwm_close(CONFIG_PWM_GROUP_ID);
#else
uapi_pwm_close(CONFIG_PWM_CHANNEL);
#endif
// uapi_tcxo_delay_ms((uint32_t)TEST_TCXO_DELAY_1000MS);
uapi_pwm_deinit();
}
static void pwm_entry(void)
{
pwm_sys_init();
pwm_init(BRIGHTNESS_PIN, BRIGHTNESS_PWM_CHANNEL, CCT_PIN, CCT_PWM_CHANNEL);
pwm_start();
}
int spotlight_main(void) {
gpio_init(SWITCH_PIN);
pwm_entry();
while (1) {
e_printf("spotlight_main\r\n");
uapi_gpio_toggle(SWITCH_PIN);
msleep(1000);
}
return 0;
}