//ads1298.c
#include "ads1298.h"
#include "uart1.h"

void ADS1298_Delayms(uint16_t pms)
{
	uint16_t pi = 0;
	uint16_t pj = 0;
	for(pi = 0; pi < 10000; pi++)
	{
		for(pj = 0; pj < pms; pj++);
	}
}
void ADS1298_TIME_Config(void)
{
	PWDN_RESET;										  //以下提到的tclk=1/2.048  us
	IC_RESET;					                          //ads1299上电之前所有输入为低
	START_RESET;       
	ADS1298_Delayms(2000);               	                  //等待晶振被唤醒
	
	PWDN_SET;                                           //上电时序开始
	IC_SET;
	ADS1298_Delayms(500);		                              //复位之前的上电时间>32ms (2^16 tclk)
	IC_RESET;											  //复位开始
	ADS1298_Delayms(500);                                          //复位低电平时间>2tclk
	IC_SET;
	ADS1298_Delayms(800);                                         //等待>18tclk,上电时序完成	
}
void ADS1298_GPIO_Config(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	RCC_APB2PeriphClockCmd( (RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB), ENABLE);
	  /* Configure SPI1 pins: SCK, MISO and MOSI ---------------------------------*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;    
  GPIO_Init(GPIOA, &GPIO_InitStructure);       //MI
  
  /* 配置控制端口 ------------------------------------------------------------*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  
  /* 配置DRDY端口 ------------------------------------------------------------*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure);  
	/*
  GPIO_InitTypeDef GPIO_InitStruct;
  RCC_APB2PeriphClockCmd( (RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB), ENABLE);
  // Configure ADS1298 SPI Section 
  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;
  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOA, &GPIO_InitStruct);       //SCK

  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;    
  GPIO_Init(GPIOA, &GPIO_InitStruct);       //MI

  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7;
  GPIO_Init(GPIOA, &GPIO_InitStruct);       //MO
    
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4;
  GPIO_Init(GPIOA, &GPIO_InitStruct);       //NSS

//ADS1298 Control Section
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_1;
  GPIO_Init(GPIOB, &GPIO_InitStruct);       //PWDN
   
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3;
  GPIO_Init(GPIOA, &GPIO_InitStruct);       //DRDY
    
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
  GPIO_Init(GPIOB, &GPIO_InitStruct);       //RESET

  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4;
  GPIO_Init(GPIOA, &GPIO_InitStruct);       //START
	*/
}

void ADS1298_NVIC_Config(void)
{
}
void ADS1298_DMA_Config(void)
{
}
void ADS1298_MODE_Config(void)
{
	SPI_InitTypeDef  SPI_InitStructure;
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
	SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;   //72MHZ  16分频到4.5MHz
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  SPI_InitStructure.SPI_CRCPolynomial = 7;
  SPI_Init(SPI1, &SPI_InitStructure);
  /* Enable SPI1  */
  SPI_Cmd(SPI1, ENABLE);
	
	/*
    SPI_InitTypeDef SPI_InitStruct;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
    
    SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
    SPI_InitStruct.SPI_CPHA = SPI_CPHA_2Edge;
    SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;
    SPI_InitStruct.SPI_CRCPolynomial = 7;
    SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
    SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
    SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
    SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
    SPI_InitStruct.SPI_NSS = SPI_NSS_Soft;
    SPI_Init(SPI1, &SPI_InitStruct);
	*/
}
void ADS1298_Init(void)
{
	ADS1298_GPIO_Config();
	ADS1298_NVIC_Config();
	ADS1298_DMA_Config();
	ADS1298_MODE_Config();

	ADS1298_Delayms(100);
	ADS1298_TIME_Config();
}
void ADS1298_RESET(void)
{
}
void ADS1298_RegSet(void)
{
	while( (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)) == 0);
	SPI_I2S_SendData(SPI1, 0x11);                       //SDATAC

//WREG r=0x01  num=3
	while( (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)) == 0);//这种句子，不要那会是致命的打击
	SPI_I2S_SendData(SPI1, 0x41);		
	while( (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)) == 0);
	SPI_I2S_SendData(SPI1, 0x02);
//CONFIG1=0x85		CONFIG2=0x11		CONFIG3=0xcc	
	while( (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)) == 0){};
  SPI_I2S_SendData(SPI1, 0x86);                       //设置采样率 83=4K,84=2k,85=1K,86=500
	while( (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)) == 0){};
  SPI_I2S_SendData(SPI1, 0x11);                       //设置内部测试信号（外驱） 
	while( (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)) == 0){};
  SPI_I2S_SendData(SPI1, 0xCC);                       //A/D使用内部参考2.4V,右腿驱动使用内部参考RLDREF=(AVDD+AVSS)/2
	
	ADS1298_Delayms(100);
		
//WREG r=0x05  num=8	
	while( (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)) == 0);
	SPI_I2S_SendData(SPI1, 0x45);                       //设置各通道
	while( (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)) == 0);
	SPI_I2S_SendData(SPI1, 0x07); 
//CH1SET=0x60		CH8SET=0x60
	while( (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)) == 0){};
  SPI_I2S_SendData(SPI1, 0x60);   //0x60为一般输入，采集脑电时使用0x60；  
	while( (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)) == 0){};	
  SPI_I2S_SendData(SPI1, 0x60);   //0x61为通道短路，测试内部噪声用；
	while( (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)) == 0){};	
  SPI_I2S_SendData(SPI1, 0x60);   //0x65为通道连接内部信号源，测试程序时用 
	while( (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)) == 0);
	SPI_I2S_SendData(SPI1, 0x60);                       //ch4  PGA=12,Input shorted 
	while( (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)) == 0);
	SPI_I2S_SendData(SPI1, 0x60);                       //ch5  PGA=12,Input shorted 
	while( (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)) == 0);
	SPI_I2S_SendData(SPI1, 0x60);                       //ch6  PGA=12,Input shorted 
	while( (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)) == 0);
	SPI_I2S_SendData(SPI1, 0x60);                       //ch7  PGA=12,Input shorted 
	while( (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)) == 0);
	SPI_I2S_SendData(SPI1, 0x60);                       //ch8  PGA=12,Input shorted 
	
//WREG r=0x0d  num=1
	while( (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)) == 0);
	SPI_I2S_SendData(SPI1, 0x4D);  
	while( (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)) == 0);
	SPI_I2S_SendData(SPI1, 0x00);
//RLD_SENSP=0xff  
	while( (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)) == 0);
	SPI_I2S_SendData(SPI1, 0xFF);                       //通道 INP 12345678 作为RLD信号驱动

//	SPI_I2S_SendData(SPI1, 0x11);
/*
	START_SET; 					                        //开始转换 (use START pin)
	while( (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)) == RESET);
	SPI_I2S_SendData(SPI1, 0x10);                       //RDATAC（连续读数据）
	*/
}
void ADS1298_RDATAC_Mode(uint8_t pmode)
{
	if(pmode == 1)
	{
		START_SET; 					                        //开始转换 (use START pin)
		while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)==0);
		SPI_I2S_SendData(SPI1, 0x10);                       //RDATAC（连续读数据）
	}
	else
	{
		START_RESET;
		while( (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)) == 0);
		SPI_I2S_SendData(SPI1, 0x11);                       //通道 INP 12345678 作为RLD信号驱动
	}
}
//使用之前最好ADS1298_RDATAC_Mode(0)
void ADS1298_Debug_Reg(uint8_t preg)
{
	ADS1298_RDATAC_Mode(0);
	ADS1298_Delayms(100);
	while( (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)) == 0);
	SPI_I2S_SendData(SPI1, preg);//SPI_I2S_SendData(SPI1, (0x20|preg))一直出错
	while( (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)) == 0);
	SPI_I2S_SendData(SPI1, 0x00);
	ADS1298_Delayms(10);
	USART_SendData(USART1, SPI_I2S_ReceiveData(SPI1));
}
extern volatile uint8_t UART1_TX_Buf[UART1_TX_LEN];// = {0};
void ADS1298_Debug_Sign(void)
{
	uint8_t pi = 0;
//	uint8_t pc[27] = {0};
	while((GPIOA->IDR & GPIO_Pin_3) == 1);
/*	
	while(!(SPI1->SR & SPI_I2S_FLAG_TXE));		   
	SPI1->DR = 0x00; 				  
	while(!(SPI1->SR & SPI_I2S_FLAG_RXNE));	
	pi=SPI1->DR;	
*/
	//按照这样发送数据，它会偏移一个字的数据
	//在读取数据的过程中，需要DIN置低.
	//		while(!(SPI1->SR & SPI_I2S_FLAG_TXE));
	//	SPI1->DR = 0x00; 
	
				while(!(SPI1->SR & SPI_I2S_FLAG_TXE));		   
			SPI1->DR = 0x00; 				  
			while(!(SPI1->SR & SPI_I2S_FLAG_RXNE));	
			pi=SPI1->DR ;	
	for(pi = 0; pi < 27; pi++)
	{
		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[pi] = SPI_I2S_ReceiveData(SPI1);
	}
	for(pi = 0; pi < 27; pi++)
	{
		USART_SendData(USART1,UART1_TX_Buf[pi]); 
		ADS1298_Delayms(10);
	}
//	UART1_DMA_ON(28);
}
/******************end of the file**********************/
