Эх сурвалжийг харах

feat(Api_rt_PWM): 抽象层PWM移植,已经测试发波。

CN\zhangkai71 2 жил өмнө
parent
commit
7c9b61d7a9

+ 7 - 11
User project/1.FrameLayer/Source/gd32f30x_it.c

@@ -171,7 +171,7 @@ void ADC0_1_IRQHandler(void)
             /* ADC0 disable */
             ADC_CTL1(ADC0) &= ~((uint32_t)ADC_CTL1_ADCON);
             /* ADC1 trigger set */
-            TIMER_CH3CV(TIMER0) = (uint32_t) pwm_stGenOut.uwSigRTrig;
+            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 */
@@ -236,19 +236,15 @@ void TIMER0_UP_TIMER9_IRQHandler(void)
                 };
                 
                 /* ADC0 trigger set */
-                TIMER_CH3CV(TIMER0) = (uint32_t) pwm_stGenOut.uwRDSONTrig;
-//                adc_enable(ADC0);
+                TIMER_CH3CV(TIMER0) = (uint32_t) Pwms[0].SyncSamplingUpTick[1];
+                /* ADC Enable*/
                 iAdc_Enable(0);
                 /* Software trigger for regular sampling*/
-//                adc_software_trigger_enable(ADC0, ADC_REGULAR_CHANNEL); 
+
                 iAdc_Convert(0);                
-                /* Compara value load */    
-                hw_uwPWMCmpr[0] = pwm_stGenOut.uwNewTIM1COMPR[0];
-                hw_uwPWMCmpr[1] = pwm_stGenOut.uwNewTIM1COMPR[1];
-                hw_uwPWMCmpr[2] = pwm_stGenOut.uwNewTIM1COMPR[2];
-                hw_uwPWMCmpr[3] = pwm_stGenOut.uwNewTIM1COMPR[3];
-                hw_uwPWMCmpr[4] = pwm_stGenOut.uwNewTIM1COMPR[4];
-                hw_uwPWMCmpr[5] = pwm_stGenOut.uwNewTIM1COMPR[5];               
+                /* Compara value load */   
+                iRtPwm_ActivateCompareValues(0);
+             
                 timer_dma_enable(TIMER0,TIMER_DMA_UPD);
                 dma_channel_enable(DMA0,DMA_CH4);  
 

+ 4 - 2
User project/2.MotorDrive/Source/adc.c

@@ -59,8 +59,10 @@ void adc_voCalibration(ADC_COF *cof, ADC_DOWN_OUT *out1, ADC_UP_OUT *out2)
         {
             if(cp_stFlg.CurrentSampleModelSelect == COMBINATION)
             {
-                pwm_stGenOut.uwRDSONTrig = 129; 
-                pwm_stGenOut.uwSigRTrig = HW_HHHPWM_PERIOD;
+                ULONG samplingTick[2];
+                samplingTick[0]=HW_HHHPWM_PERIOD;
+                samplingTick[1]=129;
+                iPwm_SyncMultiSamplingCountUp(0, &samplingTick[0], 2);
                 pwm_stGenOut.blSampleCalibFlag = TRUE;
                 if (out1->uwADCCalibCt < (1 << ADC_CALIB_INDEX))
                 {

+ 7 - 0
User project/2.MotorDrive/Source/spdctrmode.c

@@ -828,6 +828,13 @@ void  scm_voSpdCtrMdDownTbc(void)
     pwm_stGenIn.uwVdcPu = adc_stUpOut.uwVdcLpfPu; // Q14
     pwm_voGen(&pwm_stGenIn, &pwm_stGenCoef, &pwm_stGenOut);
 
+    iPwm_SetCompareGroupValues16(0, pwm_stGenOut.uwNewTIM1COMPR);
+    ULONG samplingTick[2];
+    samplingTick[0]=pwm_stGenOut.uwSigRTrig;
+    samplingTick[1]=pwm_stGenOut.uwRDSONTrig;
+    iPwm_SyncMultiSamplingCountUp(0, &samplingTick[0], 2);
+    
+    
     Test_U_in.swAlphaPu = pwm_stGenOut.swUalphaPu - scm_swUalphaCompPu; // Q14
     Test_U_in.swBetaPu = pwm_stGenOut.swUbetaPu - scm_swUbetaCompPu;    // Q14
     Test_U_in.uwThetaPu = scm_uwAngIParkPu;                             // Q15

+ 8 - 8
User project/3.BasicFunction/Include/bikelight.h

@@ -30,16 +30,16 @@
 //---------------------------------------
 /* 前灯控制IO on/off */
 
-#define IO_FORWARDLED_ON()             iGpio_Write(HW_GPIO_FLIG_EN_PIN,iGpio_HighLevel)//PC15
-#define IO_FORWARDLED_OFF()            iGpio_Write(HW_GPIO_FLIG_EN_PIN,iGpio_LowLevel) //PC15
-#define IO_FORWARDLED_12V_ENABLE()     iGpio_Write(HW_GPIO_FLIG_6V_12V_EN_PIN,iGpio_HighLevel)//PC13
-#define IO_FORWARDLED_12V_DISABLE()    iGpio_Write(HW_GPIO_FLIG_6V_12V_EN_PIN,iGpio_LowLevel)//PC13
+#define IO_FORWARDLED_ON()             iGpio_Write(HW_GPIO_FLIG_EN_PIN,ApiGpio_HighLevel)//PC15
+#define IO_FORWARDLED_OFF()            iGpio_Write(HW_GPIO_FLIG_EN_PIN,ApiGpio_LowLevel) //PC15
+#define IO_FORWARDLED_12V_ENABLE()     iGpio_Write(HW_GPIO_FLIG_6V_12V_EN_PIN,ApiGpio_HighLevel)//PC13
+#define IO_FORWARDLED_12V_DISABLE()    iGpio_Write(HW_GPIO_FLIG_6V_12V_EN_PIN,ApiGpio_LowLevel)//PC13
 
 /* 后灯控制IO on/off */
-#define IO_BACKLED_ON()             iGpio_Write(HW_GPIO_BLIG_EN_PIN,iGpio_HighLevel)//PC8
-#define IO_BACKLED_OFF()            iGpio_Write(HW_GPIO_BLIG_EN_PIN,iGpio_LowLevel) //PC8
-#define IO_BACKLED_12V_ENABLE()     iGpio_Write(HW_GPIO_BLIG_6V_12V_EN_PIN,iGpio_HighLevel)//PC7
-#define IO_BACKLED_12V_DISABLE()    iGpio_Write(HW_GPIO_BLIG_6V_12V_EN_PIN,iGpio_LowLevel)//PC7
+#define IO_BACKLED_ON()             iGpio_Write(HW_GPIO_BLIG_EN_PIN,ApiGpio_HighLevel)//PC8
+#define IO_BACKLED_OFF()            iGpio_Write(HW_GPIO_BLIG_EN_PIN,ApiGpio_LowLevel) //PC8
+#define IO_BACKLED_12V_ENABLE()     iGpio_Write(HW_GPIO_BLIG_6V_12V_EN_PIN,ApiGpio_HighLevel)//PC7
+#define IO_BACKLED_12V_DISABLE()    iGpio_Write(HW_GPIO_BLIG_6V_12V_EN_PIN,ApiGpio_LowLevel)//PC7
 
 //---------------------------------------
 #define    BikeLedAddCycle    8  //亮尾灯周期

+ 3 - 3
User project/3.BasicFunction/Source/can.c

@@ -618,7 +618,7 @@ void DataProcess(UWORD ID, UBYTE Mode, UWORD Cmd, UBYTE Data[]) /* parasoft-supp
                     SendData(ID_MC_TO_CDL, MODE_REPORT, 0xA903, (UBYTE *)"ACK");
                 }
                 
-                iGpio_Write(HW_GPIO_POWERLOCK_PIN,iGpio_LowLevel);
+                iGpio_Write(HW_GPIO_POWERLOCK_PIN,ApiGpio_LowLevel);
             }
             break;
         }
@@ -638,7 +638,7 @@ void DataProcess(UWORD ID, UBYTE Mode, UWORD Cmd, UBYTE Data[]) /* parasoft-supp
                     SendData(ID_MC_TO_CDL, MODE_REPORT, 0xA903, (UBYTE *)"ACK");
                 }
                 
-                iGpio_Write(HW_GPIO_POWERLOCK_PIN,iGpio_LowLevel);
+                iGpio_Write(HW_GPIO_POWERLOCK_PIN,ApiGpio_LowLevel);
             }
             break;
         }
@@ -700,7 +700,7 @@ void DataProcess(UWORD ID, UBYTE Mode, UWORD Cmd, UBYTE Data[]) /* parasoft-supp
                     SendData(ID_MC_TO_CDL, MODE_REPORT, 0xA903, (UBYTE *)"ACK");
                 }
                 
-                iGpio_Write(HW_GPIO_POWERLOCK_PIN,iGpio_LowLevel);
+                iGpio_Write(HW_GPIO_POWERLOCK_PIN,ApiGpio_LowLevel);
             }
             break;
         }

+ 6 - 6
User project/3.BasicFunction/Source/i2c_master.c

@@ -63,7 +63,7 @@ void  i2c_voWaitEEReady(UBYTE SlaveAddr)
 //    while(!I2C_EE_ComuFltFlg)
 //    {
 //        /* write to EEPROM enable*/
-//        iGpio_Write(HW_GPIO_I2CWP_PIN,iGpio_LowLevel);
+//        iGpio_Write(HW_GPIO_I2CWP_PIN,ApiGpio_LowLevel);
 //        /* wait until I2C bus is idle */
 //        while(i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) && (++ulTimeCnt < I2C_SHORT_TIMEOUT));
 //        if(ulTimeCnt == I2C_SHORT_TIMEOUT)
@@ -137,7 +137,7 @@ void  i2c_voWaitEEReady(UBYTE SlaveAddr)
 //        }        
 //    }
 //    /* write to EEPROM disable */
-//    iGpio_Write(HW_GPIO_I2CWP_PIN,iGpio_LowLevel);
+//    iGpio_Write(HW_GPIO_I2CWP_PIN,ApiGpio_LowLevel);
     
 }
 
@@ -334,7 +334,7 @@ void i2c_voHistoryWriteBuffer(void)
 //    if(!I2C_EE_ComuFltFlg)
 //    {
 //        /* write to EEPROM enable*/
-//        iGpio_Write(HW_GPIO_I2CWP_PIN,iGpio_LowLevel);
+//        iGpio_Write(HW_GPIO_I2CWP_PIN,ApiGpio_LowLevel);
 //        /* wait until I2C bus is idle */
 //        while((i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) != 0) && (ulTimeCnt< I2C_SHORT_TIMEOUT))
 //    	{
@@ -417,7 +417,7 @@ void i2c_voHistoryWriteBuffer(void)
 //        }
 //        ulTimeCnt = 0;
 //        /* write to EEPROM disable */
-//        iGpio_Write(HW_GPIO_I2CWP_PIN,iGpio_LowLevel);
+//        iGpio_Write(HW_GPIO_I2CWP_PIN,ApiGpio_LowLevel);
 //
 //        /* I2C EEPROM communication timeout fault */
 //        if(uwTimeoutNum > 0)
@@ -445,7 +445,7 @@ void i2c_voPageWrite2EE(const UBYTE *pBuffer, UBYTE SlaveAddr, UBYTE WriteAddr,
     if(!I2C_EE_ComuFltFlg)
     {
         /* write to EEPROM enable*/
-        iGpio_Write(HW_GPIO_I2CWP_PIN,iGpio_HighLevel);
+        iGpio_Write(HW_GPIO_I2CWP_PIN,ApiGpio_HighLevel);
         /* wait until I2C bus is idle */
         while((i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) != 0) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
     	{
@@ -545,7 +545,7 @@ void i2c_voPageWrite2EE(const UBYTE *pBuffer, UBYTE SlaveAddr, UBYTE WriteAddr,
         }  
         /* write to EEPROM disable */
         
-        iGpio_Write(HW_GPIO_I2CWP_PIN,iGpio_LowLevel);
+        iGpio_Write(HW_GPIO_I2CWP_PIN,ApiGpio_LowLevel);
     }
     else
     {}

+ 2 - 2
User project/3.BasicFunction/Source/power.c

@@ -71,7 +71,7 @@ void power_voPowerManagement(ULONG ulAutoPowerOffDelayTime, ULONG SysTickCnt, UL
                 power_stPowStateOut.uwPowerStartupCnt++;
                 if (power_stPowStateOut.uwPowerStartupCnt == power_stPowStateCof.uwPowerStartTouchTimeCnt)
                 {
-                    iGpio_Write(HW_GPIO_POWERLOCK_PIN,iGpio_HighLevel);
+                    iGpio_Write(HW_GPIO_POWERLOCK_PIN,ApiGpio_HighLevel);
                     power_stPowStateOut.powerstate = POWER_ON;
                     power_stPowStateOut.uwPowerOn2OffCnt = 0;
                 }
@@ -147,7 +147,7 @@ void power_voPowerManagement(ULONG ulAutoPowerOffDelayTime, ULONG SysTickCnt, UL
                 power_stPowStateOut.blPowerShutdownFlg = TRUE;
                 power_stPowStateOut.uwPowerShutdnCnt = 0;
                 
-                iGpio_Write(HW_GPIO_POWERLOCK_PIN,iGpio_LowLevel);
+                iGpio_Write(HW_GPIO_POWERLOCK_PIN,ApiGpio_LowLevel);
             }
             break;
         }

+ 23 - 23
User project/3.BasicFunction/Source/spi_master.c

@@ -294,15 +294,15 @@ void  spi_voResolver(const SPI_RESOLVER_COEF *coef, SPI_RESOLVER_OUT *out)
 void spi_voResolverLock(void)
 {
     /* CS signal enable */
-    iGpio_Write(HW_GPIO_SPICS_PIN,iGpio_HighLevel);
-    iGpio_Write(HW_GPIO_SPICS_PIN,iGpio_HighLevel);
+    iGpio_Write(HW_GPIO_SPICS_PIN,ApiGpio_HighLevel);
+    iGpio_Write(HW_GPIO_SPICS_PIN,ApiGpio_HighLevel);
     while (spi_i2s_flag_get(SPI2, SPI_FLAG_TBE) != SET)
     {}
     spi_i2s_data_transmit(SPI2,0x0550);
     while (spi_i2s_flag_get(SPI2, SPI_FLAG_TRANS) != RESET)
     {}
     /* CS signal disable */
-    iGpio_Write(HW_GPIO_SPICS_PIN,iGpio_LowLevel);
+    iGpio_Write(HW_GPIO_SPICS_PIN,ApiGpio_LowLevel);
 }
 /*************************************************************************
  Function:
@@ -320,45 +320,45 @@ void spi_voReadWriteSeneorReg(void) /* parasoft-suppress METRICS-28 "本项目
     UWORD SPI_DelayCnt1 = 0, SPI_DelayCnt2 = 0;
 
     /* Read the BCT register value */
-    iGpio_Write(HW_GPIO_SPICS_PIN,iGpio_HighLevel); /*!< CS signal enable,for data update */
+    iGpio_Write(HW_GPIO_SPICS_PIN,ApiGpio_HighLevel); /*!< CS signal enable,for data update */
     while (spi_i2s_flag_get(SPI2, SPI_FLAG_TBE) != SET)
     {}
     spi_i2s_data_transmit(SPI2, 0x4200); //comp value
     while (spi_i2s_flag_get(SPI2, SPI_FLAG_TRANS) != RESET)
     {}
-    iGpio_Write(HW_GPIO_SPICS_PIN,iGpio_LowLevel);
+    iGpio_Write(HW_GPIO_SPICS_PIN,ApiGpio_LowLevel);
     while (spi_i2s_flag_get(SPI2, SPI_FLAG_RBNE) != SET)
     {}
     uwReadBCTReg = spi_i2s_data_receive(SPI2); /*!< Read the first time */
-    iGpio_Write(HW_GPIO_SPICS_PIN,iGpio_HighLevel);
+    iGpio_Write(HW_GPIO_SPICS_PIN,ApiGpio_HighLevel);
     while (spi_i2s_flag_get(SPI2, SPI_FLAG_TBE) != SET)
     {}
     spi_i2s_data_transmit(SPI2, 0x0000); /*!< Write the second time */
     while (spi_i2s_flag_get(SPI2, SPI_FLAG_TRANS) != RESET)
     {}
-    iGpio_Write(HW_GPIO_SPICS_PIN,iGpio_LowLevel);
+    iGpio_Write(HW_GPIO_SPICS_PIN,ApiGpio_LowLevel);
     while (spi_i2s_flag_get(SPI2, SPI_FLAG_RBNE) != SET)
     {}
     uwReadBCTReg = spi_i2s_data_receive(SPI2); /*!< Read the BCT register value */
 
     /* Read the ETX ETY value */
-    iGpio_Write(HW_GPIO_SPICS_PIN,iGpio_HighLevel);
+    iGpio_Write(HW_GPIO_SPICS_PIN,ApiGpio_HighLevel);
     while (spi_i2s_flag_get(SPI2, SPI_FLAG_TBE) != SET)
     {}
     spi_i2s_data_transmit(SPI2, 0x4300); //comp direction 
     while (spi_i2s_flag_get(SPI2, SPI_FLAG_TRANS) != RESET)
     {}
-    iGpio_Write(HW_GPIO_SPICS_PIN,iGpio_LowLevel);
+    iGpio_Write(HW_GPIO_SPICS_PIN,ApiGpio_LowLevel);
     while (spi_i2s_flag_get(SPI2, SPI_FLAG_RBNE) != SET)
     {}
     uwReadETXY = spi_i2s_data_receive(SPI2); /*!< Read the first time */
-    iGpio_Write(HW_GPIO_SPICS_PIN,iGpio_HighLevel);
+    iGpio_Write(HW_GPIO_SPICS_PIN,ApiGpio_HighLevel);
     while (spi_i2s_flag_get(SPI2, SPI_FLAG_TBE) != SET)
     {}
     spi_i2s_data_transmit(SPI2, 0x0000); /*!< Write the second time */
     while (spi_i2s_flag_get(SPI2, SPI_FLAG_TRANS) != RESET)
     {}
-    iGpio_Write(HW_GPIO_SPICS_PIN,iGpio_LowLevel);
+    iGpio_Write(HW_GPIO_SPICS_PIN,ApiGpio_LowLevel);
     while (spi_i2s_flag_get(SPI2, SPI_FLAG_RBNE) != SET)
     {}
     uwReadETXY = spi_i2s_data_receive(SPI2); /*!< Read the ETX ETY value */
@@ -376,14 +376,14 @@ void spi_voReadWriteSeneorReg(void) /* parasoft-suppress METRICS-28 "本项目
     //if (spi_blWriteRegFinishFlg == FALSE)
     {
         /* Write and Read BCT value*/
-        iGpio_Write(HW_GPIO_SPICS_PIN,iGpio_HighLevel);   // Data update
+        iGpio_Write(HW_GPIO_SPICS_PIN,ApiGpio_HighLevel);   // Data update
         while (spi_i2s_flag_get(SPI2, SPI_FLAG_TBE) != SET)// Discontinuous transmission, can not indicate send complete
         {}
         spi_i2s_data_transmit(SPI2, 0x8230); // LSB,BCT=48
         while (spi_i2s_flag_get(SPI2, SPI_FLAG_RBNE) != SET)
         {}
         uwWriteBCTReg = spi_i2s_data_receive(SPI2);       
-        iGpio_Write(HW_GPIO_SPICS_PIN,iGpio_LowLevel);
+        iGpio_Write(HW_GPIO_SPICS_PIN,ApiGpio_LowLevel);
         /* Delay at least 22ms */
         while (SPI_DelayCnt2 < 20)
         {
@@ -394,23 +394,23 @@ void spi_voReadWriteSeneorReg(void) /* parasoft-suppress METRICS-28 "本项目
                 SPI_DelayCnt1 = 0;
             }
         }
-        iGpio_Write(HW_GPIO_SPICS_PIN,iGpio_HighLevel);
+        iGpio_Write(HW_GPIO_SPICS_PIN,ApiGpio_HighLevel);
         while (spi_i2s_flag_get(SPI2, SPI_FLAG_TBE) != SET)
         {}
         spi_i2s_data_transmit(SPI2, 0x0000);
         while (spi_i2s_flag_get(SPI2, SPI_FLAG_RBNE) != SET)
         {}
         uwWriteBCTReg = spi_i2s_data_receive(SPI2);
-        iGpio_Write(HW_GPIO_SPICS_PIN,iGpio_LowLevel);
+        iGpio_Write(HW_GPIO_SPICS_PIN,ApiGpio_LowLevel);
         /* Write and Read ETX or ETY */
-        iGpio_Write(HW_GPIO_SPICS_PIN,iGpio_HighLevel);
+        iGpio_Write(HW_GPIO_SPICS_PIN,ApiGpio_HighLevel);
         while (spi_i2s_flag_get(SPI2, SPI_FLAG_TBE) != SET)
         {}
         spi_i2s_data_transmit(SPI2, 0x8301);
         while (spi_i2s_flag_get(SPI2, SPI_FLAG_RBNE) != SET)
         {}
         uwWriteETXY = spi_i2s_data_receive(SPI2);
-        iGpio_Write(HW_GPIO_SPICS_PIN,iGpio_LowLevel);
+        iGpio_Write(HW_GPIO_SPICS_PIN,ApiGpio_LowLevel);
         /* Delay at least 22ms */
         SPI_DelayCnt2 = 0;
         while (SPI_DelayCnt2 < 20)
@@ -422,24 +422,24 @@ void spi_voReadWriteSeneorReg(void) /* parasoft-suppress METRICS-28 "本项目
                 SPI_DelayCnt1 = 0;
             }
         }
-        iGpio_Write(HW_GPIO_SPICS_PIN,iGpio_HighLevel);
+        iGpio_Write(HW_GPIO_SPICS_PIN,ApiGpio_HighLevel);
         while (spi_i2s_flag_get(SPI2, SPI_FLAG_TBE) != SET)
         {}
         spi_i2s_data_transmit(SPI2, 0x0000);
         while (spi_i2s_flag_get(SPI2, SPI_FLAG_RBNE) != SET)
         {}
         uwWriteETXY = spi_i2s_data_receive(SPI2);
-        iGpio_Write(HW_GPIO_SPICS_PIN,iGpio_LowLevel);
+        iGpio_Write(HW_GPIO_SPICS_PIN,ApiGpio_LowLevel);
         
         /* Write and Read RD value*/
-        iGpio_Write(HW_GPIO_SPICS_PIN,iGpio_HighLevel);
+        iGpio_Write(HW_GPIO_SPICS_PIN,ApiGpio_HighLevel);
         while (spi_i2s_flag_get(SPI2, SPI_FLAG_TBE) != SET)
         {}
         spi_i2s_data_transmit(SPI2, 0x8980); // RD=1,Counterclockwise,8980; RD=0,Clockwise,8900
         while (spi_i2s_flag_get(SPI2, SPI_FLAG_RBNE) != SET)
         {}
         uwWriteRD = spi_i2s_data_receive(SPI2);
-        iGpio_Write(HW_GPIO_SPICS_PIN,iGpio_LowLevel);
+        iGpio_Write(HW_GPIO_SPICS_PIN,ApiGpio_LowLevel);
         /* Delay at least 22ms */
         SPI_DelayCnt2 = 0;
         while (SPI_DelayCnt2 < 20)
@@ -451,14 +451,14 @@ void spi_voReadWriteSeneorReg(void) /* parasoft-suppress METRICS-28 "本项目
                 SPI_DelayCnt1 = 0;
             }
         }
-        iGpio_Write(HW_GPIO_SPICS_PIN,iGpio_HighLevel);
+        iGpio_Write(HW_GPIO_SPICS_PIN,ApiGpio_HighLevel);
         while (spi_i2s_flag_get(SPI2, SPI_FLAG_TBE) != SET)
         {}
         spi_i2s_data_transmit(SPI2, 0x0000);
         while (spi_i2s_flag_get(SPI2, SPI_FLAG_RBNE) != SET)
         {}
         uwWriteRD = spi_i2s_data_receive(SPI2);
-        iGpio_Write(HW_GPIO_SPICS_PIN,iGpio_LowLevel);
+        iGpio_Write(HW_GPIO_SPICS_PIN,ApiGpio_LowLevel);
         
         if (uwWriteBCTReg == 0x3000 && uwWriteETXY == 0x0100 && uwWriteRD == 0x8000) // MSB
         {

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

@@ -29,7 +29,7 @@
 
 /* ---------------------------------- 系统时钟 ---------------------------------- */
 
-#define HW_MCU_CLOCK_HZ 64000000UL
+#define HW_MCU_CLOCK_HZ 72000000UL
 
 /* --------------------------------- 电力系统参数 --------------------------------- */
 
@@ -37,11 +37,11 @@
 
 /* --------------------------------- PWM频率配置 -------------------------------- */
 
-#define HW_TIM_CLOCK_HZ  64000000UL
+#define HW_TIM_CLOCK_HZ  72000000UL
 #define HW_TIM_CLOCK_MHZ (HW_TIM_CLOCK_HZ / 1000000UL)
 
-#define HW_INIT_FPWM_HZ   5000
-#define HW_INIT_FTBC_TICK 1
+#define HW_INIT_FPWM_HZ   16000
+#define HW_INIT_FTBC_TICK 2
 #define HW_INIT_FTBS_TICK 4
 #define HW_INIT_FTBC_HZ   (HW_INIT_FPWM_HZ / HW_INIT_FTBC_TICK)
 #define HW_INIT_FTBS_HZ   (HW_INIT_FPWM_HZ / HW_INIT_FTBS_TICK)

+ 5 - 4
User project/4.BasicHardwSoftwLayer/1.BasicHardwLayer/Include/hwsetup.h

@@ -24,6 +24,7 @@
 #include "gd32f30x_libopt.h"
 #include "STLmain.h"
 #include "syspar.h"
+#include "board_config.h"
 /************************************************************************
  Compiler Directives (N/A)
 ************************************************************************/
@@ -49,10 +50,10 @@
 /* Time task base use TIMER5 */
 #define HW_1MS_PERIOD TIM5CLK_KHZ 
 /* PWM period define */
-#define HW_PWM_PERIOD     (TIM0CLK_KHZ * 1000 / FPWM_HZ)
-#define HW_HPWM_PERIOD    (HW_PWM_PERIOD / 2)
-#define HW_HHPWM_PERIOD   (HW_HPWM_PERIOD / 2)
-#define HW_HHHPWM_PERIOD  (HW_HHPWM_PERIOD / 2)
+#define HW_PWM_PERIOD     HW_INIT_PWM_PERIOD
+#define HW_HPWM_PERIOD    HW_INIT_HPWM_PERIOD
+#define HW_HHPWM_PERIOD   HW_INIT_HHPWM_PERIOD
+#define HW_HHHPWM_PERIOD  HW_INIT_HHHPWM_PERIOD
 #define HW_PWM_PERIOD_INV (((ULONG)1 << 21) / HW_PWM_PERIOD) // Q21
 /* ADC trigger time */
 #define HW_ADC_TRI_TIME_US 10 /* unit: 0.1us */

+ 24 - 23
User project/4.BasicHardwSoftwLayer/1.BasicHardwLayer/Source/hwsetup.c

@@ -344,7 +344,7 @@ void hw_voInitDMA(void)
 
     dma_init_struct.periph_addr = (uint32_t)(&TIMER_DMATB(TIMER0));
     dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
-    dma_init_struct.memory_addr = (uint32_t)(hw_uwPWMCmpr);
+    dma_init_struct.memory_addr = (uint32_t)(Pwms[0].ActiveCompareValues);
     dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
     dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_16BIT;
     dma_init_struct.memory_width = DMA_MEMORY_WIDTH_16BIT;
@@ -1226,12 +1226,13 @@ void hw_voTimEn(void)
 void hw_voPWMInit(void)
 {
     /* Set 50% duty */
-    pwm_stGenOut.uwNewTIM1COMPR[0] = HW_HHPWM_PERIOD;
-    pwm_stGenOut.uwNewTIM1COMPR[1] = HW_HHPWM_PERIOD;
-    pwm_stGenOut.uwNewTIM1COMPR[2] = HW_HHPWM_PERIOD;
-    pwm_stGenOut.uwNewTIM1COMPR[3] = HW_HHPWM_PERIOD;
-    pwm_stGenOut.uwNewTIM1COMPR[4] = HW_HHPWM_PERIOD;
-    pwm_stGenOut.uwNewTIM1COMPR[5] = HW_HHPWM_PERIOD;
+    UWORD prd = iPwm_GetCountMax(0);
+    iPwm_SetCompareValue(0, 0, prd >> 1);
+    iPwm_SetCompareValue(0, 1, prd >> 1);
+    iPwm_SetCompareValue(0, 2, prd >> 1);
+    iPwm_SetCompareValueDelay(0, 0, prd >> 1);
+    iPwm_SetCompareValueDelay(0, 1, prd >> 1);
+    iPwm_SetCompareValueDelay(0, 2, prd >> 1);
 
     /* PWM off */
     hw_voPWMOff();
@@ -1314,12 +1315,13 @@ void hw_voPWMOff(void)
 void hw_voCharge(void)
 {
     /* Set 50% duty */
-    pwm_stGenOut.uwNewTIM1COMPR[0] = HW_HHPWM_PERIOD;
-    pwm_stGenOut.uwNewTIM1COMPR[1] = HW_HHPWM_PERIOD;
-    pwm_stGenOut.uwNewTIM1COMPR[2] = HW_HHPWM_PERIOD;
-    pwm_stGenOut.uwNewTIM1COMPR[3] = HW_HHPWM_PERIOD;
-    pwm_stGenOut.uwNewTIM1COMPR[4] = HW_HHPWM_PERIOD;
-    pwm_stGenOut.uwNewTIM1COMPR[5] = HW_HHPWM_PERIOD;
+    UWORD prd = iPwm_GetCountMax(0);
+    iPwm_SetCompareValue(0, 0, prd >> 1);
+    iPwm_SetCompareValue(0, 1, prd >> 1);
+    iPwm_SetCompareValue(0, 2, prd >> 1);
+    iPwm_SetCompareValueDelay(0, 0, prd >> 1);
+    iPwm_SetCompareValueDelay(0, 1, prd >> 1);
+    iPwm_SetCompareValueDelay(0, 2, prd >> 1);
 
     /* PWM on */
     hw_voPWMOn();
@@ -1336,16 +1338,15 @@ void hw_voCharge(void)
 void hw_voThrPhsShrt(void)
 {
     /* Set 0% duty */
-    pwm_stGenOut.uwNewTIM1COMPR[0] = HW_HPWM_PERIOD;
-    pwm_stGenOut.uwNewTIM1COMPR[1] = HW_HPWM_PERIOD;
-    pwm_stGenOut.uwNewTIM1COMPR[2] = HW_HPWM_PERIOD;
-    pwm_stGenOut.uwNewTIM1COMPR[3] = HW_HPWM_PERIOD;
-    pwm_stGenOut.uwNewTIM1COMPR[4] = HW_HPWM_PERIOD;
-    pwm_stGenOut.uwNewTIM1COMPR[5] = HW_HPWM_PERIOD;
-
-    pwm_stGenOut.uwFirstTrigCOMPR = HW_HHHPWM_PERIOD;
-    pwm_stGenOut.uwSecondTrigCOMPR = HW_HPWM_PERIOD - HW_HHHPWM_PERIOD;
-
+    iPwm_SetCompareValue(0, 0, 0);
+    iPwm_SetCompareValue(0, 1, 0);
+    iPwm_SetCompareValue(0, 2, 0);
+    iPwm_SetCompareValueDelay(0, 2, 0);
+    iPwm_SetCompareValueDelay(0, 2, 0);
+    iPwm_SetCompareValueDelay(0, 2, 0);
+    iPwm_SetCompareValueImmediate(0, 2, 0);
+    iPwm_SetCompareValueImmediate(0, 2, 0);
+    iPwm_SetCompareValueImmediate(0, 2, 0);
     /* PWM on */
     hw_voPWMOn();
 }

+ 1 - 1
User project/4.BasicHardwSoftwLayer/2.BasicSoftwLayer/Source/CodePara.c

@@ -46,7 +46,7 @@ void CodeParaInit(void)
 }
 #elif ((MOTOR_ID_SEL == MOTOR_WELLING_MTB_36V) || (MOTOR_ID_SEL == MOTOR_WELLING_MTB_48V))
 {
-    cp_stFlg.RunModelSelect = MountainBIKE;//ClZLOOP; MountainBIKE
+    cp_stFlg.RunModelSelect = ClZLOOP;//ClZLOOP; MountainBIKE
     cp_stFlg.RotateDirectionSelect = ForwardRotate;
 }
 #endif     

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

@@ -5,6 +5,6 @@ void iRt_Init()
     iRtGpio_Init();
 //    iRtTimer_Init();
 //    iRtUart_Init();
-//    iRtPwm_Init();
+    iRtPwm_Init();
     iRtAdc_Init();
 }

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

@@ -1,10 +1,11 @@
+#include "api.h"
 #include "api_config.h"
 #include "api_board_support.h"
 #include "board_config.h"
 #include "api_rt_gpio.h"
 //#include "api_rt_uart.h"
 //#include "api_rt_timer.h"
-//#include "api_rt_pwm.h"
+#include "api_rt_pwm.h"
 #include "api_rt_adc.h"
 
 #ifndef _API_RT_H_

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

@@ -74,7 +74,7 @@ void iAdc_Convert(uint8_t devIndex)
     ADC_CTL1(Adcs[devIndex].AdcBase) |= ADC_CTL1_SWRCST;
 }
 
-void iAdc_ChannelGroupSwitch(uint8_t devIndex, uint8_t groupIndex)
+int8_t iAdc_ChannelGroupSwitch(uint8_t devIndex, uint8_t groupIndex)
 {
 }
 

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

@@ -0,0 +1,22 @@
+#ifndef _API_RT_COMMON_H_
+#define _API_RT_COMMON_H_
+
+#include "stdint.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif // __cplusplus
+
+typedef struct
+{
+    uint8_t Enable;
+    void (*Action)();
+} ApiRt_Interrupt;
+
+void iRt_Init();
+
+#ifdef __cplusplus
+}
+#endif // __cplusplus
+
+#endif

+ 10 - 0
User project/5.Api_rt/api_rt_dbg.c

@@ -0,0 +1,10 @@
+#include "api_rt_dbg.h"
+
+AssertFailRecord AssertRecord;
+
+void assert_fail(char *fileName, int lineNo)
+{
+    AssertRecord.LastFileName = fileName;
+    AssertRecord.LastLineNo   = lineNo;
+    AssertRecord.FailCount += 1;
+}

+ 74 - 0
User project/5.Api_rt/api_rt_dbg.h

@@ -0,0 +1,74 @@
+#ifndef _API_RT_DBG_H_
+#define _API_RT_DBG_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif // __cplusplus
+
+#define API_RT_ASSERT_NONE 0
+#define API_RT_ASSERT_MIN  1
+#define API_RT_ASSERT_FULL 2
+
+#define API_RT_USE_ASSERT API_RT_ASSERT_MIN
+
+#if API_RT_USE_ASSERT == API_RT_ASSERT_FULL
+#define ASSERT_EQ(X, Y)                      \
+    do                                       \
+    {                                        \
+        if (((X) != (Y)))                    \
+        {                                    \
+            assert_fail(__FILE__, __LINE__); \
+            while (1)                        \
+                ;                            \
+        }                                    \
+    } while (0)
+#define ASSERT_LESS(X, Y)                    \
+    do                                       \
+    {                                        \
+        if (((X) >= (Y)))                    \
+        {                                    \
+            assert_fail(__FILE__, __LINE__); \
+            while (1)                        \
+                ;                            \
+        }                                    \
+    } while (0)
+#elif API_RT_USE_ASSERT == API_RT_ASSERT_MIN
+#define ASSERT_EQ(X, Y)   \
+    do                    \
+    {                     \
+        if (((X) != (Y))) \
+        {                 \
+            while (1)     \
+                ;         \
+        }                 \
+    } while (0)
+#define ASSERT_LESS(X, Y) \
+    do                    \
+    {                     \
+        if (((X) >= (Y))) \
+        {                 \
+            while (1)     \
+                ;         \
+        }                 \
+    } while (0)
+#elif API_RT_USE_ASSERT == API_RT_ASSERT_NONE
+#define ASSERT_EQ(X, Y)
+#define ASSERT_LESS(X, Y)
+#endif
+
+typedef struct
+{
+    int   FailCount;
+    char *LastFileName;
+    int   LastLineNo;
+} AssertFailRecord;
+
+extern AssertFailRecord AssertRecord;
+
+void assert_fail(char *fileName, int lineNo);
+
+#ifdef __cplusplus
+}
+#endif // __cplusplus
+
+#endif

+ 5 - 5
User project/5.Api_rt/api_rt_gpio.c

@@ -46,26 +46,26 @@ void iGpio_SetMode(uint16_t pin, ApiGpio_PinMode mode)
 }
 
 
-iGpio_Level iGpio_Read(uint16_t pin)
+ApiGpio_Level iGpio_Read(uint16_t pin)
 {
     const Gpio_Handle *const gpio = &Gpios[pin];
 
-    iGpio_Level level = iGpio_LowLevel;
+    ApiGpio_Level level = ApiGpio_LowLevel;
 
     if ((uint32_t)RESET != (GPIO_ISTAT(gpio->GpioBase)&((uint32_t)((uint32_t)0x01U << gpio->Index))))
     {
-        level = iGpio_HighLevel;
+        level = ApiGpio_HighLevel;
     }
 
     return level;
 }
 
 
-void iGpio_Write(uint16_t pin, iGpio_Level level)
+void iGpio_Write(uint16_t pin, ApiGpio_Level level)
 {
     const Gpio_Handle *const gpio = &Gpios[pin];
 
-    if (level == iGpio_HighLevel)
+    if (level == ApiGpio_HighLevel)
     {
       GPIO_OCTL(gpio->GpioBase) |= ((uint32_t)((uint32_t)0x01U << gpio->Index));
     }

+ 565 - 0
User project/5.Api_rt/api_rt_pwm.c

@@ -0,0 +1,565 @@
+#include "api_rt_pwm.h"
+#include "api_rt_dbg.h"
+#include "board_config.h"
+#include "gd32f30x.h"
+#include <stdint.h>
+#include "api_rt_adc.h"
+
+ApiRtPwm_Handle Pwms[1];
+
+/* ========================================================================== */
+/* ============================ Api RT Functions ============================ */
+/* ========================================================================== */
+
+void iRtPwm_Init()
+{
+    Pwms[0].PwmBase = TIMER0;
+    for (int i = 0; i < 6; i++)
+    {
+        Pwms[0].CompareValues[i]       = HW_INIT_HHPWM_PERIOD;
+        Pwms[0].ActiveCompareValues[i] = HW_INIT_HHPWM_PERIOD;
+    }
+    Pwms[0].OutputEnable        = 0;
+#if API_FUNCTION_PWM_SYNC_MULTI_SAMPLING
+    Pwms[0].SyncSamplingEnable        = 1;
+    Pwms[0].SyncSamplingUpTickCount   = 1;
+    Pwms[0].SyncSamplingUpTick[0]     = HW_INIT_HHPWM_PERIOD;
+    Pwms[0].SyncSamplingDownTickCount = 1;
+    Pwms[0].SyncSamplingDownTick[0]   = HW_SAMPLE_BEFORE_UPDATE_CNTS;
+#endif
+
+}
+
+void iRtPwm_ActivateCompareValues(uint8_t devIndex)
+{
+    ASSERT_LESS(devIndex, 1);
+    for (int i = 0; i < 6; i++)
+    {
+        Pwms[devIndex].ActiveCompareValues[i] = Pwms[devIndex].CompareValues[i];
+    }
+}
+
+/* ========================================================================== */
+/* ============================== API Functions ============================= */
+/* ========================================================================== */
+
+uint32_t iPwm_GetClock(uint8_t devIndex)
+{
+    return HW_TIM_CLOCK_HZ;
+}
+
+void iPwm_EnableCount(uint8_t devIndex)
+{
+    uint32_t base = Pwms[devIndex].PwmBase;
+    TIMER_CTL0(base) |= (uint32_t)BIT(0);
+}
+
+void iPwm_DisableCount(uint8_t devIndex)
+{
+    uint32_t base = Pwms[devIndex].PwmBase;
+    TIMER_CTL0(base) &= ~(uint32_t)BIT(0);
+}
+
+uint32_t iPwm_GetCountMax(uint8_t devIndex)
+{
+    uint32_t base = Pwms[devIndex].PwmBase;
+    uint32_t count_value = 0U;
+    count_value = TIMER_CAR(base);
+    return count_value;
+}
+
+void iPwm_SetCountMax(uint8_t devIndex, uint32_t countMax)
+{
+    uint32_t base = Pwms[devIndex].PwmBase;
+    TIMER_CAR(base)= countMax;
+}
+
+uint32_t iPwm_GetCount(uint8_t devIndex)
+{
+    uint32_t base = Pwms[devIndex].PwmBase;
+    uint32_t count_value = 0U;
+    count_value = TIMER_CNT(base);
+    return count_value;
+}
+
+void iPwm_SetCount(uint8_t devIndex, uint32_t count)
+{
+    uint32_t base = Pwms[devIndex].PwmBase;
+    TIMER_CNT(base)= count;
+}
+
+ApiPwm_CountMode iPwm_GetCountMode(uint8_t devIndex)
+{
+    uint32_t base = Pwms[devIndex].PwmBase;
+    ApiPwm_CountMode mode = ApiPwm_CountUp;    
+    
+    if ((TIMER_CTL0(base) & (uint16_t)0x0060) == (uint16_t)0x0000)
+    {
+        if ((TIMER_CTL0(base) & (uint16_t)0x0010) != 0)
+        {
+            mode = ApiPwm_CountUp;
+        }
+#if API_FUNCTION_PWM_COUNTDOWN == SUPPORT
+        else
+        {
+            mode = ApiPwm_CountDown;
+        }
+#endif
+    }
+    else
+    {
+        mode = ApiPwm_CountUpDown;
+    }
+    return mode;
+}
+
+void iPwm_SetCountMode(uint8_t devIndex, ApiPwm_CountMode mode)
+{  
+    uint32_t base = Pwms[devIndex].PwmBase;
+    switch (mode)
+    {
+    case ApiPwm_CountUp:
+        TIMER_CTL0(base) &= ~((uint16_t)0x0060);
+        TIMER_CTL0(base) &= ~(uint16_t)0x0010;
+        break;
+#if FUNCTION_PWM_COUNTDOWN == SUPPORT
+    case ApiPwm_CountDown:
+        TIMER_CTL0(base) &= ~((uint16_t)0x0060);
+        TIMER_CTL0(base) |= (uint16_t)0x0010;
+        break;
+#endif
+    case ApiPwm_CountUpDown:
+        TIMER_CTL0(base) |= ((uint16_t)0x0060);
+        break;
+    default:
+        break;
+    }
+}
+
+ApiPwm_CompareMode iPwm_GetCompareMode(uint8_t devIndex, uint8_t channelIndex)
+{
+    uint32_t base = Pwms[devIndex].PwmBase;
+    uint8_t      conf = 0;
+    switch (channelIndex)
+    {
+    case 0:
+        conf = TIMER_CHCTL0(base) & (uint32_t)0x000000F0UL;
+        break;
+    case 1:
+        conf = (TIMER_CHCTL0(base) & (uint32_t)0x0000F000UL) >> 16;
+        break;
+    case 2:
+        conf = TIMER_CHCTL1(base) & (uint32_t)0x000000F0UL;
+        break;
+    case 3:
+        conf = (TIMER_CHCTL1(base) & (uint32_t)0x0000F000UL) >> 16;
+        break;
+    default:
+        break;
+    }
+
+    ApiPwm_CompareMode mode = ApiPwm_NoAction;
+    switch (conf >> 4)
+    {
+    case 0x00:
+        mode = ApiPwm_NoAction;
+        break;
+    case 0x01:
+        mode = ApiPwm_UpMatchSet;
+#if FUNCTION_PWM_COUNTDOWN
+        if (iPwm_GetCountMode(devIndex) == Pwm_CountDown)
+            mode = ApiPwm_DownMatchSet;
+#endif
+        break;
+    case 0x02:
+        mode = ApiPwm_UpMatchClear;
+#if FUNCTION_PWM_COUNTDOWN
+        if (iPwm_GetCountMode(devIndex) == Pwm_CountDown)
+            mode = ApiPwm_DownMatchClear;
+#endif
+        break;
+    case 0x03:
+        mode = ApiPwm_UpMatchToggle;
+#if FUNCTION_PWM_COUNTDOWN
+        if (iPwm_GetCountMode(devIndex) == Pwm_CountDown)
+            mode = ApiPwm_DownMatchToggle;
+#endif
+        break;
+    case 0x04:
+        mode = ApiPwm_ForceClear;
+        break;
+    case 0x05:
+        mode = ApiPwm_ForceSet;
+        break;
+    case 0x06:
+        mode = ApiPwm_HigherClear;
+        break;
+    case 0x07:
+        mode = ApiPwm_HigherSet;
+        break;
+    default:
+        mode = ApiPwm_NoAction;
+        break;
+    }
+    return mode;
+}
+
+void iPwm_SetCompareMode(uint8_t devIndex, uint8_t channelIndex, ApiPwm_CompareMode mode)
+{
+    uint32_t base = Pwms[devIndex].PwmBase;
+
+    uint8_t conf = 0;
+    switch (mode)
+    {
+    case ApiPwm_NoAction:
+        conf = 0x00;
+        break;
+    case ApiPwm_UpMatchSet:
+        conf = 0x01;
+        break;
+    case ApiPwm_UpMatchClear:
+        conf = 0x02;
+        break;
+    case ApiPwm_UpMatchToggle:
+        conf = 0x03;
+        break;
+    case ApiPwm_ForceClear:
+        conf = 0x04;
+        break;
+    case ApiPwm_ForceSet:
+        conf = 0x05;
+        break;
+    case ApiPwm_HigherClear:
+        conf = 0x06;
+        break;
+    case ApiPwm_HigherSet:
+        conf = 0x07;
+        break;
+    default:
+        break;
+    }
+
+#if API_FUNCTION_PWM_COUNTDOWN
+    if (iPwm_GetCountMode(devIndex) == ApiPwm_CountDown)
+    {
+        switch (mode)
+        {
+        case ApiPwm_DownMatchSet:
+            conf = 0x01;
+            break;
+        case ApiPwm_DownMatchClear:
+            conf = 0x02;
+            break;
+        case ApiPwm_DownMatchToggle:
+            conf = 0x03;
+            break;
+        }
+    }
+#endif
+
+    switch (channelIndex)
+    {
+    case 0:
+        TIMER_CHCTL0(base) &= ~((uint32_t)0x000000F0UL);
+        TIMER_CHCTL0(base) |= (((uint32_t)conf) << 4);
+        break;
+    case 1:
+        TIMER_CHCTL0(base) &= ~((uint32_t)0x0000F000UL);
+        TIMER_CHCTL0(base) |= (((uint32_t)conf) << (4 + 8));
+        break;
+    case 2:
+        TIMER_CHCTL0(base) &= ~((uint32_t)0x000000F0UL);
+        TIMER_CHCTL0(base) |= (((uint32_t)conf) << 4);
+        break;
+    case 3:
+        TIMER_CHCTL0(base) &= ~((uint32_t)0x0000F000UL);
+        TIMER_CHCTL0(base) |= (((uint32_t)conf) << (4 + 8));
+        break;
+    }
+}
+
+uint32_t iPwm_GetCompareValue(uint8_t devIndex, uint8_t channelIndex)
+{
+    uint32_t base = Pwms[devIndex].PwmBase;
+
+    uint32_t value = 0;
+    switch (channelIndex)
+    {
+    case 0:
+        value = TIMER_CH0CV(base);
+        break;
+    case 1:
+        value = TIMER_CH1CV(base);
+        break;
+    case 2:
+        value = TIMER_CH2CV(base);
+        break;
+    case 3:
+        value = TIMER_CH3CV(base);
+        break;
+    default:
+        break;
+    }
+
+    return value;
+}
+
+void iPwm_SetCompareValue(uint8_t devIndex, uint8_t channelIndex, uint32_t value)
+{
+    ASSERT_LESS(devIndex, 1);
+    ASSERT_LESS(channelIndex, 3);
+    Pwms[devIndex].CompareValues[channelIndex] = value;
+}
+
+void iPwm_SetCompareValueDelay(uint8_t devIndex, uint8_t channelIndex, uint32_t value)
+{
+    ASSERT_LESS(devIndex, 1);
+    ASSERT_LESS(channelIndex, 3);
+    Pwms[devIndex].CompareValues[channelIndex + 3] = value;
+}
+
+void iPwm_SetCompareGroupValues16(uint8_t devIndex, uint16_t* values)
+{
+    for (int i = 0; i < 6; i++)
+    {
+        Pwms[devIndex].CompareValues[i] = values[i];
+    }
+}
+
+
+void iPwm_SetCompareValueImmediate(uint8_t devIndex, uint8_t channelIndex, uint32_t value)
+{
+    ASSERT_LESS(devIndex, 1);
+    ASSERT_LESS(channelIndex, 4);  
+    uint32_t base = Pwms[devIndex].PwmBase;
+
+    switch (channelIndex)
+    {
+    case 0:
+        TIMER_CH0CV(base) = value;
+        break;
+    case 1:
+        TIMER_CH1CV(base) = value;
+        break;
+    case 2:
+        TIMER_CH2CV(base) = value;
+        break;
+    case 3:
+        TIMER_CH3CV(base) = value;
+        break;
+    default:
+        break;
+    }
+}
+
+int8_t iPwm_GetEnableState(uint8_t devIndex)
+{
+    ASSERT_LESS(devIndex, 1);
+
+    return Pwms[devIndex].OutputEnable;
+}
+
+void iPwm_EnableOutput(uint8_t devIndex)
+{
+    ASSERT_LESS(devIndex, 1);
+
+    uint32_t base = Pwms[devIndex].PwmBase;
+
+    TIMER_CCHP(base) |= (uint32_t)0x00008000UL;
+
+    Pwms[devIndex].OutputEnable = 1;
+}
+
+void iPwm_DisableOutput(uint8_t devIndex)
+{
+    ASSERT_LESS(devIndex, 1);
+
+    uint32_t base = Pwms[devIndex].PwmBase;
+
+    TIMER_CCHP(base) &= ~((uint32_t)0x00008000UL);
+
+    Pwms[devIndex].OutputEnable = 0;
+}
+
+// // int8_t Pwm_GetChannelEnableState(uint8_t devIndex, uint8_t channelIndex)
+// // {
+
+// // }
+
+// // void Pwm_EnableChannelOutput(uint8_t devIndex, uint8_t channelIndex)
+// // {
+// //     ASSERT_LESS(devIndex, 1);
+// //     ASSERT_LESS(channelIndex, 4);
+
+// //     TIM_TypeDef *base = Pwms[devIndex].PwmBase;
+
+// //     if (channelIndex < 64)
+// //     {
+// //         base->CCER |= ((uint32_t)0x01) << (channelIndex * 4);
+// //     }
+// //     else
+// //     {
+// //         base->CCER |= ((uint32_t)0x04) << ((channelIndex - 64) * 4);
+// //     }
+// // }
+
+// // void Pwm_DisableChannelOutput(uint8_t devIndex, uint8_t channelIndex)
+// // {
+// //     ASSERT_LESS(devIndex, 1);
+// //     ASSERT_LESS(channelIndex, 4);
+
+// //     TIM_TypeDef *base = Pwms[devIndex].PwmBase;
+
+// //     if (channelIndex < 64)
+// //     {
+// //         base->CCER &= ~(((uint32_t)0x01) << (channelIndex * 4));
+// //     }
+// //     else
+// //     {
+// //         base->CCER &= ~(((uint32_t)0x04) << ((channelIndex - 64) * 4));
+// //     }
+// // }
+
+//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);
+//}
+//
+#if API_FUNCTION_PWM_SYNC_MULTI_SAMPLING
+
+void iPwm_EnableSyncMultiSampling(uint8_t devIndex)
+{
+    Pwms[devIndex].SyncSamplingEnable = 1;
+}
+
+void iPwm_DisableSyncMultiSampling(uint8_t devIndex)
+{
+    Pwms[devIndex].SyncSamplingEnable        = 0;
+    Pwms[devIndex].SyncSamplingUpTickCount   = 1;
+    Pwms[devIndex].SyncSamplingUpTick[0]     = TIMER_CAR(Pwms[devIndex].PwmBase)>> 1;
+    Pwms[devIndex].SyncSamplingDownTickCount = 1;
+    Pwms[devIndex].SyncSamplingDownTick[0]   = TIMER_CAR(Pwms[devIndex].PwmBase)>> 1;
+}
+
+void iPwm_SyncMultiSamplingCountUp(uint8_t devIndex, uint32_t samplingTicks[], uint8_t samplingCounts)
+{
+    if (Pwms[devIndex].SyncSamplingEnable == 1)
+    {
+        Pwms[devIndex].SyncSamplingUpTickCount = samplingCounts;
+        for (int i = 0; i < samplingCounts; i++)
+        {
+            Pwms[devIndex].SyncSamplingUpTick[i] = samplingTicks[i];
+        }
+    }
+}
+
+void iPwm_SyncMultiSamplingCountDown(uint8_t devIndex, uint32_t samplingTicks[], uint8_t samplingCounts)
+{
+    if (Pwms[devIndex].SyncSamplingEnable == 1)
+    {
+        Pwms[devIndex].SyncSamplingDownTickCount = samplingCounts;
+        for (int i = 0; i < samplingCounts; i++)
+        {
+            Pwms[devIndex].SyncSamplingDownTick[i] = samplingTicks[i];
+        }
+    }
+}
+
+#endif

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

@@ -0,0 +1,36 @@
+#ifndef _API_PWM_LOW_H_
+#define _API_PWM_LOW_H_
+
+#include <stdint.h>
+#include "api_pwm.h"
+#include "api_rt_common.h"
+#include "gd32f30x.h"
+
+typedef struct
+{
+    uint32_t      PwmBase;
+    uint16_t      CompareValues[6];
+    uint16_t      ActiveCompareValues[6];
+    int8_t        OutputEnable;
+    
+#if API_FUNCTION_PWM_SYNC_MULTI_SAMPLING
+    int8_t SyncSamplingEnable;
+    int8_t SyncSamplingUpTickCount;
+    uint16_t SyncSamplingUpTick[4];
+    int8_t SyncSamplingDownTickCount;
+    uint16_t SyncSamplingDownTick[4];
+#endif
+
+} ApiRtPwm_Handle;
+
+extern ApiRtPwm_Handle Pwms[1];
+
+void iRtPwm_Init();
+
+void iRtPwm_ActivateCompareValues(uint8_t devIndex);
+void iRtPwm_UpdateIsr(uint8_t devIndex);
+void iRtPwm_CountZeroIsr(uint8_t devIndex);
+void iRtPwm_CountMaxIsr(uint8_t devIndex);
+void iRtPwm_SyncSamplingAdcIsr(uint8_t devIndex);
+
+#endif

+ 1 - 1
User project/api

@@ -1 +1 @@
-Subproject commit cb277ecfcbc3f19d9312b9eef96c72e91be14ab6
+Subproject commit 5755de440c5f33d322acbf8bf5c9c8afcb4d1df3

+ 12 - 0
WLMCP.ewp

@@ -2829,12 +2829,24 @@
             <file>
                 <name>$PROJ_DIR$\User project\5.Api_rt\api_rt_adc.h</name>
             </file>
+            <file>
+                <name>$PROJ_DIR$\User project\5.Api_rt\api_rt_dbg.c</name>
+            </file>
+            <file>
+                <name>$PROJ_DIR$\User project\5.Api_rt\api_rt_dbg.h</name>
+            </file>
             <file>
                 <name>$PROJ_DIR$\User project\5.Api_rt\api_rt_gpio.c</name>
             </file>
             <file>
                 <name>$PROJ_DIR$\User project\5.Api_rt\api_rt_gpio.h</name>
             </file>
+            <file>
+                <name>$PROJ_DIR$\User project\5.Api_rt\api_rt_pwm.c</name>
+            </file>
+            <file>
+                <name>$PROJ_DIR$\User project\5.Api_rt\api_rt_pwm.h</name>
+            </file>
         </group>
         <group>
             <name>api</name>