当前位置:网站首页>络达开发---自定义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);
}
执行的结果如下图所示:
边栏推荐
猜你喜欢
随机推荐
ArcGIS Pro 创建tpk
EN 12467纤维水泥平板产品—CE认证
EPro-PnP: Generalized End-to-End Probabilistic Perspective-n-Points for Monocular Object Pose Est...
分库分表ShardingSphere-JDBC笔记整理
Linux安装redis数据库
22. Inventory service
networkmanager无法打开
word 设置标题前分页
云原生-FRP内网穿透(详解)使用云服务器将内网集群服务暴露至公网(二)
22/8/9 贪心问题合集
【微波工程学习记录1】功率分配器和定向耦合器
微信小程序获取当前页面的url和参数
Update chromedriver driver programming skills │ selenium
How to build speed, speed up again
BEVDepth: Acquisition of Reliable Depth for Multi-view 3D Object Detection Paper Notes
The statistical data analysis, interview manual"
[GXYCTF2019]BabySQli
Dual machine thermal for comprehensive experiment (VRRP + OSPF + + NAT + DHCP + VTP PVSTP + single-arm routing)
异常:try catch finally throws throw
单片机人机交互--矩阵按键