main.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under Ultimate Liberty license
  13. * SLA0044, the "License"; You may not use this file except in compliance with
  14. * the License. You may obtain a copy of the License at:
  15. * www.st.com/SLA0044
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22. #include "cmsis_os.h"
  23. #include "adc.h"
  24. #include "aes.h"
  25. #include "comp.h"
  26. #include "crc.h"
  27. #include "i2c.h"
  28. #include "pka.h"
  29. #include "rf.h"
  30. #include "rng.h"
  31. #include "rtc.h"
  32. #include "spi.h"
  33. #include "tim.h"
  34. #include "usart.h"
  35. #include "usb_device.h"
  36. #include "gpio.h"
  37. /* Private includes ----------------------------------------------------------*/
  38. /* USER CODE BEGIN Includes */
  39. /* USER CODE END Includes */
  40. /* Private typedef -----------------------------------------------------------*/
  41. /* USER CODE BEGIN PTD */
  42. /* USER CODE END PTD */
  43. /* Private define ------------------------------------------------------------*/
  44. /* USER CODE BEGIN PD */
  45. /* USER CODE END PD */
  46. /* Private macro -------------------------------------------------------------*/
  47. /* USER CODE BEGIN PM */
  48. /* USER CODE END PM */
  49. /* Private variables ---------------------------------------------------------*/
  50. /* USER CODE BEGIN PV */
  51. /* USER CODE END PV */
  52. /* Private function prototypes -----------------------------------------------*/
  53. void SystemClock_Config(void);
  54. void MX_FREERTOS_Init(void);
  55. /* USER CODE BEGIN PFP */
  56. /* USER CODE END PFP */
  57. /* Private user code ---------------------------------------------------------*/
  58. /* USER CODE BEGIN 0 */
  59. /* USER CODE END 0 */
  60. /**
  61. * @brief The application entry point.
  62. * @retval int
  63. */
  64. int main(void)
  65. {
  66. /* USER CODE BEGIN 1 */
  67. /* USER CODE END 1 */
  68. /* MCU Configuration--------------------------------------------------------*/
  69. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  70. HAL_Init();
  71. /* USER CODE BEGIN Init */
  72. /* USER CODE END Init */
  73. /* Configure the system clock */
  74. SystemClock_Config();
  75. /* USER CODE BEGIN SysInit */
  76. /* USER CODE END SysInit */
  77. /* Initialize all configured peripherals */
  78. MX_GPIO_Init();
  79. MX_ADC1_Init();
  80. MX_I2C1_Init();
  81. MX_RTC_Init();
  82. MX_SPI1_Init();
  83. MX_SPI2_Init();
  84. MX_USART1_UART_Init();
  85. MX_USB_Device_Init();
  86. MX_TIM1_Init();
  87. MX_TIM2_Init();
  88. MX_TIM16_Init();
  89. MX_COMP1_Init();
  90. MX_RF_Init();
  91. MX_PKA_Init();
  92. MX_RNG_Init();
  93. MX_AES1_Init();
  94. MX_AES2_Init();
  95. MX_CRC_Init();
  96. /* USER CODE BEGIN 2 */
  97. /* USER CODE END 2 */
  98. /* Init scheduler */
  99. osKernelInitialize(); /* Call init function for freertos objects (in freertos.c) */
  100. MX_FREERTOS_Init();
  101. /* Start scheduler */
  102. osKernelStart();
  103. /* We should never get here as control is now taken by the scheduler */
  104. /* Infinite loop */
  105. /* USER CODE BEGIN WHILE */
  106. while (1)
  107. {
  108. /* USER CODE END WHILE */
  109. /* USER CODE BEGIN 3 */
  110. }
  111. /* USER CODE END 3 */
  112. }
  113. /**
  114. * @brief System Clock Configuration
  115. * @retval None
  116. */
  117. void SystemClock_Config(void)
  118. {
  119. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  120. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  121. RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
  122. /** Configure LSE Drive Capability
  123. */
  124. HAL_PWR_EnableBkUpAccess();
  125. __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_MEDIUMLOW);
  126. /** Configure the main internal regulator output voltage
  127. */
  128. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  129. /** Initializes the RCC Oscillators according to the specified parameters
  130. * in the RCC_OscInitTypeDef structure.
  131. */
  132. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE
  133. |RCC_OSCILLATORTYPE_LSE;
  134. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  135. RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  136. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  137. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  138. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  139. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  140. RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV2;
  141. RCC_OscInitStruct.PLL.PLLN = 8;
  142. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  143. RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  144. RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  145. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  146. {
  147. Error_Handler();
  148. }
  149. /** Configure the SYSCLKSource, HCLK, PCLK1 and PCLK2 clocks dividers
  150. */
  151. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK4|RCC_CLOCKTYPE_HCLK2
  152. |RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  153. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  154. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  155. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  156. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  157. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  158. RCC_ClkInitStruct.AHBCLK2Divider = RCC_SYSCLK_DIV2;
  159. RCC_ClkInitStruct.AHBCLK4Divider = RCC_SYSCLK_DIV1;
  160. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
  161. {
  162. Error_Handler();
  163. }
  164. /** Initializes the peripherals clocks
  165. */
  166. PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_SMPS|RCC_PERIPHCLK_RFWAKEUP
  167. |RCC_PERIPHCLK_RTC|RCC_PERIPHCLK_USART1
  168. |RCC_PERIPHCLK_I2C1|RCC_PERIPHCLK_CLK48SEL
  169. |RCC_PERIPHCLK_USB|RCC_PERIPHCLK_RNG
  170. |RCC_PERIPHCLK_ADC;
  171. PeriphClkInitStruct.PLLSAI1.PLLN = 6;
  172. PeriphClkInitStruct.PLLSAI1.PLLP = RCC_PLLP_DIV2;
  173. PeriphClkInitStruct.PLLSAI1.PLLQ = RCC_PLLQ_DIV2;
  174. PeriphClkInitStruct.PLLSAI1.PLLR = RCC_PLLR_DIV2;
  175. PeriphClkInitStruct.PLLSAI1.PLLSAI1ClockOut = RCC_PLLSAI1_USBCLK|RCC_PLLSAI1_ADCCLK;
  176. PeriphClkInitStruct.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
  177. PeriphClkInitStruct.I2c1ClockSelection = RCC_I2C1CLKSOURCE_PCLK1;
  178. PeriphClkInitStruct.UsbClockSelection = RCC_USBCLKSOURCE_PLLSAI1;
  179. PeriphClkInitStruct.RngClockSelection = RCC_RNGCLKSOURCE_CLK48;
  180. PeriphClkInitStruct.AdcClockSelection = RCC_ADCCLKSOURCE_PLLSAI1;
  181. PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
  182. PeriphClkInitStruct.RFWakeUpClockSelection = RCC_RFWKPCLKSOURCE_LSE;
  183. PeriphClkInitStruct.SmpsClockSelection = RCC_SMPSCLKSOURCE_HSE;
  184. PeriphClkInitStruct.SmpsDivSelection = RCC_SMPSCLKDIV_RANGE1;
  185. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  186. {
  187. Error_Handler();
  188. }
  189. /* USER CODE BEGIN Smps */
  190. /* USER CODE END Smps */
  191. /** Enables the Clock Security System
  192. */
  193. HAL_RCC_EnableCSS();
  194. /** Enables the Clock Security System
  195. */
  196. HAL_RCCEx_EnableLSECSS();
  197. }
  198. /* USER CODE BEGIN 4 */
  199. /* USER CODE END 4 */
  200. /**
  201. * @brief This function is executed in case of error occurrence.
  202. * @retval None
  203. */
  204. void Error_Handler(void)
  205. {
  206. /* USER CODE BEGIN Error_Handler_Debug */
  207. /* User can add his own implementation to report the HAL error return state */
  208. __disable_irq();
  209. while (1)
  210. {
  211. }
  212. /* USER CODE END Error_Handler_Debug */
  213. }
  214. #ifdef USE_FULL_ASSERT
  215. /**
  216. * @brief Reports the name of the source file and the source line number
  217. * where the assert_param error has occurred.
  218. * @param file: pointer to the source file name
  219. * @param line: assert_param error line source number
  220. * @retval None
  221. */
  222. void assert_failed(uint8_t *file, uint32_t line)
  223. {
  224. /* USER CODE BEGIN 6 */
  225. /* User can add his own implementation to report the file name and line number,
  226. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  227. /* USER CODE END 6 */
  228. }
  229. #endif /* USE_FULL_ASSERT */
  230. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/