Merge pull request #546 from heyuanjie87/ForPullRequest

[DeviceDrivers] Add hardware timer framework
This commit is contained in:
Bernard Xiong committed 2015-09-09 22:30:45 +08:00
commit 342e8d688d
13 files changed
+956 -2

No files matched your search

+153
View File
@@ -0,0 +1,153 @@
/*
* File : drv_hwtimer.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2015, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE
*
* Change Logs:
* Date Author Notes
* 2015-09-02 heyuanjie87 the first version
*/
#include <rtthread.h>
#include <rtdevice.h>
#include "lpc_timer.h"
#include "lpc_clkpwr.h"
#include "drv_hwtimer.h"
#ifdef RT_USING_HWTIMER
static void NVIC_Configuration(void)
{
NVIC_EnableIRQ(TIMER0_IRQn);
}
static void timer_init(rt_hwtimer_t *timer, rt_uint32_t state)
{
LPC_TIM_TypeDef *tim;
TIM_TIMERCFG_Type cfg;
tim = (LPC_TIM_TypeDef *)timer->parent.user_data;
TIM_DeInit(tim);
if (state == 1)
{
NVIC_Configuration();
cfg.PrescaleOption = TIM_PRESCALE_TICKVAL;
cfg.PrescaleValue = 0xFFFF;
TIM_Init(tim, TIM_TIMER_MODE, &cfg);
}
}
static rt_err_t timer_start(rt_hwtimer_t *timer, rt_uint32_t t, rt_hwtimer_mode_t opmode)
{
LPC_TIM_TypeDef *tim;
TIM_MATCHCFG_Type match;
tim = (LPC_TIM_TypeDef *)timer->parent.user_data;
match.MatchChannel = 0;
match.IntOnMatch = ENABLE;
match.ResetOnMatch = ENABLE;
match.StopOnMatch = (opmode == HWTIMER_MODE_ONESHOT)? ENABLE : DISABLE;
match.ExtMatchOutputType = 0;
match.MatchValue = t;
TIM_ConfigMatch(tim, &match);
TIM_Cmd(tim, ENABLE);
return RT_EOK;
}
static void timer_stop(rt_hwtimer_t *timer)
{
LPC_TIM_TypeDef *tim;
tim = (LPC_TIM_TypeDef *)timer->parent.user_data;
TIM_Cmd(tim, DISABLE);
}
static rt_err_t timer_ctrl(rt_hwtimer_t *timer, rt_uint32_t cmd, void *arg)
{
LPC_TIM_TypeDef *tim;
rt_err_t err = RT_EOK;
tim = (LPC_TIM_TypeDef *)timer->parent.user_data;
switch (cmd)
{
case HWTIMER_CTRL_FREQ_SET:
{
uint32_t clk;
uint32_t pre;
clk = CLKPWR_GetCLK(CLKPWR_CLKTYPE_PER);
pre = clk / *((uint32_t*)arg) - 1;
tim->PR = pre;
}
break;
default:
{
err = -RT_ENOSYS;
}
break;
}
return err;
}
static rt_uint32_t timer_counter_get(rt_hwtimer_t *timer)
{
LPC_TIM_TypeDef *tim;
tim = (LPC_TIM_TypeDef *)timer->parent.user_data;
return tim->TC;
}
static const struct rt_hwtimer_info _info =
{
1000000, /* the maximum count frequency can be set */
2000, /* the minimum count frequency can be set */
0xFFFFFF, /* the maximum counter value */
HWTIMER_CNTMODE_UP,/* Increment or Decreasing count mode */
};
static const struct rt_hwtimer_ops _ops =
{
timer_init,
timer_start,
timer_stop,
timer_counter_get,
timer_ctrl,
};
static rt_hwtimer_t _timer0;
int lpc_hwtimer_init(void)
{
_timer0.info = &_info;
_timer0.ops = &_ops;
rt_device_hwtimer_register(&_timer0, "timer0", LPC_TIM0);
return 0;
}
void TIMER0_IRQHandler(void)
{
if (TIM_GetIntStatus(LPC_TIM0, TIM_MR0_INT) != RESET)
{
TIM_ClearIntPending(LPC_TIM0, TIM_MR0_INT);
rt_device_hwtimer_isr(&_timer0);
}
}
INIT_BOARD_EXPORT(lpc_hwtimer_init);
#endif
+8
View File
@@ -0,0 +1,8 @@
#ifndef __DRV_HWTIMER_H__
#define __DRV_HWTIMER_H__
int stm32_hwtimer_init(void);
#endif
+157
View File
@@ -0,0 +1,157 @@
/*
* File : drv_hwtimer.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2015, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE
*
* Change Logs:
* Date Author Notes
* 2015-09-02 heyuanjie87 the first version
*/
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include "drv_hwtimer.h"
#ifdef RT_USING_HWTIMER
static void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable the TIM5 global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
static void timer_init(rt_hwtimer_t *timer, rt_uint32_t state)
{
TIM_TypeDef *tim;
tim = (TIM_TypeDef *)timer->parent.user_data;
TIM_DeInit(tim);
if (state == 1)
{
NVIC_Configuration();
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_CounterModeConfig(tim, TIM_CounterMode_Up);
}
}
static rt_err_t timer_start(rt_hwtimer_t *timer, rt_uint32_t t, rt_hwtimer_mode_t opmode)
{
TIM_TypeDef *tim;
uint16_t m;
tim = (TIM_TypeDef *)timer->parent.user_data;
TIM_SetAutoreload(tim, t);
m = (opmode == HWTIMER_MODE_ONESHOT)? TIM_OPMode_Single : TIM_OPMode_Repetitive;
TIM_SelectOnePulseMode(tim, m);
TIM_Cmd(tim, ENABLE);
return RT_EOK;
}
static void timer_stop(rt_hwtimer_t *timer)
{
TIM_TypeDef *tim;
tim = (TIM_TypeDef *)timer->parent.user_data;
TIM_Cmd(tim, DISABLE);
}
static rt_err_t timer_ctrl(rt_hwtimer_t *timer, rt_uint32_t cmd, void *arg)
{
TIM_TypeDef *tim;
rt_err_t err = RT_EOK;
tim = (TIM_TypeDef *)timer->parent.user_data;
switch (cmd)
{
case HWTIMER_CTRL_FREQ_SET:
{
RCC_ClocksTypeDef clk;
uint16_t val;
rt_uint32_t freq;
RCC_GetClocksFreq(&clk);
freq = *((rt_uint32_t*)arg);
clk.PCLK1_Frequency *= 2;
val = clk.PCLK1_Frequency/freq;
TIM_ITConfig(tim, TIM_IT_Update, DISABLE);
TIM_PrescalerConfig(tim, val - 1, TIM_PSCReloadMode_Immediate);
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
TIM_ITConfig(tim, TIM_IT_Update, ENABLE);
}
break;
default:
{
err = -RT_ENOSYS;
}
break;
}
return err;
}
static rt_uint32_t timer_counter_get(rt_hwtimer_t *timer)
{
TIM_TypeDef *tim;
tim = (TIM_TypeDef *)timer->parent.user_data;
return TIM_GetCounter(tim);
}
static const struct rt_hwtimer_info _info =
{
1000000, /* the maximum count frequency can be set */
2000, /* the minimum count frequency can be set */
0xFFFF, /* the maximum counter value */
HWTIMER_CNTMODE_UP,/* Increment or Decreasing count mode */
};
static const struct rt_hwtimer_ops _ops =
{
timer_init,
timer_start,
timer_stop,
timer_counter_get,
timer_ctrl,
};
static rt_hwtimer_t _timer0;
int stm32_hwtimer_init(void)
{
_timer0.info = &_info;
_timer0.ops = &_ops;
rt_device_hwtimer_register(&_timer0, "timer0", TIM2);
return 0;
}
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
rt_device_hwtimer_isr(&_timer0);
}
}
INIT_BOARD_EXPORT(stm32_hwtimer_init);
#endif
+8
View File
@@ -0,0 +1,8 @@
#ifndef __DRV_HWTIMER_H__
#define __DRV_HWTIMER_H__
int stm32_hwtimer_init(void);
#endif
+3
View File
@@ -69,6 +69,9 @@
/* Using GPIO pin framework */
#define RT_USING_PIN
/* Using Hardware Timer framework */
//#define RT_USING_HWTIMER
/* SECTION: Console options */
#define RT_USING_CONSOLE
/* the buffer size of console*/
+98
View File
@@ -0,0 +1,98 @@
定时器设备
===
##功能
---
* 时间测量
* 周期或单次执行回调函数
##编译
---
1. 在rtconfig.h添加 `#define RT_USING_HWTIMER`
##使用流程
---
1. 以读写方式打开设备
2. 设置超时回调函数(如果需要)
3. 根据需要设置定时模式(单次/周期)
4. 设置计数频率(可选)
5. 写入超时值,定时器随即启动
6. 停止定时器(可选)
7. 关闭设备(如果需要)
应用参考 [hwtimer_test] (/examples/test/hwtimer\_test.c)
##驱动编写指南
---
###操作接口
```
struct rt_hwtimer_ops
{
void (*init)(struct rt_hwtimer_device *timer, rt_uint32_t state);
rt_err_t (*start)(struct rt_hwtimer_device *timer, rt_uint32_t cnt, rt_hwtimer_mode_t mode);
void (*stop)(struct rt_hwtimer_device *timer);
rt_uint32_t (*count_get)(struct rt_hwtimer_device *timer);
rt_err_t (*control)(struct rt_hwtimer_device *timer, rt_uint32_t cmd, void *args);
};
```
* init - state <1 打开设备 0 关闭设备>
* start - cnt <超时值> - mode <单次/周期>
* stop - <停止计数>
* count_get - <读取计数器值>
* control - <设置计数频率 >
###定时器特征信息
```
struct rt_hwtimer_info
{
rt_int32_t maxfreq;
rt_int32_t minfreq;
rt_uint32_t maxcnt;
rt_uint8_t cntmode;
};
```
* maxfreq <设备支持的最大计数频率>
* minfreq <设备支持的最小计数频率>
* maxcnt <计数器最大计数值>
* cntmode <递增计数/递减计数>
###注册设备
```
static rt_hwtimer_t _timer0;
int stm32_hwtimer_init(void)
{
_timer0.info = &_info;
_timer0.ops = &_ops;
rt_device_hwtimer_register(&_timer0, "timer0", TIM2);
return 0;
}
```
###定时器中断
```
void timer_irq_handler(void)
{
//其它操作
rt_device_hwtimer_isr(&_timer0);
}
```
##注意事项
---
<font color="#FF0000">可能出现定时误差</font>
误差原因:
假设计数器最大值0xFFFF,计数频率1Mhz,定时时间1秒又1微秒。
由于定时器一次最多只能计时到65535us,对于1000001us的定时要求。
可以50000us定时20次完成,此时将会出现计算误差1us。
+8
View File
@@ -0,0 +1,8 @@
from building import *
cwd = GetCurrentDir()
src = Glob('*.c')
CPPPATH = [cwd + '/../include']
group = DefineGroup('DeviceDrivers', src, depend = ['RT_USING_HWTIMER'], CPPPATH = CPPPATH)
Return('group')
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,78 @@
#ifndef __HWTIMER_H__
#define __HWTIMER_H__
#include <rtthread.h>
#include <rtdevice.h>
#ifdef __cplusplus
extern "C" {
#endif
/* Timer Control Command */
typedef enum
{
HWTIMER_CTRL_FREQ_SET = 0x01, /* set the count frequency */
HWTIMER_CTRL_STOP, /* stop timer */
HWTIMER_CTRL_INFO_GET, /* get a timer feature information */
HWTIMER_CTRL_MODE_SET /* Setting the timing mode(oneshot/period) */
} rt_hwtimer_ctrl_t;
/* Timing Mode */
typedef enum
{
HWTIMER_MODE_ONESHOT = 0x01,
HWTIMER_MODE_PERIOD
} rt_hwtimer_mode_t;
/* Time Value */
typedef struct rt_hwtimerval
{
rt_int32_t sec; /* second */
rt_int32_t usec; /* microsecond */
} rt_hwtimerval_t;
#define HWTIMER_CNTMODE_UP 0x01 /* increment count mode */
#define HWTIMER_CNTMODE_DW 0x02 /* decreasing count mode */
struct rt_hwtimer_device;
struct rt_hwtimer_ops
{
void (*init)(struct rt_hwtimer_device *timer, rt_uint32_t state);
rt_err_t (*start)(struct rt_hwtimer_device *timer, rt_uint32_t cnt, rt_hwtimer_mode_t mode);
void (*stop)(struct rt_hwtimer_device *timer);
rt_uint32_t (*count_get)(struct rt_hwtimer_device *timer);
rt_err_t (*control)(struct rt_hwtimer_device *timer, rt_uint32_t cmd, void *args);
};
/* Timer Feature Information */
struct rt_hwtimer_info
{
rt_int32_t maxfreq; /* the maximum count frequency timer support */
rt_int32_t minfreq; /* the minimum count frequency timer support */
rt_uint32_t maxcnt; /* counter maximum value */
rt_uint8_t cntmode; /* count mode (inc/dec) */
};
typedef struct rt_hwtimer_device
{
struct rt_device parent;
const struct rt_hwtimer_ops *ops;
const struct rt_hwtimer_info *info;
rt_int32_t freq; /* counting frequency set by the user */
rt_int32_t overflow; /* timer overflows */
float period_sec;
rt_int32_t cycles; /* how many times will generate a timeout event after overflow */
rt_int32_t reload; /* reload cycles(using in period mode) */
rt_hwtimer_mode_t mode; /* timing mode(oneshot/period) */
} rt_hwtimer_t;
rt_err_t rt_device_hwtimer_register(rt_hwtimer_t *timer, const char *name, void *user_data);
void rt_device_hwtimer_isr(rt_hwtimer_t *timer);
#ifdef __cplusplus
}
#endif
#endif
+4
View File
@@ -370,6 +370,10 @@ rt_inline void rt_work_init(struct rt_work* work, void (*work_func)(struct rt_wo
#include "drivers/can.h"
#endif
#ifdef RT_USING_HWTIMER
#include "drivers/hwtimer.h"
#endif
#ifdef __cplusplus
}
#endif
+1
View File
@@ -467,6 +467,7 @@ static long _list_device(struct rt_list_node *list)
"PM Pseudo Device",
"Pipe",
"Portal Device",
"Timer Device",
"Miscellaneous Device",
"Unknown"
};
+80
View File
@@ -0,0 +1,80 @@
#include <rtthread.h>
#include <rtdevice.h>
#include <finsh.h>
#ifdef RT_USING_HWTIMER
#define TIMER "timer0"
static rt_err_t timer_timeout_cb(rt_device_t dev, rt_size_t size)
{
rt_kprintf("HT %d\n", rt_tick_get());
return 0;
}
int hwtimer(void)
{
rt_err_t err;
rt_hwtimerval_t val;
rt_device_t dev = RT_NULL;
rt_tick_t tick;
rt_hwtimer_mode_t mode;
int freq = 10000;
int t = 5;
if ((dev = rt_device_find(TIMER)) == RT_NULL)
{
rt_kprintf("No Device: %s\n", TIMER);
return -1;
}
if (rt_device_open(dev, RT_DEVICE_OFLAG_RDWR) != RT_EOK)
{
rt_kprintf("Open %s Fail\n", TIMER);
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)
{
rt_kprintf("Set Freq=%dhz Fail\n", freq);
goto EXIT;
}
/* 周期模式 */
mode = HWTIMER_MODE_PERIOD;
err = rt_device_control(dev, HWTIMER_CTRL_MODE_SET, &mode);
tick = rt_tick_get();
rt_kprintf("Start Timer> Tick: %d\n", tick);
/* 设置定时器超时值并启动定时器 */
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_kprintf("Sleep %d sec\n", t);
rt_thread_delay(t*RT_TICK_PER_SECOND);
/* 停止定时器 */
err = rt_device_control(dev, HWTIMER_CTRL_STOP, RT_NULL);
rt_kprintf("Timer Stoped\n");
/* 读取计数 */
rt_device_read(dev, 0, &val, sizeof(val));
rt_kprintf("Read: Sec = %d, Usec = %d\n", val.sec, val.usec);
EXIT:
err = rt_device_close(dev);
rt_kprintf("Close %s\n", TIMER);
return err;
}
FINSH_FUNCTION_EXPORT(hwtimer, "Test hardware timer");
#endif
+3 -2
View File
@@ -758,8 +758,9 @@ enum rt_device_class_type
RT_Device_Class_PM, /**< PM pseudo device */
RT_Device_Class_Pipe, /**< Pipe device */
RT_Device_Class_Portal, /**< Portal device */
RT_Device_Class_Miscellaneous, /**< Miscellaneous device */
RT_Device_Class_Unknown /**< unknown device */
RT_Device_Class_Timer, /**< Timer device */
RT_Device_Class_Miscellaneous, /**< Miscellaneous device */
RT_Device_Class_Unknown /**< unknown device */
};
/**