summaryrefslogtreecommitdiff
path: root/components/drivers
diff options
context:
space:
mode:
Diffstat (limited to 'components/drivers')
-rw-r--r--components/drivers/hwtimer/hwtimer.c94
-rw-r--r--components/drivers/include/drivers/hwtimer.h53
2 files changed, 50 insertions, 97 deletions
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);