From 87e2fdc91a0b29b17c01e5ec64930134fdd2cc55 Mon Sep 17 00:00:00 2001
From: sunlight <2524828700@qq.com>
Date: Wed, 11 Sep 2024 16:57:13 +0800
Subject: [PATCH] finish base of Task structure
---
.settings/language.settings.xml | 4 +-
.vscode/settings.json | 5 +-
Usr/service/app_core.c | 35 +++++++------
Usr/service/app_core.h | 1 -
Usr/service/front_end_controler.c | 102 ++++++++++++++++++++++++--------------
Usr/service/front_end_controler.h | 25 ++++++----
Usr/service/page/Page_main.h | 1 +
Usr/service/page/page_processer.c | 25 ++++++----
Usr/service/page/page_processer.h | 8 +--
stm32basic/text.txt | 87 ++++++++++++++++++++++++++++++++
stm32basic/ztask.c | 55 ++++++++++++++++++++
stm32basic/ztask.h | 37 ++++++++++++++
12 files changed, 306 insertions(+), 79 deletions(-)
create mode 100644 stm32basic/text.txt
create mode 100644 stm32basic/ztask.c
create mode 100644 stm32basic/ztask.h
diff --git a/.settings/language.settings.xml b/.settings/language.settings.xml
index b61688f..a3e1934 100644
--- a/.settings/language.settings.xml
+++ b/.settings/language.settings.xml
@@ -5,7 +5,7 @@
-
+
@@ -16,7 +16,7 @@
-
+
diff --git a/.vscode/settings.json b/.vscode/settings.json
index 842c4af..9eb0f85 100644
--- a/.vscode/settings.json
+++ b/.vscode/settings.json
@@ -17,6 +17,9 @@
"page_main.h": "c",
"page_processer.h": "c",
"page_settingtime.h": "c",
- "standbymode.h": "c"
+ "standbymode.h": "c",
+ "ztask.h": "c",
+ "stdbool.h": "c",
+ "stdint.h": "c"
}
}
\ No newline at end of file
diff --git a/Usr/service/app_core.c b/Usr/service/app_core.c
index f725e25..a90409c 100644
--- a/Usr/service/app_core.c
+++ b/Usr/service/app_core.c
@@ -4,31 +4,36 @@
#define TAG "main"
-void umain(void){
- App_initialize();
-
-
- Page_initialize(Page_main);//注册一遍就够了
- Page_initialize(Page_SetingTime);
-
- while (1)
- {
- appsetup();
- }
-}
-
void App_initialize(void){
DBUG_USART1_UART_Init(460800);
ZLOGI(TAG,"======================= boardinfo ==================== ");
//zlog("DBUG_UART");
App_HardWare_initialize();//硬件初始化
- Front_initialize();//前端串口初始化
+ task_init();
+
+ // Front_initialize();//前端串口初始化
}
+void umain(void){
+ App_initialize();
+
+ task_init();
+ while (1)
+ {
+ appsetup();
+ HAL_Delay(500);
+ }
+}
+
void appsetup(void)
{
+
+
+ Front_initialize();
+ // Page_initialize(Page_main);
+ // Page_initialize(Page_SetingTime);
- Front_startSchedule();
+ Front_startSchedule();
}
diff --git a/Usr/service/app_core.h b/Usr/service/app_core.h
index cf7e477..0c314ce 100644
--- a/Usr/service/app_core.h
+++ b/Usr/service/app_core.h
@@ -7,5 +7,4 @@
void umain(void);
-void App_initialize(void);
void appsetup(void);
diff --git a/Usr/service/front_end_controler.c b/Usr/service/front_end_controler.c
index 9980981..b6e6d2f 100644
--- a/Usr/service/front_end_controler.c
+++ b/Usr/service/front_end_controler.c
@@ -3,15 +3,19 @@
#define TAG "FronEndControler"
-uint8_t IsBelongPage;
+Task_t usartRxtask;
+Task_t eventProcesstask;
+
FunctionCB m_cb[50];
int32_t m_ncb = 0;
static bool processerstate;
static tjc_packet_t* eventpacket;
static UART_HandleTypeDef* tjcUart;//前端数据串口接收
-void Front_initialize(void)
-{
+
+
+//临时函数
+void task_init(){
static uint8_t rxbuf[128];
tjcUart = &huart4;
@@ -25,11 +29,42 @@ void Front_initialize(void)
}
+
+
+
+
#define UART_RX_OVERTIME 5//接收最大时间
-void uartRxTask(void)
-{
+/*
+* @brief: 串口数据打包
+*
+*/
+void processRxpacket(uint8_t* data, size_t len ){
+ if(!(data[len - 1] == 0xFF && data[len - 2] && data[len - 3])){//接收到帧尾数据
+ ZLOGI(TAG,"RX invalid packet");
+ processerstate = error;
+ }
+ else{
+ uint8_t packetType = data[0];
+ static tjc_packet_t packet;
+ packet.datalen = len;
+ memset(packet.data, 0, sizeof(packet.data));
+ memcpy(packet.data, data, len);
+ ZLOGI(TAG, "packet information: %s\n", packet.data);
+
+ eventpacket = &packet;
+ processerstate = sucess;
+ }
+}
+
+
+
+/*
+* @brief: 串口数据接收
+*
+*/
+void uartRxTask(void){
static uint8_t processbuf[128];
int32_t rxsize = 0;
//ZLOGI("main","test3\n");
@@ -38,7 +73,6 @@ void uartRxTask(void)
HAL_Delay(1);//延时1毫秒以后串口发送的数据没问题
if (tjcUart->USR_UartITRxOff != 0 && haspassedms(tjcUart->USR_UartITLastRxTicket) > UART_RX_OVERTIME)
{
- // memset(processbuf, 0, 128);
memcpy(processbuf, tjcUart->USR_UartITRxBuf, tjcUart->USR_UartITRxOff );
rxsize = tjcUart->USR_UartITRxOff;
tjcUart->USR_UartITRxOff = 0;
@@ -48,18 +82,21 @@ void uartRxTask(void)
if(rxsize != 0)
{
processRxpacket(processbuf, rxsize);
- // ZLOGI("main","%s\n",processbuf);
- // ZLOGI("main","test1\n");
}
}
+
+
+/*
+* @brief:前端事件处理
+*
+*/
tjc_event_t event_cache;
tjc_event_t input_file_event_cache;
bool input_file_event_cache_state;
-void eventProcessTask(void)
-{
+void eventProcessTask(void){
static tjc_packet_t packet;
packet = *(eventpacket);
@@ -72,61 +109,52 @@ void eventProcessTask(void)
event_cache.pid = packet.data[1];
event_cache.bid = packet.data[2];
event_cache.date.button_event.val = packet.data[3];
-
- ///callUsrEventCb(&event);
+ callUsrEventCb(&event_cache);
}else if (packetType == tjc_inputfield_content_change_event1){
event_cache.event_id = packet.data[0];
event_cache.pid = packet.data[1];
event_cache.bid = packet.data[2];
// const char* strbegin = (const char*)&packet.data[3];
// strbegin = zcpystr(event_cache.date.inputfiled_content.text, strbegin, sizeof(event_cache.d.inputfield_content.text));
- // callUsrEventCb(&event_cache);
+ callUsrEventCb(&event_cache);
input_file_event_cache = event_cache;//文本修改事件需要刷新页面
input_file_event_cache_state = true;
}else if (tjc_sys_event_page_id == packetType){
event_cache.event_id = packet.data[0];
event_cache.pid = packet.data[1];
event_cache.bid = packet.data[2];
+
+ callUsrEventCb(&event_cache);
}
}
}
+
+
+void Front_initialize(void){
+ Task_init(&usartRxtask);
+ //Task_init(&eventProcesstask);
+}
+
void Front_startSchedule(void)
{
- uartRxTask();
- eventProcessTask();
+ //uart_start注册数据处理
+ Task_start(uartRxTask,&usartRxtask);
+ //Task_start(eventProcessTask,&usartRxtask);
}
-void processRxpacket(uint8_t* data, size_t len )
-{
- if(!(data[len - 1] == 0xFF && data[len - 2] && data[len - 3])){//接收到帧尾数据
- ZLOGI(TAG,"RX invalid packet");
- processerstate = error;
- }
- else{
- uint8_t packetType = data[0];
- static tjc_packet_t packet;
- packet.datalen = len;
- memcpy(packet.data, data, len);
- ZLOGI(TAG, "packet information: %s\n", packet.data);
-
- eventpacket = &packet;
- processerstate = sucess;
- //memcpy(eventpacket.data, packet.data, len);
- }
-}
-void regOnUsrEventCb(FunctionCB cb, uint8_t page){
+void regOnUsrEventCb(FunctionCB cb){
m_cb[m_ncb] = cb;
m_ncb++;
- IsBelongPage = page;
}
void callUsrEventCb(tjc_event_t* event){
-
-
+ for (int32_t i = 0; i < m_ncb; i++) {
+ m_cb[i](event);
+ }
}
diff --git a/Usr/service/front_end_controler.h b/Usr/service/front_end_controler.h
index 235974e..c34d96b 100644
--- a/Usr/service/front_end_controler.h
+++ b/Usr/service/front_end_controler.h
@@ -6,6 +6,7 @@
#include "zlog.h"
#include "tjc_processer.h"
#include "page_processer.h"
+#include "ztask.h"
@@ -19,19 +20,25 @@ typedef enum{
sucess,
} process_state;
-typedef void (*FunctionCB)(tjc_event_t* ,uint8_t);
+typedef void (*FunctionCB)(tjc_event_t*);
-// typedef struct{
-
-
-// // Function callUsrEventCb
-
-// } FrontEndControler_t;
extern uint8_t IsBelongPage;
void Front_startSchedule(void);
void Front_initialize(void);
-void processRxpacket(uint8_t* data, size_t len );
-void regOnUsrEventCb(FunctionCB cb,page_t page);
+
+
+void regOnUsrEventCb(FunctionCB cb);
void callUsrEventCb(tjc_event_t* event);
+
+
+
+
+void eventProcessTask_init(void);
+void eventProcessTask_start(taskfuction cb);
+void eventProcessTask(void);
+
+
+
+void task_init();
diff --git a/Usr/service/page/Page_main.h b/Usr/service/page/Page_main.h
index 97285c6..c8e6c64 100644
--- a/Usr/service/page/Page_main.h
+++ b/Usr/service/page/Page_main.h
@@ -1,6 +1,7 @@
#pragma once
#include "stm32f4xx_hal.h"
+
void OnPageLoad_main();
void OnPageButton_main(uint8_t bid, uint8_t val);
\ No newline at end of file
diff --git a/Usr/service/page/page_processer.c b/Usr/service/page/page_processer.c
index 8730bbf..a8fa399 100644
--- a/Usr/service/page/page_processer.c
+++ b/Usr/service/page/page_processer.c
@@ -9,27 +9,30 @@ PageProcesser init[] = {
};
+
+uint8_t JudgePage(uint8_t page){
+ switch(page){
+ case pg_main: return 1;break;
+ case pg_SettingTime: return 1;break;
+ default: return 0;break;
+ }
+}
void page_processer(tjc_event_t* event){
- uint8_t page = event->pid;
- if(page == IsBelongPage)
- Page_Mrg = true;
- else
- Page_Mrg = false;
+ uint8_t page;
+ if(!JudgePage(event->pid))
+ return ;
- if(Page_Mrg)
- {
+ page = event->pid;
if(event->event_id == tjc_sys_event_page_id){
init[page].OnPageLoad();
}else if(event->event_id == tjc_button_event){
init[page].OnPageButton(event->bid, event->date.button_event.val);
}
-
- }
}
void Page_initialize(uint8_t page)
{
- regOnUsrEventCb(page_processer,page);
+ regOnUsrEventCb(page_processer);
}
@@ -38,3 +41,5 @@ void Page_initialize(uint8_t page)
+
+
diff --git a/Usr/service/page/page_processer.h b/Usr/service/page/page_processer.h
index fd904ce..af8e8e5 100644
--- a/Usr/service/page/page_processer.h
+++ b/Usr/service/page/page_processer.h
@@ -7,10 +7,10 @@
// extern PageProcesser Page_initialize;
-typedef enum{
- Page_main,
- Page_SetingTime,
-} page_t;
+// typedef enum{
+// Page_main,
+// Page_SetingTime,
+// } page_t;
typedef struct {
void (*OnPageLoad)();
diff --git a/stm32basic/text.txt b/stm32basic/text.txt
new file mode 100644
index 0000000..736fe91
--- /dev/null
+++ b/stm32basic/text.txt
@@ -0,0 +1,87 @@
+
+
+// void eventProcessTask_init(void){
+// if(eventProcesstask.TaskNotify){
+
+// eventProcesstask.TaskNotify--;
+// if(eventProcesstask.m_tasktransition){
+// eventProcesstask.m_status = working;
+// ZLOGI(TAG, "eventRXtask_init");
+// if(eventProcesstask.m_taskfunc) eventProcesstask.m_taskfunc();
+// eventProcesstask.m_status = end;
+// }
+// else{
+// eventProcesstask.m_status = idle;
+// }
+// }
+// }
+// void eventProcessTask_start(taskfuction cb){
+
+// if(eventProcesstask.m_status != idle){
+// eventProcesstask.m_tasktransition = false;//状态: 非空闲,状态转换关
+// eventProcesstask.TaskNotify++; //通知任务
+// }
+// else{
+// eventProcesstask.m_taskfunc = cb;
+// eventProcesstask.m_tasktransition = true;//状态: 空闲,状态转换开
+// eventProcesstask.TaskNotify++; //通知任务
+// }
+// ZLOGI(TAG, "eventTaskNotify: %d", eventProcesstask.TaskNotify);
+// }
+
+// void eventProcesstask_start(FunctionCB cb){
+// eventProcessfunc = cb;
+// }
+
+
+// /*
+// * @brief 回调函数注册
+// *
+// *
+// *
+// *判断任务状态
+// *任务空闲:状态转换开
+// * 状态转换开:1.回调函数登记注册
+// * 2.任务通知
+// *
+// *任务非空闲:状态转换关
+// * 状态转换关:1.不注册
+// * 2.任务通知等待任务状态空闲
+// */
+// void uartRxTask_start(taskfuction cb){
+
+
+// if(usartRxtask.m_status != idle){
+// usartRxtask.m_tasktransition = false;//状态: 非空闲,状态转换关
+// usartRxtask.TaskNotify++; //通知任务
+// }
+// else{
+// usartRxtask.m_taskfunc = cb;
+// usartRxtask.m_tasktransition = true;//状态: 空闲,状态转换开
+// usartRxtask.TaskNotify++; //通知任务
+// }
+// ZLOGI(TAG, "uartTaskNotify: %d", usartRxtask.TaskNotify);
+// }
+
+// /*
+// * @brief: uart回调任务状态转换,由任务时间表进行任务通知
+// *
+// */
+// void uartRxTask_init(void){
+// if(usartRxtask.TaskNotify){
+
+// usartRxtask.TaskNotify--;
+// if(usartRxtask.m_tasktransition){
+// usartRxtask.m_status = working;
+// ZLOGI(TAG, "uartRXtask_init");
+// if(usartRxtask.m_taskfunc) usartRxtask.m_taskfunc();
+// usartRxtask.m_status = end;
+// }
+// else{
+// usartRxtask.m_status = idle;
+// }
+// }
+// }
+
+
+先写出uart的一系列函数,在将其上升为任务函数
\ No newline at end of file
diff --git a/stm32basic/ztask.c b/stm32basic/ztask.c
new file mode 100644
index 0000000..1386bf2
--- /dev/null
+++ b/stm32basic/ztask.c
@@ -0,0 +1,55 @@
+#include "ztask.h"
+
+#define TAG "ztask"
+
+
+
+/*
+* @brief: uart回调任务状态转换,由任务时间表进行任务通知
+*
+*/
+void Task_init(Task_t* task){
+ if(task->TaskNotify){
+
+ task->TaskNotify--;
+ if(task->m_tasktransition){
+ task->m_status = working;
+ ZLOGI(TAG, "task_init");
+ if(task->m_taskfunc) task->m_taskfunc();
+ task->m_status = end;
+ }
+ else{
+ task->m_status = idle;
+ }
+ }
+}
+
+
+/*
+* @brief 回调函数注册
+*
+*
+*
+*判断任务状态
+*任务空闲:状态转换开
+* 状态转换开:1.回调函数登记注册
+* 2.任务通知
+*
+*任务非空闲:状态转换关
+* 状态转换关:1.不注册
+* 2.任务通知等待任务状态空闲
+*/
+void Task_start(taskfuction cb, Task_t* task){
+
+ if(task->m_status != idle){
+ task->m_tasktransition = false;//状态: 非空闲,状态转换关
+ task->TaskNotify++; //通知任务
+ }
+ else{
+ task->m_taskfunc = cb;
+ task->m_tasktransition = true;//状态: 空闲,状态转换开
+ task->TaskNotify++; //通知任务
+ }
+ ZLOGI(TAG, "TaskNotify: %d", task->TaskNotify);
+
+}
\ No newline at end of file
diff --git a/stm32basic/ztask.h b/stm32basic/ztask.h
new file mode 100644
index 0000000..1c4df96
--- /dev/null
+++ b/stm32basic/ztask.h
@@ -0,0 +1,37 @@
+#pragma once
+#include
+#include
+#include "zlog.h"
+//extern taskfuction m_taskfunc;
+
+typedef void (*taskfuction)();
+//typedef void (*taskfuction)(taskfuction);
+
+//状态机
+typedef enum{
+ idle,
+ working,
+ end,
+} status_t;
+
+typedef struct{
+
+ uint32_t TaskNotify;//任务通知信号量
+
+ status_t m_status;
+ bool m_tasktransition;
+
+ void (*m_taskfunc)();
+
+ void (*init)(taskfuction);
+ void (*start)(taskfuction);
+
+
+}Task_t;//任务结构体
+
+
+void Task_init(Task_t* task);
+void Task_start(taskfuction cb, Task_t* task);
+
+
+