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.
 
 
 
 
 
 

404 satır
11 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 "plc_device.h"
  27. #include "plsr_core.h"
  28. #include "stdio.h"
  29. /* USER CODE END Includes */
  30. /* Private typedef -----------------------------------------------------------*/
  31. /* USER CODE BEGIN PTD */
  32. /* USER CODE END PTD */
  33. /* Private define ------------------------------------------------------------*/
  34. /* USER CODE BEGIN PD */
  35. /* USER CODE END PD */
  36. /* Private macro -------------------------------------------------------------*/
  37. /* USER CODE BEGIN PM */
  38. /* USER CODE END PM */
  39. /* Private variables ---------------------------------------------------------*/
  40. UART_HandleTypeDef huart1;
  41. DMA_HandleTypeDef hdma_usart1_rx;
  42. DMA_HandleTypeDef hdma_usart1_tx;
  43. static volatile uint8_t ConnectFlag;
  44. /* USER CODE BEGIN PV */
  45. static OS_STK AppTaskStartStk[APP_TASK_START_STK_SIZE];
  46. static OS_STK PlsrTaskStk[APP_TASK_PLSR_STK_SIZE];
  47. /* USER CODE END PV */
  48. /* Private function prototypes -----------------------------------------------*/
  49. void SystemClock_Config(void);
  50. static HAL_StatusTypeDef BackupSramInit(void);
  51. static void MX_GPIO_Init(void);
  52. static void MX_DMA_Init(void);
  53. static void MX_USART1_UART_Init(void);
  54. /* USER CODE BEGIN PFP */
  55. static void AppTaskStart(void *pArg);
  56. /* USER CODE END PFP */
  57. /* Private user code ---------------------------------------------------------*/
  58. /* USER CODE BEGIN 0 */
  59. static void AppTaskStart(void *pArg)
  60. {
  61. (void)pArg;
  62. /*
  63. * F4作为Modbus RTU从站,触摸屏作为主站
  64. * 初始化函数会立即启动USART1的DMA空闲接收
  65. */
  66. (void)ModbusSlaveInit(&huart1, MODBUS_SLAVE_DEFAULT_ADDRESS);
  67. //ModbusRetainedRegistersLoad();
  68. while (1)
  69. {
  70. /*
  71. * 维护几个供触摸屏首次联调读取的保持寄存器:
  72. */
  73. // (void)ModbusSlaveSetHoldingRegister( // OS时钟
  74. // HMI_REG_UPTIME_SECONDS, (uint16_t)(OSTimeGet() / OS_TICKS_PER_SEC));
  75. // (void)ModbusSlaveSetHoldingRegister( // 有效帧计数
  76. // HMI_REG_RX_FRAME_COUNT,
  77. // (uint16_t)ModbusSlaveStatistics.validFrameCount);
  78. // (void)ModbusSlaveSetHoldingRegister( // 从站地址不匹配计数
  79. // D3, (uint16_t)ModbusSlaveStatistics.ignoredAddressCount);
  80. // (void)ModbusSlaveSetHoldingRegister( // 非法功能码计数
  81. // D4, (uint16_t)ModbusSlaveStatistics.illegalFunctionCount);
  82. // (void)ModbusSlaveSetHoldingRegister( // 非法地址计数
  83. // D5, (uint16_t)ModbusSlaveStatistics.illegalAddressCount);
  84. // (void)ModbusSlaveSetHoldingRegister( // 非法数据值计数
  85. // D6, (uint16_t)ModbusSlaveStatistics.illegalValueCount);
  86. /*
  87. * 每1ms轮询一次,
  88. */
  89. ModbusSlavePoll();
  90. // ModbusRetainedRegistersPoll();
  91. if (ModbusSlaveIsConnected(MODBUS_CONNECTION_TIMEOUT_MS) != 0U)
  92. {
  93. /* 主站在线 */
  94. ConnectFlag = 1;
  95. }
  96. else
  97. {
  98. /* 主站超时或断开 */
  99. ConnectFlag = 0;
  100. }
  101. OSTimeDly(1U);
  102. }
  103. }
  104. /* USER CODE END 0 */
  105. /**
  106. * @brief The application entry point.
  107. * @retval int
  108. */
  109. int main(void)
  110. {
  111. /* USER CODE BEGIN 1 */
  112. /* USER CODE END 1 */
  113. /* MCU
  114. * Configuration--------------------------------------------------------*/
  115. /* Reset peripherals and initialize the Flash interface and the Systick. */
  116. HAL_Init();
  117. /* USER CODE BEGIN Init */
  118. /* USER CODE END Init */
  119. /* Configure the system clock */
  120. SystemClock_Config();
  121. /* USER CODE BEGIN SysInit */
  122. /* USER CODE END SysInit */
  123. /* Initialize all configured peripherals */
  124. MX_GPIO_Init();
  125. MX_DMA_Init();
  126. MX_USB_DEVICE_Init();
  127. MX_USART1_UART_Init();
  128. if (BackupSramInit() != HAL_OK)
  129. {
  130. Error_Handler();
  131. }
  132. if (PlcDeviceInit() != PLC_DEVICE_OK)
  133. {
  134. Error_Handler();
  135. }
  136. if (PlsrInit() != PLSR_RESULT_OK)
  137. {
  138. Error_Handler();
  139. }
  140. /* USER CODE BEGIN 2 */
  141. INT8U osError;
  142. OSInit();
  143. osError = OSTaskCreateExt(PlsrTask, 0,
  144. &PlsrTaskStk[APP_TASK_PLSR_STK_SIZE - 1u],
  145. APP_TASK_PLSR_PRIO, APP_TASK_PLSR_PRIO,
  146. &PlsrTaskStk[0], APP_TASK_PLSR_STK_SIZE, 0,
  147. (OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR));
  148. if (osError != OS_ERR_NONE)
  149. {
  150. Error_Handler();
  151. }
  152. osError = OSTaskCreateExt(AppTaskStart, 0,
  153. &AppTaskStartStk[APP_TASK_START_STK_SIZE - 1u],
  154. APP_TASK_START_PRIO, APP_TASK_START_PRIO,
  155. &AppTaskStartStk[0], APP_TASK_START_STK_SIZE, 0,
  156. (OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR));
  157. if (osError != OS_ERR_NONE)
  158. {
  159. Error_Handler();
  160. }
  161. OSStart();
  162. /* USER CODE END 2 */
  163. /* Infinite loop */
  164. /* USER CODE BEGIN WHILE */
  165. while (1)
  166. {
  167. /* USER CODE END WHILE */
  168. /* USER CODE BEGIN 3 */
  169. }
  170. /* USER CODE END 3 */
  171. }
  172. static HAL_StatusTypeDef BackupSramInit(void)
  173. {
  174. HAL_PWR_EnableBkUpAccess();
  175. __HAL_RCC_BKPSRAM_CLK_ENABLE();
  176. return HAL_PWREx_EnableBkUpReg();
  177. }
  178. /**
  179. * @brief System Clock Configuration
  180. * @retval None
  181. */
  182. void SystemClock_Config(void)
  183. {
  184. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  185. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  186. /** Configure the main internal regulator output voltage
  187. */
  188. __HAL_RCC_PWR_CLK_ENABLE();
  189. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  190. /** Initializes the RCC Oscillators according to the specified parameters
  191. * in the RCC_OscInitTypeDef structure.
  192. */
  193. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  194. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  195. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  196. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  197. RCC_OscInitStruct.PLL.PLLM = 12;
  198. RCC_OscInitStruct.PLL.PLLN = 336;
  199. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  200. RCC_OscInitStruct.PLL.PLLQ = 7;
  201. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  202. {
  203. Error_Handler();
  204. }
  205. /** Initializes the CPU, AHB and APB buses clocks
  206. */
  207. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
  208. | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  209. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  210. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  211. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  212. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  213. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  214. {
  215. Error_Handler();
  216. }
  217. /** Enables the Clock Security System
  218. */
  219. HAL_RCC_EnableCSS();
  220. }
  221. /**
  222. * @brief USART1 Initialization Function
  223. * @param None
  224. * @retval None
  225. */
  226. static void MX_USART1_UART_Init(void)
  227. {
  228. /* USER CODE BEGIN USART1_Init 0 */
  229. /* USER CODE END USART1_Init 0 */
  230. /* USER CODE BEGIN USART1_Init 1 */
  231. /* USER CODE END USART1_Init 1 */
  232. huart1.Instance = USART1;
  233. huart1.Init.BaudRate = 9600;
  234. huart1.Init.WordLength = UART_WORDLENGTH_9B;
  235. huart1.Init.StopBits = UART_STOPBITS_1;
  236. huart1.Init.Parity = UART_PARITY_EVEN;
  237. huart1.Init.Mode = UART_MODE_TX_RX;
  238. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  239. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  240. if (HAL_UART_Init(&huart1) != HAL_OK)
  241. {
  242. Error_Handler();
  243. }
  244. /* USER CODE BEGIN USART1_Init 2 */
  245. /* USER CODE END USART1_Init 2 */
  246. }
  247. /**
  248. * Enable DMA controller clock
  249. */
  250. static void MX_DMA_Init(void)
  251. {
  252. /* DMA controller clock enable */
  253. __HAL_RCC_DMA2_CLK_ENABLE();
  254. /* DMA interrupt init */
  255. /* DMA2_Stream2_IRQn interrupt configuration */
  256. HAL_NVIC_SetPriority(DMA2_Stream2_IRQn, 5, 0);
  257. HAL_NVIC_EnableIRQ(DMA2_Stream2_IRQn);
  258. /* DMA2_Stream7_IRQn interrupt configuration */
  259. HAL_NVIC_SetPriority(DMA2_Stream7_IRQn, 5, 0);
  260. HAL_NVIC_EnableIRQ(DMA2_Stream7_IRQn);
  261. }
  262. /**
  263. * @brief GPIO Initialization Function
  264. * @param None
  265. * @retval None
  266. */
  267. static void MX_GPIO_Init(void)
  268. {
  269. /* USER CODE BEGIN MX_GPIO_Init_1 */
  270. /* USER CODE END MX_GPIO_Init_1 */
  271. /* GPIO Ports Clock Enable */
  272. __HAL_RCC_GPIOH_CLK_ENABLE();
  273. __HAL_RCC_GPIOA_CLK_ENABLE();
  274. /* USER CODE BEGIN MX_GPIO_Init_2 */
  275. /* USER CODE END MX_GPIO_Init_2 */
  276. }
  277. /* USER CODE BEGIN 4 */
  278. void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
  279. {
  280. if (huart->Instance == USART1)
  281. {
  282. /* 应答发送完成后,协议模块重新启动DMA接收。 */
  283. ModbusSlaveOnTxComplete(huart);
  284. }
  285. }
  286. void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
  287. {
  288. if (huart->Instance == USART1)
  289. {
  290. ModbusSlaveOnRxEvent(huart, Size);
  291. }
  292. }
  293. void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
  294. {
  295. if (huart->Instance == USART1)
  296. {
  297. /* 溢出、噪声、帧错误等异常后恢复USART1 DMA接收。 */
  298. ModbusSlaveOnUartError(huart);
  299. }
  300. }
  301. /* USER CODE END 4 */
  302. /**
  303. * @brief This function is executed in case of error occurrence.
  304. * @retval None
  305. */
  306. void Error_Handler(void)
  307. {
  308. /* USER CODE BEGIN Error_Handler_Debug */
  309. /* User can add his own implementation to report the HAL error return state
  310. */
  311. __disable_irq();
  312. while (1)
  313. {
  314. }
  315. /* USER CODE END Error_Handler_Debug */
  316. }
  317. #ifdef USE_FULL_ASSERT
  318. /**
  319. * @brief Reports the name of the source file and the source line number
  320. * where the assert_param error has occurred.
  321. * @param file: pointer to the source file name
  322. * @param line: assert_param error line source number
  323. * @retval None
  324. */
  325. void assert_failed(uint8_t *file, uint32_t line)
  326. {
  327. /* USER CODE BEGIN 6 */
  328. /* User can add code to report the file name and line number,
  329. for example: printf("Wrong parameter: %s:%d\r\n", file, line) */
  330. /* USER CODE END 6 */
  331. }
  332. #endif /* USE_FULL_ASSERT */