简化操作接口

This commit is contained in:
heyuanjie87 2015-09-06 15:05:47 +08:00
parent 47b3a4d3cc
commit d19f424909
4 changed files with 82 additions and 144 deletions

View File

@ -1,5 +1,5 @@
/* /*
* File : gpio.c * File : drv_hwtimer.c
* This file is part of RT-Thread RTOS * This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2015, RT-Thread Development Team * COPYRIGHT (C) 2015, RT-Thread Development Team
* *
@ -31,35 +31,34 @@ static void NVIC_Configuration(void)
NVIC_Init(&NVIC_InitStructure); NVIC_Init(&NVIC_InitStructure);
} }
static void timer_init(rt_hwtimer_t *timer) static void timer_init(rt_hwtimer_t *timer, rt_uint32_t state)
{ {
TIM_TypeDef *tim; TIM_TypeDef *tim;
tim = (TIM_TypeDef *)timer->parent.user_data; tim = (TIM_TypeDef *)timer->parent.user_data;
TIM_DeInit(tim); TIM_DeInit(tim);
NVIC_Configuration();
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); if (state == 1)
TIM_CounterModeConfig(tim, TIM_CounterMode_Up); {
NVIC_Configuration();
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_CounterModeConfig(tim, TIM_CounterMode_Up);
}
} }
static void timer_deinit(rt_hwtimer_t *timer) static rt_err_t timer_start(rt_hwtimer_t *timer, rt_uint32_t t, rt_hwtimer_mode_t opmode)
{
TIM_TypeDef *tim;
tim = (TIM_TypeDef *)timer->parent.user_data;
TIM_DeInit(tim);
}
static void timer_start(rt_hwtimer_t *timer, rt_hwtimer_mode_t opmode)
{ {
TIM_TypeDef *tim; TIM_TypeDef *tim;
uint16_t m; uint16_t m;
tim = (TIM_TypeDef *)timer->parent.user_data; tim = (TIM_TypeDef *)timer->parent.user_data;
TIM_SetAutoreload(tim, t);
m = (opmode == HWTIMER_MODE_ONESHOT)? TIM_OPMode_Single : TIM_OPMode_Repetitive; m = (opmode == HWTIMER_MODE_ONESHOT)? TIM_OPMode_Single : TIM_OPMode_Repetitive;
TIM_SelectOnePulseMode(tim, m); TIM_SelectOnePulseMode(tim, m);
TIM_Cmd(tim, ENABLE); TIM_Cmd(tim, ENABLE);
return RT_EOK;
} }
static void timer_stop(rt_hwtimer_t *timer) static void timer_stop(rt_hwtimer_t *timer)
@ -90,11 +89,11 @@ static rt_err_t timer_ctrl(rt_hwtimer_t *timer, rt_uint32_t cmd, void *arg)
freq = *((rt_uint32_t*)arg); freq = *((rt_uint32_t*)arg);
clk.PCLK1_Frequency *= 2; clk.PCLK1_Frequency *= 2;
val = clk.PCLK1_Frequency/freq; val = clk.PCLK1_Frequency/freq;
TIM_ITConfig(tim, TIM_IT_Update, DISABLE); TIM_ITConfig(tim, TIM_IT_Update, DISABLE);
TIM_PrescalerConfig(tim, val - 1, TIM_PSCReloadMode_Immediate); TIM_PrescalerConfig(tim, val - 1, TIM_PSCReloadMode_Immediate);
TIM_ClearITPendingBit(TIM2, TIM_IT_Update); TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
TIM_ITConfig(tim, TIM_IT_Update, ENABLE); TIM_ITConfig(tim, TIM_IT_Update, ENABLE);
} }
break; break;
default: default:
@ -116,31 +115,19 @@ static rt_uint32_t timer_counter_get(rt_hwtimer_t *timer)
return TIM_GetCounter(tim); return TIM_GetCounter(tim);
} }
static rt_err_t timer_timeout_set(rt_hwtimer_t *timer, rt_uint32_t t)
{
TIM_TypeDef *tim;
tim = (TIM_TypeDef *)timer->parent.user_data;
TIM_SetAutoreload(tim, t);
return RT_EOK;
}
static const struct rt_hwtimer_info _info = static const struct rt_hwtimer_info _info =
{ {
1000000, /* 可设置的最大计数时钟 */ 1000000, /* the maximum count frequency can be set */
2000, /* 可设置的最小计数时钟 */ 2000, /* the minimum count frequency can be set */
0xFFFF, /* 最大超时值 */ 0xFFFF, /* the maximum counter value */
HWTIMER_CNTMODE_UP,/* 递增计数方式 */ HWTIMER_CNTMODE_UP,/* Increment or Decreasing count mode */
}; };
static const struct rt_hwtimer_ops _ops = static const struct rt_hwtimer_ops _ops =
{ {
timer_init, timer_init,
timer_deinit,
timer_start, timer_start,
timer_stop, timer_stop,
timer_timeout_set,
timer_counter_get, timer_counter_get,
timer_ctrl, timer_ctrl,
}; };

View File

@ -25,9 +25,8 @@
#include <rtthread.h> #include <rtthread.h>
#include <rtdevice.h> #include <rtdevice.h>
rt_inline rt_err_t timeout_set(rt_hwtimer_t *timer, rt_hwtimerval_t *tv) rt_inline rt_uint32_t timeout_calc(rt_hwtimer_t *timer, rt_hwtimerval_t *tv)
{ {
rt_err_t err;
float overflow; float overflow;
float timeout; float timeout;
rt_uint32_t counter; rt_uint32_t counter;
@ -36,15 +35,6 @@ rt_inline rt_err_t timeout_set(rt_hwtimer_t *timer, rt_hwtimerval_t *tv)
float devi_min = 1; float devi_min = 1;
float devi; float devi;
if (timer->ops->stop != RT_NULL)
{
timer->ops->stop(timer);
}
else
{
return -RT_ENOSYS;
}
/* 把定时器溢出时间和定时时间换算成秒 */ /* 把定时器溢出时间和定时时间换算成秒 */
overflow = timer->info->maxcnt/(float)timer->freq; overflow = timer->info->maxcnt/(float)timer->freq;
tv_sec = tv->sec + tv->usec/(float)1000000; tv_sec = tv->sec + tv->usec/(float)1000000;
@ -90,12 +80,7 @@ rt_inline rt_err_t timeout_set(rt_hwtimer_t *timer, rt_hwtimerval_t *tv)
timer->period_sec = timeout; timer->period_sec = timeout;
counter = timeout*timer->freq; counter = timeout*timer->freq;
if (timer->ops->timeout_set != RT_NULL) return counter;
{
err = timer->ops->timeout_set(timer, counter);
}
return err;
} }
static rt_err_t rt_hwtimer_init(struct rt_device *dev) static rt_err_t rt_hwtimer_init(struct rt_device *dev)
@ -115,10 +100,11 @@ static rt_err_t rt_hwtimer_init(struct rt_device *dev)
} }
timer->mode = HWTIMER_MODE_ONESHOT; timer->mode = HWTIMER_MODE_ONESHOT;
timer->cycles = 0; timer->cycles = 0;
timer->overflow = 0;
if (timer->ops->init) if (timer->ops->init)
{ {
timer->ops->init(timer); timer->ops->init(timer, 1);
} }
else else
{ {
@ -152,9 +138,9 @@ static rt_err_t rt_hwtimer_close(struct rt_device *dev)
rt_hwtimer_t *timer; rt_hwtimer_t *timer;
timer = (rt_hwtimer_t*)dev; timer = (rt_hwtimer_t*)dev;
if (timer->ops->deinit != RT_NULL) if (timer->ops->init != RT_NULL)
{ {
timer->ops->deinit(timer); timer->ops->init(timer, 0);
} }
else else
{ {
@ -195,21 +181,29 @@ static rt_size_t rt_hwtimer_read(struct rt_device *dev, rt_off_t pos, void *buff
static rt_size_t rt_hwtimer_write(struct rt_device *dev, rt_off_t pos, const void *buffer, rt_size_t size) static rt_size_t rt_hwtimer_write(struct rt_device *dev, rt_off_t pos, const void *buffer, rt_size_t size)
{ {
rt_size_t len = 0; rt_uint32_t t;
rt_hwtimerval_t *t; rt_hwtimer_mode_t opm = HWTIMER_MODE_PERIOD;
rt_err_t err; rt_hwtimer_t *timer;
if (size == sizeof(rt_hwtimerval_t)) timer = (rt_hwtimer_t *)dev;
if ((timer->ops->start == RT_NULL) || (timer->ops->stop == RT_NULL))
return 0;
if (size != sizeof(rt_hwtimerval_t))
return 0;
if ((timer->cycles <= 1) && (timer->mode == HWTIMER_MODE_ONESHOT))
{ {
t = (rt_hwtimerval_t*)buffer; opm = HWTIMER_MODE_ONESHOT;
err = timeout_set((rt_hwtimer_t*)dev, t);
if (err == RT_EOK)
{
len = size;
}
} }
timer->ops->stop(timer);
timer->overflow = 0;
return len; t = timeout_calc(timer, (rt_hwtimerval_t*)buffer);
if (timer->ops->start(timer, t, opm) != RT_EOK)
size = 0;
return size;
} }
static rt_err_t rt_hwtimer_control(struct rt_device *dev, rt_uint8_t cmd, void *args) static rt_err_t rt_hwtimer_control(struct rt_device *dev, rt_uint8_t cmd, void *args)
@ -221,31 +215,6 @@ static rt_err_t rt_hwtimer_control(struct rt_device *dev, rt_uint8_t cmd, void *
switch (cmd) switch (cmd)
{ {
case HWTIMER_CTRL_START:
{
if (timer->ops->start != RT_NULL)
{
rt_hwtimer_mode_t opm;
if ((timer->cycles <= 1) && (timer->mode == HWTIMER_MODE_ONESHOT))
{
opm = HWTIMER_MODE_ONESHOT;
}
else
{
opm = HWTIMER_MODE_PERIOD;
}
timer->overflow = 0;
timer->ops->start(timer, opm);
}
else
{
result = -RT_ENOSYS;
}
}
break;
case HWTIMER_CTRL_STOP: case HWTIMER_CTRL_STOP:
{ {
if (timer->ops->stop != RT_NULL) if (timer->ops->stop != RT_NULL)
@ -258,17 +227,6 @@ static rt_err_t rt_hwtimer_control(struct rt_device *dev, rt_uint8_t cmd, void *
} }
} }
break; break;
case HWTIMER_CTRL_TIMEOUT_SET:
{
if (args == RT_NULL)
{
result = -RT_EEMPTY;
break;
}
result = timeout_set(timer, (rt_hwtimerval_t*)args);
}
break;
case HWTIMER_CTRL_FREQ_SET: case HWTIMER_CTRL_FREQ_SET:
{ {
rt_uint32_t *f; rt_uint32_t *f;
@ -381,7 +339,7 @@ rt_err_t rt_device_hwtimer_register(rt_hwtimer_t *timer, const char *name, void
device = &(timer->parent); device = &(timer->parent);
device->type = RT_Device_Class_HwTimer; device->type = RT_Device_Class_Timer;
device->rx_indicate = RT_NULL; device->rx_indicate = RT_NULL;
device->tx_complete = RT_NULL; device->tx_complete = RT_NULL;

View File

@ -8,54 +8,50 @@
extern "C" { extern "C" {
#endif #endif
/* 定时器控制命令 */ /* Timer Control Command */
typedef enum typedef enum
{ {
HWTIMER_CTRL_TIMEOUT_SET = 0x01, /* 设置超时值 */ HWTIMER_CTRL_FREQ_SET = 0x01, /* set the count frequency */
HWTIMER_CTRL_FREQ_SET, /* 设置计数频率 */ HWTIMER_CTRL_STOP, /* stop timer */
HWTIMER_CTRL_START, /* 启动定时器 */ HWTIMER_CTRL_INFO_GET, /* get a timer feature information */
HWTIMER_CTRL_STOP, /* 停止定时器 */ HWTIMER_CTRL_MODE_SET /* Setting the timing mode(oneshot/period) */
HWTIMER_CTRL_INFO_GET, /* 获取定时器特征信息 */
HWTIMER_CTRL_MODE_SET /* 设置定时模式 */
} rt_hwtimer_ctrl_t; } rt_hwtimer_ctrl_t;
/* 定时模式 */ /* Timing Mode */
typedef enum typedef enum
{ {
HWTIMER_MODE_ONESHOT = 0x01, HWTIMER_MODE_ONESHOT = 0x01,
HWTIMER_MODE_PERIOD HWTIMER_MODE_PERIOD
} rt_hwtimer_mode_t; } rt_hwtimer_mode_t;
/* 定时器计数值 */ /* Time Value */
typedef struct rt_hwtimerval typedef struct rt_hwtimerval
{ {
rt_int32_t sec; /* */ rt_int32_t sec; /* second */
rt_int32_t usec; /* 微秒 */ rt_int32_t usec; /* microsecond */
} rt_hwtimerval_t; } rt_hwtimerval_t;
#define HWTIMER_CNTMODE_UP 0x01 /* 定时器递增计数方式 */ #define HWTIMER_CNTMODE_UP 0x01 /* increment count mode */
#define HWTIMER_CNTMODE_DW 0x02 /* 定时器递减计数方式 */ #define HWTIMER_CNTMODE_DW 0x02 /* decreasing count mode */
struct rt_hwtimer_device; struct rt_hwtimer_device;
struct rt_hwtimer_ops struct rt_hwtimer_ops
{ {
void (*init)(struct rt_hwtimer_device *timer); void (*init)(struct rt_hwtimer_device *timer, rt_uint32_t state);
void (*deinit)(struct rt_hwtimer_device *timer); rt_err_t (*start)(struct rt_hwtimer_device *timer, rt_uint32_t cnt, rt_hwtimer_mode_t mode);
void (*start)(struct rt_hwtimer_device *timer, rt_hwtimer_mode_t mode);
void (*stop)(struct rt_hwtimer_device *timer); void (*stop)(struct rt_hwtimer_device *timer);
rt_err_t (*timeout_set)(struct rt_hwtimer_device *timer, rt_uint32_t t);
rt_uint32_t (*count_get)(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); rt_err_t (*control)(struct rt_hwtimer_device *timer, rt_uint32_t cmd, void *args);
}; };
/* 定时器基本参数(不可动态更改) */ /* Timer Feature Information */
struct rt_hwtimer_info struct rt_hwtimer_info
{ {
rt_int32_t maxfreq; /* 定时器支持的最大计数时钟 */ rt_int32_t maxfreq; /* the maximum count frequency timer support */
rt_int32_t minfreq; /* 定时器支持的最小计数时钟 */ rt_int32_t minfreq; /* the minimum count frequency timer support */
rt_uint32_t maxcnt; /* 计数器最大超时值 */ rt_uint32_t maxcnt; /* counter maximum value */
rt_uint8_t cntmode; /* 计数模式(递增/递减,应用层无需关心此参数) */ rt_uint8_t cntmode; /* count mode (inc/dec) */
}; };
typedef struct rt_hwtimer_device typedef struct rt_hwtimer_device
@ -64,13 +60,12 @@ typedef struct rt_hwtimer_device
const struct rt_hwtimer_ops *ops; const struct rt_hwtimer_ops *ops;
const struct rt_hwtimer_info *info; const struct rt_hwtimer_info *info;
/* 驱动层不用关心以下参数 */ rt_int32_t freq; /* counting frequency set by the user */
rt_int32_t freq; /* 计数频率 */ rt_int32_t overflow; /* timer overflows */
rt_int32_t overflow; /* 溢出次数 */ float period_sec;
float period_sec; /* 溢出周期(s) */ rt_int32_t cycles; /* how many times will generate a timeout event after overflow */
rt_int32_t cycles; /* 循环次数 */ rt_int32_t reload; /* reload cycles(using in period mode) */
rt_int32_t reload; /* 重载cycles */ rt_hwtimer_mode_t mode; /* timing mode(oneshot/period) */
rt_hwtimer_mode_t mode; /* 定时模式 */
} rt_hwtimer_t; } rt_hwtimer_t;
rt_err_t rt_device_hwtimer_register(rt_hwtimer_t *timer, const char *name, void *user_data); rt_err_t rt_device_hwtimer_register(rt_hwtimer_t *timer, const char *name, void *user_data);

View File

@ -13,15 +13,15 @@ static rt_err_t timer_timeout_cb(rt_device_t dev, rt_size_t size)
return 0; return 0;
} }
int hwtimer(int freq, int t) int hwtimer(void)
{ {
rt_err_t err; rt_err_t err;
rt_hwtimerval_t val; rt_hwtimerval_t val;
rt_device_t dev = RT_NULL; rt_device_t dev = RT_NULL;
rt_tick_t tick; rt_tick_t tick;
rt_hwtimer_mode_t mode; rt_hwtimer_mode_t mode;
int freq = 10000;
t = (t <= 0)? 5 : t; int t = 5;
if ((dev = rt_device_find(TIMER)) == RT_NULL) if ((dev = rt_device_find(TIMER)) == RT_NULL)
{ {
@ -32,7 +32,7 @@ int hwtimer(int freq, int t)
if (rt_device_open(dev, RT_DEVICE_OFLAG_RDWR) != RT_EOK) if (rt_device_open(dev, RT_DEVICE_OFLAG_RDWR) != RT_EOK)
{ {
rt_kprintf("Open %s Fail\n", TIMER); rt_kprintf("Open %s Fail\n", TIMER);
return -1; return -1;
} }
rt_device_set_rx_indicate(dev, timer_timeout_cb); rt_device_set_rx_indicate(dev, timer_timeout_cb);
@ -44,7 +44,13 @@ int hwtimer(int freq, int t)
goto EXIT; 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.sec = t;
val.usec = 0; val.usec = 0;
rt_kprintf("SetTime: Sec %d, Usec %d\n", val.sec, val.usec); rt_kprintf("SetTime: Sec %d, Usec %d\n", val.sec, val.usec);
@ -53,14 +59,6 @@ int hwtimer(int freq, int t)
rt_kprintf("SetTime Fail\n"); rt_kprintf("SetTime Fail\n");
goto EXIT; goto EXIT;
} }
/* 周期模式 */
mode = HWTIMER_MODE_PERIOD;
err = rt_device_control(dev, HWTIMER_CTRL_MODE_SET, &mode);
/* 启动定时器 */
tick = rt_tick_get();
err = rt_device_control(dev, HWTIMER_CTRL_START, RT_NULL);
rt_kprintf("Start Timer> Tick: %d\n", tick);
rt_kprintf("Sleep %d sec\n", t); rt_kprintf("Sleep %d sec\n", t);
rt_thread_delay(t*RT_TICK_PER_SECOND); rt_thread_delay(t*RT_TICK_PER_SECOND);