本文介绍的 API 可以参考 SDK 中 zyf_timer.h 文件。
1.1 ZYF_CreateTimer 此函数用来创建一个定时器,每个任务最多支持 10 个定时器。· 函数原型 ZYF_Timer_t *ZYF_CreateTimer(ZYF_TimeOutCallback callback, void* param);· 参数 callback:定时器的回调函数 param:用于传递用户参数。· 返回值 创建的计时器实例。内存不足时为 NULL,或参数无效
1.3.2 ZYF_DeleteTimer 此函数用来删除指定的定时器· 函数原型 int32_t ZYF_DeleteTimer(ZYF_Timer_t *timerId);· 参数 timerId:需要被删除的定时器 ID· 返回值 ZYF_RET_OK 表示寄存器正常; ZYF_RET_ERR_PARAM 指示所有计时器已用完。
1.3.3 ZYF_StartTimer 此函数用来开启一个指定 ID 的定时器· 函数原型
ZYF_StartTimer(ZYF_Timer_t *timerId, uint32_t interval);· 参数 timerId:定时器 ID Interval:设置定时器的间隔时间,单位 ms· 返回值 ZYF_RET_OK 成功 ZYF_RET_ERR_PARAM 指示参数错误
1.3.4 ZYF_StopTimer 此函数用来停止指定 ID 的定时器。· 函数原型 int32_t ZYF_StopTimer(ZYF_Timer_t *timerId); · 参数 timerId:定时器 ID· 返回值 ZYF_RET_OK 成功 ZYF_RET_ERR_PARAM 指示参数错误 注释 :如果您在 u opencpu 任务中注册定时器 ID ,则只能启动或停止定时器在同一任务中,否则定时器无法启动或停止。
本章节主要介绍如何在 SDK 中使用 example 单独测试定时器功能。 编译方法:.\examples\build\对应的.bat 文件双击执行或打开就可以编译。 生成文件:.\out\对应目录\hex\M601_example_**.pac
使用方法: 1、使用 ZYF_CreateTimer()创建一个定时器。 2、使用 ZYF_StartTimer()开始这个定时器。
#define OSI_LOG_TAG OSI_MAKE_LOG_TAG('T', 'I', 'M', 'E') #include <stdint.h> #include <string.h> #include "zyf_trace.h" #include "zyf_time.h" #include "zyf_thread.h" #include "zyf_app.h" #include "zyf_uart.h" static Uart_Param_t g_uart1param; void ZYF_TimeTest(void) { uint64_t utcsec = 0; ZYF_Time_t toutTime; ZYF_Time_t tTime = { .year = 2020, .month = 02, .day = 28, .hour = 19, .minute = 30, .second = 0, .timezone = 0 }; ZYF_SetLocalTime(&tTime); ZYF_ThreadSleep(2000); memset(&tTime, 0, sizeof(ZYF_Time_t)); ZYF_GetLocalTime(&tTime); ZYF_LOG("time => d/d/d,d:d:d", tTime.year, tTime.month, tTime.day, tTime.hour, tTime.minute, tTime.second); // utcsec = ZYF_MakeTime(&tTime); // ZYF_LOG("total seconds => %ll",utcsec); // ZYF_Time2CalendarTime(utcsec,&toutTime); // ZYF_LOG("time => d/d/d,d:d:d", toutTime.year, toutTime.month, toutTime.day, toutTime.hour, toutTime.minute, toutTime.second); } void TimeThread_Example(void * Param) { ZYF_MsgQ_t *ptMsg; ZYF_AppMsg_t tMsg; int iRet = -1; ptMsg = ZYF_MsgQCreate(10, sizeof(ZYF_AppMsg_t)); ZYF_LOG("thread enter!"); ZYF_TimeTest(); while (1) { ZYF_LOG("in while."); iRet = ZYF_MsgQGet(ptMsg, (void *)&tMsg); if (iRet < 0) { ZYF_LOG("Failed to get msg"); ZYF_ThreadSleep(1000); } } } void UartWriteCallBack(void* Param) // general com { Uart_Param_t *uartparam = (Uart_Param_t *)Param; if(Param == NULL) { return; } ZYF_UartWrite(uartparam->port,(uint8_t *)"UartWrite succeed\r\n",strlen("UartWrite succeed\r\n")); ZYF_UartWriteCallbackSwitch(uartparam->port,false); } void UartReadCallBack(void* Param) // { uint32_t recvlen = 0; Uart_Param_t *uartparam = (Uart_Param_t *)Param; ZYF_LOG("Uart%d recv",uartparam->port); while(ZYF_UartRead(uartparam->port, &(uartparam->uartbuf[recvlen]), 1)) { ZYF_LOG("recv :x",uartparam->uartbuf[recvlen]); recvlen++; } ZYF_UartWrite(uartparam->port,uartparam->uartbuf,recvlen); ZYF_UartWriteCallbackSwitch(uartparam->port,true); } static void AppUartInit(void) { int32_t ret; g_uart1param.port = DEBUG_PORT; ZYF_UartRegister(g_uart1param.port, UartReadCallBack,&g_uart1param); ZYF_UartWriteCbRegister(g_uart1param.port,UartWriteCallBack,&g_uart1param); ZYF_UartOpen(g_uart1param.port, 115200, ZYF_FC_NONE); ZYF_LOG("AppUartInit"); return; } static void prvInvokeGlobalCtors(void) { extern void (*__init_array_start[])(); extern void (*__init_array_end[])(); size_t count = __init_array_end - __init_array_start; for (size_t i = 0; i < count; ++i) __init_array_start[i](); } int appimg_enter(void *param) { AppUartInit(); ZYF_LOG("application image enter, param 0x%x", param); prvInvokeGlobalCtors(); ZYF_ThreadCreate("UartThread_Example", TimeThread_Example, NULL, ZYF_PRIORITY_HIGH, 10*1024); return 0; } void appimg_exit(void) { OSI_LOGI(0, "application image exit"); }