Ver código fonte

增加powerreset状态,收到复位指令时执行软件复位;BMS通信中断时不计算续航;增加硬件ID号

Ye Jin 1 ano atrás
pai
commit
18f6b58016

+ 2 - 1
User project/3.BasicFunction/Include/power.h

@@ -56,7 +56,8 @@ typedef enum
     POWER_ON = 1,
     POWER_ON_END = 2,
     POWER_OFF = 3,
-    POWER_OFF_END = 4
+    POWER_OFF_END = 4,
+    POWER_RESET = 5
 } POWER_STATE;
 
 /**

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

@@ -656,13 +656,21 @@ void DataProcess(UWORD ID, UBYTE Mode, UWORD Cmd, UBYTE Data[]) /* parasoft-supp
         }
         case 0x2505: //复位指令
         {
-            if (strncmp("RESET", (char *)Data, DataLength) == 0)
+            if (strncmp("OFF..", (char *)Data, DataLength) == 0)
             {
                 //执行关机,需硬件重启
                 power_stPowStateOut.powerstate = POWER_OFF;
                 power_stPowStateOut.blPowerStartupFlg = FALSE;
                 PowerOffDTimeOut = cp_ulSystickCnt;
-                SendData(ID_MC_TO_CDL, MODE_REPORT, 0xA903, (UBYTE *)"ACK");
+                SendData(ID_MC_TO_CDL, MODE_REPORT, 0xA903, (uint8_t *)"ACK");
+            }
+            else if (strncmp("RESET", (char *)Data, DataLength) == 0)
+            {
+                //执行复位,跳转进入Bootloader
+                power_stPowStateOut.powerstate = POWER_RESET;
+                power_stPowStateOut.blPowerStartupFlg = FALSE;
+                PowerOffDTimeOut = cp_ulSystickCnt;
+                SendData(ID_MC_TO_CDL, MODE_REPORT, 0xA903, (uint8_t *)"ACK");
             }
             break;
         }

+ 121 - 108
User project/3.BasicFunction/Source/canAppl.c

@@ -56,6 +56,7 @@ UBYTE                    MC_MotorSPD_rpm_Percent = 0;
 UBYTE                    MC_WorkMode;
 UBYTE                    MC_BC_COM = 0;
 static LPF_OUT                    BMS_swCurIdcLpf;
+static BOOL blBMSCommFault = FALSE;
 
 void Can_voUpdateMC_UpcInfo(void)
 {
@@ -209,19 +210,27 @@ void Can_voUpdateMC_UpcInfo(void)
 static UWORD SizeMCUP;
 void Can_voInitMC_Run(void)
 {
+    UBYTE MCU_ID[12];
+    ULONG MCU_ID_CRC32;
     flash_voRead();
     // Hardware version
 #if(MOTOR_ID_SEL == MOTOR_WELLING_CITY_36V)
-	strncpy(MC_VerInfo.HW_Version, (char *)"CITY-36V.       ", 16);
+	strncpy(MC_VerInfo.HW_Version, (char *)"CITY-36V", 8);
 #elif(MOTOR_ID_SEL == MOTOR_WELLING_CITY_48V)
-	strncpy(MC_VerInfo.HW_Version, (char *)"CITY-48V.       ", 16);
+	strncpy(MC_VerInfo.HW_Version, (char *)"CITY-48V", 8);
 #elif(MOTOR_ID_SEL == MOTOR_WELLING_MTB_36V)
-	strncpy(MC_VerInfo.HW_Version, (char *)"MTB-36V.        ", 16);
+	strncpy(MC_VerInfo.HW_Version, (char *)"MTB-36V.", 8);
 #elif(MOTOR_ID_SEL == MOTOR_WELLING_MTB_48V)
-	strncpy(MC_VerInfo.HW_Version, (char *)"MTB-48V.        ", 16);
+	strncpy(MC_VerInfo.HW_Version, (char *)"MTB-48V.", 8);
 #else
-	strncpy(MC_VerInfo.HW_Version, (char *)"TEST.           ", 16);
+	strncpy(MC_VerInfo.HW_Version, (char *)"TEST.   ", 8);
 #endif
+    
+    memcpy((uint8_t*)MCU_ID, (uint8_t*)(HW_ID_START_ADDRESS), 12);
+    memcpy((uint8_t*)(MC_VerInfo.HW_Version + 8), (uint8_t*)(MCU_ID), 4);
+    MCU_ID_CRC32 = CRC32_Calculate(MCU_ID,12);
+    memcpy((uint8_t*)(MC_VerInfo.HW_Version + 12), (uint8_t*)(&MCU_ID_CRC32), 4);
+    
     // Software version
     char chFwVersion[16]="V0r1r10_";
     strncat(chFwVersion,COMMIT_TIME,9);
@@ -248,7 +257,7 @@ void Can_voInitMC_Run(void)
     mth_voLPFilterCoef(1000000 / 100, EVENT_1MS_HZ, &BMS_swCurIdcLpf.uwKx); // 100Hz
 }
 
-void Can_voMC_Run_1ms(void) /* parasoft-suppress METRICS-28 "本项目圈复杂度无法更改,后续避免" */
+void Can_voMC_Run_1ms(void) /* parasoft-suppress METRICS-28 "閺堫剟銆嶉惄顔兼箑婢跺秵娼呮惔锔芥¥濞夋洘娲块弨鐧哥礉閸氬海鐢婚柆鍨�帳" */
 {
     if (cp_stBikeRunInfoPara.BikeSpeedKmH > 30)
     {
@@ -537,6 +546,12 @@ void Can_voMC_Run_5ms(void)
 void Can_voMC_Run_200ms(void)
 {
     UWORD TempPower;
+    
+    if((cp_ulSystickCnt - ulBMS_ComTimeOutCount) > 3000)
+    {
+        blBMSCommFault = TRUE;       // communication abnormal
+    }
+    
     Can_GearSt_switch();
     SizeMCUP = sizeof(MC_UpcInfo.stAssistInfo);
     if (MC_BC_COM == 1)
@@ -561,8 +576,8 @@ void Can_voMC_Run_200ms(void)
         SendData(ID_MC_TO_CDL, MODE_REPORT, 0xBA20, (UBYTE *)&MC_RunInfoToCDL.SysStatus);
     }
 
-    MC_RunInfo.BikeSpeed = cp_stBikeRunInfoPara.BikeSpeedKmH;                ///>车速 0.1km/h,地址偏移0
-    MC_RunInfo.MotorSpeed = (SWORD)((SLONG)scm_uwSpdFbkLpfAbsPu * (SLONG)cof_uwVbRpm >> 15); ///>输出转速 1rpm,地址偏移2
+    MC_RunInfo.BikeSpeed = cp_stBikeRunInfoPara.BikeSpeedKmH;                ///>鏉烇箓鈧�?0.1km/h閿涘苯婀撮崸鈧�崑蹇曅�0
+    MC_RunInfo.MotorSpeed = (SWORD)((SLONG)scm_uwSpdFbkLpfAbsPu * (SLONG)cof_uwVbRpm >> 15); ///>鏉堟挸鍤�潪顒勨偓?1rpm閿涘苯婀撮崸鈧�崑蹇曅�2
     TempPower = scm_swMotorPwrInLpfWt;
     if (TempPower > 5000)
     {
@@ -589,26 +604,26 @@ void Can_voMC_Run_200ms(void)
       MC_RunInfo.Power = (PowerFlt < 0) ? 0 : (PowerFlt) >> 10;
     }while(0);
     
-    //MC_RunInfo.Power = TempPower / 10;                                                                    ///>电功率 1W,地址偏移4
+    //MC_RunInfo.Power = TempPower / 10;                                                                    ///>閻㈤潧濮涢悳?1W閿涘苯婀撮崸鈧�崑蹇曅�4
     //MC_RunInfo.Power = (scm_swIqFdbLpfPu * 250 * cof_uwIbAp / 1500) >> 14;                                      //MaxIq = 15A,MaxPower=250W
-    MC_RunInfo.BusVoltage = (UWORD)((((ULONG)adc_stUpOut.uwVdcLpfPu + (ULONG)BMS_VoltEstimat.uwVdcCompPu) * cof_uwUbVt * 100) >> 14); ///>母线电压 1mV,地址偏移6
-    //MC_RunInfo.BusCurrent = (UWORD)((ULONG)BMS_VoltEstimat.swIdcPu * cof_uwIbAp * 100 >> 14);                                  ///>母线电流 1mA,地址偏移8
+    MC_RunInfo.BusVoltage = (UWORD)((((ULONG)adc_stUpOut.uwVdcLpfPu + (ULONG)BMS_VoltEstimat.uwVdcCompPu) * cof_uwUbVt * 100) >> 14); ///>濮e秶鍤庨悽闈涘竾 1mV閿涘苯婀撮崸鈧�崑蹇曅�6
+    //MC_RunInfo.BusCurrent = (UWORD)((ULONG)BMS_VoltEstimat.swIdcPu * cof_uwIbAp * 100 >> 14);                                  ///>濮e秶鍤庨悽鍨�ウ 1mA閿涘苯婀撮崸鈧�崑蹇曅�8
     MC_RunInfo.BusCurrent = (scm_swIqFdbLpfPu * cof_uwIbAp * 10) >> 14;
-    MC_RunInfo.Cadence = (UBYTE)(((ULONG)cadence_stFreGetOut.uwLPFFrequencyPu * cof_uwFbHz * 60) >> 20);                              ///>踏频 1rpm,地址偏移10
-    MC_RunInfo.Torque = (UBYTE)(((ULONG)torsensor_stTorSensorOut.uwTorqueLPFPu * cof_uwTorqNm / 10) >> 14);                    ///>踩踏力矩 1Nm,地址偏移11
-    MC_RunInfo.CadenceDir = (MC_CadenceDir_Struct_t)cadence_stFreGetOut.cadence_dir;                                  ///>踩踏方向 0-正,1-反,2-停止,地址偏移12
-    MC_RunInfo.GearSt = (UBYTE)MC_ControlCode.GearSt;                                                                        ///>助力档位,地址偏移13
-    MC_RunInfo.LightSwitch = MC_ControlCode.LightSwitch;                                                              ///>灯开关 0xF0-关,0xF1-开,地址偏移14
-    MC_RunInfo.SOC = (UBYTE)Can_SOC_Cal();                                                                                   ///>剩余电量 1%,地址偏移15
+    MC_RunInfo.Cadence = (UBYTE)(((ULONG)cadence_stFreGetOut.uwLPFFrequencyPu * cof_uwFbHz * 60) >> 20);                              ///>闊�繘顣� 1rpm閿涘苯婀撮崸鈧�崑蹇曅�10
+    MC_RunInfo.Torque = (UBYTE)(((ULONG)torsensor_stTorSensorOut.uwTorqueLPFPu * cof_uwTorqNm / 10) >> 14);                    ///>闊�晞绗嶉崝娑氱叐 1Nm閿涘苯婀撮崸鈧�崑蹇曅�11
+    MC_RunInfo.CadenceDir = (MC_CadenceDir_Struct_t)cadence_stFreGetOut.cadence_dir;                                  ///>闊�晞绗嶉弬鐟版倻 0-濮�?1-閸�?2-閸嬫粍顒涢敍灞芥勾閸р偓閸嬪繒些12
+    MC_RunInfo.GearSt = (UBYTE)MC_ControlCode.GearSt;                                                                        ///>閸斺晛濮忓�锝勭秴閿涘苯婀撮崸鈧�崑蹇曅�13
+    MC_RunInfo.LightSwitch = MC_ControlCode.LightSwitch;                                                              ///>閻忣垰绱戦崗?0xF0-閸忕�绱�0xF1-瀵�偓閿涘苯婀撮崸鈧�崑蹇曅�14
+    MC_RunInfo.SOC = (UBYTE)Can_SOC_Cal();                                                                                   ///>閸撯晙缍戦悽鐢稿櫤 1%閿涘苯婀撮崸鈧�崑蹇曅�15
     Can_Trip_Cal();
-    MC_RunInfo.ODO_Km = (UWORD)(MC_RideLog.ODO_Km / 10);                                                                       ///>总里程 1km,地址偏移18
+    MC_RunInfo.ODO_Km = (UWORD)(MC_RideLog.ODO_Km / 10);                                                                       ///>閹��鍣风粙?1km閿涘苯婀撮崸鈧�崑蹇曅�18
 
     if (cp_stBikeRunInfoPara.blGearStUpdate)
     {
         if((cp_stBikeRunInfoPara.uwBikeGear > 0) && (cp_stBikeRunInfoPara.uwBikeGear <= 5))
         {
             MC_RunInfo.PowerPerKm =
-                    (UBYTE)(*(&cp_stHistoryPara.uwG1AvgPwrConsumption + (cp_stBikeRunInfoPara.uwBikeGear - 1)) / 10); ///>骞冲潎鍔熻€� 0.01Ah/km 锛屽湴鍧€鍋忕Щ20 
+                    (UBYTE)(*(&cp_stHistoryPara.uwG1AvgPwrConsumption + (cp_stBikeRunInfoPara.uwBikeGear - 1)) / 10); ///>妤犵偛鍟垮�搴ㄥ礉閻旂儵鍋�?0.01Ah/km 闁挎稑鑻�﹢鎾�锤閳ь剟宕戣箛鏇�?0 
             MC_RunInfo.RemainDistance = BMS_RunInfo.RC / MC_RunInfo.PowerPerKm / 10;
         }
         else
@@ -624,10 +639,10 @@ void Can_voMC_Run_200ms(void)
         cp_stBikeRunInfoPara.uwAvePowerCNT = 0;
         cp_stBikeRunInfoPara.uwCruisDis = 0;
     }
-    MC_RunInfo.T_PCB = (UBYTE)adc_stUpOut.PCBTemp + (UBYTE)40;                    ///>PCB温度 +40℃,地址偏移21
-    MC_RunInfo.T_Coil = (UBYTE)adc_stUpOut.PCBTemp + (UBYTE)40;                   ///>绕组温度 +40℃,地址偏移22
-    MC_RunInfo.T_MCU = (UBYTE)adc_stUpOut.PCBTemp + (UBYTE)40;                    ///>MCU温度 +40℃,地址偏移23
-    MC_RunInfo.Ride_Time = (UWORD)(cp_stBikeRunInfoPara.ulRiTime >> 10);     ///>开机后骑行时间 1s,地址偏移26
+    MC_RunInfo.T_PCB = (UBYTE)adc_stUpOut.PCBTemp + (UBYTE)40;                    ///>PCB濞撯晛瀹� +40閳╁喛绱濋崷鏉挎絻閸嬪繒些21
+    MC_RunInfo.T_Coil = (UBYTE)adc_stUpOut.PCBTemp + (UBYTE)40;                   ///>缂佹洜绮嶅〒鈺佸� +40閳╁喛绱濋崷鏉挎絻閸嬪繒些22
+    MC_RunInfo.T_MCU = (UBYTE)adc_stUpOut.PCBTemp + (UBYTE)40;                    ///>MCU濞撯晛瀹� +40閳╁喛绱濋崷鏉挎絻閸嬪繒些23
+    MC_RunInfo.Ride_Time = (UWORD)(cp_stBikeRunInfoPara.ulRiTime >> 10);     ///>瀵�偓閺堝搫鎮楁�鎴n攽閺冨爼妫� 1s閿涘苯婀撮崸鈧�崑蹇曅�26
 
     if (MC_WorkMode == 1)
     {
@@ -655,89 +670,92 @@ void Can_Trip_Cal(void)
 
 void Can_RemainTrip_Cal(void)
 {
-    UWORD uwCruisCoef;                         //拧艢艧脛膸木臉媒Q12
-    cp_stBikeRunInfoPara.uwCruisDis++;        // 0.1 km
-    if (cp_stBikeRunInfoPara.uwCruisDis >= 5) // 5 = 0.5km
+    if(blBMSCommFault == FALSE)
     {
-        cp_stBikeRunInfoPara.uwCruisDis = 0;
-        cp_stBikeRunInfoPara.BMSRestChargeNow = BMS_RunInfo.RC;
-        cp_stBikeRunInfoPara.uwPowerPerKm = (cp_stBikeRunInfoPara.BMSRestChargeLast - cp_stBikeRunInfoPara.BMSRestChargeNow)
-                                            << 1; // BMS_RunInfo.RC   (BMSRestChargeLast - BMSRestChargeNow) /0.5 km
-        cp_stBikeRunInfoPara.BMSRestChargeLast = cp_stBikeRunInfoPara.BMSRestChargeNow;
-        cp_stBikeRunInfoPara.uwPowerPerKmSum -= cp_stBikeRunInfoPara.uwPowerPerKmBuf[cp_stBikeRunInfoPara.uwAvePowerCNT];
-        cp_stBikeRunInfoPara.uwPowerPerKmBuf[cp_stBikeRunInfoPara.uwAvePowerCNT] = cp_stBikeRunInfoPara.uwPowerPerKm;
-        cp_stBikeRunInfoPara.uwPowerPerKmSum += cp_stBikeRunInfoPara.uwPowerPerKmBuf[cp_stBikeRunInfoPara.uwAvePowerCNT];
-        if (cp_stBikeRunInfoPara.uwPowerPerKmBuf[31] == 0)
-        {
-            cp_stBikeRunInfoPara.uwAvePowerPerKm = cp_stBikeRunInfoPara.uwPowerPerKmSum / (cp_stBikeRunInfoPara.uwAvePowerCNT + 1);
-        }
-        else
-        {
-            cp_stBikeRunInfoPara.uwAvePowerPerKm = cp_stBikeRunInfoPara.uwPowerPerKmSum >> 5;
-        }
-        cp_stBikeRunInfoPara.uwAvePowerCNT++;
-        if (cp_stBikeRunInfoPara.uwAvePowerCNT >= 32)
-        {
-            cp_stBikeRunInfoPara.uwAvePowerCNT = 0;
-        }
-
-        if(cp_stBikeRunInfoPara.uwAvePowerPerKm > 0)
+        UWORD uwCruisCoef;                         //閹费嗗⒎閼寡嗗壘閼跺憡婀�懛澶婄崯Q12
+        cp_stBikeRunInfoPara.uwCruisDis++;        // 0.1 km
+        if (cp_stBikeRunInfoPara.uwCruisDis >= 5) // 5 = 0.5km
         {
-            if (cp_stBikeRunInfoPara.uwBikeGear == 1)
-            {
-                uwCruisCoef = (UWORD)(((ULONG)cp_stBikeRunInfoPara.uwAvePowerPerKm << 12) / cp_stHistoryPara.uwG1AvgPwrConsumption);
-                cp_stHistoryPara.uwG1AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG1AvgPwrConsumption * uwCruisCoef >> 12);
-                cp_stHistoryPara.uwG2AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG2AvgPwrConsumption * uwCruisCoef >> 12);
-                cp_stHistoryPara.uwG3AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG3AvgPwrConsumption * uwCruisCoef >> 12);
-                cp_stHistoryPara.uwG4AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG4AvgPwrConsumption * uwCruisCoef >> 12);
-                cp_stHistoryPara.uwG5AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG5AvgPwrConsumption * uwCruisCoef >> 12);
-                MC_RunInfo.PowerPerKm = (UBYTE)(cp_stHistoryPara.uwG1AvgPwrConsumption / 10);
-            }
-            else if (cp_stBikeRunInfoPara.uwBikeGear == 2)
-            {
-                uwCruisCoef = (UWORD)(((ULONG)cp_stBikeRunInfoPara.uwAvePowerPerKm << 12) / cp_stHistoryPara.uwG2AvgPwrConsumption);
-                cp_stHistoryPara.uwG1AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG1AvgPwrConsumption * uwCruisCoef >> 12);
-                cp_stHistoryPara.uwG2AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG2AvgPwrConsumption * uwCruisCoef >> 12);
-                cp_stHistoryPara.uwG3AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG3AvgPwrConsumption * uwCruisCoef >> 12);
-                cp_stHistoryPara.uwG4AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG4AvgPwrConsumption * uwCruisCoef >> 12);
-                cp_stHistoryPara.uwG5AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG5AvgPwrConsumption * uwCruisCoef >> 12);
-                MC_RunInfo.PowerPerKm = (UBYTE)(cp_stHistoryPara.uwG2AvgPwrConsumption / 10);
-            }
-            else if (cp_stBikeRunInfoPara.uwBikeGear == 3)
+            cp_stBikeRunInfoPara.uwCruisDis = 0;
+            cp_stBikeRunInfoPara.BMSRestChargeNow = BMS_RunInfo.RC;
+            cp_stBikeRunInfoPara.uwPowerPerKm = (cp_stBikeRunInfoPara.BMSRestChargeLast - cp_stBikeRunInfoPara.BMSRestChargeNow)
+                                                << 1; // BMS_RunInfo.RC   (BMSRestChargeLast - BMSRestChargeNow) /0.5 km
+            cp_stBikeRunInfoPara.BMSRestChargeLast = cp_stBikeRunInfoPara.BMSRestChargeNow;
+            cp_stBikeRunInfoPara.uwPowerPerKmSum -= cp_stBikeRunInfoPara.uwPowerPerKmBuf[cp_stBikeRunInfoPara.uwAvePowerCNT];
+            cp_stBikeRunInfoPara.uwPowerPerKmBuf[cp_stBikeRunInfoPara.uwAvePowerCNT] = cp_stBikeRunInfoPara.uwPowerPerKm;
+            cp_stBikeRunInfoPara.uwPowerPerKmSum += cp_stBikeRunInfoPara.uwPowerPerKmBuf[cp_stBikeRunInfoPara.uwAvePowerCNT];
+            if (cp_stBikeRunInfoPara.uwPowerPerKmBuf[31] == 0)
             {
-                uwCruisCoef = (UWORD)(((ULONG)cp_stBikeRunInfoPara.uwAvePowerPerKm << 12) / cp_stHistoryPara.uwG3AvgPwrConsumption);
-                cp_stHistoryPara.uwG1AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG1AvgPwrConsumption * uwCruisCoef >> 12);
-                cp_stHistoryPara.uwG2AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG2AvgPwrConsumption * uwCruisCoef >> 12);
-                cp_stHistoryPara.uwG3AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG3AvgPwrConsumption * uwCruisCoef >> 12);
-                cp_stHistoryPara.uwG4AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG4AvgPwrConsumption * uwCruisCoef >> 12);
-                cp_stHistoryPara.uwG5AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG5AvgPwrConsumption * uwCruisCoef >> 12);
-                MC_RunInfo.PowerPerKm = (UBYTE)(cp_stHistoryPara.uwG3AvgPwrConsumption / 10);
+                cp_stBikeRunInfoPara.uwAvePowerPerKm = cp_stBikeRunInfoPara.uwPowerPerKmSum / (cp_stBikeRunInfoPara.uwAvePowerCNT + 1);
             }
-            else if (cp_stBikeRunInfoPara.uwBikeGear == 4)
+            else
             {
-                uwCruisCoef = (UWORD)(((ULONG)cp_stBikeRunInfoPara.uwAvePowerPerKm << 12) / cp_stHistoryPara.uwG4AvgPwrConsumption);
-                cp_stHistoryPara.uwG1AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG1AvgPwrConsumption * uwCruisCoef >> 12);
-                cp_stHistoryPara.uwG2AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG2AvgPwrConsumption * uwCruisCoef >> 12);
-                cp_stHistoryPara.uwG3AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG3AvgPwrConsumption * uwCruisCoef >> 12);
-                cp_stHistoryPara.uwG4AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG4AvgPwrConsumption * uwCruisCoef >> 12);
-                cp_stHistoryPara.uwG5AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG5AvgPwrConsumption * uwCruisCoef >> 12);
-                MC_RunInfo.PowerPerKm = (UBYTE)(cp_stHistoryPara.uwG4AvgPwrConsumption / 10);
+                cp_stBikeRunInfoPara.uwAvePowerPerKm = cp_stBikeRunInfoPara.uwPowerPerKmSum >> 5;
             }
-            else if (cp_stBikeRunInfoPara.uwBikeGear == 5)
+            cp_stBikeRunInfoPara.uwAvePowerCNT++;
+            if (cp_stBikeRunInfoPara.uwAvePowerCNT >= 32)
             {
-                uwCruisCoef = (UWORD)(((ULONG)cp_stBikeRunInfoPara.uwAvePowerPerKm << 12) / cp_stHistoryPara.uwG5AvgPwrConsumption);
-                cp_stHistoryPara.uwG1AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG1AvgPwrConsumption * uwCruisCoef >> 12);
-                cp_stHistoryPara.uwG2AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG2AvgPwrConsumption * uwCruisCoef >> 12);
-                cp_stHistoryPara.uwG3AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG3AvgPwrConsumption * uwCruisCoef >> 12);
-                cp_stHistoryPara.uwG4AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG4AvgPwrConsumption * uwCruisCoef >> 12);
-                cp_stHistoryPara.uwG5AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG5AvgPwrConsumption * uwCruisCoef >> 12);
-                MC_RunInfo.PowerPerKm = (UBYTE)(cp_stHistoryPara.uwG5AvgPwrConsumption / 10);
+                cp_stBikeRunInfoPara.uwAvePowerCNT = 0;
             }
-            else
+
+            if(cp_stBikeRunInfoPara.uwAvePowerPerKm > 0)
             {
-                uwCruisCoef = 4096;
+                if (cp_stBikeRunInfoPara.uwBikeGear == 1)
+                {
+                    uwCruisCoef = (UWORD)(((ULONG)cp_stBikeRunInfoPara.uwAvePowerPerKm << 12) / cp_stHistoryPara.uwG1AvgPwrConsumption);
+                    cp_stHistoryPara.uwG1AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG1AvgPwrConsumption * uwCruisCoef >> 12);
+                    cp_stHistoryPara.uwG2AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG2AvgPwrConsumption * uwCruisCoef >> 12);
+                    cp_stHistoryPara.uwG3AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG3AvgPwrConsumption * uwCruisCoef >> 12);
+                    cp_stHistoryPara.uwG4AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG4AvgPwrConsumption * uwCruisCoef >> 12);
+                    cp_stHistoryPara.uwG5AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG5AvgPwrConsumption * uwCruisCoef >> 12);
+                    MC_RunInfo.PowerPerKm = (UBYTE)(cp_stHistoryPara.uwG1AvgPwrConsumption / 10);
+                }
+                else if (cp_stBikeRunInfoPara.uwBikeGear == 2)
+                {
+                    uwCruisCoef = (UWORD)(((ULONG)cp_stBikeRunInfoPara.uwAvePowerPerKm << 12) / cp_stHistoryPara.uwG2AvgPwrConsumption);
+                    cp_stHistoryPara.uwG1AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG1AvgPwrConsumption * uwCruisCoef >> 12);
+                    cp_stHistoryPara.uwG2AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG2AvgPwrConsumption * uwCruisCoef >> 12);
+                    cp_stHistoryPara.uwG3AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG3AvgPwrConsumption * uwCruisCoef >> 12);
+                    cp_stHistoryPara.uwG4AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG4AvgPwrConsumption * uwCruisCoef >> 12);
+                    cp_stHistoryPara.uwG5AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG5AvgPwrConsumption * uwCruisCoef >> 12);
+                    MC_RunInfo.PowerPerKm = (UBYTE)(cp_stHistoryPara.uwG2AvgPwrConsumption / 10);
+                }
+                else if (cp_stBikeRunInfoPara.uwBikeGear == 3)
+                {
+                    uwCruisCoef = (UWORD)(((ULONG)cp_stBikeRunInfoPara.uwAvePowerPerKm << 12) / cp_stHistoryPara.uwG3AvgPwrConsumption);
+                    cp_stHistoryPara.uwG1AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG1AvgPwrConsumption * uwCruisCoef >> 12);
+                    cp_stHistoryPara.uwG2AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG2AvgPwrConsumption * uwCruisCoef >> 12);
+                    cp_stHistoryPara.uwG3AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG3AvgPwrConsumption * uwCruisCoef >> 12);
+                    cp_stHistoryPara.uwG4AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG4AvgPwrConsumption * uwCruisCoef >> 12);
+                    cp_stHistoryPara.uwG5AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG5AvgPwrConsumption * uwCruisCoef >> 12);
+                    MC_RunInfo.PowerPerKm = (UBYTE)(cp_stHistoryPara.uwG3AvgPwrConsumption / 10);
+                }
+                else if (cp_stBikeRunInfoPara.uwBikeGear == 4)
+                {
+                    uwCruisCoef = (UWORD)(((ULONG)cp_stBikeRunInfoPara.uwAvePowerPerKm << 12) / cp_stHistoryPara.uwG4AvgPwrConsumption);
+                    cp_stHistoryPara.uwG1AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG1AvgPwrConsumption * uwCruisCoef >> 12);
+                    cp_stHistoryPara.uwG2AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG2AvgPwrConsumption * uwCruisCoef >> 12);
+                    cp_stHistoryPara.uwG3AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG3AvgPwrConsumption * uwCruisCoef >> 12);
+                    cp_stHistoryPara.uwG4AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG4AvgPwrConsumption * uwCruisCoef >> 12);
+                    cp_stHistoryPara.uwG5AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG5AvgPwrConsumption * uwCruisCoef >> 12);
+                    MC_RunInfo.PowerPerKm = (UBYTE)(cp_stHistoryPara.uwG4AvgPwrConsumption / 10);
+                }
+                else if (cp_stBikeRunInfoPara.uwBikeGear == 5)
+                {
+                    uwCruisCoef = (UWORD)(((ULONG)cp_stBikeRunInfoPara.uwAvePowerPerKm << 12) / cp_stHistoryPara.uwG5AvgPwrConsumption);
+                    cp_stHistoryPara.uwG1AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG1AvgPwrConsumption * uwCruisCoef >> 12);
+                    cp_stHistoryPara.uwG2AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG2AvgPwrConsumption * uwCruisCoef >> 12);
+                    cp_stHistoryPara.uwG3AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG3AvgPwrConsumption * uwCruisCoef >> 12);
+                    cp_stHistoryPara.uwG4AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG4AvgPwrConsumption * uwCruisCoef >> 12);
+                    cp_stHistoryPara.uwG5AvgPwrConsumption = (UWORD)((ULONG)cp_stHistoryPara.uwG5AvgPwrConsumption * uwCruisCoef >> 12);
+                    MC_RunInfo.PowerPerKm = (UBYTE)(cp_stHistoryPara.uwG5AvgPwrConsumption / 10);
+                }
+                else
+                {
+                    uwCruisCoef = 4096;
+                }
+                MC_RunInfo.RemainDistance = BMS_RunInfo.RC / MC_RunInfo.PowerPerKm / 10;
             }
-            MC_RunInfo.RemainDistance = BMS_RunInfo.RC / MC_RunInfo.PowerPerKm / 10;
         }
     }
 }
@@ -752,22 +770,17 @@ static SLONG slBMSMinVol = 41600;
 static SWORD SOC_Cnt, SOC_Value, SOC_MIN;
 static ULONG Voltage_Sum;
 static BOOL  blSOCfirstSet = FALSE;
-static BOOL  blSOCSWitch = FALSE;
-UWORD        Can_SOC_Cal(void)
+
+UWORD Can_SOC_Cal(void)
 {
     UWORD templenght, VoltageAvg = 0;
-    //开机1s等待电压稳定后初始化电量
+    //瀵�偓閺�?s缁涘�绶熼悽闈涘竾缁嬪啿鐣鹃崥搴″灥婵��瀵查悽鐢稿櫤
     if (cp_ulSystickCnt < 1000)
     {
         return (UWORD)0;
     }
-    //开机初始化一次电量
-    if((cp_ulSystickCnt - ulBMS_ComTimeOutCount) > 3000)
-    {
-      blSOCSWitch = TRUE;       // communication abnormal
-    }
     
-    if(blSOCSWitch == FALSE)
+    if(blBMSCommFault == FALSE)
     {
         SOC_Value = BMS_RunInfo.SOC;                      
     }
@@ -795,7 +808,7 @@ UWORD        Can_SOC_Cal(void)
         }
         blSOCfirstSet = TRUE;
     }
-    //1min更新一次电量
+    //1min閺囧瓨鏌婃稉鈧�▎锛勬暩闁�?
     else
     {
         templenght = 300; // 60s in 200ms time task
@@ -845,14 +858,14 @@ void Can_GearSt_switch(void)
 {
     cp_stBikeRunInfoPara.uwBikeGearLast = cp_stBikeRunInfoPara.uwBikeGear;
 
-    if (MC_WorkMode == 1) // 配置模式不自动关闭助力
+    if (MC_WorkMode == 1) // 闁板秶鐤嗗Ο鈥崇础娑撳秷鍤滈崝銊ュ彠闂傤厼濮�崝?
     {
         ulOBC_ComTimeOutCount = cp_ulSystickCnt;
     }
 #ifdef RUN_ARCH_SIM 
     ulOBC_ComTimeOutCount = cp_ulSystickCnt;
 #endif
-    if ((cp_ulSystickCnt - ulOBC_ComTimeOutCount) < 3000) // 控制器与仪表通信中断超过3s,关闭助力
+    if ((cp_ulSystickCnt - ulOBC_ComTimeOutCount) < 3000) // 閹貉冨煑閸c劋绗屾禒顏囥€冮柅姘�繆娑擃厽鏌囩搾鍛扮箖3s閿涘苯鍙ч梻顓炲И閸�?
     {
         if (MC_ControlCode.GearSt <= 0x05)
         {

+ 7 - 0
User project/3.BasicFunction/Source/power.c

@@ -164,6 +164,13 @@ void power_voPowerManagement(UWORD uwPowerPara, ULONG SysTickCnt, ULONG OBC_Butt
         {   
             break;
         }
+        case POWER_RESET:
+        {
+            //Finish save and wait 1s, or delay for 3s
+            __set_FAULTMASK(1);//关闭所有中断
+            NVIC_SystemReset();        
+            break;
+        }
         default:break;
     }
 }

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

@@ -27,6 +27,10 @@
 #define HW_RAM_START_ADDRESS 0x20000000UL
 #define HW_RAM_END_ADDRESS   0x20017fffUL
 
+/* ---------------------------------- 系统ID--------------------------------- */
+
+#define HW_ID_START_ADDRESS 0x1FFFF7ACUL
+
 /* ---------------------------------- MCU Frequency ---------------------------------- */
 
 #define HW_CLK_CORE_KHZ 72000