

延時(shí)型硬件定時(shí)器: 由硬件定時(shí)器外設(shè)提供的延時(shí)功能。
計(jì)數(shù)型硬件定時(shí)器: 提供較精確的類(lèi)似時(shí)間戳的功能。
周期型硬件定時(shí)器: 可設(shè)置中斷頻率的計(jì)數(shù)器,不僅能提供計(jì)數(shù)器的功能,也能根據(jù)中斷頻率提供更精確的定時(shí)。
輸入捕獲定時(shí)器: 可測(cè)量脈沖寬度或者測(cè)量頻率。


/**
* rief 硬件定時(shí)器中斷服務(wù)函數(shù)。
* param[in] p_arg : 任務(wù)參數(shù)
*/
static void mytimer_isr (void *p_arg)
{
aw_gpio_toggle((int)p_arg);
aw_kprintf("enter isr
");
}
/**
* rief hwtimer 測(cè)試函數(shù)
*/
aw_local void* __task_handle (void *arg)
{
int fd;
aw_err_t ret;
uint32_t count = 5;
aw_hwtimer_rate_t p_intr_freq;
p_intr_freq.rate_denominator = 5;
p_intr_freq.rate_numerator = 1;
fd = aw_open(CONFIG_DEMO_HWTIMER_PEROID_DEV_NAME, AW_O_RDWR, 0);
if (fd < 0) {
aw_kprintf("hwtimer open failed:%d
", fd);
while(1);
}
ret = aw_hwtimer_period_intr_freq_set_frac(fd, &p_intr_freq);
while (count) {
aw_hwtimer_period_wait(fd, 500);
mytimer_isr(arg);
count --;
}
// 配置每秒中斷2次
ret = aw_hwtimer_period_intr_freq_set(fd, 2);
ret = aw_hwtimer_period_start(fd);
if (ret != AW_OK) {
aw_kprintf("Timer allocation fail!
");
}
ret = aw_hwtimer_period_wait(fd, AW_WAIT_FOREVER);
while (1) {
aw_hwtimer_period_wait(fd, AW_WAIT_FOREVER);
mytimer_isr(arg);
}
for (;;) {
aw_mdelay(1000);
}
aw_close(fd);
return 0;
}
下表為使用硬件周期型定時(shí)器,在中斷中進(jìn)行引腳翻轉(zhuǎn),通過(guò)邏輯分析儀所測(cè)量出的實(shí)際數(shù)據(jù),在使用設(shè)計(jì)時(shí)可作為部分參考依據(jù)。

aw_local void* __task_handle (void *arg)
{
uint32_t count = 0;
int fd, led_fd;
int ret;
uint32_t start_count;
fd = aw_open(CONFIG_DEMO_HWTIMER_PEROID_DEV_NAME, AW_O_RDWR, 0);
if (fd < 0) {
aw_kprintf("hwtimer open fail! :%d
",fd);
return;
}
/* 打開(kāi)設(shè)備會(huì)點(diǎn)亮LED */
led_fd = aw_open("/dev/led_run", AW_O_RDWR, 0);
if (led_fd < 0) {
aw_kprintf("led open fail! :%d
", led_fd);
aw_close(fd);
return;
}
ret = aw_hwtimer_count_rate_get(fd, &start_count);
if (ret != AW_OK) {
aw_kprintf("Timer count rate get fail!
");
aw_close(fd);
aw_close(led_fd);
return;
}
// 設(shè)置時(shí)鐘頻率
ret = aw_hwtimer_count_rate_set(fd, start_count/2);
if (ret != AW_OK) {
aw_kprintf("Timer count rate set fail!
");
aw_close(fd);
aw_close(led_fd);
return;
}
ret = aw_hwtimer_count_start(fd);
if (ret != AW_OK) {
aw_kprintf("Timer start fail!
");
aw_close(fd);
aw_close(led_fd);
return;
}
for (;;) {
aw_led_toggle(led_fd);
aw_mdelay(500);
aw_led_toggle(led_fd);
aw_hwtimer_count_get(fd, &count);
aw_kprintf("Count is %d
", count);
}
aw_close(fd);
aw_close(led_fd);
return 0;
}


aw_local void* __task_handle (void *arg)
{
int i;
int fd;
aw_err_t ret;
aw_timespec_t timespec;
aw_timestamp_t start_timestamp, stop_timestamp;
aw_timestamp_freq_t timestamp_freq;
uint64_t delay_ns, diff;
uint32_t ns_numerator = 1000000000;
timestamp_freq = aw_timestamp_freq_get();
while (0 == (timestamp_freq % 10)) {
timestamp_freq /= 10;
ns_numerator /= 10;
}
fd = aw_open(CONFIG_DEMO_HWTIMER_DELAY_DEV_NAME, AW_O_RDWR, 0);
if (fd < 0) {
aw_kprintf("hwtimer open failed:%d
", fd);
while(1);
}
delay_ns = 2001000;
for (i = 0; i < 100; i++) {
timespec.tv_sec = delay_ns / 1000000000u;
timespec.tv_nsec = (uint32_t)(delay_ns % 1000000000u);
start_timestamp = aw_timestamp_get();
ret = aw_hwtimer_delay(fd, ×pec);
if (ret !=AW_OK) {
aw_kprintf("hwtimer delay failed:%d
", ret);
}
aw_barrier();
stop_timestamp = aw_timestamp_get();
stop_timestamp -= start_timestamp;
diff = stop_timestamp;
diff *= ns_numerator;
diff /= timestamp_freq;
diff = diff - delay_ns;
aw_kprintf(
"hwtimer_delay delay = %u,diff = %u ns
",
(uint32_t)delay_ns,
(uint32_t)diff);
delay_ns += 100000;
}
aw_close(fd);
return 0;
}

{SDK}demosperipheralcap路徑下為捕獲型定時(shí)器例程,例程關(guān)鍵代碼如下:
/* 單邊沿觸發(fā)*/
static void test_cap_single_edge(
int fd,
int gpio_cap,
uint32_t ms,
aw_hwtimer_cap_config_t *p_config,
int is_rising)
{
uint64_t cap_val1, cap_val2;
aw_err_t ret;
// 制造兩次上升沿
mk_edge(gpio_cap, 5);
aw_task_delay(ms);
mk_edge(gpio_cap, 5);
// 此時(shí)應(yīng)該產(chǎn)生了兩次捕獲事件
// 把它們讀出來(lái)
ret = aw_hwtimer_cap_read(fd, &cap_val1, AW_WAIT_FOREVER);
if (AW_OK != ret) {
aw_kprintf("cap read cap_val1 failed
");
return;
}
ret = aw_hwtimer_cap_read(fd, &cap_val2, AW_WAIT_FOREVER);
if (AW_OK != ret) {
aw_kprintf("cap read cap_val2 failed
");
return;
}
cap_val2 -= cap_val1;
cap_val2 *= 1000000;
cap_val2 /= p_config->sample_rate;
if (is_rising) {
aw_kprintf("two rising edge between %u ms
", ms + 5);
}
else {
aw_kprintf("two falling edge between %u ms
", ms + 5);
}
aw_kprintf("two capture events between %llu us
", cap_val2);
}
static void demo_cap_base(int gpio_cap)
{
int fd;
aw_err_t ret;
aw_hwtimer_cap_config_t config;
// 使得測(cè)試GPIO輸出為0
aw_gpio_set(gpio_cap, 0);
fd = aw_open(CONFIG_DEMO_HWTIMER_CAP_DEV_NAME, AW_O_RDWR, 0);
if (fd < 0) {
aw_kprintf("cap open failed!
");
return;
}
// 獲取捕獲定時(shí)器的配置
ret = aw_hwtimer_cap_config_get(fd, &config);
if (ret != AW_OK) {
aw_kprintf("cap config get failed...
");
aw_close(fd);
return ;
}
int is_rising;
// 配置為上升沿觸發(fā)捕獲
config.cap_edge_flags = AW_CAPTURE_RISING_EDGE;
is_rising = 1;
ret = aw_hwtimer_cap_config_set(fd, &config);
if (ret != AW_OK) {
aw_kprintf("cap config set failed...
");
aw_close(fd);
return ;
}
ret = aw_hwtimer_cap_start(fd);
if (ret != AW_OK) {
aw_kprintf("cap start failed...
");
aw_close(fd);
return ;
}
test_cap_single_edge(fd, gpio_cap, 20, &config, is_rising);
aw_close(fd);
}






至此,所有類(lèi)型的硬件定時(shí)器樣例均已展示完畢,在軟件應(yīng)用設(shè)計(jì)中可根據(jù)實(shí)際需求選取不同類(lèi)型的定時(shí)器進(jìn)行使用。更多其他類(lèi)型外設(shè)的用法介紹,請(qǐng)關(guān)注后續(xù)同系列推文~






-
致遠(yuǎn)電子
+關(guān)注
關(guān)注
13文章
409瀏覽量
31501
原文標(biāo)題:【產(chǎn)品應(yīng)用】AWorksLP 樣例詳解(MR6450)—— HWTimer
文章出處:【微信號(hào):ZLG_zhiyuan,微信公眾號(hào):ZLG致遠(yuǎn)電子】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
深度解析 | 基于HPM6450的RISC-V核心板究竟有哪些過(guò)人之處?

40mR/650V SiC 碳化硅MOSFET,替代30mR 超結(jié)MOSFET或者20-30mR的GaN!
關(guān)于cc2541程序代碼樣例
深蕾半導(dǎo)體開(kāi)發(fā)者中心上線(xiàn)多個(gè)AI Demo樣例

為何ZLG致遠(yuǎn)電子要推出LGA嵌入式核心板?

差分硅振替換SiTime產(chǎn)品應(yīng)用于SSD,相位抖動(dòng)低于350fs

使用官方SDK開(kāi)發(fā)6450
TPA3255產(chǎn)品應(yīng)用在消防廣播,固定輸入下, 如果輸出負(fù)載變化時(shí),輸出電壓還能保持不變嗎?
武漢凡谷:現(xiàn)階段已有產(chǎn)品應(yīng)用到5.5G系統(tǒng)
【Vision Board創(chuàng)客營(yíng)連載體驗(yàn)】HWTIMER設(shè)備體驗(yàn)
AI時(shí)代下的場(chǎng)效應(yīng)管功能運(yùn)用與產(chǎn)品應(yīng)用

淺談智慧校園的用電安全管理分析及產(chǎn)品應(yīng)用

評(píng)論