#include "includes.h"
#include <string.h>
#include <stdarg.h>

/**
 * BLV_C8_PB 串口使用情况
 * 	UART1 用与RCU进行双向通讯		115200  -> 对应设置 416
 * 	UART0 用于PB数据发送，没有接收	9600	-> 对应设置 5000
 * */

UART_t g_uart;		//目前该项目只使用串口1 进行双向通讯

void UARTx_Init(UART_IDX uart_id, Uart_prt prt_cf) {
	switch(uart_id){
		case UART_0:
			UART0_DeInit();                                               //clear all UART Register
			UART_IO_Init(IO_UART0,0);                                     //use PA0.1->RXD0, PA0.0->TXD0
			
			UARTInitRxTxIntEn(UART0,10000,UART_PAR_NONE);				  //baudrate=sysclock 48M/1000=4800,tx rx int enabled 
			UART0_Int_Enable();
			break;
		case UART_1:
			UART1_DeInit();                                                 //clear all UART Register
			UART_IO_Init(IO_UART1,0);                                    	//use PA0.13->RXD1, PB0.0->TXD1
			
			UARTInitRxTxIntEn(UART1,416,UART_PAR_NONE);					//baudrate=sysclock 48M/416=115200 tx rx int enabled 
			UART1_Int_Enable();
			
			memset(&g_uart,0,sizeof(UART_t));
			
			g_uart.RecvTimeout = Recv_115200_TimeOut;
			g_uart.processing_cf = prt_cf;
			
			break;
		case UART_2:
			UART2_DeInit();                                                 //clear all UART Register
			UART_IO_Init(IO_UART2,1);                                    	//use PA0.13->RXD1, PB0.0->TXD1
			
			UARTInitRxTxIntEn(UART2,98,UART_PAR_NONE);					//baudrate=sysclock 48M/416=115200 tx rx int enabled 
			//UART1_Int_Enable();
			
			break;
	}
}

/*******************************************************************************
* Function Name  : UART1_RecvINT_Processing 
* Description    : 串口1 接收中断处理函数 - 接收中断调用
*******************************************************************************/
void UART1_RecvINT_Processing(char data){
	if((g_uart.RecvLen + 1) >= USART_BUFFER_SIZE) g_uart.RecvLen = 0;
	g_uart.RecvBuffer[g_uart.RecvLen++] = (U8_T)data;
	
	g_uart.RecvIdleTiming = SysTick_1ms;
	g_uart.Receiving = 0x01;
}


void UART1_TASK(void){
	U8_T rev = 0;
	if(g_uart.Receiving == 0x01){
		
		if(SysTick_1ms - g_uart.RecvIdleTiming > Recv_115200_TimeOut){
			g_uart.RecvIdleTiming = SysTick_1ms;

			memcpy(g_uart.DealBuff,g_uart.RecvBuffer,g_uart.RecvLen);
			g_uart.DealLen = g_uart.RecvLen;
			g_uart.RecvLen = 0;
			g_uart.Receiving = 0;
			
			Dbg_Println(DBG_BIT_SYS_STATUS, "UART recv Len %d", g_uart.DealLen);
			Dbg_Print_Buff(DBG_BIT_SYS_STATUS,"UART buff",g_uart.DealBuff,g_uart.DealLen);
			
			if(g_uart.processing_cf != NULL){
               rev = g_uart.processing_cf(g_uart.DealBuff,g_uart.DealLen);
            }
			
			if(rev == 0x01){
				//Dbg_Print_Buff(DBG_BIT_Debug_STATUS,"error buff ",g_uart.DealBuff,g_uart.DealLen);
			}

            memset(g_uart.DealBuff,0,USART_BUFFER_SIZE);
		}
	}
}


/*调试信息输出接口*/
U32_T Dbg_Switch = (DBG_OPT_Debug_STATUS << DBG_BIT_Debug_STATUS)
        + (DBG_OPT_DEVICE_STATUS << DBG_BIT_DEVICE_STATUS)
        + (DBG_OPT_SYS_STATUS << DBG_BIT_SYS_STATUS);
	
#if DBG_LOG_EN

char Dbg_Buffer[256] = {0};
U32_T SysTick_Now = 0, SysTick_Last = 0, SysTick_Diff = 0;

#endif

void Dbg_NoTick_Println(int DbgOptBit, const char *cmd, ...){
#if DBG_LOG_EN
	U16_T str_offset = 0;

	if (Dbg_Switch & (1 << DbgOptBit)) {

        va_list args;       	//定义一个va_list类型的变量，用来储存单个参数
        va_start(args, cmd); 	//使args指向可变参数的第一个参数
        str_offset = vsnprintf(Dbg_Buffer, sizeof(Dbg_Buffer) ,cmd, args);  //必须用vprintf等带V的
        va_end(args);       	//结束可变参数的获取
        
		DBG_Printf(Dbg_Buffer,str_offset);
		
		DBG_Printf("\r\n",2);
    }

#endif	
}

void Dbg_Print(int DbgOptBit, const char *cmd, ...){

#if DBG_LOG_EN
	U16_T str_offset = 0;

	if (Dbg_Switch & (1 << DbgOptBit)) {
        SysTick_Now = SysTick_1ms;
        SysTick_Diff = SysTick_Now - SysTick_Last;      //上一次打印时间差
        SysTick_Last = SysTick_Now;
        str_offset = snprintf(Dbg_Buffer, sizeof(Dbg_Buffer),"%8d [%6d]: ", SysTick_Now, SysTick_Diff);
		DBG_Printf(Dbg_Buffer,str_offset);
		
        va_list args;       	//定义一个va_list类型的变量，用来储存单个参数
        va_start(args, cmd); 	//使args指向可变参数的第一个参数
        str_offset = vsnprintf(Dbg_Buffer, sizeof(Dbg_Buffer) ,cmd, args);  //必须用vprintf等带V的
        va_end(args);       	//结束可变参数的获取
        
		DBG_Printf(Dbg_Buffer,str_offset);
    }

#endif	
}

void Dbg_Println(int DbgOptBit, const char *cmd, ...){

#if DBG_LOG_EN
	U16_T str_offset = 0;

	if (Dbg_Switch & (1 << DbgOptBit)) {
        SysTick_Now = SysTick_1ms;
        SysTick_Diff = SysTick_Now - SysTick_Last;      //上一次打印时间差
        SysTick_Last = SysTick_Now;
        str_offset = snprintf(Dbg_Buffer, sizeof(Dbg_Buffer) , "%8ld [%6ld]: ", SysTick_Now, SysTick_Diff);
		DBG_Printf(Dbg_Buffer,str_offset);
		
        va_list args;       	//定义一个va_list类型的变量，用来储存单个参数
        va_start(args, cmd); 	//使args指向可变参数的第一个参数
        str_offset = vsnprintf(Dbg_Buffer, sizeof(Dbg_Buffer) ,cmd, args);  //必须用vprintf等带V的
        va_end(args);       	//结束可变参数的获取
        
		DBG_Printf(Dbg_Buffer,str_offset);
		
		DBG_Printf("\r\n",2);
    }

#endif	
}


void Dbg_Print_Buff(int DbgOptBit, const char *cmd, U8_T *buff,U16_T len){
#if DBG_LOG_EN
	U16_T str_offset = 0;

	if (Dbg_Switch & (1 << DbgOptBit)) {
        SysTick_Now = SysTick_1ms;
        SysTick_Diff = SysTick_Now - SysTick_Last;      //上一次打印时间差
        SysTick_Last = SysTick_Now;
        str_offset = snprintf(Dbg_Buffer, sizeof(Dbg_Buffer) , "%8ld [%6ld]: %s", SysTick_Now, SysTick_Diff,cmd);
		DBG_Printf(Dbg_Buffer,str_offset);
		
        for (uint32_t i = 0; i < len; i++) {
			str_offset = snprintf(Dbg_Buffer, sizeof(Dbg_Buffer) , "%02X ", buff[i]);
            DBG_Printf(Dbg_Buffer,str_offset);
        }
		
		DBG_Printf("\r\n",2);
    }

#endif	
}








