tbc.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /************************************************************************
  2. Project: Welling Motor Control Paltform
  3. Filename: tbc.c
  4. Partner Filename: tbc.h
  5. Description: Time base for current loop
  6. Complier: IAR Embedded Workbench for ARM 7.80, IAR Systems.
  7. CPU TYPE : GD32F3x0
  8. *************************************************************************
  9. Copyright (c) 2018 Welling Motor Technology(Shanghai) Co. Ltd.
  10. All rights reserved.
  11. *************************************************************************
  12. *************************************************************************
  13. Revising History (ECL of this file):
  14. ************************************************************************/
  15. /************************************************************************
  16. Beginning of File, do not put anything above here except notes
  17. Compiler Directives:
  18. *************************************************************************/
  19. #ifndef _TBC_C_
  20. #define _TBC_C_
  21. #endif
  22. /************************************************************************
  23. Included File
  24. *************************************************************************/
  25. #include "user.h"
  26. #include "FSM_1st.h"
  27. #include "FSM_2nd.h"
  28. #include "spdctrmode.h"
  29. #include "queue.h"
  30. #include "canAppl.h"
  31. #include "alarm.h"
  32. #include "STLmain.h"
  33. #include "api.h"
  34. #include "torquesensor.h"
  35. /*************************************************************************
  36. Exported Functions (N/A)
  37. *************************************************************************/
  38. /***************************************************************
  39. Function: tbc_voIsr;
  40. Description: TBC interrupt service
  41. Call by:
  42. Input Variables: N/A
  43. Output/Return Variables: N/A
  44. Subroutine Call: ...;
  45. Reference: N/A
  46. ****************************************************************/
  47. void tbc_voUpIsr(void)
  48. {
  49. /* Uart Monitor */
  50. uart_voAppMonitor();
  51. /* Motor Position Cal */
  52. if( cp_stFlg.ThetaGetModelSelect == ANG_RESOLVER )
  53. {
  54. spi_voResolverLock();
  55. spi_voResolver(&spi_stResolverCoef, &spi_stResolverOut);
  56. }
  57. else if( cp_stFlg.ThetaGetModelSelect == ANG_SWITCHHALL )
  58. {
  59. switchhall_voPosCalTbc();
  60. }
  61. else
  62. {
  63. //do noting
  64. }
  65. /* 1st FSM */
  66. FSM1st_Sys_state.Tbcup_hook();
  67. #ifdef RUN_ARCH_SIM
  68. SWORD elecOmega = 0;
  69. UWORD gearstate = 0;
  70. INJ_PT(INT16, uart_slSpdRefRpm, 4);
  71. INJ_PT(INT16, spi_stResolverOut.uwSpiThetaPu, 0);
  72. INJ_PT(INT16, elecOmega, 1);
  73. INJ_PT(INT16, gearstate, 5);
  74. MC_ControlCode.GearSt =gearstate;
  75. spi_stResolverOut.swSpdFbkPu = ((SLONG)elecOmega << 15) / cof_uwWebRadps * 10;
  76. TEST_PT(INT, scm_uwAngParkPu, 0);
  77. TEST_PT(INT, scm_swSpdRefPu, 1);
  78. TEST_PT(INT, scm_stSpdFbkLpf.slY.sw.hi, 2);
  79. TEST_PT(INT, uart_slSpdRefRpm, 3);
  80. TEST_PT(INT, alm_unCode.all, 4);
  81. TEST_PT(INT, adc_stUpOut.uwVdcLpfPu, 5);
  82. TEST_PT(INT, scm_swIqRefPu, 6);
  83. TEST_PT(INT, scm_swIqFdbLpfPu, 7);
  84. TEST_PT(INT, adc_stDownOut.slSampIaPu, 8);
  85. TEST_PT(INT, adc_stDownOut.swIaPu, 9);
  86. TEST_PT(INT, adc_stUpOut.swCalibIaPu, 10);
  87. TEST_PT(INT, crd_stCurParkOut.swQPu, 11);
  88. TEST_PT(INT, cadence_stFreGetOut.uwFrequencyPu, 12);
  89. TEST_PT(INT, bikespeed_stFreGetOut.uwFrequencyPu, 13);
  90. TEST_PT(INT, torsensor_stTorSensorOut.uwTorquePu, 14);
  91. TEST_PT(INT, cp_stBikeRunInfoPara.uwBikeGear, 15);
  92. #endif
  93. }
  94. /***************************************************************
  95. Function: tbc_voIsr;
  96. Description: TBC interrupt service
  97. Call by:
  98. Input Variables: N/A
  99. Output/Return Variables: N/A
  100. Subroutine Call: ...;
  101. Reference: N/A
  102. ****************************************************************/
  103. static BOOL tbc_pvt_blErrorFlag = FALSE;
  104. void tbc_voDownIsr(void)
  105. {
  106. /* ADC Sample */
  107. adc_voSampleUp(&adc_stCof, &adc_stUpOut);
  108. /* Alarm detect */
  109. alm_stIn.blADCInitOvrFlg = sysfsm_stFlg.blADCInitOvrFlg;
  110. alm_stIn.uwIpeakPu = adc_stDownOut.uwIpeakPu;
  111. alm_stIn.swMotorPwrInWt = scm_swMotorPwrInLpfWt;
  112. alm_stIn.uwIaAbsPu = adc_stDownOut.uwIaAbsPu;
  113. alm_stIn.uwIbAbsPu = adc_stDownOut.uwIbAbsPu;
  114. alm_stIn.uwIcAbsPu = adc_stDownOut.uwIcAbsPu;
  115. alm_stIn.swIalhpaPu = crd_stCurClarkOut.swAlphaPu;
  116. alm_stIn.swIbetaPu = crd_stCurClarkOut.swBetaPu;
  117. alm_stIn.swIqRefPu = scm_swIqRefPu;
  118. alm_stIn.uwVdcPu = adc_stUpOut.uwVdcLpfPu;
  119. alm_stIn.uwSpdFbkLpfAbsPu = scm_uwSpdFbkLpfAbsPu;
  120. if (scm_swSpdRefPu >= 0)
  121. {
  122. alm_stIn.uwSpdRefAbsPu = (UWORD)scm_swSpdRefPu;
  123. }
  124. else
  125. {
  126. alm_stIn.uwSpdRefAbsPu = (UWORD)-scm_swSpdRefPu;
  127. }
  128. alm_stIn.uwIPMTempCe = adc_stUpOut.PCBTemp;
  129. alm_stIn.uwIdcOffset = adc_stCof.uwIdcOffset;
  130. // alm_stIn.swDrumSpdAbsNowRpm = uart_swDrumSpdAbsNowRpm;
  131. // alm_stIn.swDrumSpdAbsPreRpm = uart_swDrumSpdAbsPreRpm;
  132. alm_stIn.blSpiThetaFltFlg = spi_stResolverOut.blSpiThetaFltFlg;
  133. alm_voDetecTBC(&alm_stIn, &alm_stDetectTbcCoef);
  134. /* MCU Fault Detect */
  135. if((FALSE == UART_bInsertPendTx)&&(UART_bMonSwitch == FALSE))
  136. {
  137. stl_voTbcProc();
  138. }
  139. /* Error Log */
  140. switch_flg.SysFault_Flag = alm_blAlmOccrFlg;
  141. if (switch_flg.SysFault_Flag == TRUE && tbc_pvt_blErrorFlag == FALSE)
  142. {
  143. tbc_pvt_blErrorFlag = TRUE;
  144. que_stErrorLog.ErrorCode = alm_unCode.all;
  145. que_stErrorLog.RunTime = MC_RunInfo.Ride_Time;
  146. que_stErrorLog.RunInfo = MC_RunInfo;
  147. que_stErrorLog.IqCurrentPu = scm_swIqFdbLpfPu;
  148. que_stErrorLog.IqVoltagePu = scm_swUqRefPu;
  149. que_stErrorLog.IdCurrentPu = scm_swIdFdbLpfPu;
  150. que_stErrorLog.IdVoltagePu = scm_swUdRefPu;
  151. que_ubPushIn(&que_stFlashErrorLog, &que_stErrorLog, 1);
  152. }
  153. else if(switch_flg.SysFault_Flag == FALSE)
  154. {
  155. tbc_pvt_blErrorFlag = FALSE;
  156. }
  157. else
  158. {
  159. //do nothing
  160. }
  161. /* ADC Init Finish Flag */
  162. if (adc_stDownOut.blADCCalibFlg && adc_stUpOut.blADCCalibFlg)
  163. {
  164. sysfsm_stFlg.blADCInitOvrFlg = TRUE;
  165. }
  166. else
  167. {
  168. sysfsm_stFlg.blADCInitOvrFlg = FALSE;
  169. }
  170. /* ADC Sample */
  171. adc_voSampleDown(&adc_stCof, &adc_stDownOut);
  172. /* ADC Rdson Calibration with Single Resistance*/
  173. adc_voSRCalibration(&adc_stCof, &adc_stUpOut, &adc_stDownOut);
  174. /* 1st FSM */
  175. FSM1st_Sys_state.TbcDown_hook();
  176. }
  177. /*************************************************************************
  178. Local Functions (N/A)
  179. *************************************************************************/
  180. /*************************************************************************
  181. Copyright (c) 2018 Welling Motor Technology(Shanghai) Co. Ltd.
  182. All rights reserved.
  183. *************************************************************************/
  184. #ifdef _TBC_C_
  185. #undef _TBC_C_ /* parasoft-suppress MISRA2004-19_6 "本项目中无法更改,后续避免使用" */
  186. #endif
  187. /*************************************************************************
  188. End of this File (EOF)!
  189. Do not put anything after this part!
  190. *************************************************************************/