123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- /*
- * uart.c
- *
- * Created on: 2019. 5. 27.
- * Author: parkyj
- */
- #include "uart.h"
- #include <stdbool.h>
- UARTQUEUE TerminalQueue;
- UARTQUEUE WifiQueue;
- bool UartDataisReved;
- void InitUartQueue(UART_HandleTypeDef *huart,pUARTQUEUE pQueue)
- {
- UART_HandleTypeDef *dst = (huart->Instance == USART2 ? &hWifi:&hTerminal);
- pQueue->data = pQueue->head = pQueue->tail = 0;
- if (HAL_UART_Receive_DMA(dst, pQueue->Buffer, 1) != HAL_OK)
- {
- // _Error_Handler(__FILE__, __LINE__);
- }
- //HAL_UART_Receive_DMA(&hTerminal, TerminalQueue.Buffer, 1);
- //HAL_UART_Receive_IT(hTerminal, pQueue->Buffer + pQueue->head, 1);
- }
- void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
- {
- pUARTQUEUE pQueue = (huart->Instance == USART2 ? &WifiQueue:&TerminalQueue);
- UART_HandleTypeDef *dst = (huart->Instance == USART2 ? &hWifi:&hTerminal);
- pQueue->head++;
- if (pQueue->head >= QUEUE_BUFFER_LENGTH) pQueue->head = 0;
- pQueue->data++;
- if (pQueue->data >= QUEUE_BUFFER_LENGTH)
- GetDataFromUartQueue(huart);
- HAL_UART_Receive_DMA(dst, pQueue->Buffer + pQueue->head, 1);
- UartDataRecvSet(true);
- // Set_UartRcv(true);
- }
- void UartDataRecvSet(bool val){
- UartDataisReved = val;
- }
- bool UartDataRecvGet(void){
- return UartDataisReved;
- }
- void PutDataToUartQueue(UART_HandleTypeDef *huart, uint8_t data)
- {
- pUARTQUEUE pQueue = (huart->Instance == USART2 ? &WifiQueue:&TerminalQueue);
- if (pQueue->data >= QUEUE_BUFFER_LENGTH)
- GetDataFromUartQueue(huart);
- pQueue->Buffer[pQueue->head++] = data;
- if (pQueue->head == QUEUE_BUFFER_LENGTH) pQueue->head = 0;
- pQueue->data++;
- // HAL_UART_Receive_DMA(&hTerminal, pQueue->Buffer + pQueue->head, 10);
- }
- typedef enum{
- Header = 0,
- Type,
- Length,
- Crcindex,
- }Bluecell_Prot_p;
- uint8_t uart_buf[QUEUE_BUFFER_LENGTH];
- bool GoodDataStatus = false;
- void GetDataFromUartQueue(UART_HandleTypeDef *huart)
- {
- UART_HandleTypeDef *dst = (huart->Instance == USART2 ? &hWifi:&hTerminal);
- pUARTQUEUE pQueue = (huart->Instance == USART2 ? &WifiQueue:&TerminalQueue);
- volatile static int cnt;
- #if 0 // PYJ.2019.12.13_BEGIN --
- if(huart->Instance == USART1){
- HAL_UART_Transmit_DMA(&hWifi, pQueue->Buffer + pQueue->tail, 1);
- }else{
- printf("%c",*(pQueue->Buffer + pQueue->tail));
- }
- #endif // PYJ.2019.12.13_END --
- printf("%c",*(pQueue->Buffer + pQueue->tail));
- uart_buf[cnt++] = *(pQueue->Buffer + pQueue->tail);
- if(*(pQueue->Buffer + pQueue->tail) == 0xbe)
- GoodDataStatus = true;
-
- // printf("Function : %s : ",__func__);
- // if (HAL_UART_Transmit_DMA(dst, pQueue->Buffer + pQueue->tail, 1) != HAL_OK)
- //if (HAL_UART_Transmit_DMA(&hTerminal, pQueue->Buffer + pQueue->tail, 1) != HAL_OK)
- //{
- // _Error_Handler(__FILE__, __LINE__);
- //}
- // printf("\r\n");
- pQueue->tail++;
- if (pQueue->tail >= QUEUE_BUFFER_LENGTH) pQueue->tail = 0;
- pQueue->data--;
- if(pQueue->data == 0 && GoodDataStatus == true){
- ESP8266_StrFilter(&uart_buf[Header]);
- memset(uart_buf,NULL,cnt);
- cnt = 0;
- GoodDataStatus = false;
- }
- }
- void Uart1_Data_Send(uint8_t* data,uint8_t size){
- HAL_UART_Transmit_DMA(&huart1, data,size);
- }
- void Uart2_Data_Send(uint8_t* data,uint8_t size){
- HAL_UART_Transmit_DMA(&huart2, data,size);
- }
|