main.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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) 2020 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. #include "fatfs/fatfs.h"
  40. #include "api-hal.h"
  41. /* USER CODE END Includes */
  42. /* Private typedef -----------------------------------------------------------*/
  43. /* USER CODE BEGIN PTD */
  44. /* USER CODE END PTD */
  45. /* Private define ------------------------------------------------------------*/
  46. /* USER CODE BEGIN PD */
  47. /* USER CODE END PD */
  48. /* Private macro -------------------------------------------------------------*/
  49. /* USER CODE BEGIN PM */
  50. /* USER CODE END PM */
  51. /* Private variables ---------------------------------------------------------*/
  52. /* USER CODE BEGIN PV */
  53. /* USER CODE END PV */
  54. /* Private function prototypes -----------------------------------------------*/
  55. void SystemClock_Config(void);
  56. void MX_FREERTOS_Init(void);
  57. /* USER CODE BEGIN PFP */
  58. /* USER CODE END PFP */
  59. /* Private user code ---------------------------------------------------------*/
  60. /* USER CODE BEGIN 0 */
  61. /* USER CODE END 0 */
  62. /**
  63. * @brief The application entry point.
  64. * @retval int
  65. */
  66. int main(void)
  67. {
  68. /* USER CODE BEGIN 1 */
  69. /* USER CODE END 1 */
  70. /* MCU Configuration--------------------------------------------------------*/
  71. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  72. HAL_Init();
  73. /* USER CODE BEGIN Init */
  74. /* USER CODE END Init */
  75. /* Configure the system clock */
  76. SystemClock_Config();
  77. /* USER CODE BEGIN SysInit */
  78. /* USER CODE END SysInit */
  79. /* Initialize all configured peripherals */
  80. MX_GPIO_Init();
  81. MX_ADC1_Init();
  82. MX_I2C1_Init();
  83. MX_RTC_Init();
  84. MX_SPI1_Init();
  85. MX_SPI2_Init();
  86. MX_USART1_UART_Init();
  87. MX_USB_Device_Init();
  88. MX_TIM1_Init();
  89. MX_TIM2_Init();
  90. MX_TIM16_Init();
  91. MX_COMP1_Init();
  92. MX_RF_Init();
  93. MX_PKA_Init();
  94. MX_RNG_Init();
  95. MX_AES1_Init();
  96. MX_AES2_Init();
  97. MX_CRC_Init();
  98. /* USER CODE BEGIN 2 */
  99. MX_FATFS_Init();
  100. delay_us_init_DWT();
  101. api_hal_vcp_init();
  102. api_hal_spi_init();
  103. /* USER CODE END 2 */
  104. /* Init scheduler */
  105. osKernelInitialize(); /* Call init function for freertos objects (in freertos.c) */
  106. MX_FREERTOS_Init();
  107. /* Start scheduler */
  108. osKernelStart();
  109. /* We should never get here as control is now taken by the scheduler */
  110. /* Infinite loop */
  111. /* USER CODE BEGIN WHILE */
  112. while (1)
  113. {
  114. /* USER CODE END WHILE */
  115. /* USER CODE BEGIN 3 */
  116. }
  117. /* USER CODE END 3 */
  118. }
  119. /**
  120. * @brief System Clock Configuration
  121. * @retval None
  122. */
  123. void SystemClock_Config(void)
  124. {
  125. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  126. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  127. RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
  128. /** Configure LSE Drive Capability
  129. */
  130. HAL_PWR_EnableBkUpAccess();
  131. __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_MEDIUMLOW);
  132. /** Configure the main internal regulator output voltage
  133. */
  134. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  135. /** Initializes the RCC Oscillators according to the specified parameters
  136. * in the RCC_OscInitTypeDef structure.
  137. */
  138. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE
  139. |RCC_OSCILLATORTYPE_LSE;
  140. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  141. RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  142. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  143. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  144. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  145. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  146. RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV2;
  147. RCC_OscInitStruct.PLL.PLLN = 8;
  148. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  149. RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  150. RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  151. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  152. {
  153. Error_Handler();
  154. }
  155. /** Configure the SYSCLKSource, HCLK, PCLK1 and PCLK2 clocks dividers
  156. */
  157. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK4|RCC_CLOCKTYPE_HCLK2
  158. |RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  159. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  160. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  161. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  162. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  163. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  164. RCC_ClkInitStruct.AHBCLK2Divider = RCC_SYSCLK_DIV2;
  165. RCC_ClkInitStruct.AHBCLK4Divider = RCC_SYSCLK_DIV1;
  166. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
  167. {
  168. Error_Handler();
  169. }
  170. /** Initializes the peripherals clocks
  171. */
  172. PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_SMPS|RCC_PERIPHCLK_RFWAKEUP
  173. |RCC_PERIPHCLK_RTC|RCC_PERIPHCLK_USART1
  174. |RCC_PERIPHCLK_I2C1|RCC_PERIPHCLK_CLK48SEL
  175. |RCC_PERIPHCLK_USB|RCC_PERIPHCLK_RNG
  176. |RCC_PERIPHCLK_ADC;
  177. PeriphClkInitStruct.PLLSAI1.PLLN = 6;
  178. PeriphClkInitStruct.PLLSAI1.PLLP = RCC_PLLP_DIV2;
  179. PeriphClkInitStruct.PLLSAI1.PLLQ = RCC_PLLQ_DIV2;
  180. PeriphClkInitStruct.PLLSAI1.PLLR = RCC_PLLR_DIV2;
  181. PeriphClkInitStruct.PLLSAI1.PLLSAI1ClockOut = RCC_PLLSAI1_USBCLK|RCC_PLLSAI1_ADCCLK;
  182. PeriphClkInitStruct.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
  183. PeriphClkInitStruct.I2c1ClockSelection = RCC_I2C1CLKSOURCE_PCLK1;
  184. PeriphClkInitStruct.UsbClockSelection = RCC_USBCLKSOURCE_PLLSAI1;
  185. PeriphClkInitStruct.RngClockSelection = RCC_RNGCLKSOURCE_CLK48;
  186. PeriphClkInitStruct.AdcClockSelection = RCC_ADCCLKSOURCE_PLLSAI1;
  187. PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
  188. PeriphClkInitStruct.RFWakeUpClockSelection = RCC_RFWKPCLKSOURCE_LSE;
  189. PeriphClkInitStruct.SmpsClockSelection = RCC_SMPSCLKSOURCE_HSE;
  190. PeriphClkInitStruct.SmpsDivSelection = RCC_SMPSCLKDIV_RANGE0;
  191. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  192. {
  193. Error_Handler();
  194. }
  195. /* USER CODE BEGIN Smps */
  196. if (!LL_RCC_LSE_IsReady()) {
  197. LL_RCC_ForceBackupDomainReset();
  198. LL_RCC_ReleaseBackupDomainReset();
  199. NVIC_SystemReset();
  200. }
  201. /* USER CODE END Smps */
  202. /** Enables the Clock Security System
  203. */
  204. HAL_RCC_EnableCSS();
  205. /** Enables the Clock Security System
  206. */
  207. HAL_RCCEx_EnableLSECSS();
  208. }
  209. /* USER CODE BEGIN 4 */
  210. /* USER CODE END 4 */
  211. /**
  212. * @brief Period elapsed callback in non blocking mode
  213. * @note This function is called when TIM17 interrupt took place, inside
  214. * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  215. * a global variable "uwTick" used as application time base.
  216. * @param htim : TIM handle
  217. * @retval None
  218. */
  219. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  220. {
  221. /* USER CODE BEGIN Callback 0 */
  222. /* USER CODE END Callback 0 */
  223. if (htim->Instance == TIM17) {
  224. HAL_IncTick();
  225. }
  226. /* USER CODE BEGIN Callback 1 */
  227. /* USER CODE END Callback 1 */
  228. }
  229. /**
  230. * @brief This function is executed in case of error occurrence.
  231. * @retval None
  232. */
  233. void Error_Handler(void)
  234. {
  235. /* USER CODE BEGIN Error_Handler_Debug */
  236. /* User can add his own implementation to report the HAL error return state */
  237. /* USER CODE END Error_Handler_Debug */
  238. }
  239. #ifdef USE_FULL_ASSERT
  240. /**
  241. * @brief Reports the name of the source file and the source line number
  242. * where the assert_param error has occurred.
  243. * @param file: pointer to the source file name
  244. * @param line: assert_param error line source number
  245. * @retval None
  246. */
  247. void assert_failed(uint8_t *file, uint32_t line)
  248. {
  249. /* USER CODE BEGIN 6 */
  250. /* User can add his own implementation to report the file name and line number,
  251. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  252. /* USER CODE END 6 */
  253. }
  254. #endif /* USE_FULL_ASSERT */
  255. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/