usart.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. #include "cjt188.h"
  9. uint8_t rxByte;
  10. uint8_t rx_232_Buffer[RX_BUFFER_SIZE];
  11. uint16_t QueueHead; // 队列头指针
  12. uint16_t QueueTail; // 队列尾指针
  13. uint8_t recv_232_done;// 232接收完成标志符
  14. uint32_t rxIndex = 0;
  15. uint8_t UART6_RX_BUF[BUFF_SIZE];
  16. uint8_t UART6_RX_STAT=0;
  17. uint32_t UART6_RX_NUM = 0;
  18. UART_HandleTypeDef USART_InitStruct_485;
  19. UART_HandleTypeDef USART_InitStruct_232;
  20. UART_HandleTypeDef USART_InitStruct_DEBUG;
  21. UART_HandleTypeDef USART_InitStruct_Test;
  22. DMA_HandleTypeDef DMA_DEBUG_RX;
  23. //重定向c库函数printf到串口,重定向后可使用printf函数
  24. int fputc(int ch, FILE *f)
  25. {
  26. /* 发送一个字节数据到串口 */
  27. USART_485->DR = (ch & (uint16_t)0x01FF);
  28. /* 等待发送完毕 */
  29. while (__HAL_UART_GET_FLAG(&USART_InitStruct_485, 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. else if (get->device_params->protocol == 4)
  206. cjt_callback(&USART_InitStruct_485);
  207. }
  208. void DEBUG_USART_Config(void)
  209. {
  210. GPIO_InitTypeDef GPIO_InitStruct = {0};
  211. // Enable USART6 clock
  212. __HAL_RCC_USART6_CLK_ENABLE();
  213. // Enable GPIOC clock
  214. __HAL_RCC_GPIOC_CLK_ENABLE();
  215. // Configure USART6 pins
  216. GPIO_InitStruct.Pin = DEBUG_USART_RX_PIN | DEBUG_USART_TX_PIN;
  217. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  218. GPIO_InitStruct.Pull = GPIO_PULLUP;
  219. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  220. GPIO_InitStruct.Alternate = GPIO_AF8_USART6;// Connect USART6 pins to AF8
  221. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  222. // Configure USART6
  223. USART_InitStruct_DEBUG.Instance = DEBUG_USART;
  224. USART_InitStruct_DEBUG.Init.BaudRate = DEBUG_USART_BAUDRATE;
  225. USART_InitStruct_DEBUG.Init.WordLength = UART_WORDLENGTH_8B;
  226. USART_InitStruct_DEBUG.Init.StopBits = UART_STOPBITS_1;
  227. USART_InitStruct_DEBUG.Init.Parity = UART_PARITY_NONE;
  228. USART_InitStruct_DEBUG.Init.Mode = UART_MODE_TX_RX; // 既支持发送也支持接收
  229. USART_InitStruct_DEBUG.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  230. USART_InitStruct_DEBUG.Init.OverSampling = UART_OVERSAMPLING_16;
  231. HAL_UART_Init(&USART_InitStruct_DEBUG);
  232. HAL_NVIC_SetPriority(DEBUG_USART_IRQ, 2, 1);
  233. HAL_NVIC_EnableIRQ(DEBUG_USART_IRQ);
  234. // Enable USART6
  235. __HAL_UART_ENABLE_IT(&USART_InitStruct_DEBUG, UART_IT_IDLE); // 使能空闲中断
  236. // __HAL_UART_ENABLE_IT(&USART_InitStruct_DEBUG, UART_IT_RXNE); // 使能接收中断
  237. __HAL_DMA_ENABLE(&DMA_DEBUG_RX); //使能DMA
  238. __HAL_UART_ENABLE(&USART_InitStruct_DEBUG);
  239. // __HAL_UART_CLEAR_FLAG(&USART_InitStruct_DEBUG, USART_FLAG_TC);
  240. }
  241. void DEBUG_USART_IRQHandler(void)
  242. {
  243. if(RESET != __HAL_UART_GET_FLAG(&USART_InitStruct_DEBUG, UART_FLAG_IDLE)) // 判断是否是空闲中断
  244. {
  245. __HAL_UART_CLEAR_IDLEFLAG(&USART_InitStruct_DEBUG); //清空空闲中断标志位
  246. //关闭DMA
  247. __HAL_DMA_DISABLE(&DMA_DEBUG_RX);
  248. // HAL_UART_DMAStop(&USART_InitStruct_DEBUG);
  249. UART6_RX_NUM = BUFF_SIZE - __HAL_DMA_GET_COUNTER(&DMA_DEBUG_RX);
  250. //清除DMA中断
  251. __HAL_DMA_CLEAR_FLAG(&DMA_DEBUG_RX,DMA_FLAG_TCIF1_5);
  252. __HAL_DMA_ENABLE(&DMA_DEBUG_RX);
  253. //接收状态置位
  254. UART6_RX_STAT=1;
  255. }
  256. }
  257. void USART_DMA_Config(void)
  258. {
  259. // 使能串口和DMA时钟
  260. __HAL_RCC_USART6_CLK_ENABLE();
  261. __HAL_RCC_DMA2_CLK_ENABLE();
  262. DMA_DEBUG_RX.Instance = DEBUG_USART_DMA_STREAM;
  263. DMA_DEBUG_RX.Init.Channel = DEBUG_USART_DMA_CHANNEL;
  264. /*方向:从外设到内存*/
  265. DMA_DEBUG_RX.Init.Direction = DMA_PERIPH_TO_MEMORY;
  266. /*外设地址不增*/
  267. DMA_DEBUG_RX.Init.PeriphInc = DMA_PINC_DISABLE;
  268. /*内存地址自增*/
  269. DMA_DEBUG_RX.Init.MemInc = DMA_MINC_ENABLE;
  270. /*外设数据单位*/
  271. DMA_DEBUG_RX.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  272. /*内存数据单位 8bit*/
  273. DMA_DEBUG_RX.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  274. /*DMA模式:不断循环*/
  275. DMA_DEBUG_RX.Init.Mode = DMA_NORMAL;
  276. /*优先级:中*/
  277. DMA_DEBUG_RX.Init.Priority = DMA_PRIORITY_MEDIUM;
  278. /*禁用FIFO*/
  279. DMA_DEBUG_RX.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  280. DMA_DEBUG_RX.Init.FIFOThreshold= DMA_FIFO_THRESHOLD_FULL;
  281. HAL_DMA_Init(&DMA_DEBUG_RX);
  282. __HAL_LINKDMA(&USART_InitStruct_DEBUG, hdmarx, DMA_DEBUG_RX);
  283. SET_BIT(USART_InitStruct_DEBUG.Instance->CR3, USART_CR3_DMAR);
  284. HAL_DMA_Start(&DMA_DEBUG_RX,DEBUG_USART_DR_BASE, (uint32_t)UART6_RX_BUF, BUFF_SIZE);
  285. __HAL_DMA_ENABLE_IT(&DMA_DEBUG_RX,DMA_IT_TC);
  286. HAL_NVIC_SetPriority(DMA2_Stream1_IRQn, 2, 2);
  287. HAL_NVIC_EnableIRQ(DMA2_Stream1_IRQn);
  288. }
  289. void DMA2_Stream1_IRQHandler() {
  290. if (__HAL_DMA_GET_IT_SOURCE(&DMA_DEBUG_RX,DMA_IT_TC))
  291. {
  292. __HAL_DMA_CLEAR_FLAG(&DMA_DEBUG_RX,DMA_IT_TC);
  293. }
  294. }
  295. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *husart)
  296. {
  297. // USART 232
  298. if(husart->Instance == USART_232)
  299. {
  300. // 将接收到的数据存储在缓冲区中
  301. rx_232_Buffer[rxIndex] = rxByte;
  302. rxIndex++;
  303. HAL_UART_Receive_IT(husart, &rxByte, 1);
  304. }
  305. // USART 485
  306. if(husart->Instance == USART_485)
  307. {
  308. if (rxIndex < RX_BUFFER_SIZE)
  309. {
  310. // 将接收到的数据存储在缓冲区中
  311. rx_232_Buffer[rxIndex] = rxByte;
  312. rxIndex++;
  313. }
  314. memset(rx_232_Buffer, 0, rxIndex);
  315. rxIndex = 0;
  316. HAL_UART_Receive_IT(husart, &rxByte, 1);
  317. }
  318. if(husart->Instance == USART6)
  319. {
  320. }
  321. }
  322. /*********************************************END OF FILE**********************/