void ICACHE_FLASH_ATTR
debug_FlashBufReset()
{
	FlashDebugBufParam.StartAddr = FLASH_DEBUG_SV_ADDR*0x1000;
	FlashDebugBufParam.Size = FLASH_DEBUG_SV_SIZE;
	FlashDebugBufParam.InPos = FLASH_DEBUG_SV_ADDR*0x1000;
	FlashDebugBufParam.OutPos = FLASH_DEBUG_SV_ADDR*0x1000;
	debug_FlashParamSv();
}
//not used
void ICACHE_FLASH_ATTR
debug_FlashUpdateExpetInfo(uint16 len)
{
	if(debug_FlashAddrCheck() && (FlashDebugBufParam.OutPos+len <= FlashDebugBufParam.InPos)){
		FlashDebugBufParam.InPos-=len;
		debug_FlashParamSv();
	}else{
		//return false;
	}
}
//reset debug buf param
void ICACHE_FLASH_ATTR
debug_FlashBufReset(flashDebugBuf* flashDbgBuf,uint32 idx_sec)
{
    flashDbgBuf->StartAddr = FLASH_DEBUG_SV_ADDR*0x1000;
    flashDbgBuf->Size = FLASH_DEBUG_SV_SIZE;
    flashDbgBuf->InPos = FLASH_DEBUG_SV_ADDR*0x1000;
    flashDbgBuf->OutPos = FLASH_DEBUG_SV_ADDR*0x1000;
	flashDbgBuf->DebugVersion = MESH_TEST_VERSION;
	flashDbgBuf->CurrentSec = FLASH_DEBUG_SV_ADDR;
    debug_FlashParamSv(flashDbgBuf,idx_sec);
}
//set debug version
void ICACHE_FLASH_ATTR
debug_SetDebugVersion(int ver)
{
    if(debug_FlashParamCsumCheck(&FlashDebugBufParam) && debug_FlashAddrCheck(&FlashDebugBufParam)){
        //FlashDebugBufParam.DebugVersion = ver;
    }else{
        debug_FlashBufInit(&FlashDebugBufParam,Flash_DEBUG_INFO_ADDR);
    }
    FlashDebugBufParam.DebugVersion = ver;
    //debug_FlashParamSv();
    debug_FlashParamSv(&FlashDebugBufParam,Flash_DEBUG_INFO_ADDR);
}
void ICACHE_FLASH_ATTR
	debug_SetDebugVersion(int ver)
{
	if(debug_FlashParamCsumCheck() && debug_FlashAddrCheck()){
    	//FlashDebugBufParam.DebugVersion = ver;
	}else{
		debug_FlashBufInit();
	}

	FlashDebugBufParam.DebugVersion = ver;
	debug_FlashParamSv();
}
void ICACHE_FLASH_ATTR
	debug_FlashPrintParamReset()
{
    FlashPrintBufParam.StartAddr = FLASH_PRINT_INFO_ADDR_0*0x1000;
    FlashPrintBufParam.Size = 0x2000;
    FlashPrintBufParam.InPos = FLASH_PRINT_INFO_ADDR_0*0x1000;
    FlashPrintBufParam.OutPos = FLASH_PRINT_INFO_ADDR_0*0x1000;
	FlashPrintBufParam.DebugVersion = MESH_TEST_VERSION;
	FlashPrintBufParam.CurrentSec = FLASH_PRINT_INFO_ADDR_0;
	
	spi_flash_erase_sector(FlashPrintBufParam.CurrentSec);
	spi_flash_erase_sector(FlashPrintBufParam.CurrentSec+1);
	
    debug_FlashParamSv(&FlashPrintBufParam,FLASH_PRINT_INDEX_ADDR);
}
void ICACHE_FLASH_ATTR
debug_PrintToFlash(uint8* data, uint16 len)
{
	if(len%4 != 0){
		os_printf("4Bytes ...\r\n");
		return;
	}
	
    if(debug_FlashAddrCheck()){
		if(FlashDebugBufParam.InPos % 0x1000 ==0)
		    spi_flash_erase_sector(FlashDebugBufParam.InPos / 0x1000);
    }else{
		debug_FlashBufInit();
    }
	if(FlashDebugBufParam.InPos+len <  FlashDebugBufParam.StartAddr+FlashDebugBufParam.Size ){
		spi_flash_write(FlashDebugBufParam.InPos,(uint32 *)data,len);
		FlashDebugBufParam.InPos+=len;
		debug_FlashParamSv();
	}
}
//write debug info to flash
void ICACHE_FLASH_ATTR
debug_PrintToFlash(uint8* data, uint16 len,flashDebugBuf* flashDbgBuf,uint32 idx_sector)
{
    ESP_DBG("debug_PrintToFlash\r\n");
    if(len%4 != 0){
        ESP_DBG("4Bytes ...\r\n");
        return;
    }
    
    if(debug_FlashAddrCheck(flashDbgBuf)){
        //if( (flashDbgBuf->InPos == flashDbgBuf->StartAddr &&flashDbgBuf->InPos == flashDbgBuf->OutPos) || 
			if((flashDbgBuf->InPos+len)/0x1000 == flashDbgBuf->CurrentSec+1){
			#if 1
			flashDbgBuf->CurrentSec = (flashDbgBuf->InPos+len>=flashDbgBuf->StartAddr+flashDbgBuf->Size)?
				                              flashDbgBuf->StartAddr/0x1000:(flashDbgBuf->InPos+len)/0x1000 ;
            //ESP_DBG("out: %08x; flashDbgBuf->CurrentSec*0x1000: %08x \r\n",flashDbgBuf->OutPos,flashDbgBuf->CurrentSec*0x1000);
			if((flashDbgBuf->OutPos<flashDbgBuf->CurrentSec*0x1000+0x1000) && (flashDbgBuf->OutPos>=flashDbgBuf->CurrentSec*0x1000)){
                flashDbgBuf->OutPos = flashDbgBuf->CurrentSec*0x1000 + 0x1000;
				if(flashDbgBuf->OutPos>=flashDbgBuf->StartAddr+flashDbgBuf->Size) flashDbgBuf->OutPos-=flashDbgBuf->Size;
			}
			//ESP_DBG("after set: out: %08x \r\n",flashDbgBuf->OutPos);
			#else
			

			if(flashDbgBuf->InPos+len>flashDbgBuf->StartAddr+flashDbgBuf->Size){
                flashDbgBuf->CurrentSec = flashDbgBuf->StartAddr/0x1000;
                ESP_DBG("overflow,loop back\r\n");
				if(flashDbgBuf->OutPos<flashDbgBuf->CurrentSec*0x1000+0x1000){
                    flashDbgBuf->OutPos = flashDbgBuf->CurrentSec*0x1000 + 0x1000;
    			}
			}else{
                flashDbgBuf->CurrentSec = (flashDbgBuf->InPos+len)/0x1000;
			}
			#endif
			spi_flash_erase_sector(flashDbgBuf->CurrentSec);		
        }
    }else{
        //debug_FlashBufInit(flashDbgBuf,idx_sector);
        ESP_DBG("error1\r\n");
    }
	#if 0
    ESP_DBG("================================\r\n");
	ESP_DBG("test data: LEN: %d\r\n",len);
	ESP_DBG("%s \r\n",data);
	ESP_DBG("================================\r\n");
	#endif
	
    if(flashDbgBuf->InPos+len <  flashDbgBuf->StartAddr+flashDbgBuf->Size ){
		ESP_DBG("in: %08x ,len: %d \r\n",flashDbgBuf->InPos,len);
        spi_flash_write(flashDbgBuf->InPos,(uint32 *)data,len);
        flashDbgBuf->InPos+=len;
        //debug_FlashParamSv();
		//debug_FlashParamSv(flashDbgBuf,Flash_DEBUG_INFO_ADDR);
		debug_FlashParamSv(flashDbgBuf,idx_sector);
		ESP_DBG("1\r\n");
    }else{
    
        ESP_DBG("in: %08x ,len: %d \r\n",flashDbgBuf->InPos,len);
		
        int len_tmp = flashDbgBuf->StartAddr+flashDbgBuf->Size - flashDbgBuf->InPos;
	    int len_remain = len-len_tmp;
		spi_flash_write(flashDbgBuf->InPos,(uint32 *)data,len_tmp);
		spi_flash_write(flashDbgBuf->StartAddr,(uint32 *)(data+len_tmp),len_remain);
		flashDbgBuf->InPos = flashDbgBuf->StartAddr+len_remain;
		//debug_FlashParamSv(flashDbgBuf,Flash_DEBUG_INFO_ADDR);
		debug_FlashParamSv(flashDbgBuf,idx_sector);
		ESP_DBG("2\r\n");
    }
}