From b34d5dbff89fd992af8196c747e18a8933d0c729 Mon Sep 17 00:00:00 2001
From: tao_z <tzj0429@163.com>
Date: Sun, 27 Jun 2021 19:50:59 +0800
Subject: [PATCH] 编译通过,能够在线调试

---
 EventRecorderStub.scvd                     |    9 +
 USR/SRC/Motor.c                            |    9 +
 USR/SRC/RttTask.c                          |   64 ++++++++++++
 USR/SRC/os_task.c                          |   92 ++++++++++++++++++
 USR/INC/motor.h                            |    1 
 USR/INC/RttTask.h                          |    5 +
 USR/INC/SysCfg.h                           |    9 +
 USR/SRC/gd32e23x_it.c                      |    3 
 USR/INC/os_task.h                          |   30 ++++++
 USR/SRC/main.c                             |   21 ++++
 CMSIS/GD/GD32E23x/Source/system_gd32e23x.c |    4 
 11 files changed, 244 insertions(+), 3 deletions(-)

diff --git a/CMSIS/GD/GD32E23x/Source/system_gd32e23x.c b/CMSIS/GD/GD32E23x/Source/system_gd32e23x.c
index a3296ea..291b410 100644
--- a/CMSIS/GD/GD32E23x/Source/system_gd32e23x.c
+++ b/CMSIS/GD/GD32E23x/Source/system_gd32e23x.c
@@ -45,8 +45,8 @@
 /* select a system clock by uncommenting the following line */
 //#define __SYSTEM_CLOCK_8M_HXTAL              (__HXTAL)
 //#define __SYSTEM_CLOCK_8M_IRC8M              (__IRC8M)
-#define __SYSTEM_CLOCK_72M_PLL_HXTAL         (uint32_t)(72000000)
-//#define __SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2    (uint32_t)(72000000)
+//#define __SYSTEM_CLOCK_72M_PLL_HXTAL         (uint32_t)(72000000)
+#define __SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2    (uint32_t)(72000000)
 
 #define SEL_IRC8M       0x00
 #define SEL_HXTAL       0x01
diff --git a/EventRecorderStub.scvd b/EventRecorderStub.scvd
new file mode 100644
index 0000000..2956b29
--- /dev/null
+++ b/EventRecorderStub.scvd
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<component_viewer schemaVersion="0.1" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:noNamespaceSchemaLocation="Component_Viewer.xsd">
+
+<component name="EventRecorderStub" version="1.0.0"/>       <!--name and version of the component-->
+  <events>
+  </events>
+
+</component_viewer>
diff --git a/USR/INC/RttTask.h b/USR/INC/RttTask.h
new file mode 100644
index 0000000..0b186cd
--- /dev/null
+++ b/USR/INC/RttTask.h
@@ -0,0 +1,5 @@
+#ifndef RTTTTASK
+#define RTTTTASK
+extern void RTT_TaskInit(void);
+extern void RTT_Task10ms(void *p);
+#endif
\ No newline at end of file
diff --git a/USR/INC/SysCfg.h b/USR/INC/SysCfg.h
new file mode 100644
index 0000000..2894824
--- /dev/null
+++ b/USR/INC/SysCfg.h
@@ -0,0 +1,9 @@
+#ifndef SYS_CFG_H
+#define SYS_CFG_H
+#define HARDWARE_VERSION ("H03")
+#define SOFTWARE_VERSION ("S01")
+#define SOFTWARE_PN ("123456789")
+#define HARDWAR_PN ("987654321")
+#define COMPILE_DATE (__DATE__)
+#define COMPILE_TIME (__TIME__)
+#endif
diff --git a/USR/INC/motor.h b/USR/INC/motor.h
index bde2ff5..befaf8b 100644
--- a/USR/INC/motor.h
+++ b/USR/INC/motor.h
@@ -37,4 +37,5 @@
 void hall_enable(void);
 void hall_disable(void);
 void set_pwm_pulse(uint16_t pulse);
+extern void Motor_Init(void);
 #endif
diff --git a/USR/INC/os_task.h b/USR/INC/os_task.h
new file mode 100644
index 0000000..80fdff7
--- /dev/null
+++ b/USR/INC/os_task.h
@@ -0,0 +1,30 @@
+#ifndef OS_TASK_H
+#define OS_TASK_H
+#include <stdio.h>
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+	typedef void (*TASK_FUNC)(void *);
+	typedef void (*GETTICK_t)(void);
+
+	typedef struct _OS_TASK_MSG
+	{
+		TASK_FUNC func; //����
+		int cycle_tick; //ʱ������
+		int work_tick;
+		int cur_tick; //��ǰʱ�Ӽ���
+		void *par;	  //����
+	} OS_TASK_MSG;
+
+	int InitOSTaskQueue(OS_TASK_MSG *p_task, int cnt);
+	int OSTaskCreate(TASK_FUNC func, int tick, int work_tick, void *par);
+	void OSSchedule(GETTICK_t GetTick);
+	void OS_GetTick(void);
+	extern volatile int OS_TickFlag;
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/USR/SRC/Motor.c b/USR/SRC/Motor.c
index 416681b..4bb7783 100644
--- a/USR/SRC/Motor.c
+++ b/USR/SRC/Motor.c
@@ -4,10 +4,19 @@
 #include "gd32e23x_timer.h"
 #include "bldc_ctrl.h"
 #include "pwm.h"
+#include "string.h"
 static volatile motor_rotate_t motor_drive = {0};
 static uint32_t motor_pluse = 0;
 static void motor_phasechange(void);
 static void update_speed_dir(uint8_t dir_in);
+
+extern void Motor_Init(void)
+{
+    motor_drive.timeout = 0;
+    motor_drive.speed = 0;
+    motor_drive.enable_flag = 0;
+    memset(motor_drive.speed_group, 0, SPEED_FILTER_NUM);
+};
 /**
   * @brief  ʹ�ܻ���������
   * @param  ��
diff --git a/USR/SRC/RttTask.c b/USR/SRC/RttTask.c
new file mode 100644
index 0000000..a4ed584
--- /dev/null
+++ b/USR/SRC/RttTask.c
@@ -0,0 +1,64 @@
+#include "SEGGER_RTT_Conf.h"
+#include "SEGGER_RTT.h"
+#include "SysCfg.h"
+#include "gd32e23x.h"
+static uint32_t RttDataOutPutFlag = 0;
+static void RTT_KeyValue(uint32_t key);
+extern void RTT_TaskInit(void)
+{
+    // /* ����ͨ��0���������ã�STM32->RTT Viewer����� */
+    // SEGGER_RTT_ConfigUpBuffer(0, "RTTUP", NULL, 0, SEGGER_RTT_MODE_NO_BLOCK_SKIP);
+
+    // /* ����ͨ��0���������ã�RTT Viewer���->STM32�� */
+    // SEGGER_RTT_ConfigDownBuffer(0, "RTTDOWN", NULL, 0, SEGGER_RTT_MODE_NO_BLOCK_SKIP);
+    SEGGER_RTT_Init();
+    RttDataOutPutFlag = 0;
+}
+
+extern void RTT_Task10ms(void *p)
+{
+    static uint32_t output_index = 0;
+    uint32_t GetKey = 0;
+    if (SEGGER_RTT_HasKey()) /* �жϽ��ջ��������Ƿ������� */
+    {
+        GetKey = SEGGER_RTT_GetKey(); /* �ӽ��ܻ�������ȡ��һ���ַ� */
+        RTT_KeyValue(GetKey);
+        // SEGGER_RTT_SetTerminal(1);
+    }
+    if (0 != RttDataOutPutFlag)
+    {
+        SEGGER_RTT_printf(0, "%d\n", output_index++);
+    }
+}
+
+static void RTT_KeyValue(uint32_t key)
+{
+    switch (key)
+    {
+    case 0:
+        /* code */
+        break;
+    case 'd':
+    case 'D':
+        RttDataOutPutFlag = !RttDataOutPutFlag;
+        SEGGER_RTT_printf(0, "Index:\tTemp\tBase\tCP\tleakCurr\tCurr\tAC_Vot\tState\tError\n");
+        break;
+    case 'v':
+    case 'V':
+        SEGGER_RTT_printf(0, "Software Version is :%s\n\r", &SOFTWARE_VERSION);
+        SEGGER_RTT_printf(0, "Hardware Version is :%s\n\r", &HARDWARE_VERSION);
+        break;
+    case 'p':
+    case 'P':
+        SEGGER_RTT_printf(0, "Software PartNum is :%s\n\r", &SOFTWARE_PN);
+        SEGGER_RTT_printf(0, "Hardware PartNum is :%s\n\r", &HARDWAR_PN);
+        break;
+    case 't':
+    case 'T':
+        SEGGER_RTT_printf(0, "Software Compile time is :%s %s\n\r", &COMPILE_DATE, &COMPILE_TIME);
+        break;
+
+    default:
+        break;
+    }
+}
\ No newline at end of file
diff --git a/USR/SRC/gd32e23x_it.c b/USR/SRC/gd32e23x_it.c
index 3d538c2..2db822c 100644
--- a/USR/SRC/gd32e23x_it.c
+++ b/USR/SRC/gd32e23x_it.c
@@ -36,6 +36,7 @@
 
 #include "gd32e23x_it.h"
 #include "motor.h"
+#include "os_task.h"
 
 /*!
     \brief      this function handles NMI exception
@@ -141,7 +142,7 @@
 */
 void SysTick_Handler(void)
 {
-    delay_decrement();
+    OS_TickFlag = 1;
 }
 /*!
     \brief      this function handles EXTI4-15 exception
diff --git a/USR/SRC/main.c b/USR/SRC/main.c
index 7730d79..a70f892 100644
--- a/USR/SRC/main.c
+++ b/USR/SRC/main.c
@@ -5,16 +5,37 @@
 #include "pwm.h"
 #include "uart.h"
 #include "clock.h"
+#include "motor.h"
+#include "bldc_ctrl.h"
+#include "bsp_pid.h"
+#include "os_task.h"
+#include "RttTask.h"
+
+static void Comm_Task(void *p)
+{
+}
 
 int main()
 {
+
+	OS_TASK_MSG task_queue[8] = {0};
+	InitOSTaskQueue(task_queue, 8);
+
 	Clock_Config();
 	GPIO_Init();
 	TimerInit();
 
 	UartInit();
 	ADC_Init();
+	Motor_Init();
+	bldcm_init();
+	PID_param_init();
+	RTT_TaskInit();
 	Ostick_config();
+
+	OSTaskCreate(&Comm_Task, 10, 7, NULL);
+	OSTaskCreate(&RTT_Task10ms, 1000, 7, NULL);
+	OSSchedule(&OS_GetTick);
 	while (1)
 		;
 	return 0;
diff --git a/USR/SRC/os_task.c b/USR/SRC/os_task.c
new file mode 100644
index 0000000..139b6e3
--- /dev/null
+++ b/USR/SRC/os_task.c
@@ -0,0 +1,92 @@
+#include "os_task.h"
+// extern int OS_TickFlag = 0;
+static OS_TASK_MSG *g_p_task = NULL; //�������
+static int g_task_cnt = 0;			 //����������
+static int g_task_pos = 0;			 //��ǰ�������
+volatile int OS_TickFlag = 0;
+/*!
+* @brief ��ʼ���������
+* @param [in] p_task �������������
+* @param [in] cnt  ������ౣ��cnt������
+* @return int ���س�ʼ���Ƿ�ɹ�
+* @retval 0 �ɹ�
+* @note null
+*/
+int InitOSTaskQueue(OS_TASK_MSG *p_task, int cnt)
+{
+	int i = 0;
+	g_p_task = p_task;
+	g_task_cnt = cnt;
+	for (i = 0; i < g_task_cnt; i++)
+	{
+		g_p_task[i].func = NULL;
+	}
+
+	return 0;
+}
+
+/*!
+* @brief ����һ������
+* @param [in] func ִ������ĺ���ָ��
+* @param [in] tick ִ�е�ǰ������Ҫ���ٸ�����
+* @param [in] work_tick ��һ�������ھ����ĸ�ʱ��Ƭȥִ��
+* @param [in] par ����������Ҫ�IJ���
+* @return int ���س�ʼ���Ƿ�ɹ�
+* @retval 0 �ɹ�
+* @retval -1 ʧ��
+* @note null
+*/
+int OSTaskCreate(TASK_FUNC func, int tick, int work_tick, void *par)
+{
+	if (g_task_pos < g_task_cnt)
+	{
+		g_p_task[g_task_pos].func = func;
+		g_p_task[g_task_pos].cycle_tick = tick;
+		g_p_task[g_task_pos].work_tick = work_tick;
+		g_p_task[g_task_pos].par = par;
+		g_task_pos++;
+		return 0;
+	}
+	else
+	{
+		return -1;
+	}
+}
+
+/*!
+* @brief ������������
+* @param [in] tick ִ�е�ǰ������Ҫ���ٸ�����
+* @note null
+*/
+void OSSchedule(GETTICK_t GetTick)
+{
+	int i = 0;
+
+	if (NULL == GetTick)
+	{
+		return;
+	}
+	while (1)
+	{
+		GetTick();
+		for (i = 0; i < g_task_pos; i++)
+		{
+			g_p_task[i].cur_tick++; //��ǰ����CPU��Ƭ
+			if ((g_p_task[i].cur_tick == g_p_task[i].work_tick) && (g_p_task[i].func != NULL))
+			{
+				g_p_task[i].func(g_p_task[i].par);
+			}
+			if (g_p_task[i].cur_tick >= g_p_task[i].cycle_tick)
+			{
+				g_p_task[i].cur_tick = 0;
+			}
+		}
+	}
+}
+
+void OS_GetTick(void)
+{
+	while (1 != OS_TickFlag)
+		;
+	OS_TickFlag = 0;
+}

--
Gitblit v1.8.0