main.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. /* USER CODE BEGIN 1 */
  66. /* USER CODE END 1 */
  67. /* MCU Configuration--------------------------------------------------------*/
  68. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  69. HAL_Init();
  70. /* USER CODE BEGIN Init */
  71. /* USER CODE END Init */
  72. /* Configure the system clock */
  73. SystemClock_Config();
  74. /* USER CODE BEGIN SysInit */
  75. /* USER CODE END SysInit */
  76. /* Initialize all configured peripherals */
  77. MX_GPIO_Init();
  78. MX_ADC1_Init();
  79. MX_I2C1_Init();
  80. MX_RTC_Init();
  81. MX_SPI1_Init();
  82. MX_SPI2_Init();
  83. MX_USB_Device_Init();
  84. MX_TIM1_Init();
  85. MX_TIM2_Init();
  86. MX_TIM16_Init();
  87. MX_COMP1_Init();
  88. MX_RF_Init();
  89. MX_PKA_Init();
  90. MX_RNG_Init();
  91. MX_AES1_Init();
  92. MX_AES2_Init();
  93. MX_CRC_Init();
  94. MX_USART1_UART_Init();
  95. /* USER CODE BEGIN 2 */
  96. /* USER CODE END 2 */
  97. /* Init scheduler */
  98. osKernelInitialize(); /* Call init function for freertos objects (in freertos.c) */
  99. MX_FREERTOS_Init();
  100. /* Start scheduler */
  101. osKernelStart();
  102. /* We should never get here as control is now taken by the scheduler */
  103. /* Infinite loop */
  104. /* USER CODE BEGIN WHILE */
  105. while(1) {
  106. /* USER CODE END WHILE */
  107. /* USER CODE BEGIN 3 */
  108. }
  109. /* USER CODE END 3 */
  110. }
  111. /**
  112. * @brief System Clock Configuration
  113. * @retval None
  114. */
  115. void SystemClock_Config(void) {
  116. LL_FLASH_SetLatency(LL_FLASH_LATENCY_3);
  117. while(LL_FLASH_GetLatency() != LL_FLASH_LATENCY_3) {
  118. }
  119. /* HSE configuration and activation */
  120. LL_RCC_HSE_Enable();
  121. while(LL_RCC_HSE_IsReady() != 1) {
  122. }
  123. /* HSI configuration and activation */
  124. LL_RCC_HSI_Enable();
  125. while(LL_RCC_HSI_IsReady() != 1) {
  126. }
  127. LL_PWR_EnableBkUpAccess();
  128. if(LL_RCC_GetRTCClockSource() != LL_RCC_RTC_CLKSOURCE_LSE) {
  129. LL_RCC_ForceBackupDomainReset();
  130. LL_RCC_ReleaseBackupDomainReset();
  131. }
  132. LL_RCC_LSE_SetDriveCapability(LL_RCC_LSEDRIVE_MEDIUMLOW);
  133. LL_RCC_LSE_Enable();
  134. /* Wait till LSE is ready */
  135. while(LL_RCC_LSE_IsReady() != 1) {
  136. }
  137. LL_RCC_HSE_EnableCSS();
  138. LL_RCC_LSE_EnableCSS();
  139. /* Main PLL configuration and activation */
  140. LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE, LL_RCC_PLLM_DIV_2, 8, LL_RCC_PLLR_DIV_2);
  141. LL_RCC_PLL_Enable();
  142. LL_RCC_PLL_EnableDomain_SYS();
  143. while(LL_RCC_PLL_IsReady() != 1) {
  144. }
  145. LL_RCC_PLLSAI1_ConfigDomain_48M(
  146. LL_RCC_PLLSOURCE_HSE, LL_RCC_PLLM_DIV_2, 6, LL_RCC_PLLSAI1Q_DIV_2);
  147. LL_RCC_PLLSAI1_ConfigDomain_ADC(
  148. LL_RCC_PLLSOURCE_HSE, LL_RCC_PLLM_DIV_2, 6, LL_RCC_PLLSAI1R_DIV_2);
  149. LL_RCC_PLLSAI1_Enable();
  150. LL_RCC_PLLSAI1_EnableDomain_48M();
  151. LL_RCC_PLLSAI1_EnableDomain_ADC();
  152. /* Wait till PLLSAI1 is ready */
  153. while(LL_RCC_PLLSAI1_IsReady() != 1) {
  154. }
  155. /* Sysclk activation on the main PLL */
  156. /* Set CPU1 prescaler*/
  157. LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
  158. /* Set CPU2 prescaler*/
  159. LL_C2_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_2);
  160. LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
  161. while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL) {
  162. }
  163. /* Set AHB SHARED prescaler*/
  164. LL_RCC_SetAHB4Prescaler(LL_RCC_SYSCLK_DIV_1);
  165. /* Set APB1 prescaler*/
  166. LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_1);
  167. /* Set APB2 prescaler*/
  168. LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_1);
  169. /* Disable MSI */
  170. LL_RCC_MSI_Disable();
  171. while(LL_RCC_MSI_IsReady() != 0) {
  172. }
  173. /* Update CMSIS variable (which can be updated also through SystemCoreClockUpdate function) */
  174. LL_SetSystemCoreClock(64000000);
  175. /* Update the time base */
  176. if(HAL_InitTick(TICK_INT_PRIORITY) != HAL_OK) {
  177. Error_Handler();
  178. }
  179. if(LL_RCC_GetRTCClockSource() != LL_RCC_RTC_CLKSOURCE_LSE) {
  180. LL_RCC_SetRTCClockSource(LL_RCC_RTC_CLKSOURCE_LSE);
  181. }
  182. LL_RCC_EnableRTC();
  183. LL_RCC_SetUSARTClockSource(LL_RCC_USART1_CLKSOURCE_PCLK2);
  184. LL_RCC_SetADCClockSource(LL_RCC_ADC_CLKSOURCE_PLLSAI1);
  185. LL_RCC_SetI2CClockSource(LL_RCC_I2C1_CLKSOURCE_PCLK1);
  186. LL_RCC_SetRNGClockSource(LL_RCC_RNG_CLKSOURCE_CLK48);
  187. LL_RCC_SetUSBClockSource(LL_RCC_USB_CLKSOURCE_PLLSAI1);
  188. LL_RCC_SetCLK48ClockSource(LL_RCC_CLK48_CLKSOURCE_PLLSAI1);
  189. LL_RCC_SetSMPSClockSource(LL_RCC_SMPS_CLKSOURCE_HSE);
  190. LL_RCC_SetSMPSPrescaler(LL_RCC_SMPS_DIV_1);
  191. LL_RCC_SetRFWKPClockSource(LL_RCC_RFWKP_CLKSOURCE_LSE);
  192. /* USER CODE BEGIN Smps */
  193. /* USER CODE END Smps */
  194. }
  195. /* USER CODE BEGIN 4 */
  196. /* USER CODE END 4 */
  197. /**
  198. * @brief This function is executed in case of error occurrence.
  199. * @retval None
  200. */
  201. void Error_Handler(void) {
  202. /* USER CODE BEGIN Error_Handler_Debug */
  203. /* User can add his own implementation to report the HAL error return state */
  204. __disable_irq();
  205. while(1) {
  206. }
  207. /* USER CODE END Error_Handler_Debug */
  208. }
  209. #ifdef USE_FULL_ASSERT
  210. /**
  211. * @brief Reports the name of the source file and the source line number
  212. * where the assert_param error has occurred.
  213. * @param file: pointer to the source file name
  214. * @param line: assert_param error line source number
  215. * @retval None
  216. */
  217. void assert_failed(uint8_t* file, uint32_t line) {
  218. /* USER CODE BEGIN 6 */
  219. /* User can add his own implementation to report the file name and line number,
  220. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  221. /* USER CODE END 6 */
  222. }
  223. #endif /* USE_FULL_ASSERT */
  224. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/