#include "Usart.h"
#include "Include.h"
#include "string.h"

#include "user_queue.h"
#include "Timer.h"

uint16_t Usart1_ucRx_length = 0;//串口1接收长度
uint8_t usart1_rx_done = 0;//串口1接收完成标志
uint8_t Usart1_Rx_Buf[USART1_RX_BUFFER_SIZE]={0};//串口1接收数据

device_state_t device[MAX_SLAVE_NUMBER];//8个设备的状态、控制结构体

extern uint16_t  usRec_Length;        //网口接收数据长度    
extern uint8_t  ucRec_Buffer[1024*10];  //网口接收数据缓存
uint8_t usart1_tx_done = 0;//串口1发送完成标志
//BLE_USART_TX_e                  A9UsartTxState_e = A9_USART_TX_IDLE;


void ble_usart_init(uint32_t baudrate)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;	
	
	RCC_AHB1PeriphClockCmd(BLE_USART_GPIO_CLK, ENABLE); 
	RCC_APB2PeriphClockCmd(BLE_USART_CLK, ENABLE);
	
	GPIO_PinAFConfig(BLE_USART_GPIO_PORT, BLE_USART_TX_PinSource, BLE_GPIO_AF_USART); 
	GPIO_PinAFConfig(BLE_USART_GPIO_PORT, BLE_USART_RX_PinSource, BLE_GPIO_AF_USART); 
	
	GPIO_InitStructure.GPIO_Pin = BLE_USART_TX_GPIO_PIN | BLE_USART_RX_GPIO_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度 50MHz
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
	GPIO_Init(BLE_USART_GPIO_PORT, &GPIO_InitStructure); 
	
	USART_InitStructure.USART_BaudRate = baudrate;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为 8 位数据格式
	USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
	USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; 
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
	USART_Init(BLE_USART, &USART_InitStructure); //初始化串口	
	
	NVIC_InitStructure.NVIC_IRQChannel = BLE_NVIC_IRQChannel;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority= 6;//抢占优先级 6
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应优先级 0
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ 通道使能
	NVIC_Init(&NVIC_InitStructure); 
	
	
	USART_ITConfig(BLE_USART, USART_IT_RXNE, ENABLE);//开启中断
	USART_ITConfig(BLE_USART, USART_IT_IDLE, ENABLE);//开启空闲中断
	
	
	USART_Cmd(BLE_USART, ENABLE); //使能串口
}

void USART1_IRQHandler(void)
{
	uint8_t temp;
	
	if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
    {
		if(Usart1_ucRx_length >= USART1_RX_BUFFER_SIZE)
		{
			Usart1_ucRx_length = 0;
		}
		
		Usart1_Rx_Buf[Usart1_ucRx_length++] = USART_ReceiveData(USART1);
		USART_ClearFlag(USART1, USART_IT_RXNE);

	}
	if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)
	{
		temp = USART1 ->SR;
		temp = USART1 ->DR;
//		ble_usart_send(Usart1_Rx_Buf, Usart1_ucRx_length);
//		Usart1_ucRx_length = 0;
		usart1_rx_done = 1;
	}
		
}

//void ble_usart_send(uint8_t *ucpTx_Data,uint8_t ucTx_length)
//{
//	uint8_t i;
//	for(i=0;i<ucTx_length;i++)
//	{
//		USART_SendData(USART1,*(ucpTx_Data+i));
//		while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
//	}
//		
//}

uint8_t ble_usart_send(uint8_t ucTx_length)
{
	uint8_t i,temp;
	uint8_t ucpTx_Data[ucTx_length];
	OutQueue(&queue,&ucpTx_Data[0],ucTx_length);//出队
	for(i=0;i<ucTx_length;i++)
	{
		USART_SendData(USART1,ucpTx_Data[i]);
		while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
	}
	return 2;
}

void net_received_data_analysis(void)
{
	
		//通过串口发送至52832
   // ble_usart_send(ucRec_Buffer, usRec_Length); 
   
	
	if(Analysis_data())//解析
	{
		if(EnterQueue(&queue,ucRec_Buffer,usRec_Length))//入队
		{
			
		}
	}
	
	if(tim_flag)//定时50ms之后发送
	{
		tim_flag = 0;
		if(!IsEmptyQueue(&queue))
		{
			ble_usart_send(usRec_Length); 					//通过串口发送至52832
		}
		
	}
	
}

void parameters_init(void)
{
	uint8_t i, j;
	for(i = 0; i < MAX_SLAVE_NUMBER; i++)
	{
		device[i].channel1.EMG_data_mode = RMS_Data;
		device[i].channel1.mode = ChannelModeAcquisition;
		device[i].channel1.rate = AcquisitionRate4K;
		device[i].channel1.state = TurnOff;
		
		device[i].channel2.EMG_data_mode = RMS_Data;
		device[i].channel2.mode = ChannelModeAcquisition;
		device[i].channel2.rate = AcquisitionRate4K;
		device[i].channel2.state = TurnOff;
		
		device[i].output_current_mA = 0;
		device[i].preinstall_state = TurnOff;
		device[i].stimulate_state = TurnOff;
		device[i].slice_state = SliceConnect;
		device[i].slice_detect_state = TurnOff;
		device[i].formwave_state = IdleState;
		for(j = 0; j < MAC_ADDRESS_LENGTH; j++)
		{
			device[i].mac_address[j] = 0x00;
		}
		device[i].connection_state = BLE_Disconnect;
		device[i].adapter_state = BatterySupply;
		device[i].electric_quantity = 100;
		device[i].reset_flag = Reset;
		device[i].frequency_Hz = 0;
		device[i].width_us = 0;
		device[i].climb_time_ms = 0;
		device[i].keep_time_ms = 0;
		device[i].down_time_ms = 0;
		device[i].rest_time_ms = 0;
	}
}

uint8_t Analysis_data()
{	
	uint8_t bcc=0,i=0,bcc1=0,j=0;
	
	while(ucRec_Buffer[i] != 0xAA)
	{
		i++;
		if(i >= usRec_Length)
		{
			return 0;
		}
	}
	
	bcc = ucRec_Buffer[ucRec_Buffer[++i]+2];//校验位  此时队首到了帧长的位置
	
	for(j=2;j<ucRec_Buffer[i]+2;j++)
	{
		bcc1+=ucRec_Buffer[j] ;
	}
	
	if(bcc != bcc1)//判断校验
	{
		return 0;
	}
	
	
	if(ucRec_Buffer[ucRec_Buffer[i]+3]!=0x55)//判断帧尾
	{
		return 0;
	}

	return 1;
}


