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.

252 lines
6.6 KiB

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 "rng.h"
  25. #include "spi.h"
  26. #include "tim.h"
  27. #include "usart.h"
  28. #include "gpio.h"
  29. /* Private includes ----------------------------------------------------------*/
  30. /* USER CODE BEGIN Includes */
  31. /* USER CODE END Includes */
  32. /* Private typedef -----------------------------------------------------------*/
  33. /* USER CODE BEGIN PTD */
  34. /* USER CODE END PTD */
  35. /* Private define ------------------------------------------------------------*/
  36. /* USER CODE BEGIN PD */
  37. /* USER CODE END PD */
  38. /* Private macro -------------------------------------------------------------*/
  39. /* USER CODE BEGIN PM */
  40. /* USER CODE END PM */
  41. /* Private variables ---------------------------------------------------------*/
  42. /* USER CODE BEGIN PV */
  43. /* USER CODE END PV */
  44. /* Private function prototypes -----------------------------------------------*/
  45. void SystemClock_Config(void);
  46. void PeriphCommonClock_Config(void);
  47. void MX_FREERTOS_Init(void);
  48. /* USER CODE BEGIN PFP */
  49. /* USER CODE END PFP */
  50. /* Private user code ---------------------------------------------------------*/
  51. /* USER CODE BEGIN 0 */
  52. /* USER CODE END 0 */
  53. /**
  54. * @brief The application entry point.
  55. * @retval int
  56. */
  57. int main(void)
  58. {
  59. /* USER CODE BEGIN 1 */
  60. /* USER CODE END 1 */
  61. /* MCU Configuration--------------------------------------------------------*/
  62. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  63. HAL_Init();
  64. /* USER CODE BEGIN Init */
  65. /* USER CODE END Init */
  66. /* Configure the system clock */
  67. SystemClock_Config();
  68. /* Configure the peripherals common clocks */
  69. PeriphCommonClock_Config();
  70. /* USER CODE BEGIN SysInit */
  71. /* USER CODE END SysInit */
  72. /* Initialize all configured peripherals */
  73. MX_GPIO_Init();
  74. MX_USART1_UART_Init();
  75. MX_TIM3_Init();
  76. MX_CRC_Init();
  77. MX_RNG_Init();
  78. MX_TIM7_Init();
  79. MX_SPI1_Init();
  80. MX_TIM6_Init();
  81. MX_TIM1_Init();
  82. MX_CAN1_Init();
  83. MX_USART2_UART_Init();
  84. MX_USART3_UART_Init();
  85. /* USER CODE BEGIN 2 */
  86. /* USER CODE END 2 */
  87. /* Call init function for freertos objects (in freertos.c) */
  88. MX_FREERTOS_Init();
  89. /* Start scheduler */
  90. osKernelStart();
  91. /* We should never get here as control is now taken by the scheduler */
  92. /* Infinite loop */
  93. /* USER CODE BEGIN WHILE */
  94. while (1)
  95. {
  96. /* USER CODE END WHILE */
  97. /* USER CODE BEGIN 3 */
  98. }
  99. /* USER CODE END 3 */
  100. }
  101. /**
  102. * @brief System Clock Configuration
  103. * @retval None
  104. */
  105. void SystemClock_Config(void)
  106. {
  107. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  108. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  109. /** Configure the main internal regulator output voltage
  110. */
  111. __HAL_RCC_PWR_CLK_ENABLE();
  112. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  113. /** Initializes the RCC Oscillators according to the specified parameters
  114. * in the RCC_OscInitTypeDef structure.
  115. */
  116. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  117. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  118. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  119. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  120. RCC_OscInitStruct.PLL.PLLM = 4;
  121. RCC_OscInitStruct.PLL.PLLN = 144;
  122. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  123. RCC_OscInitStruct.PLL.PLLQ = 6;
  124. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  125. {
  126. Error_Handler();
  127. }
  128. /** Initializes the CPU, AHB and APB buses clocks
  129. */
  130. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  131. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  132. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  133. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  134. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  135. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  136. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  137. {
  138. Error_Handler();
  139. }
  140. HAL_RCC_MCOConfig(RCC_MCO2, RCC_MCO2SOURCE_PLLI2SCLK, RCC_MCODIV_4);
  141. }
  142. /**
  143. * @brief Peripherals Common Clock Configuration
  144. * @retval None
  145. */
  146. void PeriphCommonClock_Config(void)
  147. {
  148. RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
  149. /** Initializes the peripherals clock
  150. */
  151. PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_PLLI2S;
  152. PeriphClkInitStruct.PLLI2S.PLLI2SN = 64;
  153. PeriphClkInitStruct.PLLI2S.PLLI2SR = 2;
  154. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  155. {
  156. Error_Handler();
  157. }
  158. }
  159. /* USER CODE BEGIN 4 */
  160. #if 0
  161. /* USER CODE END 4 */
  162. /**
  163. * @brief Period elapsed callback in non blocking mode
  164. * @note This function is called when TIM11 interrupt took place, inside
  165. * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  166. * a global variable "uwTick" used as application time base.
  167. * @param htim : TIM handle
  168. * @retval None
  169. */
  170. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  171. {
  172. /* USER CODE BEGIN Callback 0 */
  173. /* USER CODE END Callback 0 */
  174. if (htim->Instance == TIM11) {
  175. HAL_IncTick();
  176. }
  177. /* USER CODE BEGIN Callback 1 */
  178. /* USER CODE END Callback 1 */
  179. }
  180. #endif
  181. /**
  182. * @brief This function is executed in case of error occurrence.
  183. * @retval None
  184. */
  185. void Error_Handler(void)
  186. {
  187. /* USER CODE BEGIN Error_Handler_Debug */
  188. /* User can add his own implementation to report the HAL error return state */
  189. __disable_irq();
  190. while (1)
  191. {
  192. }
  193. /* USER CODE END Error_Handler_Debug */
  194. }
  195. #ifdef USE_FULL_ASSERT
  196. /**
  197. * @brief Reports the name of the source file and the source line number
  198. * where the assert_param error has occurred.
  199. * @param file: pointer to the source file name
  200. * @param line: assert_param error line source number
  201. * @retval None
  202. */
  203. void assert_failed(uint8_t *file, uint32_t line)
  204. {
  205. /* USER CODE BEGIN 6 */
  206. /* User can add his own implementation to report the file name and line number,
  207. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  208. /* USER CODE END 6 */
  209. }
  210. #endif /* USE_FULL_ASSERT */