main.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2025 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "cmsis_os.h"
  22. #include "dma.h"
  23. #include "iwdg.h"
  24. #include "spi.h"
  25. #include "tim.h"
  26. #include "usart.h"
  27. #include "gpio.h"
  28. /* Private includes ----------------------------------------------------------*/
  29. /* USER CODE BEGIN Includes */
  30. /* USER CODE END Includes */
  31. /* Private typedef -----------------------------------------------------------*/
  32. /* USER CODE BEGIN PTD */
  33. extern uint8_t dataReceive485[BUFFER_SIZE485]; // 485
  34. extern uint8_t dataReceivep[BUFFER_SIZEP]; // 调试串口接收buffer
  35. extern uint32_t uartIRQ_rx_len ; // 485串口接收数据长度
  36. extern uint32_t uartprint_rx_len ;
  37. /* USER CODE END PTD */
  38. /* Private define ------------------------------------------------------------*/
  39. /* USER CODE BEGIN PD */
  40. /* USER CODE END PD */
  41. /* Private macro -------------------------------------------------------------*/
  42. /* USER CODE BEGIN PM */
  43. /* USER CODE END PM */
  44. /* Private variables ---------------------------------------------------------*/
  45. /* USER CODE BEGIN PV */
  46. /* USER CODE END PV */
  47. /* Private function prototypes -----------------------------------------------*/
  48. void SystemClock_Config(void);
  49. void MX_FREERTOS_Init(void);
  50. /* USER CODE BEGIN PFP */
  51. /* USER CODE END PFP */
  52. /* Private user code ---------------------------------------------------------*/
  53. /* USER CODE BEGIN 0 */
  54. void Restart_UART2_DMA(void) // 调试 DMA+IDLE中断
  55. {
  56. HAL_UART_DMAStop(&huart2); // 停止 DMA
  57. memset(dataReceivep, 0, sizeof(dataReceivep));
  58. __HAL_UART_DISABLE_IT(&huart2, UART_IT_IDLE); // 先关闭 IDLE 中断
  59. uartprint_rx_len = 0;
  60. __HAL_UART_CLEAR_IDLEFLAG(&huart2); // 第一步:清旧标志清除可能的残留标志
  61. HAL_UART_Receive_DMA(&huart2, dataReceivep, BUFFER_SIZEP); // 第二步:启动DMA
  62. __HAL_UART_ENABLE_IT(&huart2, UART_IT_IDLE); // 第三步:开启IDLE中断
  63. }
  64. void Restart_UART1_DMA(void) //485
  65. {
  66. HAL_UART_DMAStop(&huart1); // 停止 DMA
  67. __HAL_UART_CLEAR_IDLEFLAG(&huart1);
  68. __HAL_UART_DISABLE_IT(&huart1, UART_IT_IDLE); // 禁止 IDLE 中断
  69. // uartIRQ_rx_len = 0;
  70. memset(dataReceive485, 0, sizeof(dataReceive485));// 清空接收缓冲区
  71. HAL_UART_Receive_DMA(&huart1, dataReceive485, BUFFER_SIZE485);// 重新启动 DMA 接收
  72. __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);// 重新开启 IDLE 中断
  73. }
  74. /* USER CODE END 0 */
  75. /**
  76. * @brief The application entry point.
  77. * @retval int
  78. */
  79. int main(void)
  80. {
  81. /* USER CODE BEGIN 1 */
  82. /* USER CODE END 1 */
  83. /* MCU Configuration--------------------------------------------------------*/
  84. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  85. HAL_Init();
  86. /* USER CODE BEGIN Init */
  87. /* USER CODE END Init */
  88. /* Configure the system clock */
  89. SystemClock_Config();
  90. /* USER CODE BEGIN SysInit */
  91. /* USER CODE END SysInit */
  92. /* Initialize all configured peripherals */
  93. MX_GPIO_Init();
  94. MX_DMA_Init();
  95. MX_USART1_UART_Init();
  96. MX_TIM15_Init();
  97. MX_IWDG_Init();
  98. MX_SPI1_Init();
  99. MX_USART2_UART_Init();
  100. /* USER CODE BEGIN 2 */
  101. /* -- version data 2025_08_10 ---*/
  102. HAL_TIM_Base_Start(&htim15); // 启动定时器
  103. // printf ("\r\n API_MUL main FACTORY start \r\n");
  104. DB__Dump_IAP_LOAD(); // 从数据库读取 iapLoadStatus IAP装载状态
  105. app_jump_check_and_jump();
  106. /* USER CODE END 2 */
  107. /* Init scheduler */
  108. osKernelInitialize(); /* Call init function for freertos objects (in freertos.c) */
  109. MX_FREERTOS_Init();
  110. /* Start scheduler */
  111. osKernelStart();
  112. /* We should never get here as control is now taken by the scheduler */
  113. /* Infinite loop */
  114. /* USER CODE BEGIN WHILE */
  115. while (1)
  116. {
  117. /* USER CODE END WHILE */
  118. /* USER CODE BEGIN 3 */
  119. }
  120. /* USER CODE END 3 */
  121. }
  122. /**
  123. * @brief System Clock Configuration
  124. * @retval None
  125. */
  126. void SystemClock_Config(void)
  127. {
  128. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  129. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  130. /** Configure the main internal regulator output voltage
  131. */
  132. if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  133. {
  134. Error_Handler();
  135. }
  136. /** Initializes the RCC Oscillators according to the specified parameters
  137. * in the RCC_OscInitTypeDef structure.
  138. */
  139. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_MSI;
  140. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  141. RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  142. RCC_OscInitStruct.MSICalibrationValue = 0;
  143. RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_8;
  144. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  145. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  146. {
  147. Error_Handler();
  148. }
  149. /** Initializes the CPU, AHB and APB buses clocks
  150. */
  151. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  152. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  153. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;
  154. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  155. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  156. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  157. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  158. {
  159. Error_Handler();
  160. }
  161. }
  162. /* USER CODE BEGIN 4 */
  163. /* USER CODE END 4 */
  164. /**
  165. * @brief Period elapsed callback in non blocking mode
  166. * @note This function is called when TIM6 interrupt took place, inside
  167. * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  168. * a global variable "uwTick" used as application time base.
  169. * @param htim : TIM handle
  170. * @retval None
  171. */
  172. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  173. {
  174. /* USER CODE BEGIN Callback 0 */
  175. /* USER CODE END Callback 0 */
  176. if (htim->Instance == TIM6) {
  177. HAL_IncTick();
  178. }
  179. /* USER CODE BEGIN Callback 1 */
  180. /* USER CODE END Callback 1 */
  181. }
  182. /**
  183. * @brief This function is executed in case of error occurrence.
  184. * @retval None
  185. */
  186. void Error_Handler(void)
  187. {
  188. /* USER CODE BEGIN Error_Handler_Debug */
  189. /* User can add his own implementation to report the HAL error return state */
  190. __disable_irq();
  191. while (1)
  192. {
  193. }
  194. /* USER CODE END Error_Handler_Debug */
  195. }
  196. #ifdef USE_FULL_ASSERT
  197. /**
  198. * @brief Reports the name of the source file and the source line number
  199. * where the assert_param error has occurred.
  200. * @param file: pointer to the source file name
  201. * @param line: assert_param error line source number
  202. * @retval None
  203. */
  204. void assert_failed(uint8_t *file, uint32_t line)
  205. {
  206. /* USER CODE BEGIN 6 */
  207. /* User can add his own implementation to report the file name and line number,
  208. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  209. /* USER CODE END 6 */
  210. }
  211. #endif /* USE_FULL_ASSERT */