123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- /**
- ******************************************************************************
- * File Name : USART.c
- * Description : This file provides code for the configuration
- * of the USART instances.
- ******************************************************************************
- * This notice applies to any and all portions of this file
- * that are not between comment pairs USER CODE BEGIN and
- * USER CODE END. Other portions of this file, whether
- * inserted by the user or by software development tools
- * are owned by their respective copyright owners.
- *
- * Copyright (c) 2019 STMicroelectronics International N.V.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted, provided that the following conditions are met:
- *
- * 1. Redistribution of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * 3. Neither the name of STMicroelectronics nor the names of other
- * contributors to this software may be used to endorse or promote products
- * derived from this software without specific written permission.
- * 4. This software, including modifications and/or derivative works of this
- * software, must execute solely and exclusively on microcontroller or
- * microprocessor devices manufactured by or for STMicroelectronics.
- * 5. Redistribution and use of this software other than as permitted under
- * this license is void and will automatically terminate your rights under
- * this license.
- *
- * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
- * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
- * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
- * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
- * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
- * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "usart.h"
- //#include "Dwin.h"
- /* USER CODE BEGIN 0 */
- #include <stdio.h>
- #include <string.h>
- //#include "TerminalSlave485.h"
- //#include "KeySlave485.h"
- //#include "CollectMaster485.h"
- //#include "ScreenMaster485.h"
- uint8_t usart1_rx_flag;
- uint8_t usart2_rx_flag;
- uint8_t usart3_rx_flag;
- uint8_t usart4_rx_flag;
- uint16_t rx1_len;
- uint16_t rx2_len;
- uint16_t rx3_len;
- uint16_t rx3_len;
- uint16_t rx4_len;
- static uint8_t USART_RX_BUF_U1[Uart1_BUF_SIZE]; /*为UART专门开一个DMA接收存储区 2021-2-23 by Daiyf*/
- //static uint8_t USART_RX_BUF_U2[Uart2_BUF_SIZE]; /*为UART2、3专门开一个DMA接收存储区 2021-4-12 by Daiyf*/
- static uint8_t USART_RX_BUF_U3[Uart3_BUF_SIZE];
- uint8_t USART_RX_BUF[Uart1_BUF_SIZE];
- static const char LIB_CRC_MODBUS_HI[] =
- {
- 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
- 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
- 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
- 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
- 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
- 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
- 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
- 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
- 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
- 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
- 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
- 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
- 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
- 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
- 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
- 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
- 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
- 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
- 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
- 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
- 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
- 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
- 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
- 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
- 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
- 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40
- };
- static const char LIB_CRC_MODBUS_LO[] =
- {
- 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06,
- 0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD,
- 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,
- 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A,
- 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4,
- 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
- 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3,
- 0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4,
- 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,
- 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29,
- 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED,
- 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
- 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60,
- 0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67,
- 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,
- 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68,
- 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E,
- 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
- 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71,
- 0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92,
- 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
- 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B,
- 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B,
- 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
- 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,
- 0x43, 0x83, 0x41, 0x81, 0x80, 0x40
- };
- uint8_t USART_TX_BUF[Uart1_BUF_SIZE];
- uint8_t USART1_RX_BUF[Uart1_BUF_SIZE];
- uint8_t USART1_RX_BUF002[Uart1_BUF_SIZE];
- uint8_t USART1_TX_BUF[Uart1_BUF_SIZE];
- uint8_t USART2_RX_BUF[Uart2_BUF_SIZE];
- uint8_t USART2_TX_BUF[Uart2_BUF_SIZE];
- uint8_t USART2_RX_BUF002[Uart2_BUF_SIZE];
- uint8_t USART3_RX_BUF[Uart3_BUF_SIZE];
- uint8_t USART3_RX_BUF002[Uart3_BUF_SIZE];
- uint8_t USART3_RX_BUF002_print[Uart3_BUF_SIZE];
- uint8_t USART3_TX_BUF[Uart3_BUF_SIZE];
- //uint8_t UART4_RX_BUF[BUF_SIZE];
- //uint8_t UART4_TX_BUF[BUF_SIZE];
- /* USER CODE END 0 */
- //UART_HandleTypeDef huart4;
- extern UART_HandleTypeDef huart1;
- extern UART_HandleTypeDef huart2;
- extern UART_HandleTypeDef huart3;
- //DMA_HandleTypeDef hdma_uart4_rx;
- extern DMA_HandleTypeDef hdma_usart1_rx;
- extern DMA_HandleTypeDef hdma_usart2_rx;
- extern DMA_HandleTypeDef hdma_usart3_rx;
- UART_HandleTypeDef huart4;
-
-
-
-
- /* USER CODE BEGIN 1 */
- uint16_t ErrorLength;
- void UsartReceive_IDLE(UART_HandleTypeDef *huart)
- {
- uint32_t i = 0;
- if((__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE) != RESET))
- {
- if(huart->Instance == USART1)
- {
- __HAL_UART_CLEAR_IDLEFLAG(huart);
- // i = huart->Instance->ISR; //清除状态寄存器SR,读取SR寄存器可以实现清除SR寄存器的功能 /*这三行不要。 2021-2-23 by Daiyf*/
- // i = huart->Instance->RDR; //读取数据寄存器中的数据
- // i = hdma_usart1_rx.Instance->NDTR;
- // __HAL_UART_DISABLE_IT(&huart1, UART_IT_RXNE);
- HAL_UART_DMAStop(huart);
- i = __HAL_DMA_GET_COUNTER(&hdma_usart1_rx);// 获取DMA中未传输的数据个数
- // HAL_UART_Receive_DMA(&huart1, USART_RX_BUF,Uart1_BUF_SIZE);//重新打开DMA接收
- /* 此处处理数据,主要是拷贝和置位标志位 */
- memcpy(USART1_RX_BUF, USART_RX_BUF_U1, (Uart1_BUF_SIZE - i)); /*为UART专门开一个DMA接收存储区 2021-2-23 by Daiyf*/
- usart1_rx_flag = 1;
- rx1_len = Uart1_BUF_SIZE - i-2;//实际接收到的有用数据,总计数减去未传输的数据个数,得到已经接收的数据个数(0x0D,0x0A)
- /* 清空缓存,重新接收 */
- ErrorLength=Uart1_BUF_SIZE;//DMA缺陷处理不定长数据时,连续数据太快导致长度异常,处理方法不用RTOS,或者不用DMA.
- // printf("%d:%s\r\n",rx1_len,USART_RX_BUF);
- // KeySlave485_Send_Data(USART_RX_BUF,rx1_len);
- // SendChar4(rx1_len);
- //memset(USART_RX_BUF_U1, 0x00, Uart1_BUF_SIZE); /*这二行不要。 2021-2-23 by Daiyf*/
- //__HAL_DMA_SET_COUNTER(&hdma_usart1_rx, 0); //清除DMA 计数
- HAL_UART_Receive_DMA(&huart1, USART_RX_BUF_U1,Uart1_BUF_SIZE);//重新打开DMA接收
- if((__HAL_UART_GET_FLAG(&huart1, UART_FLAG_ORE) != RESET))
- {
- // i = huart->Instance->ISR; //清除状态寄存器SR,读取SR寄存器可以实现清除SR寄存器的功能
- // i = huart->Instance->RDR; //读取数据寄存器中的数据
- // i = hdma_usart1_rx.Instance->NDTR;
- __HAL_UART_CLEAR_OREFLAG(&huart1);
- HAL_DMA_DeInit(&hdma_usart1_rx);
- }
- }
- // else if(huart->Instance == USART2)
- // {
- // __HAL_UART_CLEAR_IDLEFLAG(huart);
- //// i = huart->Instance->ISR;
- //// i = huart->Instance->RDR;
- //// i = hdma_usart2_rx.Instance->NDTR;
- // HAL_UART_DMAStop(huart);
- // HAL_UART_Receive_DMA(&huart2, USART_RX_BUF_U2,Uart2_BUF_SIZE);
- // /* 此处处理数据,主要是拷贝和置位标志位 */
- //
- //
- //
- // //memcpy(USART2_RX_BUF, USART_RX_BUF, rx2_len);
- // memcpy(USART2_RX_BUF, USART_RX_BUF_U2, Uart2_BUF_SIZE - i); //这里原来可能有点问题,改了一下与其他串口方式相同处理
- // __nop();
- // memcpy(USART2_RX_BUF, USART_RX_BUF_U2, 32);
- // __nop();
- //
- //
- //
- // usart2_rx_flag = 1;
- // rx2_len = Uart2_BUF_SIZE - i;//实际接收到的有用数据
- // // memset(USART_RX_BUF_U2, 0x00, Uart2_BUF_SIZE);
- // }
- else if(huart->Instance == USART3)
- {
- __HAL_UART_CLEAR_IDLEFLAG(huart);
- #if 1
- HAL_UART_DMAStop(huart);
- i = __HAL_DMA_GET_COUNTER(&hdma_usart3_rx);// 获取DMA中未传输的数据个数
- memcpy(USART3_RX_BUF, USART_RX_BUF_U3, (Uart3_BUF_SIZE - i));
- usart3_rx_flag = 1;
- rx3_len = Uart3_BUF_SIZE - i;
- ErrorLength=Uart3_BUF_SIZE;
-
- HAL_UART_Receive_DMA(&huart3, USART_RX_BUF_U3,Uart3_BUF_SIZE);//重新打开DMA接收
- if((__HAL_UART_GET_FLAG(&huart3, UART_FLAG_ORE) != RESET))
- {
- // i = huart->Instance->ISR; //清除状态寄存器SR,读取SR寄存器可以实现清除SR寄存器的功能
- // i = huart->Instance->RDR; //读取数据寄存器中的数据
- // i = hdma_usart3_rx.Instance->NDTR;
- __HAL_UART_CLEAR_OREFLAG(&huart3);
- HAL_DMA_DeInit(&hdma_usart3_rx);
- }
-
- #else
- i = huart->Instance->ISR;
- i = huart->Instance->RDR;
- i = hdma_usart3_rx.Instance->NDTR;
- HAL_UART_DMAStop(huart);
- HAL_UART_Receive_DMA(&huart3, USART_RX_BUF_U3,Uart3_BUF_SIZE); /*为UART3专门开一个DMA接收存储区 2021-4-12 by Daiyf*/
-
- /* 此处处理数据,主要是拷贝和置位标志位 */
- memcpy(USART3_RX_BUF, USART_RX_BUF_U3, (Uart3_BUF_SIZE - i));
- usart3_rx_flag = 1;
- rx3_len = Uart3_BUF_SIZE - i;//实际接收到的有用数据
- #endif
-
-
-
-
-
- memset(USART_RX_BUF, 0x00, BUF_SIZE);
- }
-
- /*
- else if(huart->Instance == UART4)
- {
- __HAL_UART_CLEAR_IDLEFLAG(huart);
- i = huart->Instance->ISR;
- i = huart->Instance->RDR;
- i = hdma_uart4_rx.Instance->NDTR;
- HAL_UART_DMAStop(huart);
- HAL_UART_Receive_DMA(&huart4, USART_RX_BUF,BUF_SIZE);
- //此处处理数据,主要是拷贝和置位标志位 //
- memcpy(UART4_RX_BUF, USART_RX_BUF, (BUF_SIZE - i));
- usart4_rx_flag = 1;
- rx4_len = BUF_SIZE - i;//实际接收到的有用数据
- memset(USART_RX_BUF, 0x00, BUF_SIZE);
-
- if((__HAL_UART_GET_FLAG(&huart4, UART_FLAG_ORE) != RESET))
- {
- i = huart->Instance->ISR; //清除状态寄存器SR,读取SR寄存器可以实现清除SR寄存器的功能
- i = huart->Instance->RDR; //读取数据寄存器中的数据
- i = hdma_uart4_rx.Instance->NDTR;
- __HAL_UART_CLEAR_OREFLAG(&huart4);
- HAL_DMA_DeInit(&hdma_uart4_rx);
- HAL_UART_MspInit(&huart4);
- HAL_UART_Receive_DMA(&huart4, USART_RX_BUF,BUF_SIZE);
- }
-
- }
-
- */
- }
- // HAL_DMA_DeInit(&hdma_usart1_rx);
- }
- void Uart_Mode_Init(void)
- {
- __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);
- __HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE);
- __HAL_UART_ENABLE_IT(&huart2, UART_IT_IDLE);
- __HAL_UART_ENABLE_IT(&huart2, UART_IT_RXNE);
- __HAL_UART_ENABLE_IT(&huart3, UART_IT_IDLE);
- __HAL_UART_ENABLE_IT(&huart3, UART_IT_RXNE);
- __HAL_UART_ENABLE_IT(&huart4, UART_IT_IDLE);
- __HAL_UART_ENABLE_IT(&huart4, UART_IT_RXNE);
-
- }
- /*****************************************************************
- * 串口2发送函数KeySlave485_Send_Data(uint8_t *buf,uint8_t len)
- ******************************************************************/
- void SendChar2 (uint8_t ch)
- {
- HAL_UART_Transmit(&huart2, &ch, 1, 0xffff);
- }
- void SendChar4 (uint8_t ch)
- {
- //KeySlave485_TX_Set(1);
- HAL_UART_Transmit(&huart4, &ch, 1, 1000);
- //KeySlave485_TX_Set(0);
- }
- /*****************************************************************
- * 串口发送数组函数
- ******************************************************************/
- void UartWrite(UART_HandleTypeDef *uartHandle, uint8_t *buf , uint8_t len)
- {
- uint8_t i;
- for(i = 0; i < len; i++)
- {
- HAL_UART_Transmit(uartHandle, &buf[i], 1, 0xFFFF);
- }
- }
- // 重定向函数1
- int fputc(int ch,FILE *f)
- {
- uint8_t temp[1]={ch};
- HAL_UART_Transmit(&huart4,temp,1,2); //
- return 0;
- }
- //16位modbusCRC校验
- uint16_t LIB_CRC_MODBUS(uint8_t* Buf, uint16_t srLen)
- {
- uint32_t srIndex;
- char ucHI = 0xFF;
- char ucLO = 0xFF;
- while(srLen--)
- {
- srIndex = ucHI ^ *Buf++;
- ucHI = ucLO ^ LIB_CRC_MODBUS_HI[srIndex];
- ucLO = LIB_CRC_MODBUS_LO[srIndex];
- }
- return ((uint16_t)ucHI<<8) + ucLO;
- }
- /* USER CODE END 1 */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|