1 Timer related API
The API introduced in this article can refer to the zyf_timer.h file in the SDK.
1.1 ZYF_CreateTimer
This function is used to create a timer. Each task supports up to 10 timers.
· Function prototype
ZYF_Timer_t *ZYF_CreateTimer(ZYF_TimeOutCallback callback, void* param);
· Parameter
callback: the callback function of the timer
param: used to pass user parameters.
· Return value The
created timer instance. NULL when insufficient memory, or invalid parameters
1.3.2 ZYF_DeleteTimer
This function is used to delete the specified timer
· Function prototype
int32_t ZYF_DeleteTimer(ZYF_Timer_t *timerId);
· Parameter
timerId: the ID of the timer to be deleted
· The return value
ZYF_RET_OK indicates that the register is normal;
ZYF_RET_ERR_PARAM indicates that all timers have been used Finish.
1.3.3 ZYF_StartTimer
This function is used to start a timer with a specified ID
. Function prototype
ZYF_StartTimer(ZYF_Timer_t *timerId, uint32_t interval);
· Parameter
timerId: timer ID
Interval: set the interval time of the timer, in ms
· Return value
ZYF_RET_OK success
ZYF_RET_ERR_PARAM indicates parameter error
ZYF_StopTime 1.3.4 R & lt
This function is used to stop the timer specified ID.
· Function prototype
int32_t ZYF_StopTimer(ZYF_Timer_t *timerId);
· Parameter
timerId: timer ID
· Return value
ZYF_RET_OK Success
ZYF_RET_ERR_PARAM indicates parameter error
Note: If you register the timer ID in the u opencpu task, you can only start or stop the timer In the same task, otherwise the timer cannot be started or stopped.
2 Introduction to timer routines
This chapter mainly introduces how to use the example in the SDK to test the timer function separately.
Compiling method: .\examples\build\ corresponds to the .bat file by double-clicking to execute or opening it to compile.
Generated file: .\out\corresponding directory\hex\M601_example_**.pac
How to use:
1. Use ZYF_CreateTimer() to create a timer.
2. Use ZYF_StartTimer() to start this timer.
#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 => %04d/%02d/%02d,%02d:%02d:%02d", 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 => %04d/%02d/%02d,%02d:%02d:%02d", 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 :%02x",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");
}