#include "sys.h"
#include "string.h"

#include "delay.h"
#include "usart.h"
#include "led.h"

#include "w25qxx.h"
#include "sdram.h"
#include "malloc.h"

#include "ff.h"
#include "exfuns.h"
#include "sdio_sdcard.h"

#include "usmart.h"

#include "cnn_model.h" 
#include "debug.h"
#include "cnn.h"
#include "tools.h"



void run_dataset(){
	char* modelusearr[] = {
		"C1autosave00095_right_new_2",
		"C1autosave00096_right_new_2",
		"C1autosave00097_right_new_2",
		"C1autosave00098_right_new_2",
		"C1autosave00099_right_new_2",
		"C1autosave00100_right_new_2",
		"C1autosave00101_right_new_2",
		"C1autosave00102_right_new_2",
		"C1autosave00103_right_new_2",
		"C1autosave00104_right_new_2",
		"C1autosave00105_right_new_2",
		"C1autosave00106_right_new_2",
		"C1autosave00107_right_new_2",
		"C1autosave00108_right_new_2",
		"C1autosave00109_right_new_2",
		"C1autosave00110_right_new_2",
		"C1autosave00111_right_new_2",
		"C1autosave00112_right_new_2",
		"C1autosave00113_right_new_2",
		"C1autosave00114_right_new_2",
		"C1autosave00115_right_new_2",
		"C1autosave00116_right_new_2",
		"C1autosave00117_right_new_2",
		"C1autosave00118_right_new_2",
		"C1autosave00119_right_new_2",
		"C1autosave00120_right_new_2",
		"C1autosave00121_right_new_2",
		"C1autosave00122_right_new_2",
		"C1autosave00123_right_new_2",
		"C1autosave00124_right_new_2",

		"filtered_C1autosave00011_right_new",
		"filtered_C1autosave00015_right_new",
		"filtered_C1autosave00043_right_new",
		"filtered_C1autosave00067_right_new",
		"filtered_C1autosave00090_right_new",
		"filtered_C1autosave00106_right_new",
		"filtered_C1autosave00118_right_new",

		"filtered_C1autosave00007_right_new",
		"filtered_C1autosave00035_right_new",
		"filtered_C1autosave00036_right_new",
		"filtered_C1autosave00040_right_new",
		"filtered_C1autosave00053_right_new",
		"filtered_C1autosave00061_right_new",
		"filtered_C1autosave00074_right_new",
		"filtered_C1autosave00077_right_new",
		"filtered_C1autosave00080_right_new",
		"filtered_C1autosave00085_right_new",
		"filtered_C1autosave00098_right_new",
		"filtered_C1autosave00100_right_new",
		"filtered_C1autosave00104_right_new",
		"filtered_C1autosave00122_right_new",
		"filtered_C1autosave00124_right_new",

		"filtered_C1autosave00108_right_new",

		"filtered_C1autosave00004_right_new",
		"filtered_C1autosave00039_right_new",
		"filtered_C1autosave00062_right_new",
	};
	for(int a=0;a<(sizeof(modelusearr) / sizeof(modelusearr[0]));a++){
		SDRAM_USED();
		model_switchdata(modelusearr[a]);
		cnn_run();
	}
	printf("\r\n运行完成\r\n");
}



int main(void)
{
 	u32 total,free;

    HAL_Init();
    Stm32_Clock_Init(360,25,2,8);
    delay_init(180);
    uart_init(9600);
	
    SDRAM_Init();
    LED_Init();
	my_mem_init(SRAMEX);

	model_init();
	TIM3_Init(5000-1,9000-1);					//STM32状态灯所使用的定时器
//////////////////////////////////////////////////调试工具usmart的初始化///////////////
    usmart_dev.init(90);
///////////////////////////////////////////////////////////////////////////////////// 
	u8 _res_sd = SD_Init();
 	if(_res_sd)printf("SD Card Error!\r\n");
	while(_res_sd)//检测不到SD卡
	{
		delay_ms(500);
		LED0=!LED0;
	}
 	exfuns_init();								//为fatfs相关变量申请内存
  	f_mount(fs[0],"0:",1); 						//挂载SD卡
	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);
		LED0=!LED0;
	}
	
	model_write("all");
	printf("初始化完成！\r\n");
	run_dataset();
	SDRAM_USED();

	while(1){
		if(isrun)cnn_run();
	}
}
