当前位置:网站首页>络达开发---自定义Timer的实现
络达开发---自定义Timer的实现
2022-08-11 01:15:00 【MicMind】
平台:AB1565M
SDK版本:V2.11.0
开发环境:windows10
络达SDK是在FreeRTOS的基础上进行构建的,因此我们可以使用该RTOS的机制来建设Timer任务。
需要在C文件中添加头文件:
#include "timers.h"
Timer的句柄为:TimerHandle_t,用来定义一个Timer对像;
相关接口函数有:
TimerHandle_t xTimerCreate( const char * const pcTimerName, const TickType_t xTimerPeriodInTicks, const UBaseType_t uxAutoReload, void * const pvTimerID, TimerCallbackFunction_t pxCallbackFunction );
用来建议一个Timer对象,参数xTimerPeriodInTicks在SDK中的单位为ms;uxAutoReload的参数表示超时后是否需要重新计时,否则Timer只会被触发一次;pvTimerID值可以为0,pxCallbackFunction为Timer超时后执行的回调函数,用户通常在该回调函数中处理一些周期性的任务。
#define xTimerStart( xTimer, xTicksToWait ) xTimerGenericCommand( ( xTimer ), tmrCOMMAND_START, ( xTaskGetTickCount() ), NULL, ( xTicksToWait ) )
用来启动Timer;
#define xTimerStop( xTimer, xTicksToWait ) xTimerGenericCommand( ( xTimer ), tmrCOMMAND_STOP, 0U, NULL, ( xTicksToWait ) )
该函数用来停止Timer的计数,但并不会销毁timer,因此被Stop后还可以重新start。参数xTimer为执行xTimerCreate时的返回值。xTicksToWait的值通常为0。
可以发现,Timer的Start和Stop均是调用的另一个函数:xTimerGenericCommand,
函数原型为:
BaseType_t xTimerGenericCommand( TimerHandle_t xTimer, const BaseType_t xCommandID, const TickType_t xOptionalValue, BaseType_t * const pxHigherPriorityTaskWoken, const TickType_t xTicksToWait ) PRIVILEGED_FUNCTION;
Timer创建时,会传递一个回调函数的参数,用于当Timer超时时来触发该回调函数,其原型为:
void notify_timer_callback(TimerHandle_t expiredTimer)
参数expiredTimer为执行xTimerCreate函数的返回值,因此可以发现,该回调函数可以被多个不同的Timer同时使用,在回调函数中通过该expiredTimer可以来区分是哪个Timer超时,从而做出不同的处理。
下图为笔者创建的一个周期性的Timer,执行周期为1s,回调函数如下
uint16_t lgTxVal=0;
static void notify_timer_callback(TimerHandle_t expiredTimer)
{
lgTxVal++;
LOG_MSGID_I(GHP,"[BLE_GHP]in timer callback function, tx:%d\r\n", 1, lgTxVal);
//send notify
ble_ghp_write_data(g_ghp_cntx.conn_handle, &lgTxVal, 2);
}
执行的结果如下图所示:
边栏推荐
- 分库分表ShardingSphere-JDBC笔记整理
- Distributed. Performance optimization
- 【HFSS学习记录2】腔体滤波器的设计与仿真
- Exceptions and exception handling mechanisms
- 【HFSS学习记录1】实例:宽带非对称多节定向耦合器设计
- More parameter exposure of Pico 4: Pancake + color perspective, and Pro version
- 22/8/9 Collection of Greedy Problems
- 力扣------值相等的最小索引
- Web APIs BOM - A Comprehensive Case of Operating Browsers
- 云原生-VMware虚拟机安装Kubesphere实战(一)
猜你喜欢
【21天学习挑战赛】折半插入排序
深度解析volatile关键字(保证够全面)
Kunpeng compilation and debugging and basic knowledge of native development tools
sed of the Three Musketeers of Shell Programming
导入数据包上传宝贝提示“类目不能为空”是什么原因,怎么解决?
【Video】Report Sharing | 2021 Insurance Industry Digital Insights
力扣------使用最小花费爬楼梯
[21 Days Learning Challenge] Half Insertion Sort
使用mysql语句操作数据表(table)
word 设置标题前分页
随机推荐
C# using timer
简陋的nuxt3学习笔记
Distributed. Performance optimization
dump_stack ()
MySQL进阶查询
@Autowired注入RedisCache报错空指针
Ambari迁移Spark2到其它机器(图文教程)
力扣------值相等的最小索引
【HFSS学习记录2】腔体滤波器的设计与仿真
【C语言】探索数据的存储(整形篇)
Shell Text Three Musketeers Sed
std::format格式化自定义类型
[ASM] The relationship between the role of the bytecode operation ClassWriter COMPUTE_FRAMES and visitMaxs
Sigma开发注意细节
详谈二叉搜索树
Single-chip human-computer interaction--matrix key
成功解决TypeError: can‘t multiply sequence by non-int of type ‘float‘
How to convert url to obj or obj to url
Dual machine thermal for comprehensive experiment (VRRP + OSPF + + NAT + DHCP + VTP PVSTP + single-arm routing)
loop word