/* * task.c * * Created on: 2023-4-20 * Author: xukaiming */ #include "task.h" #include "task_hal.h" #include "CH392_PARAM.H" #include "main.h" typedef struct mytask { uint8_t cActive; // >0:活动,1:初始化后立即启动 uint8_t cTaskMode; // 任务工作方式 uint8_t cType; // 任务类型 uint8_t reserve1; int32_t lLastTime; int32_t lPeriod; // 活动周期,uint=1mS void (*TaskProc)(struct pt *pt1); // 任务处理子程序 struct pt pt1; }TASK_DATA,*PTASK_DATA; // define timer data struct typedef struct mytimer { uint8_t cUse; int8_t *TimerID; uint8_t cMode; // timer work mode uint8_t reserve1; int32_t lPeriod; // timer period, unit=10ms. int32_t lLastTime; // timer count for reload void (*routine)(void * Param1); // user's timer routine void *Param1; }TIMER_DATA,*PTIMER_DATA; // function prototype static void TaskTimer(struct pt *pt1); TASK_DATA TaskDb[]={ {1, TASK_MODE_PERIOD, TASK_TIMER, 0, 10L, 10L, TaskTimer, 0} , {1, TASK_MODE_PERIOD, TASK_BLINK, 0, 100L, 100L, TaskBlinLed, 0}, #ifdef DUALNET {1, TASK_MODE_PERIOD, TASK_SENDDATA, 0, 100L, 100L, TaskSendData, 0}, #endif {1, TASK_MODE_PERIOD, TASK_WDT, 0, 500L, 500L, TaskWDT, 0}, }; TIMER_DATA TimerData[MAX_TIMER]; static uint32_t TMP1msCount = 0; //1ms到标志,控制多任务处理的时间隔为1ms uint32_t gRstReason = 0; static void idle(void) { NetRouting(); } void TaskProc(void) { uint32_t i = 0, j = 0; while(1) { if(!TMP1msCount) //保证1ms扫描一次 { idle(); } else { for(i=0;icUse) continue; if((--(pt->lLastTime))<=0) { switch(pt->cMode) { case TIMER_MODE_TIMER: // timer only pt->cUse=0; break; case TIMER_MODE_CYCROUTINE: // cyclely routine pt->lLastTime+=pt->lPeriod; pt->routine(pt->Param1); // start user's routine break; case TIMER_MODE_ONCEROUTINE: // timer routine once pt->cUse=0; //防止在routine里stoptimer后启动timer造成错误停止 pt->routine(pt->Param1); // start user routine break; default: pt->cUse=0; break; } } } } //Counter void Task_SysTick_Handler(void) { TMP1msCount++; } void SerialInit(void); //Start State Machine void InitTask(void) { #if(EnEventRecorder) Init_EventRecorder(); #else SerialInit(); #endif SetSYSTickCallBack(Task_SysTick_Handler); InitSysTick(1000); InitLEDPort(); gRstReason = checkRstReason(); } #if(EnEventRecorder) #include "EventRecorder.h" void Init_EventRecorder(void) { EventRecorderInitialize(EventRecordAll,1U); EventRecorderStart(); } #endif #if(!EnEventRecorder) void SerialInit(void) { UART_InitStructure UART_initStruct; PORT_Init(PORTC, PIN2, PORTC_PIN2_UART3_RX, 1); // PORT_Init(PORTC, PIN3, PORTC_PIN3_UART3_TX, 0); // UART_initStruct.Baudrate = 115200*2; UART_initStruct.DataBits = UART_DATA_8BIT; UART_initStruct.Parity = UART_PARITY_NONE; UART_initStruct.StopBits = UART_STOP_1BIT; UART_initStruct.RXThreshold = 3; UART_initStruct.RXThresholdIEn = 0; UART_initStruct.TXThreshold = 3; UART_initStruct.TXThresholdIEn = 0; UART_initStruct.TimeoutTime = 10; UART_initStruct.TimeoutIEn = 0; UART_Init(UART3, &UART_initStruct); UART_Open(UART3); } /****************************************************************************************************************************************** * 函数名称: fputc() * 功能说明: printf()使用此函数完成实际的串口打印动作 * 输 入: int ch 要打印的字符 * FILE *f 文件句柄 * 输 出: 无 * 注意事项: 无 ******************************************************************************************************************************************/ int fputc(int ch, FILE *f) { UART_WriteByte(UART3, ch); while(UART_IsTXBusy(UART3)); return ch; } #endif