main.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2024 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 "usb_device.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. DMA_HandleTypeDef hdma_adc1;
  37. TIM_HandleTypeDef htim3;
  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_DMA_Init(void);
  44. static void MX_ADC1_Init(void);
  45. static void MX_TIM3_Init(void);
  46. /* USER CODE BEGIN PFP */
  47. /* USER CODE END PFP */
  48. /* Private user code ---------------------------------------------------------*/
  49. /* USER CODE BEGIN 0 */
  50. /* USER CODE END 0 */
  51. /**
  52. * @brief The application entry point.
  53. * @retval int
  54. */
  55. int main(void)
  56. {
  57. /* USER CODE BEGIN 1 */
  58. /* USER CODE END 1 */
  59. /* MCU Configuration--------------------------------------------------------*/
  60. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  61. HAL_Init();
  62. /* USER CODE BEGIN Init */
  63. /* USER CODE END Init */
  64. /* Configure the system clock */
  65. SystemClock_Config();
  66. /* USER CODE BEGIN SysInit */
  67. /* USER CODE END SysInit */
  68. /* Initialize all configured peripherals */
  69. MX_GPIO_Init();
  70. MX_DMA_Init();
  71. MX_USB_DEVICE_Init();
  72. MX_ADC1_Init();
  73. MX_TIM3_Init();
  74. /* USER CODE BEGIN 2 */
  75. /* USER CODE END 2 */
  76. /* Infinite loop */
  77. /* USER CODE BEGIN WHILE */
  78. while (1)
  79. {
  80. /* USER CODE END WHILE */
  81. /* USER CODE BEGIN 3 */
  82. }
  83. /* USER CODE END 3 */
  84. }
  85. /**
  86. * @brief System Clock Configuration
  87. * @retval None
  88. */
  89. void SystemClock_Config(void)
  90. {
  91. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  92. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  93. /** Configure the main internal regulator output voltage
  94. */
  95. __HAL_RCC_PWR_CLK_ENABLE();
  96. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  97. /** Initializes the RCC Oscillators according to the specified parameters
  98. * in the RCC_OscInitTypeDef structure.
  99. */
  100. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE;
  101. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  102. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  103. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  104. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  105. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  106. RCC_OscInitStruct.PLL.PLLM = 15;
  107. RCC_OscInitStruct.PLL.PLLN = 144;
  108. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  109. RCC_OscInitStruct.PLL.PLLQ = 5;
  110. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  111. {
  112. Error_Handler();
  113. }
  114. /** Initializes the CPU, AHB and APB buses clocks
  115. */
  116. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  117. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  118. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  119. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  120. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  121. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  122. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  123. {
  124. Error_Handler();
  125. }
  126. }
  127. /**
  128. * @brief ADC1 Initialization Function
  129. * @param None
  130. * @retval None
  131. */
  132. static void MX_ADC1_Init(void)
  133. {
  134. /* USER CODE BEGIN ADC1_Init 0 */
  135. /* USER CODE END ADC1_Init 0 */
  136. ADC_ChannelConfTypeDef sConfig = {0};
  137. /* USER CODE BEGIN ADC1_Init 1 */
  138. /* USER CODE END ADC1_Init 1 */
  139. /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  140. */
  141. hadc1.Instance = ADC1;
  142. hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
  143. hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  144. hadc1.Init.ScanConvMode = DISABLE;
  145. hadc1.Init.ContinuousConvMode = DISABLE;
  146. hadc1.Init.DiscontinuousConvMode = DISABLE;
  147. hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING;
  148. hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T3_TRGO;
  149. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  150. hadc1.Init.NbrOfConversion = 1;
  151. hadc1.Init.DMAContinuousRequests = ENABLE;
  152. hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  153. if (HAL_ADC_Init(&hadc1) != HAL_OK)
  154. {
  155. Error_Handler();
  156. }
  157. /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  158. */
  159. sConfig.Channel = ADC_CHANNEL_0;
  160. sConfig.Rank = 1;
  161. sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
  162. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  163. {
  164. Error_Handler();
  165. }
  166. /* USER CODE BEGIN ADC1_Init 2 */
  167. /* USER CODE END ADC1_Init 2 */
  168. }
  169. /**
  170. * @brief TIM3 Initialization Function
  171. * @param None
  172. * @retval None
  173. */
  174. static void MX_TIM3_Init(void)
  175. {
  176. /* USER CODE BEGIN TIM3_Init 0 */
  177. /* USER CODE END TIM3_Init 0 */
  178. TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  179. TIM_MasterConfigTypeDef sMasterConfig = {0};
  180. /* USER CODE BEGIN TIM3_Init 1 */
  181. /* USER CODE END TIM3_Init 1 */
  182. htim3.Instance = TIM3;
  183. htim3.Init.Prescaler = 0;
  184. htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  185. htim3.Init.Period = 20;
  186. htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  187. htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  188. if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
  189. {
  190. Error_Handler();
  191. }
  192. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  193. if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
  194. {
  195. Error_Handler();
  196. }
  197. sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
  198. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  199. if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  200. {
  201. Error_Handler();
  202. }
  203. /* USER CODE BEGIN TIM3_Init 2 */
  204. /* USER CODE END TIM3_Init 2 */
  205. }
  206. /**
  207. * Enable DMA controller clock
  208. */
  209. static void MX_DMA_Init(void)
  210. {
  211. /* DMA controller clock enable */
  212. __HAL_RCC_DMA2_CLK_ENABLE();
  213. /* DMA interrupt init */
  214. /* DMA2_Stream0_IRQn interrupt configuration */
  215. HAL_NVIC_SetPriority(DMA2_Stream0_IRQn, 0, 0);
  216. HAL_NVIC_EnableIRQ(DMA2_Stream0_IRQn);
  217. }
  218. /**
  219. * @brief GPIO Initialization Function
  220. * @param None
  221. * @retval None
  222. */
  223. static void MX_GPIO_Init(void)
  224. {
  225. /* USER CODE BEGIN MX_GPIO_Init_1 */
  226. /* USER CODE END MX_GPIO_Init_1 */
  227. /* GPIO Ports Clock Enable */
  228. __HAL_RCC_GPIOC_CLK_ENABLE();
  229. __HAL_RCC_GPIOH_CLK_ENABLE();
  230. __HAL_RCC_GPIOA_CLK_ENABLE();
  231. /* USER CODE BEGIN MX_GPIO_Init_2 */
  232. /* USER CODE END MX_GPIO_Init_2 */
  233. }
  234. /* USER CODE BEGIN 4 */
  235. /* USER CODE END 4 */
  236. /**
  237. * @brief This function is executed in case of error occurrence.
  238. * @retval None
  239. */
  240. void Error_Handler(void)
  241. {
  242. /* USER CODE BEGIN Error_Handler_Debug */
  243. /* User can add his own implementation to report the HAL error return state */
  244. __disable_irq();
  245. while (1)
  246. {
  247. }
  248. /* USER CODE END Error_Handler_Debug */
  249. }
  250. #ifdef USE_FULL_ASSERT
  251. /**
  252. * @brief Reports the name of the source file and the source line number
  253. * where the assert_param error has occurred.
  254. * @param file: pointer to the source file name
  255. * @param line: assert_param error line source number
  256. * @retval None
  257. */
  258. void assert_failed(uint8_t *file, uint32_t line)
  259. {
  260. /* USER CODE BEGIN 6 */
  261. /* User can add his own implementation to report the file name and line number,
  262. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  263. /* USER CODE END 6 */
  264. }
  265. #endif /* USE_FULL_ASSERT */