Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 

302 рядки
7.5 KiB

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2026 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. UART_HandleTypeDef huart1;
  36. DMA_HandleTypeDef hdma_usart1_rx;
  37. DMA_HandleTypeDef hdma_usart1_tx;
  38. /* USER CODE BEGIN PV */
  39. static const uint8_t uart1_test_message[] = "USART1 DMA TX TEST\r\n";
  40. static volatile uint32_t uart1_tx_ok_count;
  41. static volatile uint32_t uart1_tx_busy_count;
  42. static volatile uint32_t uart1_tx_error_count;
  43. static volatile uint32_t uart1_tx_complete_count;
  44. /* USER CODE END PV */
  45. /* Private function prototypes -----------------------------------------------*/
  46. void SystemClock_Config(void);
  47. static void MX_GPIO_Init(void);
  48. static void MX_DMA_Init(void);
  49. static void MX_USART1_UART_Init(void);
  50. /* USER CODE BEGIN PFP */
  51. /* USER CODE END PFP */
  52. /* Private user code ---------------------------------------------------------*/
  53. /* USER CODE BEGIN 0 */
  54. /* USER CODE END 0 */
  55. /**
  56. * @brief The application entry point.
  57. * @retval int
  58. */
  59. int main(void)
  60. {
  61. /* USER CODE BEGIN 1 */
  62. /* USER CODE END 1 */
  63. /* MCU Configuration--------------------------------------------------------*/
  64. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  65. HAL_Init();
  66. /* USER CODE BEGIN Init */
  67. /* USER CODE END Init */
  68. /* Configure the system clock */
  69. SystemClock_Config();
  70. /* USER CODE BEGIN SysInit */
  71. /* USER CODE END SysInit */
  72. /* Initialize all configured peripherals */
  73. MX_GPIO_Init();
  74. MX_DMA_Init();
  75. MX_USB_DEVICE_Init();
  76. MX_USART1_UART_Init();
  77. /* USER CODE BEGIN 2 */
  78. /* USER CODE END 2 */
  79. /* Infinite loop */
  80. /* USER CODE BEGIN WHILE */
  81. while (1)
  82. {
  83. /* USER CODE END WHILE */
  84. /* USER CODE BEGIN 3 */
  85. HAL_StatusTypeDef uart_status;
  86. uart_status = HAL_UART_Transmit_DMA(&huart1,
  87. (uint8_t *)uart1_test_message,
  88. sizeof(uart1_test_message) - 1U);
  89. if (uart_status == HAL_OK)
  90. {
  91. uart1_tx_ok_count++;
  92. }
  93. else if (uart_status == HAL_BUSY)
  94. {
  95. uart1_tx_busy_count++;
  96. }
  97. else
  98. {
  99. uart1_tx_error_count++;
  100. }
  101. HAL_Delay(1000U);
  102. }
  103. /* USER CODE END 3 */
  104. }
  105. /**
  106. * @brief System Clock Configuration
  107. * @retval None
  108. */
  109. void SystemClock_Config(void)
  110. {
  111. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  112. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  113. /** Configure the main internal regulator output voltage
  114. */
  115. __HAL_RCC_PWR_CLK_ENABLE();
  116. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  117. /** Initializes the RCC Oscillators according to the specified parameters
  118. * in the RCC_OscInitTypeDef structure.
  119. */
  120. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  121. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  122. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  123. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  124. RCC_OscInitStruct.PLL.PLLM = 12;
  125. RCC_OscInitStruct.PLL.PLLN = 336;
  126. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  127. RCC_OscInitStruct.PLL.PLLQ = 7;
  128. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  129. {
  130. Error_Handler();
  131. }
  132. /** Initializes the CPU, AHB and APB buses clocks
  133. */
  134. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  135. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  136. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  137. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  138. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  139. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  140. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  141. {
  142. Error_Handler();
  143. }
  144. /** Enables the Clock Security System
  145. */
  146. HAL_RCC_EnableCSS();
  147. }
  148. /**
  149. * @brief USART1 Initialization Function
  150. * @param None
  151. * @retval None
  152. */
  153. static void MX_USART1_UART_Init(void)
  154. {
  155. /* USER CODE BEGIN USART1_Init 0 */
  156. /* USER CODE END USART1_Init 0 */
  157. /* USER CODE BEGIN USART1_Init 1 */
  158. /* USER CODE END USART1_Init 1 */
  159. huart1.Instance = USART1;
  160. huart1.Init.BaudRate = 115200;
  161. huart1.Init.WordLength = UART_WORDLENGTH_9B;
  162. huart1.Init.StopBits = UART_STOPBITS_1;
  163. huart1.Init.Parity = UART_PARITY_EVEN;
  164. huart1.Init.Mode = UART_MODE_TX_RX;
  165. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  166. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  167. if (HAL_UART_Init(&huart1) != HAL_OK)
  168. {
  169. Error_Handler();
  170. }
  171. /* USER CODE BEGIN USART1_Init 2 */
  172. /* USER CODE END USART1_Init 2 */
  173. }
  174. /**
  175. * Enable DMA controller clock
  176. */
  177. static void MX_DMA_Init(void)
  178. {
  179. /* DMA controller clock enable */
  180. __HAL_RCC_DMA2_CLK_ENABLE();
  181. /* DMA interrupt init */
  182. /* DMA2_Stream2_IRQn interrupt configuration */
  183. HAL_NVIC_SetPriority(DMA2_Stream2_IRQn, 5, 0);
  184. HAL_NVIC_EnableIRQ(DMA2_Stream2_IRQn);
  185. /* DMA2_Stream7_IRQn interrupt configuration */
  186. HAL_NVIC_SetPriority(DMA2_Stream7_IRQn, 5, 0);
  187. HAL_NVIC_EnableIRQ(DMA2_Stream7_IRQn);
  188. }
  189. /**
  190. * @brief GPIO Initialization Function
  191. * @param None
  192. * @retval None
  193. */
  194. static void MX_GPIO_Init(void)
  195. {
  196. /* USER CODE BEGIN MX_GPIO_Init_1 */
  197. /* USER CODE END MX_GPIO_Init_1 */
  198. /* GPIO Ports Clock Enable */
  199. __HAL_RCC_GPIOH_CLK_ENABLE();
  200. __HAL_RCC_GPIOA_CLK_ENABLE();
  201. /* USER CODE BEGIN MX_GPIO_Init_2 */
  202. /* USER CODE END MX_GPIO_Init_2 */
  203. }
  204. /* USER CODE BEGIN 4 */
  205. void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
  206. {
  207. if (huart->Instance == USART1)
  208. {
  209. uart1_tx_complete_count++;
  210. }
  211. }
  212. /* USER CODE END 4 */
  213. /**
  214. * @brief This function is executed in case of error occurrence.
  215. * @retval None
  216. */
  217. void Error_Handler(void)
  218. {
  219. /* USER CODE BEGIN Error_Handler_Debug */
  220. /* User can add his own implementation to report the HAL error return state */
  221. __disable_irq();
  222. while (1)
  223. {
  224. }
  225. /* USER CODE END Error_Handler_Debug */
  226. }
  227. #ifdef USE_FULL_ASSERT
  228. /**
  229. * @brief Reports the name of the source file and the source line number
  230. * where the assert_param error has occurred.
  231. * @param file: pointer to the source file name
  232. * @param line: assert_param error line source number
  233. * @retval None
  234. */
  235. void assert_failed(uint8_t *file, uint32_t line)
  236. {
  237. /* USER CODE BEGIN 6 */
  238. /* User can add his own implementation to report the file name and line number,
  239. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  240. /* USER CODE END 6 */
  241. }
  242. #endif /* USE_FULL_ASSERT */