4.硬件定時(shí)器的使用和學(xué)習(xí)
這里依然使用mdk的看法環(huán)境,使用mdk編譯程序,下載程序
4.1配置使能硬件定時(shí)器2
4.2 編寫(xiě)定時(shí)器的測(cè)試函數(shù)
/*
程序清單:這是一個(gè) hwtimer 設(shè)備使用例程
例程導(dǎo)出了 hwtimer_sample 命令到控制終端
命令調(diào)用格式:hwtimer_sample
程序功能:硬件定時(shí)器超時(shí)回調(diào)函數(shù)周期性的打印當(dāng)前tick值,2次tick值之差換算為時(shí)間等同于定時(shí)時(shí)間值。
/
#include
#include
#define HWTIMER_DEV_NAME "time2" / 定時(shí)器名稱(chēng) /
/ 定時(shí)器超時(shí)回調(diào)函數(shù) /
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; / 定時(shí)器超時(shí)值 /
rt_device_t hw_dev = RT_NULL; / 定時(shí)器設(shè)備句柄 /
rt_hwtimer_mode_t mode; / 定時(shí)器模式 /
rt_uint32_t freq = 10000; / 計(jì)數(shù)頻率 /
/ 查找定時(shí)器設(shè)備 /
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;
}
/ 以讀寫(xiě)方式打開(kāi)設(shè)備 /
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;
}
/ 設(shè)置超時(shí)回調(diào)函數(shù) /
rt_device_set_rx_indicate(hw_dev, timeout_cb);
/ 設(shè)置計(jì)數(shù)頻率(若未設(shè)置該項(xiàng),默認(rèn)為1Mhz 或 支持的最小計(jì)數(shù)頻率) /
rt_device_control(hw_dev, HWTIMER_CTRL_FREQ_SET, &freq);
/ 設(shè)置模式為周期性定時(shí)器(若未設(shè)置,默認(rèn)是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;
}
/ 設(shè)置定時(shí)器超時(shí)值為5s并啟動(dòng)定時(shí)器 /
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;
}
/ 延時(shí)3500ms /
rt_thread_mdelay(3500);
/ 讀取定時(shí)器當(dāng)前值 /
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;
}
/ 導(dǎo)出到 msh 命令列表中 */
MSH_CMD_EXPORT(hwtimer_sample, hwtimer sample);
4.3測(cè)試函數(shù),查看運(yùn)行結(jié)果
4.4硬件定時(shí)器設(shè)備驅(qū)動(dòng)框架學(xué)習(xí)
使用方法:
4.4.1實(shí)現(xiàn)定時(shí)器的各個(gè)操作函數(shù)
/*
- 定時(shí)器 初始化函數(shù)
- 定時(shí)器起始函數(shù)
- 定時(shí)器停止函數(shù)
- 定時(shí)器的計(jì)數(shù)值獲取
- 定時(shí)器的控制函數(shù)
*/
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配置定時(shí)器的基本參數(shù)
/ 定時(shí)器特征描述 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; / 最大計(jì)數(shù) 值counter maximum value /
rt_uint8_t cntmode; / 計(jì)數(shù)方向 count mode (inc/dec) */
};
typedef struct rt_hwtimer_device
{
struct rt_device parent;//基本設(shè)備驅(qū)動(dòng)框架
const struct rt_hwtimer_ops *ops;//定時(shí)器特有的操作函數(shù)
const struct rt_hwtimer_info info;//定時(shí)器相關(guān)的參數(shù)信息
rt_int32_t freq; / 用戶(hù)設(shè)置的計(jì)數(shù)頻率 counting frequency set by the user /
rt_int32_t overflow; / 定時(shí)器溢出 timer overflows /
float period_sec;
rt_int32_t cycles; / 溢出后將生成超時(shí)事件多少次 how many times will generate a timeout event after overflow /
rt_int32_t reload; / 重新加載循環(huán)(使用周期模式) reload cycles(using in period mode) /
rt_hwtimer_mode_t mode; / 計(jì)時(shí)模式(一次/周期) timing mode(oneshot/period) /
} rt_hwtimer_t;
4.4.3注冊(cè)定時(shí)器的設(shè)備到驅(qū)動(dòng)框架
/
定時(shí)器設(shè)備注冊(cè)函數(shù)
*/
rt_err_t rt_device_hwtimer_register(rt_hwtimer_t *timer, const char *name, void *user_data);
4.4.4詳細(xì)的定時(shí)器設(shè)備驅(qū)動(dòng)相關(guān)
/*
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
/ 定時(shí)器的控制命令類(lèi)型 /
typedef enum
{
HWTIMER_CTRL_FREQ_SET = RT_DEVICE_CTRL_BASE(Timer) + 0x01, / 設(shè)置技術(shù)的頻率set the count frequency /
HWTIMER_CTRL_STOP = RT_DEVICE_CTRL_BASE(Timer) + 0x02, / 停止定時(shí)器stop timer /
HWTIMER_CTRL_INFO_GET = RT_DEVICE_CTRL_BASE(Timer) + 0x03, / 獲取計(jì)時(shí)器功能信息 get a timer feature information /
HWTIMER_CTRL_MODE_SET = RT_DEVICE_CTRL_BASE(Timer) + 0x04 / 設(shè)置定時(shí)器的工作模式 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;
/ 計(jì)數(shù)的方向 /
#define HWTIMER_CNTMODE_UP 0x01 / increment count mode /
#define HWTIMER_CNTMODE_DW 0x02 / decreasing count mode /
struct rt_hwtimer_device;
/
- 定時(shí)器 初始化函數(shù)
- 定時(shí)器起始函數(shù)
- 定時(shí)器停止函數(shù)
- 定時(shí)器的計(jì)數(shù)值獲取
- 定時(shí)器的控制函數(shù)
*/
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);
};
/ 定時(shí)器特征描述 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; / 最大計(jì)數(shù) 值counter maximum value /
rt_uint8_t cntmode; / 計(jì)數(shù)方向 count mode (inc/dec) */
};
typedef struct rt_hwtimer_device
{
struct rt_device parent;//基本設(shè)備驅(qū)動(dòng)框架
const struct rt_hwtimer_ops *ops;//定時(shí)器特有的操作函數(shù)
const struct rt_hwtimer_info info;//定時(shí)器相關(guān)的參數(shù)信息
rt_int32_t freq; / 用戶(hù)設(shè)置的計(jì)數(shù)頻率 counting frequency set by the user /
rt_int32_t overflow; / 定時(shí)器溢出 timer overflows /
float period_sec;
rt_int32_t cycles; / 溢出后將生成超時(shí)事件多少次 how many times will generate a timeout event after overflow /
rt_int32_t reload; / 重新加載循環(huán)(使用周期模式) reload cycles(using in period mode) /
rt_hwtimer_mode_t mode; / 計(jì)時(shí)模式(一次/周期) timing mode(oneshot/period) /
} rt_hwtimer_t;
/
定時(shí)器設(shè)備注冊(cè)函數(shù)
*/
rt_err_t rt_device_hwtimer_register(rt_hwtimer_t *timer, const char *name, void *user_data);
/ 定時(shí)器回調(diào)函數(shù) /
void rt_device_hwtimer_isr(rt_hwtimer_t *timer);
#ifdef __cplusplus
}
#endif
#endif
-
驅(qū)動(dòng)器
+關(guān)注
關(guān)注
53文章
8263瀏覽量
146683 -
定時(shí)器
+關(guān)注
關(guān)注
23文章
3254瀏覽量
115071 -
計(jì)時(shí)器
+關(guān)注
關(guān)注
1文章
426瀏覽量
32780 -
回調(diào)函數(shù)
+關(guān)注
關(guān)注
0文章
87瀏覽量
11598 -
RT-Thread
+關(guān)注
關(guān)注
31文章
1301瀏覽量
40265
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論