tao_z
2021-06-27 b34d5dbff89fd992af8196c747e18a8933d0c729
编译通过,能够在线调试
6 files added
5 files modified
247 ■■■■■ changed files
CMSIS/GD/GD32E23x/Source/system_gd32e23x.c 4 ●●●● patch | view | raw | blame | history
EventRecorderStub.scvd 9 ●●●●● patch | view | raw | blame | history
USR/INC/RttTask.h 5 ●●●●● patch | view | raw | blame | history
USR/INC/SysCfg.h 9 ●●●●● patch | view | raw | blame | history
USR/INC/motor.h 1 ●●●● patch | view | raw | blame | history
USR/INC/os_task.h 30 ●●●●● patch | view | raw | blame | history
USR/SRC/Motor.c 9 ●●●●● patch | view | raw | blame | history
USR/SRC/RttTask.c 64 ●●●●● patch | view | raw | blame | history
USR/SRC/gd32e23x_it.c 3 ●●●● patch | view | raw | blame | history
USR/SRC/main.c 21 ●●●●● patch | view | raw | blame | history
USR/SRC/os_task.c 92 ●●●●● patch | view | raw | blame | history
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
EventRecorderStub.scvd
New file
@@ -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>
USR/INC/RttTask.h
New file
@@ -0,0 +1,5 @@
#ifndef RTTTTASK
#define RTTTTASK
extern void RTT_TaskInit(void);
extern void RTT_Task10ms(void *p);
#endif
USR/INC/SysCfg.h
New file
@@ -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
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
USR/INC/os_task.h
New file
@@ -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
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  ÎÞ
USR/SRC/RttTask.c
New file
@@ -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;
    }
}
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
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;
USR/SRC/os_task.c
New file
@@ -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 ´«ÈëÈÎÎñÐèÒªµÄ²ÎÊý
* @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;
}