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

375 рядки
10 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. /* uC/OS-II的公开接口、任务类型和配置宏都由此头文件提供。 */
  25. #include "ucos_ii.h"
  26. /* USER CODE END Includes */
  27. /* Private typedef -----------------------------------------------------------*/
  28. /* USER CODE BEGIN PTD */
  29. /* USER CODE END PTD */
  30. /* Private define ------------------------------------------------------------*/
  31. /* USER CODE BEGIN PD */
  32. /* USER CODE END PD */
  33. /* Private macro -------------------------------------------------------------*/
  34. /* USER CODE BEGIN PM */
  35. /* USER CODE END PM */
  36. /* Private variables ---------------------------------------------------------*/
  37. UART_HandleTypeDef huart1;
  38. DMA_HandleTypeDef hdma_usart1_rx;
  39. DMA_HandleTypeDef hdma_usart1_tx;
  40. /* USER CODE BEGIN PV */
  41. static const uint8_t uart1_test_message[] = "USART1 DMA TX TEST\r\n";
  42. static volatile uint32_t uart1_tx_ok_count;
  43. static volatile uint32_t uart1_tx_busy_count;
  44. static volatile uint32_t uart1_tx_error_count;
  45. static volatile uint32_t uart1_tx_complete_count;
  46. /*
  47. * 每个任务必须拥有独立栈空间。
  48. * OS_STK在本工程中为32位,因此256个OS_STK占用1024字节RAM。
  49. * Cortex-M栈从高地址向低地址增长,创建任务时要把数组末元素作为栈顶传入。
  50. */
  51. static OS_STK AppTaskStartStk[APP_TASK_START_STK_SIZE];
  52. /* USER CODE END PV */
  53. /* Private function prototypes -----------------------------------------------*/
  54. void SystemClock_Config(void);
  55. static void MX_GPIO_Init(void);
  56. static void MX_DMA_Init(void);
  57. static void MX_USART1_UART_Init(void);
  58. /* USER CODE BEGIN PFP */
  59. static void AppTaskStart(void *p_arg);
  60. /* USER CODE END PFP */
  61. /* Private user code ---------------------------------------------------------*/
  62. /* USER CODE BEGIN 0 */
  63. /*
  64. * uC/OS-II任务函数的固定形式是 void Task(void *p_arg)。
  65. * 任务通常包含一个永久循环;需要等待时应调用OS延时或等待内核对象,
  66. * 让出CPU给其他就绪任务,而不是在任务里忙等待。
  67. */
  68. static void AppTaskStart(void *p_arg)
  69. {
  70. HAL_StatusTypeDef uart_status;
  71. /* 当前没有给任务传递参数,显式转换可避免编译器产生未使用警告。 */
  72. (void)p_arg;
  73. while (1)
  74. {
  75. /* DMA发送启动后立即返回,真正的发送完成由DMA中断回调通知。 */
  76. uart_status = HAL_UART_Transmit_DMA(&huart1,
  77. (uint8_t *)uart1_test_message,
  78. sizeof(uart1_test_message) - 1U);
  79. if (uart_status == HAL_OK)
  80. {
  81. uart1_tx_ok_count++;
  82. }
  83. else if (uart_status == HAL_BUSY)
  84. {
  85. uart1_tx_busy_count++;
  86. }
  87. else
  88. {
  89. uart1_tx_error_count++;
  90. }
  91. /*
  92. * 把当前任务延时OS_TICKS_PER_SEC个系统节拍。
  93. * 当前OS_TICKS_PER_SEC为1000,SysTick周期为1ms,因此这里延时1秒。
  94. * 延时期间任务处于等待态,内核会调度其他就绪任务(目前主要是空闲任务)。
  95. */
  96. OSTimeDly(OS_TICKS_PER_SEC);
  97. }
  98. }
  99. /* USER CODE END 0 */
  100. /**
  101. * @brief The application entry point.
  102. * @retval int
  103. */
  104. int main(void)
  105. {
  106. /* USER CODE BEGIN 1 */
  107. /* USER CODE END 1 */
  108. /* MCU Configuration--------------------------------------------------------*/
  109. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  110. HAL_Init();
  111. /* USER CODE BEGIN Init */
  112. /* USER CODE END Init */
  113. /* Configure the system clock */
  114. SystemClock_Config();
  115. /* USER CODE BEGIN SysInit */
  116. /* USER CODE END SysInit */
  117. /* Initialize all configured peripherals */
  118. MX_GPIO_Init();
  119. MX_DMA_Init();
  120. MX_USB_DEVICE_Init();
  121. MX_USART1_UART_Init();
  122. /* USER CODE BEGIN 2 */
  123. /* uC/OS-II接口通常用返回错误码的方式报告执行结果。 */
  124. INT8U os_err;
  125. /*
  126. * 初始化内核的任务表、就绪表、事件控制块等内部数据。
  127. * OSInit()只完成初始化,此时任务调度尚未开始。
  128. */
  129. OSInit();
  130. /*
  131. * 创建应用启动任务,各参数依次为:
  132. * 1. 任务入口函数;
  133. * 2. 传给任务的参数(当前不需要,所以为0);
  134. * 3. 初始栈顶地址;
  135. * 4. 任务优先级,数值越小优先级越高;
  136. * 5. 任务ID,本工程暂时与优先级取相同值;
  137. * 6. 栈底地址;
  138. * 7. 栈容量,单位是OS_STK元素而不是字节;
  139. * 8. 用户扩展指针(当前不用);
  140. * 9. 创建选项:允许栈检查并在创建时清零任务栈。
  141. */
  142. os_err = OSTaskCreateExt(AppTaskStart,
  143. 0,
  144. &AppTaskStartStk[APP_TASK_START_STK_SIZE - 1u],
  145. APP_TASK_START_PRIO,
  146. APP_TASK_START_PRIO,
  147. &AppTaskStartStk[0],
  148. APP_TASK_START_STK_SIZE,
  149. 0,
  150. (OS_TASK_OPT_STK_CHK |
  151. OS_TASK_OPT_STK_CLR));
  152. if (os_err != OS_ERR_NONE)
  153. {
  154. Error_Handler();
  155. }
  156. /*
  157. * 启动抢占式调度,内核会运行当前最高优先级的就绪任务。
  158. * OSStart()成功后不会返回,后面的裸机while循环不会再被执行。
  159. */
  160. OSStart();
  161. /* USER CODE END 2 */
  162. /* Infinite loop */
  163. /* USER CODE BEGIN WHILE */
  164. while (1)
  165. {
  166. /* USER CODE END WHILE */
  167. /* USER CODE BEGIN 3 */
  168. }
  169. /* USER CODE END 3 */
  170. }
  171. /**
  172. * @brief System Clock Configuration
  173. * @retval None
  174. */
  175. void SystemClock_Config(void)
  176. {
  177. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  178. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  179. /** Configure the main internal regulator output voltage
  180. */
  181. __HAL_RCC_PWR_CLK_ENABLE();
  182. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  183. /** Initializes the RCC Oscillators according to the specified parameters
  184. * in the RCC_OscInitTypeDef structure.
  185. */
  186. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  187. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  188. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  189. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  190. RCC_OscInitStruct.PLL.PLLM = 12;
  191. RCC_OscInitStruct.PLL.PLLN = 336;
  192. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  193. RCC_OscInitStruct.PLL.PLLQ = 7;
  194. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  195. {
  196. Error_Handler();
  197. }
  198. /** Initializes the CPU, AHB and APB buses clocks
  199. */
  200. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  201. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  202. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  203. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  204. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  205. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  206. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  207. {
  208. Error_Handler();
  209. }
  210. /** Enables the Clock Security System
  211. */
  212. HAL_RCC_EnableCSS();
  213. }
  214. /**
  215. * @brief USART1 Initialization Function
  216. * @param None
  217. * @retval None
  218. */
  219. static void MX_USART1_UART_Init(void)
  220. {
  221. /* USER CODE BEGIN USART1_Init 0 */
  222. /* USER CODE END USART1_Init 0 */
  223. /* USER CODE BEGIN USART1_Init 1 */
  224. /* USER CODE END USART1_Init 1 */
  225. huart1.Instance = USART1;
  226. huart1.Init.BaudRate = 115200;
  227. huart1.Init.WordLength = UART_WORDLENGTH_9B;
  228. huart1.Init.StopBits = UART_STOPBITS_1;
  229. huart1.Init.Parity = UART_PARITY_EVEN;
  230. huart1.Init.Mode = UART_MODE_TX_RX;
  231. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  232. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  233. if (HAL_UART_Init(&huart1) != HAL_OK)
  234. {
  235. Error_Handler();
  236. }
  237. /* USER CODE BEGIN USART1_Init 2 */
  238. /* USER CODE END USART1_Init 2 */
  239. }
  240. /**
  241. * Enable DMA controller clock
  242. */
  243. static void MX_DMA_Init(void)
  244. {
  245. /* DMA controller clock enable */
  246. __HAL_RCC_DMA2_CLK_ENABLE();
  247. /* DMA interrupt init */
  248. /* DMA2_Stream2_IRQn interrupt configuration */
  249. HAL_NVIC_SetPriority(DMA2_Stream2_IRQn, 5, 0);
  250. HAL_NVIC_EnableIRQ(DMA2_Stream2_IRQn);
  251. /* DMA2_Stream7_IRQn interrupt configuration */
  252. HAL_NVIC_SetPriority(DMA2_Stream7_IRQn, 5, 0);
  253. HAL_NVIC_EnableIRQ(DMA2_Stream7_IRQn);
  254. }
  255. /**
  256. * @brief GPIO Initialization Function
  257. * @param None
  258. * @retval None
  259. */
  260. static void MX_GPIO_Init(void)
  261. {
  262. /* USER CODE BEGIN MX_GPIO_Init_1 */
  263. /* USER CODE END MX_GPIO_Init_1 */
  264. /* GPIO Ports Clock Enable */
  265. __HAL_RCC_GPIOH_CLK_ENABLE();
  266. __HAL_RCC_GPIOA_CLK_ENABLE();
  267. /* USER CODE BEGIN MX_GPIO_Init_2 */
  268. /* USER CODE END MX_GPIO_Init_2 */
  269. }
  270. /* USER CODE BEGIN 4 */
  271. void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
  272. {
  273. if (huart->Instance == USART1)
  274. {
  275. uart1_tx_complete_count++;
  276. }
  277. }
  278. /* USER CODE END 4 */
  279. /**
  280. * @brief This function is executed in case of error occurrence.
  281. * @retval None
  282. */
  283. void Error_Handler(void)
  284. {
  285. /* USER CODE BEGIN Error_Handler_Debug */
  286. /* User can add his own implementation to report the HAL error return state */
  287. __disable_irq();
  288. while (1)
  289. {
  290. }
  291. /* USER CODE END Error_Handler_Debug */
  292. }
  293. #ifdef USE_FULL_ASSERT
  294. /**
  295. * @brief Reports the name of the source file and the source line number
  296. * where the assert_param error has occurred.
  297. * @param file: pointer to the source file name
  298. * @param line: assert_param error line source number
  299. * @retval None
  300. */
  301. void assert_failed(uint8_t *file, uint32_t line)
  302. {
  303. /* USER CODE BEGIN 6 */
  304. /* User can add his own implementation to report the file name and line number,
  305. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  306. /* USER CODE END 6 */
  307. }
  308. #endif /* USE_FULL_ASSERT */