加入定时器设备

This commit is contained in:
heyuanjie87
2015-09-02 22:00:24 +08:00
parent cb51bdb245
commit c84887d021
9 changed files with 676 additions and 1 deletions
+170
View File
@@ -0,0 +1,170 @@
/*
* File : gpio.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)
{
TIM_TypeDef *tim;
tim = (TIM_TypeDef *)timer->parent.user_data;
TIM_DeInit(tim);
NVIC_Configuration();
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_CounterModeConfig(tim, TIM_CounterMode_Up);
}
static void timer_deinit(rt_hwtimer_t *timer)
{
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;
uint16_t m;
tim = (TIM_TypeDef *)timer->parent.user_data;
m = (opmode == HWTIMER_MODE_ONESHOT)? TIM_OPMode_Single : TIM_OPMode_Repetitive;
TIM_SelectOnePulseMode(tim, m);
TIM_Cmd(tim, ENABLE);
}
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 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 =
{
1000000, /* 可设置的最大计数时钟 */
2000, /* 可设置的最小计数时钟 */
0xFFFF, /* 最大超时值 */
HWTIMER_CNTMODE_UP,/* 递增计数方式 */
};
static const struct rt_hwtimer_ops _ops =
{
timer_init,
timer_deinit,
timer_start,
timer_stop,
timer_timeout_set,
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*/
+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,83 @@
#ifndef __HWTIMER_H__
#define __HWTIMER_H__
#include <rtthread.h>
#include <rtdevice.h>
#ifdef __cplusplus
extern "C" {
#endif
/* 定时器控制命令 */
typedef enum
{
HWTIMER_CTRL_TIMEOUT_SET = 0x01, /* 设置超时值 */
HWTIMER_CTRL_FREQ_SET, /* 设置计数频率 */
HWTIMER_CTRL_START, /* 启动定时器 */
HWTIMER_CTRL_STOP, /* 停止定时器 */
HWTIMER_CTRL_INFO_GET, /* 获取定时器特征信息 */
HWTIMER_CTRL_MODE_SET /* 设置定时模式 */
} rt_hwtimer_ctrl_t;
/* 定时模式 */
typedef enum
{
HWTIMER_MODE_ONESHOT = 0x01,
HWTIMER_MODE_PERIOD
} rt_hwtimer_mode_t;
/* 定时器计数值 */
typedef struct rt_hwtimerval
{
rt_int32_t sec; /* 秒 */
rt_int32_t usec; /* 微秒 */
} rt_hwtimerval_t;
#define HWTIMER_CNTMODE_UP 0x01 /* 定时器递增计数方式 */
#define HWTIMER_CNTMODE_DW 0x02 /* 定时器递减计数方式 */
struct rt_hwtimer_device;
struct rt_hwtimer_ops
{
void (*init)(struct rt_hwtimer_device *timer);
void (*deinit)(struct rt_hwtimer_device *timer);
void (*start)(struct rt_hwtimer_device *timer, rt_hwtimer_mode_t mode);
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_err_t (*control)(struct rt_hwtimer_device *timer, rt_uint32_t cmd, void *args);
};
/* 定时器基本参数(不可动态更改) */
struct rt_hwtimer_info
{
rt_int32_t maxfreq; /* 定时器支持的最大计数时钟 */
rt_int32_t minfreq; /* 定时器支持的最小计数时钟 */
rt_uint32_t maxcnt; /* 计数器最大超时值 */
rt_uint8_t cntmode; /* 计数模式(递增/递减,应用层无需关心此参数) */
};
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; /* 计数频率 */
rt_int32_t overflow; /* 溢出次数 */
float period_sec; /* 溢出周期(s) */
rt_int32_t cycles; /* 循环次数 */
rt_int32_t reload; /* 重载cycles */
rt_hwtimer_mode_t mode; /* 定时模式 */
} 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
@@ -468,6 +468,7 @@ static long _list_device(struct rt_list_node *list)
"Pipe",
"Portal Device",
"Miscellaneous Device",
"Hardware Timer Device",
"Unknown"
};
+2 -1
View File
@@ -759,7 +759,8 @@ enum rt_device_class_type
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_HwTimer, /**< Hardware timer device */
RT_Device_Class_Unknown /**< unknown device */
};
/**