基质喷涂
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.

272 lines
6.9 KiB

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