app.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #include "api.h"
  2. #include "board_config.h"
  3. #include "tbc.h"
  4. #include "tbs.h"
  5. #include "sys_task.h"
  6. #include "TimeTask_Event.h"
  7. #include "InputCapture.h"
  8. #include "syspar.h"
  9. #include "user.h"
  10. #include "TimeTask_Event.h"
  11. #include "FSM_1st.h"
  12. #include "FSM_2nd.h"
  13. #include "can.h"
  14. #include "cmdgennew.h"
  15. #include "canAppl.h"
  16. #include "flash_master.h"
  17. #include "torquesensor.h"
  18. #include "power.h"
  19. #include "STLmain.h"
  20. #include "display.h"
  21. void PeripheralInit();
  22. void PeripheralStart();
  23. void AppInit()
  24. {
  25. /* 初始化外设(Api层级配置)并配置调度逻辑 */
  26. PeripheralInit();
  27. // /* 1 电机控制环路初始化 */
  28. // /* 1.1 初始化PU值系统 */
  29. // /* 1.2 初始化参数系统 */
  30. // /* 1.3 算法模块初始化 */
  31. //
  32. // McPipelineInit();
  33. // McModulesConfig();
  34. // McPipelineModulesInit();
  35. // McPipelineStaticCoef();
  36. //
  37. // /* 2 控制组件初始化 */
  38. //
  39. // SysCtrlInit();
  40. // McCtrlInit();
  41. //
  42. // /* 状态机初始化 */
  43. //
  44. // SysFsmInit();
  45. // CtrlFsmInit();
  46. // McFsmInit();
  47. //
  48. // /* 故障处理初始化 */
  49. //
  50. // AlmInit();
  51. //
  52. // /* 串口调试初始化 */
  53. //
  54. // uart_voMonitorInit();
  55. /* Code Para Init */
  56. CodeParaInit();
  57. /* AssitPara Init */
  58. ass_voAssitEEInit();
  59. /* DisplayInit */
  60. display_voDisplayInit();
  61. #ifdef RUN_ARCH_SIM
  62. cp_stFlg.SpiOffsetFirstSetFlg = 1;
  63. #else
  64. /* Para Copy */
  65. mn_voParaSet();
  66. /* PowerInit */
  67. power_voPowerInit();
  68. //cp_stFlg.ParaFirstSetFlg = 0; //eeprom save test
  69. if(cp_stFlg.ParaFirstSetFlg == 0)
  70. {
  71. flash_voParaInit();
  72. /* Parameter Default Value Write*/
  73. i2c_voDefaultWriteBuffer();
  74. i2c_voInfoWrite2EE(&i2c_stTXCoef, &i2c_stTXOut);
  75. /* History Default Value Write */
  76. i2c_voHistoryDefaultWriteBuffer();
  77. i2c_voHistoryWrite2EE(&i2c_stTXCoef, &i2c_stTXOut);
  78. /* Read EE and Update code para */
  79. mn_voParaSet();
  80. }
  81. #endif
  82. Can_voUpdateMC_UpcInfo();
  83. /* System parameter initial */
  84. mn_voSoftwareInit();
  85. /* 启动外设 */
  86. PeripheralStart();
  87. }
  88. void AppLoop()
  89. {
  90. /* Uart Monitor Service */
  91. // ReadFrame_Poll2();
  92. // uart_voMainDec();
  93. /* Control mode FSM reset */
  94. if (!sysfsm_stFlg.blFSMRstOvrFlg)
  95. {
  96. RUN_FSM_voInit();
  97. Switch_speed_FSMInit();
  98. sysfsm_stFlg.blFSMRstOvrFlg = TRUE;
  99. }
  100. /* Control mode variable clear */
  101. if (!sysfsm_stFlg.blCtrlMdVarClcOvrFlg)
  102. {
  103. sysfsm_stFlg.blCtrlMdVarClcOvrFlg = TRUE;
  104. }
  105. CanRx_Process();
  106. ReadFrame_Poll2();
  107. uart_voMainDec();
  108. TimingTaskLoopServer();
  109. if(cp_stFlg.SpiOffsetFirstSetFinishFlg == TRUE && switch_flg.SysRun_Flag == FALSE && cp_stFlg.SpiOffsetFirstSetFlg==0 && FSM2nd_Run_state.state == Exit)
  110. {
  111. MC_UpcInfo.stMContorlInfo.uwSaveFlg = 1;
  112. cp_stFlg.SpiOffsetFirstSetFlg = 1;
  113. mn_voEEUperParaUpdate();
  114. /* Parameter real value write*/
  115. i2c_voParaWriteBuffer();
  116. i2c_voInfoWrite2EE(&i2c_stTXCoef, &i2c_stTXOut);
  117. MC_UpcInfo.stMContorlInfo.uwSaveFlg = 0;
  118. }
  119. if (cp_stFlg.ParaUpdateFlg == TRUE)
  120. {
  121. mn_voParaUpdate();
  122. cp_stFlg.ParaUpdateFlg = FALSE;
  123. }
  124. if (power_stPowStateOut.powerstate == POWER_OFF)
  125. {
  126. if (FSM2nd_Run_state.state == Exit)
  127. {
  128. if (cp_stFlg.ParaHistorySaveEEFinishFlg == FALSE)
  129. {
  130. cp_stHistoryPara.uwOpenTimes++;
  131. cp_stHistoryPara.ulODOTime = MC_RideLog.ODO_Time;
  132. cp_stHistoryPara.ulTripSumTime = MC_RideLog.TRIP_Time;
  133. mn_voEEHistoryParaUpdate();
  134. i2c_voHistoryWriteBuffer();
  135. i2c_voHistoryWrite2EE(&i2c_stTXCoef, &i2c_stTXOut);
  136. cp_stFlg.ParaHistorySaveEEFinishFlg = TRUE;
  137. flash_voWrite();
  138. /* Error Log Write */
  139. flash_voErrorWrite();
  140. if (cp_stFlg.ParaSaveEEFlg == TRUE)
  141. {
  142. mn_voEEUperParaUpdate();
  143. /* Parameter real value write*/
  144. i2c_voParaWriteBuffer();
  145. i2c_voInfoWrite2EE(&i2c_stTXCoef, &i2c_stTXOut);
  146. cp_stFlg.ParaSaveEEFinishFlg = TRUE;
  147. cp_stFlg.ParaSaveEEFlg = FALSE;
  148. }
  149. }
  150. }
  151. }
  152. }
  153. void PeripheralInit()
  154. {
  155. /* Gpio配置 */
  156. // iGpio_SetMode(HW_GPIO_HALLA_PIN, ApiGpio_Output);
  157. // iGpio_SetMode(HW_GPIO_HALLB_PIN, ApiGpio_Output);
  158. // iGpio_SetMode(HW_GPIO_HALLC_PIN, ApiGpio_Output);
  159. /* Pwm配置 */
  160. iPwm_BindDeviceInterrupt(0, ApiPwm_CountZeroInt, tbc_voUpIsr);
  161. iPwm_BindDeviceInterrupt(0, ApiPwm_CountMaxInt, tbc_voDownIsr);
  162. iPwm_EnableDeviceInterrupt(0, ApiPwm_CountZeroInt);
  163. iPwm_EnableDeviceInterrupt(0, ApiPwm_CountMaxInt);
  164. /* Tbs 定时器配置 */
  165. iTimer_BindInterrupt(HW_TBS_TIMER, tbs_voIsr);
  166. iTimer_EnableInterrupt(HW_TBS_TIMER);
  167. /* Event1ms 定时器配置 */
  168. iTimer_BindInterrupt(HW_EVENT1MS_TIMER, Event_1ms);
  169. iTimer_EnableInterrupt(HW_EVENT1MS_TIMER);
  170. /* 1ms 定时器配置 */
  171. // clock = iTimer_GetClock(HW_SYSTICK_TIMER);
  172. // iTimer_SetPeriod(HW_SYSTICK_TIMER, clock / 1000 - 1);
  173. // iTimer_EnableAutoReload(HW_SYSTICK_TIMER);
  174. iTimer_BindInterrupt(HW_SYSTICK_TIMER, SysTask1Ms);
  175. iTimer_EnableInterrupt(HW_SYSTICK_TIMER);
  176. iCap_BindDeviceInterrupt(0,IC_CountMaxISR);
  177. iCap_BindChannelInterrupt(0,CAP_CH(2),IC_CadenceISR);
  178. iCap_BindChannelInterrupt(0,CAP_CH(3),IC_BikeSpdISR);
  179. iCap_EnableDeviceInterrupt(0);
  180. iCap_EnableChannelInterrupt(0,CAP_CH(2));
  181. iCap_EnableChannelInterrupt(0,CAP_CH(3));
  182. }
  183. void PeripheralStart()
  184. {
  185. // iPwm_EnableCount(0);
  186. // iTimer_Start(HW_TBS_TIMER);
  187. // iTimer_Start(HW_SYSTICK_TIMER);
  188. }