main.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2026 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 "adc.h"
  22. #include "fdcan.h"
  23. #include "gpdma.h"
  24. #include "icache.h"
  25. #include "tim.h"
  26. #include "gpio.h"
  27. /* Private includes ----------------------------------------------------------*/
  28. /* USER CODE BEGIN Includes */
  29. #include "can_link.h"
  30. #include "soft_led.h"
  31. #include "task.h"
  32. #include "weight_init.h"
  33. #include "soft_can.h"
  34. /* USER CODE END Includes */
  35. /* Private typedef -----------------------------------------------------------*/
  36. /* USER CODE BEGIN PTD */
  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. /* USER CODE BEGIN PFP */
  50. /* USER CODE END PFP */
  51. /* Private user code ---------------------------------------------------------*/
  52. /* USER CODE BEGIN 0 */
  53. /* USER CODE END 0 */
  54. /**
  55. * @brief The application entry point.
  56. * @retval int
  57. */
  58. int main(void)
  59. {
  60. /* USER CODE BEGIN 1 */
  61. /* USER CODE END 1 */
  62. /* MCU Configuration--------------------------------------------------------*/
  63. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  64. HAL_Init();
  65. /* USER CODE BEGIN Init */
  66. /* USER CODE END Init */
  67. /* Configure the system clock */
  68. SystemClock_Config();
  69. /* USER CODE BEGIN SysInit */
  70. /* USER CODE END SysInit */
  71. /* Initialize all configured peripherals */
  72. MX_GPIO_Init();
  73. MX_GPDMA1_Init();
  74. MX_ADC1_Init();
  75. MX_FDCAN1_Init();
  76. MX_FDCAN2_Init();
  77. MX_TIM1_Init();
  78. MX_TIM2_Init();
  79. MX_TIM3_Init();
  80. MX_TIM4_Init();
  81. MX_TIM5_Init();
  82. MX_TIM12_Init();
  83. MX_ICACHE_Init();
  84. /* USER CODE BEGIN 2 */
  85. MY_ADC_INIT();
  86. MY_CAN_INIT();
  87. MY_TIM_PWM_INIT();
  88. MY_WEIGHT_INIT();
  89. /* USER CODE END 2 */
  90. /* Infinite loop */
  91. /* USER CODE BEGIN WHILE */
  92. while (1)
  93. {
  94. /* USER CODE END WHILE */
  95. Task_Polling();
  96. /* USER CODE BEGIN 3 */
  97. }
  98. /* USER CODE END 3 */
  99. }
  100. /**
  101. * @brief System Clock Configuration
  102. * @retval None
  103. */
  104. void SystemClock_Config(void)
  105. {
  106. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  107. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  108. /** Configure the main internal regulator output voltage
  109. */
  110. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE0);
  111. while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
  112. /** Initializes the RCC Oscillators according to the specified parameters
  113. * in the RCC_OscInitTypeDef structure.
  114. */
  115. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  116. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  117. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  118. RCC_OscInitStruct.PLL.PLLSource = RCC_PLL1_SOURCE_HSE;
  119. RCC_OscInitStruct.PLL.PLLM = 2;
  120. RCC_OscInitStruct.PLL.PLLN = 40;
  121. RCC_OscInitStruct.PLL.PLLP = 2;
  122. RCC_OscInitStruct.PLL.PLLQ = 2;
  123. RCC_OscInitStruct.PLL.PLLR = 2;
  124. RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1_VCIRANGE_3;
  125. RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1_VCORANGE_WIDE;
  126. RCC_OscInitStruct.PLL.PLLFRACN = 0;
  127. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  128. {
  129. Error_Handler();
  130. }
  131. /** Initializes the CPU, AHB and APB buses clocks
  132. */
  133. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  134. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
  135. |RCC_CLOCKTYPE_PCLK3;
  136. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  137. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  138. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  139. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  140. RCC_ClkInitStruct.APB3CLKDivider = RCC_HCLK_DIV1;
  141. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  142. {
  143. Error_Handler();
  144. }
  145. /** Configure the programming delay
  146. */
  147. __HAL_FLASH_SET_PROGRAM_DELAY(FLASH_PROGRAMMING_DELAY_2);
  148. }
  149. /* USER CODE BEGIN 4 */
  150. /* USER CODE END 4 */
  151. /**
  152. * @brief This function is executed in case of error occurrence.
  153. * @retval None
  154. */
  155. void Error_Handler(void)
  156. {
  157. /* USER CODE BEGIN Error_Handler_Debug */
  158. /* User can add his own implementation to report the HAL error return state */
  159. __disable_irq();
  160. while (1)
  161. {
  162. }
  163. /* USER CODE END Error_Handler_Debug */
  164. }
  165. #ifdef USE_FULL_ASSERT
  166. /**
  167. * @brief Reports the name of the source file and the source line number
  168. * where the assert_param error has occurred.
  169. * @param file: pointer to the source file name
  170. * @param line: assert_param error line source number
  171. * @retval None
  172. */
  173. void assert_failed(uint8_t *file, uint32_t line)
  174. {
  175. /* USER CODE BEGIN 6 */
  176. /* User can add his own implementation to report the file name and line number,
  177. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  178. /* USER CODE END 6 */
  179. }
  180. #endif /* USE_FULL_ASSERT */