Explorar el Código

feat(Api_rt_timer): 增加计时器抽象层,增加中断抽象层,已经测试计调度程序运行频率与中断频率相同。

CN\zhangkai71 hace 2 años
padre
commit
66cd902305

+ 27 - 0
User project/1.FrameLayer/Include/sys_task.h

@@ -0,0 +1,27 @@
+/**
+ * @file sys_task.h
+ * @author Xiao Lifan (xiaolf6@midea.com)
+ * @brief 控制系统任务调度
+ * @version 0.1
+ * @date 2023-05-24
+ *
+ * @copyright Copyright (c) 2023
+ *
+ */
+
+#ifndef _SYS_TASK_H_
+#define _SYS_TASK_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif // __cplusplus
+
+void SysTaskBg();
+void SysTask1Ms();
+void SysTask10Ms();
+
+#ifdef __cplusplus
+}
+#endif // __cplusplus
+
+#endif

+ 1 - 1
User project/1.FrameLayer/Include/tbc.h

@@ -20,7 +20,7 @@ Revising History (ECL of this file):
 *************************************************************************/
 #ifndef TBC_H
 #define TBC_H
-
+#include "typedefine.h"
 /************************************************************************
  Compiler Directives (N/A)
 *************************************************************************/

+ 1 - 1
User project/1.FrameLayer/Include/tbs.h

@@ -20,7 +20,7 @@ Revising History (ECL of this file):
 ************************************************************************/
 #ifndef TBS_H
 #define TBS_H
-
+#include "typedefine.h"
 /************************************************************************
  Compiler Directives (N/A)
 *************************************************************************/

+ 16 - 14
User project/1.FrameLayer/Source/TimeTask_Event.c

@@ -53,7 +53,9 @@ static BOOL Event_pvt_blBikeThroFlg = FALSE, Event_pvt_blBikeThroFlgZ1 = FALSE;
  ******************************/
 void  Event_1ms(void) /* parasoft-suppress METRICS-28 "本项目圈复杂度无法更改,后续避免" */
 {
-   
+    /* Timing of time slices */
+    TimingTaskTimerServer();
+    
     /* 1st FSM control */
     FSM_1st_Main();
     FSM1st_Sys_state.Event_hook();
@@ -269,18 +271,18 @@ void Event_10ms(void)
 //           Signal_detect();
           
             /* Use upper computer */
-            if(cp_stFlg.RotateDirectionSelect == ForwardRotate)
-            {
-                uart_slSpdRefRpm = ((SLONG)MC_MotorSPD_rpm_Percent*5000)/100;
-            }
-            else if(cp_stFlg.RotateDirectionSelect == BackwardRotate)
-            {
-                uart_slSpdRefRpm = -((SLONG)MC_MotorSPD_rpm_Percent*5000)/100;
-            }
-            else
-            {
-            	//do nothing
-            }
+//            if(cp_stFlg.RotateDirectionSelect == ForwardRotate)
+//            {
+//                uart_slSpdRefRpm = ((SLONG)MC_MotorSPD_rpm_Percent*5000)/100;
+//            }
+//            else if(cp_stFlg.RotateDirectionSelect == BackwardRotate)
+//            {
+//                uart_slSpdRefRpm = -((SLONG)MC_MotorSPD_rpm_Percent*5000)/100;
+//            }
+//            else
+//            {
+//            	//do nothing
+//            }
       
              if(abs((int32_t)uart_slSpdRefRpm) < 300)
              {
@@ -393,7 +395,7 @@ void  Event_200ms(void)
             alm_stBikeIn.uwThrottleReg = adc_stUpOut.uwThrottleReg;
             alm_stBikeIn.blThrottleExistFlg = FALSE;
             alm_stBikeIn.blMotorNTCExistFlg = FALSE;
-            alm_voDetec200MS(&alm_stBikeIn, &alm_stDetect200MSCoef);   
+//            alm_voDetec200MS(&alm_stBikeIn, &alm_stDetect200MSCoef);   
         }
         
         if (switch_flg.SysFault_Flag == TRUE)

+ 93 - 0
User project/1.FrameLayer/Source/app.c

@@ -0,0 +1,93 @@
+#include "api.h"
+#include "board_config.h"
+#include "tbc.h"
+#include "tbs.h"
+#include "sys_task.h"
+#include "TimeTask_Event.h"
+
+void PeripheralInit();
+void PeripheralStart();
+
+void AppInit()
+{
+    /* 初始化外设(Api层级配置)并配置调度逻辑 */
+
+    PeripheralInit();
+
+//    /* 1 电机控制环路初始化 */
+//    /* 1.1 初始化PU值系统 */
+//    /* 1.2 初始化参数系统 */
+//    /* 1.3 算法模块初始化 */
+//
+//    McPipelineInit();
+//    McModulesConfig();
+//    McPipelineModulesInit();
+//    McPipelineStaticCoef();
+//
+//    /* 2 控制组件初始化 */
+//
+//    SysCtrlInit();
+//    McCtrlInit();
+//
+//    /* 状态机初始化 */
+//
+//    SysFsmInit();
+//    CtrlFsmInit();
+//    McFsmInit();
+//
+//    /* 故障处理初始化 */
+//
+//    AlmInit();
+//
+//    /* 串口调试初始化 */
+//
+//    uart_voMonitorInit();
+
+    /* 启动外设 */
+
+    PeripheralStart();
+
+}
+
+void AppLoop()
+{
+    /* Uart Monitor Service */
+//    ReadFrame_Poll2();
+//    uart_voMainDec();
+}
+
+void PeripheralInit()
+{
+    /* Gpio配置 */
+//    iGpio_SetMode(HW_GPIO_HALLA_PIN, ApiGpio_Output);
+//    iGpio_SetMode(HW_GPIO_HALLB_PIN, ApiGpio_Output);
+//    iGpio_SetMode(HW_GPIO_HALLC_PIN, ApiGpio_Output);
+
+    /* Pwm配置 */
+    iPwm_BindDeviceInterrupt(0, ApiPwm_CountZeroInt, tbc_voUpIsr);
+    iPwm_BindDeviceInterrupt(0, ApiPwm_CountMaxInt, tbc_voDownIsr);
+    iPwm_EnableDeviceInterrupt(0, ApiPwm_CountZeroInt);
+    iPwm_EnableDeviceInterrupt(0, ApiPwm_CountMaxInt);
+
+    /* Tbs 定时器配置 */
+    iTimer_BindInterrupt(HW_TBS_TIMER, tbs_voIsr);
+    iTimer_EnableInterrupt(HW_TBS_TIMER);
+    
+    /* Event1ms 定时器配置 */
+    iTimer_BindInterrupt(HW_EVENT1MS_TIMER, Event_1ms);
+    iTimer_EnableInterrupt(HW_EVENT1MS_TIMER);
+    
+    /* 1ms 定时器配置 */
+//    clock = iTimer_GetClock(HW_SYSTICK_TIMER);
+//    iTimer_SetPeriod(HW_SYSTICK_TIMER, clock / 1000 - 1);
+//    iTimer_EnableAutoReload(HW_SYSTICK_TIMER);
+    iTimer_BindInterrupt(HW_SYSTICK_TIMER, SysTask1Ms);
+    iTimer_EnableInterrupt(HW_SYSTICK_TIMER);
+}
+
+void PeripheralStart()
+{
+//    iPwm_EnableCount(0);
+//    iTimer_Start(HW_TBS_TIMER);
+//    iTimer_Start(HW_SYSTICK_TIMER);
+}

+ 18 - 119
User project/1.FrameLayer/Source/gd32f30x_it.c

@@ -136,14 +136,7 @@ void PendSV_Handler(void)
 */
 void SysTick_Handler(void)
 {
-    cp_ulSystickCnt ++;
-    /* MCU Self Check*/
-    clas_ubSystickFlg = 1; 
-    stl_voSystickProc();
-    /* UART Timeout */
-    UART_voApplTimer();
-    /* TBT interrupt */
-    //tbt_voIsr();
+    iRtTimer_Isr(HW_SYSTICK_TIMER);   
 }
 /*!
     \brief      
@@ -153,51 +146,13 @@ void SysTick_Handler(void)
 */
 
 void ADC0_1_IRQHandler(void)
-{
+{    
+
     /* MCU self check count */
     clasB_uwADCCnt++;
-
-    if (cp_stFlg.CurrentSampleModelSelect == COMBINATION)
-    {
-        if (ADC_STAT(ADC0) & ADC_INT_FLAG_EOIC)
-        {         
-//            adc_uwRdsonUReg = ADC_IDATA0(ADC0);
-//            adc_uwRdsonVReg = ADC_IDATA1(ADC0);
-//            adc_uwRdsonWReg = ADC_IDATA2(ADC0);
-            Adcs[1].Results[HW_ADC_IA_CH] = ADC_IDATA0(ADC0);
-            Adcs[1].Results[HW_ADC_IB_CH] = ADC_IDATA1(ADC0);
-            Adcs[1].Results[HW_ADC_IC_CH] = ADC_IDATA2(ADC0);  
-                            
-            /* ADC0 disable */
-            ADC_CTL1(ADC0) &= ~((uint32_t)ADC_CTL1_ADCON);
-            /* ADC1 trigger set */
-            TIMER_CH3CV(TIMER0) = (uint32_t) Pwms[0].SyncSamplingUpTick[0];//pwm_stGenOut.uwSigRTrig;
-             /* ADC1 enable */
-            ADC_CTL1(ADC1) |= (uint32_t)ADC_CTL1_ADCON;
-            /* ADC0 interrupt flag clear */
-            ADC_STAT(ADC0) &= ~((uint32_t)ADC_INT_FLAG_EOIC);
-        }
-        else if (ADC_STAT(ADC1) & ADC_INT_FLAG_EOIC)
-        {         
-            if (pwm_stGenOut.blSampleCalibFlag == TRUE)
-            {
-                 Adcs[2].Results[HW_ADC_IDC_CH] = ADC_IDATA0(ADC1);
-            }           
-
-            /* ADC1 disable */
-            ADC_CTL1(ADC1) &= ~((uint32_t)ADC_CTL1_ADCON);
-            /* ADC1 interrupt flag clear */
-            ADC_STAT(ADC1) &= ~((uint32_t)ADC_INT_FLAG_EOIC); 
-        }
-        else
-        {
-            //do noting
-        }
-    }
-    else
-    {
-        //do noting
-    }
+    
+    iRtAdc_CompleteIsr(0);
+   
 }
 
 /*!
@@ -208,56 +163,10 @@ void ADC0_1_IRQHandler(void)
 */
 void TIMER0_UP_TIMER9_IRQHandler(void)
 {   
-    ULONG ulOvTimeCnt = 0;    
-    
-    /* MCU self check count */
-    clasB_uwTIM0Cnt ++;
-     
-    if(cp_stFlg.CurrentSampleModelSelect == COMBINATION)
-    {      
-        if(TIMER_INTF(TIMER0) & TIMER_INT_FLAG_UP)
-        {           
-            if((TIMER_CTL0(TIMER0) & TIMER_CTL0_DIR) == 0) // When Counting Up
-            { 
-                /* TBC Up interrupt */
-                tbc_voUpIsr();             
-            }
-            else
-            {               
-                TIMER_CNT(TIMER6) = 0;               
-                
-                /* TBC Down interrupt */
-                tbc_voDownIsr();
-                
-                /* Timing management, refer to the Software design description for details */
-                while ((TIMER_CNT(TIMER6) < HW_PWM_PERIOD) && (ulOvTimeCnt < 10000))
-                {
-                    ulOvTimeCnt++;
-                };
-                
-                /* ADC0 trigger set */
-                TIMER_CH3CV(TIMER0) = (uint32_t) Pwms[0].SyncSamplingUpTick[1];
-                /* ADC Enable*/
-                iAdc_Enable(0);
-                /* Software trigger for regular sampling*/
-
-                iAdc_Convert(0);                
-                /* Compara value load */   
-                iRtPwm_ActivateCompareValues(0);
-             
-                timer_dma_enable(TIMER0,TIMER_DMA_UPD);
-                dma_channel_enable(DMA0,DMA_CH4);  
+      /* MCU self check count */
+    clasB_uwTIM0Cnt ++;   
+    iRtPwm_UpdateIsr(0);
 
-            }
-        } 
-        /* Timer0 update interrupt flag clear */
-        TIMER_INTF(TIMER0) = ~(uint32_t)TIMER_INT_FLAG_UP;
-    }
-    else
-    {
-        //do noting
-    }
-    
 }
 
 /*!
@@ -325,16 +234,11 @@ void TIMER1_IRQHandler(void)
     \retval     none
 */
 void TIMER3_IRQHandler(void)
-{
+{       
+    /* MCU self check count */
+    clasB_uwTIM3Cnt++;
     
-    if(timer_interrupt_flag_get(TIMER3, TIMER_INT_FLAG_UP) != 0)
-    {
-        /* MCU self check count */
-        clasB_uwTIM3Cnt++;
-        /* TBS interrupt */
-        tbs_voIsr();
-        timer_interrupt_flag_clear(TIMER3, TIMER_INT_FLAG_UP);
-    }
+    iRtTimer_Isr(HW_TBS_TIMER);   
 }
 /*!
     \brief      
@@ -344,16 +248,11 @@ void TIMER3_IRQHandler(void)
 */
 void TIMER5_IRQHandler(void)
 {
-    if(timer_interrupt_flag_get(TIMER5, TIMER_INT_FLAG_UP) != 0)
-    {
-        /* MCU self check count */
-        clasB_uwTIM5Cnt++;
-        /* Timing of time slices */
-        TimingTaskTimerServer();
-        /* Event_1ms interrupt */
-        Event_1ms();
-        timer_interrupt_flag_clear(TIMER5, TIMER_INT_FLAG_UP);
-    }
+    /* MCU self check count */
+    clasB_uwTIM5Cnt++;
+    
+    iRtTimer_Isr(HW_EVENT1MS_TIMER);  
+    
 }
 
 /*!

+ 5 - 2
User project/1.FrameLayer/Source/main.c

@@ -35,6 +35,9 @@
 /************************************************************************
  Exported Functions:
 ************************************************************************/
+void AppInit();
+void AppLoop();
+
 /***************************************************************
  Function: main;
  Description:main function
@@ -634,8 +637,8 @@ void mn_voSoftwareInit(void)
 {
     /* System coefficient calculate */
     cof_voSysInit();
-    /* Motor parameter init */
-    //   mn_voMtParInit();
+    /* Api App Init*/
+    AppInit();
     /* Control Parameters init */
     mn_voMcParInit();
     /*cmd handle Initial */

+ 22 - 0
User project/1.FrameLayer/Source/sys_task.c

@@ -0,0 +1,22 @@
+#include "uart_monitor_appl.h"
+#include "sys_task.h"
+#include "CodePara.h"
+#include "STLmain.h"
+#include "ClassB.h"
+
+
+void SysTask1Ms()
+{
+    cp_ulSystickCnt ++;
+    /* MCU Self Check*/
+    clas_ubSystickFlg = 1; 
+    stl_voSystickProc();
+    /* UART Timeout */
+    UART_voApplTimer();
+
+}
+
+void SysTask10Ms()
+{
+    
+}

+ 1 - 2
User project/1.FrameLayer/Source/tbs.c

@@ -38,11 +38,10 @@ Revising History (ECL of this file):
  Subroutine Call: ...;
  Reference: N/A
 ****************************************************************/
-
 void tbs_voIsr(void)
 {
     /* Alarm detect */
-    alm_voDetecTBS(&alm_stIn, &alm_stDetectTbsCoef);
+//    alm_voDetecTBS(&alm_stIn, &alm_stDetectTbsCoef);
 
     /* 1st FSM */
     FSM1st_Sys_state.Tbs_hook();

+ 2 - 0
User project/3.BasicFunction/Include/classB.h

@@ -20,6 +20,8 @@ WLBDM_M0_SR_20170814-new FSM1.1, by cyf, create this file;
 *************************************************************************/
 #ifndef CLASSB_H
 #define CLASSB_H
+#include "typedefine.h"
+#include <stdint.h>
 /************************************************************************
  Compiler Directives (N/A)
 *************************************************************************/

+ 8 - 4
User project/4.BasicHardwSoftwLayer/1.BasicHardwLayer/Include/board_config.h

@@ -72,9 +72,10 @@
 /* ========================================================================== */
 /* ================================ API接口序号定义 =============================== */
 /* ========================================================================== */
-
-#define HW_TBS_TIMER     0
-#define HW_SYSTICK_TIMER 1
+#define HW_PWMCNT_TIMER                 0
+#define HW_TBS_TIMER                    1
+#define HW_EVENT1MS_TIMER               2
+#define HW_SYSTICK_TIMER                3
 
 #define TBS_TIMER                       0
 #define SYSTICK_TIMER                   1
@@ -112,6 +113,9 @@
 #define HW_GPIO_CANRX_PIN                  11 ///> GPIOB  PIN8 
 #define HW_GPIO_CANSTB_PIN                 12 ///> GPIOA  PIN12 
 #define HW_GPIO_I2CWP_PIN                  13 ///> GPIOC  PIN12 
-#define HW_GPIO_CadDir_PIN                  14 ///> GPIOB  PIN2
+#define HW_GPIO_CadDir_PIN                 14 ///> GPIOB  PIN2
 
+/* TIMER */
+#define HW_TIMER_CANDANCE_PIN             0  ///> TIMER1  CH2
+#define HW_TIMER_BIKESPD_PIN              1  ///> TIMER1  CH3
 /* Api Device Index Definations */

+ 1 - 1
User project/5.Api_rt/api_rt.c

@@ -3,7 +3,7 @@
 void iRt_Init()
 {
     iRtGpio_Init();
-//    iRtTimer_Init();
+    iRtTimer_Init();
 //    iRtUart_Init();
     iRtPwm_Init();
     iRtAdc_Init();

+ 1 - 1
User project/5.Api_rt/api_rt.h

@@ -4,7 +4,7 @@
 #include "board_config.h"
 #include "api_rt_gpio.h"
 //#include "api_rt_uart.h"
-//#include "api_rt_timer.h"
+#include "api_rt_timer.h"
 #include "api_rt_pwm.h"
 #include "api_rt_adc.h"
 

+ 14 - 6
User project/5.Api_rt/api_rt_adc.c

@@ -1,7 +1,8 @@
 #include "api_rt_adc.h"
+#include "api_rt_pwm.h"
 #include "gd32f30x.h"
 
-Adc_Handle Adcs[3];
+ApiRtAdc_Handle Adcs[3];
 
 
 void iRtAdc_Init(void)
@@ -28,23 +29,30 @@ void iRtAdc_Init(void)
     }
 
 };
-//void AdcLow_CompleteIsr(uint8_t devIndex);
+
+void iRtAdc_CompleteIsr(uint8_t devIndex)
+{
+    /* 处理同步采样的逻辑 */
+    iRtPwm_SyncSamplingAdcIsr(0);
+
+    if (Adcs[devIndex].CompleteISR.Action != 0)
+    {
+        Adcs[devIndex].CompleteISR.Action();
+    }
+}
 
 uint16_t iAdc_GetResult(uint8_t devIndex, uint8_t channelIndex)
 {
-//    ASSERT_LESS(devIndex, 1);
     return Adcs[devIndex].Results[channelIndex];
 }
 
 uint16_t* iAdc_GetResultPointer(uint8_t devIndex)
 {
-  //    ASSERT_LESS(devIndex, 1);
     return Adcs[devIndex].Results;
 }
 
 void iAdc_Enable(uint8_t devIndex)
 {
-//    ASSERT_LESS(devIndex, 1);
     if(RESET == (ADC_CTL1(Adcs[devIndex].AdcBase) & ADC_CTL1_ADCON)){
         ADC_CTL1(Adcs[devIndex].AdcBase) |= (uint32_t)ADC_CTL1_ADCON;
     }   
@@ -52,7 +60,6 @@ void iAdc_Enable(uint8_t devIndex)
 
 void iAdc_Disable(uint8_t devIndex)
 {
-//    ASSERT_LESS(devIndex, 1);
     ADC_CTL1(Adcs[devIndex].AdcBase) &= ~((uint32_t)ADC_CTL1_ADCON);
 }
 
@@ -76,6 +83,7 @@ void iAdc_Convert(uint8_t devIndex)
 
 int8_t iAdc_ChannelGroupSwitch(uint8_t devIndex, uint8_t groupIndex)
 {
+    return 0;
 }
 
 void iAdc_EnableConversionInterrupt(uint8_t devIndex)

+ 4 - 3
User project/5.Api_rt/api_rt_adc.h

@@ -3,6 +3,7 @@
 
 #include <stdint.h>
 #include "api_adc.h"
+#include "api_rt_common.h"
 #include "gd32f30x.h"
 
 #define ADC_DEVICE_COUNT        1
@@ -14,10 +15,10 @@ typedef struct
     uint32_t DmaBase;
     dma_channel_enum *DmaChannelBase;
     uint16_t Results[ADC_RESULT_SIZE];
+    ApiRt_Interrupt      CompleteISR;
+}  ApiRtAdc_Handle;
 
-}  Adc_Handle;
-
-extern Adc_Handle Adcs[3];
+extern ApiRtAdc_Handle Adcs[3];
 
 void iRtAdc_Init(void);
 void iRtAdc_CompleteIsr(uint8_t devIndex);

+ 218 - 102
User project/5.Api_rt/api_rt_pwm.c

@@ -1,9 +1,11 @@
 #include "api_rt_pwm.h"
 #include "api_rt_dbg.h"
+#include "api_rt_timer.h"
 #include "board_config.h"
 #include "gd32f30x.h"
 #include <stdint.h>
 #include "api_rt_adc.h"
+#include "classB.h"
 
 ApiRtPwm_Handle Pwms[1];
 
@@ -19,7 +21,19 @@ void iRtPwm_Init()
         Pwms[0].CompareValues[i]       = HW_INIT_HHPWM_PERIOD;
         Pwms[0].ActiveCompareValues[i] = HW_INIT_HHPWM_PERIOD;
     }
+    Pwms[0].CountZeroISR.Enable = 0;
+    Pwms[0].CountZeroISR.Action = 0;
+    Pwms[0].CountMaxISR.Enable  = 0;
+    Pwms[0].CountMaxISR.Action  = 0;
+    Pwms[0].BreakISR.Enable     = 0;
+    Pwms[0].BreakISR.Action     = 0;
     Pwms[0].OutputEnable        = 0;
+    for (int i = 0; i < 4; i++)
+    {
+        Pwms[0].ChannelISR[i].Enable = 0;
+        Pwms[0].ChannelISR[i].Action = 0;
+        // Pwms[0].ChannelOutputEnable[i] = 0;
+    }
 #if API_FUNCTION_PWM_SYNC_MULTI_SAMPLING
     Pwms[0].SyncSamplingEnable        = 1;
     Pwms[0].SyncSamplingUpTickCount   = 1;
@@ -28,6 +42,107 @@ void iRtPwm_Init()
     Pwms[0].SyncSamplingDownTick[0]   = HW_SAMPLE_BEFORE_UPDATE_CNTS;
 #endif
 
+}
+void iRtPwm_CountZeroIsr(uint8_t devIndex)
+{
+    if (Pwms[devIndex].CountZeroISR.Enable)
+    {
+        if (Pwms[devIndex].CountZeroISR.Action != 0)
+        {
+            Pwms[devIndex].CountZeroISR.Action();
+        }
+    }
+}
+
+void iRtPwm_CountMaxIsr(uint8_t devIndex)
+{
+    if (Pwms[devIndex].CountMaxISR.Enable)
+    {
+        if (Pwms[devIndex].CountMaxISR.Action != 0)
+        {
+            Pwms[devIndex].CountMaxISR.Action();
+        }
+    }
+}
+
+void iRtPwm_UpdateIsr(uint8_t devIndex)
+{
+    uint32_t base = Pwms[devIndex].PwmBase;
+    uint32_t ulOvTimeCnt = 0;       
+  
+    if(TIMER_INTF(base) & TIMER_INT_FLAG_UP)
+    {           
+        if((TIMER_CTL0(base) & TIMER_CTL0_DIR) == 0) // When Counting Up
+        { 
+            /* TBC Up interrupt */
+            iRtPwm_CountZeroIsr(devIndex);             
+        }
+        else
+        {               
+            /* Reset Timer6 counts*/
+            iTimer_Reset(HW_PWMCNT_TIMER); 
+            /* TBC Down interrupt */
+            iRtPwm_CountMaxIsr(devIndex);
+            
+            /* Timing management, refer to the Software design description for details */
+            while ((iTimer_GetCount(HW_PWMCNT_TIMER) < HW_INIT_PWM_PERIOD) && (ulOvTimeCnt < 10000))
+            {
+                ulOvTimeCnt++;
+            };
+            
+            /* ADC0 trigger set */
+            iPwm_SetCompareValueImmediate(0,3,Pwms[0].SyncSamplingUpTick[1]);
+            /* ADC Enable*/
+            iAdc_Enable(0);
+            /* Software trigger for regular sampling*/
+            iAdc_Convert(0);                
+            /* Compara value load */   
+            iRtPwm_ActivateCompareValues(0);             
+            timer_dma_enable(base,TIMER_DMA_UPD);
+            dma_channel_enable(DMA0,DMA_CH4);  
+        }
+    } 
+    /* Timer0 update interrupt flag clear */
+    TIMER_INTF(base) = ~(uint32_t)TIMER_INT_FLAG_UP;
+}
+
+void iRtPwm_SyncSamplingAdcIsr(uint8_t devIndex)
+{
+  
+    if (ADC_STAT(ADC0) & ADC_INT_FLAG_EOIC)
+    {         
+//            adc_uwRdsonUReg = ADC_IDATA0(ADC0);
+//            adc_uwRdsonVReg = ADC_IDATA1(ADC0);
+//            adc_uwRdsonWReg = ADC_IDATA2(ADC0);
+        Adcs[1].Results[HW_ADC_IA_CH] = ADC_IDATA0(ADC0);
+        Adcs[1].Results[HW_ADC_IB_CH] = ADC_IDATA1(ADC0);
+        Adcs[1].Results[HW_ADC_IC_CH] = ADC_IDATA2(ADC0);  
+                        
+        /* ADC0 disable */
+        ADC_CTL1(ADC0) &= ~((uint32_t)ADC_CTL1_ADCON);
+        /* ADC1 trigger set */
+        iPwm_SetCompareValueImmediate(0,3,Pwms[0].SyncSamplingUpTick[0]);
+         /* ADC1 enable */
+        ADC_CTL1(ADC1) |= (uint32_t)ADC_CTL1_ADCON;
+        /* ADC0 interrupt flag clear */
+        ADC_STAT(ADC0) &= ~((uint32_t)ADC_INT_FLAG_EOIC);
+    }
+    else if (ADC_STAT(ADC1) & ADC_INT_FLAG_EOIC)
+    {         
+        if (Pwms[0].SyncSamplingUpTick[0] != HW_INIT_HHHPWM_PERIOD)
+        {
+             Adcs[2].Results[HW_ADC_IDC_CH] = ADC_IDATA0(ADC1);
+        }           
+        /* ADC1 disable */
+        ADC_CTL1(ADC1) &= ~((uint32_t)ADC_CTL1_ADCON);
+        /* ADC1 interrupt flag clear */
+        ADC_STAT(ADC1) &= ~((uint32_t)ADC_INT_FLAG_EOIC); 
+    }
+    else
+    {
+        //do noting
+    }
+
 }
 
 void iRtPwm_ActivateCompareValues(uint8_t devIndex)
@@ -39,6 +154,7 @@ void iRtPwm_ActivateCompareValues(uint8_t devIndex)
     }
 }
 
+
 /* ========================================================================== */
 /* ============================== API Functions ============================= */
 /* ========================================================================== */
@@ -420,108 +536,108 @@ void iPwm_DisableOutput(uint8_t devIndex)
 // //     }
 // // }
 
-//void iPwm_EnableDeviceInterrupt(uint8_t devIndex, ApiPwm_DeviceInterrupt interrupt)
-//{
-//    ASSERT_LESS(devIndex, 1);
-//
-//    switch (interrupt)
-//    {
-//    case ApiPwm_CountZeroInt:
-//        Pwms[devIndex].CountZeroISR.Enable = 1;
-//        break;
-//    case ApiPwm_CountMaxInt:
-//        Pwms[devIndex].CountMaxISR.Enable = 1;
-//        break;
-//    case ApiPwm_BreakInt:
-//        Pwms[devIndex].BreakISR.Enable = 1;
-//        break;
-//    default:
-//        break;
-//    }
-//}
-//
-//void iPwm_DisableDeviceInterrupt(uint8_t devIndex, ApiPwm_DeviceInterrupt interrupt)
-//{
-//    ASSERT_LESS(devIndex, 1);
-//
-//    switch (interrupt)
-//    {
-//    case ApiPwm_CountZeroInt:
-//        Pwms[devIndex].CountZeroISR.Enable = 0;
-//        break;
-//    case ApiPwm_CountMaxInt:
-//        Pwms[devIndex].CountMaxISR.Enable = 0;
-//        break;
-//    case ApiPwm_BreakInt:
-//        Pwms[devIndex].BreakISR.Enable = 0;
-//        break;
-//    default:
-//        break;
-//    }
-//}
-//
-//void iPwm_EnableChannelInterrupt(uint8_t devIndex, uint8_t channelIndex, ApiPwm_ChannelInterrupt interrupt)
-//{
-//    ASSERT_LESS(devIndex, 1);
-//    ASSERT_LESS(channelIndex, 4);
-//
-//    Pwms[devIndex].ChannelISR[channelIndex].Enable = 1;
-//}
-//
-//void iPwm_DisableChannelInterrupt(uint8_t devIndex, uint8_t channelIndex, ApiPwm_ChannelInterrupt interrupt)
-//{
-//    ASSERT_LESS(devIndex, 1);
-//    ASSERT_LESS(channelIndex, 4);
-//
-//    Pwms[devIndex].ChannelISR[channelIndex].Enable = 0;
-//}
-//
-//void iPwm_BindDeviceInterrupt(uint8_t devIndex, ApiPwm_DeviceInterrupt interrupt, void (*action)())
-//{
-//    ASSERT_LESS(devIndex, 1);
-//
-//    switch (interrupt)
-//    {
-//    case ApiPwm_CountZeroInt:
-//        Pwms[devIndex].CountZeroISR.Action = action;
-//        break;
-//    case ApiPwm_CountMaxInt:
-//        Pwms[devIndex].CountMaxISR.Action = action;
-//        break;
-//    case ApiPwm_BreakInt:
-//        Pwms[devIndex].BreakISR.Action = action;
-//        break;
-//    default:
-//        break;
-//    }
-//}
-//
-//void iPwm_BindChannelInterrupt(uint8_t devIndex, uint8_t channelIndex, ApiPwm_DeviceInterrupt interrupt, void (*action)())
-//{
-//    ASSERT_LESS(devIndex, 1);
-//    ASSERT_LESS(channelIndex, 4);
-//
-//    Pwms[devIndex].ChannelISR[channelIndex].Action = action;
-//}
-//
-//int8_t iPwm_GetBreakState(uint8_t devIndex)
-//{
-//    ASSERT_LESS(devIndex, 1);
-//
-//    TIM_TypeDef *base = Pwms[devIndex].PwmBase;
-//
-//    return (base->SR & ((uint32_t)0x00000080UL)) ? 1 : 0;
-//}
-//
-//void iPwm_ClearBreak(uint8_t devIndex)
-//{
-//    ASSERT_LESS(devIndex, 1);
-//
-//    TIM_TypeDef *base = Pwms[devIndex].PwmBase;
-//
-//    base->SR &= ~((uint32_t)0x00000080UL);
-//}
-//
+void iPwm_EnableDeviceInterrupt(uint8_t devIndex, ApiPwm_DeviceInterrupt interrupt)
+{
+    ASSERT_LESS(devIndex, 1);
+
+    switch (interrupt)
+    {
+    case ApiPwm_CountZeroInt:
+        Pwms[devIndex].CountZeroISR.Enable = 1;
+        break;
+    case ApiPwm_CountMaxInt:
+        Pwms[devIndex].CountMaxISR.Enable = 1;
+        break;
+    case ApiPwm_BreakInt:
+        Pwms[devIndex].BreakISR.Enable = 1;
+        break;
+    default:
+        break;
+    }
+}
+
+void iPwm_DisableDeviceInterrupt(uint8_t devIndex, ApiPwm_DeviceInterrupt interrupt)
+{
+    ASSERT_LESS(devIndex, 1);
+
+    switch (interrupt)
+    {
+    case ApiPwm_CountZeroInt:
+        Pwms[devIndex].CountZeroISR.Enable = 0;
+        break;
+    case ApiPwm_CountMaxInt:
+        Pwms[devIndex].CountMaxISR.Enable = 0;
+        break;
+    case ApiPwm_BreakInt:
+        Pwms[devIndex].BreakISR.Enable = 0;
+        break;
+    default:
+        break;
+    }
+}
+
+void iPwm_EnableChannelInterrupt(uint8_t devIndex, uint8_t channelIndex, ApiPwm_ChannelInterrupt interrupt)
+{
+    ASSERT_LESS(devIndex, 1);
+    ASSERT_LESS(channelIndex, 4);
+
+    Pwms[devIndex].ChannelISR[channelIndex].Enable = 1;
+}
+
+void iPwm_DisableChannelInterrupt(uint8_t devIndex, uint8_t channelIndex, ApiPwm_ChannelInterrupt interrupt)
+{
+    ASSERT_LESS(devIndex, 1);
+    ASSERT_LESS(channelIndex, 4);
+
+    Pwms[devIndex].ChannelISR[channelIndex].Enable = 0;
+}
+
+void iPwm_BindDeviceInterrupt(uint8_t devIndex, ApiPwm_DeviceInterrupt interrupt, void (*action)())
+{
+    ASSERT_LESS(devIndex, 1);
+
+    switch (interrupt)
+    {
+    case ApiPwm_CountZeroInt:
+        Pwms[devIndex].CountZeroISR.Action = action;
+        break;
+    case ApiPwm_CountMaxInt:
+        Pwms[devIndex].CountMaxISR.Action = action;
+        break;
+    case ApiPwm_BreakInt:
+        Pwms[devIndex].BreakISR.Action = action;
+        break;
+    default:
+        break;
+    }
+}
+
+void iPwm_BindChannelInterrupt(uint8_t devIndex, uint8_t channelIndex, ApiPwm_DeviceInterrupt interrupt, void (*action)())
+{
+    ASSERT_LESS(devIndex, 1);
+    ASSERT_LESS(channelIndex, 4);
+
+    Pwms[devIndex].ChannelISR[channelIndex].Action = action;
+}
+
+int8_t iPwm_GetBreakState(uint8_t devIndex)
+{
+    ASSERT_LESS(devIndex, 1);
+
+    uint32_t base = Pwms[devIndex].PwmBase;
+
+    return (TIMER_INTF(base) & ((uint32_t)0x00000080UL)) ? 1 : 0;
+}
+
+void iPwm_ClearBreak(uint8_t devIndex)
+{
+    ASSERT_LESS(devIndex, 1);
+
+    uint32_t base = Pwms[devIndex].PwmBase;
+
+    TIMER_INTF(base) &= ~((uint32_t)0x00000080UL);
+}
+
 #if API_FUNCTION_PWM_SYNC_MULTI_SAMPLING
 
 void iPwm_EnableSyncMultiSampling(uint8_t devIndex)

+ 4 - 0
User project/5.Api_rt/api_rt_pwm.h

@@ -11,6 +11,10 @@ typedef struct
     uint32_t      PwmBase;
     uint16_t      CompareValues[6];
     uint16_t      ActiveCompareValues[6];
+    ApiRt_Interrupt CountZeroISR;
+    ApiRt_Interrupt CountMaxISR;
+    ApiRt_Interrupt BreakISR;
+    ApiRt_Interrupt ChannelISR[4];
     int8_t        OutputEnable;
     
 #if API_FUNCTION_PWM_SYNC_MULTI_SAMPLING

+ 177 - 0
User project/5.Api_rt/api_rt_timer.c

@@ -0,0 +1,177 @@
+#include "api_timer.h"
+#include "api_rt_timer.h"
+#include "api_rt_dbg.h"
+
+#include "board_config.h"
+#include "gd32f30x.h"
+#include "classB.h"
+
+ApiRtTimer_Handle Timers[GENERAL_TIMER_COUNT + 1];
+
+void iRtTimer_Init()
+{
+    Timers[HW_PWMCNT_TIMER].TimerBase      = TIMER6;
+    Timers[HW_PWMCNT_TIMER].TickISR.Enable = 0;
+    
+    Timers[HW_TBS_TIMER].TimerBase      = TIMER3;
+    Timers[HW_TBS_TIMER].TickISR.Enable = 0;
+    
+    Timers[HW_EVENT1MS_TIMER].TimerBase      = TIMER5;
+    Timers[HW_EVENT1MS_TIMER].TickISR.Enable = 0;
+    
+    Timers[HW_SYSTICK_TIMER].TimerBase      = 0;
+    Timers[HW_SYSTICK_TIMER].TickISR.Enable = 0;
+}
+
+void iRtTimer_Isr(uint8_t devIndex)
+{
+    if (Timers[devIndex].TickISR.Action != 0)
+    {
+        Timers[devIndex].TickISR.Action();
+    }
+    if (devIndex < GENERAL_TIMER_COUNT)
+    {
+        uint32_t base = Timers[devIndex].TimerBase;
+        TIMER_INTF(base)&= ~0x0001;
+    }
+}
+
+uint32_t iTimer_GetClock(uint8_t devIndex)
+{
+    if (devIndex < SYSTEM_TICK_TIMER_INDEX)
+    {
+        return HW_TIM_CLOCK_HZ;
+    }
+    else
+    {
+        return HW_TIM_CLOCK_HZ;
+    }
+}
+
+void iTimer_Start(uint8_t devIndex)
+{
+    if (devIndex < SYSTEM_TICK_TIMER_INDEX)
+    {
+        uint32_t base = Timers[devIndex].TimerBase;
+        TIMER_CTL0(base) |= (uint32_t)BIT(0);
+    }
+    else 
+    {
+        SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
+    }
+}
+
+void iTimer_Stop(uint8_t devIndex)
+{
+    if (devIndex < SYSTEM_TICK_TIMER_INDEX)
+    {
+        uint32_t base = Timers[devIndex].TimerBase;
+        TIMER_CTL0(base) &= ~(uint32_t)BIT(0);
+    }
+    else
+    {
+        SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
+    }
+}
+
+void iTimer_EnableAutoReload(uint8_t devIndex)
+{
+    if (devIndex < SYSTEM_TICK_TIMER_INDEX)
+    {
+        uint32_t base = Timers[devIndex].TimerBase;
+        TIMER_CTL0(base) |= (uint32_t)BIT(7);
+    }
+}
+
+void iTimer_DisableAutoReload(uint8_t devIndex)
+{
+    if (devIndex < SYSTEM_TICK_TIMER_INDEX)
+    {
+        uint32_t base = Timers[devIndex].TimerBase;
+        TIMER_CTL0(base) &= ~(uint32_t)BIT(7);
+    }
+}
+
+uint32_t iTimer_GetPeriod(uint8_t devIndex)
+{
+    if (devIndex < SYSTEM_TICK_TIMER_INDEX)
+    {
+        uint32_t base = Timers[devIndex].TimerBase;
+        uint32_t count_value = 0U;
+        count_value = TIMER_CAR(base);
+        return count_value;
+    }
+    else
+    {
+        return SysTick->LOAD;
+    }
+}
+
+void iTimer_SetPeriod(uint8_t devIndex, uint32_t ticks)
+{
+    if (devIndex < SYSTEM_TICK_TIMER_INDEX)
+    {
+        uint32_t base = Timers[devIndex].TimerBase;
+
+        TIMER_CAR(base) = ticks;
+    }
+    else
+    {
+        SysTick->LOAD = ticks;
+    }
+}
+
+uint32_t iTimer_GetCount(uint8_t devIndex)
+{
+    if (devIndex < SYSTEM_TICK_TIMER_INDEX)
+    {
+        uint32_t base = Timers[devIndex].TimerBase;        
+        uint32_t count_value = 0U;
+        count_value = TIMER_CNT(base);
+        return count_value;
+    }
+    else
+    {
+        return SysTick->VAL;
+    }
+}
+
+void iTimer_Reset(uint8_t devIndex)
+{
+    if (devIndex < SYSTEM_TICK_TIMER_INDEX)
+    {
+        uint32_t base = Timers[devIndex].TimerBase;  
+        TIMER_CNT(base) = 0;
+    }
+    else
+    {
+        SysTick->VAL = SysTick->LOAD;
+    }
+}
+
+void iTimer_BindInterrupt(uint8_t devIndex, void (*action)())
+{
+    Timers[devIndex].TickISR.Action = action;
+}
+
+void iTimer_EnableInterrupt(uint8_t devIndex)
+{
+    if (devIndex < SYSTEM_TICK_TIMER_INDEX) {
+        uint32_t base = Timers[devIndex].TimerBase; 
+        TIMER_DMAINTEN(base)|= (uint16_t)0x01;
+    }
+    else {
+
+    }
+}
+
+void iTimer_DisableInterrupt(uint8_t devIndex)
+{
+    if (devIndex < SYSTEM_TICK_TIMER_INDEX) {
+        uint32_t base = Timers[devIndex].TimerBase; 
+        TIMER_DMAINTEN(base) &= ~((uint16_t)0x01);
+    }
+    else {
+
+    }
+}

+ 22 - 0
User project/5.Api_rt/api_rt_timer.h

@@ -0,0 +1,22 @@
+#ifndef _API_TIMER_LOW_H_
+#define _API_TIMER_LOW_H_
+
+#include <stdint.h>
+#include "api_rt_common.h"
+#include "gd32f30x.h"
+
+#define GENERAL_TIMER_COUNT         3
+#define SYSTEM_TICK_TIMER_INDEX     GENERAL_TIMER_COUNT
+
+typedef struct 
+{
+    uint32_t TimerBase;
+    ApiRt_Interrupt TickISR;
+} ApiRtTimer_Handle;
+
+extern ApiRtTimer_Handle Timers[GENERAL_TIMER_COUNT + 1];
+
+void iRtTimer_Init();
+void iRtTimer_Isr(uint8_t devIndex);
+
+#endif

+ 15 - 0
WLMCP.ewp

@@ -2429,6 +2429,9 @@
                 <file>
                     <name>$PROJ_DIR$\User project\1.FrameLayer\Include\main.h</name>
                 </file>
+                <file>
+                    <name>$PROJ_DIR$\User project\1.FrameLayer\Include\sys_task.h</name>
+                </file>
                 <file>
                     <name>$PROJ_DIR$\User project\1.FrameLayer\Include\tbc.h</name>
                 </file>
@@ -2444,6 +2447,9 @@
             </group>
             <group>
                 <name>Source</name>
+                <file>
+                    <name>$PROJ_DIR$\User project\1.FrameLayer\Source\app.c</name>
+                </file>
                 <file>
                     <name>$PROJ_DIR$\User project\1.FrameLayer\Source\FSM_1st.c</name>
                 </file>
@@ -2456,6 +2462,9 @@
                 <file>
                     <name>$PROJ_DIR$\User project\1.FrameLayer\Source\main.c</name>
                 </file>
+                <file>
+                    <name>$PROJ_DIR$\User project\1.FrameLayer\Source\sys_task.c</name>
+                </file>
                 <file>
                     <name>$PROJ_DIR$\User project\1.FrameLayer\Source\tbc.c</name>
                 </file>
@@ -2847,6 +2856,12 @@
             <file>
                 <name>$PROJ_DIR$\User project\5.Api_rt\api_rt_pwm.h</name>
             </file>
+            <file>
+                <name>$PROJ_DIR$\User project\5.Api_rt\api_rt_timer.c</name>
+            </file>
+            <file>
+                <name>$PROJ_DIR$\User project\5.Api_rt\api_rt_timer.h</name>
+            </file>
         </group>
         <group>
             <name>api</name>