4.硬件定時器的使用和學(xué)習(xí)
這里依然使用mdk的看法環(huán)境,使用mdk編譯程序,下載程序
4.1配置使能硬件定時器2
4.2 編寫定時器的測試函數(shù)
/*
程序清單:這是一個 hwtimer 設(shè)備使用例程
例程導(dǎo)出了 hwtimer_sample 命令到控制終端
命令調(diào)用格式:hwtimer_sample
程序功能:硬件定時器超時回調(diào)函數(shù)周期性的打印當(dāng)前tick值,2次tick值之差換算為時間等同于定時時間值。
/
#include
#include
#define HWTIMER_DEV_NAME "time2" / 定時器名稱 /
/ 定時器超時回調(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; / 定時器超時值 /
rt_device_t hw_dev = RT_NULL; / 定時器設(shè)備句柄 /
rt_hwtimer_mode_t mode; / 定時器模式 /
rt_uint32_t freq = 10000; / 計數(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;
}
/ 以讀寫方式打開設(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è)置超時回調(diào)函數(shù) /
rt_device_set_rx_indicate(hw_dev, timeout_cb);
/ 設(shè)置計數(shù)頻率(若未設(shè)置該項,默認(rèn)為1Mhz 或 支持的最小計數(shù)頻率) /
rt_device_control(hw_dev, HWTIMER_CTRL_FREQ_SET, &freq);
/ 設(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è)置定時器超時值為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);
/ 讀取定時器當(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測試函數(shù),查看運(yùn)行結(jié)果
4.4硬件定時器設(shè)備驅(qū)動框架學(xué)習(xí)
使用方法:
4.4.1實(shí)現(xiàn)定時器的各個操作函數(shù)
/*
- 定時器 初始化函數(shù)
- 定時器起始函數(shù)
- 定時器停止函數(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ù)
/ 定時器特征描述 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; / 最大計數(shù) 值counter maximum value /
rt_uint8_t cntmode; / 計數(shù)方向 count mode (inc/dec) */
};
typedef struct rt_hwtimer_device
{
struct rt_device parent;//基本設(shè)備驅(qū)動框架
const struct rt_hwtimer_ops *ops;//定時器特有的操作函數(shù)
const struct rt_hwtimer_info info;//定時器相關(guān)的參數(shù)信息
rt_int32_t freq; / 用戶設(shè)置的計數(shù)頻率 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; / 重新加載循環(huán)(使用周期模式) reload cycles(using in period mode) /
rt_hwtimer_mode_t mode; / 計時模式(一次/周期) timing mode(oneshot/period) /
} rt_hwtimer_t;
4.4.3注冊定時器的設(shè)備到驅(qū)動框架
/
定時器設(shè)備注冊函數(shù)
*/
rt_err_t rt_device_hwtimer_register(rt_hwtimer_t *timer, const char *name, void *user_data);
4.4.4詳細(xì)的定時器設(shè)備驅(qū)動相關(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
/ 定時器的控制命令類型 /
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, / 停止定時器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 / 設(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;
/ 計數(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ù)
*/
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; / 最大計數(shù) 值counter maximum value /
rt_uint8_t cntmode; / 計數(shù)方向 count mode (inc/dec) */
};
typedef struct rt_hwtimer_device
{
struct rt_device parent;//基本設(shè)備驅(qū)動框架
const struct rt_hwtimer_ops *ops;//定時器特有的操作函數(shù)
const struct rt_hwtimer_info info;//定時器相關(guān)的參數(shù)信息
rt_int32_t freq; / 用戶設(shè)置的計數(shù)頻率 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; / 重新加載循環(huán)(使用周期模式) reload cycles(using in period mode) /
rt_hwtimer_mode_t mode; / 計時模式(一次/周期) timing mode(oneshot/period) /
} rt_hwtimer_t;
/
定時器設(shè)備注冊函數(shù)
*/
rt_err_t rt_device_hwtimer_register(rt_hwtimer_t *timer, const char *name, void *user_data);
/ 定時器回調(diào)函數(shù) /
void rt_device_hwtimer_isr(rt_hwtimer_t *timer);
#ifdef __cplusplus
}
#endif
#endif
-
驅(qū)動器
+關(guān)注
關(guān)注
53文章
8288瀏覽量
147139 -
定時器
+關(guān)注
關(guān)注
23文章
3256瀏覽量
115442 -
計時器
+關(guān)注
關(guān)注
1文章
426瀏覽量
32877 -
回調(diào)函數(shù)
+關(guān)注
關(guān)注
0文章
87瀏覽量
11626 -
RT-Thread
+關(guān)注
關(guān)注
31文章
1305瀏覽量
40419
發(fā)布評論請先 登錄
相關(guān)推薦
如何利用STM32L475開發(fā)板去處理定時器捕獲模塊應(yīng)用程序
【實(shí)驗(yàn)38】定時器定時
基于MCU的模塊定時器的詳細(xì)解析
新唐 NuMaker-M2354模塊評測任務(wù)大挑戰(zhàn)
基于硬件定時器的軟件定時器
![基于<b class='flag-5'>硬件</b><b class='flag-5'>定時器</b>的軟件<b class='flag-5'>定時器</b>](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
基于cubemx的stm32開發(fā)之路(使用正點(diǎn)原子戰(zhàn)艦V3開發(fā)板)——基本定時器的應(yīng)用
![基于cubemx的stm32<b class='flag-5'>開發(fā)</b>之路(使用正點(diǎn)原子戰(zhàn)艦V3<b class='flag-5'>開發(fā)板</b>)——基本<b class='flag-5'>定時器</b>的應(yīng)用](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
MM32F0140定時器模塊計數(shù)定時功能
![MM32F0140<b class='flag-5'>定時器</b><b class='flag-5'>模塊</b>計數(shù)<b class='flag-5'>定時</b>功能](https://file.elecfans.com/web2/M00/3B/C7/poYBAGJOn5uAQN40AAFhwdiBQa8543.png)
Linux驅(qū)動開發(fā)高精度定時器的精度測量評測
【合宙Air105開發(fā)板試用體驗(yàn)】小小定時器,能有大作用!
N32L40XCL-STB 開發(fā)板模塊評測任務(wù)大挑戰(zhàn)
英飛凌開發(fā)板模塊評測任務(wù)大挑戰(zhàn)
英飛凌開發(fā)板模塊評測任務(wù)大挑戰(zhàn)-SPI驅(qū)動測試
![<b class='flag-5'>英飛凌</b><b class='flag-5'>開發(fā)板</b><b class='flag-5'>模塊</b><b class='flag-5'>評測</b><b class='flag-5'>任務(wù)</b>大<b class='flag-5'>挑戰(zhàn)</b>-SPI驅(qū)動測試](https://file1.elecfans.com/web2/M00/90/0B/wKgZomTUlPOAN1-mAAF-wfqPnag600.jpg)
評論