diff options
| author | heyuanjie87 <[email protected]> | 2015-09-06 15:05:47 +0800 |
|---|---|---|
| committer | heyuanjie87 <[email protected]> | 2015-09-06 15:05:47 +0800 |
| commit | d19f424909bbcaa22b222728aa94b65e4aa8a9ed (patch) | |
| tree | a6b86a535a94feb3ea074e93e24b363ea1875a97 | |
| parent | 47b3a4d3cc090dcf3a9abd554178acecc3c016cd (diff) | |
简化操作接口
| -rw-r--r-- | bsp/stm32f40x/drivers/drv_hwtimer.c | 53 | ||||
| -rw-r--r-- | components/drivers/hwtimer/hwtimer.c | 94 | ||||
| -rw-r--r-- | components/drivers/include/drivers/hwtimer.h | 53 | ||||
| -rw-r--r-- | examples/test/hwtimer_test.c | 24 |
4 files changed, 81 insertions, 143 deletions
diff --git a/bsp/stm32f40x/drivers/drv_hwtimer.c b/bsp/stm32f40x/drivers/drv_hwtimer.c index ba6aed9dcb..8802021e18 100644 --- a/bsp/stm32f40x/drivers/drv_hwtimer.c +++ b/bsp/stm32f40x/drivers/drv_hwtimer.c @@ -1,5 +1,5 @@ /* - * File : gpio.c + * File : drv_hwtimer.c * This file is part of RT-Thread RTOS * COPYRIGHT (C) 2015, RT-Thread Development Team * @@ -31,35 +31,34 @@ static void NVIC_Configuration(void) 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 = (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); + if (state == 1) + { + NVIC_Configuration(); + RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); + TIM_CounterModeConfig(tim, TIM_CounterMode_Up); + } } -static void timer_start(rt_hwtimer_t *timer, rt_hwtimer_mode_t opmode) +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) @@ -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); clk.PCLK1_Frequency *= 2; 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_ClearITPendingBit(TIM2, TIM_IT_Update); - TIM_ITConfig(tim, TIM_IT_Update, ENABLE); + TIM_ClearITPendingBit(TIM2, TIM_IT_Update); + TIM_ITConfig(tim, TIM_IT_Update, ENABLE); } break; default: @@ -116,31 +115,19 @@ static rt_uint32_t timer_counter_get(rt_hwtimer_t *timer) 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,/* ����������ʽ */ + 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_deinit, timer_start, timer_stop, - timer_timeout_set, timer_counter_get, timer_ctrl, }; diff --git a/components/drivers/hwtimer/hwtimer.c b/components/drivers/hwtimer/hwtimer.c index 0d2df8f8ea..22b8d6262c 100644 --- a/components/drivers/hwtimer/hwtimer.c +++ b/components/drivers/hwtimer/hwtimer.c @@ -25,9 +25,8 @@ #include <rtthread.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 timeout; 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; - if (timer->ops->stop != RT_NULL) - { - timer->ops->stop(timer); - } - else - { - return -RT_ENOSYS; - } - /* �Ѷ�ʱ�����ʱ��Ͷ�ʱʱ�任����� */ overflow = timer->info->maxcnt/(float)timer->freq; 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; counter = timeout*timer->freq; - if (timer->ops->timeout_set != RT_NULL) - { - err = timer->ops->timeout_set(timer, counter); - } - - return err; + return counter; } 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->cycles = 0; + timer->overflow = 0; if (timer->ops->init) { - timer->ops->init(timer); + timer->ops->init(timer, 1); } else { @@ -152,9 +138,9 @@ static rt_err_t rt_hwtimer_close(struct rt_device *dev) rt_hwtimer_t *timer; 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 { @@ -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) { - rt_size_t len = 0; - rt_hwtimerval_t *t; - rt_err_t err; + rt_uint32_t t; + rt_hwtimer_mode_t opm = HWTIMER_MODE_PERIOD; + rt_hwtimer_t *timer; + + 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 (size == sizeof(rt_hwtimerval_t)) + if ((timer->cycles <= 1) && (timer->mode == HWTIMER_MODE_ONESHOT)) { - t = (rt_hwtimerval_t*)buffer; - err = timeout_set((rt_hwtimer_t*)dev, t); - if (err == RT_EOK) - { - len = size; - } + opm = HWTIMER_MODE_ONESHOT; } + 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) @@ -221,31 +215,6 @@ static rt_err_t rt_hwtimer_control(struct rt_device *dev, rt_uint8_t cmd, void * 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: { 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; - 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: { 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->type = RT_Device_Class_HwTimer; + device->type = RT_Device_Class_Timer; device->rx_indicate = RT_NULL; device->tx_complete = RT_NULL; diff --git a/components/drivers/include/drivers/hwtimer.h b/components/drivers/include/drivers/hwtimer.h index e5b5d4661b..24cec74d32 100644 --- a/components/drivers/include/drivers/hwtimer.h +++ b/components/drivers/include/drivers/hwtimer.h @@ -8,54 +8,50 @@ extern "C" { #endif -/* ��ʱ���������� */ +/* Timer Control Command */ 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 /* ���ö�ʱģʽ */ + 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; /* �� */ - rt_int32_t usec; /* �� */ + rt_int32_t sec; /* second */ + rt_int32_t usec; /* microsecond */ } rt_hwtimerval_t; -#define HWTIMER_CNTMODE_UP 0x01 /* ��ʱ������������ʽ */ -#define HWTIMER_CNTMODE_DW 0x02 /* ��ʱ���ݼ�������ʽ */ +#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); - void (*deinit)(struct rt_hwtimer_device *timer); - void (*start)(struct rt_hwtimer_device *timer, rt_hwtimer_mode_t mode); + 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_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); }; -/* ��ʱ����������(���ɶ�̬����) */ +/* Timer Feature Information */ struct rt_hwtimer_info { - rt_int32_t maxfreq; /* ��ʱ��֧�ֵ�������ʱ�� */ - rt_int32_t minfreq; /* ��ʱ��֧�ֵ���С����ʱ�� */ - rt_uint32_t maxcnt; /* ���������ʱֵ */ - rt_uint8_t cntmode; /* ����ģʽ(����/�ݼ�,Ӧ�ò�������Ĵ˲���) */ + 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 @@ -64,13 +60,12 @@ typedef struct rt_hwtimer_device 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_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); diff --git a/examples/test/hwtimer_test.c b/examples/test/hwtimer_test.c index e90f5c4783..8f69c4ef87 100644 --- a/examples/test/hwtimer_test.c +++ b/examples/test/hwtimer_test.c @@ -13,15 +13,15 @@ static rt_err_t timer_timeout_cb(rt_device_t dev, rt_size_t size) return 0; } -int hwtimer(int freq, int t) +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; - - t = (t <= 0)? 5 : t; + int freq = 10000; + int t = 5; 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) { rt_kprintf("Open %s Fail\n", TIMER); - return -1; + return -1; } rt_device_set_rx_indicate(dev, timer_timeout_cb); @@ -44,7 +44,13 @@ int hwtimer(int freq, int t) 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); @@ -53,14 +59,6 @@ int hwtimer(int freq, int t) rt_kprintf("SetTime Fail\n"); 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_thread_delay(t*RT_TICK_PER_SECOND); |
