usart.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. #include "usart.h"
  2. #include "mmodbus.h"
  3. #include "dlt645.h"
  4. #include "gateway_message.h"
  5. #include "tcp_server.h"
  6. #include "timer.h"
  7. #include "updata.h"
  8. uint8_t rxByte;
  9. uint8_t rx_232_Buffer[RX_BUFFER_SIZE];
  10. uint16_t QueueHead; // 队列头指针
  11. uint16_t QueueTail; // 队列尾指针
  12. uint8_t recv_232_done;// 232接收完成标志符
  13. uint32_t rxIndex = 0;
  14. uint8_t UART6_RX_BUF[BUFF_SIZE];
  15. uint8_t UART6_RX_STAT=0;
  16. uint32_t UART6_RX_NUM = 0;
  17. UART_HandleTypeDef USART_InitStruct_485;
  18. UART_HandleTypeDef USART_InitStruct_232;
  19. UART_HandleTypeDef USART_InitStruct_DEBUG;
  20. UART_HandleTypeDef USART_InitStruct_Test;
  21. DMA_HandleTypeDef DMA_DEBUG_RX;
  22. //重定向c库函数printf到串口,重定向后可使用printf函数
  23. int fputc(int ch, FILE *f)
  24. {
  25. // /* 发送一个字节数据到串口 */
  26. // USART_232->DR = (ch & (uint16_t)0x01FF);
  27. //
  28. // /* 等待发送完毕 */
  29. // while (__HAL_UART_GET_FLAG(&USART_InitStruct_232, USART_FLAG_TXE) == RESET);
  30. // return (ch);
  31. }
  32. /* 232串口配置 */
  33. void USART_232_config(void)
  34. {
  35. __HAL_RCC_GPIOA_CLK_ENABLE();
  36. __HAL_RCC_USART1_CLK_ENABLE();
  37. GPIO_InitTypeDef GPIO_InitStruct = {0};
  38. GPIO_InitStruct.Pin = USART_232_TX_PIN | USART_232_RX_PIN;
  39. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  40. GPIO_InitStruct.Pull = GPIO_PULLUP;
  41. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  42. GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
  43. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  44. USART_InitStruct_232.Instance = USART_232;
  45. USART_InitStruct_232.Init.BaudRate = USART_232_BAUDRATE;
  46. USART_InitStruct_232.Init.WordLength = UART_WORDLENGTH_8B;
  47. USART_InitStruct_232.Init.StopBits = UART_STOPBITS_1;
  48. USART_InitStruct_232.Init.Parity = UART_PARITY_NONE;
  49. USART_InitStruct_232.Init.Mode = UART_MODE_TX_RX;
  50. USART_InitStruct_232.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  51. USART_InitStruct_232.Init.OverSampling = UART_OVERSAMPLING_8;
  52. HAL_UART_Init(&USART_InitStruct_232);
  53. HAL_NVIC_SetPriority(USART_232_IRQ, 1, 0);
  54. HAL_NVIC_EnableIRQ(USART_232_IRQ);
  55. // HAL_UART_Receive_IT(&USART_InitStruct_232, &rxByte, 1);
  56. __HAL_UART_ENABLE_IT(&USART_InitStruct_232, UART_IT_RXNE); // 使能接收中断
  57. __HAL_UART_ENABLE_IT(&USART_InitStruct_232, UART_IT_IDLE); // 使能空闲中断
  58. }
  59. void USART_232_IRQHandler(void){
  60. GATEWAY_PARAMS *get;
  61. get= get_gateway_config_params();
  62. if(get->comProtocol)// get->protocol 1:232 0:485
  63. {
  64. if(get->device_params->protocol == 3)
  65. mmodbus_callback(&USART_InitStruct_232);
  66. else if(get->device_params->protocol == 1 || get->device_params->protocol == 2)
  67. dlt_callback(&USART_InitStruct_232);
  68. }
  69. else
  70. {
  71. //HAL_UART_IRQHandler(&USART_InitStruct_232);
  72. if(RESET != __HAL_UART_GET_IT_SOURCE(&USART_InitStruct_232, UART_IT_RXNE))
  73. {
  74. if (rxIndex < RX_BUFFER_SIZE - 1)
  75. {
  76. rx_232_Buffer[rxIndex] = USART_InitStruct_232.Instance->DR;
  77. rxIndex++;
  78. }
  79. else
  80. {
  81. uint8_t data = USART1->DR;
  82. }
  83. }
  84. if (RESET != __HAL_UART_GET_FLAG(&USART_InitStruct_232, USART_FLAG_IDLE) && __HAL_UART_GET_IT_SOURCE(&USART_InitStruct_232, UART_IT_IDLE))
  85. {
  86. uint8_t i;
  87. i = USART_InitStruct_232.Instance->SR;
  88. i = USART_InitStruct_232.Instance->DR;
  89. rxIndex = 0;
  90. recv_232_done = 1;
  91. return;
  92. }
  93. }
  94. }
  95. void USART_232_Send(uint8_t *message,uint16_t size)
  96. {
  97. recv_232_done = 0;
  98. HAL_UART_Transmit(&USART_InitStruct_232, message,size,HAL_MAX_DELAY);
  99. }
  100. /* 485串口配置 */
  101. void USART_485_DE_init()
  102. {
  103. __HAL_RCC_GPIOC_CLK_ENABLE();
  104. GPIO_InitTypeDef GPIO_InitStruct = {0};
  105. GPIO_InitStruct.Pin = USART_485_DE_PIN;
  106. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  107. GPIO_InitStruct.Pull = GPIO_PULLUP;
  108. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  109. HAL_GPIO_Init(USART_485_DE_GPIO_PORT, &GPIO_InitStruct);
  110. }
  111. void USART_485_DE_TX()
  112. {
  113. HAL_GPIO_WritePin(USART_485_DE_GPIO_PORT,USART_485_DE_PIN,GPIO_PIN_SET);
  114. }
  115. void USART_485_DE_RX()
  116. {
  117. HAL_GPIO_WritePin(USART_485_DE_GPIO_PORT,USART_485_DE_PIN,GPIO_PIN_RESET);
  118. }
  119. // 485发送数据
  120. void USART_485_Send(uint8_t *message,uint16_t size)
  121. {
  122. USART_485_DE_TX();
  123. HAL_UART_Transmit(&USART_InitStruct_485, message, size, HAL_MAX_DELAY);
  124. USART_485_DE_RX();
  125. }
  126. // USART_485 初始化
  127. void USART_485_config()
  128. {
  129. USART_485_DE_init();
  130. __HAL_RCC_USART3_CLK_ENABLE();
  131. __HAL_RCC_GPIOC_CLK_ENABLE();
  132. GPIO_InitTypeDef GPIO_InitStruct = {0};
  133. // 配置 GPIOC Pin 11 和 10 分别作为 USART3 的 RX 和 TX 引脚
  134. GPIO_InitStruct.Pin = USART_485_RX_PIN | USART_485_TX_PIN;
  135. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  136. GPIO_InitStruct.Pull = GPIO_PULLUP;
  137. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  138. GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
  139. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  140. USART_InitStruct_485.Instance = USART_485;
  141. USART_InitStruct_485.Init.BaudRate = USART_485_BAUDRATE;
  142. USART_InitStruct_485.Init.WordLength = UART_WORDLENGTH_8B;
  143. USART_InitStruct_485.Init.StopBits = UART_STOPBITS_1;
  144. USART_InitStruct_485.Init.Parity = UART_PARITY_NONE;
  145. USART_InitStruct_485.Init.Mode = UART_MODE_TX_RX; // 既支持发送也支持接收
  146. USART_InitStruct_485.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  147. USART_InitStruct_485.Init.OverSampling = UART_OVERSAMPLING_16;
  148. HAL_UART_Init(&USART_InitStruct_485);
  149. HAL_NVIC_SetPriority(USART_485_IRQ, 0, 0);
  150. HAL_NVIC_EnableIRQ(USART_485_IRQ);
  151. // 初始化 DE 引脚 接收数据
  152. HAL_GPIO_WritePin(USART_485_DE_GPIO_PORT,USART_485_DE_PIN,GPIO_PIN_RESET);
  153. // HAL_UART_Receive_IT(&USART_InitStruct_485, &rxByte, 1);
  154. __HAL_UART_ENABLE_IT(&USART_InitStruct_485, UART_IT_RXNE); // 使能接收中断
  155. __HAL_UART_ENABLE_IT(&USART_InitStruct_485, UART_IT_IDLE); // 使能空闲中断
  156. }
  157. /*
  158. * 函数名:void Config_485_Port(uint32_t baudrate, uint8_t databits, uint8_t stopbits, uint8_t parity, uint8_t flowcontrol)
  159. * 输入参数:baudrate,databits,stopbits,parity,flowcontol 串口的配置参数
  160. * 输出参数:无
  161. * 返回值:无
  162. * 函数作用:根据输入参数进行配置485串口参数
  163. */
  164. void Config_485_Port(uint32_t baudrate, uint8_t databits, uint8_t stopbits, uint8_t parity, uint8_t flowcontrol)
  165. {
  166. uint8_t wordLength;
  167. if(parity!=0)
  168. {
  169. wordLength=databits+1;
  170. }else wordLength=databits;
  171. __HAL_RCC_USART3_CLK_ENABLE();
  172. __HAL_RCC_GPIOC_CLK_ENABLE();
  173. GPIO_InitTypeDef GPIO_InitStruct = {0};
  174. // 配置 GPIOC Pin 11 和 10 分别作为 USART3 的 RX 和 TX 引脚
  175. GPIO_InitStruct.Pin = USART_485_RX_PIN | USART_485_TX_PIN;
  176. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  177. GPIO_InitStruct.Pull = GPIO_PULLUP;
  178. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  179. GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
  180. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  181. USART_InitStruct_485.Instance = USART_485;
  182. USART_InitStruct_485.Init.BaudRate = baudrate;
  183. USART_InitStruct_485.Init.WordLength = (wordLength== 9) ? UART_WORDLENGTH_9B : UART_WORDLENGTH_8B;
  184. USART_InitStruct_485.Init.StopBits = UART_STOPBITS_1;
  185. USART_InitStruct_485.Init.Parity = (parity == 1) ? (UART_PARITY_ODD) : (parity == 2 ? UART_PARITY_EVEN : UART_PARITY_NONE);
  186. USART_InitStruct_485.Init.Mode = UART_MODE_TX_RX; // 既支持发送也支持接收
  187. USART_InitStruct_485.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  188. USART_InitStruct_485.Init.OverSampling = UART_OVERSAMPLING_16;
  189. HAL_UART_Init(&USART_InitStruct_485);
  190. HAL_NVIC_SetPriority(USART_485_IRQ, 0, 0);
  191. HAL_NVIC_EnableIRQ(USART_485_IRQ);
  192. // 初始化 DE 引脚 接收数据
  193. HAL_GPIO_WritePin(USART_485_DE_GPIO_PORT,USART_485_DE_PIN,GPIO_PIN_RESET);
  194. // HAL_UART_Receive_IT(&USART_InitStruct_485, &rxByte, 1);
  195. __HAL_UART_ENABLE_IT(&USART_InitStruct_485, UART_IT_RXNE); // 使能接收中断
  196. __HAL_UART_ENABLE_IT(&USART_InitStruct_485, UART_IT_IDLE); // 使能空闲中断
  197. }
  198. void USART_485_IRQHandler(void){
  199. GATEWAY_PARAMS *get;
  200. get= get_gateway_config_params();
  201. if(get->device_params->protocol == 3)
  202. mmodbus_callback(&USART_InitStruct_485);
  203. else if(get->device_params->protocol == 1 || get->device_params->protocol == 2)
  204. dlt_callback(&USART_InitStruct_485);
  205. }
  206. void DEBUG_USART_Config(void)
  207. {
  208. GPIO_InitTypeDef GPIO_InitStruct = {0};
  209. // Enable USART6 clock
  210. __HAL_RCC_USART6_CLK_ENABLE();
  211. // Enable GPIOC clock
  212. __HAL_RCC_GPIOC_CLK_ENABLE();
  213. // Configure USART6 pins
  214. GPIO_InitStruct.Pin = DEBUG_USART_RX_PIN | DEBUG_USART_TX_PIN;
  215. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  216. GPIO_InitStruct.Pull = GPIO_PULLUP;
  217. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  218. GPIO_InitStruct.Alternate = GPIO_AF8_USART6;// Connect USART6 pins to AF8
  219. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  220. // Configure USART6
  221. USART_InitStruct_DEBUG.Instance = DEBUG_USART;
  222. USART_InitStruct_DEBUG.Init.BaudRate = DEBUG_USART_BAUDRATE;
  223. USART_InitStruct_DEBUG.Init.WordLength = UART_WORDLENGTH_8B;
  224. USART_InitStruct_DEBUG.Init.StopBits = UART_STOPBITS_1;
  225. USART_InitStruct_DEBUG.Init.Parity = UART_PARITY_NONE;
  226. USART_InitStruct_DEBUG.Init.Mode = UART_MODE_TX_RX; // 既支持发送也支持接收
  227. USART_InitStruct_DEBUG.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  228. USART_InitStruct_DEBUG.Init.OverSampling = UART_OVERSAMPLING_16;
  229. HAL_UART_Init(&USART_InitStruct_DEBUG);
  230. HAL_NVIC_SetPriority(DEBUG_USART_IRQ, 2, 1);
  231. HAL_NVIC_EnableIRQ(DEBUG_USART_IRQ);
  232. // Enable USART6
  233. __HAL_UART_ENABLE_IT(&USART_InitStruct_DEBUG, UART_IT_IDLE); // 使能空闲中断
  234. // __HAL_UART_ENABLE_IT(&USART_InitStruct_DEBUG, UART_IT_RXNE); // 使能接收中断
  235. __HAL_DMA_ENABLE(&DMA_DEBUG_RX); //使能DMA
  236. __HAL_UART_ENABLE(&USART_InitStruct_DEBUG);
  237. // __HAL_UART_CLEAR_FLAG(&USART_InitStruct_DEBUG, USART_FLAG_TC);
  238. }
  239. void DEBUG_USART_IRQHandler(void)
  240. {
  241. if(RESET != __HAL_UART_GET_FLAG(&USART_InitStruct_DEBUG, UART_FLAG_IDLE)) // 判断是否是空闲中断
  242. {
  243. __HAL_UART_CLEAR_IDLEFLAG(&USART_InitStruct_DEBUG); //清空空闲中断标志位
  244. //关闭DMA
  245. __HAL_DMA_DISABLE(&DMA_DEBUG_RX);
  246. // HAL_UART_DMAStop(&USART_InitStruct_DEBUG);
  247. UART6_RX_NUM = BUFF_SIZE - __HAL_DMA_GET_COUNTER(&DMA_DEBUG_RX);
  248. //清除DMA中断
  249. __HAL_DMA_CLEAR_FLAG(&DMA_DEBUG_RX,DMA_FLAG_TCIF1_5);
  250. __HAL_DMA_ENABLE(&DMA_DEBUG_RX);
  251. //接收状态置位
  252. UART6_RX_STAT=1;
  253. }
  254. }
  255. void USART_DMA_Config(void)
  256. {
  257. // 使能串口和DMA时钟
  258. __HAL_RCC_USART6_CLK_ENABLE();
  259. __HAL_RCC_DMA2_CLK_ENABLE();
  260. DMA_DEBUG_RX.Instance = DEBUG_USART_DMA_STREAM;
  261. DMA_DEBUG_RX.Init.Channel = DEBUG_USART_DMA_CHANNEL;
  262. /*方向:从外设到内存*/
  263. DMA_DEBUG_RX.Init.Direction = DMA_PERIPH_TO_MEMORY;
  264. /*外设地址不增*/
  265. DMA_DEBUG_RX.Init.PeriphInc = DMA_PINC_DISABLE;
  266. /*内存地址自增*/
  267. DMA_DEBUG_RX.Init.MemInc = DMA_MINC_ENABLE;
  268. /*外设数据单位*/
  269. DMA_DEBUG_RX.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  270. /*内存数据单位 8bit*/
  271. DMA_DEBUG_RX.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  272. /*DMA模式:不断循环*/
  273. DMA_DEBUG_RX.Init.Mode = DMA_NORMAL;
  274. /*优先级:中*/
  275. DMA_DEBUG_RX.Init.Priority = DMA_PRIORITY_MEDIUM;
  276. /*禁用FIFO*/
  277. DMA_DEBUG_RX.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  278. DMA_DEBUG_RX.Init.FIFOThreshold= DMA_FIFO_THRESHOLD_FULL;
  279. HAL_DMA_Init(&DMA_DEBUG_RX);
  280. __HAL_LINKDMA(&USART_InitStruct_DEBUG, hdmarx, DMA_DEBUG_RX);
  281. SET_BIT(USART_InitStruct_DEBUG.Instance->CR3, USART_CR3_DMAR);
  282. HAL_DMA_Start(&DMA_DEBUG_RX,DEBUG_USART_DR_BASE, (uint32_t)UART6_RX_BUF, BUFF_SIZE);
  283. __HAL_DMA_ENABLE_IT(&DMA_DEBUG_RX,DMA_IT_TC);
  284. HAL_NVIC_SetPriority(DMA2_Stream1_IRQn, 2, 2);
  285. HAL_NVIC_EnableIRQ(DMA2_Stream1_IRQn);
  286. }
  287. void DMA2_Stream1_IRQHandler() {
  288. if (__HAL_DMA_GET_IT_SOURCE(&DMA_DEBUG_RX,DMA_IT_TC))
  289. {
  290. __HAL_DMA_CLEAR_FLAG(&DMA_DEBUG_RX,DMA_IT_TC);
  291. }
  292. }
  293. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *husart)
  294. {
  295. // USART 232
  296. if(husart->Instance == USART_232)
  297. {
  298. // 将接收到的数据存储在缓冲区中
  299. rx_232_Buffer[rxIndex] = rxByte;
  300. rxIndex++;
  301. HAL_UART_Receive_IT(husart, &rxByte, 1);
  302. }
  303. // USART 485
  304. if(husart->Instance == USART_485)
  305. {
  306. if (rxIndex < RX_BUFFER_SIZE)
  307. {
  308. // 将接收到的数据存储在缓冲区中
  309. rx_232_Buffer[rxIndex] = rxByte;
  310. rxIndex++;
  311. }
  312. memset(rx_232_Buffer, 0, rxIndex);
  313. rxIndex = 0;
  314. HAL_UART_Receive_IT(husart, &rxByte, 1);
  315. }
  316. if(husart->Instance == USART6)
  317. {
  318. }
  319. }
  320. /*********************************************END OF FILE**********************/