diff --git a/components/drivers/hwtimer/README_CN.md b/components/drivers/hwtimer/README_CN.md index fb3b4fa60a25b8a21e17448b73b2870d0f5dce43..810b1b1d80bff4d751ccaac916490234c16b44b8 100644 --- a/components/drivers/hwtimer/README_CN.md +++ b/components/drivers/hwtimer/README_CN.md @@ -1,17 +1,16 @@ -定时器设备 -=== - -##功能 ---- +# 定时器设备 + +## 功能 + * 时间测量 * 周期或单次执行回调函数 -##编译 ---- +## 编译 + 1. 在rtconfig.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/examples/test/hwtimer_test.c b/examples/test/hwtimer_test.c index 8f69c4ef87a73befe18fdcad818d1385c7d50321..428373dde056b0faa884e18676bea9d4214d036b 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 */