/*********************************************************
//file		:hd_dev_gpio.c
//author	:libo
//date		:2020/05/10
//version	:V1.0
//brief		:GSP HARD层GPIO接口C文件
*********************************************************/
/* Includes-----------------------------------------------------------------------------------*/
//#define FUNC_SPI_W25QXX_USE
#include <string.h>
#include <stdbool.h>
#include "cmsis_os.h"
#include "func_spi_w25qxx.h"
#include "obj_hal_w25qxx.h"
#include "obj_soft_w25qxx.h"

#define SPI_FLASH_SECT_BOOT 		0	

#ifdef FUNC_SPI_W25QXX_USE
/* Includes-----------------------------------------------------------------------------------*/
#include <string.h>
#include "dev_spi_sdcard.h"
#include "obj_spi_w25qxx.h"


/* Private macro------------------------------------------------------------------------------*/
#define domain_add     0x000000
#define phone_add      0x001000
/* Private typedef----------------------------------------------------------------------------*/
/* Private define-----------------------------------------------------------------------------*/
/* Private variables--------------------------------------------------------------------------*/
/* Private function prototypes----------------------------------------------------------------*/
uint8_t func_w25q_disk_ioctl (
	BYTE pdrv,		/* Physical drive nmuber (0..) */
	BYTE cmd,			/* Control code */
	void *buff		/* Buffer to send/receive control data */
)
{
	uint8_t res = RES_PARERR;

	switch(cmd)
	{
		case CTRL_SYNC:
				res = RES_OK; 
				break;	 
		case GET_SECTOR_SIZE:
				*(WORD*)buff = w25qxx.SectorSize;
				res = RES_OK;
				break;	 
		case GET_BLOCK_SIZE:
				*(WORD*)buff = w25qxx.BlockSize;
				res = RES_OK;
				break;	 
		case GET_SECTOR_COUNT:
				*(DWORD*)buff = w25qxx.SectorCount;
				res = RES_OK;
				break;
		default:
				res = RES_PARERR;
				break;
	}
		
	return res;
}

void func_w25q_init(void)
{	

	W25qxx_Cs_Init();		
	W25qxx_Spi_Init();	

  W25qxx_Init();
	
	while(w25qxx.ID != ID_W25Q128)			//检测不到ID_W25Q128
	{
		W25qxx_Init();
		HAL_Delay(100);
	}
	return;
}


void func_w25q_spi_flash_bootcnt(void)
{	
	uint8_t 	key;
	uint32_t  boot_cnt = 0;
	uint8_t write_buff[] = "6686666666";
	uint8_t read_buff[20] = {0};
	
	UNUSED(key);
	UNUSED(write_buff);
	UNUSED(read_buff);
	
	func_w25q_init();
	
	while(w25qxx.ID != ID_W25Q128)			//检测不到W25Q256
	{
		W25qxx_Init();
		HAL_Delay(100);
	}
	while(1)
	{				
			W25qxx_ReadSector((uint8_t *)&boot_cnt,phone_add,0,sizeof(boot_cnt));
			boot_cnt++;
			W25qxx_EraseSector(phone_add);
		  W25qxx_WriteSector((uint8_t *)&boot_cnt,phone_add,0,sizeof(boot_cnt));
//		 if(key)
//		 {
//				HAL_Delay(100);
//				W25qxx_WritePage(phone_buff,phone_add,0,strlen((char*)phone_buff));
//		 }
//		 else
//		 {
//				HAL_Delay(100);
//				W25qxx_ReadPage(read_buff,phone_add,0,20);
//		 }	
				
			osDelay(1000);			
	}

}

void func_w25q_spi_flash_buffer(void)
{	
	uint8_t 	key;

	uint8_t phone_buff[] = "6686666666";
	uint8_t read_buff[20] = {0};
	

	func_w25q_init();
	
	while(w25qxx.ID != ID_W25Q128)			//检测不到W25Q256
	{
		W25qxx_Init();
		HAL_Delay(100);
	}
	while(1)
	{				

		 if(key)
		 {
				HAL_Delay(100);
				W25qxx_WritePage(phone_buff,phone_add,0,strlen((char*)phone_buff));
		 }
		 else
		 {
				HAL_Delay(100);
				W25qxx_ReadPage(read_buff,phone_add,0,20);
		 }	
				
			osDelay(1000);			
	}

}

#endif    /*************FUNC_SPI_W25QXX_USE*******************/

#if  defined(USE_OBJ_HAL_W25QXX)  || defined(USE_OBJ_SOFT_W25QXX)
//要写入到W25Q16的字符串数组
const uint8_t TEXT_Buffer[]={"STM32 SPI TEST"};
#define SIZE sizeof(TEXT_Buffer)	 
	
void func_w25q_id_check(void)
{
	uint32_t W25Q_ReadID = 0xffffffff;
	while((W25Q_ReadID <= W25Q512)
			&&(W25Q_ReadID >= W25Q32))		//检测不到W25Q128
	{

		W25Q_ReadID = W25QXX_ReadID();
		HAL_Delay(100);
	}	
}

void func_w25q_init(void)
{	

	W25QXX_Init();				    					//W25QXX初始化
	
	W25QXX_WAKEUP();
	
	HAL_Delay(100);
	
	uint8_t erase_flag = 0;

	if(erase_flag)
	{
		W25QXX_Erase_Chip();
	}

	return;
}


#if 0

uint8_t func_w25q_disk_ioctl (
	BYTE pdrv,		/* Physical drive nmuber (0..) */
	BYTE cmd,			/* Control code */
	void *buff		/* Buffer to send/receive control data */
)
{
	uint8_t res = RES_PARERR;

	switch(cmd)
	{
		case CTRL_SYNC:
				res = RES_OK; 
				break;	 
		case GET_SECTOR_SIZE:
				*(WORD*)buff = SPI_FLASH_SECTOR_SIZE;
				res = RES_OK;
				break;	 
		case GET_BLOCK_SIZE:
				*(WORD*)buff = SPI_FLASH_BLOCK_SIZE;
				res = RES_OK;
				break;	 
		case GET_SECTOR_COUNT:
				*(DWORD*)buff = SPI_FLASH_SECTOR_COUNT;
				res = RES_OK;
				break;
		default:
				res = RES_PARERR;
				break;
	}
		
	return res;
}

#endif

uint32_t boot_cnt[2] = {0};	

uint8_t func_w25q_bootcnt(void)
{	
		uint32_t falsh_addr = 0;	
		uint8_t ret = false;	
		falsh_addr = SPI_FLASH_SECT_BOOT * w25qxx_obj.SectorSize;	
		W25QXX_Read((uint8_t*)&boot_cnt[0], falsh_addr, sizeof(uint32_t));
	
		boot_cnt[0]++;	
		W25QXX_Write((uint8_t*)&boot_cnt[0], falsh_addr, sizeof(uint32_t));
	
		W25QXX_Read((uint8_t*)&boot_cnt[1], falsh_addr, sizeof(uint32_t));
	
		if(boot_cnt[0] == boot_cnt[1])
    {
			ret = true;
		}
		
		return ret;		
}


uint32_t FLASH_ReadID = 0xffffffff;
int func_w25q_hal_main(void)
{
		//uint8_t key;
		uint8_t idx=0;

		uint32_t FLASH_SIZE;	

		uint8_t TXT_WR[64] = {0};
		uint8_t TXT_RD[64] = {0};

		W25QXX_Init();				    					//W25QXX初始化
		HAL_Delay(100);
	
		FLASH_SIZE=1*1024*1024;	//FLASH 大小为32M字节
		W25QXX_Read(&idx,FLASH_SIZE-100,sizeof(idx));
		while(1)
		{
				FLASH_ReadID = W25QXX_ReadID();
				idx++;
				memset((void *)TXT_WR,idx,sizeof(TXT_WR));
				W25QXX_Write((uint8_t*)TXT_WR,FLASH_SIZE-100,sizeof(TXT_WR));		//从倒数第100个地址处开始,写入SIZE长度的数据
				osDelay(100);
				memset((void *)TXT_RD,0x00,sizeof(TXT_RD));
				W25QXX_Read(TXT_RD,FLASH_SIZE-100,sizeof(TXT_RD));					//从倒数第100个地址处开始,读出SIZE个字节		
				
				if(memcmp(TXT_RD,TXT_WR,sizeof(TXT_RD)) != 0 )
				{
					osDelay(2000);
					memset((void *)TXT_RD,0x00,sizeof(TXT_RD));
					W25QXX_Read(TXT_RD,FLASH_SIZE-100,sizeof(TXT_RD));					//从倒数第100个地址处开始,读出SIZE个字节		
				
				}
				osDelay(1000);
		}		    
}
#endif


uint16_t  func_w25q_stat(void)
{
	return W25QXX_ID_OK();
}

void func_w25q_erase_ship(void)
{
	 W25QXX_Erase_Chip();
}
void func_w25q_test(void)
{
	uint8_t erase_ship = 0;
	if(erase_ship)
	{
		func_w25q_erase_ship();
	}	
	
#if 0
		while(1)
		{
			//add boly 20221107  W25QXX件初始化
			//func_w25q_spi_flash_bootcnt();
			//func_w25q_spi_flash_buffer();
			func_w25q_hal_main();
			osDelay(1000);
			
			//end boly
		}
#endif	
}




