Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

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