main.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2024 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 "can.h"
  22. #include "gpio.h"
  23. #include "usart.h"
  24. #include "usb_device.h"
  25. /* Private includes ----------------------------------------------------------*/
  26. /* USER CODE BEGIN Includes */
  27. #include "can_task.h"
  28. #include "iap.h"
  29. #include "schedule.h"
  30. /* USER CODE END Includes */
  31. /* Private typedef -----------------------------------------------------------*/
  32. /* USER CODE BEGIN PTD */
  33. /* USER CODE END PTD */
  34. /* Private define ------------------------------------------------------------*/
  35. /* USER CODE BEGIN PD */
  36. /* USER CODE END PD */
  37. /* Private macro -------------------------------------------------------------*/
  38. /* USER CODE BEGIN PM */
  39. /* USER CODE END PM */
  40. /* Private variables ---------------------------------------------------------*/
  41. /* USER CODE BEGIN PV */
  42. /* USER CODE END PV */
  43. /* Private function prototypes -----------------------------------------------*/
  44. void SystemClock_Config(void);
  45. /* USER CODE BEGIN PFP */
  46. /* USER CODE END PFP */
  47. /* Private user code ---------------------------------------------------------*/
  48. /* USER CODE BEGIN 0 */
  49. #define NVIC_VectTab_FLASH (uint32_t)0x8000000 // stm32的flash起始地址
  50. #define APP_FLASH_OFFSET 0x4000
  51. #define BOOT_EN 1
  52. // 重设中断向量表
  53. void NVIC_SetVectorTable(uint32_t NVIC_VecTab, uint32_t OffSet)
  54. {
  55. assert_param(IS_NVIC_VECTAB(NVIC_VecTab));
  56. assert_param(IS_NVIC_OFFSET(OffSet));
  57. SCB->VTOR = NVIC_VecTab | (OffSet & (uint32_t)0x1FFFFF80);
  58. }
  59. /* USER CODE END 0 */
  60. /**
  61. * @brief The application entry point.
  62. * @retval int
  63. */
  64. int main(void)
  65. {
  66. /* USER CODE BEGIN 1 */
  67. #if (BOOT_EN == 1)
  68. NVIC_SetVectorTable(NVIC_VectTab_FLASH, APP_FLASH_OFFSET);
  69. __set_PRIMASK(0);
  70. #endif
  71. /* USER CODE END 1 */
  72. /* MCU Configuration--------------------------------------------------------*/
  73. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  74. HAL_Init();
  75. /* USER CODE BEGIN Init */
  76. /* USER CODE END Init */
  77. /* Configure the system clock */
  78. SystemClock_Config();
  79. /* USER CODE BEGIN SysInit */
  80. /* USER CODE END SysInit */
  81. /* Initialize all configured peripherals */
  82. MX_GPIO_Init();
  83. MX_CAN_Init();
  84. MX_USART1_UART_Init();
  85. MX_USART2_UART_Init();
  86. MX_USART3_UART_Init();
  87. MX_USB_DEVICE_Init();
  88. /* USER CODE BEGIN 2 */
  89. can_network_init();
  90. LL_USART_EnableIT_RXNE(USART1);
  91. set_firmver();
  92. iap_flag_release();
  93. iap_param_init();
  94. schedule_init(TASK_PERIOD);
  95. /* USER CODE END 2 */
  96. /* Infinite loop */
  97. /* USER CODE BEGIN WHILE */
  98. while (1)
  99. {
  100. schedule();
  101. /* USER CODE END WHILE */
  102. /* USER CODE BEGIN 3 */
  103. }
  104. /* USER CODE END 3 */
  105. }
  106. /**
  107. * @brief System Clock Configuration
  108. * @retval None
  109. */
  110. void SystemClock_Config(void)
  111. {
  112. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  113. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  114. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  115. /** Initializes the RCC Oscillators according to the specified parameters
  116. * in the RCC_OscInitTypeDef structure.
  117. */
  118. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  119. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  120. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  121. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  122. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  123. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  124. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  125. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  126. {
  127. Error_Handler();
  128. }
  129. /** Initializes the CPU, AHB and APB buses clocks
  130. */
  131. RCC_ClkInitStruct.ClockType =
  132. RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  133. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  134. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  135. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  136. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  137. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  138. {
  139. Error_Handler();
  140. }
  141. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
  142. PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5;
  143. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  144. {
  145. Error_Handler();
  146. }
  147. }
  148. /* USER CODE BEGIN 4 */
  149. /* USER CODE END 4 */
  150. /**
  151. * @brief This function is executed in case of error occurrence.
  152. * @retval None
  153. */
  154. void Error_Handler(void)
  155. {
  156. /* USER CODE BEGIN Error_Handler_Debug */
  157. /* User can add his own implementation to report the HAL error return state */
  158. __disable_irq();
  159. while (1)
  160. {
  161. }
  162. /* USER CODE END Error_Handler_Debug */
  163. }
  164. #ifdef USE_FULL_ASSERT
  165. /**
  166. * @brief Reports the name of the source file and the source line number
  167. * where the assert_param error has occurred.
  168. * @param file: pointer to the source file name
  169. * @param line: assert_param error line source number
  170. * @retval None
  171. */
  172. void assert_failed(uint8_t *file, uint32_t line)
  173. {
  174. /* USER CODE BEGIN 6 */
  175. /* User can add his own implementation to report the file name and line
  176. number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file,
  177. line) */
  178. /* USER CODE END 6 */
  179. }
  180. #endif /* USE_FULL_ASSERT */