#include "pinctrl.h" #include "gpio.h" #include "pwm.h" #include "timer.h" #define LIGHT_GPIO_W GPIO_02 #define LIGHT_GPIO_Y GPIO_08 static uint32_t period_ns = 40000; //80M/2K static uint8_t channel_id_w = 0; static uint8_t channel_id_y = 0; static uint32_t high_ns = 0; static uint32_t low_ns = 0; void hfgpio_pwm_init(void) { high_ns = ((period_ns / 1000) * 0) / 1000; // 高电平时间 low_ns = period_ns - high_ns; // 低电平时间 pwm_config_t cfg_no_repeat = { low_ns, high_ns, 0, 0xFF, true }; channel_id_w = LIGHT_GPIO_W % 8; channel_id_y = LIGHT_GPIO_Y % 8; uapi_pin_set_mode(LIGHT_GPIO_W, PIN_MODE_1); uapi_pin_set_mode(LIGHT_GPIO_Y, PIN_MODE_1); uapi_pwm_deinit(); uapi_pwm_init(); uapi_pwm_open(channel_id_w, &cfg_no_repeat); uapi_pwm_set_group(channel_id_w, &channel_id_w, 1); uapi_pwm_start_group(channel_id_w); uapi_pwm_open(channel_id_y, &cfg_no_repeat); uapi_pwm_set_group(channel_id_y, &channel_id_y, 1); uapi_pwm_start_group(channel_id_y); } static void hfgpio_pwm_update(int fid, uint32_t hrate) { high_ns = ((period_ns / 1000) * hrate) / 1000; // 高电平时间 low_ns = period_ns - high_ns; // 低电平时间 if(fid == LIGHT_GPIO_W){ uapi_pwm_update_duty_ratio(channel_id_w, low_ns, high_ns); uapi_pwm_start_group(channel_id_w); }else if(fid == LIGHT_GPIO_Y){ uapi_pwm_update_duty_ratio(channel_id_y, low_ns, high_ns); uapi_pwm_start_group(channel_id_y); } } static int pwm_hrate = 0; static int dir_flag = 0; static timer_handle_t timer1_handle = 0; static void light_dim_timer_callback(uintptr_t data) { if(data){} if(dir_flag == 0){ pwm_hrate++; if(pwm_hrate == 1000){ dir_flag = 1; } } else if(dir_flag == 1){ pwm_hrate--; if(pwm_hrate == 0){ dir_flag = 0; } } hfgpio_pwm_update(LIGHT_GPIO_W, pwm_hrate); hfgpio_pwm_update(LIGHT_GPIO_Y, pwm_hrate); uapi_timer_start(timer1_handle, 5000, light_dim_timer_callback, 0); } void light_hwtimer_dim_entry(void) { uapi_timer_init(); int ret = 0; ret = uapi_timer_adapter(TIMER_INDEX_1, TIMER_1_IRQN, 1); ret = uapi_timer_create(TIMER_INDEX_1, &timer1_handle); ret = uapi_timer_start(timer1_handle, 1000, light_dim_timer_callback, 0); if(ret != 0) osal_printk("hwtimer start fail\r\n"); else osal_printk("hwtimer start success\r\n"); } void pwm_test(void) { hfgpio_pwm_init(); light_hwtimer_dim_entry(); }