#define MAPP_FLASH_OTP_C
#include "app_data_setting.h"
#include "nvm_ioctl.h"

#define FLASH_OTP_BYTENUM                    	32
pthread_mutex_t opt_mutex = PTHREAD_MUTEX_INITIALIZER;

void  App_Flash_OTP_Write(void)
{
	UINT16 writecnt = 0;
	UINT16 readcnt = 0;
	INT32 ret = 0;
	unsigned char	readbuffer[FLASH_OTP_BYTENUM] = {0};
	unsigned char	writebuf[FLASH_OTP_BYTENUM] = {0};
	unsigned char	otpbuf[FLASH_OTP_BYTENUM] =
	{
		0x0f,0x0f,0x0f,0x0f,
		0x0f,0x0f,0x0f,0x0f,
		0x02,0x05,0x06,0xcc,
		0x4b,0xab,0xbc,0xcd,
		0xde,0xef,0xfa,0x00,
		0x00,0x00,0x00,0x00,
		0x0f,0x0f,0x0f,0x0f,
	}; 
	
	pthread_mutex_lock(&opt_mutex);        	
#if (CONFIG_CHIPID == 0x531 || CONFIG_CHIPID == 0x533)
	Cmd_Flash_Encrypt_Control(0);
#else
	Cmd_Flash_Encrypt_Control(1);
#endif
	writecnt = 0;
	writebuf[writecnt++] = 0xB1;//enter secured OTP
	Cmd_SPICmd_Lock();
	Cmd_SPICmdRW(writecnt, writebuf, readcnt, readbuffer);
	//Cmd_SPICmd_UnLock();
	writecnt = FLASH_OTP_BYTENUM;
	ret = Cmd_FlashWrite(0xBC000000, (UINT32)otpbuf, writecnt);//write buffer
	if(ret != 0)
	{
		printf("\n----------------------->write error!\n");
	}
	memset(writebuf, 0, FLASH_OTP_BYTENUM);   
	writecnt = 0;
	writebuf[writecnt++] = 0xC1;//exit secured OTP
	//Cmd_SPICmd_Lock();
	Cmd_SPICmdRW(writecnt, writebuf, readcnt, readbuffer);
	Cmd_SPICmd_UnLock();
#if (CONFIG_CHIPID == 0x531 || CONFIG_CHIPID == 0x533)
	Cmd_Flash_Encrypt_Control(1);
#else
	Cmd_Flash_Encrypt_Control(0);
#endif
	pthread_mutex_unlock(&opt_mutex);		
} 

unsigned char App_Flash_OTP_Check(void)
{
	UINT16 writecnt = 0;
	UINT16 readcnt;
	INT32 ret = 0;
	UINT8 i =0, bOTPcheck = 0; 
	unsigned char writebuf[FLASH_OTP_BYTENUM] = {0};
	unsigned char readbuffer[FLASH_OTP_BYTENUM] = {0};
	unsigned char otpbuffer[FLASH_OTP_BYTENUM] =	
	{
		0x0f,0x0f,0x0f,0x0f,
		0x0f,0x0f,0x0f,0x0f,
		0x02,0x05,0x06,0xcc,
		0x4b,0xab,0xbc,0xcd,
		0xde,0xef,0xfa,0x00,
		0x00,0x00,0x00,0x00,
		0x0f,0x0f,0x0f,0x0f,
	}; 
	
	readcnt = 0;
	writecnt = 0;
	writebuf[writecnt++] = 0xB1;//enter secured OTP
	
	pthread_mutex_lock(&opt_mutex);        	
#if (CONFIG_CHIPID == 0x531 || CONFIG_CHIPID == 0x533)
	Cmd_Flash_Encrypt_Control(0);
#else
	Cmd_Flash_Encrypt_Control(1);
#endif
	Cmd_SPICmd_Lock();
	Cmd_SPICmdRW(writecnt, writebuf, readcnt, readbuffer);  
	readcnt = FLASH_OTP_BYTENUM;
	memset(readbuffer, 0, FLASH_OTP_BYTENUM);	 
	ret = Cmd_FlashRead(0xBC000000, (UINT32)readbuffer, readcnt);
	if(ret != 0)
	{
		printf("\n----------------------->read error!\n");
	}

	for (i = 0 ; i < (FLASH_OTP_BYTENUM - 8) ; i++)        
	{     
		if(readbuffer[i] != otpbuffer[i])             
		{			
			bOTPcheck = 0;			
			break;        	
		}        
	}	 
	if(i == (FLASH_OTP_BYTENUM - 8))	 
	{	 	
		bOTPcheck = 1;	 
	}
	
	readcnt = 0;
	writecnt = 0;
	writebuf[writecnt++] = 0xC1;//exit secured OTP
	Cmd_SPICmdRW(writecnt, writebuf, readcnt, readbuffer);
	Cmd_SPICmd_UnLock();
#if (CONFIG_CHIPID == 0x531 || CONFIG_CHIPID == 0x533)
	Cmd_Flash_Encrypt_Control(1);
#else
	Cmd_Flash_Encrypt_Control(0);
#endif
	pthread_mutex_unlock(&opt_mutex);		
	return  bOTPcheck;
}

#undef MAPP_FLASH_OTP_C

