 
 /*
 调试函数，DEBUG模式
 指令功能如下：
 */
#include "stm32f10x.h"
#include "uart1.h"
#include "ads1298.h"

extern volatile uint8_t UART1_TX_Buf[UART1_TX_LEN];// = {0};
extern volatile uint8_t UART1_RX_Buf[UART1_RX_LEN];// = {0};
extern volatile uint16_t UART1_TX_Index;// = 0;
extern volatile uint16_t UART1_RX_Index;// = 0;
extern volatile uint16_t UART1_RX_Len;// = 0;
volatile uint16_t UART1_RX_RIndex = 0;//读取数据采取FIFO算法
volatile uint8_t CHid = 1;

void Main_Delay(uint16_t pms);
void ReadData_Send(void);
int main(void)
{
	volatile uint8_t temp_reg = 0x20;
	uint8_t pi = 0;
	UART1_Init();
	ADS1298_Init();
	ADS1298_RegSet();
	USART_SendData( USART1, 0x53);
	Main_Delay(100);
	for(;;)
	{
		if(UART1_RX_Len)
		{
			if(UART1_RX_RIndex >= UART1_RX_LEN)
			{
				UART1_RX_RIndex = 0;
			}
			/*
			命令执行也应该进行FIFO缓冲执行,尽管执行在慢速下OK，我深信高速下会漏掉数据的。
			*/
			switch(UART1_RX_Buf[UART1_RX_RIndex])
			{
				case 0x31:	UART1_TX_Buf[0] = 0;UART1_TX_Buf[1] = temp_reg;UART1_DMA_ON(2);	break;					//测试USART
				case 0x32:	ADS1298_RDATAC_Mode(0);ADS1298_Debug_Reg(temp_reg);break;		//ADS298寄存器测试
				//case 0x33:	ADS1298_Debug_Sign();break;		//ADS1298信号测试
				case 0x33:	temp_reg++;UART1_TX_Buf[0] = temp_reg;UART1_DMA_ON(1); break;//ADS1298读取寄存器更改
				case 0x34:	temp_reg--;UART1_TX_Buf[0] = temp_reg;UART1_DMA_ON(1);break;//ADS1298读取寄存器更改
				case 0x35:	ADS1298_RDATAC_Mode(0);break;
				case 0x36:	ADS1298_RDATAC_Mode(1);break;
					/*START_SET; 					                        //开始转换 (use START pin)
										while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)==0);
										SPI_I2S_SendData(SPI1, 0x10);                       //RDATAC（连续读数据）
										break;*/
				case 0x37: 
										for(;;)
										{
												if(!(GPIOA->IDR & GPIO_Pin_3)) 	                   //读取DRDY状态，为低时，开始读取数据
												{  
													while(!(SPI1->SR & SPI_I2S_FLAG_TXE));		   
													SPI1->DR = 0x00; 				  
													while( !(SPI1->SR & SPI_I2S_FLAG_RXNE) );	
													pi = SPI1->DR ;
													ReadData_Send();
												}
												
										}
										break;
				case 0x38: ADS1298_Debug_Sign();break;
				case 0x39: break;
				case 0x40: UART1_TX_Buf[0] = CHid;UART1_DMA_ON(1);break;
				case 0x41: CHid++;UART1_TX_Buf[0] = CHid;UART1_DMA_ON(1);break;
				case 0x42: CHid--;UART1_TX_Buf[0] = CHid;UART1_DMA_ON(1);break;
				default	 : UART1_TX_Buf[0] = temp_reg;UART1_TX_Buf[1] = CHid;UART1_DMA_ON(2);	break;
			}
			UART1_RX_RIndex++;
			UART1_RX_Len--;
		}
	}
}

void ReadData_Send(void)
{
	uint8_t Count = 0;
	for(Count = 0; Count < 27; Count++)
	{
		while(!(SPI1->SR & SPI_I2S_FLAG_TXE));		       //等待SPI的TXE置位，表发送缓冲器空
		SPI1->DR = 0x00; 				                   //发送数据0x00，作为同步时钟	，注：不能发ads129X的敏感指令
		while(!(SPI1->SR & SPI_I2S_FLAG_RXNE));		       //等待SPI的RXNE置位，表接收缓冲器非空
		//USART_SendData(USART1, SPI_I2S_ReceiveData(SPI1));
		UART1_TX_Buf[Count] = SPI_I2S_ReceiveData(SPI1);
	}
	for(Count = 0; Count < 3; Count++)
	{
		UART1_TX_Buf[Count] = UART1_TX_Buf[Count + 3*CHid];
	}
	UART1_TX_Buf[3] = 0xcc;
	UART1_DMA_ON(4);
}
void Main_Delay(uint16_t pms)
{
	uint16_t pi = 0;
	uint16_t pj = 0;
	for(pi = 0 ; pi < 10000; pi++)
	{
		for(pj = 0; pj < pms; pj++);
	}
}

/*********************************************END OF FILE**********************/

