From 4fd6f2ef53d832e5528ddd72c653a4e3bd32254a Mon Sep 17 00:00:00 2001 From: zylx Date: Wed, 12 Dec 2018 16:59:02 +0800 Subject: [PATCH 1/3] [examples][test]Perfect test function --- examples/test/hwtimer_test.c | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/examples/test/hwtimer_test.c b/examples/test/hwtimer_test.c index 8f69c4ef87..428373dde0 100644 --- a/examples/test/hwtimer_test.c +++ b/examples/test/hwtimer_test.c @@ -8,7 +8,7 @@ static rt_err_t timer_timeout_cb(rt_device_t dev, rt_size_t size) { - rt_kprintf("HT %d\n", rt_tick_get()); + rt_kprintf("enter hardware timer isr\n"); return 0; } @@ -35,7 +35,7 @@ int hwtimer(void) return -1; } - rt_device_set_rx_indicate(dev, timer_timeout_cb); + /* 时间测量 */ /* 计数时钟设置(默认1Mhz或支持的最小计数频率) */ err = rt_device_control(dev, HWTIMER_CTRL_FREQ_SET, &freq); if (err != RT_EOK) @@ -69,12 +69,34 @@ int hwtimer(void) rt_device_read(dev, 0, &val, sizeof(val)); rt_kprintf("Read: Sec = %d, Usec = %d\n", val.sec, val.usec); + /* 定时执行回调函数 -- 单次模式 */ + /* 设置超时回调函数 */ + rt_device_set_rx_indicate(dev, timer_timeout_cb); + + /* 单次模式 */ + mode = HWTIMER_MODE_PERIOD; + err = rt_device_control(dev, HWTIMER_CTRL_MODE_SET, &mode); + + /* 设置定时器超时值并启动定时器 */ + val.sec = t; + val.usec = 0; + rt_kprintf("SetTime: Sec %d, Usec %d\n", val.sec, val.usec); + if (rt_device_write(dev, 0, &val, sizeof(val)) != sizeof(val)) + { + rt_kprintf("SetTime Fail\n"); + goto EXIT; + } + + /* 等待回调函数执行 */ + rt_thread_delay((t + 1)*RT_TICK_PER_SECOND); + EXIT: err = rt_device_close(dev); rt_kprintf("Close %s\n", TIMER); return err; } - -FINSH_FUNCTION_EXPORT(hwtimer, "Test hardware timer"); +#ifdef FINSH_USING_MSH +MSH_CMD_EXPORT(hwtimer, "Test hardware timer"); #endif +#endif /* RT_USING_HWTIMER */ From f497b17111f955dbce988bd9925ea8312a542d93 Mon Sep 17 00:00:00 2001 From: zylx Date: Wed, 12 Dec 2018 17:13:37 +0800 Subject: [PATCH 2/3] [components][drivers]fix hardware timer --- components/drivers/hwtimer/README_CN.md | 49 ++++++++++++------------- components/drivers/hwtimer/hwtimer.c | 2 +- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/components/drivers/hwtimer/README_CN.md b/components/drivers/hwtimer/README_CN.md index fb3b4fa60a..810b1b1d80 100644 --- a/components/drivers/hwtimer/README_CN.md +++ b/components/drivers/hwtimer/README_CN.md @@ -1,17 +1,16 @@ -瀹氭椂鍣ㄨ澶 -=== - -##鍔熻兘 ---- +# 瀹氭椂鍣ㄨ澶 + +## 鍔熻兘 + * 鏃堕棿娴嬮噺 * 鍛ㄦ湡鎴栧崟娆℃墽琛屽洖璋冨嚱鏁 -##缂栬瘧 ---- +## 缂栬瘧 + 1. 鍦╮tconfig.h娣诲姞 `#define RT_USING_HWTIMER` - -##浣跨敤娴佺▼ ---- + +## 浣跨敤娴佺▼ + 1. 浠ヨ鍐欐柟寮忔墦寮璁惧 2. 璁剧疆瓒呮椂鍥炶皟鍑芥暟锛堝鏋滈渶瑕侊級 3. 鏍规嵁闇瑕佽缃畾鏃舵ā寮忥紙鍗曟/鍛ㄦ湡锛 @@ -19,12 +18,12 @@ 5. 鍐欏叆瓒呮椂鍊硷紝瀹氭椂鍣ㄩ殢鍗冲惎鍔 6. 鍋滄瀹氭椂鍣紙鍙夛級 7. 鍏抽棴璁惧锛堝鏋滈渶瑕侊級 - + 搴旂敤鍙傝 [hwtimer_test] (/examples/test/hwtimer\_test.c) -##椹卞姩缂栧啓鎸囧崡 ---- -###鎿嶄綔鎺ュ彛 +## 椹卞姩缂栧啓鎸囧崡 + +### 鎿嶄綔鎺ュ彛 ``` struct rt_hwtimer_ops @@ -43,8 +42,8 @@ struct rt_hwtimer_ops * count_get - <璇诲彇璁℃暟鍣ㄥ> * control - <璁剧疆璁℃暟棰戠巼 > -###瀹氭椂鍣ㄧ壒寰佷俊鎭 - +### 瀹氭椂鍣ㄧ壒寰佷俊鎭 + ``` struct rt_hwtimer_info { @@ -60,7 +59,8 @@ struct rt_hwtimer_info * maxcnt <璁℃暟鍣ㄦ渶澶ц鏁板> * cntmode <閫掑璁℃暟/閫掑噺璁℃暟> -###娉ㄥ唽璁惧 +### 娉ㄥ唽璁惧 + ``` static rt_hwtimer_t _timer0; int stm32_hwtimer_init(void) @@ -73,8 +73,9 @@ int stm32_hwtimer_init(void) return 0; } ``` - -###瀹氭椂鍣ㄤ腑鏂 + +### 瀹氭椂鍣ㄤ腑鏂 + ``` void timer_irq_handler(void) { @@ -84,15 +85,13 @@ void timer_irq_handler(void) } ``` -##娉ㄦ剰浜嬮」 ---- - -鍙兘鍑虹幇瀹氭椂璇樊 - +## 娉ㄦ剰浜嬮」 + +**鍙兘鍑虹幇瀹氭椂璇樊** 璇樊鍘熷洜锛 鍋囪璁℃暟鍣ㄦ渶澶у0xFFFF锛岃鏁伴鐜1Mhz锛屽畾鏃舵椂闂1绉掑張1寰銆 - + 鐢变簬瀹氭椂鍣ㄤ竴娆℃渶澶氬彧鑳借鏃跺埌65535us锛屽浜1000001us鐨勫畾鏃惰姹傘 鍙互50000us瀹氭椂20娆″畬鎴愶紝姝ゆ椂灏嗕細鍑虹幇璁$畻璇樊1us銆 diff --git a/components/drivers/hwtimer/hwtimer.c b/components/drivers/hwtimer/hwtimer.c index cd85b3baef..aafa465b1e 100644 --- a/components/drivers/hwtimer/hwtimer.c +++ b/components/drivers/hwtimer/hwtimer.c @@ -37,7 +37,7 @@ rt_inline rt_uint32_t timeout_calc(rt_hwtimer_t *timer, rt_hwtimerval_t *tv) { timeout = tv_sec/i; - if (timeout <= overflow) + if (timeout <= overflow && timeout * timer->freq <= timer->info->maxcnt) { counter = timeout*timer->freq; devi = tv_sec - (counter/(float)timer->freq)*i; From 6ea32ee488c8b76202d6707515dfff8cc2b28e4b Mon Sep 17 00:00:00 2001 From: zylx Date: Thu, 13 Dec 2018 09:59:07 +0800 Subject: [PATCH 3/3] [components][drivers] delete redundant judgment --- components/drivers/hwtimer/hwtimer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/drivers/hwtimer/hwtimer.c b/components/drivers/hwtimer/hwtimer.c index aafa465b1e..cd85b3baef 100644 --- a/components/drivers/hwtimer/hwtimer.c +++ b/components/drivers/hwtimer/hwtimer.c @@ -37,7 +37,7 @@ rt_inline rt_uint32_t timeout_calc(rt_hwtimer_t *timer, rt_hwtimerval_t *tv) { timeout = tv_sec/i; - if (timeout <= overflow && timeout * timer->freq <= timer->info->maxcnt) + if (timeout <= overflow) { counter = timeout*timer->freq; devi = tv_sec - (counter/(float)timer->freq)*i;