main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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 BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22. /* Private includes ----------------------------------------------------------*/
  23. /* USER CODE BEGIN Includes */
  24. /* USER CODE END Includes */
  25. /* Private typedef -----------------------------------------------------------*/
  26. /* USER CODE BEGIN PTD */
  27. /* USER CODE END PTD */
  28. /* Private define ------------------------------------------------------------*/
  29. /* USER CODE BEGIN PD */
  30. /* USER CODE END PD */
  31. /* Private macro -------------------------------------------------------------*/
  32. /* USER CODE BEGIN PM */
  33. /* USER CODE END PM */
  34. /* Private variables ---------------------------------------------------------*/
  35. ADC_HandleTypeDef hadc1;
  36. UART_HandleTypeDef huart1;
  37. PCD_HandleTypeDef hpcd_USB_OTG_FS;
  38. /* USER CODE BEGIN PV */
  39. /* USER CODE END PV */
  40. /* Private function prototypes -----------------------------------------------*/
  41. void SystemClock_Config(void);
  42. static void MX_GPIO_Init(void);
  43. static void MX_ADC1_Init(void);
  44. static void MX_UCPD1_Init(void);
  45. static void MX_USART1_UART_Init(void);
  46. static void MX_USB_OTG_FS_PCD_Init(void);
  47. static void MX_ICACHE_Init(void);
  48. /* USER CODE BEGIN PFP */
  49. /* USER CODE END PFP */
  50. /* Private user code ---------------------------------------------------------*/
  51. /* USER CODE BEGIN 0 */
  52. /* USER CODE END 0 */
  53. /**
  54. * @brief The application entry point.
  55. * @retval int
  56. */
  57. int main(void)
  58. {
  59. /* USER CODE BEGIN 1 */
  60. /* USER CODE END 1 */
  61. /* MCU Configuration--------------------------------------------------------*/
  62. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  63. HAL_Init();
  64. HAL_PWREx_EnableVddIO2();
  65. /* USER CODE BEGIN Init */
  66. /* USER CODE END Init */
  67. /* Configure the system clock */
  68. SystemClock_Config();
  69. /* USER CODE BEGIN SysInit */
  70. /* USER CODE END SysInit */
  71. /* Initialize all configured peripherals */
  72. MX_GPIO_Init();
  73. MX_ADC1_Init();
  74. MX_UCPD1_Init();
  75. MX_USART1_UART_Init();
  76. MX_USB_OTG_FS_PCD_Init();
  77. MX_ICACHE_Init();
  78. /* USER CODE BEGIN 2 */
  79. /* USER CODE END 2 */
  80. /* Infinite loop */
  81. /* USER CODE BEGIN WHILE */
  82. while (1)
  83. {
  84. /* USER CODE END WHILE */
  85. /* USER CODE BEGIN 3 */
  86. }
  87. /* USER CODE END 3 */
  88. }
  89. /**
  90. * @brief System Clock Configuration
  91. * @retval None
  92. */
  93. void SystemClock_Config(void)
  94. {
  95. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  96. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  97. /** Configure the main internal regulator output voltage
  98. */
  99. if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  100. {
  101. Error_Handler();
  102. }
  103. /** Initializes the CPU, AHB and APB busses clocks
  104. */
  105. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48|RCC_OSCILLATORTYPE_HSI;
  106. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  107. RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
  108. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  109. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  110. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  111. RCC_OscInitStruct.PLL.PLLMBOOST = RCC_PLLMBOOST_DIV1;
  112. RCC_OscInitStruct.PLL.PLLM = 1;
  113. RCC_OscInitStruct.PLL.PLLN = 10;
  114. RCC_OscInitStruct.PLL.PLLP = 2;
  115. RCC_OscInitStruct.PLL.PLLQ = 2;
  116. RCC_OscInitStruct.PLL.PLLR = 1;
  117. RCC_OscInitStruct.PLL.PLLRGE = RCC_PLLVCIRANGE_1;
  118. RCC_OscInitStruct.PLL.PLLFRACN = 0;
  119. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  120. {
  121. Error_Handler();
  122. }
  123. /** Initializes the CPU, AHB and APB busses clocks
  124. */
  125. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  126. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
  127. |RCC_CLOCKTYPE_PCLK3;
  128. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  129. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  130. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  131. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  132. RCC_ClkInitStruct.APB3CLKDivider = RCC_HCLK_DIV1;
  133. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  134. {
  135. Error_Handler();
  136. }
  137. __HAL_RCC_PWR_CLK_DISABLE();
  138. }
  139. /**
  140. * @brief ADC1 Initialization Function
  141. * @param None
  142. * @retval None
  143. */
  144. static void MX_ADC1_Init(void)
  145. {
  146. /* USER CODE BEGIN ADC1_Init 0 */
  147. /* USER CODE END ADC1_Init 0 */
  148. /* USER CODE BEGIN ADC1_Init 1 */
  149. /* USER CODE END ADC1_Init 1 */
  150. /** Common config
  151. */
  152. hadc1.Instance = ADC1;
  153. hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
  154. hadc1.Init.Resolution = ADC_RESOLUTION_14B;
  155. hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
  156. hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  157. hadc1.Init.LowPowerAutoWait = DISABLE;
  158. hadc1.Init.ContinuousConvMode = DISABLE;
  159. hadc1.Init.NbrOfConversion = 1;
  160. hadc1.Init.DiscontinuousConvMode = DISABLE;
  161. hadc1.Init.DMAContinuousRequests = DISABLE;
  162. hadc1.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_HIGH;
  163. hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
  164. hadc1.Init.LeftBitShift = ADC_LEFTBITSHIFT_NONE;
  165. hadc1.Init.ConversionDataManagement = ADC_CONVERSIONDATA_DR;
  166. hadc1.Init.OversamplingMode = DISABLE;
  167. if (HAL_ADC_Init(&hadc1) != HAL_OK)
  168. {
  169. Error_Handler();
  170. }
  171. /* USER CODE BEGIN ADC1_Init 2 */
  172. /* USER CODE END ADC1_Init 2 */
  173. }
  174. /**
  175. * @brief ICACHE Initialization Function
  176. * @param None
  177. * @retval None
  178. */
  179. static void MX_ICACHE_Init(void)
  180. {
  181. /* USER CODE BEGIN ICACHE_Init 0 */
  182. /* USER CODE END ICACHE_Init 0 */
  183. /* USER CODE BEGIN ICACHE_Init 1 */
  184. /* USER CODE END ICACHE_Init 1 */
  185. /** Enable instruction cache in 1-way (direct mapped cache)
  186. */
  187. if (HAL_ICACHE_ConfigAssociativityMode(ICACHE_1WAY) != HAL_OK)
  188. {
  189. Error_Handler();
  190. }
  191. if (HAL_ICACHE_Enable() != HAL_OK)
  192. {
  193. Error_Handler();
  194. }
  195. /* USER CODE BEGIN ICACHE_Init 2 */
  196. /* USER CODE END ICACHE_Init 2 */
  197. }
  198. /**
  199. * @brief UCPD1 Initialization Function
  200. * @param None
  201. * @retval None
  202. */
  203. static void MX_UCPD1_Init(void)
  204. {
  205. /* USER CODE BEGIN UCPD1_Init 0 */
  206. /* USER CODE END UCPD1_Init 0 */
  207. LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
  208. /* Peripheral clock enable */
  209. LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_UCPD1);
  210. LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOB);
  211. LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOA);
  212. /**UCPD1 GPIO Configuration
  213. PB15 ------> UCPD1_CC2
  214. PA15 (JTDI) ------> UCPD1_CC1
  215. */
  216. GPIO_InitStruct.Pin = LL_GPIO_PIN_15;
  217. GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
  218. GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  219. LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  220. GPIO_InitStruct.Pin = LL_GPIO_PIN_15;
  221. GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
  222. GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  223. LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  224. /* USER CODE BEGIN UCPD1_Init 1 */
  225. /* USER CODE END UCPD1_Init 1 */
  226. /* USER CODE BEGIN UCPD1_Init 2 */
  227. /* USER CODE END UCPD1_Init 2 */
  228. }
  229. /**
  230. * @brief USART1 Initialization Function
  231. * @param None
  232. * @retval None
  233. */
  234. static void MX_USART1_UART_Init(void)
  235. {
  236. /* USER CODE BEGIN USART1_Init 0 */
  237. /* USER CODE END USART1_Init 0 */
  238. /* USER CODE BEGIN USART1_Init 1 */
  239. /* USER CODE END USART1_Init 1 */
  240. huart1.Instance = USART1;
  241. huart1.Init.BaudRate = 115200;
  242. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  243. huart1.Init.StopBits = UART_STOPBITS_1;
  244. huart1.Init.Parity = UART_PARITY_NONE;
  245. huart1.Init.Mode = UART_MODE_TX_RX;
  246. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  247. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  248. huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  249. huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  250. huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  251. if (HAL_UART_Init(&huart1) != HAL_OK)
  252. {
  253. Error_Handler();
  254. }
  255. if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
  256. {
  257. Error_Handler();
  258. }
  259. if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
  260. {
  261. Error_Handler();
  262. }
  263. if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK)
  264. {
  265. Error_Handler();
  266. }
  267. /* USER CODE BEGIN USART1_Init 2 */
  268. /* USER CODE END USART1_Init 2 */
  269. }
  270. /**
  271. * @brief USB_OTG_FS Initialization Function
  272. * @param None
  273. * @retval None
  274. */
  275. static void MX_USB_OTG_FS_PCD_Init(void)
  276. {
  277. /* USER CODE BEGIN USB_OTG_FS_Init 0 */
  278. /* USER CODE END USB_OTG_FS_Init 0 */
  279. /* USER CODE BEGIN USB_OTG_FS_Init 1 */
  280. /* USER CODE END USB_OTG_FS_Init 1 */
  281. hpcd_USB_OTG_FS.Instance = USB_OTG_FS;
  282. hpcd_USB_OTG_FS.Init.dev_endpoints = 6;
  283. hpcd_USB_OTG_FS.Init.phy_itface = PCD_PHY_EMBEDDED;
  284. hpcd_USB_OTG_FS.Init.Sof_enable = DISABLE;
  285. hpcd_USB_OTG_FS.Init.low_power_enable = DISABLE;
  286. hpcd_USB_OTG_FS.Init.lpm_enable = DISABLE;
  287. hpcd_USB_OTG_FS.Init.battery_charging_enable = DISABLE;
  288. hpcd_USB_OTG_FS.Init.use_dedicated_ep1 = DISABLE;
  289. hpcd_USB_OTG_FS.Init.vbus_sensing_enable = DISABLE;
  290. if (HAL_PCD_Init(&hpcd_USB_OTG_FS) != HAL_OK)
  291. {
  292. Error_Handler();
  293. }
  294. /* USER CODE BEGIN USB_OTG_FS_Init 2 */
  295. /* USER CODE END USB_OTG_FS_Init 2 */
  296. }
  297. /**
  298. * @brief GPIO Initialization Function
  299. * @param None
  300. * @retval None
  301. */
  302. static void MX_GPIO_Init(void)
  303. {
  304. GPIO_InitTypeDef GPIO_InitStruct = {0};
  305. /* GPIO Ports Clock Enable */
  306. __HAL_RCC_GPIOC_CLK_ENABLE();
  307. __HAL_RCC_GPIOB_CLK_ENABLE();
  308. __HAL_RCC_GPIOG_CLK_ENABLE();
  309. __HAL_RCC_GPIOA_CLK_ENABLE();
  310. /*Configure GPIO pin Output Level */
  311. HAL_GPIO_WritePin(LED_RED_GPIO_Port, LED_RED_Pin, GPIO_PIN_RESET);
  312. /*Configure GPIO pin Output Level */
  313. HAL_GPIO_WritePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin, GPIO_PIN_RESET);
  314. /*Configure GPIO pin Output Level */
  315. HAL_GPIO_WritePin(GPIOB, UCPD_DBn_Pin|LED_BLUE_Pin, GPIO_PIN_RESET);
  316. /*Configure GPIO pin : USER_BUTTON_Pin */
  317. GPIO_InitStruct.Pin = USER_BUTTON_Pin;
  318. GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  319. GPIO_InitStruct.Pull = GPIO_NOPULL;
  320. HAL_GPIO_Init(USER_BUTTON_GPIO_Port, &GPIO_InitStruct);
  321. /*Configure GPIO pin : UCPD_FLT_Pin */
  322. GPIO_InitStruct.Pin = UCPD_FLT_Pin;
  323. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  324. GPIO_InitStruct.Pull = GPIO_NOPULL;
  325. HAL_GPIO_Init(UCPD_FLT_GPIO_Port, &GPIO_InitStruct);
  326. /*Configure GPIO pin : LED_RED_Pin */
  327. GPIO_InitStruct.Pin = LED_RED_Pin;
  328. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  329. GPIO_InitStruct.Pull = GPIO_PULLUP;
  330. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  331. HAL_GPIO_Init(LED_RED_GPIO_Port, &GPIO_InitStruct);
  332. /*Configure GPIO pin : LED_GREEN_Pin */
  333. GPIO_InitStruct.Pin = LED_GREEN_Pin;
  334. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  335. GPIO_InitStruct.Pull = GPIO_PULLUP;
  336. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  337. HAL_GPIO_Init(LED_GREEN_GPIO_Port, &GPIO_InitStruct);
  338. /*Configure GPIO pin : UCPD_DBn_Pin */
  339. GPIO_InitStruct.Pin = UCPD_DBn_Pin;
  340. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  341. GPIO_InitStruct.Pull = GPIO_NOPULL;
  342. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  343. HAL_GPIO_Init(UCPD_DBn_GPIO_Port, &GPIO_InitStruct);
  344. /*Configure GPIO pin : LED_BLUE_Pin */
  345. GPIO_InitStruct.Pin = LED_BLUE_Pin;
  346. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  347. GPIO_InitStruct.Pull = GPIO_PULLUP;
  348. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  349. HAL_GPIO_Init(LED_BLUE_GPIO_Port, &GPIO_InitStruct);
  350. }
  351. /* USER CODE BEGIN 4 */
  352. /* USER CODE END 4 */
  353. /**
  354. * @brief This function is executed in case of error occurrence.
  355. * @retval None
  356. */
  357. void Error_Handler(void)
  358. {
  359. /* USER CODE BEGIN Error_Handler_Debug */
  360. /* User can add his own implementation to report the HAL error return state */
  361. __disable_irq();
  362. while (1)
  363. {
  364. }
  365. /* USER CODE END Error_Handler_Debug */
  366. }
  367. #ifdef USE_FULL_ASSERT
  368. /**
  369. * @brief Reports the name of the source file and the source line number
  370. * where the assert_param error has occurred.
  371. * @param file: pointer to the source file name
  372. * @param line: assert_param error line source number
  373. * @retval None
  374. */
  375. void assert_failed(uint8_t *file, uint32_t line)
  376. {
  377. /* USER CODE BEGIN 6 */
  378. /* User can add his own implementation to report the file name and line number,
  379. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  380. /* USER CODE END 6 */
  381. }
  382. #endif /* USE_FULL_ASSERT */
  383. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/