/*
 * bsp_Dflash.c
 *
 *  Created on: Aug 7, 2014
 *      Author: QTC
 */
 
#define  GLOBALS_BSP_IIC
#include "../App_Headers/includes.h"


/*-------------------------------------------------------------------------
* Function Name  : bsp_IIC_Start
* Description    : 
* Input          : None
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
void bsp_IIC_Start(void)
{
	SDADIR = 1;
	asm NOP;
	asm NOP;
	asm NOP;
  asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;	
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;	
	asm NOP;
	asm NOP;			
	SDA    = HIGH;									
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;	
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;	
	asm NOP;
	asm NOP;			
	SCL = HIGH;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;	
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;	
	asm NOP;
	asm NOP;			
	SDA   = LOW;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;	
	asm NOP;
	asm NOP;				
	SCL   = LOW;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;	
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;	
	asm NOP;
	asm NOP;			
	bsp_IIC_delay();
	bsp_IIC_delay();     
}

/*-------------------------------------------------------------------------
* Function Name  : bsp_IIC_Stop
* Description    : stop
* Input          : None
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
void bsp_IIC_Stop(void)
{
	SDADIR = 1;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;	
	asm NOP;
	asm NOP;			
	SCL = LOW;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;	
	asm NOP;
	asm NOP;		
	SDA = LOW;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;		
	SCL = HIGH;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;		
	SDA = HIGH;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;	
	asm NOP;
	asm NOP;			
	bsp_IIC_delay();
	bsp_IIC_delay(); 
}

/*-------------------------------------------------------------------------
* Function Name  : bsp_delay
* Description    : ΡΣΚ±
* Input          : None
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
void bsp_IIC_delay(void)
{
    INT8U i= 0;
  	  
    while(i < 210)
    {
	     i++;
	     _FEED_COP(); 
    }
}

/*-------------------------------------------------------------------------
* Function Name  : bsp_IIC_ACK
* Description    : ack
* Input          : None
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
void bsp_IIC_ACK(void)
{
	SDADIR = 1;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;	
	asm NOP;
	asm NOP;				
	SDA = LOW;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;	
	asm NOP;
	asm NOP;			
	SCL = HIGH;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;	
	asm NOP;
	asm NOP;				
	SCL = LOW;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;	
	asm NOP;
	asm NOP;		
	SDA = HIGH;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;			
}
/*-------------------------------------------------------------------------
* Function Name  : bsp_IIC_NACK
* Description    : nack
* Input          : None
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
void bsp_IIC_NACK(void)
{
	SDADIR = 1; 
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;	
	asm NOP;
	asm NOP;
  asm NOP;
  asm NOP;
 	asm NOP;	
 	asm NOP;
 	asm NOP;				
	SDA = HIGH;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;	
	asm NOP;
	asm NOP;			
	SCL = HIGH;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;	
	asm NOP;
	asm NOP;		
	SCL = LOW;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;	
	asm NOP;
	asm NOP;				
}
/*-------------------------------------------------------------------------
* Function Name  : bsp_IIC_WaitAck
* Description    : ack
* Input          : None
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
INT8U bsp_IIC_WaitAck(void)
{
    INT8U  ack;
    INT16U i = IIC_WaitOverTime; 
	
    SDADIR = 1; 
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;  	
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;	
  	asm NOP;
  	asm NOP;  	
    SDA = HIGH;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;	
  	asm NOP;
  	asm NOP;  	
    SCL = HIGH;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;	
  	asm NOP;
  	asm NOP; 
  	asm NOP;
  	asm NOP;
  	asm NOP;	
  	asm NOP;
  	asm NOP;  	 	
    SDADIR = 0;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;	
  	asm NOP;
  	asm NOP;    
    while((ack) && (--i != 0 ))
    {
    	ack = SDA;	
    	_FEED_COP(); 
    }
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;   
    SCL = LOW;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;
  	asm NOP;	
  	asm NOP;
  	asm NOP;  	  	
    return(ack);	
}


/*-------------------------------------------------------------------------
* Function Name  : bsp_IIC_SendByte
* Description    : 
* Input          : None
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
INT8U bsp_IIC_SendByte(INT8U Data)   
{ 
	INT8U temp;
	INT8U d = Data;
	SDADIR = 1; 
  asm NOP;
  asm NOP;
 	asm NOP;
 	asm NOP;
  asm NOP;
  asm NOP;
  asm NOP;
  asm NOP;
 	asm NOP;

	for(temp = 8;temp != 0;temp--)		
	{
	    if(d & 0x80)	
	    {
      		SDA = HIGH;		
	    }
	    else
	    {
  		    SDA = LOW;					
	    }
      asm NOP;
      asm NOP;
      asm NOP;
     	asm NOP;
      asm NOP;
      asm NOP;
      asm NOP;
      asm NOP;
      asm NOP;		
    	asm NOP;
    	asm NOP;
    	asm NOP;
    	asm NOP;
    	asm NOP;
    	asm NOP;
    	asm NOP;
    	asm NOP;	
    	asm NOP;
    	asm NOP;
    	asm NOP;
    	asm NOP;
    	asm NOP;
    	asm NOP;
    	asm NOP;	
    	asm NOP;
    	asm NOP;	    		    
	    SCL = HIGH;
      asm NOP;
      asm NOP;
      asm NOP;
     	asm NOP;
      asm NOP;
      asm NOP;
      asm NOP;
      asm NOP;
      asm NOP;
    	asm NOP;
    	asm NOP;
    	asm NOP;
    	asm NOP;
    	asm NOP;
    	asm NOP;
    	asm NOP;
    	asm NOP;	
    	asm NOP;
    	asm NOP;
    	asm NOP;
    	asm NOP;
    	asm NOP;
    	asm NOP;	
    	asm NOP;
    	asm NOP;    	    	      		    
	    SCL = LOW;
      asm NOP;
      asm NOP;
      asm NOP;
     	asm NOP;
      asm NOP;
      asm NOP;
      asm NOP;
      asm NOP;
      asm NOP;
    	asm NOP;
    	asm NOP;
    	asm NOP;
    	asm NOP;
    	asm NOP;
    	asm NOP;
    	asm NOP;
    	asm NOP;	
    	asm NOP;
    	asm NOP;    	      		    
	    d = d <<1;	    
	}
  asm NOP;
  asm NOP;
  asm NOP;
  asm NOP;
  asm NOP;
  asm NOP;
  asm NOP;
  asm NOP;
  asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;
	asm NOP;	
	asm NOP;
	asm NOP;  			
	return 1;
}
/*-------------------------------------------------------------------------
* Function Name  : bsp_IIC_ReadByte
* Description    : 
* Input          : None
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
INT8U bsp_IIC_ReadByte(void)   
{ 
	INT8U temp;
	INT8U rbyte = 0;
	SDADIR = 0; 
  asm NOP;
  asm NOP;
 	asm NOP;
 	asm NOP;
  asm NOP;
  asm NOP;
  asm NOP;
  asm NOP;
 	asm NOP;
	for(temp = 8; temp != 0;temp--)				
	{
	    SCL = HIGH;
      asm NOP;
      asm NOP;
     	asm NOP;
     	asm NOP;
      asm NOP;
      asm NOP;
      asm NOP;
      asm NOP;
     	asm NOP;
    	asm NOP;
    	asm NOP;
    	asm NOP;
    	asm NOP;
    	asm NOP;	
    	asm NOP;
    	asm NOP;     		    
	    rbyte = rbyte << 1;						
      asm NOP;
      asm NOP;
     	asm NOP;
     	asm NOP;
      asm NOP;
      asm NOP;
      asm NOP;
      asm NOP;
     	asm NOP;
    	asm NOP;
    	asm NOP;
    	asm NOP;
    	asm NOP;
    	asm NOP;	
    	asm NOP;
    	asm NOP;     		    
	    rbyte = rbyte | ((INT8U)(SDA));	
      asm NOP;
      asm NOP;
     	asm NOP;
     	asm NOP;
      asm NOP;
      asm NOP;
      asm NOP;
      asm NOP;
     	asm NOP;
    	asm NOP;
    	asm NOP;
    	asm NOP;
    	asm NOP;
    	asm NOP;	
    	asm NOP;
    	asm NOP;     		    
	    SCL = LOW;
      asm NOP;
      asm NOP;
     	asm NOP;
     	asm NOP;
      asm NOP;
      asm NOP;
      asm NOP;
      asm NOP;
     	asm NOP;
    	asm NOP;
    	asm NOP;
    	asm NOP;
    	asm NOP;
    	asm NOP;	
    	asm NOP;
    	asm NOP;     	
	}
	return(rbyte);							
}
/*-------------------------------------------------------------------------
* Function Name  : bsp_IIC_WriteByte
* Description    : 
* Input          : None
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
INT8U bsp_IIC_WritePage(INT8U  PhyAdd,INT8U  MemAdd, INT8U *Data,INT8U len)   
{
    INT8U  b = 3;
    INT8U  i;
    INT8U  *OldData;
    OldData = Data;

IIC_Start: 
    Data = OldData;
    bsp_IIC_Start();		
    bsp_IIC_SendByte(PhyAdd);		
    if(bsp_IIC_WaitAck())          
    {
	      goto IIC_Err;
    }
    bsp_IIC_SendByte(MemAdd);				
    if(bsp_IIC_WaitAck())               
    {
	      goto IIC_Err;
    }
    for(i = 0 ;i < len ; i++)			
    {
        bsp_IIC_SendByte(*Data);				
	      if(bsp_IIC_WaitAck())               
      	{
            goto IIC_Err;
      	}
	      Data++;					
    }
    bsp_IIC_Stop();				
    bsp_IIC_delay();
    return RunComplete;
IIC_Err:   
    bsp_IIC_Stop(); 
    b--;
    if(b != 0)
    {
        _FEED_COP(); 
        goto IIC_Start; 
    }
    else
    {
        return(RunErr);
    } 	
}

/*-------------------------------------------------------------------------
* Function Name  : bsp_IIC_ReadPage
* Description    : 
* Input          : None
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
INT8U bsp_IIC_ReadPage(INT8U  PhyAdd,INT8U  MemAdd, INT8U *Data,INT8U len)   
{     
    INT8U  b = 3;
    INT8U  i;
    
IIC_Start:  	
    bsp_IIC_Start();							
    bsp_IIC_SendByte(PhyAdd);			
    if(bsp_IIC_WaitAck())             
    {
        goto IIC_Err;
    }
    bsp_IIC_SendByte(MemAdd);				
    if(bsp_IIC_WaitAck())               
    {
        goto IIC_Err;
    }
    bsp_IIC_Start();								
    bsp_IIC_SendByte(PhyAdd | 0x01);			
    if(bsp_IIC_WaitAck())              
    {
        goto IIC_Err;
    }
    for(i = 0 ;i < (len -1) ; i++)					
    {
        *Data=bsp_IIC_ReadByte();			
        bsp_IIC_ACK();				
        Data++;					
    }
    *Data = bsp_IIC_ReadByte();		
    bsp_IIC_NACK();						
    bsp_IIC_Stop();							
    return RunComplete;
IIC_Err:   
    bsp_IIC_Stop(); 
    b--;
    if(b != 0)
    {
        _FEED_COP(); 
        goto IIC_Start; 
    }
    else
    {
        return(RunErr);
    } 		
}
