Advertisement
nt2ds

Untitled

Sep 25th, 2023
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.91 KB | Source Code | 0 0
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2023 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.  
  22. /* Private includes ----------------------------------------------------------*/
  23. /* USER CODE BEGIN Includes */
  24. /* USER CODE END Includes */
  25.  
  26. /* Private typedef -----------------------------------------------------------*/
  27. /* USER CODE BEGIN PTD */
  28.  
  29. /* USER CODE END PTD */
  30.  
  31. /* Private define ------------------------------------------------------------*/
  32. /* USER CODE BEGIN PD */
  33.  
  34. /* USER CODE END PD */
  35.  
  36. /* Private macro -------------------------------------------------------------*/
  37. /* USER CODE BEGIN PM */
  38.  
  39. /* USER CODE END PM */
  40.  
  41. /* Private variables ---------------------------------------------------------*/
  42. ADC_HandleTypeDef hadc1;
  43. DMA_HandleTypeDef hdma_adc1;
  44.  
  45. PCD_HandleTypeDef hpcd_USB_OTG_FS;
  46.  
  47. /* USER CODE BEGIN PV */
  48.  
  49. /* USER CODE END PV */
  50.  
  51. /* Private function prototypes -----------------------------------------------*/
  52. void SystemClock_Config(void);
  53. static void MX_GPIO_Init(void);
  54. static void MX_DMA_Init(void);
  55. static void MX_USB_OTG_FS_PCD_Init(void);
  56. static void MX_ADC1_Init(void);
  57. /* USER CODE BEGIN PFP */
  58.  
  59. /* USER CODE END PFP */
  60.  
  61. /* Private user code ---------------------------------------------------------*/
  62. /* USER CODE BEGIN 0 */
  63.  
  64. uint32_t value[1]; // POT VALUES TO BE STORED
  65.  
  66.  
  67. /* USER CODE END 0 */
  68.  
  69. /**
  70. * @brief The application entry point.
  71. * @retval int
  72. */
  73. int main(void)
  74. {
  75. /* USER CODE BEGIN 1 */
  76.  
  77. /* USER CODE END 1 */
  78.  
  79. /* MCU Configuration--------------------------------------------------------*/
  80.  
  81. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  82. HAL_Init();
  83.  
  84. /* USER CODE BEGIN Init */
  85.  
  86. /* USER CODE END Init */
  87.  
  88. /* Configure the system clock */
  89. SystemClock_Config();
  90.  
  91. /* USER CODE BEGIN SysInit */
  92.  
  93. /* USER CODE END SysInit */
  94.  
  95. /* Initialize all configured peripherals */
  96. MX_GPIO_Init();
  97. MX_DMA_Init();
  98. MX_USB_OTG_FS_PCD_Init();
  99. MX_ADC1_Init();
  100. /* USER CODE BEGIN 2 */
  101.  
  102. HAL_ADC_Start_DMA(&hadc1, value,1);
  103. /* USER CODE END 2 */
  104.  
  105. /* Infinite loop */
  106. /* USER CODE BEGIN WHILE */
  107. while (1)
  108. {
  109. /* USER CODE END WHILE */
  110.  
  111. /* USER CODE BEGIN 3 */
  112. HAL_GPIO_TogglePin(LD1_GPIO_Port, LD1_Pin);
  113.  
  114. HAL_Delay(1000);
  115.  
  116. }
  117. /* USER CODE END 3 */
  118. }
  119.  
  120. /**
  121. * @brief System Clock Configuration
  122. * @retval None
  123. */
  124. void SystemClock_Config(void)
  125. {
  126. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  127. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  128.  
  129. /** Configure the main internal regulator output voltage
  130. */
  131. __HAL_RCC_PWR_CLK_ENABLE();
  132. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  133.  
  134. /** Initializes the RCC Oscillators according to the specified parameters
  135. * in the RCC_OscInitTypeDef structure.
  136. */
  137. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  138. RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
  139. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  140. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  141. RCC_OscInitStruct.PLL.PLLM = 4;
  142. RCC_OscInitStruct.PLL.PLLN = 168;
  143. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  144. RCC_OscInitStruct.PLL.PLLQ = 7;
  145. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  146. {
  147. Error_Handler();
  148. }
  149.  
  150. /** Initializes the CPU, AHB and APB buses clocks
  151. */
  152. RCC_ClkInitStruct.ClockType = 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_DIV4;
  157. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV16;
  158.  
  159. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  160. {
  161. Error_Handler();
  162. }
  163. }
  164.  
  165. /**
  166. * @brief ADC1 Initialization Function
  167. * @param None
  168. * @retval None
  169. */
  170. static void MX_ADC1_Init(void)
  171. {
  172.  
  173. /* USER CODE BEGIN ADC1_Init 0 */
  174.  
  175. /* USER CODE END ADC1_Init 0 */
  176.  
  177. ADC_ChannelConfTypeDef sConfig = {0};
  178.  
  179. /* USER CODE BEGIN ADC1_Init 1 */
  180.  
  181. /* USER CODE END ADC1_Init 1 */
  182.  
  183. /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  184. */
  185. hadc1.Instance = ADC1;
  186. hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
  187. hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  188. hadc1.Init.ScanConvMode = DISABLE;
  189. hadc1.Init.ContinuousConvMode = ENABLE;
  190. hadc1.Init.DiscontinuousConvMode = DISABLE;
  191. hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  192. hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  193. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  194. hadc1.Init.NbrOfConversion = 1;
  195. hadc1.Init.DMAContinuousRequests = ENABLE;
  196. hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  197. if (HAL_ADC_Init(&hadc1) != HAL_OK)
  198. {
  199. Error_Handler();
  200. }
  201.  
  202. /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  203. */
  204. sConfig.Channel = ADC_CHANNEL_3;
  205. sConfig.Rank = 1;
  206. sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
  207. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  208. {
  209. Error_Handler();
  210. }
  211. /* USER CODE BEGIN ADC1_Init 2 */
  212.  
  213. /* USER CODE END ADC1_Init 2 */
  214.  
  215. }
  216.  
  217. /**
  218. * @brief USB_OTG_FS Initialization Function
  219. * @param None
  220. * @retval None
  221. */
  222. static void MX_USB_OTG_FS_PCD_Init(void)
  223. {
  224.  
  225. /* USER CODE BEGIN USB_OTG_FS_Init 0 */
  226.  
  227. /* USER CODE END USB_OTG_FS_Init 0 */
  228.  
  229. /* USER CODE BEGIN USB_OTG_FS_Init 1 */
  230.  
  231. /* USER CODE END USB_OTG_FS_Init 1 */
  232. hpcd_USB_OTG_FS.Instance = USB_OTG_FS;
  233. hpcd_USB_OTG_FS.Init.dev_endpoints = 4;
  234. hpcd_USB_OTG_FS.Init.speed = PCD_SPEED_FULL;
  235. hpcd_USB_OTG_FS.Init.dma_enable = DISABLE;
  236. hpcd_USB_OTG_FS.Init.phy_itface = PCD_PHY_EMBEDDED;
  237. hpcd_USB_OTG_FS.Init.Sof_enable = ENABLE;
  238. hpcd_USB_OTG_FS.Init.low_power_enable = DISABLE;
  239. hpcd_USB_OTG_FS.Init.lpm_enable = DISABLE;
  240. hpcd_USB_OTG_FS.Init.vbus_sensing_enable = ENABLE;
  241. hpcd_USB_OTG_FS.Init.use_dedicated_ep1 = DISABLE;
  242. if (HAL_PCD_Init(&hpcd_USB_OTG_FS) != HAL_OK)
  243. {
  244. Error_Handler();
  245. }
  246. /* USER CODE BEGIN USB_OTG_FS_Init 2 */
  247.  
  248. /* USER CODE END USB_OTG_FS_Init 2 */
  249.  
  250. }
  251.  
  252. /**
  253. * Enable DMA controller clock
  254. */
  255. static void MX_DMA_Init(void)
  256. {
  257.  
  258. /* DMA controller clock enable */
  259. __HAL_RCC_DMA2_CLK_ENABLE();
  260.  
  261. /* DMA interrupt init */
  262. /* DMA2_Stream0_IRQn interrupt configuration */
  263. HAL_NVIC_SetPriority(DMA2_Stream0_IRQn, 0, 0);
  264. HAL_NVIC_EnableIRQ(DMA2_Stream0_IRQn);
  265.  
  266. }
  267.  
  268. /**
  269. * @brief GPIO Initialization Function
  270. * @param None
  271. * @retval None
  272. */
  273. static void MX_GPIO_Init(void)
  274. {
  275. GPIO_InitTypeDef GPIO_InitStruct = {0};
  276. /* USER CODE BEGIN MX_GPIO_Init_1 */
  277. /* USER CODE END MX_GPIO_Init_1 */
  278.  
  279. /* GPIO Ports Clock Enable */
  280. __HAL_RCC_GPIOC_CLK_ENABLE();
  281. __HAL_RCC_GPIOH_CLK_ENABLE();
  282. __HAL_RCC_GPIOA_CLK_ENABLE();
  283. __HAL_RCC_GPIOB_CLK_ENABLE();
  284. __HAL_RCC_GPIOD_CLK_ENABLE();
  285. __HAL_RCC_GPIOG_CLK_ENABLE();
  286.  
  287. /*Configure GPIO pin Output Level */
  288. HAL_GPIO_WritePin(GPIOB, LD1_Pin|LD3_Pin|LD2_Pin, GPIO_PIN_RESET);
  289.  
  290. /*Configure GPIO pin Output Level */
  291. HAL_GPIO_WritePin(USB_PowerSwitchOn_GPIO_Port, USB_PowerSwitchOn_Pin, GPIO_PIN_RESET);
  292.  
  293. /*Configure GPIO pin : USER_Btn_Pin */
  294. GPIO_InitStruct.Pin = USER_Btn_Pin;
  295. GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  296. GPIO_InitStruct.Pull = GPIO_NOPULL;
  297. HAL_GPIO_Init(USER_Btn_GPIO_Port, &GPIO_InitStruct);
  298.  
  299. /*Configure GPIO pins : RMII_MDC_Pin RMII_RXD0_Pin RMII_RXD1_Pin */
  300. GPIO_InitStruct.Pin = RMII_MDC_Pin|RMII_RXD0_Pin|RMII_RXD1_Pin;
  301. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  302. GPIO_InitStruct.Pull = GPIO_NOPULL;
  303. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  304. GPIO_InitStruct.Alternate = GPIO_AF11_ETH;
  305. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  306.  
  307. /*Configure GPIO pins : RMII_REF_CLK_Pin RMII_MDIO_Pin RMII_CRS_DV_Pin */
  308. GPIO_InitStruct.Pin = RMII_REF_CLK_Pin|RMII_MDIO_Pin|RMII_CRS_DV_Pin;
  309. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  310. GPIO_InitStruct.Pull = GPIO_NOPULL;
  311. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  312. GPIO_InitStruct.Alternate = GPIO_AF11_ETH;
  313. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  314.  
  315. /*Configure GPIO pins : LD1_Pin LD3_Pin LD2_Pin */
  316. GPIO_InitStruct.Pin = LD1_Pin|LD3_Pin|LD2_Pin;
  317. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  318. GPIO_InitStruct.Pull = GPIO_NOPULL;
  319. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  320. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  321.  
  322. /*Configure GPIO pin : RMII_TXD1_Pin */
  323. GPIO_InitStruct.Pin = RMII_TXD1_Pin;
  324. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  325. GPIO_InitStruct.Pull = GPIO_NOPULL;
  326. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  327. GPIO_InitStruct.Alternate = GPIO_AF11_ETH;
  328. HAL_GPIO_Init(RMII_TXD1_GPIO_Port, &GPIO_InitStruct);
  329.  
  330. /*Configure GPIO pins : STLK_RX_Pin STLK_TX_Pin */
  331. GPIO_InitStruct.Pin = STLK_RX_Pin|STLK_TX_Pin;
  332. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  333. GPIO_InitStruct.Pull = GPIO_NOPULL;
  334. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  335. GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
  336. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  337.  
  338. /*Configure GPIO pin : USB_PowerSwitchOn_Pin */
  339. GPIO_InitStruct.Pin = USB_PowerSwitchOn_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(USB_PowerSwitchOn_GPIO_Port, &GPIO_InitStruct);
  344.  
  345. /*Configure GPIO pin : USB_OverCurrent_Pin */
  346. GPIO_InitStruct.Pin = USB_OverCurrent_Pin;
  347. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  348. GPIO_InitStruct.Pull = GPIO_NOPULL;
  349. HAL_GPIO_Init(USB_OverCurrent_GPIO_Port, &GPIO_InitStruct);
  350.  
  351. /*Configure GPIO pins : RMII_TX_EN_Pin RMII_TXD0_Pin */
  352. GPIO_InitStruct.Pin = RMII_TX_EN_Pin|RMII_TXD0_Pin;
  353. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  354. GPIO_InitStruct.Pull = GPIO_NOPULL;
  355. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  356. GPIO_InitStruct.Alternate = GPIO_AF11_ETH;
  357. HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
  358.  
  359. /*Configure GPIO pin : PB8 */
  360. GPIO_InitStruct.Pin = GPIO_PIN_8;
  361. GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  362. GPIO_InitStruct.Pull = GPIO_NOPULL;
  363. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  364. GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
  365. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  366.  
  367. /* USER CODE BEGIN MX_GPIO_Init_2 */
  368. /* USER CODE END MX_GPIO_Init_2 */
  369. }
  370.  
  371. /* USER CODE BEGIN 4 */
  372.  
  373. /* USER CODE END 4 */
  374.  
  375. /**
  376. * @brief This function is executed in case of error occurrence.
  377. * @retval None
  378. */
  379. void Error_Handler(void)
  380. {
  381. /* USER CODE BEGIN Error_Handler_Debug */
  382. /* User can add his own implementation to report the HAL error return state */
  383. __disable_irq();
  384. while (1)
  385. {
  386. }
  387. /* USER CODE END Error_Handler_Debug */
  388. }
  389.  
  390. #ifdef USE_FULL_ASSERT
  391. /**
  392. * @brief Reports the name of the source file and the source line number
  393. * where the assert_param error has occurred.
  394. * @param file: pointer to the source file name
  395. * @param line: assert_param error line source number
  396. * @retval None
  397. */
  398. void assert_failed(uint8_t *file, uint32_t line)
  399. {
  400. /* USER CODE BEGIN 6 */
  401. /* User can add his own implementation to report the file name and line number,
  402. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  403. /* USER CODE END 6 */
  404. }
  405. #endif /* USE_FULL_ASSERT */
  406.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement