You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

254 lines
6.6 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2023 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 "can.h"
  23. #include "crc.h"
  24. #include "dma.h"
  25. #include "rng.h"
  26. #include "spi.h"
  27. #include "tim.h"
  28. #include "usart.h"
  29. #include "gpio.h"
  30. /* Private includes ----------------------------------------------------------*/
  31. /* USER CODE BEGIN Includes */
  32. /* USER CODE END Includes */
  33. /* Private typedef -----------------------------------------------------------*/
  34. /* USER CODE BEGIN PTD */
  35. /* USER CODE END PTD */
  36. /* Private define ------------------------------------------------------------*/
  37. /* USER CODE BEGIN PD */
  38. /* USER CODE END PD */
  39. /* Private macro -------------------------------------------------------------*/
  40. /* USER CODE BEGIN PM */
  41. /* USER CODE END PM */
  42. /* Private variables ---------------------------------------------------------*/
  43. /* USER CODE BEGIN PV */
  44. /* USER CODE END PV */
  45. /* Private function prototypes -----------------------------------------------*/
  46. void SystemClock_Config(void);
  47. void PeriphCommonClock_Config(void);
  48. void MX_FREERTOS_Init(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. /* Configure the peripherals common clocks */
  70. PeriphCommonClock_Config();
  71. /* USER CODE BEGIN SysInit */
  72. /* USER CODE END SysInit */
  73. /* Initialize all configured peripherals */
  74. MX_GPIO_Init();
  75. MX_DMA_Init();
  76. MX_USART1_UART_Init();
  77. MX_TIM3_Init();
  78. MX_CRC_Init();
  79. MX_RNG_Init();
  80. MX_TIM7_Init();
  81. MX_SPI1_Init();
  82. MX_TIM6_Init();
  83. MX_TIM1_Init();
  84. MX_CAN1_Init();
  85. MX_USART2_UART_Init();
  86. MX_USART3_UART_Init();
  87. /* USER CODE BEGIN 2 */
  88. /* USER CODE END 2 */
  89. /* Call init function for freertos objects (in freertos.c) */
  90. MX_FREERTOS_Init();
  91. /* Start scheduler */
  92. osKernelStart();
  93. /* We should never get here as control is now taken by the scheduler */
  94. /* Infinite loop */
  95. /* USER CODE BEGIN WHILE */
  96. while (1)
  97. {
  98. /* USER CODE END WHILE */
  99. /* USER CODE BEGIN 3 */
  100. }
  101. /* USER CODE END 3 */
  102. }
  103. /**
  104. * @brief System Clock Configuration
  105. * @retval None
  106. */
  107. void SystemClock_Config(void)
  108. {
  109. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  110. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  111. /** Configure the main internal regulator output voltage
  112. */
  113. __HAL_RCC_PWR_CLK_ENABLE();
  114. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  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.PLL.PLLState = RCC_PLL_ON;
  121. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  122. RCC_OscInitStruct.PLL.PLLM = 4;
  123. RCC_OscInitStruct.PLL.PLLN = 144;
  124. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  125. RCC_OscInitStruct.PLL.PLLQ = 6;
  126. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  127. {
  128. Error_Handler();
  129. }
  130. /** Initializes the CPU, AHB and APB buses clocks
  131. */
  132. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  133. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  134. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  135. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  136. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  137. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  138. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  139. {
  140. Error_Handler();
  141. }
  142. HAL_RCC_MCOConfig(RCC_MCO2, RCC_MCO2SOURCE_PLLI2SCLK, RCC_MCODIV_4);
  143. }
  144. /**
  145. * @brief Peripherals Common Clock Configuration
  146. * @retval None
  147. */
  148. void PeriphCommonClock_Config(void)
  149. {
  150. RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
  151. /** Initializes the peripherals clock
  152. */
  153. PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_PLLI2S;
  154. PeriphClkInitStruct.PLLI2S.PLLI2SN = 64;
  155. PeriphClkInitStruct.PLLI2S.PLLI2SR = 2;
  156. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  157. {
  158. Error_Handler();
  159. }
  160. }
  161. /* USER CODE BEGIN 4 */
  162. #if 0
  163. /* USER CODE END 4 */
  164. /**
  165. * @brief Period elapsed callback in non blocking mode
  166. * @note This function is called when TIM11 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 == TIM11) {
  177. HAL_IncTick();
  178. }
  179. /* USER CODE BEGIN Callback 1 */
  180. /* USER CODE END Callback 1 */
  181. }
  182. #endif
  183. /**
  184. * @brief This function is executed in case of error occurrence.
  185. * @retval None
  186. */
  187. void Error_Handler(void)
  188. {
  189. /* USER CODE BEGIN Error_Handler_Debug */
  190. /* User can add his own implementation to report the HAL error return state */
  191. __disable_irq();
  192. while (1)
  193. {
  194. }
  195. /* USER CODE END Error_Handler_Debug */
  196. }
  197. #ifdef USE_FULL_ASSERT
  198. /**
  199. * @brief Reports the name of the source file and the source line number
  200. * where the assert_param error has occurred.
  201. * @param file: pointer to the source file name
  202. * @param line: assert_param error line source number
  203. * @retval None
  204. */
  205. void assert_failed(uint8_t *file, uint32_t line)
  206. {
  207. /* USER CODE BEGIN 6 */
  208. /* User can add his own implementation to report the file name and line number,
  209. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  210. /* USER CODE END 6 */
  211. }
  212. #endif /* USE_FULL_ASSERT */