PWM DEMO #include <string.h> #include "om_driver.h" /// Test mux for timer counter #define PAD_TIM0_COUNT 14 #define MUX_TIM0_COUNT PINMUX_PAD14_GPIO_MODE_CFG static void tim_count_callback(void *om_tim, drv_event_t event, void *buff, void *num) { if (event == DRV_EVENT_TIMER_UPDATE) { drv_gpio_toggle(OM_GPIO0, BITMASK(PAD_TIM0_COUNT)); } } void example_tim0_count(void) { pin_config_t pin_cfg[] = { {PAD_TIM0_COUNT, {MUX_TIM0_COUNT}, PMU_PIN_MODE_PP, PMU_PIN_DRIVER_CURRENT_NORMAL}, }; gpio_config_t gpio_cfg[] = { {OM_GPIO0, PAD_TIM0_COUNT, GPIO_DIR_OUTPUT, GPIO_LEVEL_LOW, GPIO_TRIG_NONE}, }; drv_pin_init(pin_cfg, sizeof(pin_cfg) / sizeof(pin_cfg[0])); drv_gpio_init(gpio_cfg, sizeof(gpio_cfg) / sizeof(gpio_cfg[0])); drv_tim_init(OM_TIM0); drv_tim_register_event_callback(OM_TIM0, tim_count_callback); tim_gp_config_t tim_cfg = { .period_us = 1000 }; drv_tim_gp_start(OM_TIM0, &tim_cfg); /* if you want to stop the timer, just use drv_tim_gp_stop(OM_TIMx) */ } //------------------------------------ /// Test pad for timer pwm #define PAD_TIM1_PWM_CH1 12 #define MUX_TIM1_PWM_CH1 PINMUX_PAD12_TIMER1_OUT0_CFG //----------------------------------------------------------- void example_tim_pwm(void) { pin_config_t pin_cfg[] = { {PAD_TIM1_PWM_CH1, {MUX_TIM1_PWM_CH1}, PMU_PIN_MODE_PP, PMU_PIN_DRIVER_CURRENT_NORMAL}, }; drv_pin_init(pin_cfg, sizeof(pin_cfg) / sizeof(pin_cfg[0])); tim_pwm_output_config_t pwm_config = { .cnt_freq = 32 * 1000 * 1000, // 1Mhz .chan = { {0, {TIM_PWM_POL_ACTIVE_HIGH, 0}}, {0, {TIM_PWM_POL_ACTIVE_HIGH, 0}}, {0, {TIM_PWM_POL_ACTIVE_HIGH, 0}}, {0, {TIM_PWM_POL_ACTIVE_HIGH, 0}} }, .dma_cfg = { 0, TIM_CHAN_ALL, NULL }, }; pwm_config.period_cnt = 68; pwm_config.chan[TIM_CHAN_1].en = 1; pwm_config.chan[TIM_CHAN_1].cfg.oc_val = 34; pwm_config.chan[TIM_CHAN_1].cfg.pol = TIM_PWM_POL_ACTIVE_HIGH; drv_tim_init(OM_TIM1); drv_tim_pwm_output_start(OM_TIM1, &pwm_config); /** * if you want to stop the timer, just use drv_tim_pwm_output_stop(...). * if you want to change the output compare value, just use drv_tim_pwm_change_oc_val(...) */ } /// Test pad for timer counter #define PAD_TIM2_PWM_CH4 11 #define MUX_TIM2_PWM_CH4 PINMUX_PAD11_TIMER2_OUT3_CFG //--------------------------------------------------------------- void example_tim2_pwm(void) { pin_config_t pin_cfg[] = { {PAD_TIM2_PWM_CH4, {MUX_TIM2_PWM_CH4}, PMU_PIN_MODE_PP, PMU_PIN_DRIVER_CURRENT_NORMAL}, }; drv_pin_init(pin_cfg, sizeof(pin_cfg) / sizeof(pin_cfg[0])); tim_pwm_output_config_t pwm_config = { .cnt_freq = 32 * 1000 * 1000, // 1Mhz .chan = { {0, {TIM_PWM_POL_ACTIVE_HIGH, 0}}, {0, {TIM_PWM_POL_ACTIVE_HIGH, 0}}, {0, {TIM_PWM_POL_ACTIVE_HIGH, 0}}, {0, {TIM_PWM_POL_ACTIVE_HIGH, 0}} }, .dma_cfg = { 0, TIM_CHAN_ALL, NULL }, }; pwm_config.period_cnt = 66; pwm_config.chan[TIM_CHAN_4].en = 1; pwm_config.chan[TIM_CHAN_4].cfg.oc_val = 33; pwm_config.chan[TIM_CHAN_4].cfg.pol = TIM_PWM_POL_ACTIVE_HIGH; drv_tim_init(OM_TIM2); drv_tim_pwm_output_start(OM_TIM2, &pwm_config); /** * if you want to stop the timer, just use drv_tim_pwm_output_stop(...). * if you want to change the output compare value, just use drv_tim_pwm_change_oc_val(...) */ } // main 函数中引用 void timer_test(void) { //TIM1 470Khz,GPIO12,TIM1,CH1 example_tim_pwm(); //TIM2 485Khz,GPIO11,TIME2,CH4 example_tim2_pwm(); //TIM0 1ms,GPIO14,GPIO Togle example_tim0_count(); } |