#include "sys.h"         //系统配置
#include "delay.h"       //延时
#include "usart.h"       //串口 
#include "dma.h"
#include "key.h"
#include "Timer.h"
#include "led.h" 
#include "hc05.h"
#include "ADS129x.h"
#include "ESP8266.h"
#include "malloc.h"
#include <string.h>
#include "adc.h"



/////////////////////////////////////////////////////////////////////////////////////////////
//本程序只供学习使用，未经作者许可，不得用于其它任何用途
//红茶电子科技-红茶
//淘宝 https://shop119364888.taobao.com
//创建日期:2020/11/01
//版本：
//版权所有，盗版必究。
/////////////////////////////////////////////////////////////////////////////////////////////

/*
	说明
	长按KEYPWR开机键三秒后，DS1常亮，设备开机，松开按键后，DS1熄灭，DS2常亮，松手后设备开机。
	拨码开关的设置详见使用文档
	开机后模块有四种工作模式：可以通过串口打印的数据查看工作状态
	每按下 KEYPWR 就会顺次切换功能
	1：蓝牙发送		
	2：WIFI发送
	3：主串口发送
	//4：数据记录到TF卡（暂无此功能，无绘图软件）
	按RESET按键设备会关机。
*/
/* 2020-11-5 新增： 
	 解决功能切换时死机的问题
*/
/* 2020-11-6 新增：	
	 WIFI同时发8通道数据
*/
/* 2021-03-28 新增：	
	 WIFI配网数据写入flash，上电自动按照设置的参数配网，不用每次启动都要AT配网
*/
/* 2021-04-13 新增：	
	 优化WIFI配网BUG
*/
/* 2021-04-18 新增：	
	 优化按键及LED
	 WIFI蓝牙采样率可以不同
*/
/* 2021-04-19 新增：	
	 WIFI最大2K发送
*/
/* 2021-04-29 新增：	
	 修改指示灯的BUG
*/
/* 2021-05-19 新增：	
	 适配新款模块 匿名上位机绘图
*/
/* 2021-07-10 新增：	
	 增加动态队列，优化驱动程序,增加DMA中断
*/
/* 2021-09-06 新增：	
	 优化WIFI驱动程序
*/
/* 2021-12-29 新增：	
	 适配匿名V7上位机
	 增加电量监测功能
	 增加ADS129X驱动
*/
/* 2022-03-14 新增：	
	 适配ADS1299 V2.2
	 程序版本 2.5.X
*/
/* 未来新增：	
	 适配OPENBCI软件（未完成）
*/

//蓝牙发送 采样率最大500
////HC05蓝牙透传最大速率20KB/S

//WIFI发送 采样率最大2K
//每帧次采样8通道，需要发送39字节   4 + 8*4 + 1 + 2 = 39
//TCP数据包 1443字节，每包可以发37次采样的数据，有效数据长度 39*37 = 1443
//500采样率 ，每秒发13.5包 数据量19.4KB/s
//1000采样率 ，每秒发27包 数据量39KB/s 
//2000采样率， 数据量78KB/s（波特率921600）
/////////////////////////////////////////////////////////////////////////////////////
//函数区		
		void Send_BlueTooth(void);//蓝牙串口发送
		void Send_ESP8266(void);
		void Send_UART1(void);
		void SoftReset(void);//软件复位
/////////////////////////////////////////////////////////////////////////////////////
//变量区


//main
int main(void)
{	
		work_state = SEND_WIFI;	
		//work_state=SEND_WIFI;	
		//work_state=SEND_UART;	
/////////////////////////////////////////////////////////////////////////      
		mem_init();	//内存管理
		//初始化系统时钟	 72M	
		SystemInit();	
		delay_init();
		// uart3_init(115200);
		// while(1) {
		// 	Main_printf("Test\r\n");
		// 	delay_ms(1000);
		// }

		LED_Init();		
		KEY_Init();		//初始化按键
		KE_IO_Init(); //拨码开关
		ADS129x_GPIO_Init();//初始化ADS129X外设
		HCO5_IO_Init();		//关蓝牙电源
		ESP8266_IO_Init();//关WIFI电源
		ESP8266_PWOERON;
		uart1_init(921600);//串口初始化	
		//while(Power_ON());//按键开机检测		
		Main_printf("Turn on\r\n");
		Main_printf("Version STM32F103C8 V2.5.2  ADS1299 V2.2 2022-05-20\r\n");	
		Main_printf("Current ads129x device id %d\r\n",ADS129X_DEVICE);
		delay_ms(200);
/////////////////////////////////////////////////////////////////////		
		TIM2_Init(500,7200);	//按键检测+按键消抖	
		TIM4_Init(10000,7200);	//系统指示灯

/////////////////////////////////////////////////////////////////////////	
		Main_printf("ADC Init\r\n");
		adc1_init();
		ADC_DMA_Config(ADCConvertedValue,1);
		ADC_DMA_Enable();
		Main_printf("Enable DMA ADC\r\n");
/////////////////////////////////////////////////////////////////////////		
	//初始化串口结构
		UART_Info =(_UART_Info*)mymalloc(sizeof(_UART_Info)); //数据结构
		UART_Info->sendbuf = (u8*)mymalloc(UART_SEND_LENGTH);	//发送缓冲区 
		if(UART_Info == NULL || UART_Info->sendbuf == NULL )
		{
				Main_printf("Memery malloc failed\r\n");
		}
		else
		{
				Main_printf("Memery malloc success，used %d\r\n",mem_perused());
		}
/////////////////////////////////////////////////////////////////////////
		while(1)
		{			
				Send_ESP8266();
				continue;	
				if(work_state == SEND_BULE)				
						Send_BlueTooth();	
				else if(work_state == SEND_WIFI)				
						Send_ESP8266();
				else if(work_state == SEND_UART)				
						Send_UART1();
		}	
}

//蓝牙发送
void Send_BlueTooth(void)
{
		u16 i,j,z,k=1,res;
		u8 addcheck=0,sumcheck=0;	
	
		Main_printf("蓝牙发送\r\n");
		TIM_Cmd(TIM4, DISABLE); 
/////////////////////////////////////////////////////////////////	
		//初始化129X
	#if ADS129X_DEVICE == ADS1299
			ADS129x_REG_BUF[1]=0xD5; //500采样率
	#elif ADS129X_DEVICE == ADS1298
			ADS129x_REG_BUF[1]=0x86; //500采样率
	#endif		
			ADS129x_ReInit(0, 0);//初始化1299	
/////////////////////////////////////////////////////////////////	
		Main_printf("申请串口缓存,内存当前占用 %d\r\n",mem_perused());	
		UART_Info->sendbuf = (u8*)mymalloc(UART_SEND_LENGTH);	//发送缓冲区 
		UART_Info->USART_Queue=queue_init(UART_QUEUE_SIZE,UART_QUEUE_LENGTH) ;//循环队列初始化大小
		if(UART_Info->sendbuf == NULL || UART_Info->USART_Queue == NULL )
		{
				Main_printf("内存申请失败\r\n");
		}
		else
		{
				Main_printf("内存申请成功，当前占用 %d\r\n",mem_perused());
		}
/////////////////////////////////////////////////////////////////		
		if(!KE_4)//开启蓝牙设置检测	
		{
				Main_printf("蓝牙设置检测\r\n");
				TIM4_Init(3000,7200);	//检测是否设置蓝牙
				TIM4_Timing=0;
				HC05_SET_FLAG=0;
				while(HC05_SET_FLAG==0 && TIM4_Timing<10)		
				HC05_SET();//检测蓝牙按键	
				if(HC05_SET_FLAG)//设置蓝牙
				{				
						LED_1=LED_OFF;
						LED_2=LED_OFF;
						Main_printf("开始设置蓝牙\r\n");			
						res=HCO5_AT_Confg();//开始设置		
						if(res)		
							Main_printf("蓝牙设置失败\r\n");							
						else									
							Main_printf("蓝牙设置成功\r\n");			
						Main_printf("重启\r\n");			
						TIM_Cmd(TIM4, ENABLE); //灯闪烁
						delay_s(3);
						TIM_Cmd(TIM4, DISABLE);//关灯 	
						POWER_OFF;//关机
						SoftReset();				
				}
		}
//////////////////////////////////////////////////////////////////
		Main_printf("开启蓝牙串口及DMA中断\r\n");
		HC05_ON;//开蓝牙		
		uart3_init(460800);//串口初始化 HC05串口	
		DMA_Config(DMA1_Channel2,(u32)&USART3->DR,(u32)UART_Info->sendbuf);
		USART_DMACmd(USART3,USART_DMAReq_Tx,ENABLE); //DMA
		UART3_DMA_TX_NVIC_Config(1);//开启串口 DMA中断
/////////////////////////////////////////////////////////////////				
		UART_Info->sendbuf[0]=0xAA;
		UART_Info->sendbuf[1]=0xFF;
		UART_Info->sendbuf[2]=0xF1;	
		UART_Info->sendbuf[3]=33;	//发送个8通道的数据 + 1字节丢包测试
/////////////////////////////////////////////////////////////////	
		Main_printf("开始发送数据\r\n");
		LED_1=LED_ON;
		LED_2=LED_ON;
		power_detective=1;
		lowpower=0;
		UART3_DMA_Finish=1;	
		KEY_TYPE=0;			
		delay_s(1);
		LED_1=LED_OFF;
		LED_2=LED_OFF;
		TIM4_Init(10000,7200);	//系统指示灯
		EXTI->IMR |= ADS129X_DRDY_LINE;//开DRDY中断
		while(work_state==SEND_BULE)//蓝牙发送
		{
				Key_Scan();
				Change_Mode();			
//////////////////////////////////////////////////////////////////////////////////////////////////
				if(UART3_DMA_Finish==1) //上一个DMA已经发送完成
				{		
						//从队列中取出数据并处理			
						if(serch_queue_data(UART_Info->USART_Queue))//队列有数据
						{
								for(i=0;i<8;i++)//处理8个通道的数据																
								{
										ADS129x_info.cannle[i] = *(*(UART_Info->USART_Queue->databuf + UART_Info->USART_Queue->front)+0+i*3)<<16 
															| *(*(UART_Info->USART_Queue->databuf + UART_Info->USART_Queue->front)+1+i*3)<<8 
															| *(*(UART_Info->USART_Queue->databuf + UART_Info->USART_Queue->front)+2+i*3);//获取原始数据
										ADS129x_info.p_Temp[i] = get_volt(ADS129x_info.cannle[i]);	//转成32位有符号数		
										ADS129x_info.cannle[i] = ADS129x_info.p_Temp[i];//赋值给无符号数，准备逻辑右移
										UART_Info->sendbuf[4+i*4]=ADS129x_info.cannle[i]>>0;
										UART_Info->sendbuf[5+i*4]=ADS129x_info.cannle[i]>>8; 
										UART_Info->sendbuf[6+i*4]=ADS129x_info.cannle[i]>>16;	
										UART_Info->sendbuf[7+i*4]=ADS129x_info.cannle[i]>>24;											
								}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////										
								//递增测试信号
								UART_Info->sendbuf[(ADS129X_USE_CANNLE+1)*4 ]=res++;//0-255
								//校验位
								sumcheck = 0;	
								addcheck=0;
								for(i=0;i< (ADS129X_USE_CANNLE+1)*4 +1 ;i++)
								{
										sumcheck += UART_Info->sendbuf[i];	
										addcheck += sumcheck;					
								}									
								UART_Info->sendbuf[(ADS129X_USE_CANNLE+1)*4 +1] = sumcheck;	//校验		
								UART_Info->sendbuf[(ADS129X_USE_CANNLE+1)*4 +2] = addcheck;	//校验	

								DMA_Enable(DMA1_Channel2,(ADS129X_USE_CANNLE+1)*4 +3);//开启DMA
								UART3_DMA_Finish=0;
								UART_Info->USART_Queue->front = (UART_Info->USART_Queue->front+1) % UART_Info->USART_Queue->capacity;//取数据，队头自增，存数据，队尾自增
						}
				}					
		}	
		EXTI->IMR &= ~(ADS129X_DRDY_LINE);//屏蔽外部中断	
		TIM_Cmd(TIM4, DISABLE); 
		HC05_OFF;//关蓝牙
		queue_Deinit(UART_Info->USART_Queue);//循环队列注销
		myfree(UART_Info->sendbuf);//释放内存
		power_detective=0;	
		Main_printf("切换工作模式\r\n");		
		Main_printf("内存当前占用 %d\r\n",mem_perused());
}



//主串口发送
void Send_UART1(void)
{	
		u16 i,j,z,k=1,res;
		u8 addcheck=0,sumcheck=0;	
	
		Main_printf("主串口发送\r\n");
		TIM_Cmd(TIM4, DISABLE); 
/////////////////////////////////////////////////////////////////	
		//初始化129X
	#if ADS129X_DEVICE == ADS1299
			ADS129x_REG_BUF[1]=0xD5; //500采样率
	#elif ADS129X_DEVICE == ADS1298
			ADS129x_REG_BUF[1]=0x86; //500采样率
	#endif		
			ADS129x_ReInit(0, 0);//初始化1299	
/////////////////////////////////////////////////////////////////	
		Main_printf("申请串口缓存,内存当前占用 %d\r\n",mem_perused());	
		UART_Info->sendbuf = (u8*)mymalloc(UART_SEND_LENGTH);	//发送缓冲区 
		UART_Info->USART_Queue=queue_init(UART_QUEUE_SIZE,UART_QUEUE_LENGTH) ;//循环队列初始化大小
		if(UART_Info->sendbuf == NULL || UART_Info->USART_Queue == NULL )
		{
				Main_printf("内存申请失败\r\n");
		}
		else
		{
				Main_printf("内存申请成功，当前占用 %d\r\n",mem_perused());
		}
/////////////////////////////////////////////////////////////////		
		Main_printf("开启主串口及DMA中断\r\n");
		uart1_init(460800);//串口初始化 HC05串口	
		DMA_Config(DMA1_Channel4,(u32)&USART1->DR,(u32)UART_Info->sendbuf);
		USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE); //DMA
		UART1_DMA_TX_NVIC_Config(1);//开启串口 DMA中断		
/////////////////////////////////////////////////////////////////				
		UART_Info->sendbuf[0]=0xAA;
		UART_Info->sendbuf[1]=0xFF;
		UART_Info->sendbuf[2]=0xF1;	
		UART_Info->sendbuf[3]=33;	//发送个8通道的数据 + 1字节丢包测试
/////////////////////////////////////////////////////////////////	
		Main_printf("开始发送数据\r\n");
		LED_1=LED_ON;
		LED_2=LED_ON;
		power_detective=1;
		lowpower=0;
		UART1_DMA_Finish=1;	
		KEY_TYPE=0;			
		delay_s(1);
		LED_1=LED_OFF;
		LED_2=LED_OFF;
		TIM4_Init(10000,7200);	//系统指示灯
		EXTI->IMR |= ADS129X_DRDY_LINE;//开DRDY中断
		while(work_state==SEND_UART)//蓝牙发送
		{
				Key_Scan();
				Change_Mode();			
//////////////////////////////////////////////////////////////////////////////////////////////////
				if(UART1_DMA_Finish==1) //上一个DMA已经发送完成
				{		
						//从队列中取出数据并处理			
						if(serch_queue_data(UART_Info->USART_Queue))//队列有数据
						{
								for(i=0;i<8;i++)//处理8个通道的数据																
								{
										ADS129x_info.cannle[i] = *(*(UART_Info->USART_Queue->databuf + UART_Info->USART_Queue->front)+0+i*3)<<16 
															| *(*(UART_Info->USART_Queue->databuf + UART_Info->USART_Queue->front)+1+i*3)<<8 
															| *(*(UART_Info->USART_Queue->databuf + UART_Info->USART_Queue->front)+2+i*3);//获取原始数据
										ADS129x_info.p_Temp[i] = get_volt(ADS129x_info.cannle[i]);	//转成32位有符号数		
										ADS129x_info.cannle[i] = ADS129x_info.p_Temp[i];//赋值给无符号数，准备逻辑右移
										UART_Info->sendbuf[4+i*4]=ADS129x_info.cannle[i]>>0;
										UART_Info->sendbuf[5+i*4]=ADS129x_info.cannle[i]>>8; 
										UART_Info->sendbuf[6+i*4]=ADS129x_info.cannle[i]>>16;	
										UART_Info->sendbuf[7+i*4]=ADS129x_info.cannle[i]>>24;											
								}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////			
								//递增测试信号
								UART_Info->sendbuf[(ADS129X_USE_CANNLE+1)*4 ]=res++;//0-255
								//校验位
								sumcheck = 0;	
								addcheck=0;
								for(i=0;i< (ADS129X_USE_CANNLE+1)*4 +1 ;i++)
								{
										sumcheck += UART_Info->sendbuf[i];	
										addcheck += sumcheck;					
								}									
								UART_Info->sendbuf[(ADS129X_USE_CANNLE+1)*4 +1] = sumcheck;	//校验		
								UART_Info->sendbuf[(ADS129X_USE_CANNLE+1)*4 +2] = addcheck;	//校验	

								DMA_Enable(DMA1_Channel4,(ADS129X_USE_CANNLE+1)*4 +3);//开启DMA
								UART1_DMA_Finish=0;
								UART_Info->USART_Queue->front = (UART_Info->USART_Queue->front+1) % UART_Info->USART_Queue->capacity;//取数据，队头自增，存数据，队尾自增
						}
				}					
		}	
		EXTI->IMR &= ~(ADS129X_DRDY_LINE);//屏蔽外部中断	
		TIM_Cmd(TIM4, DISABLE); 
		queue_Deinit(UART_Info->USART_Queue);//循环队列注销
		myfree(UART_Info->sendbuf);//释放内存	
		power_detective=0;	
		Main_printf("切换工作模式\r\n");		
		Main_printf("内存当前占用 %d\r\n",mem_perused());
}


void TEST_ESP8266(void)
{
	u16 i,j,z,k=1,res;
		u8 addcheck=0,sumcheck=0;	
		u8 datanum=0;	
	
		Main_printf("WIFI发送\r\n");
		TIM_Cmd(TIM4, DISABLE); 
/////////////////////////////////////////////////////////////////
		//初始化129X
	#if ADS129X_DEVICE == ADS1299
			ADS129x_REG_BUF[1]=0xD4; //1000采样率
	#elif ADS129X_DEVICE == ADS1298
			ADS129x_REG_BUF[1]=0x85; //1000采样率
	#endif		
		ADS129x_ReInit(1, 0);//初始化1299	
/////////////////////////////////////////////////////////////////	
		Main_printf("内存当前占用 %d\r\n",mem_perused());
		WIFI_Info =(_WIFI_Info*)mymalloc(sizeof(_WIFI_Info)); //数据结构
		WIFI_Info->sendbuf = (u8*)mymalloc(WIFI_SEND_LENGTH);	//WIFI发送缓冲区 	
		WIFI_Info->WIFI_Queue =  queue_init(WIFI_QUEUE_SIZE,WIFI_QUEUE_LENGTH) ;//循环队列初始化
		if(WIFI_Info == NULL || WIFI_Info->sendbuf == NULL || WIFI_Info->WIFI_Queue == NULL)
		{
				Main_printf("发送结构体 内存申请失败\r\n");
		}
		Main_printf("内存申请成功 当前内存使用率 %d\r\n",mem_perused());	
/////////////////////////////////////////////////////////////////		
		Main_printf("初始化WIFI\r\n");	

		Main_printf("开启DMA及中断11111111\r\n");
		ESP8266_Init();//初始化
		DMA_Config(DMA1_Channel7,(u32)&USART2->DR,(u32)WIFI_Info->sendbuf);
		UART2_DMA_TX_NVIC_Config(1);//开启串口 DMA中断
		USART_DMACmd(USART2,USART_DMAReq_Tx,ENABLE); //DMA
///////////////////////////////////////////////////////////////////////////////////	
		Wifi_Sendbuf_Init(); //初始化发送缓冲区	
///////////////////////////////////////////////////////////////////////////////////		
		Main_printf("开始发送数据\r\n");
		LED_1=LED_ON;
		LED_2=LED_ON;
		power_detective=1;
		lowpower=0;
		UART2_DMA_Finish=1;	
		KEY_TYPE=0;			
		delay_s(1);
		LED_1=LED_OFF;
		LED_2=LED_OFF;
		TIM4_Init(10000,7200);	//系统指示灯
		EXTI->IMR |= ADS129X_DRDY_LINE;//开DRDY中断
///////////////////////////////////////////////////////////////////////////////////
		while(work_state==SEND_WIFI)//WIFI发送,
		{	
				Key_Scan();
				Change_Mode();	
				if(WIFI_Info->WIFI_Queue->Queue_Full_flag==1)
				{
						WIFI_Info->WIFI_Queue->Queue_Full_flag=0;
						Main_printf("WIFI queue full ");
				}	
				if(UART2_DMA_Finish==1)
				{	
						if(serch_queue_data(WIFI_Info->WIFI_Queue))//队列有数据
						{
								for(j=0;j<ADS129x_info.Ads129x_Write_Num;j++)//一帧N次采样的数据
								{
										for(i=0;i<8;i++)//处理8个通道的数据																
										{
												ADS129x_info.cannle[i] = *(*(WIFI_Info->WIFI_Queue->databuf + WIFI_Info->WIFI_Queue->front)+0+i*3+j*ADS129x_info.Ads129x_Data_Move)<<16 
																	| *(*(WIFI_Info->WIFI_Queue->databuf + WIFI_Info->WIFI_Queue->front)+1+i*3+j*ADS129x_info.Ads129x_Data_Move)<<8 
																	| *(*(WIFI_Info->WIFI_Queue->databuf + WIFI_Info->WIFI_Queue->front)+2+i*3+j*ADS129x_info.Ads129x_Data_Move);//获取原始数据
												ADS129x_info.p_Temp[i] = get_volt(ADS129x_info.cannle[i]);	//转成32位有符号数		
												ADS129x_info.cannle[i] = ADS129x_info.p_Temp[i];//赋值给无符号数，准备逻辑右移
											
												WIFI_Info->sendbuf[4+i*4+j*WIFI_Info->datalength]=ADS129x_info.cannle[i];	
												WIFI_Info->sendbuf[5+i*4+j*WIFI_Info->datalength]=ADS129x_info.cannle[i]>>8;
												WIFI_Info->sendbuf[6+i*4+j*WIFI_Info->datalength]=ADS129x_info.cannle[i]>>16;
												WIFI_Info->sendbuf[7+i*4+j*WIFI_Info->datalength]=ADS129x_info.cannle[i]>>24;											
										}
										//递增测试信号
										WIFI_Info->sendbuf[ (ADS129X_USE_CANNLE+1)*4 +j*WIFI_Info->datalength]=res++;//0-255
										
										//校验位
										sumcheck = 0;	addcheck=0;
										for(i=0;i< (ADS129X_USE_CANNLE+1)*4 +1 ;i++)
										{
												sumcheck += WIFI_Info->sendbuf[i +j*WIFI_Info->datalength ];	
												addcheck += sumcheck;					
										}									
										WIFI_Info->sendbuf[(ADS129X_USE_CANNLE+1)*4 +1 +j*WIFI_Info->datalength] = sumcheck;	//校验		
										WIFI_Info->sendbuf[(ADS129X_USE_CANNLE+1)*4 +2 +j*WIFI_Info->datalength] = addcheck;	//校验	
								}
		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////											
								DMA_Enable(DMA1_Channel7,WIFI_SEND_LENGTH);//开启DMA
								UART2_DMA_Finish=0;
								WIFI_Info->WIFI_Queue->front = (WIFI_Info->WIFI_Queue->front+1) % WIFI_Info->WIFI_Queue->capacity;//取数据，队头自增，存数据，队尾自增
						}
				}			
		}
		EXTI->IMR &= ~(ADS129X_DRDY_LINE);//屏蔽外部中断
		TIM_Cmd(TIM4, DISABLE); 
		ESP8266_PWOEROFF;//关WIFI
		queue_Deinit(WIFI_Info->WIFI_Queue);//释放内存
		myfree(WIFI_Info->sendbuf);//释放内存
		myfree(WIFI_Info);//释放内存
		power_detective=0;	

		Main_printf("切换工作模式\r\n");
		Main_printf("内存当前占用 %d\r\n",mem_perused());
}


uint8_t g_aucSendIndexData = 0;
int iDebug = 0;

void testLoop() {
	static int Total = 0;
	char szBuff[330] = {0};
	int i=0;
	int iCnt = 1000;

	for (; i< 330; i++) {
		*((uint8_t *)(WIFI_Info->sendbuf + i)) = i;
	}

	delay_s(5);

	Main_printf("Start Send test Data\r\n");
	while (iCnt > 0) {
		if(UART2_DMA_Finish==1)
	    {
			delay_ms(4);
		 	DMA_Enable(DMA1_Channel7,330);//开启DMA
			Total ++;
		 	UART2_DMA_Finish=0;
			iCnt --;
		}

		//ESP8266_SendData(WIFI_Info->sendbuf, 330);

		delay_us(10);
		if (Total % 100 == 0) {
			delay_ms(10);
			Main_printf("Start Send %d \r\n", Total);
		}
	}


	Main_printf("Start Send %d\r\n", Total * 330);
	while(1) {

	}
}

//WIFI发送 采样率最大2K
void Send_ESP8266(void)
{	
		u16 i,j,z,k=1,res;
		u8 addcheck=0,sumcheck=0;	
		u8 datanum=0;	
	
		Main_printf("WIFI发送\r\n");
		TIM_Cmd(TIM4, DISABLE); 
/////////////////////////////////////////////////////////////////
		//ADS129x_ReInit(1, 2);//初始化1299	
/////////////////////////////////////////////////////////////////	
		Main_printf("Memery Current %d\r\n",mem_perused());
		WIFI_Info =(_WIFI_Info*)mymalloc(sizeof(_WIFI_Info)); //数据结构
		WIFI_Info->sendbuf = (u8*)mymalloc(1024);	//WIFI发送缓冲区 	
		WIFI_Info->WIFI_Queue =  queue_init(WIFI_QUEUE_SIZE,WIFI_QUEUE_LENGTH) ;//循环队列初始化
		if(WIFI_Info == NULL || WIFI_Info->sendbuf == NULL || WIFI_Info->WIFI_Queue == NULL)
		{
				Main_printf("Wifi send struct malloc failed\r\n");
		}
		Main_printf("Wifi send struct malloc success %d\r\n",mem_perused());	
		Main_printf("WIFI Init\r\n");
		Main_printf("开启DMA及中断\r\n");
		ADS129x_Stop();
		//ADS129x_ReInit(1, 0);
		ESP8266_Init();//初始化
		DMA_Config(DMA1_Channel7,(u32)&USART2->DR,(u32)WIFI_Info->sendbuf);
		UART2_DMA_TX_NVIC_Config(1);//开启串口 DMA中断
		USART_DMACmd(USART2,USART_DMAReq_Tx,ENABLE); //DMA
///////////////////////////////////////////////////////////////////////////////////	
		Wifi_Sendbuf_Init(); //初始化发送缓冲区	
///////////////////////////////////////////////////////////////////////////////////		
		Main_printf("开始发送数据\r\n");
		LED_1=LED_ON;
		LED_2=LED_ON;
		power_detective=1;
		lowpower=0;
		UART2_DMA_Finish=1;	
		KEY_TYPE=0;			
		delay_s(1);
		LED_1=LED_OFF;
		LED_2=LED_OFF;
		TIM4_Init(10000,7200);	//系统指示灯

///////////////////////////////////////////////////////////////////////////////////
		while(work_state==SEND_WIFI)//WIFI发送,
		{	
				Key_Scan();
				Change_Mode();	
				if(WIFI_Info->WIFI_Queue->Queue_Full_flag==1)
				{
						WIFI_Info->WIFI_Queue->Queue_Full_flag=0;
						Main_printf("WIFI queue full ");
				}

				//testLoop();

				/* Loop Message. */
				Cyton_UartPraseLoop();

				if(UART2_DMA_Finish==1)
				{	
						if(serch_queue_data(WIFI_Info->WIFI_Queue))//队列有数据
						{
								//delay_ms(1);
								for(j=0;j<ADS129x_info.Ads129x_Write_Num;j++)//一帧N次采样的数据
								{
						#if 0
										for(i=0;i<8;i++)//处理8个通道的数据																
										{
												ADS129x_info.cannle[i] = *(*(WIFI_Info->WIFI_Queue->databuf + WIFI_Info->WIFI_Queue->front)+0+i*3+j*ADS129x_info.Ads129x_Data_Move)<<16 
																	| *(*(WIFI_Info->WIFI_Queue->databuf + WIFI_Info->WIFI_Queue->front)+1+i*3+j*ADS129x_info.Ads129x_Data_Move)<<8 
																	| *(*(WIFI_Info->WIFI_Queue->databuf + WIFI_Info->WIFI_Queue->front)+2+i*3+j*ADS129x_info.Ads129x_Data_Move);//获取原始数据
												ADS129x_info.p_Temp[i] = get_volt(ADS129x_info.cannle[i]);	//转成32位有符号数		
												ADS129x_info.cannle[i] = ADS129x_info.p_Temp[i];//赋值给无符号数，准备逻辑右移
											
												WIFI_Info->sendbuf[4+i*4+j*WIFI_Info->datalength]=ADS129x_info.cannle[i];	
												WIFI_Info->sendbuf[5+i*4+j*WIFI_Info->datalength]=ADS129x_info.cannle[i]>>8;
												WIFI_Info->sendbuf[6+i*4+j*WIFI_Info->datalength]=ADS129x_info.cannle[i]>>16;
												WIFI_Info->sendbuf[7+i*4+j*WIFI_Info->datalength]=ADS129x_info.cannle[i]>>24;											
										}
										//递增测试信号
										WIFI_Info->sendbuf[ (ADS129X_USE_CANNLE+1)*4 +j*WIFI_Info->datalength]=res++;//0-255
										
										//校验位
										sumcheck = 0;	addcheck=0;
										for(i=0;i< (ADS129X_USE_CANNLE+1)*4 +1 ;i++)
										{
												sumcheck += WIFI_Info->sendbuf[i +j*WIFI_Info->datalength ];	
												addcheck += sumcheck;					
										}									
										WIFI_Info->sendbuf[(ADS129X_USE_CANNLE+1)*4 +1 +j*WIFI_Info->datalength] = sumcheck;	//校验		
										WIFI_Info->sendbuf[(ADS129X_USE_CANNLE+1)*4 +2 +j*WIFI_Info->datalength] = addcheck;	//校验
						#else
									/* 1: 0xa0 2: autoCnt 3: 24 byte 27-32:0x00 33: 0xC0*/
									WIFI_Info->sendbuf[j * WIFI_Info->datalength] = 0xA0;
									WIFI_Info->sendbuf[j * WIFI_Info->datalength + 1] = g_aucSendIndexData ++;
									memcpy(&WIFI_Info->sendbuf[j * WIFI_Info->datalength + 2],  
										(*(WIFI_Info->WIFI_Queue->databuf + WIFI_Info->WIFI_Queue->front) +j * ADS129x_info.Ads129x_Data_Move), 
										ADS129x_info.Ads129x_Data_Move); /* 24 byte data.*/
									WIFI_Info->sendbuf[j * WIFI_Info->datalength + 27] = 0x0;
									WIFI_Info->sendbuf[j * WIFI_Info->datalength + 27] = 0x0;
									WIFI_Info->sendbuf[j * WIFI_Info->datalength + 28] = 0x0;
									WIFI_Info->sendbuf[j * WIFI_Info->datalength + 29] = 0x0;
									WIFI_Info->sendbuf[j * WIFI_Info->datalength + 30] = 0x0;
									WIFI_Info->sendbuf[j * WIFI_Info->datalength + 31] = 0x0;
									WIFI_Info->sendbuf[j * WIFI_Info->datalength + 32] = 0xC0;
						#endif
								}
		////////////////////////////////////////////////////////////////////////////////////////////////////////////////////											
								//ESP8266_SendData(WIFI_Info->sendbuf, WIFI_SEND_LENGTH);
								delay_ms(1);
								DMA_Enable(DMA1_Channel7,WIFI_SEND_LENGTH);//开启DMA
								UART2_DMA_Finish=0;
								WIFI_Info->WIFI_Queue->front = (WIFI_Info->WIFI_Queue->front+1) % WIFI_Info->WIFI_Queue->capacity;//取数据，队头自增，存数据，队尾自增
								if (iDebug ++ % 1000 == 0) {
									Main_printf("WIFI Send  WIFI_Info->WIFI_Queue->front %d ADS129x_info.Ads129x_Data_Move %d Ads129x_Write_Num %d WIFI_Info->datalength %d\n", 
									WIFI_Info->WIFI_Queue->front, ADS129x_info.Ads129x_Data_Move, ADS129x_info.Ads129x_Write_Num, WIFI_Info->datalength);
								}
						} else {
							//printf("n");
							//delay_ms(500);
						}
				}
		}

		EXTI->IMR &= ~(ADS129X_DRDY_LINE);//屏蔽外部中断
		TIM_Cmd(TIM4, DISABLE); 
		ESP8266_PWOEROFF;//关WIFI
		queue_Deinit(WIFI_Info->WIFI_Queue);//释放内存
		myfree(WIFI_Info->sendbuf);//释放内存
		myfree(WIFI_Info);//释放内存
		power_detective=0;	

		Main_printf("切换工作模式\r\n");
		Main_printf("内存当前占用 %d\r\n",mem_perused());
}


//TF存储
void Write_TF(void)
{
	
}





void SoftReset(void)//软件复位
{
		//__disable_irq()只是禁止CPU去响应中断，没有真正的去屏蔽中断的触发
		//__enable_irq()开启中断后，由于相应的中断标志没有清空，因而还会触发中断
    __set_FAULTMASK(1); // 关闭所有中断
    NVIC_SystemReset(); // 复位
}




/**********************************************************************
编译结果里面的几个数据的意义：
Code：表示程序所占用 FLASH 的大小（FLASH）
RO-data：即 Read Only-data， 表示程序定义的常量，如 const 类型（FLASH）
RW-data：即 Read Write-data， 表示已被初始化的全局变量（SRAM）
ZI-data：即 Zero Init-data， 表示未被初始化的全局变量(SRAM)
***********************************************************************/

//由上图可知，重映射的方式一共有三种。分别描述如下：
//1.GPIO_Remap_SWJ_JTAGDisable： /*!< JTAG-DP Disabled and SW-DP Enabled */  即能用PB3，PB4，PA15做普通IO，PA13&14用于SWD调试
//2.GPIO_Remap_SWJ_Disable：  /*!< Full SWJ Disabled (JTAG-DP + SW-DP) */  5个引脚全为普通引脚，但不能再用JTAG&SWD仿真器调试，只能用st-link调试 
//3.GPIO_Remap_SWJ_NoJTRST： /*!< Full SWJ Enabled (JTAG-DP + SW-DP) but without JTRST */PB4可为普通IO口，JTAG&SWD正常使用，但JTAG没有复位

//如果你用到所有的五个引脚当做普通IO口，那么上述步骤二中的重映射配置应写为GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE); 
//如果你用PB3，PB4，PA15做普通IO，PA13&14用于SWD调试，则重映射配置应写为GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); 
//同理可配置只用PB4可为普通IO口的情况。

//		DMA_Config(DMA1_Channel4,(u32)&USART1->DR,(u32)data_to_send2);
//		USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE); //DMA		
//		DMA_Config(DMA1_Channel7,(u32)&USART2->DR,(u32)data_to_send3);
//		USART_DMACmd(USART2,USART_DMAReq_Tx,ENABLE); //DMA		
//		DMA_Config(DMA1_Channel2,(u32)&USART3->DR,(u32)data_to_send3);
//		USART_DMACmd(USART3,USART_DMAReq_Tx,ENABLE); //DMA	