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.

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