main.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /**
  2. ******************************************************************************
  3. * @file : main.c
  4. * @brief : Main program body
  5. ******************************************************************************
  6. ** This notice applies to any and all portions of this file
  7. * that are not between comment pairs USER CODE BEGIN and
  8. * USER CODE END. Other portions of this file, whether
  9. * inserted by the user or by software development tools
  10. * are owned by their respective copyright owners.
  11. *
  12. * COPYRIGHT(c) 2019 STMicroelectronics
  13. *
  14. * Redistribution and use in source and binary forms, with or without modification,
  15. * are permitted provided that the following conditions are met:
  16. * 1. Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  22. * may be used to endorse or promote products derived from this software
  23. * without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  29. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  33. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. ******************************************************************************
  37. */
  38. /* Includes ------------------------------------------------------------------*/
  39. #include "main.h"
  40. #include "stm32f1xx_hal.h"
  41. #include "tim.h"
  42. #include "adc.h"
  43. #include "can.h"
  44. #include "dma.h"
  45. #include "iwdg.h"
  46. #include "usart.h"
  47. #include "gpio.h"
  48. #include "iwdg.h"
  49. /* USER CODE BEGIN Includes */
  50. #include "key_drivers.h"
  51. #include "var.h"
  52. #include "eeprom.h"
  53. #include "tasks.h"
  54. #include "app_loader.h"
  55. #include "stm32fxx_STUlib.h"
  56. #include "rtc.h"
  57. #include "functional_safety_key.h"
  58. /* USER CODE END Includes */
  59. /* Private variables ---------------------------------------------------------*/
  60. /* USER CODE BEGIN PV */
  61. /* Private variables ---------------------------------------------------------*/
  62. uint8_t TimeBase_10ms=0;
  63. /* USER CODE END PV */
  64. /* Private function prototypes -----------------------------------------------*/
  65. void SystemClock_Config(void);
  66. /* USER CODE BEGIN PFP */
  67. /* Private function prototypes -----------------------------------------------*/
  68. void STU_TimeBase(void);
  69. /* USER CODE END PFP */
  70. /* USER CODE BEGIN 0 */
  71. /* USER CODE END 0 */
  72. /**
  73. * @brief The application entry point.
  74. *
  75. * @retval None
  76. */
  77. int main(void)
  78. {
  79. /* USER CODE BEGIN 1 */
  80. IAP_Init();
  81. /* USER CODE END 1 */
  82. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  83. HAL_Init();
  84. /* Configure the system clock */
  85. SystemClock_Config();
  86. /* MCU Configuration----------------------------------------------------------*/
  87. MX_GPIO_Init();
  88. MX_TIM1_Init();
  89. MX_TIM2_Init();
  90. MX_DMA_Init();
  91. MX_ADC1_Init();
  92. MX_USART1_UART_Init();
  93. LED_INIT_ALL();
  94. MX_CAN_Init();
  95. // MX_RTC_Init();
  96. STU_InitRunTimeChecks();
  97. /* USER CODE BEGIN Init */
  98. HAL_TIM_Base_Start(&htim2);
  99. ADC_Start(); //ADC启动
  100. Var_Init(); //变量初始化
  101. EEPROM_Check(); //EEPROM参数检测
  102. KeyInitial();
  103. //第一次发送握手指令给TE
  104. SendUartDataToTE(ID_CDL_TO_PBU_TE, MODE_REPORT, 0x8800, (uint8_t*)NULL);
  105. PBU_PowerOn_Process.PowerOnTimeCnt=SysTime_5ms;
  106. do
  107. {
  108. Uart_RxData_Process(&UART_RxBuff_Struct1, 500);
  109. CAN_RxData_Process(&CAN_RxBuf_Struct_CDL, 500);
  110. if(PBU_TE_Status == UPDATE_ING)
  111. {
  112. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET);
  113. }
  114. else if(PBU_TE_Status == UPDATE_FINISH)
  115. {
  116. HAL_Delay(20);
  117. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
  118. }
  119. else
  120. {
  121. if( TimeCntDiff_5ms(PBU_PowerOn_Process.PowerOnTimeCnt) > 10 )
  122. {
  123. PBU_PowerOn_Process.PowerOnTimeCnt=SysTime_5ms;
  124. PBU_PowerOn_Judge();
  125. Power_Mos_Check(); //置标志后发送错误码
  126. }
  127. }
  128. }while(PBU_PowerOn_Process.PowerKeyStatus == 0);
  129. /* USER CODE END Init */
  130. /* USER CODE BEGIN SysInit */
  131. HAL_Delay(1000);
  132. SendCfgToMC(); //发送电机配置参数
  133. MC_Online_TimeCnt=SysTime_5ms;
  134. BMS_Online_TimeCnt=SysTime_5ms;
  135. Vin_test_TimeCnt=SysTime_5ms;
  136. /* USER CODE END SysInit */
  137. /* USER CODE BEGIN 2 */
  138. MX_IWDG_Init();
  139. /* USER CODE END 2 */
  140. /* Infinite loop */
  141. /* USER CODE BEGIN WHILE */
  142. while (1)
  143. {
  144. /* USER CODE END WHILE */
  145. /* USER CODE BEGIN 3 */
  146. FSK_Process();
  147. PBU_RunMode_Process(PBU_RunMode);
  148. Led_Process();
  149. CanRx_Process();
  150. Battery_Information_Process();
  151. Uart_RxData_Process(&UART_RxBuff_Struct1, 200);
  152. PBU_PowerOn_Judge();
  153. AutoPowerOff_Process();
  154. PBU_Error_Process();
  155. HAL_IWDG_Refresh(&hiwdg1);
  156. STU_TimeBase();
  157. STU_DoRunTimeChecks();
  158. }
  159. /* USER CODE END 3 */
  160. }
  161. /**
  162. * @brief System Clock Configuration
  163. * @retval None
  164. */
  165. void SystemClock_Config(void)
  166. {
  167. RCC_OscInitTypeDef RCC_OscInitStruct;
  168. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  169. RCC_PeriphCLKInitTypeDef PeriphClkInit;
  170. /**Initializes the CPU, AHB and APB busses clocks
  171. */
  172. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE;
  173. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  174. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  175. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  176. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  177. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  178. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  179. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  180. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  181. {
  182. _Error_Handler(__FILE__, __LINE__);
  183. }
  184. /**Initializes the CPU, AHB and APB busses clocks
  185. */
  186. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  187. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  188. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  189. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  190. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  191. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  192. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  193. {
  194. _Error_Handler(__FILE__, __LINE__);
  195. }
  196. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  197. PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV8;
  198. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  199. {
  200. _Error_Handler(__FILE__, __LINE__);
  201. }
  202. /**Configure the Systick interrupt time
  203. */
  204. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  205. /**Configure the Systick
  206. */
  207. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  208. /* SysTick_IRQn interrupt configuration */
  209. HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  210. }
  211. /* USER CODE BEGIN 4 */
  212. void STU_TimeBase(void)
  213. {
  214. STU_TestStatus ram_test;
  215. if(TimeBase_10ms >= 10 )
  216. {
  217. TimeBase_10ms = 0;
  218. TimeBaseFlag = 0xAAAAAAAAuL;
  219. TimeBaseFlagInv = 0x55555555uL;
  220. /*--------------------------------------------------------------------------*/
  221. /* --------------------- RAM functional test -------------------*/
  222. /*--------------------------------------------------------------------------*/
  223. __disable_irq();
  224. ram_test = STU_RamRun();
  225. __enable_irq();
  226. switch ( ram_test )
  227. {
  228. case TEST_RUNNING:
  229. break;
  230. case TEST_OK:
  231. break;
  232. case TEST_FAILURE:
  233. case CLASS_C_DATA_FAIL:
  234. default:
  235. FailSafePOR();
  236. break;
  237. }
  238. }
  239. }
  240. /* USER CODE END 4 */
  241. /**
  242. * @brief This function is executed in case of error occurrence.
  243. * @param file: The file name as string.
  244. * @param line: The line in file as a number.
  245. * @retval None
  246. */
  247. void _Error_Handler(char *file, int line)
  248. {
  249. /* USER CODE BEGIN Error_Handler_Debug */
  250. /* User can add his own implementation to report the HAL error return state */
  251. while(1)
  252. {
  253. }
  254. /* USER CODE END Error_Handler_Debug */
  255. }
  256. #ifdef USE_FULL_ASSERT
  257. /**
  258. * @brief Reports the name of the source file and the source line number
  259. * where the assert_param error has occurred.
  260. * @param file: pointer to the source file name
  261. * @param line: assert_param error line source number
  262. * @retval None
  263. */
  264. void assert_failed(uint8_t* file, uint32_t line)
  265. {
  266. /* USER CODE BEGIN 6 */
  267. /* User can add his own implementation to report the file name and line number,
  268. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  269. /* USER CODE END 6 */
  270. }
  271. #endif /* USE_FULL_ASSERT */
  272. /**
  273. * @}
  274. */
  275. /**
  276. * @}
  277. */
  278. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/