#include "main.h"
#include "Function.h"
#include "spi.h"
#include "sdram.h"
#include "math.h"
#include "exfuns.h"
#include "malloc.h"
#include "cnn_model.h" 

uint32_t number;
char file_name[20];

/****************************************	FPGA启动信号	*********************************************/

void sendStartSignalToFPGA(void) {
			HAL_GPIO_WritePin(GPIOD, GPIO_PIN_11, GPIO_PIN_SET);//实现发送启动信号的逻辑
			delay_ms(1);
			HAL_GPIO_WritePin(GPIOD, GPIO_PIN_11, GPIO_PIN_RESET);  
}

/****************************************	数据处理	*********************************************/

float Data_processing(uint16_t temp)
{
	float Data;
	temp = temp << 1; 
	temp = temp ^ 0x8000;
	Data = (((float)temp*9)/65536) - 4.5;
	return Data;
}

/****************************************	接收采集信号 -> SDRAM	*********************************************/

#if 1
void SPI_Read_SDRAM(void){
	uint32_t i = 0;
	unsigned int count_err = 0;
	unsigned char flag = 1;
	uint16_t Temp;
	float Data;
	sendStartSignalToFPGA();
	printf("SDRAM开始写入\r\n");
	while(flag){
		SPI_FPGA_CS_LOW();
		Temp = SPI1_ReadWriteByte(0x00);
		SPI_FPGA_CS_HIGH();
		Data = Data_processing(Temp);
		if(Temp != 0x0000 && fabsf(Data) > 0.00001){
			count_err = 0;
			*(float*)(Bank5_SDRAM_ADDR+4*i++) = Data;
			if( i >= Data_volume ){
				printf("SDRAM写入结束\r\n");
				number = i;
				i = 0;
				flag = 0;
			}
		}
		else{	
			count_err++;
			if(count_err > 10000){
				printf("信号采集失败\r\n");
				break;
			}
		}
	}
}
#endif

#if 0
void SPI_Read_SDRAM(void){
	uint32_t i = 0;
	unsigned int count_err = 0;
	unsigned char flag = 1;
	uint16_t buff[100];
	uint16_t Temp;
	float Data;
	sendStartSignalToFPGA();
	printf("SDRAM开始写入\r\n");
	while(flag){
		SPI_FPGA_CS_LOW();
		Temp = SPI1_ReadWriteByte(0x00);
		SPI_FPGA_CS_HIGH();
		Data = Data_processing(Temp);
		if(Temp != 0x0000 && Data != 0.000000){
			count_err = 0;
			*(uint16_t*)(Bank5_SDRAM_ADDR+2*i++) = Temp;
			/*	每次取前100个数据 若前100个数据相同 则判断为数据采集失败	*/
			if(i == 101){
				for(unsigned int j = 0;j < 100;j++){
					if(buff[j] == buff[j+1]){
						count_err++;
					}
					else break;
				}
				if(count_err > 70){
					printf("信号采集失败\r\n");
					flag = 0;
					break;
				}
			}
			else if(i < 101){
				buff[i] = Temp;
			}
			/************************************************************/
			if( i >= Data_volume ){
				printf("SDRAM写入结束\r\n");
				number = i;
				flag = 0;
			}
		}
		/*	若连续100个数据都为0 则判断为采集失败	*/
		else{	
			count_err++;
			if(count_err > 100){
				printf("信号采集失败\r\n");
				flag = 0;
				break;
			}
		}
	}
}
#endif

/****************************************	初始化SD卡	*********************************************/

void Formatting_test(void)
{
	u32 total,free;
	exfuns_init();
	uint8_t _res_sd;
	res = f_mount(&fatfs,"0:",1);// 挂载SD卡
	delay_ms(1);
	if(res != RES_OK)							
	{
		printf("f_mount error!\r\n");// 挂载失败
		while(1)
		{
			delay_ms(500);
			LED_R=!LED_R;
		}
	}
	else
		printf("f_mount successful!\r\n");// 挂载成功
	
	fatbuf=(u8*)mymalloc(SRAMEX,512);			//为fatbuf申请内存
	_res_sd = exf_getfree("0:",&total,&free);	//得到SD卡的总容量和剩余容量
	if(_res_sd)printf("SD Card Fatfs Error!");
	while(_res_sd)
	{	  
		delay_ms(100);
		LED_R=!LED_R;
	}
	printf("初始化完成！\r\n");
}


/****************************************	接收采集信号 -> SDRAM	*********************************************/

void SDRAM_TO_SD(void)
{
	static unsigned char filenum = 1;
	float temp;
	int i;

	while(1){
		sprintf(file_name,"souce_%d.bin",filenum);
		res = f_stat(file_name,&fno);
		if(res == FR_OK){
			filenum++;
			printf("文件存在\r\n");
		}
		else if(res == FR_NO_FILE){
			printf("打开文件%s\r\n",file_name);
			res = f_open(&file1,file_name,FA_CREATE_ALWAYS | FA_WRITE | FA_READ);
			if(res != FR_OK){
				printf("文件打开失败\r\n");
				while(1)
				{
					delay_ms(500);
					LED_R=!LED_R;
				}
			}
			break;
		}
	}
	printf("SD开始写入\r\n");
	for(i=0,number=1300000;number>0;number--,i++){
		temp = *(float*)(Bank5_SDRAM_ADDR+4*i+40000);  //SDRAM中前面10000个不要
		res=f_write(&file1,&temp,4,&fnum);
		if(i % 10000 == 0)	printf("%d\r\n",i);			//显示SD卡写入进度
	}
	memset((float*)Bank5_SDRAM_ADDR,0,Data_volume);
	i = 0;
	printf("SD写入结束\r\n");
	f_close(&file1);
}

/*******************************************************************************************************/

void open_file(void)
{

			res = f_open(&file1,"ttt.bin",FA_CREATE_ALWAYS | FA_WRITE | FA_READ);
			if(res != FR_OK){
				printf("文件打开失败\r\n");
				while(1)
				{
					delay_ms(500);
					LED_R=!LED_R;
				}
			}
}



