25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 

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