From 45bad8b2f85dee6c8c09f2e9a2c7c670583c0518 Mon Sep 17 00:00:00 2001 From: jzlv Date: Mon, 26 Jul 2021 10:47:31 +0800 Subject: [PATCH] [refactor][examples/timer] refactor timer demo --- examples/timer/timer_basic/main.c | 49 +++++-------------------------- examples/timer/timer_int/main.c | 41 ++++++++++---------------- 2 files changed, 22 insertions(+), 68 deletions(-) diff --git a/examples/timer/timer_basic/main.c b/examples/timer/timer_basic/main.c index 3d07ebfb..4f018997 100644 --- a/examples/timer/timer_basic/main.c +++ b/examples/timer/timer_basic/main.c @@ -20,61 +20,26 @@ * under the License. * */ - -#include "hal_timer.h" #include "bflb_platform.h" +#include "hal_timer.h" -volatile uint32_t cnt = 0; int main(void) { bflb_platform_init(0); - timer_user_cfg_t timer_user_cfg0; - timer_user_cfg0.timeout_val = 1000 * 10; /* us */ - timer_user_cfg0.comp_it = TIMER_COMP0_IT; + timer_register(TIMER1_INDEX, "timer1"); - timer_user_cfg_t timer_user_cfg1; - timer_user_cfg1.timeout_val = 1000 * 100; /* us */ - timer_user_cfg1.comp_it = TIMER_COMP1_IT; + struct device *timer1 = device_find("timer1"); - timer_user_cfg_t timer_user_cfg2; - timer_user_cfg2.timeout_val = 1000 * 1000; /* us */ - timer_user_cfg2.comp_it = TIMER_COMP2_IT; - - timer_register(TIMER_CH1_INDEX, "timer_ch1"); - - struct device *timer_ch1 = device_find("timer_ch1"); - - if (timer_ch1) { - device_open(timer_ch1, DEVICE_OFLAG_POLL); - device_control(timer_ch1, DEVICE_CTRL_TIMER_CH_START, (void *)(&timer_user_cfg0)); - device_control(timer_ch1, DEVICE_CTRL_TIMER_CH_START, (void *)(&timer_user_cfg1)); - device_control(timer_ch1, DEVICE_CTRL_TIMER_CH_START, (void *)(&timer_user_cfg2)); + if (timer1) { + device_open(timer1, DEVICE_OFLAG_STREAM_TX); // 1s timing } else { MSG("timer device open failed! \n"); } - while (1) { - if (SET == device_control(timer_ch1, DEVICE_CTRL_GET_MATCH_STATUS, (void *)TIMER_EVENT_COMP2)) { - MSG("Comparator 2 match, timer reload\n"); - device_control(timer_ch1, DEVICE_CTRL_TIMER_CH_START, (void *)(&timer_user_cfg0)); - device_control(timer_ch1, DEVICE_CTRL_TIMER_CH_START, (void *)(&timer_user_cfg1)); - device_control(timer_ch1, DEVICE_CTRL_TIMER_CH_START, (void *)(&timer_user_cfg2)); - MSG("Counter value=%d,M0=%d, M1=%d,M2=%d \n", device_control(timer_ch1, DEVICE_CTRL_GET_CONFIG, NULL), - device_control(timer_ch1, DEVICE_CTRL_GET_MATCH_STATUS, (void *)TIMER_EVENT_COMP0), - device_control(timer_ch1, DEVICE_CTRL_GET_MATCH_STATUS, (void *)TIMER_EVENT_COMP1), - device_control(timer_ch1, DEVICE_CTRL_GET_MATCH_STATUS, (void *)TIMER_EVENT_COMP2)); - break; + if (device_control(timer1, DEVICE_CTRL_GET_INT, (void *)TIMER_COMP0_IT)) { + BL_CASE_SUCCESS; } - - MSG("Counter value=%d,M0=%d, M1=%d,M2=%d \n", device_control(timer_ch1, DEVICE_CTRL_GET_CONFIG, NULL), - device_control(timer_ch1, DEVICE_CTRL_GET_MATCH_STATUS, (void *)TIMER_EVENT_COMP0), - device_control(timer_ch1, DEVICE_CTRL_GET_MATCH_STATUS, (void *)TIMER_EVENT_COMP1), - device_control(timer_ch1, DEVICE_CTRL_GET_MATCH_STATUS, (void *)TIMER_EVENT_COMP2)); - } - - BL_CASE_SUCCESS; - while (1) { bflb_platform_delay_ms(100); } } diff --git a/examples/timer/timer_int/main.c b/examples/timer/timer_int/main.c index 020a0693..0c0ac027 100644 --- a/examples/timer/timer_int/main.c +++ b/examples/timer/timer_int/main.c @@ -20,25 +20,24 @@ * under the License. * */ - -#include "hal_timer.h" #include "bflb_platform.h" +#include "hal_timer.h" -struct device *timer_ch0; +struct device *timer0; volatile uint32_t cnt = 0; -void timer_ch0_irq_callback(struct device *dev, void *args, uint32_t size, uint32_t state) +void timer0_irq_callback(struct device *dev, void *args, uint32_t size, uint32_t state) { - MSG("timer ch0 interrupt! \n"); - if (state == TIMER_EVENT_COMP0) { - cnt++; - MSG("timer event comp0! cnt=%d\n", cnt); + MSG("timer event comp0! \r\n"); } else if (state == TIMER_EVENT_COMP1) { - MSG("timer event comp1! \n"); + MSG("timer event comp1! \r\n"); } else if (state == TIMER_EVENT_COMP2) { - MSG("timer event comp2! \n"); + BL_CASE_SUCCESS; + timer_timeout_cfg_t cfg = { 2, 12000000 }; /*modify compare id 2 timeout 12s*/ + device_write(dev, 0, &cfg, sizeof(timer_timeout_cfg_t)); + MSG("timer event comp2! \r\n"); } } @@ -46,28 +45,18 @@ int main(void) { bflb_platform_init(0); - timer_user_cfg_t timer_user_cfg; - timer_user_cfg.timeout_val = 1000 * 1000; /* us */ - timer_user_cfg.comp_it = TIMER_COMP0_IT; + timer_register(TIMER0_INDEX, "timer0"); - timer_register(TIMER_CH0_INDEX, "timer_ch0"); + timer0 = device_find("timer0"); - timer_ch0 = device_find("timer_ch0"); - - if (timer_ch0) { - device_open(timer_ch0, DEVICE_OFLAG_INT); - device_set_callback(timer_ch0, timer_ch0_irq_callback); - device_control(timer_ch0, DEVICE_CTRL_SET_INT, NULL); - device_control(timer_ch0, DEVICE_CTRL_TIMER_CH_START, (void *)(&timer_user_cfg)); + if (timer0) { + device_open(timer0, DEVICE_OFLAG_INT_TX); /* 1s,2s,3s timing*/ + device_set_callback(timer0, timer0_irq_callback); + device_control(timer0, DEVICE_CTRL_SET_INT, (void *)(TIMER_COMP0_IT | TIMER_COMP1_IT | TIMER_COMP2_IT)); } else { MSG("timer device open failed! \n"); } while (1) { - if (cnt == 10) { - device_control(timer_ch0, DEVICE_CTRL_TIMER_CH_STOP, (void *)(&timer_user_cfg)); - MSG("timer test over! \n"); - BL_CASE_SUCCESS; - } } }