diff options
| author | yangjie11 <[email protected]> | 2020-11-18 09:56:35 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2020-11-18 09:56:35 +0800 |
| commit | d4a682e8d97fa131e5a8d795f8c5451d55cdd5b0 (patch) | |
| tree | 4773b40cb012475c5daf74bfd55707e141477669 /components/drivers/hwtimer | |
| parent | 2b98f4720c827ae1850df78299f6b273d568c8c7 (diff) | |
| parent | 2ef3c9d861f55253620996a94831a4be31ca6620 (diff) | |
Merge pull request #3 from RT-Thread/master
sync
Diffstat (limited to 'components/drivers/hwtimer')
| -rw-r--r-- | components/drivers/hwtimer/README_CN.md | 49 | ||||
| -rw-r--r-- | components/drivers/hwtimer/hwtimer.c | 3 |
2 files changed, 26 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. 在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) } ``` -##注意事项 ---- - -<font color="#FF0000">可能出现定时误差</font> - +## 注意事项 + +**可能出现定时误差** 误差原因: 假设计数器最大值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..3953919fae 100644 --- a/components/drivers/hwtimer/hwtimer.c +++ b/components/drivers/hwtimer/hwtimer.c @@ -153,7 +153,7 @@ static rt_size_t rt_hwtimer_read(struct rt_device *dev, rt_off_t pos, void *buff cnt = timer->ops->count_get(timer); if (timer->info->cntmode == HWTIMER_CNTMODE_DW) { - cnt = timer->info->maxcnt - cnt; + cnt = (timer->freq * timer->period_sec) - cnt; } t = timer->overflow * timer->period_sec + cnt/(float)timer->freq; @@ -255,6 +255,7 @@ static rt_err_t rt_hwtimer_control(struct rt_device *dev, int cmd, void *args) *((struct rt_hwtimer_info*)args) = *timer->info; } + break; case HWTIMER_CTRL_MODE_SET: { rt_hwtimer_mode_t *m; |
