Merge pull request #2065 from qgyhd1234/hwtimer

完善 hwtimer 测试例程,修改 readme 显示错误
This commit is contained in:
Bernard Xiong 2018-12-14 11:05:58 +08:00 committed by GitHub
commit f12d5bfba0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 29 deletions

View File

@ -1,17 +1,16 @@
定时器设备 # 定时器设备
===
## 功能
##功能
---
* 时间测量 * 时间测量
* 周期或单次执行回调函数 * 周期或单次执行回调函数
##编译 ## 编译
---
1. 在rtconfig.h添加 `#define RT_USING_HWTIMER` 1. 在rtconfig.h添加 `#define RT_USING_HWTIMER`
##使用流程 ## 使用流程
---
1. 以读写方式打开设备 1. 以读写方式打开设备
2. 设置超时回调函数(如果需要) 2. 设置超时回调函数(如果需要)
3. 根据需要设置定时模式(单次/周期) 3. 根据需要设置定时模式(单次/周期)
@ -22,9 +21,9 @@
应用参考 [hwtimer_test] (/examples/test/hwtimer\_test.c) 应用参考 [hwtimer_test] (/examples/test/hwtimer\_test.c)
##驱动编写指南 ## 驱动编写指南
---
###操作接口 ### 操作接口
``` ```
struct rt_hwtimer_ops struct rt_hwtimer_ops
@ -43,7 +42,7 @@ struct rt_hwtimer_ops
* count_get - <读取计数器值> * count_get - <读取计数器值>
* control - <设置计数频率 > * control - <设置计数频率 >
###定时器特征信息 ### 定时器特征信息
``` ```
struct rt_hwtimer_info struct rt_hwtimer_info
@ -60,7 +59,8 @@ struct rt_hwtimer_info
* maxcnt <计数器最大计数值> * maxcnt <计数器最大计数值>
* cntmode <递增计数/递减计数> * cntmode <递增计数/递减计数>
###注册设备 ### 注册设备
``` ```
static rt_hwtimer_t _timer0; static rt_hwtimer_t _timer0;
int stm32_hwtimer_init(void) int stm32_hwtimer_init(void)
@ -74,7 +74,8 @@ int stm32_hwtimer_init(void)
} }
``` ```
###定时器中断 ### 定时器中断
``` ```
void timer_irq_handler(void) void timer_irq_handler(void)
{ {
@ -84,11 +85,9 @@ void timer_irq_handler(void)
} }
``` ```
##注意事项 ## 注意事项
---
<font color="#FF0000">可能出现定时误差</font>
**可能出现定时误差**
误差原因: 误差原因:

View File

@ -8,7 +8,7 @@
static rt_err_t timer_timeout_cb(rt_device_t dev, rt_size_t size) 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; return 0;
} }
@ -35,7 +35,7 @@ int hwtimer(void)
return -1; return -1;
} }
rt_device_set_rx_indicate(dev, timer_timeout_cb); /* 时间测量 */
/* 计数时钟设置(默认1Mhz或支持的最小计数频率) */ /* 计数时钟设置(默认1Mhz或支持的最小计数频率) */
err = rt_device_control(dev, HWTIMER_CTRL_FREQ_SET, &freq); err = rt_device_control(dev, HWTIMER_CTRL_FREQ_SET, &freq);
if (err != RT_EOK) if (err != RT_EOK)
@ -69,12 +69,34 @@ int hwtimer(void)
rt_device_read(dev, 0, &val, sizeof(val)); rt_device_read(dev, 0, &val, sizeof(val));
rt_kprintf("Read: Sec = %d, Usec = %d\n", val.sec, val.usec); 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: EXIT:
err = rt_device_close(dev); err = rt_device_close(dev);
rt_kprintf("Close %s\n", TIMER); rt_kprintf("Close %s\n", TIMER);
return err; return err;
} }
#ifdef FINSH_USING_MSH
FINSH_FUNCTION_EXPORT(hwtimer, "Test hardware timer"); MSH_CMD_EXPORT(hwtimer, "Test hardware timer");
#endif #endif
#endif /* RT_USING_HWTIMER */