📄 ctimer.c
字号:
#include "tc.h"
#define CSTIMER_GLOBALS
#include "ctimer.h"
#undef CSTIMER_GLOBALS
#include "ptimer.h"
#include "tcpip.h"
#include "cs.h"
#include "macro.h"
#include "router.h"
#include "eb40a.h"
#define TC2_DEFAULT_MODE (AT91C_TC_WAVESEL_UP_AUTO | \
AT91C_TC_ASWTRG_CLEAR | \
AT91C_TC_BSWTRG_CLEAR | \
TC_CLKS_MCK128 | \
AT91C_TC_CPCDIS | \
AT91C_TC_WAVE)
static uint16 gCyc2ActTicks;
static uint16 gAct2NonTicks;
static uint16 gNon2CycTicks;
void TC2IntHandler(void);
__inline uint8 CSUpdateCyc(void) {
Time curtime;
uint16 ticks;
uint32 nasecs;
uint32 timeoff;
uint32 tmp;
Getime(&curtime);
timeoff = (curtime.secs * 1000) % gMacroCyc;
tmp = curtime.nasecs / 1000000;
nasecs = curtime.nasecs - (tmp * 1000000);
timeoff = (tmp + timeoff) % gMacroCyc;
if(timeoff < CSTIMER_MAX_MILLISECONS) {
ticks = ((gMacroCyc - timeoff) * CSTIMER_TICKS_MILLISECONS) - (nasecs / CS_TICK_NANOSECONDS);
TCReload(AT91C_BASE_TC2, ticks);
}
else {
return SYS_TIMER_OVERFLOW;
}
if(timeoff < gAtcPrdOff) {
gCStatus = CS_CYCSTART;
}
else if(timeoff > gAtcPrdOff && timeoff < gNonPrdOff) {
gCStatus = CS_ACTIVETIME;
}
else {
gCStatus = CS_NONPRDTIME;
}
return SYS_NO_ERR;
}
void CSTimerInit(void) {
gCStatus = CS_UNUSABLE;
TimerInit(AT91C_BASE_TC2,
AT91C_TC_CPCTRG|TC_CLKS_MCK32,
AT91C_AIC_PRIOR_HIGHEST - 5,
AT91C_ID_TC2,
AT91C_ID_TC2,
AT91C_TC_CPCS,
TC2IntHandler);
}
void CSTimerStart(void) {
CSUpdateCyc();
gCyc2ActTicks = gAtcPrdOff * CSTIMER_TICKS_MILLISECONS;
gAct2NonTicks = (gNonPrdOff - gAtcPrdOff) * CSTIMER_TICKS_MILLISECONS;
gNon2CycTicks = (gMacroCyc - gNonPrdOff) * CSTIMER_TICKS_MILLISECONS;
// add node to cycle timer list
}
void CSTimerStop(void) {
TCStop(AT91C_BASE_TC2);
gCyc2ActTicks = 0;
gAct2NonTicks = 0;
gNon2CycTicks = 0;
// flush cycle timer list
}
void TC2Int(void) {
uint32 status;
status = AT91C_BASE_TC2->TC_SR;
if(gCStatus & CS_ACTIVETIME) {
TCReload(AT91C_BASE_TC2, gAct2NonTicks);
gCStatus = CS_NONPRDTIME;
}
else if(gCStatus == CS_CYCSTART) {
TCReload(AT91C_BASE_TC2, gCyc2ActTicks);
gCStatus = CS_ACTIVETIME;
}
else {
#if EPA_MODULE_EN > 0
OSQPost(gpMacroMsgQ, (void*)0);
#endif // EPA_MODULE_EN
#if ROUTER_MODULE_EN > 0
OSQPost(gpRIPMacroMsgQ,(void*)0);
#endif //ROUTER_MODULE_EN
CSUpdateCyc();
if(gCyc2ActTicks == 0) {
gCStatus = (CS_CYCSTART | CS_ACTIVETIME);
}
else {
gCStatus = CS_CYCSTART;
}
}
// OSQPost(gpCSendMsgQ, (void*)0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -