Explorar el Código

feat:区分应用层与底层,仍有部分要更改;仿真层api更改

CN\guohui27 hace 2 años
padre
commit
5c8a16ad82
Se han modificado 44 ficheros con 364 adiciones y 1120 borrados
  1. 6 8
      User project/1.FrameLayer/Source/TimeTask_Event.c
  2. 5 0
      User project/1.FrameLayer/Source/main.c
  3. 2 2
      User project/2.MotorDrive/Include/cmdgennew.h
  4. 5 4
      User project/2.MotorDrive/Include/uart_monitor.h
  5. 5 8
      User project/2.MotorDrive/Source/adc.c
  6. 8 7
      User project/2.MotorDrive/Source/alarm.c
  7. 1 1
      User project/2.MotorDrive/Source/packed
  8. 7 7
      User project/2.MotorDrive/Source/spdctrmode.c
  9. 2 2
      User project/2.MotorDrive/Source/uart_monitor.c
  10. 73 0
      User project/3.BasicFunction/Include/can.h
  11. 3 9
      User project/3.BasicFunction/Include/classB.h
  12. 2 10
      User project/3.BasicFunction/Include/i2c_master.h
  13. 2 0
      User project/3.BasicFunction/Include/switchhall.h
  14. 2 2
      User project/3.BasicFunction/Source/AssistCurve.c
  15. 2 1
      User project/3.BasicFunction/Source/InputCapture.c
  16. 0 6
      User project/3.BasicFunction/Source/bikebrake.c
  17. 2 2
      User project/3.BasicFunction/Source/bikespeed.c
  18. 2 1
      User project/3.BasicFunction/Source/bikethrottle.c
  19. 24 0
      User project/3.BasicFunction/Source/can.c
  20. 34 2
      User project/3.BasicFunction/Source/flash_master.c
  21. 4 934
      User project/3.BasicFunction/Source/i2c_master.c
  22. 21 0
      User project/3.BasicFunction/Source/spi_master.c
  23. 10 0
      User project/3.BasicFunction/Source/switchhall.c
  24. 12 16
      User project/3.BasicFunction/Source/torquesensor.c
  25. 4 4
      User project/4.BasicHardwSoftwLayer/1.BasicHardwLayer/Include/hwsetup.h
  26. 1 0
      User project/4.BasicHardwSoftwLayer/2.BasicSoftwLayer/Include/typedefine.h
  27. 1 37
      User project/4.BasicHardwSoftwLayer/2.BasicSoftwLayer/Include/user.h
  28. 1 1
      User project/5.Api_rt/api_rt_i2c.c
  29. 2 1
      tests/sim/sim_board/api_rt/api_rt_adc.c
  30. 1 1
      tests/sim/sim_board/api_rt/api_rt_adc.h
  31. 56 21
      tests/sim/sim_board/api_rt/api_rt_pwm.c
  32. 2 0
      tests/sim/sim_board/api_rt/api_rt_pwm.h
  33. 1 0
      tests/sim/sim_board/include/mcu_instance.h
  34. 7 2
      tests/sim/sim_board/source/board.c
  35. 1 0
      tests/sim/sim_board/source/mcu_instance.c
  36. 20 12
      tests/sim/sim_board/source/sfun_wrapper.c
  37. 24 4
      tests/sim/sim_board/xmake.lua
  38. 0 0
      tests/unit_test/STLmain.h
  39. 2 2
      tests/unit_test/test_brake.cpp
  40. 3 3
      tests/unit_test/test_cursample_calib.cpp
  41. 2 2
      tests/unit_test/test_pwrlim.cpp
  42. 1 1
      tests/unit_test/test_torquesensor.cpp
  43. 1 1
      tests/unit_test/test_user.h
  44. 0 6
      xmake.lua

+ 6 - 8
User project/1.FrameLayer/Source/TimeTask_Event.c

@@ -18,15 +18,13 @@
 #include "FSM_1st.h"
 #include "can.h"
 #include "canAppl.h"
-#include "gd32f30x.h"
 #include "syspar.h"
 #include "torquesensor.h"
 #include "bikelight.h"
 #include "bikethrottle.h"
 #include "user.h"
 #include "STLmain.h"
-#include "api_rt.h"
-//#include "api.h"
+#include "api.h"
 /******************************
  *
  *  Parameter
@@ -119,8 +117,8 @@ void  Event_1ms(void) /* parasoft-suppress METRICS-28 "本项目圈复杂度无
         {
             ass_stCalIn.swDirection = 1;
         }
-        ass_stCalIn.swFlxIqLimit = (SWORD)abs(flx_stCtrlOut.swIqLimPu);
-        ass_stCalIn.swPwrIqLimit = (SWORD)abs(pwr_stPwrLimOut2.swIqLimPu);
+        ass_stCalIn.swFlxIqLimit = (SWORD)ABS(flx_stCtrlOut.swIqLimPu);
+        ass_stCalIn.swPwrIqLimit = (SWORD)ABS(pwr_stPwrLimOut2.swIqLimPu);
         ass_stCalIn.uwbikespeed = bikespeed_stFreGetOut.uwLPFFrequencyPu;
         ass_stCalIn.uwcadancelast = ass_stCalIn.uwcadance;
         ass_stCalIn.uwcadance = cadence_stFreGetOut.uwLPFFrequencyPu;
@@ -159,7 +157,7 @@ void  Event_1ms(void) /* parasoft-suppress METRICS-28 "本项目圈复杂度无
                 /* Initial Value of Torque Assit Output */
                 ass_stCalOut.swAssitCurRef = scm_swIqFdbLpfPu;    
                 ass_pvt_stCurLpf.slY.sw.hi = scm_swIqFdbLpfPu;
-                ass_stCalOut.swTorRefEnd = (SWORD)abs(scm_swIqFdbLpfPu);
+                ass_stCalOut.swTorRefEnd = (SWORD)ABS(scm_swIqFdbLpfPu);
             }
 
             uart_swTorqRefNm = ass_stCalOut.swAssitCurRef;
@@ -308,7 +306,7 @@ void Event_10ms(void)
             	//do nothing
             }
       
-             if(abs((int32_t)uart_slSpdRefRpm) < 300)
+             if(ABS((int32_t)uart_slSpdRefRpm) < 300)
              {
                  uart_slSpdRefRpm = 0;
              }
@@ -387,7 +385,7 @@ void Event_100ms(void)
             Event_pvt_uwBikeSpdRefTargetZ1 = Event_pvt_uwBikeSpdRefTarget;
             
             /* Bike Speed Closed Loop */
-            swIqLowerPu = (SWORD)((flx_stCtrlOut.swIqLimPu < abs(pwr_stPwrLimOut2.swIqLimPu)) ? flx_stCtrlOut.swIqLimPu : abs(pwr_stPwrLimOut2.swIqLimPu));
+            swIqLowerPu = (SWORD)((flx_stCtrlOut.swIqLimPu < ABS(pwr_stPwrLimOut2.swIqLimPu)) ? flx_stCtrlOut.swIqLimPu : ABS(pwr_stPwrLimOut2.swIqLimPu));
             bikespeed_stPIIn.slSpdRefPu = Event_pvt_uwBikeSpdRef;
             bikespeed_stPIIn.slSpdFdkPu = bikespeed_stFreGetOut.uwLPFFrequencyPu; //bikespeed_stFreGetOut.uwFrequencyPu; 
             bikespeed_stPIIn.swIqMaxPu = swIqLowerPu; // ((SLONG)55 << 14)/60;

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

@@ -51,7 +51,9 @@ void AppLoop();
 ****************************************************************/
 int main(void) /* parasoft-suppress GJB5369-4_2_1_10 "mismatch" */
 {
+#ifndef RUN_ARCH_SIM
     SCB->VTOR = 0x08003000;
+
     /* Disable all interrupts */
     DISABLE_IRQ; 
     /* MCU Core and GPIO configuration */
@@ -102,6 +104,7 @@ int main(void) /* parasoft-suppress GJB5369-4_2_1_10 "mismatch" */
     hw_voIWDGInit(FWDGT_PSC_DIV32,1250);//1s  
     /* Enable all interrupts */
     ENABLE_IRQ;
+
     /* Enter infinite loop */
     while (1)
     {
@@ -178,6 +181,8 @@ int main(void) /* parasoft-suppress GJB5369-4_2_1_10 "mismatch" */
     }
 
     return 0;
+
+#endif
 }
 /***************************************************************
  Function: mn_voSoftwareInit;

+ 2 - 2
User project/2.MotorDrive/Include/cmdgennew.h

@@ -21,7 +21,7 @@ Revising History (ECL of this file):
 #ifndef CMDGENNEW_H
 #define CMDGENNEW_H
 /************************************************************************
- Compiler Directives��
+ Compiler Directives
 *************************************************************************/
 #ifdef _CMDGENNEW_C_
 #define CMDGENNEW_EXT
@@ -50,7 +50,7 @@ typedef struct
     SWORD      swOldSpdRefPu;
     SWORD      swNewCmdDir;
     SWORD      swOldCmdDir;
-    FlagStatus blConsVolBrakeFlg;
+    BOOL blConsVolBrakeFlg;
     SWORD      swIntRefPu; // Q14, command pu ,(electrical value)
     SLONG      slIntRefPu;
     SWORD      swMotorDir;

+ 5 - 4
User project/2.MotorDrive/Include/uart_monitor.h

@@ -95,10 +95,9 @@ typedef enum UART_WorkMode
 
 /* ------------------- Callback function type definitions ------------------- */
 
-typedef void (*CB_ERR)(UART_ERR_Type);
+typedef void (*CB_ERROR)(UART_ERR_Type);
 typedef void (*CB_DONE_READ)(UART_ERR_Type, ULONG);
 typedef void (*CB_DONE_WRITE)(UART_ERR_Type);
-
 /* ------------------------- HAL configuration type ------------------------- */
 
 typedef struct UART_CONFIG
@@ -120,7 +119,7 @@ typedef enum MsgType
     UART_SEND_PARA   = 2  // send out Parameter value
 } UART_MsgType;
 
-typedef struct MSG
+typedef struct Msg
 {
     UART_MsgType enType;
     UBYTE       *pBuf;
@@ -192,7 +191,7 @@ _UART_MONITOR_EXT UART_MsgQNodeType         UART_stMsgQueue[MSG_QUEUE_SIZE]; //
 _UART_MONITOR_EXT UART_MsgQType             UART_stMsgQ       = UART_MsgQ_CONFIG_DEFAULT;
 _UART_MONITOR_EXT volatile UART_CONFIG_Type UART_Config       = UART_DRIVER_CONFIG_DEFAULT; // define configure object and assign default value.
 _UART_MONITOR_EXT UWORD                     UART_uwTimeoutCtr = 0;
-_UART_MONITOR_EXT volatile CB_ERR           UART_fpError      = NULL;
+_UART_MONITOR_EXT volatile CB_ERROR           UART_fpError      = NULL;
 UBYTE                                       uart_ReadBuffer2[RX_BUFFER_SIZE]; // cancel static just for debug
 static UBYTE                               *uart_pReadBuffer2 = uart_ReadBuffer2;
 UWORD volatile uart_DataNum2                                  = 0;
@@ -251,6 +250,8 @@ void CBError_Swan(UART_ERR_Type _err);
 void uart_Write_Poll2(ULONG _NumBytes, const UBYTE *_Buffer);
 void HAL_CancelRead_Poll2(void);
 BOOL SwanDecoder(UBYTE *ptubBuffer, UWORD Num, UBYTE **ptptubACKbuffer, UWORD *ACKnum);
+
+
 #endif
 /************************************************************************
  Copyright (c) 2019 Welling Motor Technology(Shanghai) Co. Ltd.

+ 5 - 8
User project/2.MotorDrive/Source/adc.c

@@ -31,9 +31,6 @@ Revising History (ECL of this file):
 #include "api.h"
 #include "api_rt.h"
 
-#ifdef RUN_ARCH_SIM
-#include "test_user.h"
-#endif
 /************************************************************************
  Constant Table:
 *************************************************************************/
@@ -282,9 +279,9 @@ void adc_voSRCalibration(ADC_COF *cof, const ADC_UP_OUT *up_out, ADC_DOWN_OUT *d
 
          if(scm_uwSpdFbkLpfAbsPu < 2500)
          {
-            adc_pvt_ulIaAbsPu = abs(down_out->slSampIaPu);
-            adc_pvt_ulIbAbsPu = abs(down_out->slSampIbPu);
-            adc_pvt_ulIcAbsPu = abs(down_out->slSampIcPu);
+            adc_pvt_ulIaAbsPu = ABS(down_out->slSampIaPu);
+            adc_pvt_ulIbAbsPu = ABS(down_out->slSampIbPu);
+            adc_pvt_ulIcAbsPu = ABS(down_out->slSampIcPu);
             
             adc_pvt_ulIPeakPu = adc_pvt_ulIaAbsPu > adc_pvt_ulIbAbsPu ? adc_pvt_ulIaAbsPu : adc_pvt_ulIbAbsPu;
             down_out->ulISamplePeakPu = adc_pvt_ulIcAbsPu > adc_pvt_ulIPeakPu ? adc_pvt_ulIcAbsPu : adc_pvt_ulIPeakPu;
@@ -311,11 +308,11 @@ void adc_voSRCalibration(ADC_COF *cof, const ADC_UP_OUT *up_out, ADC_DOWN_OUT *d
          {
             if(adc_pvt_blCalGainFlg)
             {
-                if(adc_pvt_slRdsonReg != 0 && abs(adc_pvt_slRdsonReg) < ulOverflowCurPu)
+                if(adc_pvt_slRdsonReg != 0 && ABS(adc_pvt_slRdsonReg) < ulOverflowCurPu)
                 {
                     adc_pvt_ulGainTemp1 = (SLONG)((SLONG)adc_pvt_swSingleReg << 10) / (SLONG)adc_pvt_slRdsonReg;
                 }
-                else if(abs(adc_pvt_slRdsonReg) >= ulOverflowCurPu)
+                else if(ABS(adc_pvt_slRdsonReg) >= ulOverflowCurPu)
                 {
                     adc_pvt_ulGainTemp1 = 780;     ///< Rdson电流采样削顶时不再校准电流,强制输出为119A防止溢出,尽快报出过流故障
                     adc_pvt_stRdsonCoefLpf.slY.sw.hi = (SWORD)adc_pvt_ulGainTemp1;

+ 8 - 7
User project/2.MotorDrive/Source/alarm.c

@@ -30,6 +30,7 @@ Included File:
 #include "FSM_1st.h"
 #include "FSM_2nd.h"
 #include "spdctrFSM.h"
+#include "api.h"
 /************************************************************************
 Private Variables:
 ************************************************************************/
@@ -288,7 +289,7 @@ void alm_voDetecTBC(const ALM_IN *in, const ALM_DETECTBC_COF *coef) /* parasoft-
     /*=======================================================================
     IPM fault
     =======================================================================*/
-    if (MCU_BRKIN_FLG != 0)
+    if (iPwm_GetBreakState(0) != 0)
     {
         alm_unAction.bit.ThrPhsShrtFrc = 1;
         alm_unCode.bit.IPMFlt = 1;
@@ -402,7 +403,7 @@ void alm_voDetecTBC(const ALM_IN *in, const ALM_DETECTBC_COF *coef) /* parasoft-
         =======================================================================*/
         alm_pvt_swSpdRefAbsPu = (SWORD)in->uwSpdRefAbsPu; // Q15
 
-        alm_pvt_swIqRefAbsPu = (SWORD)abs(in->swIqRefPu);
+        alm_pvt_swIqRefAbsPu = (SWORD)ABS(in->swIqRefPu);
         alm_pvt_slIqRefLpfAbsPu = (SLONG)0x0010 * (alm_pvt_swIqRefAbsPu - alm_pvt_swIqRefLpfAbsPu) + alm_pvt_slIqRefLpfAbsPu; // Q30
         alm_pvt_swIqRefLpfAbsPu = (SWORD)(alm_pvt_slIqRefLpfAbsPu >> 15);
 
@@ -813,11 +814,11 @@ void alm_voStopTBC(const ALM_IN *in, const ALM_STOPTBC_COF *coef)
 {
     if (alm_unAction.bit.ThrPhsShrtFrc != 0)
     {
-        if (MCU_BRKIN_FLG != 0)
+        if (iPwm_GetBreakState(0) != 0)
         {
             hw_voPWMOff();     /* PWM off */
-            MCU_BRKIN_FLG_CLR; /* Clear TIME0 break flag */
-            MCU_POEN_FLG_EN;   /* Enable TIME0 Channel outputs */
+            iPwm_ClearBreak(0); /* Clear TIME0 break flag */
+            iPwm_EnableOutput(0);   /* Enable TIME0 Channel outputs */
 
             alm_stStopCt.ulThrPhsShrtFrc++;
             if (alm_stStopCt.ulThrPhsShrtFrc > coef->ulThrPhsShrtFrcValCt) // 200ms
@@ -1027,8 +1028,8 @@ void alm_voReset(const ALM_IN *in, const ALM_RESET_COF *coef) /* parasoft-suppre
         /* IPM fault */
         if ((alm_unCode.bit.IPMFlt == TRUE) && (alm_stRecCt.ulIPMOC >= coef->ulIPMOcRecValCt))
         {
-            MCU_BRKIN_FLG_CLR; /* Clear TIME0 break flag */
-            MCU_POEN_FLG_EN;   /* Enable TIME0 Channel outputs */
+            iPwm_ClearBreak(0); /* Clear TIME0 break flag */
+            iPwm_EnableOutput(0);   /* Enable TIME0 Channel outputs */
 
             alm_unCode.bit.IPMFlt = 0;
         }

+ 1 - 1
User project/2.MotorDrive/Source/packed

@@ -1 +1 @@
-Subproject commit 723ff2a3e6931847527419a7bf116182ef7c380c
+Subproject commit 658c0be0c2234c4c1a8ede5edb87e178ed843552

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

@@ -150,13 +150,13 @@ void scm_voSpdCtrMdCoef(void)
     SLONG slLqPu = 0;
     ULONG ulAccel100rpmpsPu = USER_MOTOR_100RPMPS2PU_Q29;
 
-    if (abs(scm_swIqRefPu) < mn_swIqTurn1Pu)
+    if (ABS(scm_swIqRefPu) < mn_swIqTurn1Pu)
     {
         scm_uwLqPu = cof_uwLqPu;
     }
     else
     {
-    	slLqPu = mn_slLqTurn1Pu + (((SLONG)abs(scm_swIqRefPu) - mn_swIqTurn1Pu) * mn_swKLqSat >> 10); // Q10
+    	slLqPu = mn_slLqTurn1Pu + (((SLONG)ABS(scm_swIqRefPu) - mn_swIqTurn1Pu) * mn_swKLqSat >> 10); // Q10
         if (slLqPu < cof_uwLqMinPu)
         {
             scm_uwLqPu = cof_uwLqMinPu;
@@ -320,7 +320,7 @@ void scm_voSpdCtrMdCoef(void)
         pwr_voPwrLimCof(&pwr_stPwrLimCofIn, &pwr_stPwrLimCof);
 
         /*Accelaration&Decelaration limit*/
-        if (abs(scm_swSpdRefPu) < USER_MOTOR_300RPM2PU)
+        if (ABS(scm_swSpdRefPu) < USER_MOTOR_300RPM2PU)
         {
             cmd_stCmdCoefIn.ulAccelPu =  ulAccel100rpmpsPu; // Q29
         }
@@ -379,7 +379,7 @@ void scm_voSpdCtrMdTbs(void)
     }
 
     /* Speed feedback Absolute */
-    scm_uwSpdFbkLpfAbsPu = (UWORD)abs(scm_stSpdFbkLpf.slY.sw.hi);
+    scm_uwSpdFbkLpfAbsPu = (UWORD)ABS(scm_stSpdFbkLpf.slY.sw.hi);
 
     /*============================================================
                 Speed command generator to generate speed ramp
@@ -448,7 +448,7 @@ void scm_voSpdCtrMdTbs(void)
     }
     else
     {
-       swIqLowerPu = (SWORD)((flx_stCtrlOut.swIqLimPu < abs(pwr_stPwrLimOut2.swIqLimPu)) ? flx_stCtrlOut.swIqLimPu : abs(pwr_stPwrLimOut2.swIqLimPu));
+       swIqLowerPu = (SWORD)((flx_stCtrlOut.swIqLimPu < ABS(pwr_stPwrLimOut2.swIqLimPu)) ? flx_stCtrlOut.swIqLimPu : ABS(pwr_stPwrLimOut2.swIqLimPu));
     }
 
     if (scm_swRotateDir > 0)
@@ -523,7 +523,7 @@ void  scm_voTorqCtrMdTbs(void)
     	//do noting
     }
     /* Speed feedback Absolute */
-    scm_uwSpdFbkLpfAbsPu = (UWORD)abs(scm_stSpdFbkLpf.slY.sw.hi);
+    scm_uwSpdFbkLpfAbsPu = (UWORD)ABS(scm_stSpdFbkLpf.slY.sw.hi);
 
     //    /*============================================================
     //              Speed command generator to generate speed ramp
@@ -540,7 +540,7 @@ void  scm_voTorqCtrMdTbs(void)
     /*=======================================================================
                                  Speed PI Controller
         =======================================================================*/
-    swIqLowerPu = (SWORD)((flx_stCtrlOut.swIqLimPu < abs(pwr_stPwrLimOut2.swIqLimPu)) ? flx_stCtrlOut.swIqLimPu : abs(pwr_stPwrLimOut2.swIqLimPu));
+    swIqLowerPu = (SWORD)((flx_stCtrlOut.swIqLimPu < ABS(pwr_stPwrLimOut2.swIqLimPu)) ? flx_stCtrlOut.swIqLimPu : ABS(pwr_stPwrLimOut2.swIqLimPu));
     /* Torque observe */
     if (scm_swRotateDir > 0)
     {

+ 2 - 2
User project/2.MotorDrive/Source/uart_monitor.c

@@ -92,7 +92,7 @@ UWORD* UART_pParaTable[PARA_TABLE_SIZE]=
 void uart_voMonitorInit(void)
 {
     UBYTE i = 0;
-    UART_fpError = (CB_ERR)CBError_monitor;      // Store CallBack function pointers  
+    UART_fpError = (CB_ERROR)CBError_monitor;      // Store CallBack function pointers  
     uart_Read_Poll2(RX_BUFFER_SIZE,uart_pReadBuffer,uart_voCBDoneRead);
     //--- insert stack initialization ---
     for(i=0;i<MSG_QUEUE_SIZE;i++) 
@@ -664,7 +664,7 @@ void ReadFrame_Poll2(void)
 ****************************************************************/
 void uart_voSwanInit(void)
 {   	
-    UART_fpError = (CB_ERR)CBError_Swan;    	           
+    UART_fpError = (CB_ERROR)CBError_Swan;    	           
     UART_Config = UART_stConfigLEN;           	           
     uart_Read_Poll2(RX_BUFFER_SIZE,uart_pReadBuffer2,CBDoneRead2);	// use poll method for littleswan receiving.
 }

+ 73 - 0
User project/3.BasicFunction/Include/can.h

@@ -21,8 +21,78 @@ Revising History (ECL of this file):
 #ifndef CAN_H
 #define CAN_H
 
+#ifdef RUN_ARCH_SIM
+#include "typedefine.h"
+
+#define ID_MC_BC     0x710
+#define ID_MC_TO_BMS 0x712
+#define ID_MC_TO_PBU 0x713
+#define ID_MC_TO_HMI 0x714
+#define ID_MC_TO_CDL 0x715
+
+#define ID_BMS_BC     0x720
+#define ID_BMS_TO_MC  0x721
+#define ID_BMS_TO_PBU 0x723
+#define ID_BMS_TO_HMI 0x724
+#define ID_BMS_TO_CDL 0x725
+
+#define ID_PBU_BC     0x730
+#define ID_PBU_TO_MC  0x731
+#define ID_PBU_TO_BMS 0x732
+#define ID_PBU_TO_CDL 0x735
+
+#define ID_HMI_BC     0x740
+#define ID_HMI_TO_MC  0x741
+#define ID_HMI_TO_BMS 0x742
+#define ID_HMI_TO_PBU 0x743
+#define ID_HMI_TO_CDL 0x745
+
+#define ID_CDL_BC     0x750
+#define ID_CDL_TO_MC  0x751
+#define ID_CDL_TO_BMS 0x752
+#define ID_CDL_TO_PBU 0x753
+#define ID_CDL_TO_HMI 0x754
+
+#define ID_TO_MC_FILTER (UWORD)0x701
+#define ID_TO_MC_MASK   (UWORD)0x70F
+
+#define ID_BC_FILTER (UWORD)0x700
+#define ID_BC_MASK   (UWORD)0x70F
+
+#define FRAME_BEGIN1 0x55
+#define FRAME_BEGIN2 0xAA
+#define FRAME_END    0xF0
+
+#define MODE_READ   0x11
+#define MODE_WRITE  0x16
+#define MODE_REPORT 0x0C
+typedef struct
+{
+    UWORD ucBufID;       // ID
+    UBYTE  ucBufSize;     //缓冲区数组:缓冲区大小
+    UBYTE  ucBufWrInde;   //缓冲区数组:写索引
+    UBYTE  ucBufRdInde;   //缓冲区数组:读索引
+    UBYTE  ucBufCnt;      //缓冲区数组:数据计数
+    UBYTE  ucBufOvf;      //缓冲区数组:溢出标志,注意清零
+    UBYTE *pcBufAddr;     //缓冲区数组:首地址指针变量
+    ULONG DelayTimeCnt;  //超时计时
+    BOOL     IsWaitRX_Flag; //超时等待数据标志
+} CAN_Buf_TypeDef;
+void     CAN_Rx_ISR(CAN_Buf_TypeDef *ptCANRx, UBYTE ucLength);
+void     CAN_SendData(UWORD ID, const UBYTE Data[], UWORD Length);
+void     SendData(UWORD ID, UBYTE Mode, UWORD Command, const UBYTE *Data);
+UBYTE    cd_ReadChar(const CAN_Buf_TypeDef *ptCANRx, UBYTE ucNum);
+void     cd_DelChar(CAN_Buf_TypeDef *ptCANRx, UBYTE ucNum);
+void     CAN_RxData_Process(CAN_Buf_TypeDef *ptCANRx, UWORD TimeOutCnt);
+void     CanRx_Process(void);
+void     DataProcess(UWORD ID, UBYTE Mode, UWORD Cmd, UBYTE Data[]);
+ULONG    CRC32_Calculate(const UBYTE pData[], UWORD Length);
+
+#else
+
 #include "typedefine.h"
 #include "gd32f30x.h"
+
 /************************************************************************
  Compiler Directives (N/A)
 *************************************************************************/
@@ -164,11 +234,14 @@ CAN_EXT void     DataProcess(UWORD ID, UBYTE Mode, UWORD Cmd, UBYTE Data[]);
 CAN_EXT ULONG    CRC32_Calculate(const UBYTE pData[], UWORD Length);
 #endif
 
+
 /************************************************************************
  Flag Define (N/A)
 *************************************************************************/
 #endif
 
+#endif
+
 /************************************************************************
  Copyright (c) 2022 Welling Motor Technology(Shanghai) Co. Ltd.
  All rights reserved.

+ 3 - 9
User project/3.BasicFunction/Include/classB.h

@@ -20,6 +20,7 @@ WLBDM_M0_SR_20170814-new FSM1.1, by cyf, create this file;
 *************************************************************************/
 #ifndef CLASSB_H
 #define CLASSB_H
+
 #include "typedefine.h"
 #include <stdint.h>
 /************************************************************************
@@ -94,6 +95,7 @@ typedef union
  Exported Variables
 *************************************************************************/
 #ifdef _CLASSB_C_
+#pragma warning(disable:4068)
 #pragma location = ".CRCsection"
 CLASSB_EXT const uint32_t sect = 0xFFFFFFFFU;
 CLASSB_EXT UBYTE          clas_blclassBErrFlg = 0;
@@ -167,13 +169,8 @@ CLASSB_EXT ULONG calss_ulRamCnt = 0;
 CLASSB_EXT BOOL  clas_blIrqFlg = FALSE;
 CLASSB_EXT ULONG  clas_flashCrc = 0;
 
-
-
-
-
-
-
 #else
+#pragma warning(disable:4068)
 #pragma location = ".CRCsection"
 CLASSB_EXT const uint32_t sect;
 CLASSB_EXT UBYTE          clas_blclassBErrFlg;
@@ -247,9 +244,6 @@ CLASSB_EXT ULONG calss_ulRamCnt;
 CLASSB_EXT BOOL  clas_blIrqFlg;
 CLASSB_EXT ULONG  clas_flashCrc;
 
-
-
-
 #endif
 /************************************************************************
  RAM ALLOCATION (N/A)

+ 2 - 10
User project/3.BasicFunction/Include/i2c_master.h

@@ -981,9 +981,6 @@ _I2C_MASTER_EXT BOOL  I2C_EE_ComuFltFlg;
  Exported Function Call Prototypes
 *************************************************************************/
 #ifdef _I2C_MASTER_C_
-_I2C_MASTER_EXT void i2c_voReadTest(void);
-_I2C_MASTER_EXT void i2c_voWriteTest(void);
-
 _I2C_MASTER_EXT void  i2c_voWaitEEReady(UBYTE SlaveAddr);
 _I2C_MASTER_EXT void  i2c_voTXCoef(I2C_TX_COF *out);
 _I2C_MASTER_EXT void  i2c_voInfoWriteBuffer(void);
@@ -993,16 +990,12 @@ _I2C_MASTER_EXT void  i2c_voHistoryWriteBuffer(void);
 _I2C_MASTER_EXT void  i2c_voDefaultWrite2EE(I2C_TX_COF *coef, I2C_TX_OUT *out);
 _I2C_MASTER_EXT void  i2c_voInfoWrite2EE(const I2C_TX_COF *coef, I2C_TX_OUT *out);
 _I2C_MASTER_EXT void  i2c_voHistoryWrite2EE(const I2C_TX_COF *coef, I2C_TX_OUT *out);
-_I2C_MASTER_EXT void  i2c_voPageWrite2EE(const UBYTE *pBuffer, UBYTE SlaveAddr, UBYTE WriteAddr, UBYTE NBytesToWrite);
 _I2C_MASTER_EXT void  i2c_voSysparaReadFromEE(I2C_RXCRC_OUT *out);
 _I2C_MASTER_EXT UWORD i2c_uwCRCCcitt(const UBYTE *ptubBuf, UWORD length);
 _I2C_MASTER_EXT void  i2c_voReadBufferCRC(I2C_RXCRC_OUT *out);
 _I2C_MASTER_EXT void  i2c_voGetValueFrmBuffer(const I2C_RXCRC_OUT *out);
-_I2C_MASTER_EXT void i2c_bus_reset(void);
-#else
-_I2C_MASTER_EXT void i2c_voReadTest(void);
-_I2C_MASTER_EXT void i2c_voWriteTest(void);
 
+#else
 _I2C_MASTER_EXT void  i2c_voWaitEEReady(UBYTE SlaveAddr);
 _I2C_MASTER_EXT void  i2c_voTXCoef(I2C_TX_COF *out);
 _I2C_MASTER_EXT void  i2c_voDefaultWriteBuffer(void);
@@ -1010,12 +1003,11 @@ _I2C_MASTER_EXT void  i2c_voParaWriteBuffer(void);
 _I2C_MASTER_EXT void  i2c_voHistoryWriteBuffer(void);
 _I2C_MASTER_EXT void  i2c_voInfoWrite2EE(const I2C_TX_COF *coef, I2C_TX_OUT *out);
 _I2C_MASTER_EXT void  i2c_voHistoryWrite2EE(const I2C_TX_COF *coef, I2C_TX_OUT *out);
-_I2C_MASTER_EXT void  i2c_voPageWrite2EE(const UBYTE *pBuffer, UBYTE SlaveAddr, UBYTE WriteAddr, UBYTE NBytesToWrite);
 _I2C_MASTER_EXT void  i2c_voSysparaReadFromEE(I2C_RXCRC_OUT *out);
 _I2C_MASTER_EXT UWORD i2c_uwCRCCcitt(const UBYTE *ptubBuf, UWORD length);
 _I2C_MASTER_EXT void  i2c_voReadBufferCRC(I2C_RXCRC_OUT *out);
 _I2C_MASTER_EXT void  i2c_voGetValueFrmBuffer(const I2C_RXCRC_OUT *out);
-_I2C_MASTER_EXT void i2c_bus_reset(void);
+
 #endif
 
 /************************************************************************

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

@@ -14,6 +14,8 @@
 *************************************************************************/
 #ifndef SWITCHHALL_H
 #define SWITCHHALL_H
+
+#include "typedefine.h"
 /************************************************************************
  Compiler Directives (N/A)
 *************************************************************************/

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

@@ -592,7 +592,7 @@ static void AssitCuvApplPerVolt(void) /* parasoft-suppress METRICS-28 "本项目
 //            {
 //                ass_stCalCoef.StartFlag = 1;
 //            }  
-            if(abs(scm_swIqRefPu- scm_swIqFdbLpfPu) > 200)
+            if(ABS(scm_swIqRefPu- scm_swIqFdbLpfPu) > 200)
             {
                 ass_pvt_swVoltCnt++;  
             }    
@@ -928,7 +928,7 @@ static void AssitCuvApplPerVolt(void) /* parasoft-suppress METRICS-28 "本项目
 //            {
 //                 /* Initial value */
 //                 ass_stTorqPIOut.slIRefPu = 0;
-//                 swCurSwitch = abs(ass_stCalOut.swTorRefTarget); //abs(ass_stCalOut.swAssitCurRef);
+//                 swCurSwitch = ABS(ass_stCalOut.swTorRefTarget); //ABS(ass_stCalOut.swAssitCurRef);
 //                 ass_stCalOut.blTorqPIFlg = TRUE;
 //            }
 // 

+ 2 - 1
User project/3.BasicFunction/Source/InputCapture.c

@@ -1,8 +1,9 @@
 #include "InputCapture.h"
 #include "bikespeed.h"
 #include "Cadence.h"
-#include "Api_rt.h"
 #include "FSM_1st.h"
+#include "api.h"
+#include "board_config.h"
 
 void IC_CountMaxISR(void)
 {

+ 0 - 6
User project/3.BasicFunction/Source/bikebrake.c

@@ -13,13 +13,9 @@
  Beginning of File, do not put anything above here except notes
  Compiler Directives:
 *************************************************************************/
-
 #include "bikebrake.h"
 #include "api_rt.h"
 
-#ifdef RUN_ARCH_SIM
-#include "test_user.h"
-#endif
 /******************************
  *
  *  Parameter
@@ -53,8 +49,6 @@ void bikebrake_voBikeBrakeInit(void)
 ****************************************************************/
 void bikebrake_voBikeBrakeDetect(void)
 {
-  
-  
     if (bikebrake_stBikeBrakeOut.blBikeBrakeOnFlg == FALSE && (iGpio_Read(HW_GPIO_BREAK_PIN) == 0))
     {
         bikebrake_stBikeBrakeOut.uwBikeBrakeCnt++;

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

@@ -447,7 +447,7 @@ void bikespeed_voPI(const BIKESPDPI_IN *in, BIKESPDPI_OUT *out)
     }
 
     bikespeed_stPICof.uwKpPu = 25000; 
-    if(((SWORD)abs(out->slErrorZ1) - (SWORD)abs(slSpdErrPu)) > 20)   //Fast Approach
+    if(((SWORD)ABS(out->slErrorZ1) - (SWORD)ABS(slSpdErrPu)) > 20)   //Fast Approach
     {
         bikespeed_stPICof.uwKiPu = 0; 
         
@@ -466,7 +466,7 @@ void bikespeed_voPI(const BIKESPDPI_IN *in, BIKESPDPI_OUT *out)
         	//do noting
         }
     }
-    else if(((SWORD)abs(out->slErrorZ1) - (SWORD)abs(slSpdErrPu)) > 0) //Fast Approach
+    else if(((SWORD)ABS(out->slErrorZ1) - (SWORD)ABS(slSpdErrPu)) > 0) //Fast Approach
     {
         if(in->slSpdFdkPu<500)
         {

+ 2 - 1
User project/3.BasicFunction/Source/bikethrottle.c

@@ -17,7 +17,8 @@
 #include "typedefine.h"
 #include "mathtool.h"
 #include "bikethrottle.h"
-#include "api_rt.h"
+#include "api.h"
+#include "board_config.h"
 /******************************
  *
  * static Parameter

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

@@ -24,6 +24,28 @@ Revising History (ECL of this file):
 /************************************************************************
  Included File:
 *************************************************************************/
+#ifdef RUN_ARCH_SIM
+#include "can.h"
+
+void     CAN_Rx_ISR(CAN_Buf_TypeDef *ptCANRx, UBYTE ucLength)
+{}
+void     CAN_SendData(UWORD ID, const UBYTE Data[], UWORD Length)
+{}
+void     SendData(UWORD ID, UBYTE Mode, UWORD Command, const UBYTE *Data)
+{}
+UBYTE    cd_ReadChar(const CAN_Buf_TypeDef *ptCANRx, UBYTE ucNum)
+{}
+void     cd_DelChar(CAN_Buf_TypeDef *ptCANRx, UBYTE ucNum)
+{}
+void     CAN_RxData_Process(CAN_Buf_TypeDef *ptCANRx, UWORD TimeOutCnt)
+{}
+void     CanRx_Process(void);
+void     DataProcess(UWORD ID, UBYTE Mode, UWORD Cmd, UBYTE Data[])
+{}
+ULONG    CRC32_Calculate(const UBYTE pData[], UWORD Length)
+{}
+
+#else
 #include "stdlib.h"
 #include "string.h"
 #include "gd32f30x.h"
@@ -935,6 +957,8 @@ ULONG CRC32_Calculate(const UBYTE pData[], UWORD Length)
     return nReg;
 }
 
+#endif
+
 /************************************************************************
  Copyright (c) 2018 Welling Motor Technology(Shanghai) Co. Ltd.
  All rights reserved.

+ 34 - 2
User project/3.BasicFunction/Source/flash_master.c

@@ -17,11 +17,28 @@
  Beginning of File, do not put anything above here except notes
  Compiler Directives:
 *************************************************************************/
-
 #ifndef _FLASH_MASTER_C
 #define _FLASH_MASTER_C_
 #endif
 
+#ifdef RUN_ARCH_SIM
+#include "flash_master.h"
+void flash_voWrite(void)
+{}
+void flash_voRead(void)
+{}
+void flash_voParaInit(void)
+{}
+void flash_voErrorWrite(void)
+{}
+void flash_voErrorRead(void)
+{}
+void FLASH_voWriteMoreData(ULONG startAddress, const UWORD writeData[], UWORD countToWrite)
+{}
+void FLASH_voReadMoreData(ULONG startAddress, UWORD readData[], UWORD countToRead)
+{}
+
+#else
 /************************************************************************
  Included File
 *************************************************************************/
@@ -121,4 +138,19 @@ void flash_voErrorRead(void)
     {
        que_voInit(&que_stFlashErrorLog);
     }   
-}
+}
+
+/************************************************************************
+ Copyright (c) 2018 Welling Motor Technology(Shanghai) Co., Ltd.
+ All rights reserved.
+*************************************************************************/
+#ifdef _FLASH_MASTER_C
+#undef _FLASH_MASTER_C_
+#endif
+
+#endif
+
+/*************************************************************************
+ End of this File (EOF)
+ Do not put anything after this part!
+*************************************************************************/

+ 4 - 934
User project/3.BasicFunction/Source/i2c_master.c

@@ -25,122 +25,11 @@
  Included File
 *************************************************************************/
 #include "i2c_master.h"
-#include "api_rt.h"
-#include "gd32f30x_dma.h"
 #include "api.h"
 /*************************************************************************
  Exported Functions (N/A)
 *************************************************************************/
 
-/*************************************************************************
- Function:
- Description:
- Call by:
- Input Variables:
- Output/Return Variables:
- Subroutine Call:
- Reference:
-*************************************************************************/
-static UWORD i2c_pvt_uwWaitCnt = 0, i2c_pvt_uwWaitCnt2 = 0;
-void  i2c_voWaitEEReady(UBYTE SlaveAddr)
-{
-    /* Wait at least 5ms */
-    while (i2c_pvt_uwWaitCnt2 < 2)
-    {
-        i2c_pvt_uwWaitCnt++;
-        if (i2c_pvt_uwWaitCnt == 10000)
-        {
-            i2c_pvt_uwWaitCnt2++;
-            i2c_pvt_uwWaitCnt = 0;
-        }
-    }
-    i2c_pvt_uwWaitCnt2 = 0;
-  
-//    /* GD MCU has ACK detect function */    
-//    ULONG ulTimeCnt = 0;
-//    UWORD uwTimeoutNum = 0, uwTimeoutNum2;
-//    
-//    while(!I2C_EE_ComuFltFlg)
-//    {
-//        /* write to EEPROM enable*/
-//        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)
-//        {
-//           uwTimeoutNum ++;
-//        }
-//        ulTimeCnt = 0;
-//        /* send a start condition to I2C bus */
-//        i2c_start_on_bus(I2C0);
-//        /* wait until SBSEND bit is set */
-//        while((!i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) && (++ulTimeCnt < I2C_SHORT_TIMEOUT));
-//        if(ulTimeCnt == I2C_SHORT_TIMEOUT)
-//        {
-//           uwTimeoutNum ++;
-//        }
-//        ulTimeCnt = 0;
-//        /* send slave address to I2C bus */
-//        i2c_master_addressing(I2C0, SlaveAddr, I2C_TRANSMITTER);
-//        /* wait until ADDSEND bit is set */
-//        while((!((i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) || (i2c_flag_get(I2C0, I2C_FLAG_AERR))))&& (++ulTimeCnt < I2C_SHORT_TIMEOUT));
-//        if(ulTimeCnt < I2C_SHORT_TIMEOUT)
-//        {
-//            if(i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) 
-//            {
-//                ulTimeCnt = 0;
-//              /* clear the bit of I2C_FLAG_ADDSEND */
-//                i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
-//                /* send a stop condition to I2C bus */
-//                i2c_stop_on_bus(I2C0);
-//                /* wait until stop condition generate */ 
-//                while((I2C_CTL0(I2C0)&I2C_CTL0_STOP) && (++ulTimeCnt < I2C_SHORT_TIMEOUT));
-//                if(ulTimeCnt == I2C_SHORT_TIMEOUT)
-//                {
-//                   uwTimeoutNum ++;
-//                }
-//                ulTimeCnt = 0;
-//                /* exit the function */
-//                I2C_EE_ComuFltFlg = FALSE;
-//                break;
-//            } 
-//            else 
-//            {
-//                /* clear the bit of I2C_FLAG_AERR */
-//                i2c_flag_clear(I2C0, I2C_FLAG_AERR);
-//                /* send a stop condition to I2C bus */
-//                i2c_stop_on_bus(I2C0);    
-//                /* wait until stop condition generate */ 
-//                while((I2C_CTL0(I2C0)&I2C_CTL0_STOP) && (++ulTimeCnt < I2C_SHORT_TIMEOUT));
-//                if(ulTimeCnt == I2C_SHORT_TIMEOUT)
-//                {
-//                   uwTimeoutNum ++;
-//                }
-//                ulTimeCnt = 0;
-//            }
-//        }
-//        else
-//        {
-//             ulTimeCnt = 0;
-//             uwTimeoutNum2 ++;
-//             if(uwTimeoutNum2 > 4)
-//             {
-//                I2C_EE_ComuFltFlg = TRUE;
-//                break;
-//             }
-//        }
-//        
-//        /* I2C communication timeout fault */
-//        if(uwTimeoutNum > 3 )
-//        {
-//           I2C_EE_ComuFltFlg = TRUE;
-//        }        
-//    }
-//    /* write to EEPROM disable */
-//    iGpio_Write(HW_GPIO_I2CWP_PIN,ApiGpio_LowLevel);
-    
-}
-
 /*************************************************************************
  Function:
  Description:
@@ -317,332 +206,6 @@ void i2c_voHistoryWriteBuffer(void)
     I2C_ubHistoyWriteBuffer[I2C_HISTORY_PARA_CRC_ARDDR] = (UBYTE)(i2c_uwCRCCcitt(I2C_pHistoryWriteBuffer, I2C_HISTORY_PARA_N_BYTES) >> 8);
     I2C_ubHistoyWriteBuffer[I2C_HISTORY_PARA_CRC_ARDDR + 1] = (UBYTE)(i2c_uwCRCCcitt(I2C_pHistoryWriteBuffer, I2C_HISTORY_PARA_N_BYTES));
 }
-/*************************************************************************
- Function:
- Description:
- Call by:
- Input Variables:
- Output/Return Variables:
- Subroutine Call:
- Reference:
-*************************************************************************/
-//static void i2c_voByteWrite2EE(UBYTE SlaveAddr, UBYTE WriteAddr, UBYTE Data) /* parasoft-suppress METRICS-28 "本项目圈复杂度无法更改,后续避免" */
-//{
-//    ULONG ulTimeCnt = 0;
-//    UWORD uwTimeoutNum = 0;
-//
-//    if(!I2C_EE_ComuFltFlg)
-//    {
-//        /* write to EEPROM enable*/
-//        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))
-//    	{
-//        	ulTimeCnt++;
-//    	}
-//        if(ulTimeCnt == I2C_SHORT_TIMEOUT)
-//        {
-//           uwTimeoutNum ++;
-//        }
-//        ulTimeCnt = 0;
-//        /* send a start condition to I2C bus */
-//        i2c_start_on_bus(I2C0);
-//        /* wait until SBSEND bit is set */
-//        while((i2c_flag_get(I2C0, I2C_FLAG_SBSEND) == 0) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
-//    	{
-//        	ulTimeCnt++;
-//    	}
-//        if(ulTimeCnt == I2C_SHORT_TIMEOUT)
-//        {
-//           uwTimeoutNum ++;
-//        }
-//        ulTimeCnt = 0;
-//        /* send slave address to I2C bus */
-//        i2c_master_addressing(I2C0, SlaveAddr, I2C_TRANSMITTER);
-//        /* wait until ADDSEND bit is set */
-//        while((i2c_flag_get(I2C0, I2C_FLAG_ADDSEND) == 0) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
-//    	{
-//        	ulTimeCnt++;
-//    	}
-//        if(ulTimeCnt == I2C_SHORT_TIMEOUT)
-//        {
-//           uwTimeoutNum ++;
-//        }
-//        ulTimeCnt = 0;
-//        i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
-//        /* wait until the transmit data buffer is empty */
-//        while((i2c_flag_get(I2C0, I2C_FLAG_TBE) == 0) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
-//    	{
-//        	ulTimeCnt++;
-//    	}
-//        if(ulTimeCnt == I2C_SHORT_TIMEOUT)
-//        {
-//           uwTimeoutNum ++;
-//        }
-//        ulTimeCnt = 0;
-//        /* writeAddr transmission */
-//        i2c_data_transmit(I2C0, WriteAddr);
-//        /* wait until the BTC bit is set */
-//        while((i2c_flag_get(I2C0, I2C_FLAG_BTC) == 0) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
-//    	{
-//        	ulTimeCnt++;
-//    	}
-//        if(ulTimeCnt == I2C_SHORT_TIMEOUT)
-//        {
-//           uwTimeoutNum ++;
-//        }
-//        ulTimeCnt = 0;
-//        /* data transmission */
-//        i2c_data_transmit(I2C0, Data);
-//        /* wait until the BTC bit is set */
-//        while((i2c_flag_get(I2C0, I2C_FLAG_BTC) == 0) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
-//    	{
-//        	ulTimeCnt++;
-//    	}
-//        if(ulTimeCnt == I2C_SHORT_TIMEOUT)
-//        {
-//           uwTimeoutNum ++;
-//        }
-//        ulTimeCnt = 0;
-//        /* send a stop condition to I2C bus */
-//        i2c_stop_on_bus(I2C0);
-//        /* wait until stop condition generate */
-//        while((I2C_CTL0(I2C0)&I2C_CTL0_STOP) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
-//        {
-//        	ulTimeCnt++;
-//        }
-//        if(ulTimeCnt == I2C_SHORT_TIMEOUT)
-//        {
-//           uwTimeoutNum ++;
-//        }
-//        ulTimeCnt = 0;
-//        /* write to EEPROM disable */
-//        iGpio_Write(HW_GPIO_I2CWP_PIN,ApiGpio_LowLevel);
-//
-//        /* I2C EEPROM communication timeout fault */
-//        if(uwTimeoutNum > 0)
-//        {
-//           I2C_EE_ComuFltFlg = TRUE;
-//        }
-//    }
-//    else
-//    {}
-//}
-/*************************************************************************
- Function:
- Description:
- Call by:
- Input Variables:
- Output/Return Variables:
- Subroutine Call:
- Reference:
-*************************************************************************/
-void i2c_voPageWrite2EE(const UBYTE *pBuffer, UBYTE SlaveAddr, UBYTE WriteAddr, UBYTE NBytesToWrite) /* parasoft-suppress METRICS-28 "本项目圈复杂度无法更改,后续避免" */
-{
-    ULONG ulTimeCnt = 0;
-    UWORD uwTimeoutNum = 0;
-    
-    if(!I2C_EE_ComuFltFlg)
-    {
-        /* write to EEPROM enable*/
-        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))
-    	{
-        	ulTimeCnt++;
-    	}
-        if(ulTimeCnt == I2C_SHORT_TIMEOUT)
-        {
-           uwTimeoutNum ++;
-        }
-        ulTimeCnt = 0;
-        /* send a start condition to I2C bus */
-        i2c_start_on_bus(I2C0);
-        /* wait until SBSEND bit is set */
-        while((i2c_flag_get(I2C0, I2C_FLAG_SBSEND) == 0) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
-    	{
-        	ulTimeCnt++;
-    	}
-        if(ulTimeCnt == I2C_SHORT_TIMEOUT)
-        {
-           uwTimeoutNum ++;
-        }
-        ulTimeCnt = 0;
-        /* send slave address to I2C bus */
-        i2c_master_addressing(I2C0, SlaveAddr, I2C_TRANSMITTER);
-        /* wait until ADDSEND bit is set */
-        while((i2c_flag_get(I2C0, I2C_FLAG_ADDSEND) == 0) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
-    	{
-        	ulTimeCnt++;
-    	}
-        if(ulTimeCnt == I2C_SHORT_TIMEOUT)
-        {
-           uwTimeoutNum ++;
-        }
-        ulTimeCnt = 0;
-        i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
-        /* wait until the transmit data buffer is empty */
-        while((i2c_flag_get(I2C0, I2C_FLAG_TBE) == 0) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
-    	{
-        	ulTimeCnt++;
-    	}
-        if(ulTimeCnt == I2C_SHORT_TIMEOUT)
-        {
-           uwTimeoutNum ++;
-        }
-        ulTimeCnt = 0;
-        /* writeAddr transmission */
-        i2c_data_transmit(I2C0, WriteAddr);
-        /* wait until the BTC bit is set */
-        while((i2c_flag_get(I2C0, I2C_FLAG_BTC) == 0) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
-    	{
-        	ulTimeCnt++;
-    	}
-        if(ulTimeCnt == I2C_SHORT_TIMEOUT)
-        {
-           uwTimeoutNum ++;
-        }
-        ulTimeCnt = 0;
-        /* data transmission */
-        while (NBytesToWrite != 0)
-        {
-        	NBytesToWrite --;
-            i2c_data_transmit(I2C0, *pBuffer);
-            pBuffer++; /* parasoft-suppress MISRA2004-17_1 "本项目无法更改,后续避免未指向数组的指针运算" */ /* parasoft-suppress MISRA2004-17_4 "本项目无法更改,后续避免非数组索引指针运算" */
-            /* wait until the BTC bit is set */
-            while((i2c_flag_get(I2C0, I2C_FLAG_BTC) == 0) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
-        	{
-            	ulTimeCnt++;
-        	}
-            if(ulTimeCnt == I2C_SHORT_TIMEOUT)
-            {
-               uwTimeoutNum ++;
-            }
-            ulTimeCnt = 0;
-            /* I2C communication timeout fault */
-            if(uwTimeoutNum > 3 )
-            {
-               I2C_EE_ComuFltFlg = TRUE;
-               break;
-            }  
-        }
-        /* send a stop condition to I2C bus */
-        i2c_stop_on_bus(I2C0);    
-        /* wait until stop condition generate */ 
-        while((I2C_CTL0(I2C0)&I2C_CTL0_STOP) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
-    	{
-        	ulTimeCnt++;
-    	}
-        if(ulTimeCnt == I2C_SHORT_TIMEOUT)
-        {
-           uwTimeoutNum ++;
-        }
-        ulTimeCnt = 0;
-         /* I2C EEPROM communication timeout fault */
-        if(uwTimeoutNum > 0)
-        {
-           I2C_EE_ComuFltFlg = TRUE;
-        }  
-        /* write to EEPROM disable */
-        
-        iGpio_Write(HW_GPIO_I2CWP_PIN,ApiGpio_LowLevel);
-    }
-    else
-    {}
-}
-/*************************************************************************
- Function:
- Description:
- Call by:
- Input Variables:
- Output/Return Variables:
- Subroutine Call:
- Reference:
-*************************************************************************/
-static void i2c_voBufferWrite2EE(const UBYTE *pBuffer, UBYTE SlaveAddr, UBYTE WriteAddr, UBYTE NBytesToWrite)
-{
-    UBYTE ubNPages = 0, ubNSingleBytes = 0, ubAddr = 0, ubCnt = 0, ubTemp = 0;
-    
-    ubAddr = WriteAddr % I2C_EE_PAGESIZE_NBYTES;
-    ubCnt = I2C_EE_PAGESIZE_NBYTES - ubAddr; // Cnt datas away from page alignment
-    ubNPages = NBytesToWrite / I2C_EE_PAGESIZE_NBYTES;
-    ubNSingleBytes = NBytesToWrite % I2C_EE_PAGESIZE_NBYTES;    
-
-    if (ubAddr == 0)
-    {
-        if (ubNPages == 0)
-        {
-            i2c_voPageWrite2EE(pBuffer, SlaveAddr, WriteAddr, ubNSingleBytes);
-            i2c_voWaitEEReady(SlaveAddr);
-        }
-        else
-        {
-            while (ubNPages != 0)
-            {
-            	ubNPages--;
-                i2c_voPageWrite2EE(pBuffer, SlaveAddr, WriteAddr, I2C_EE_PAGESIZE_NBYTES);
-                i2c_voWaitEEReady(SlaveAddr);
-                WriteAddr += I2C_EE_PAGESIZE_NBYTES;
-                pBuffer += I2C_EE_PAGESIZE_NBYTES; /* parasoft-suppress MISRA2004-17_1 "本项目无法更改,后续避免未指向数组的指针运算" */ /* parasoft-suppress MISRA2004-17_4 "本项目无法更改,后续避免非数组索引指针运算" */
-            }
-            if (ubNSingleBytes != 0)
-            {
-                i2c_voPageWrite2EE(pBuffer, SlaveAddr, WriteAddr, ubNSingleBytes);
-                i2c_voWaitEEReady(SlaveAddr);
-            }
-        }
-    }
-    else
-    {
-        if (ubNPages == 0)
-        {
-            if (ubNSingleBytes > ubCnt)
-            {
-                ubTemp = ubNSingleBytes - ubCnt;
-                i2c_voPageWrite2EE(pBuffer, SlaveAddr, WriteAddr, ubCnt);
-                i2c_voWaitEEReady(SlaveAddr);
-                WriteAddr += ubCnt;
-                pBuffer += ubCnt; /* parasoft-suppress MISRA2004-17_1 "本项目无法更改,后续避免未指向数组的指针运算" */ /* parasoft-suppress MISRA2004-17_4 "本项目无法更改,后续避免非数组索引指针运算" */
-
-                i2c_voPageWrite2EE(pBuffer, SlaveAddr, WriteAddr, ubTemp);
-                i2c_voWaitEEReady(SlaveAddr);
-            }
-            else
-            {
-                i2c_voPageWrite2EE(pBuffer, SlaveAddr, WriteAddr, NBytesToWrite);
-                i2c_voWaitEEReady(SlaveAddr);
-            }
-        }
-        else
-        {
-            NBytesToWrite -= ubCnt;
-            ubNPages = NBytesToWrite / I2C_EE_PAGESIZE_NBYTES;
-            ubNSingleBytes = NBytesToWrite % I2C_EE_PAGESIZE_NBYTES;
-            if (ubCnt != 0)
-            {
-                /* Write the remaining bytes of the page where WriteAddr is located */
-                i2c_voPageWrite2EE(pBuffer, SlaveAddr, WriteAddr, ubCnt);
-                i2c_voWaitEEReady(SlaveAddr);
-                WriteAddr += ubCnt;
-                pBuffer += ubCnt; /* parasoft-suppress MISRA2004-17_1 "本项目无法更改,后续避免未指向数组的指针运算" */ /* parasoft-suppress MISRA2004-17_4 "本项目无法更改,后续避免非数组索引指针运算" */
-            }
-            while (ubNPages != 0)
-            {
-            	ubNPages--;
-                i2c_voPageWrite2EE(pBuffer, SlaveAddr, WriteAddr, I2C_EE_PAGESIZE_NBYTES);
-                i2c_voWaitEEReady(SlaveAddr);
-                WriteAddr += I2C_EE_PAGESIZE_NBYTES;
-                pBuffer += I2C_EE_PAGESIZE_NBYTES; /* parasoft-suppress MISRA2004-17_1 "本项目无法更改,后续避免未指向数组的指针运算" */ /* parasoft-suppress MISRA2004-17_4 "本项目无法更改,后续避免非数组索引指针运算" */
-            }
-            if (ubNSingleBytes != 0)
-            {
-                i2c_voPageWrite2EE(pBuffer, SlaveAddr, WriteAddr, I2C_EE_PAGESIZE_NBYTES);
-                i2c_voWaitEEReady(SlaveAddr);
-            }
-        }
-    }
-
-}
 
 /*************************************************************************
  Function:
@@ -680,7 +243,7 @@ void i2c_voInfoWrite2EE(const I2C_TX_COF *coef, I2C_TX_OUT *out)
         {
             ubSlaveAddr = I2C_SLAVEADDR_BLOCK1;
         }
-        //i2c_voBufferWrite2EE(I2C_pWriteBuffer, ubSlaveAddr, ubWriteAddr, ubNBytes);
+
         iI2C_Write(0, ubSlaveAddr, ubWriteAddr, 8, I2C_pWriteBuffer, ubNBytes);
     }
 
@@ -731,7 +294,6 @@ void i2c_voHistoryWrite2EE(const I2C_TX_COF *coef, I2C_TX_OUT *out)
             ubSlaveAddr = I2C_SLAVEADDR_BLOCK3;
         }
         
-        //i2c_voBufferWrite2EE(I2C_pHistoryWriteBuffer, ubSlaveAddr, ubWriteAddr, ubNBytes);
         iI2C_Write(0, ubSlaveAddr, ubWriteAddr, 8, I2C_pHistoryWriteBuffer, ubNBytes);
     }
     if ((ubReTX2 == 0) && (I2C_EE_ComuFltFlg != TRUE))
@@ -743,6 +305,7 @@ void i2c_voHistoryWrite2EE(const I2C_TX_COF *coef, I2C_TX_OUT *out)
         out->blTX2FinishFlg = FALSE;
     }
 }
+
 /*************************************************************************
  Function:
  Description:
@@ -752,314 +315,6 @@ void i2c_voHistoryWrite2EE(const I2C_TX_COF *coef, I2C_TX_OUT *out)
  Subroutine Call:
  Reference:
 *************************************************************************/
-void i2c_bus_reset(void)
-{
-    i2c_deinit(I2C0);
-    /* configure SDA/SCL for GPIO */
-    GPIO_BC(GPIOB) |= GPIO_PIN_6;
-    GPIO_BC(GPIOB) |= GPIO_PIN_7;
-    gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6);
-    gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_7);
-    __NOP();
-    __NOP();
-    __NOP();
-    __NOP();
-    __NOP();
-    GPIO_BOP(GPIOB) |= GPIO_PIN_6;
-    __NOP();
-    __NOP();
-    __NOP();
-    __NOP();
-    __NOP();
-    GPIO_BOP(GPIOB) |= GPIO_PIN_7;
-    /* connect I2C_SCL_PIN to I2C_SCL */
-    /* connect I2C_SDA_PIN to I2C_SDA */
-    gpio_init(GPIOB, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_6);
-    gpio_init(GPIOB, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_7);
-    /* configure the I2CX interface */
-        /* configure I2C0 clock */
-    i2c_clock_config(I2C0, 100000, I2C_DTCY_2);
-    
-    /* configure I2C0 address */
-    i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, I2C_SLAVEADDR_BLOCK1);
-    
-    /* enable acknowledge */
-    i2c_ack_config(I2C0, I2C_ACK_ENABLE);
-    
-//    /* enable I2C0 DMA */
-//    i2c_dma_config(I2C0, I2C_DMA_ON); 
-       
-    /* enable I2C0 */
-    i2c_enable(I2C0);
-}
-/*************************************************************************
- Function:
- Description:
- Call by:
- Input Variables:
- Output/Return Variables:
- Subroutine Call:
- Reference:
-*************************************************************************/
-//  void i2c_voSysparaReadFromEE(I2C_RXCRC_OUT *out) /* parasoft-suppress METRICS-28 "本项目圈复杂度无法更改,后续避免" */
-//  {
-  
-//      UWORD timeout = 0;
-//      UBYTE ubRdNBytes = 0, ubRdSlaveAddr = 0, ubRdAddr = 0;
-//      UBYTE state = (UBYTE)I2C_START;
-//      UBYTE read_cycle = 0;
-//      UBYTE ubRdCnt = 2;
-   
-//      UBYTE i2c_timeout_flag = 0;
-//      UBYTE *p_buffer;
-    
-//       while(ubRdCnt != 0)
-//       {
-//      	 ubRdCnt--;
-
-//          if (ubRdCnt == 1)
-//          {
-//              ubRdSlaveAddr = I2C_SLAVEADDR_BLOCK1;
-//              ubRdAddr = 0x00;
-//              ubRdNBytes = I2C_RX1_NBYTES;           
-//              p_buffer = I2C_ubReadBuffer[0];
-//              i2c_timeout_flag = 0;
-//          }
-//          else if (ubRdCnt == 0)
-//          {
-//              ubRdSlaveAddr = I2C_SLAVEADDR_BLOCK3;
-//              ubRdAddr = 0x00;
-//              ubRdNBytes = I2C_RX2_NBYTES;
-//              p_buffer = I2C_ubReadBuffer[1];
-//              i2c_timeout_flag = 0;
-//          }
-//          else
-//          {
-//          	//do nothing
-//          }
-        
-//          /* enable acknowledge */
-//          i2c_ack_config(I2C0, I2C_ACK_ENABLE);
-        
-//          while(i2c_timeout_flag ==0)
-//          {
-//              switch(state) 
-//              {
-//              case I2C_START:
-//                  if(RESET == read_cycle) 
-//                  {
-//                      /* disable I2C0 */
-//                      i2c_disable(I2C0);
-//                      /* enable I2C0 */
-//                      i2c_enable(I2C0);
-//                      /* enable acknowledge */
-//                      i2c_ack_config(I2C0, I2C_ACK_ENABLE);
-//                      /* i2c master sends start signal only when the bus is idle */
-//                      while((i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) != 0) && (timeout < I2C_SHORT_TIMEOUT))
-//                      {
-//                          timeout++;
-//                      }
-                    
-//                      if(timeout < I2C_SHORT_TIMEOUT) 
-//                      {
-//                          /* send the start signal */
-//                          i2c_start_on_bus(I2C0);
-//                          timeout = 0;
-//                          state = (UBYTE)I2C_SEND_ADDRESS;
-//                      } 
-//                      else 
-//                      {
-//                          i2c_bus_reset();
-//                          timeout = 0;
-//                          state = (UBYTE)I2C_START;
-//                      }
-//                  } 
-//                  else 
-//                  {
-//                      i2c_start_on_bus(I2C0);
-//                      timeout = 0;
-//                      state = (UBYTE)I2C_SEND_ADDRESS;
-//                  }
-//                  break;
-//              case I2C_SEND_ADDRESS:
-//                  /* i2c master sends START signal successfully */
-//                  while((i2c_flag_get(I2C0, I2C_FLAG_SBSEND) == 0) && (timeout < I2C_SHORT_TIMEOUT))
-//                  {
-//                      timeout++;
-//                  }
-//                  if(timeout < I2C_SHORT_TIMEOUT) 
-//                  {
-//                      if(RESET == read_cycle) 
-//                      {
-//                          i2c_master_addressing(I2C0, ubRdSlaveAddr, I2C_TRANSMITTER);
-//                          state = (UBYTE)I2C_CLEAR_ADDRESS_FLAG;
-//                      } 
-//                      else 
-//                      {
-//                          i2c_master_addressing(I2C0, ubRdSlaveAddr, I2C_RECEIVER);
-//                          state = (UBYTE)I2C_CLEAR_ADDRESS_FLAG;
-//                      }
-//                      timeout = 0;
-//                  } 
-//                  else 
-//                  {
-//                      timeout = 0;
-//                      state = (UBYTE)I2C_START;
-//                      read_cycle = 0;
-//                  }
-//                  break;
-//              case I2C_CLEAR_ADDRESS_FLAG:
-//                  /* address flag set means i2c slave sends ACK */
-//                  while((i2c_flag_get(I2C0, I2C_FLAG_ADDSEND) == 0) && (timeout < I2C_SHORT_TIMEOUT))
-//                  {
-//                      timeout++;
-//                  }
-//                  if(timeout < I2C_SHORT_TIMEOUT) 
-//                  {
-//                      i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
-//                      timeout = 0;
-//                      state = (UBYTE)I2C_TRANSMIT_DATA;
-//                  } 
-//                  else 
-//                  {
-//                      timeout = 0;
-//                      state = (UBYTE)I2C_START;
-//                      read_cycle = 0;   
-//                  }
-//                  break;
-//              case I2C_TRANSMIT_DATA:
-//                  if(RESET == read_cycle) 
-//                  {
-//                      /* wait until the transmit data buffer is empty */
-//                      while((i2c_flag_get(I2C0, I2C_FLAG_TBE) == 0) && (timeout < I2C_SHORT_TIMEOUT))
-//                      {
-//                          timeout++;
-//                      }
-//                      if(timeout < I2C_SHORT_TIMEOUT) 
-//                      {
-//                          /* send the EEPROM's internal address to write to : only one byte address */
-//                          i2c_data_transmit(I2C0, ubRdAddr);
-//                          timeout = 0;
-//                      } 
-//                      else 
-//                      {
-//                          timeout = 0;
-//                          state = (UBYTE)I2C_START;
-//                          read_cycle = 0;    
-//                      }
-//                      /* wait until BTC bit is set */
-//                      while((i2c_flag_get(I2C0, I2C_FLAG_BTC) == 0) && (timeout < I2C_SHORT_TIMEOUT))
-//                      {
-//                          timeout++;
-//                      }
-//                      if(timeout < I2C_SHORT_TIMEOUT) 
-//                      {
-//                          timeout = 0;
-//                          state = (UBYTE)I2C_START;
-//                          read_cycle++;
-//                      }
-//                      else 
-//                      {
-//                          timeout = 0;
-//                          state = (UBYTE)I2C_START;
-//                          read_cycle = 0;
-//                      }
-//                  } 
-//                  else 
-//                  {
-//                      /* one byte master reception procedure (polling) */
-//                      if(ubRdNBytes < 2) 
-//                      {
-//                          /* disable acknowledge */
-//                          i2c_ack_config(I2C0, I2C_ACK_DISABLE);
-//                          /* clear ADDSEND register by reading I2C_STAT0 then I2C_STAT1 register (I2C_STAT0 has already been read) */
-//                          i2c_flag_get(I2C0, I2C_FLAG_ADDSEND);
-//                          /* send a stop condition to I2C bus*/
-//                          i2c_stop_on_bus(I2C0);
-//                          /* wait for the byte to be received */
-//                          while(i2c_flag_get(I2C0, I2C_FLAG_RBNE) == 0)
-//                          {
-//                          	// do nothing
-//                          }
-//                          /* read the byte received from the EEPROM */
-//                          *p_buffer = i2c_data_receive(I2C0);
-//                          /* decrement the read bytes counter */
-//                          ubRdNBytes--;
-//                          timeout = 0;
-//                          state = (UBYTE)I2C_STOP;
-//                      } 
-//                      else 
-//                      {  /* more than one byte master reception procedure (DMA) */                   
-                      
-//                          dma_transfer_number_config(DMA0, DMA_CH6, ubRdNBytes);        
-//                          DMA_CH6MADDR(DMA0) = (ULONG)p_buffer;
-                      
-//                          i2c_dma_last_transfer_config(I2C0, I2C_DMALST_ON);
-//                          /* enable I2C0 DMA */
-//                          i2c_dma_config(I2C0, I2C_DMA_ON);
-//                          /* enable DMA0 channel5 */
-//                          dma_channel_enable(DMA0, DMA_CH6);
-//                          /* wait until BTC bit is set */
-//                          while(dma_flag_get(DMA0, DMA_CH6, DMA_FLAG_FTF) == 0)
-//                          {
-//                          	// do nothing
-//                          }
-
-//                          state = (UBYTE)I2C_STOP;
-//                      }
-//                  }
-//                  break;
-//              case I2C_STOP:
-//                  /* send a stop condition to I2C bus */
-//                  i2c_stop_on_bus(I2C0);
-//                  /* i2c master sends STOP signal successfully */
-//                  while((I2C_CTL0(I2C0) & I2C_CTL0_STOP) && (timeout < I2C_SHORT_TIMEOUT)) 
-//                  {
-//                      timeout++;
-//                  }
-
-//                  if(timeout < I2C_SHORT_TIMEOUT)
-//                  {
-//                      timeout = 0;                 
-//                      i2c_timeout_flag = 1;
-//                      state = (UBYTE)I2C_START;
-//                      read_cycle = 0;
-                   
-//                      /* disable DMA0 CH6 */
-//                      dma_channel_disable(DMA0, DMA_CH6);
-//                      /* disable I2C0 DMA */
-//                      i2c_dma_config(I2C0, I2C_DMA_OFF); 
-//                      i2c_dma_last_transfer_config(I2C0, I2C_DMALST_OFF);
-//                  } 
-//                  else 
-//                  {
-//                       timeout = 0;
-//                      //state = I2C_START;
-//                       state = (UBYTE)I2C_STOP;
-//                       read_cycle = 0;
-//                  }
-                                              
-//                  break;
-//              default:
-//                  state = (UBYTE)I2C_START;
-//                  read_cycle = 0;
-//                  i2c_timeout_flag = 1;
-//                  timeout = 0;
-                
-//                  break;
-//              }
-//          }
-        
-//       }
-    
-//      i2c_voReadBufferCRC(out);
-
-//      i2c_voGetValueFrmBuffer(out);
-
-//      out->ReadFinishFlg = TRUE;
-    
-//  }
-
 void i2c_voSysparaReadFromEE(I2C_RXCRC_OUT *out) /* parasoft-suppress METRICS-28 "本项目圈复杂度无法更改,后续避免" */
 {
    UBYTE ubRdNBytes = 0, ubRdSlaveAddr = 0, ubRdAddr = 0;
@@ -1098,6 +353,7 @@ void i2c_voSysparaReadFromEE(I2C_RXCRC_OUT *out) /* parasoft-suppress METRICS-28
 
     out->ReadFinishFlg = TRUE;
 }
+
 /*************************************************************************
  Function:
  Description:
@@ -1368,194 +624,8 @@ void i2c_voGetValueFrmBuffer(const I2C_RXCRC_OUT *out)
     Syspara2.stHistoryPara.uwPosSensorAlamTimes.uwReal = I2C_uwHistoryParaRead[30];
 
 }
-/*************************************************************************
- Function:
- Description:
- Call by:
- Input Variables:
- Output/Return Variables:
- Subroutine Call:
- Reference:
-*************************************************************************/
-void  i2c_voWriteTest(void)
-{
-    UBYTE WriteBuffer[4];
-    UBYTE  *pBuffer;
-    
-    WriteBuffer[0] = 0x06;
-    WriteBuffer[1] = 0x08;  
-    WriteBuffer[2] = 0x0A;
-    WriteBuffer[3] = 0x01;
-    pBuffer = WriteBuffer;
-    i2c_voPageWrite2EE(pBuffer, I2C_SLAVEADDR_BLOCK1, 0x00, 4);
-}
 
-/*************************************************************************
- Function:
- Description:
- Call by:
- Input Variables:
- Output/Return Variables:
- Subroutine Call:
- Reference:
-*************************************************************************/
-void  i2c_voReadTest(void) /* parasoft-suppress METRICS-28 "本项目圈复杂度无法更改,后续避免" */
-{
-    UBYTE  ReadBuffer[6];
-    
-    ULONG ulTimeCnt = 0;
-    UWORD uwTimeoutNum = 0;
-    UWORD uwReadNBytes = 6;
-    
-    /* wait until I2C bus is idle */
-    while((i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) != 0) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
-    {
-    	ulTimeCnt++;
-    }
-    if(ulTimeCnt == I2C_SHORT_TIMEOUT)
-    {
-       uwTimeoutNum ++;
-    }
-    ulTimeCnt = 0;
-    /* send a start condition to I2C bus */
-    i2c_start_on_bus(I2C0);
-    /* wait until SBSEND bit is set */
-    while((i2c_flag_get(I2C0, I2C_FLAG_SBSEND) == 0) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
-    {
-    	ulTimeCnt++;
-    }
-    if(ulTimeCnt == I2C_SHORT_TIMEOUT)
-    {
-       uwTimeoutNum ++;
-    }
-    ulTimeCnt = 0;
-    /* send slave address to I2C bus */
-    i2c_master_addressing(I2C0, I2C_SLAVEADDR_BLOCK1, I2C_TRANSMITTER);
-    /* wait until ADDSEND bit is set */
-    while((i2c_flag_get(I2C0, I2C_FLAG_ADDSEND) == 0) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
-    {
-    	ulTimeCnt++;
-    }
-    if(ulTimeCnt == I2C_SHORT_TIMEOUT)
-    {
-       uwTimeoutNum ++;
-    }
-    ulTimeCnt = 0;
-    /* clear ADDSEND bit */
-    i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
-    /* wait until the TBE bit is set */
-    while((i2c_flag_get(I2C0, I2C_FLAG_TBE) == 0) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
-    {
-    	ulTimeCnt++;
-    }
-    if(ulTimeCnt == I2C_SHORT_TIMEOUT)
-    {
-       uwTimeoutNum ++;
-    }
-    ulTimeCnt = 0;
-    /* readAddr transmission */
-    i2c_data_transmit(I2C0, 0x00);
-    /* wait until the BTC bit is set */
-    while((i2c_flag_get(I2C0, I2C_FLAG_BTC) == 0) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
-    {
-    	ulTimeCnt++;
-    }
-    if(ulTimeCnt == I2C_SHORT_TIMEOUT)
-    {
-       uwTimeoutNum ++;
-    }
-    ulTimeCnt = 0;
-    
-    /* send a start condition to I2C bus */
-    i2c_start_on_bus(I2C0);
-    /* wait until SBSEND bit is set */
-    while((i2c_flag_get(I2C0, I2C_FLAG_SBSEND) == 0) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
-    {
-    	ulTimeCnt++;
-    }
-    if(ulTimeCnt == I2C_SHORT_TIMEOUT)
-    {
-       uwTimeoutNum ++;
-    }
-    ulTimeCnt = 0;
-    /* send slave address to I2C bus */
-    i2c_master_addressing(I2C0, I2C_SLAVEADDR_BLOCK1, I2C_RECEIVER);
-    /* wait until ADDSEND bit is set */
-    while((i2c_flag_get(I2C0, I2C_FLAG_ADDSEND) == 0)&& (ulTimeCnt < I2C_SHORT_TIMEOUT))
-    {
-    	ulTimeCnt++;
-    }
-    if(ulTimeCnt == I2C_SHORT_TIMEOUT)
-    {
-       uwTimeoutNum ++;
-    }
-    ulTimeCnt = 0;
-    /* clear ADDSEND bit */
-    i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
-    
-    while(uwReadNBytes != 0)
-    {
-    	uwReadNBytes--;
-        if(2 == uwReadNBytes) 
-        {
-            /* wait until BTC bit is set */
-            while((i2c_flag_get(I2C0, I2C_FLAG_BTC) == 0) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
-            {
-            	ulTimeCnt++;
-            }
-            if(ulTimeCnt == I2C_SHORT_TIMEOUT)
-            {
-               uwTimeoutNum ++;
-            }
-            ulTimeCnt = 0;
-            /* disable acknowledge */
-            i2c_ack_config(I2C0, I2C_ACK_DISABLE);
-        }
-        if(1 == uwReadNBytes) 
-        {
-            /* wait until BTC bit is set */
-            while((i2c_flag_get(I2C0, I2C_FLAG_BTC) == 0) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
-            {
-            	ulTimeCnt++;
-            }
-            if(ulTimeCnt == I2C_SHORT_TIMEOUT)
-            {
-               uwTimeoutNum ++;
-            }
-            ulTimeCnt = 0;
-            /* send a stop condition to I2C bus */
-            i2c_stop_on_bus(I2C0);
-         }
-        
-        while((i2c_flag_get(I2C0, I2C_FLAG_RBNE) == 0) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
-        {
-        	ulTimeCnt++;
-        }
-        if(ulTimeCnt == I2C_SHORT_TIMEOUT)
-        {
-           uwTimeoutNum ++;
-        }
-        ulTimeCnt = 0;
-        ReadBuffer[5 - uwReadNBytes] = i2c_data_receive(I2C0);
-    }
-       
-    
-    /* wait until stop condition generate */ 
-    while((I2C_CTL0(I2C0)&I2C_CTL0_STOP) && (ulTimeCnt < I2C_SHORT_TIMEOUT))
-    {
-    	ulTimeCnt++;
-    }
-    if(ulTimeCnt == I2C_SHORT_TIMEOUT)
-    {
-       uwTimeoutNum ++;
-    }
-    ulTimeCnt = 0;
-    
-    if(uwTimeoutNum > 3)
-    {
-        I2C_EE_ComuFltFlg = TRUE;
-    }
-}
+
 /*************************************************************************
  Copyright (c) 2018 Welling Motor Technology(Shanghai) Co. Ltd.
  All rights reserved.

+ 21 - 0
User project/3.BasicFunction/Source/spi_master.c

@@ -24,6 +24,24 @@
 /************************************************************************
  Included File
 *************************************************************************/
+#ifdef RUN_ARCH_SIM
+#include "spi_master.h"
+
+void spi_voResolverInit(void)
+{}
+void spi_voResolverCoef(SPI_RESOLVER_COEFIN *in, SPI_RESOLVER_COEF *out)
+{}
+void spi_voResolver(const SPI_RESOLVER_COEF *coef, SPI_RESOLVER_OUT *out)
+{}
+void spi_voResolverLock(void)
+{}
+void spi_voMagneticDetection(void)
+{}
+void spi_voReadWriteSeneorReg(void)
+{}
+
+#else
+
 #include "user.h"
 #include "spi_master.h"
 #include "api_rt.h"
@@ -469,6 +487,7 @@ void spi_voReadWriteSeneorReg(void) /* parasoft-suppress METRICS-28 "本项目
         }
     }
 }
+
 /*************************************************************************
  Local Functions (N/A)
 *************************************************************************/
@@ -479,6 +498,8 @@ void spi_voReadWriteSeneorReg(void) /* parasoft-suppress METRICS-28 "本项目
 *************************************************************************/
 #ifdef _SPI_MASTER_C_
 #undef _SPI_MASTER_C_ /* parasoft-suppress MISRA2004-19_6 "本项目中无法更改,后续避免使用" */
+#endif
+
 #endif
 /*************************************************************************
  End of this File (EOF)!

+ 10 - 0
User project/3.BasicFunction/Source/switchhall.c

@@ -13,6 +13,15 @@
  Beginning of File, do not put anything above here except notes
  Compiler Directives:
 *************************************************************************/
+#ifdef RUN_ARCH_SIM
+#include "switchhall.h"
+void switchhall_voInit(void)
+{}
+void switchhall_voTim3Int(void)
+{}
+void switchhall_voPosCalTbc(void)
+{}
+#else
 #include "gd32f30x.h"
 #include "typedefine.h"
 #include "syspar.h"
@@ -917,6 +926,7 @@ void switchhall_voPosCalTbc(void) /* parasoft-suppress METRICS-28 "本项目圈
 //    switchhall_stOut.swHighSpdLpfPu = switchhall_pvt_slHighSpdLpfPu >> 15;
 }
 
+#endif
 /*************************************************************************
  End of this File (EOF)!
  Do not put anything after this part!

+ 12 - 16
User project/3.BasicFunction/Source/torquesensor.c

@@ -16,14 +16,10 @@
 #include "torquesensor.h"
 #include "syspar.h"
 #include "mathtool.h"
-#include "torquesensor.h"
 #include "CodePara.h"
 #include "canAppl.h"
-#include "api_rt.h"
-
-#ifdef RUN_ARCH_SIM
-#include "test_user.h"
-#endif
+#include "api.h"
+#include "board_config.h"
 /******************************
  *
  * static  Parameter
@@ -141,7 +137,7 @@ void torsensor_voTorSensorCof(void)
                 uwAverageOffset = torsensor_stTorSensorCof.uwTorqueOffsetOrign;
             }            
 
-            if(abs((SWORD)torsensor_stTorSensorCof.uwTorqueOffsetPowerUp - (SWORD)uwAverageOffset) > 200)   
+            if(ABS((SWORD)torsensor_stTorSensorCof.uwTorqueOffsetPowerUp - (SWORD)uwAverageOffset) > 200)   
             {
                 torsensor_stTorSensorCof.uwTorqueOffset = uwAverageOffset;
             }
@@ -273,28 +269,28 @@ void torsensor_voTorSensorCof(void)
 //        else if (TorqReg <= torsensor_stTorSensorCof.uwBikeTorStep1ADC)
 //        {
 //            TorqValuePu = 0 +
-//                ((((SQWORD)abs((SWORD)(TorqReg) - torsensor_stTorSensorCof.uwTorqueOffset)) *
+//                ((((SQWORD)ABS((SWORD)(TorqReg) - torsensor_stTorSensorCof.uwTorqueOffset)) *
 //                 torsensor_stTorSensorCof.ulTorqueReg2Pu1) >>
 //                10); // Q14
 //        }
 //        else if (TorqReg <= torsensor_stTorSensorCof.uwBikeTorStep2ADC)
 //        {
 //            TorqValuePu = torsensor_stTorSensorCof.uwBikeTorStep1NmPu + 
-//                ((((SQWORD)abs((SWORD)(TorqReg) - torsensor_stTorSensorCof.uwBikeTorStep1ADC)) *
+//                ((((SQWORD)ABS((SWORD)(TorqReg) - torsensor_stTorSensorCof.uwBikeTorStep1ADC)) *
 //                 torsensor_stTorSensorCof.ulTorqueReg2Pu2) >>
 //                10); // Q14
 //        }
 //        else if (TorqReg <= torsensor_stTorSensorCof.uwBikeTorStep3ADC)
 //        {
 //            TorqValuePu = torsensor_stTorSensorCof.uwBikeTorStep2NmPu +
-//                ((((SQWORD)abs((SWORD)(TorqReg) - torsensor_stTorSensorCof.uwBikeTorStep2ADC)) *
+//                ((((SQWORD)ABS((SWORD)(TorqReg) - torsensor_stTorSensorCof.uwBikeTorStep2ADC)) *
 //                 torsensor_stTorSensorCof.ulTorqueReg2Pu3) >>
 //                10); // Q14
 //        }
 //        else if (TorqReg<= torsensor_stTorSensorCof.uwBikeTorStep4ADC)
 //        {
 //            TorqValuePu = torsensor_stTorSensorCof.uwBikeTorStep3NmPu + 
-//                ((((SQWORD)abs((SWORD)(TorqReg) - torsensor_stTorSensorCof.uwBikeTorStep3ADC)) *
+//                ((((SQWORD)ABS((SWORD)(TorqReg) - torsensor_stTorSensorCof.uwBikeTorStep3ADC)) *
 //                 torsensor_stTorSensorCof.ulTorqueReg2Pu4) >>
 //                10); // Q14
 //        }
@@ -391,7 +387,7 @@ void torsensor_voTorADC(void) // need to match ADC_StartConversion(ADC1);
     torsensor_stTorSensorOut.uwTorqueReg = iAdc_GetResultPointer(0)[HW_ADC_TORQ_CH];
 #if (TORSENSOR_USEMOL == TORSENSOR_USEDEFAULT)
     torsensor_stTorSensorOut.uwTorquePu =
-        (((SQWORD)abs((SWORD)(torsensor_stTorSensorOut.uwTorqueReg) - torsensor_stTorSensorCof.uwTorqueOffset)) *
+        (((SQWORD)ABS((SWORD)(torsensor_stTorSensorOut.uwTorqueReg) - torsensor_stTorSensorCof.uwTorqueOffset)) *
             torsensor_stTorSensorCof.ulTorqueReg2PuDefault) >>
         10; // Q14
 
@@ -403,27 +399,27 @@ void torsensor_voTorADC(void) // need to match ADC_StartConversion(ADC1);
     else if (torsensor_stTorSensorOut.uwTorqueReg <= torsensor_stTorSensorCof.uwBikeTorStep1ADC)
     {
         torsensor_stTorSensorOut.uwTorquePu = (UWORD)(0 +
-            ((((UQWORD)abs((SWORD)torsensor_stTorSensorOut.uwTorqueReg - torsensor_stTorSensorCof.uwTorqueOffset)) *
+            ((((UQWORD)ABS((SWORD)torsensor_stTorSensorOut.uwTorqueReg - torsensor_stTorSensorCof.uwTorqueOffset)) *
                 torsensor_stTorSensorCof.ulTorqueReg2Pu1) >> 10)); // Q14
     }
     else if (torsensor_stTorSensorOut.uwTorqueReg <= torsensor_stTorSensorCof.uwBikeTorStep2ADC)
     {
         torsensor_stTorSensorOut.uwTorquePu = (UWORD)(torsensor_stTorSensorCof.uwBikeTorStep1NmPu +
-            ((((UQWORD)abs((SWORD)torsensor_stTorSensorOut.uwTorqueReg - torsensor_stTorSensorCof.uwBikeTorStep1ADC)) *
+            ((((UQWORD)ABS((SWORD)torsensor_stTorSensorOut.uwTorqueReg - torsensor_stTorSensorCof.uwBikeTorStep1ADC)) *
                 torsensor_stTorSensorCof.ulTorqueReg2Pu2) >> 10)); // Q14
 
     }
     else if (torsensor_stTorSensorOut.uwTorqueReg <= torsensor_stTorSensorCof.uwBikeTorStep3ADC)
     {
         torsensor_stTorSensorOut.uwTorquePu = (UWORD)(torsensor_stTorSensorCof.uwBikeTorStep2NmPu +
-            ((((UQWORD)abs((SWORD)torsensor_stTorSensorOut.uwTorqueReg - torsensor_stTorSensorCof.uwBikeTorStep2ADC)) *
+            ((((UQWORD)ABS((SWORD)torsensor_stTorSensorOut.uwTorqueReg - torsensor_stTorSensorCof.uwBikeTorStep2ADC)) *
                 torsensor_stTorSensorCof.ulTorqueReg2Pu3) >> 10)); // Q14
 
     }
     else if (torsensor_stTorSensorOut.uwTorqueReg <= torsensor_stTorSensorCof.uwBikeTorStep4ADC)
     {
         torsensor_stTorSensorOut.uwTorquePu = (UWORD)(torsensor_stTorSensorCof.uwBikeTorStep3NmPu +
-            ((((UQWORD)abs((SWORD)torsensor_stTorSensorOut.uwTorqueReg - torsensor_stTorSensorCof.uwBikeTorStep3ADC)) *
+            ((((UQWORD)ABS((SWORD)torsensor_stTorSensorOut.uwTorqueReg - torsensor_stTorSensorCof.uwBikeTorStep3ADC)) *
                 torsensor_stTorSensorCof.ulTorqueReg2Pu4) >> 10)); // Q14
 
     }

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

@@ -23,8 +23,8 @@
 
 #ifndef RUN_ARCH_SIM
 #include "gd32f30x_libopt.h"
-#endif
 #include "STLmain.h"
+#endif
 #include "syspar.h"
 #include "board_config.h"
 
@@ -73,9 +73,9 @@ extern "C" {
 #define ENABLE_IRQ  stl_voEnableIrq()
 #define DISABLE_IRQ stl_voDisableIrq()
 /* IPM fault flag define */
-#define MCU_BRKIN_FLG     (TIMER_INTF(TIMER0) & 0x0080)
-#define MCU_BRKIN_FLG_CLR (TIMER_INTF(TIMER0) &= ~0x0080)
-#define MCU_POEN_FLG_EN   (TIMER_CCHP(TIMER0) |= 0x00008000)
+// #define MCU_BRKIN_FLG     (TIMER_INTF(TIMER0) & 0x0080)
+// #define MCU_BRKIN_FLG_CLR (TIMER_INTF(TIMER0) &= ~0x0080)
+// #define MCU_POEN_FLG_EN   (TIMER_CCHP(TIMER0) |= 0x00008000)
 /* SPI CS enable */
 //#define IO_SPI2_NSS_DISABLE  (GPIO_OCTL(GPIOD)|= 0x0004)          
 //#define IO_SPI2_NSS_ENABLE   (GPIO_OCTL(GPIOD)&= ~0x0004)  

+ 1 - 0
User project/4.BasicHardwSoftwLayer/2.BasicSoftwLayer/Include/typedefine.h

@@ -78,6 +78,7 @@ typedef unsigned long long UQWORD;
 
 #define _IQ(A) _IQ14(A) // Global IQ
 
+#define ABS(x) ((x) >= 0 ? (x) : (-(x)))
 
 #ifdef RUN_ARCH_SIM
 typedef int BOOL;

+ 1 - 37
User project/4.BasicHardwSoftwLayer/2.BasicSoftwLayer/Include/user.h

@@ -500,43 +500,8 @@ Update Time
 /************************************************************************
  Head files (N/A)
 *************************************************************************/
-#ifdef RUN_ARCH_SIM
-#include "CodePara.h"
-#include "hwsetup.h"
-#include "mathtool.h"
-#include "main.h"
-#include "spdctrFSM.h"
-#include "glbcof.h"
-#include "crdnt.h"
-#include "tbc.h"
-#include "tbs.h"
-#include "tbt.h"
-#include "alarm.h"
-#include "adc.h"
-#include "spi_master.h"
-#include "i2c_master.h"
-#include "spdctrmode.h"
-#include "acr.h"
-#include "asr.h"
-#include "torqobs.h"
-#include "obs.h"
-#include "dbc.h"
-#include "flxwkn.h"
-#include "pwm.h"
-#include "brake.h"
-#include "hfinj.h"
-#include "pwrlim.h"
-#include "bootloader.h"
-#include "brake.h"
-#include "macroequ.h"
-#include "alignstartup.h"
-#include "switchhall.h"
-#else
-
-//#include <stdlib.h>
-//#include "gd32F30x.h"
-#include "CodePara.h"
 #include "classB.h"
+#include "CodePara.h"
 #include "hwsetup.h"
 #include "mathtool.h"
 #include "main.h"
@@ -567,7 +532,6 @@ Update Time
 #include "macroequ.h"
 #include "alignstartup.h"
 #include "switchhall.h"
-#endif
 /************************************************************************
  Flag Define (N/A)
 *************************************************************************/

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

@@ -391,7 +391,7 @@ int iI2C_Read(uint8_t devIndex, uint16_t devAddr, uint16_t memAddr, uint8_t memA
                 } 
                 else 
                 {
-                    i2cBusResetCnt += i2c_bus_reset();
+                    i2cBusResetCnt += iRtI2C_BusReset(devIndex);
                     timeoutCnt = 0;
                     state = (uint8_t)I2C_START;
                 }

+ 2 - 1
tests/sim/sim_board/api_rt/api_rt_adc.c

@@ -4,12 +4,13 @@
 #include "board.h"
 #include <stdint.h>
 
-ApiRtAdc_Handle Adcs[2];
+ApiRtAdc_Handle Adcs[3];
 
 void iRtAdc_Init()
 {
     Adcs[0].AdcBase = &adc;
     Adcs[1].AdcBase = &adc2;
+    Adcs[2].AdcBase = &adc3;
 }
 
 uint16_t iAdc_GetResult(uint8_t devIndex, uint8_t channelIndex)

+ 1 - 1
tests/sim/sim_board/api_rt/api_rt_adc.h

@@ -18,7 +18,7 @@ typedef struct
     uint16_t        Results[API_RT_ADC_RESULT_SIZE];
 } ApiRtAdc_Handle;
 
-extern ApiRtAdc_Handle Adcs[2];
+extern ApiRtAdc_Handle Adcs[3];
 
 extern void iRtAdc_Init();
 

+ 56 - 21
tests/sim/sim_board/api_rt/api_rt_pwm.c

@@ -10,26 +10,31 @@ ApiRtPwm_Handle Pwms[1];
 
 void iRtPwm_AdcEocIsr()
 {
-    if (Pwms[0].PwmBase->CntDir == ApiPwm_CountUp)
+    if (Pwms[0].PwmPrdCnt == 0)
     {
-        Adcs[1].Results[Pwms[0].AdcTriggerIndex] = Pwms[0].AdcTriggerAdcBase->SamplingOutput[0];
-        Pwms[0].AdcTriggerIndex += 1;
+        Adcs[1].Results[HW_ADC_IA_CH] = Pwms[0].AdcTriggerAdcBase->SamplingOutput[0];
+        Adcs[1].Results[HW_ADC_IB_CH] = Pwms[0].AdcTriggerAdcBase->SamplingOutput[1];
+        Adcs[1].Results[HW_ADC_IC_CH] = Pwms[0].AdcTriggerAdcBase->SamplingOutput[2];
+        Pwms[0].AdcTriggerIndex += 3;
+        Pwms[0].PwmPrdCnt = 1;
         if (Pwms[0].AdcTriggerIndex < Pwms[0].AdcTriggerUpCounts)
         {
-            Pwms[0].AdcTriggerTimerBase->CompareReloadInput = Pwms[0].AdcTriggerUpTicks[Pwms[0].AdcTriggerIndex];
+            Pwms[0].AdcTriggerTimerBase->CompareReloadInput = Pwms[0].AdcTriggerUpTicks[0];  ///< 设定单电阻采样触发值
         }
         else
         {
             Pwms[0].AdcTriggerAdcBase->Base.Enable = 0;
         }
     }
-    else
+    else if (Pwms[0].PwmPrdCnt == 1)
     {
-        Adcs[1].Results[Pwms[0].AdcTriggerIndex] = Pwms[0].AdcTriggerAdcBase->SamplingOutput[0];
+        Adcs[2].Results[HW_ADC_IDC_CH] = Pwms[0].AdcTriggerAdcBase->SamplingOutput[3];    ///< 虚拟外设采用同一ADC采样三相电流与母线电流
+
         Pwms[0].AdcTriggerIndex += 1;
+        Pwms[0].PwmPrdCnt = 0;
         if (Pwms[0].AdcTriggerIndex < Pwms[0].AdcTriggerDownCounts)
         {
-            Pwms[0].AdcTriggerTimerBase->CompareReloadInput = Pwms[0].AdcTriggerDownTicks[Pwms[0].AdcTriggerIndex];
+            Pwms[0].AdcTriggerTimerBase->CompareReloadInput = Pwms[0].AdcTriggerDownTicks[1];  ///< 设定Rdson采样触发值
         }
         else
         {
@@ -59,6 +64,8 @@ void iRtPwm_Init()
         Pwms[0].ChannelISR[i].Action = 0;
         // Pwms[0].ChannelOutputEnable[i] = 0;
     }
+    Pwms[0].PwmPrdCnt = 0;
+    Pwms[0].PwmPrdCnt2 = 0;
 
     scheduler.InterruptBind(&scheduler, &pwm.Base, PwmInterruptOutUpdate, iRtPwm_UpdateIsr);
 
@@ -89,7 +96,7 @@ void iRtPwm_Init()
 
 uint32_t iPwm_GetClock(uint8_t devIndex)
 {
-    return 64000000UL;
+    return 72000000UL;
 }
 
 void iPwm_EnableCount(uint8_t devIndex)
@@ -411,29 +418,57 @@ void iRtPwm_UpdateIsr()
 {
     if (Pwms[0].PwmBase->CntDir == SIM_PWM_DIR_UP)
     {
-        if (Pwms[0].MultiSyncEnable == 1 && Pwms[0].AdcTriggerUpCounts > 0)
+        if(Pwms[0].PwmPrdCnt2 == 0)
+        {
+            if (Pwms[0].MultiSyncEnable == 1 && Pwms[0].AdcTriggerUpCounts > 0)
+            {
+                Pwms[0].AdcTriggerIndex                         = 0;
+                Pwms[0].AdcTriggerTimerBase->CompareReloadInput = Pwms[0].AdcTriggerUpTicks[0];
+                Pwms[0].AdcTriggerAdcBase->Base.Enable          = 1;
+            }
+            if (Pwms[0].CountZeroISR.Enable && Pwms[0].CountZeroISR.Action != 0)
+            {
+                Pwms[0].CountZeroISR.Action();
+            }
+
+            Pwms[0].PwmPrdCnt2 = 1;
+        }
+        else if(Pwms[0].PwmPrdCnt2 == 2)
         {
-            Pwms[0].AdcTriggerIndex                         = 0;
-            Pwms[0].AdcTriggerTimerBase->CompareReloadInput = Pwms[0].AdcTriggerUpTicks[0];
-            Pwms[0].AdcTriggerAdcBase->Base.Enable          = 1;
+            Pwms[0].PwmPrdCnt2 = 3;
         }
-        if (Pwms[0].CountZeroISR.Enable && Pwms[0].CountZeroISR.Action != 0)
+        else 
         {
-            Pwms[0].CountZeroISR.Action();
+            // do nothing 
         }
     }
     else if (Pwms[0].PwmBase->CntDir == SIM_PWM_DIR_DOWN)
     {
-        if (Pwms[0].MultiSyncEnable == 1 && Pwms[0].AdcTriggerDownCounts > 0)
+        if(Pwms[0].PwmPrdCnt2 == 1)
         {
-            Pwms[0].AdcTriggerIndex                         = 0;
-            Pwms[0].AdcTriggerTimerBase->CompareReloadInput = Pwms[0].AdcTriggerDownTicks[0];
-            Pwms[0].AdcTriggerAdcBase->Base.Enable          = 1;
+            if (Pwms[0].MultiSyncEnable == 1 && Pwms[0].AdcTriggerDownCounts > 0)
+            {
+                Pwms[0].AdcTriggerIndex                         = 0;
+                Pwms[0].AdcTriggerTimerBase->CompareReloadInput = Pwms[0].AdcTriggerDownTicks[0];
+                Pwms[0].AdcTriggerAdcBase->Base.Enable          = 1;
+            }
+            if (Pwms[0].CountMaxISR.Enable && Pwms[0].CountMaxISR.Action != 0)
+            {
+                Pwms[0].CountMaxISR.Action();
+            }
+
+            Pwms[0].PwmPrdCnt2 = 2;
+        }
+        else if(Pwms[0].PwmPrdCnt2 == 3)
+        {
+            /* 两个pwm周期更新一次比较值 */
+            iRtPwm_ActivateCompareValues(0);
+
+            Pwms[0].PwmPrdCnt2 = 0;
         }
-        if (Pwms[0].CountMaxISR.Enable && Pwms[0].CountMaxISR.Action != 0)
+        else 
         {
-            Pwms[0].CountMaxISR.Action();
+            // do nothing 
         }
-        iRtPwm_ActivateCompareValues(0);
     }
 }

+ 2 - 0
tests/sim/sim_board/api_rt/api_rt_pwm.h

@@ -28,6 +28,8 @@ typedef struct
     uint8_t           AdcTriggerDownCounts;
     uint32_t          AdcTriggerDownTicks[8];
     struct Sim_Adc   *AdcTriggerAdcBase;
+    uint8_t           PwmPrdCnt;
+    uint8_t           PwmPrdCnt2;
 } ApiRtPwm_Handle;
 
 extern ApiRtPwm_Handle Pwms[1];

+ 1 - 0
tests/sim/sim_board/include/mcu_instance.h

@@ -5,6 +5,7 @@ extern struct Sim_Scheduler scheduler;
 extern struct Sim_Pwm       pwm;
 extern struct Sim_Adc       adc;
 extern struct Sim_Adc       adc2;
+extern struct Sim_Adc       adc3;
 extern struct Sim_Gpio      ledPin;
 extern struct Sim_Gpio      capPin;
 extern struct Sim_Gpio      breakPin;

+ 7 - 2
tests/sim/sim_board/source/board.c

@@ -7,7 +7,7 @@
 
 double BoardSampleTime       = 1e-6;
 double BoardSampleRate       = 1000000;
-int    BoardSampleTimeFactor = 64;
+int    BoardSampleTimeFactor = 72;
 
 void AppInit();
 void AppLoop();
@@ -32,6 +32,7 @@ void BoardInit(double ts)
     Sim_PwmInit(&pwm);
     Sim_AdcInit(&adc);
     Sim_AdcInit(&adc2);
+    Sim_AdcInit(&adc3);
     Sim_GpioInit(&ledPin);
     Sim_GpioInit(&capPin);
     Sim_GpioInit(&breakPin);
@@ -45,6 +46,7 @@ void BoardInit(double ts)
     scheduler.Register(&scheduler, &pwm.Base);
     scheduler.Register(&scheduler, &adc.Base);
     scheduler.Register(&scheduler, &adc2.Base);
+    scheduler.Register(&scheduler, &adc3.Base);
     scheduler.Register(&scheduler, &ledPin.Base);
     scheduler.Register(&scheduler, &capPin.Base);
     scheduler.Register(&scheduler, &breakPin.Base);
@@ -98,7 +100,7 @@ void BoardInit(double ts)
     scheduler.TriggerBind(&scheduler, &sampleTrigTimer.Base, TimerTriggerOutCCIF, &adc.Base, AdcTriggerInSample);
 
     /* 采样触发定时器配置2 */
-    sampleTrigTimer2.CountMode            = TimerCenterAlignedMode3;
+    sampleTrigTimer2.CountMode            = TimerCenterAlignedMode2;
     sampleTrigTimer2.CountDir             = TimerCountUp;
     sampleTrigTimer2.PreloadEnable        = 1;
     sampleTrigTimer2.CountReloadInput     = TICK_TRANS(HW_INIT_HPWM_PERIOD);
@@ -109,6 +111,9 @@ void BoardInit(double ts)
     scheduler.TriggerBind(&scheduler, &sampleTrigTimer2.Base, TimerTriggerOutCCIF, &adc2.Base, AdcTriggerInSample);
     sampleTrigTimer2.Base.Enable = 0;
 
+    /* 采样触发定时器配置3 */
+    scheduler.TriggerBind(&scheduler, &sampleTrigTimer2.Base, TimerTriggerOutCCIF, &adc3.Base, AdcTriggerInSample);
+
     /* Tbs定时器配置 */
     tbsTick.CountMode             = TimerEdgeAlignedMode;
     tbsTick.CountDir              = TimerCountDown;

+ 1 - 0
tests/sim/sim_board/source/mcu_instance.c

@@ -4,6 +4,7 @@ struct Sim_Scheduler scheduler;
 struct Sim_Pwm       pwm;
 struct Sim_Adc       adc;
 struct Sim_Adc       adc2;
+struct Sim_Adc       adc3;
 struct Sim_Gpio      ledPin;
 struct Sim_Gpio      capPin;
 struct Sim_Gpio      breakPin;

+ 20 - 12
tests/sim/sim_board/source/sfun_wrapper.c

@@ -11,24 +11,32 @@ void McuInit(double ts)
 void McuRun(const double *ain, const int32_t *din, const injection_dtype *ctrl, double *aout, int *dout, double *watchOut)
 {
     /* ADC模块输入 */
-    adc.ChannelInput[5] = ain[0]; // C相电压
-    adc.ChannelInput[6] = ain[1]; // B相电压
-    adc.ChannelInput[7] = ain[2]; // A相电压
-    adc.ChannelInput[3] = ain[3]; // B相电流
-    adc.ChannelInput[2] = ain[4]; // A相电流
-    adc.ChannelInput[4] = ain[5]; // 母线电压
-    adc.ChannelInput[1] = ain[6]; // 温度
-    // adc.ChannelInput[7] = ain[7]; // AC电压
-    adc.ChannelInput[7] = ain[8]; // 母线电流
-    adc.ChannelInput[0] = 0;      // 温度
+    adc.ChannelInput[0] = ain[5]; // 母线电压
+    adc.ChannelInput[1] = 0;      // 6V
+    adc.ChannelInput[2] = 0;      // 5V
+    adc.ChannelInput[3] = 1.5;    // PCB温度
+    adc.ChannelInput[4] = 1.5;    // 电机温度
+    adc.ChannelInput[4] = 0;      // 12V
+    adc.ChannelInput[4] = 0;      // 转把
+    adc.ChannelInput[4] = 1.5;    // 力矩传感器
+
+    adc2.ChannelInput[0] = ain[4];              // A相电流
+    adc2.ChannelInput[1] = ain[3];              // B相电流
+    adc2.ChannelInput[2] = 0 - ain[4] - ain[3]; // C相电流
+    adc2.ChannelInput[3] = ain[8];              // 母线电流
 
-    adc2.ChannelInput[0] = ain[8]; // 母线电流
+    // adc.ChannelInput[5] = ain[0]; // C相电压
+    // adc.ChannelInput[6] = ain[1]; // B相电压
+    // adc.ChannelInput[7] = ain[2]; // A相电压
+    // adc.ChannelInput[4] = ain[5]; // 母线电压
+    // adc.ChannelInput[1] = ain[6]; // 温度
+    // adc.ChannelInput[7] = ain[7]; // AC电压
 
     /* Break 输入 */
     breakPin.IOIn = din[0];
 
     /* Cap 输入 */
-    capPin.IOIn    = din[1];
+    capPin.IOIn = din[1];
     inputCap.PWMIn = din[1];
 
     /* 注入变量 */

+ 24 - 4
tests/sim/sim_board/xmake.lua

@@ -5,19 +5,39 @@ target("sim_sfunc")
     set_kind("shared")
     set_languages("c11", "c++20")
     add_defines("MATLAB_MEX_FILE", "_CRT_SECURE_NO_WARNINGS", "_DEBUG", "_CONSOLE")
-    add_files("$(projectdir)/app/**/*.c")
+    --add_files("$(projectdir)/app/**/*.c")
+    add_files("$(projectdir)/User project/1.FrameLayer/Source/app.c")
+    add_files("$(projectdir)/User project/1.FrameLayer/Source/FSM_1st.c")
+    add_files("$(projectdir)/User project/1.FrameLayer/Source/FSM_2nd.c")
+    add_files("$(projectdir)/User project/1.FrameLayer/Source/main.c")
+    add_files("$(projectdir)/User project/1.FrameLayer/Source/sys_task.c")
+    add_files("$(projectdir)/User project/1.FrameLayer/Source/tbc.c")
+    add_files("$(projectdir)/User project/1.FrameLayer/Source/tbs.c")
+    add_files("$(projectdir)/User project/1.FrameLayer/Source/tbt.c")
+    add_files("$(projectdir)/User project/1.FrameLayer/Source/TimeTask_Event.c")
+    add_files("$(projectdir)/User project/2.MotorDrive/Source/**.c")
+    add_files("$(projectdir)/User project/3.BasicFunction/Source/**.c")
+    add_files("$(projectdir)/User project/4.BasicHardwSoftwLayer/2.BasicSoftwLayer/Source/**.c")
     add_files("api_rt/*.c")
     add_files("peripheral/source/*.c")
     add_files("source/*.c")
+
+    add_includedirs("$(projectdir)/MSTLibraries/SelfTestLib/inc")
     add_includedirs("$(projectdir)/api")
-    add_includedirs("$(projectdir)/app/general", "$(projectdir)/app/system", "$(projectdir)/app/motor_control", "$(projectdir)/app/config", "$(projectdir)/app/tools", "$(projectdir)/app/motor_control/modules")
-    add_includedirs("$(projectdir)/app/motor_control/modules/algs")
-    add_includedirs("$(projectdir)/board/export")
+    add_includedirs("$(projectdir)/User project/1.FrameLayer/Include")
+    add_includedirs("$(projectdir)/User project/2.MotorDrive/Include")
+    add_includedirs("$(projectdir)/User project/3.BasicFunction/Include")
+    add_includedirs("$(projectdir)/User project/4.BasicHardwSoftwLayer/1.BasicHardwLayer/Include")
+    add_includedirs("$(projectdir)/User project/4.BasicHardwSoftwLayer/2.BasicSoftwLayer/Include")
+    add_includedirs("$(projectdir)/tests/unit_test")
     add_includedirs("api_rt")
     add_includedirs("include")
     add_includedirs("peripheral/include")
     add_includedirs("$(env MATLABROOT)/extern/include")
     add_includedirs("$(env MATLABROOT)/simulink/include")
+
+    add_defines("RUN_ARCH_SIM")
+
     add_linkdirs("$(env MATLABROOT)/extern/lib/win64/microsoft")
     add_links("libmex")
     add_links("libmat")

+ 0 - 0
tests/unit_test/STLmain.h


+ 2 - 2
tests/unit_test/test_brake.cpp

@@ -72,7 +72,7 @@ TEST_P(ConsVoltBrakeTest1, ConsVoltBrake)
         cvb_stBrakeIn.swIqRefPu = ((s32)iqrefIn << 14) / IBASE;         
         cvb_stBrakeIn.swSpdPu = McStatus1.Pu.swElecOmega;
         cvb_stBrakeIn.uwAngelPu = McStatus1.Pu.swElecAngle;
-        cvb_stBrakeIn.uwSpdLpfAbsPu = abs(McStatus1.Pu.swElecOmega);
+        cvb_stBrakeIn.uwSpdLpfAbsPu = ABS(McStatus1.Pu.swElecOmega);
         cvb_voBrake(&cvb_stBrakeIn,&cvb_stBrakeCoef,&cvb_stBrakeOut);
 
         double iqlim,iqrefOut,elecAngle,delta;
@@ -152,7 +152,7 @@ TEST_P(ConsVoltBrakeTest1, ConsVoltBrake)
         double idrefOut;
         if((cvb_stBrakeIn.swIqRefPu < 0 && McStatus1.Pu.swElecOmega > 0) || (cvb_stBrakeIn.swIqRefPu > 0 && McStatus1.Pu.swElecOmega < 0))
         {
-            if(abs(McStatus1.Pu.swElecOmega) < cvb_stBrakeCoef.uwLowSpdPu)
+            if(ABS(McStatus1.Pu.swElecOmega) < cvb_stBrakeCoef.uwLowSpdPu)
             {
                 idrefOut =  cvb_stBrakeIn.swIdRefPu + cvb_stBrakeCoef.swIdRcyPu;
             }

+ 3 - 3
tests/unit_test/test_cursample_calib.cpp

@@ -143,7 +143,7 @@ TEST_P(CurSampleCalibTest1, SingResCalib)
                     switch (pwm_stGenOut.uwSingelRSampleArea)
                     {
                     case SampleA:
-                        ADC_IDATA0(ADC1) = (UWORD)(adc_stCof.uwIdcOffset + abs(ia) * 100 * 4096 / ADC_IDC_CUR_MAX_AP);
+                        ADC_IDATA0(ADC1) = (UWORD)(adc_stCof.uwIdcOffset + ABS(ia) * 100 * 4096 / ADC_IDC_CUR_MAX_AP);
                         if (ADC_IDATA0(ADC1) > 4096)
                         {
                             ADC_IDATA0(ADC1) = 4096;
@@ -151,7 +151,7 @@ TEST_P(CurSampleCalibTest1, SingResCalib)
 
                         break;
                     case SampleB:
-                        ADC_IDATA0(ADC1) = (UWORD)(adc_stCof.uwIdcOffset + abs(ib) * 100 * 4096 / ADC_IDC_CUR_MAX_AP);
+                        ADC_IDATA0(ADC1) = (UWORD)(adc_stCof.uwIdcOffset + ABS(ib) * 100 * 4096 / ADC_IDC_CUR_MAX_AP);
                         if (ADC_IDATA0(ADC1) > 4096)
                         {
                             ADC_IDATA0(ADC1) = 4096;
@@ -159,7 +159,7 @@ TEST_P(CurSampleCalibTest1, SingResCalib)
 
                         break;
                     case SampleC:
-                        ADC_IDATA0(ADC1) = (UWORD)(adc_stCof.uwIdcOffset + abs(ic) * 100 * 4096 / ADC_IDC_CUR_MAX_AP);
+                        ADC_IDATA0(ADC1) = (UWORD)(adc_stCof.uwIdcOffset + ABS(ic) * 100 * 4096 / ADC_IDC_CUR_MAX_AP);
                         if (ADC_IDATA0(ADC1) > 4096)
                         {
                             ADC_IDATA0(ADC1) = 4096;

+ 2 - 2
tests/unit_test/test_pwrlim.cpp

@@ -160,8 +160,8 @@ TEST_P(PwrLimTest2, PwrLim)
         {
             asr_stSpdPIIn.swSpdRefPu = spdRefPu;            
             asr_stSpdPIIn.swSpdFdbPu = McStatus1.Pu.swElecOmega; 
-            asr_stSpdPIIn.swIqMaxPu = abs(pwr_stPwrLimOut2.swIqLimPu);
-            asr_stSpdPIIn.swIqMinPu = -abs(pwr_stPwrLimOut2.swIqLimPu);
+            asr_stSpdPIIn.swIqMaxPu = ABS(pwr_stPwrLimOut2.swIqLimPu);
+            asr_stSpdPIIn.swIqMinPu = -ABS(pwr_stPwrLimOut2.swIqLimPu);
             asr_voSpdPI(&asr_stSpdPIIn, &asr_stSpdPICoef, &asr_stSpdPIOut);
         }
 

+ 1 - 1
tests/unit_test/test_torquesensor.cpp

@@ -226,7 +226,7 @@ TEST_P(TorquePowerOnTest1, OffsetUpdate)
             offsetAvg = (offsetLast[0] + offsetLast[1] + offsetLast[2] + offsetLast[3]) / 4;
         }
 
-        if(abs((double)hw_uwADC0[7] - offsetAvg) > 200)
+        if(ABS((double)hw_uwADC0[7] - offsetAvg) > 200)
         {
             EXPECT_NEAR(torsensor_stTorSensorCof.uwTorqueOffset, offsetAvg, 2);
         }

+ 1 - 1
tests/unit_test/test_user.h

@@ -55,7 +55,7 @@ extern "C" {
 #define TIMER_CH3CV(timex) testCh3CapValue[timex]
 #define GPIO_ISTAT(gpiox)  testGpioBValue[gpiox]
 
-#define abs(x) ((x) >= 0 ? (x) : (-(x)))
+//#define ABS(x) ((x) >= 0 ? (x) : (-(x)))
 
 extern int testAdcInjectData0[2];
 extern int testAdcInjectData1[2];

+ 0 - 6
xmake.lua

@@ -23,12 +23,6 @@ target("unittest")
     add_files("User project/2.MotorDrive/Source/packed/pwrlim.c", "User project/2.MotorDrive/Source/packed/asr.c", "User project/2.MotorDrive/Source/packed/brake.c")
     add_files("User project/3.BasicFunction/Source/Temp.c")
 
-    
-    -- add_includedirs("Std project/CMSIS")
-    -- add_includedirs("Std project/CMSIS/GD/GD32F30x/Inc_GD")
-    -- add_includedirs("Std project/GD32F30x_standard_peripheral/Inc_Peri")
-    -- add_includedirs("MSTLibraries/MISC/inc")
-    -- add_includedirs("MSTLibraries/SelfTestLib/inc")
     add_includedirs("User project/1.FrameLayer/Include")
     add_includedirs("User project/2.MotorDrive/Include")
     add_includedirs("User project/3.BasicFunction/Include")