/*
 * uart_rcv.c
 *
 *  Created on: 2022年4月20日
 *      Author: dell
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "onechip_include_header.h"



uint8_t g_wifi_rx_buf[WIFI_RX_BUF_SIZE];
uint32_t g_wifi_rx_last;
uint32_t g_wifi_rx_head;

void esp_receive_Rec_IDLE(UART_HandleTypeDef *huart)
{
	// if ((__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE) != RESET))
	// {
	// 	__HAL_UART_CLEAR_IDLEFLAG(huart);
	// 	g_wifi_rx_len		= WIFI_RX_BUF_SIZE - __HAL_DMA_GET_COUNTER(huart->hdmarx);
	// 	HAL_UART_DMAStop(huart); 
	// 	HAL_UART_Receive_DMA(huart, g_wifi_rx_buf, WIFI_RX_BUF_SIZE);	
	// }
   // 手动判断并读取
    if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE)) {
        uint8_t data = (uint8_t)(huart->Instance->DR & 0xFF);
		__HAL_UART_CLEAR_FLAG(&huart2, UART_FLAG_RXNE);
        g_wifi_rx_buf[g_wifi_rx_last++] = data;
		if(g_wifi_rx_last == WIFI_RX_BUF_SIZE){
			g_wifi_rx_last = 0;
		}
        // 存缓冲区...
      
        // 清除RXNE标志（读DR后自动清除，这里不需要额外操作）
        // 但若要用HAL方式清除可调用：__HAL_UART_CLEAR_FLAG(&huart2, UART_FLAG_RXNE);
    }
    
    // 处理错误（如ORE）
    if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE)) {
        __HAL_UART_CLEAR_OREFLAG(huart);
        // 也可读取DR丢弃数据
        volatile uint8_t temp = huart->Instance->DR;
        (void)temp;
    }

	
}



void wifi_receive_clear(void){
	memset(g_wifi_rx_buf,0,WIFI_RX_BUF_SIZE);
	g_wifi_rx_last = 0;
	g_wifi_rx_head = 0;
}




void esp_receive_Rec_Start(UART_HandleTypeDef *huart){
		__HAL_UART_CLEAR_IDLEFLAG(huart);
	  memset((char *)g_wifi_rx_buf,0,WIFI_RX_BUF_SIZE);
	// 	HAL_UART_Receive_DMA(huart, g_wifi_rx_buf, WIFI_RX_BUF_SIZE);	
	__HAL_UART_ENABLE_IT(huart, UART_IT_RXNE);
}
