stm32fxx_STUstartup.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. #include "stm32f1xx_hal.h"
  2. #define ALLOC_GLOBALS
  3. #include "stm32fxx_STUlib.h"
  4. //#include "motor_control.h"
  5. //#include "power12V_driver.h"
  6. #include "tasks.h"
  7. #include "can.h"
  8. #include "gpio.h"
  9. uint8_t FIT_testflag=1;
  10. //static uint32_t sRomTestData=0x55555555; //0x55555554
  11. CRC_HandleTypeDef CrcHandle = {0};
  12. void StartUpClock_Config_Reset(void);
  13. //#ifdef __CC_ARM /* KEIL Compiler */
  14. void $Sub$$main(void)
  15. {
  16. __disable_irq();
  17. if ( CRC_FLAG != 0xAAu )
  18. {
  19. STU_StartUp(); /* trick to call StartUp before main entry */
  20. }
  21. CRC_FLAG = 0u;
  22. __enable_irq();
  23. $Super$$main();
  24. }
  25. //#endif /* __CC_ARM */
  26. void STU_StartUp(void)
  27. {
  28. uint32_t crc_result;
  29. uint32_t index;
  30. /* Reset of all peripherals, Initializes the Flash interface and the Systick */
  31. HAL_Init();
  32. /*时钟倍频到72Mhz*/
  33. StartUpClock_Config();
  34. // /*FIT*/
  35. // FIT_rom_test();
  36. /* Initialization of counters for control flow monitoring */
  37. init_control_flow();
  38. /*--------------------------------------------------------------------------*/
  39. /*------------------- CPU registers test --------------------*/
  40. /*--------------------------------------------------------------------------*/
  41. control_flow_call(CPU_TEST_CALLER);
  42. STU_CpuTestStartup();
  43. control_flow_resume(CPU_TEST_CALLER);
  44. STU_CPURegsAddressing();
  45. /*--------------------------------------------------------------------------*/
  46. /*--------------------- ROM CRC check ------------------------*/
  47. /*--------------------------------------------------------------------------*/
  48. control_flow_call(CRC32_TEST_CALLER);
  49. CrcHandle.Instance = CRC;
  50. __HAL_CRC_DR_RESET(&CrcHandle);
  51. HAL_CRC_Init(&CrcHandle);
  52. for(index = 0; index < (uint32_t)ROM_SIZEinWORDS; index++)
  53. {
  54. CRC->DR = (*((uint32_t *)ROM_START + index));
  55. }
  56. crc_result = CRC->DR;
  57. if(crc_result != *(uint32_t *)(&REF_CRC32))
  58. {
  59. /*ROM err*/
  60. FailSafePOR();
  61. }
  62. control_flow_resume(CRC32_TEST_CALLER);
  63. /*--------------------------------------------------------------------------*/
  64. /* --------------------- Instruct and decode test -------------------*/
  65. /*--------------------------------------------------------------------------*/
  66. control_flow_call(INSTRUCT_AND_DECODE_TEST_CALLER);
  67. if(STU_InstructAndDecode() == ERROR)
  68. {
  69. FailSafePOR();
  70. }
  71. control_flow_resume(INSTRUCT_AND_DECODE_TEST_CALLER);
  72. /*--------------------------------------------------------------------------*/
  73. /* --------------------- Addressing test -------------------*/
  74. /*--------------------------------------------------------------------------*/
  75. control_flow_call(ADDRESSING_TEST_CALLER);
  76. if(STU_AddressingTest() == ERROR)
  77. {
  78. FailSafePOR();
  79. }
  80. control_flow_resume(ADDRESSING_TEST_CALLER);
  81. /*--------------------------------------------------------------------------*/
  82. /* --------------------- Peripheral Registers test -------------------*/
  83. /*--------------------------------------------------------------------------*/
  84. control_flow_call(PERIPHERAL_REGS_TEST_CALLER);
  85. if(STU_PeripheralRegistersTest() == ERROR)
  86. {
  87. FailSafePOR();
  88. }
  89. /*做两次测试,确保0->1,1->0均能有效翻转*/
  90. if(STU_PeripheralRegistersTest() == ERROR)
  91. {
  92. FailSafePOR();
  93. }
  94. control_flow_resume(PERIPHERAL_REGS_TEST_CALLER);
  95. /*--------------------------------------------------------------------------*/
  96. /* Verify Control flow before RAM init (which clears Ctrl flow counters) */
  97. /*--------------------------------------------------------------------------*/
  98. if (control_flow_check_point(STARTUP_CHECK) == ERROR)
  99. {
  100. FailSafePOR();
  101. }
  102. /*--------------------------------------------------------------------------*/
  103. /* --------------------- RAM functional test -------------------*/
  104. /*--------------------------------------------------------------------------*/
  105. //__disable_irq();
  106. if(STU_FullRamWalkpat((uint32_t *)RAM_START ,RAM_SIZE) == ERROR)
  107. {
  108. FailSafePOR();
  109. }
  110. //__enable_irq();
  111. /*复位时钟到默认状态*/
  112. StartUpClock_Config_Reset();
  113. GotoCompilerStartUp();
  114. }
  115. void StartUpClock_Config(void)
  116. //{
  117. // RCC_OscInitTypeDef RCC_OscInitStruct;
  118. // RCC_ClkInitTypeDef RCC_ClkInitStruct;
  119. // /**Initializes the CPU, AHB and APB busses clocks
  120. // */
  121. // RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  122. // RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  123. // RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  124. // RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  125. // RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  126. // RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  127. // RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  128. // if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  129. // {
  130. // _Error_Handler(__FILE__, __LINE__);
  131. // }
  132. // /**Initializes the CPU, AHB and APB busses clocks
  133. // */
  134. // RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  135. // |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  136. // RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  137. // RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  138. // RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  139. // RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  140. // if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  141. // {
  142. // _Error_Handler(__FILE__, __LINE__);
  143. // }
  144. // /**Configure the Systick interrupt time
  145. // */
  146. // HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  147. // /**Configure the Systick
  148. // */
  149. // HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  150. // /* SysTick_IRQn interrupt configuration */
  151. // HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  152. //}
  153. {
  154. RCC_OscInitTypeDef RCC_OscInitStruct;
  155. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  156. RCC_PeriphCLKInitTypeDef PeriphClkInit;
  157. /**Initializes the CPU, AHB and APB busses clocks
  158. */
  159. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE;
  160. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  161. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  162. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  163. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  164. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  165. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  166. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  167. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  168. {
  169. _Error_Handler(__FILE__, __LINE__);
  170. }
  171. /**Initializes the CPU, AHB and APB busses clocks
  172. */
  173. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  174. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  175. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  176. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  177. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  178. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  179. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  180. {
  181. _Error_Handler(__FILE__, __LINE__);
  182. }
  183. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  184. PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
  185. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  186. {
  187. _Error_Handler(__FILE__, __LINE__);
  188. }
  189. /**Configure the Systick interrupt time
  190. */
  191. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  192. /**Configure the Systick
  193. */
  194. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  195. /* SysTick_IRQn interrupt configuration */
  196. HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  197. }
  198. /**
  199. * @brief System Clock Configuration
  200. * @retval None
  201. */
  202. void StartUpClock_Config_Reset(void)
  203. {
  204. RCC_OscInitTypeDef RCC_OscInitStruct;
  205. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  206. /**Initializes the CPU, AHB and APB busses clocks
  207. */
  208. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  209. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  210. RCC_OscInitStruct.HSICalibrationValue = 16;
  211. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  212. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  213. {
  214. _Error_Handler(__FILE__, __LINE__);
  215. }
  216. /**Initializes the CPU, AHB and APB busses clocks
  217. */
  218. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  219. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  220. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  221. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  222. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  223. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  224. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  225. {
  226. _Error_Handler(__FILE__, __LINE__);
  227. }
  228. /**Configure the Systick interrupt time
  229. */
  230. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  231. /**Configure the Systick
  232. */
  233. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  234. /* SysTick_IRQn interrupt configuration */
  235. HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  236. }
  237. /**
  238. * @brief Verifies the consistency and value of control flow counters
  239. * @param : check value of the positive counter
  240. * @retval : ErrorStatus (SUCCESS, ERROR)
  241. */
  242. ErrorStatus control_flow_check_point(uint32_t chck)
  243. {
  244. ErrorStatus Result= SUCCESS;
  245. if ((CtrlFlowCnt != (chck)) || ((CtrlFlowCnt ^ CtrlFlowCntInv) != 0xFFFFFFFFuL))
  246. {
  247. Result= ERROR;
  248. }
  249. return(Result);
  250. }
  251. void FailSafePOR(void)
  252. {
  253. __enable_irq();
  254. MX_GPIO_Init();
  255. MX_CAN_Init();
  256. while(1)
  257. {
  258. //MCU故障
  259. PBU_hardwareErrorCode.ERROR_Bit.MCU_Fault = 1;
  260. SendData(ID_PBU_BC, MODE_REPORT, 0x1504, (uint8_t*)&PBU_ErrorCode.Code);
  261. HAL_Delay(200);
  262. }
  263. }