tbc.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 "syspar.h"
  26. #include "user.h"
  27. #include "FSM_1st.h"
  28. #include "FSM_2nd.h"
  29. #include "spdctrmode.h"
  30. #include "queue.h"
  31. #include "canAppl.h"
  32. #include "alarm.h"
  33. /*************************************************************************
  34. Exported Functions (N/A)
  35. *************************************************************************/
  36. /***************************************************************
  37. Function: tbc_voIsr;
  38. Description: TBC interrupt service
  39. Call by:
  40. Input Variables: N/A
  41. Output/Return Variables: N/A
  42. Subroutine Call: ...;
  43. Reference: N/A
  44. ****************************************************************/
  45. void tbc_voUpIsr(void)
  46. {
  47. /* Uart Monitor */
  48. uart_voAppMonitor();
  49. if( cp_stFlg.ThetaGetModelSelect == ANG_RESOLVER )
  50. {
  51. /*Resolver lock data*/
  52. spi_voResolverLock();
  53. /* Resolver data read */
  54. spi_voResolver(&spi_stResolverCoef, &spi_stResolverOut);
  55. }
  56. else if( cp_stFlg.ThetaGetModelSelect == ANG_SWITCHHALL )
  57. {
  58. /*Switchhall Position Cal*/
  59. switchhall_voPosCalTbc();
  60. }
  61. else
  62. {
  63. //do nothing
  64. }
  65. FSM1st_Sys_state.Tbcup_hook();
  66. #ifdef RUN_ARCH_SIM
  67. SWORD elecOmega = 0;
  68. INJ_PT(INT16, uart_slSpdRefRpm, 4);
  69. INJ_PT(INT16, spi_stResolverOut.uwSpiThetaPu, 0);
  70. INJ_PT(INT16, elecOmega, 1);
  71. spi_stResolverOut.swSpdFbkPu = ((SLONG)elecOmega << 15) / cof_uwWebRadps * 10;
  72. TEST_PT(INT, scm_uwAngParkPu, 0);
  73. TEST_PT(INT, scm_swSpdRefPu, 1);
  74. TEST_PT(INT, scm_stSpdFbkLpf.slY.sw.hi, 2);
  75. TEST_PT(INT, uart_slSpdRefRpm, 3);
  76. TEST_PT(INT, alm_unCode.all, 4);
  77. TEST_PT(INT, adc_stUpOut.uwVdcLpfPu, 5);
  78. TEST_PT(INT, scm_swIqRefPu, 6);
  79. TEST_PT(INT, scm_swIqFdbLpfPu, 7);
  80. TEST_PT(INT, adc_stDownOut.slSampIaPu, 8);
  81. TEST_PT(INT, adc_stDownOut.swIaPu, 9);
  82. TEST_PT(INT, adc_stUpOut.swCalibIaPu, 10);
  83. TEST_PT(INT, crd_stCurParkOut.swQPu, 11);
  84. #endif
  85. }
  86. /***************************************************************
  87. Function: tbc_voIsr;
  88. Description: TBC interrupt service
  89. Call by:
  90. Input Variables: N/A
  91. Output/Return Variables: N/A
  92. Subroutine Call: ...;
  93. Reference: N/A
  94. ****************************************************************/
  95. static BOOL tbc_pvt_blErrorFlag = FALSE;
  96. void tbc_voDownIsr(void)
  97. {
  98. /* ADC Sample */
  99. adc_voSampleUp(&adc_stCof, &adc_stUpOut);
  100. /* Alarm detect */
  101. alm_stIn.blADCInitOvrFlg = sysfsm_stFlg.blADCInitOvrFlg;
  102. alm_stIn.uwIpeakPu = adc_stDownOut.uwIpeakPu; // Q14
  103. alm_stIn.swMotorPwrInWt = scm_swMotorPwrInLpfWt; // unit: 0.1w
  104. alm_stIn.uwIaAbsPu = adc_stDownOut.uwIaAbsPu;
  105. alm_stIn.uwIbAbsPu = adc_stDownOut.uwIbAbsPu; // Q14
  106. alm_stIn.uwIcAbsPu = adc_stDownOut.uwIcAbsPu; // Q14
  107. alm_stIn.swIalhpaPu = crd_stCurClarkOut.swAlphaPu; // Q14
  108. alm_stIn.swIbetaPu = crd_stCurClarkOut.swBetaPu; // Q14
  109. alm_stIn.swIqRefPu = scm_swIqRefPu;
  110. alm_stIn.uwVdcPu = adc_stUpOut.uwVdcLpfPu; // Q14
  111. alm_stIn.uwVdcCompPu = adc_stUpOut.uwVdcLpfPu + BMS_VoltEstimat.uwVdcCompPu; // Q14
  112. alm_stIn.uwSpdFbkLpfAbsPu = scm_uwSpdFbkLpfAbsPu; // Q15
  113. if (scm_swSpdRefPu >= 0)
  114. {
  115. alm_stIn.uwSpdRefAbsPu = (UWORD)scm_swSpdRefPu;
  116. }
  117. else
  118. {
  119. alm_stIn.uwSpdRefAbsPu = (UWORD)(-scm_swSpdRefPu);
  120. }
  121. alm_stIn.uwIPMTempCe = adc_stUpOut.PCBTemp;
  122. alm_stIn.uwIdcOffset = adc_stCof.uwIdcOffset;
  123. // alm_stIn.swDrumSpdAbsNowRpm = uart_swDrumSpdAbsNowRpm;
  124. // alm_stIn.swDrumSpdAbsPreRpm = uart_swDrumSpdAbsPreRpm;
  125. alm_stIn.blSpiThetaFltFlg = spi_stResolverOut.blSpiThetaFltFlg;
  126. alm_voDetecTBC(&alm_stIn, &alm_stDetectTbcCoef);
  127. /* MCU Fault Detect */
  128. if((FALSE == UART_bInsertPendTx)&&(UART_bMonSwitch == FALSE))
  129. {
  130. stl_voTbcProc();
  131. }
  132. /* System FSM */
  133. switch_flg.SysFault_Flag = alm_blAlmOccrFlg;
  134. if (switch_flg.SysFault_Flag == TRUE && tbc_pvt_blErrorFlag == FALSE)
  135. {
  136. tbc_pvt_blErrorFlag = TRUE;
  137. que_stErrorLog.ErrorCode = alm_unCode.all;
  138. que_stErrorLog.RunTime = MC_RunInfo.Ride_Time;
  139. que_stErrorLog.RunInfo = MC_RunInfo;
  140. que_stErrorLog.IqCurrentPu = scm_swIqFdbLpfPu;
  141. que_stErrorLog.IqVoltagePu = scm_swUqRefPu;
  142. que_stErrorLog.IdCurrentPu = scm_swIdFdbLpfPu;
  143. que_stErrorLog.IdVoltagePu = scm_swUdRefPu;
  144. que_ubPushIn(&que_stFlashErrorLog, &que_stErrorLog, 1);//54
  145. }
  146. else if(switch_flg.SysFault_Flag == FALSE)
  147. {
  148. tbc_pvt_blErrorFlag = FALSE;
  149. }
  150. else
  151. {
  152. //do nothing
  153. }
  154. if (adc_stDownOut.blADCCalibFlg && adc_stUpOut.blADCCalibFlg)
  155. {
  156. sysfsm_stFlg.blADCInitOvrFlg = TRUE;
  157. }
  158. else
  159. {
  160. sysfsm_stFlg.blADCInitOvrFlg = FALSE;
  161. }
  162. /* ADC sample */
  163. adc_voSampleDown(&adc_stCof, &adc_stDownOut);
  164. /* ADC Rdson calibration with single resistance*/
  165. adc_voSRCalibration(&adc_stCof, &adc_stUpOut, &adc_stDownOut);
  166. /* System FSM */
  167. FSM1st_Sys_state.TbcDown_hook();
  168. }
  169. /*************************************************************************
  170. Local Functions (N/A)
  171. *************************************************************************/
  172. /*************************************************************************
  173. Copyright (c) 2018 Welling Motor Technology(Shanghai) Co. Ltd.
  174. All rights reserved.
  175. *************************************************************************/
  176. #ifdef _TBC_C_
  177. #undef _TBC_C_
  178. #endif
  179. /*************************************************************************
  180. End of this File (EOF)!
  181. Do not put anything after this part!
  182. *************************************************************************/