4.硬件定時器的使用和學習
這里依然使用mdk的看法環境,使用mdk編譯程序,下載程序
4.1配置使能硬件定時器2
4.2 編寫定時器的測試函數
/*
程序清單:這是一個 hwtimer 設備使用例程
例程導出了 hwtimer_sample 命令到控制終端
命令調用格式:hwtimer_sample
程序功能:硬件定時器超時回調函數周期性的打印當前tick值,2次tick值之差換算為時間等同于定時時間值。
/
#include
#include
#define HWTIMER_DEV_NAME "time2" / 定時器名稱 /
/ 定時器超時回調函數 /
static rt_err_t timeout_cb(rt_device_t dev, rt_size_t size)
{
rt_kprintf("this is hwtimer timeout callback fucntion!n");
rt_kprintf("tick is :%d !n", rt_tick_get());
return 0;
}
int hwtimer_sample(void)
{
rt_err_t ret = RT_EOK;
rt_hwtimerval_t timeout_s; / 定時器超時值 /
rt_device_t hw_dev = RT_NULL; / 定時器設備句柄 /
rt_hwtimer_mode_t mode; / 定時器模式 /
rt_uint32_t freq = 10000; / 計數頻率 /
/ 查找定時器設備 /
hw_dev = rt_device_find(HWTIMER_DEV_NAME);
if (hw_dev == RT_NULL)
{
rt_kprintf("hwtimer sample run failed! can't find %s device!n", HWTIMER_DEV_NAME);
return RT_ERROR;
}
/ 以讀寫方式打開設備 /
ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
if (ret != RT_EOK)
{
rt_kprintf("open %s device failed!n", HWTIMER_DEV_NAME);
return ret;
}
/ 設置超時回調函數 /
rt_device_set_rx_indicate(hw_dev, timeout_cb);
/ 設置計數頻率(若未設置該項,默認為1Mhz 或 支持的最小計數頻率) /
rt_device_control(hw_dev, HWTIMER_CTRL_FREQ_SET, &freq);
/ 設置模式為周期性定時器(若未設置,默認是HWTIMER_MODE_ONESHOT)/
mode = HWTIMER_MODE_PERIOD;
ret = rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, &mode);
if (ret != RT_EOK)
{
rt_kprintf("set mode failed! ret is :%dn", ret);
return ret;
}
/ 設置定時器超時值為5s并啟動定時器 /
timeout_s.sec = 5; / 秒 /
timeout_s.usec = 0; / 微秒 /
if (rt_device_write(hw_dev, 0, &timeout_s, sizeof(timeout_s)) != sizeof(timeout_s))
{
rt_kprintf("set timeout value failedn");
return RT_ERROR;
}
/ 延時3500ms /
rt_thread_mdelay(3500);
/ 讀取定時器當前值 /
rt_device_read(hw_dev, 0, &timeout_s, sizeof(timeout_s));
rt_kprintf("Read: Sec = %d, Usec = %dn", timeout_s.sec, timeout_s.usec);
return ret;
}
/ 導出到 msh 命令列表中 */
MSH_CMD_EXPORT(hwtimer_sample, hwtimer sample);
4.3測試函數,查看運行結果
4.4硬件定時器設備驅動框架學習
使用方法:
4.4.1實現定時器的各個操作函數
/*
- 定時器 初始化函數
- 定時器起始函數
- 定時器停止函數
- 定時器的計數值獲取
- 定時器的控制函數
*/
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);
};
4.4.2配置定時器的基本參數
/ 定時器特征描述 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;
4.4.3注冊定時器的設備到驅動框架
/
定時器設備注冊函數
*/
rt_err_t rt_device_hwtimer_register(rt_hwtimer_t *timer, const char *name, void *user_data);
4.4.4詳細的定時器設備驅動相關
/*
Copyright (c) 2006-2023, RT-Thread Development Team
SPDX-License-Identifier: Apache-2.0
Change Logs:
Date Author Notes
/
#ifndef HWTIMER_H
#define HWTIMER_H
#include
#ifdef __cplusplus
extern "C" {
#endif
/ 定時器的控制命令類型 /
typedef enum
{
HWTIMER_CTRL_FREQ_SET = RT_DEVICE_CTRL_BASE(Timer) + 0x01, / 設置技術的頻率set the count frequency /
HWTIMER_CTRL_STOP = RT_DEVICE_CTRL_BASE(Timer) + 0x02, / 停止定時器stop timer /
HWTIMER_CTRL_INFO_GET = RT_DEVICE_CTRL_BASE(Timer) + 0x03, / 獲取計時器功能信息 get a timer feature information /
HWTIMER_CTRL_MODE_SET = RT_DEVICE_CTRL_BASE(Timer) + 0x04 / 設置定時器的工作模式 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
-
驅動器
+關注
關注
53文章
8268瀏覽量
146814 -
定時器
+關注
關注
23文章
3255瀏覽量
115163 -
計時器
+關注
關注
1文章
426瀏覽量
32795 -
回調函數
+關注
關注
0文章
87瀏覽量
11606 -
RT-Thread
+關注
關注
31文章
1305瀏覽量
40302
發布評論請先 登錄
相關推薦
評論