sdk/UART/SimplUART/APP/userTimer.c

213 lines
5.9 KiB
C
Raw Normal View History

2022-11-19 17:16:40 +00:00
//
// Created by KS00596 on 2022/7/13.
//
#include "userTimer.h"
#include "pt.h"
#define TASK_MAX 20
// protoTheads ϵͳʱ<CDB3><CAB1>
volatile u32 ptSysTick = 0;
/**
* @brief <EFBFBD><EFBFBD>ȡprotoTheads ϵͳʱ<EFBFBD><EFBFBD>
*
* @return u32 ϵͳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˶<EFBFBD><EFBFBD>ٸ<EFBFBD>tick
*/
u32 PT_GetSystemTime()
{
return ptSysTick;
}
typedef struct
{
USER_TIMER_HDL task[TASK_MAX];
} USER_TIME_TASK;
static USER_TIME_TASK tisTask;
/**
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*
* @param hdl <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param priv <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
* @param time ʱ<EFBFBD><EFBFBD>
* @return u8 ID<EFBFBD><EFBFBD>
*/
u8 UserTimerAdd(void *hdl, void *priv, u16 time)
{
u8 i = 0;
for (; i < TASK_MAX; i++)
{
if (tisTask.task[i].use == 0)
{
break;
}
}
if (i == TASK_MAX)
return TASK_CREAT_ERR;
/**********************************************************
<EFBFBD><EFBFBD>ע<EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>normal
**********************************************************/
tisTask.task[i].hdl = hdl;
tisTask.task[i].tick = 0;
tisTask.task[i].time = time;
tisTask.task[i].use = 1;
tisTask.task[i].priv = priv;
tisTask.task[i].timeOut = 0;
/**********************************************************
<EFBFBD><EFBFBD>ע<EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>prototheads
**********************************************************/
tisTask.task[i].ptTickYead=0;
tisTask.task[i].ptWake=0;
PT_INIT(&tisTask.task[i].pt);
return i;
}
/**
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ö<EFBFBD>ʱ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>
*
* @param id <EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>ID<EFBFBD><EFBFBD>
* @param time <EFBFBD>µ<EFBFBD>ʱ<EFBFBD><EFBFBD>
*/
void UserTimerResetTime(u8 id, u16 time)
{
tisTask.task[id].tick = 0;
tisTask.task[id].time = time;
}
/**
* @brief ɾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>
*
* @param id <EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>ID<EFBFBD><EFBFBD>
*/
void UserTimerDel(u8 id)
{
tisTask.task[id].use = 0;
}
/**
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>
*
* @param hdl <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param priv <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
* @param time ʱ<EFBFBD><EFBFBD>
* @return u8 <EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>ID<EFBFBD><EFBFBD>
*/
u8 UserTimerOutAdd(void *hdl, void *priv, u16 time)
{
u8 i = 0;
for (; i < TASK_MAX; i++)
{
if (tisTask.task[i].use == 0)
{
break;
}
}
if (i == TASK_MAX)
return TASK_CREAT_ERR;
/**********************************************************
<EFBFBD><EFBFBD>ע<EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>normal
**********************************************************/
tisTask.task[i].hdl = hdl;
tisTask.task[i].tick = 0;
tisTask.task[i].time = time;
tisTask.task[i].use = 1;
tisTask.task[i].priv = priv;
tisTask.task[i].timeOut = 1;
/**********************************************************
<EFBFBD><EFBFBD>ע<EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>protothreads
**********************************************************/
tisTask.task[i].ptTickYead=0;
tisTask.task[i].ptWake=0;
PT_INIT(&tisTask.task[i].pt);
return i;
}
void UserTimerOutDel(u8 id)
{
tisTask.task[id].use = 0;
}
void UserTimerTask()
{
for (u8 i = 0; i < TASK_MAX; i++)
{
/**********************************************************
<EFBFBD><EFBFBD>ע<EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>
**********************************************************/
if (tisTask.task[i].use == 1)
{
/**********************************************************
<EFBFBD><EFBFBD>ע<EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>prototheads<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
**********************************************************/
if (tisTask.task[i].ptTickYead)
{
tisTask.task[i].ptTickYead--;
if (tisTask.task[i].ptTickYead == 0)
{
/**********************************************************
<EFBFBD><EFBFBD>ע<EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>protothread<EFBFBD>ӳٵ<EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>ˣ<EFBFBD>ִ<EFBFBD><EFBFBD><EFBFBD>º<EFBFBD><EFBFBD><EFBFBD>
**********************************************************/
tisTask.task[i].hdl(&tisTask.task[i],tisTask.task[i].priv);
/**********************************************************
<EFBFBD><EFBFBD>ע<EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>߾<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѹ<EFBFBD><EFBFBD>ˣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD>ִ<EFBFBD><EFBFBD>
**********************************************************/
tisTask.task[i].ptWake = 1;
}
}
else
{
tisTask.task[i].ptWake = 0;
}
/**********************************************************
<EFBFBD><EFBFBD>ע<EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD>Ǿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>ʱ
**********************************************************/
if (tisTask.task[i].time <= tisTask.task[i].tick)
{
/**********************************************************
<EFBFBD><EFBFBD>ע<EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>־
**********************************************************/
tisTask.task[i].tick = 0;
/**********************************************************
<EFBFBD><EFBFBD>ע<EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>proto<EFBFBD><EFBFBD>ʱ<EFBFBD>Ѿ<EFBFBD>ִ<EFBFBD><EFBFBD><EFBFBD>ˣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͳ<EFBFBD>ִ<EFBFBD><EFBFBD><EFBFBD>ˣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¿<EFBFBD>ʼ<EFBFBD><EFBFBD>ʱ
**********************************************************/
if (tisTask.task[i].ptWake)
{
tisTask.task[i].tick++;
continue;
}
/**********************************************************
<EFBFBD><EFBFBD>ע<EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>ִ<EFBFBD>к<EFBFBD><EFBFBD><EFBFBD>
**********************************************************/
tisTask.task[i].hdl(&tisTask.task[i],tisTask.task[i].priv);
/**********************************************************
<EFBFBD><EFBFBD>ע<EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>dz<EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
**********************************************************/
if (tisTask.task[i].timeOut == 1)
{
tisTask.task[i].use = 0;
}
}
/**********************************************************
<EFBFBD><EFBFBD>ע<EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
**********************************************************/
tisTask.task[i].tick++;
} // if (tisTask.task[i].use == 1)
}
}