Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
NXP 微控制器
硬件定时器
【NXP-MCXA153】 定时器驱动移植
发布于 2024-12-17 10:31:01 浏览:163
订阅该版
[tocm] ## 介绍 NXP MCXA153开发板上一共有3个CTimer定时器,每一路均支持捕获和输出模式 ## 移植流程 **以CTIMER为例** ① 在board里边添加相应的外设:配置CTIMER的时钟源 ② 添加相应的Kconfig开关,用以指示相应的外设开启与关闭(本质是通过宏定义或者条件编译的方式) ③ 根据[SDK_2_14_2_FRDM-MCXA153](https://mcuxpresso.nxp.com/zh/builder?hw=FRDM-MCXA153)提供的WWDT示例工程编写wdt驱动,需要实现3个关键的函数 - rt_hw_hwtimer_init - mcxa_ctimer_init - mcxa_ctimer_start - mcxa_ctimer_stop - mcxa_ctimer_count_get - mcxa_ctimer_control ④ 添加相应的库文件依赖:fsl_ctimer.c ## 驱动文件 ### pin_mux.c 在`BOARD_InitPins`函数里加入以下代码:配置TIMER0、TIIMER1、IMTER2的时钟源(kFRO_HF表示96MHz分频时钟源)以及外设复位 ```c CLOCK_SetClockDiv(kCLOCK_DivCTIMER0, 1u); CLOCK_AttachClk(kFRO_HF_to_CTIMER0); CLOCK_SetClockDiv(kCLOCK_DivCTIMER1, 1u); CLOCK_AttachClk(kFRO_HF_to_CTIMER1); CLOCK_SetClockDiv(kCLOCK_DivCTIMER2, 1u); CLOCK_AttachClk(kFRO_HF_to_CTIMER2); RESET_ReleasePeripheralReset(kCTIMER0_RST_SHIFT_RSTn); RESET_ReleasePeripheralReset(kCTIMER1_RST_SHIFT_RSTn); RESET_ReleasePeripheralReset(kCTIMER2_RST_SHIFT_RSTn); ``` ### board/Kconfig 加入CTIMER相关配置,这里是menuconfig菜单的选项部分 ``` menuconfig BSP_USING_HWTIMER config BSP_USING_HWTIMER bool "Enable Hardware Timer" select RT_USING_HWTIMER default y if BSP_USING_HWTIMER config BSP_USING_CTIMER0 bool "Enable CIMER0" default y config BSP_USING_CTIMER1 bool "Enable CIMER1" default n config BSP_USING_CTIMER2 bool "Enable CIMER2" default n endif ``` ### drv_hwtimer.c 硬件定时器驱动代码:编写定时器设备初始化、控制设置(启动、停止、读取计数)及中断响应代码 ```c /* * Copyright (c) 2006-2024 RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2024-11-26 hywing the first version. * */ #include
#ifdef BSP_USING_HWTIMER #define LOG_TAG "drv.hwtimer" #include
#include
#include "fsl_ctimer.h" enum { #ifdef BSP_USING_CTIMER0 TIM0_INDEX, #endif #ifdef BSP_USING_CTIMER1 TIM1_INDEX, #endif #ifdef BSP_USING_CTIMER2 TIM2_INDEX, #endif }; #ifdef BSP_USING_CTIMER0 #define TIM0_CONFIG \ { \ .tim_handle = CTIMER0, \ .tim_irqn = CTIMER0_IRQn, \ .name = "timer0", \ } #endif /* TIM0_CONFIG */ #ifdef BSP_USING_CTIMER1 #define TIM1_CONFIG \ { \ .tim_handle = CTIMER1, \ .tim_irqn = CTIMER1_IRQn, \ .name = "timer1", \ } #endif /* TIM1_CONFIG */ #ifdef BSP_USING_CTIMER2 #define TIM2_CONFIG \ { \ .tim_handle = CTIMER2, \ .tim_irqn = CTIMER2_IRQn, \ .name = "timer2", \ } #endif /* TIM2_CONFIG */ struct mcxa_hwtimer { rt_hwtimer_t time_device; CTIMER_Type* tim_handle; enum IRQn tim_irqn; char* name; }; static struct mcxa_hwtimer mcxa_hwtimer_obj[] = { #ifdef BSP_USING_CTIMER0 TIM0_CONFIG, #endif #ifdef BSP_USING_CTIMER1 TIM1_CONFIG, #endif #ifdef BSP_USING_CTIMER2 TIM2_CONFIG, #endif }; static void NVIC_Configuration(void) { #ifdef BSP_USING_CTIMER0 EnableIRQ(CTIMER0_IRQn); #endif #ifdef BSP_USING_CTIMER1 EnableIRQ(CTIMER1_IRQn); #endif #ifdef BSP_USING_CTIMER2 EnableIRQ(CTIMER2_IRQn); #endif } static rt_err_t mcxa_ctimer_control(rt_hwtimer_t *timer, rt_uint32_t cmd, void *args) { rt_err_t err = RT_EOK; CTIMER_Type *hwtimer_dev; hwtimer_dev = (CTIMER_Type *)timer->parent.user_data; RT_ASSERT(timer != RT_NULL); switch (cmd) { case HWTIMER_CTRL_FREQ_SET: { uint32_t clk; uint32_t pre; if(hwtimer_dev == CTIMER0) clk = CLOCK_GetCTimerClkFreq(0U); if(hwtimer_dev == CTIMER1) clk = CLOCK_GetCTimerClkFreq(1U); if(hwtimer_dev == CTIMER2) clk = CLOCK_GetCTimerClkFreq(2U); pre = clk / *((uint32_t *)args) - 1; hwtimer_dev->PR = pre; } break; default: err = -RT_ENOSYS; break; } return err; } static rt_uint32_t mcxa_ctimer_count_get(rt_hwtimer_t *timer) { rt_uint32_t CurrentTimer_Count; CTIMER_Type *hwtimer_dev; hwtimer_dev = (CTIMER_Type *)timer->parent.user_data; RT_ASSERT(timer != RT_NULL); CurrentTimer_Count = hwtimer_dev->TC; return CurrentTimer_Count; } static void mcxa_ctimer_init(rt_hwtimer_t *timer, rt_uint32_t state) { CTIMER_Type *hwtimer_dev; ctimer_config_t cfg; hwtimer_dev = (CTIMER_Type *)timer->parent.user_data; RT_ASSERT(timer != RT_NULL); /* Use Main clock for some of the Ctimers */ if(hwtimer_dev == CTIMER0) CLOCK_AttachClk(kFRO_HF_to_CTIMER0); if(hwtimer_dev == CTIMER1) CLOCK_AttachClk(kFRO_HF_to_CTIMER1); if(hwtimer_dev == CTIMER2) CLOCK_AttachClk(kFRO_HF_to_CTIMER2); CTIMER_Init(hwtimer_dev, &cfg); if (state == 1) { NVIC_Configuration(); CTIMER_GetDefaultConfig(&cfg); CTIMER_Init(hwtimer_dev, &cfg); } } static rt_err_t mcxa_ctimer_start(rt_hwtimer_t *timer, rt_uint32_t cnt, rt_hwtimer_mode_t mode) { CTIMER_Type *hwtimer_dev; hwtimer_dev = (CTIMER_Type *)timer->parent.user_data; /* Match Configuration for Channel 0 */ ctimer_match_config_t matchCfg; RT_ASSERT(timer != RT_NULL); /* Configuration*/ matchCfg.enableCounterReset = true; matchCfg.enableCounterStop = (mode == HWTIMER_MODE_ONESHOT) ? true : false;; matchCfg.matchValue = cnt; matchCfg.outControl = kCTIMER_Output_NoAction; matchCfg.outPinInitState = false; matchCfg.enableInterrupt = true; CTIMER_SetupMatch(hwtimer_dev, kCTIMER_Match_1, &matchCfg); NVIC_Configuration(); CTIMER_StartTimer(hwtimer_dev); return RT_EOK; } static void mcxa_ctimer_stop(rt_hwtimer_t *timer) { CTIMER_Type *hwtimer_dev; hwtimer_dev = (CTIMER_Type *)timer->parent.user_data; RT_ASSERT(timer != RT_NULL); CTIMER_StopTimer(hwtimer_dev); } static const struct rt_hwtimer_ops mcxa_hwtimer_ops = { .init = mcxa_ctimer_init, .start = mcxa_ctimer_start, .stop = mcxa_ctimer_stop, .count_get = mcxa_ctimer_count_get, .control = mcxa_ctimer_control, }; static const struct rt_hwtimer_info mcxa_hwtimer_info = { 96000000, /* the maximum count frequency can be set */ 6103, /* the minimum count frequency can be set */ 0xFFFFFFFF, HWTIMER_CNTMODE_UP, }; int rt_hw_hwtimer_init(void) { int i = 0; int result = RT_EOK; for (i = 0; i < sizeof(mcxa_hwtimer_obj) / sizeof(mcxa_hwtimer_obj[0]); i++) { mcxa_hwtimer_obj[i].time_device.info = &mcxa_hwtimer_info; mcxa_hwtimer_obj[i].time_device.ops = &mcxa_hwtimer_ops; if (rt_device_hwtimer_register(&mcxa_hwtimer_obj[i].time_device, mcxa_hwtimer_obj[i].name, mcxa_hwtimer_obj[i].tim_handle) == RT_EOK) { LOG_D("%s register success", mcxa_hwtimer_obj[i].name); } else { LOG_E("%s register failed", mcxa_hwtimer_obj[i].name); result = -RT_ERROR; } } return result; } INIT_DEVICE_EXPORT(rt_hw_hwtimer_init); #ifdef BSP_USING_CTIMER0 void CTIMER0_IRQHandler(void) { rt_interrupt_enter(); uint32_t int_stat; /* Get Interrupt status flags */ int_stat = CTIMER_GetStatusFlags(CTIMER0); /* Clear the status flags that were set */ CTIMER_ClearStatusFlags(CTIMER0, int_stat); rt_device_hwtimer_isr(&mcxa_hwtimer_obj[TIM0_INDEX].time_device); rt_interrupt_leave(); } #endif /* BSP_USING_HWTIMER0 */ #ifdef BSP_USING_CTIMER1 void CTIMER1_IRQHandler(void) { rt_interrupt_enter(); uint32_t int_stat; /* Get Interrupt status flags */ int_stat = CTIMER_GetStatusFlags(CTIMER1); /* Clear the status flags that were set */ CTIMER_ClearStatusFlags(CTIMER1, int_stat); rt_device_hwtimer_isr(&mcxa_hwtimer_obj[TIM1_INDEX].time_device); rt_interrupt_leave(); } #endif /* BSP_USING_HWTIMER1 */ #ifdef BSP_USING_CTIMER2 void CTIMER2_IRQHandler(void) { rt_interrupt_enter(); uint32_t int_stat; /* Get Interrupt status flags */ int_stat = CTIMER_GetStatusFlags(CTIMER2); /* Clear the status flags that were set */ CTIMER_ClearStatusFlags(CTIMER2, int_stat); rt_device_hwtimer_isr(&mcxa_hwtimer_obj[TIM2_INDEX].time_device); rt_interrupt_leave(); } #endif /* BSP_USING_HWTIMER2 */ #endif /* BSP_USING_HWTIMER */ ``` ### SConscript 在`Libraries/MCXA153/SConscript`文件里边加上以下代码 ``` src += ['MCXA153/drivers/fsl_ctimer.c'] ``` 在`Libraries/drivers/SConscript`文件里边加上以下代码 ``` if GetDepend('BSP_USING_HWTIMER'): src += ['drv_hwtimer.c'] ``` ## 测试用例 打开使能CTIMER驱动,保存配置退出menuconfig ![CTIMER配置.png](https://oss-club.rt-thread.org/uploads/20241217/0d6eb944f39eacdf716d6516240c68b2.png.webp) 拉取更新软件包,并导出MDK5工程,最后编译修改工程 ``` pkgs --update scons --target=mdk5 ``` 硬件定时器测试例程 ```c #include
#include
#define HWTIMER_DEV_NAME "timer2" 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; } static int hwtimer_sample(int argc, char *argv[]) { 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 = 96000000; 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); rt_device_control(hw_dev, HWTIMER_CTRL_FREQ_SET, &freq); 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 :%d\n", ret); return ret; } 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 failed\n"); return RT_ERROR; } rt_thread_mdelay(3500); rt_device_read(hw_dev, 0, &timeout_s, sizeof(timeout_s)); rt_kprintf("Read: Sec = %d, Usec = %d\n", timeout_s.sec, timeout_s.usec); return ret; } MSH_CMD_EXPORT(hwtimer_sample, hwtimer sample); #define LED_PIN ((3*32)+12) int main(void) { #if defined(__CC_ARM) rt_kprintf("using armcc, version: %d\n", __ARMCC_VERSION); #elif defined(__clang__) rt_kprintf("using armclang, version: %d\n", __ARMCC_VERSION); #elif defined(__ICCARM__) rt_kprintf("using iccarm, version: %d\n", __VER__); #elif defined(__GNUC__) rt_kprintf("using gcc, version: %d.%d\n", __GNUC__, __GNUC_MINOR__); #endif rt_pin_mode(LED_PIN, PIN_MODE_OUTPUT); /* Set GPIO as Output */ rt_kprintf("MCXA153 HelloWorld\n"); while (1) { rt_pin_write(LED_PIN, PIN_HIGH); /* Set GPIO output 1 */ rt_thread_mdelay(500); /* Delay 500mS */ rt_pin_write(LED_PIN, PIN_LOW); /* Set GPIO output 0 */ rt_thread_mdelay(500); /* Delay 500mS */ } } ``` 烧录到开发板上去,实验现象:隔5秒钟打印当前系统的时间节拍 ![定时器实验现象.png](https://oss-club.rt-thread.org/uploads/20241217/887417fcc3a82a992e41b4744e5082f9.png.webp) ### 总结 - 硬件定时器比软件定时器更准确,适合对时间敏感的编程场合 - 定时器中断起始和结束地方需要加上进出中断的函数:rt_interrupt_enter()和rt_interrupt_leave()
0
条评论
默认排序
按发布时间排序
登录
注册新账号
关于作者
hywing
嵌入式系统开发工程师,从事物联网、工业自动化、汽车电子开发工作
文章
9
回答
3
被采纳
0
关注TA
发私信
相关文章
1
试贴-消灭0主题
2
LPC M4的一些资料
3
LPC4088的临时分支
4
lpc1788 ad 不稳定
5
1788 LCD控制器缓冲区字节问题
6
一起来学习LPC4088吧
7
上传LPC4088的realtouch主工程
8
RealBoard 4088预定帖 [第一批板子不多,预定提前结束]
9
晒RealBoard LPC4088开箱照啦,速带小板凳前来围观
10
4088主程序需要的SD卡资源
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
DMA
USB
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
I2C_IIC
ESP8266
UART
WIZnet_W5500
ota在线升级
cubemx
PWM
flash
freemodbus
BSP
packages_软件包
潘多拉开发板_Pandora
定时器
ADC
flashDB
GD32
socket
编译报错
中断
Debug
rt_mq_消息队列_msg_queue
keil_MDK
SFUD
msh
ulog
C++_cpp
MicroPython
本月问答贡献
出出啊
1518
个答案
343
次被采纳
小小李sunny
1444
个答案
290
次被采纳
张世争
813
个答案
177
次被采纳
crystal266
549
个答案
161
次被采纳
whj467467222
1222
个答案
149
次被采纳
本月文章贡献
出出啊
1
篇文章
3
次点赞
小小李sunny
1
篇文章
1
次点赞
张世争
1
篇文章
3
次点赞
crystal266
2
篇文章
2
次点赞
whj467467222
2
篇文章
2
次点赞
回到
顶部
发布
问题
投诉
建议
回到
底部