⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spi_simulate.c

📁 这是ARM在Vxworks的驱动源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
      ++state;
      buf[i] = rcvd;
      if (i>=COMMAND_HEAD_LEN)
      	pbuf[i-byte-COMMAND_HEAD_LEN] = buf[i];
    }
  }
  
  DISABLE_SPISEL(chip);  
  
  return BCM_OK;
}
#endif	
	
/****************************************************************/
/*																*/
/* FUNCTION NAME: setup_page_address   							*/
/*																*/
/* INPUTS		: chip - the chip id		                	*/
/*				: page - page address							*/      
/*																*/
/* RETURN		: void									 		*/
/*																*/
/* NOTE			: setup page address. 							*/
/*				: turn to the page which is to be read or written    */
/*				: if current page is not the exact page			*/
/*																*/
/****************************************************************/
static void setup_page_address(int chip, UINT8 page)
{
	static UINT8 curPage[4]={0xff,0xff,0xff,0xff}; /*could not be 0. init is 0*/
//	Print("chip:0x%x, Page:0x%x\n", chip, page);
//	Print("curPage:0x%x, 0x%x,0x%x, 0x%x\n",
//		curPage[0],curPage[1],curPage[2],curPage[3]);
	/*if current page,no act.*/
//	if(curPage[chip] == (UINT8)page)
//		return ;
	
	curPage[chip] = (UINT8)page;
	
	write_page_address(chip, page);
}
/****************************************************************/
/*																*/
/* FUNCTION NAME: write_page_address   							*/
/*																*/
/* INPUTS		: chip - the chip id		                	*/
/*				: page - page address							*/      
/*																*/
/* RETURN		: void									 		*/
/*																*/
/* NOTE			: write page address. 							*/
/*				: write to the page which is to be read or written    */
/*				: even if current page is  the exact page   	*/
/*																*/
/****************************************************************/
static void write_page_address(int chip, UINT8 page)
{	
	txbuf[0] = NORMAL_SPI_CMD | SPI_WRITE |SPI_CHIP(chip);	/* cmd */
	txbuf[1] = PAGE_REG;	                                /* page address */
	txbuf[2] = page;
	ENABLE_SPISEL(chip);
	spi_simulate_send_bytes(txbuf, COMMAND_HEAD_LEN+1);	
	DISABLE_SPISEL(chip);
}
/****************************************************************/
/*																*/
/* FUNCTION NAME: setup_reg_address   							*/
/*																*/
/* INPUTS		: chip - the chip id		                	*/
/*				: page - page address							*/      
/*																*/
/* RETURN		: register value						 		*/
/*																*/
/* NOTE			: setup reg address. 							*/
/*				: turn to the reg which is to be read or written     */
/*				: and get current value in the reg address		*/
/*																*/
/****************************************************************/
static void setup_reg_address(int chip, UINT8 reg)
{
	
	txbuf[0] = NORMAL_SPI_CMD | SPI_READ |SPI_CHIP(chip);	/* cmd */
	txbuf[1] = reg;	                                        /* reg address */
	txbuf[2] = 0;
	ENABLE_SPISEL(chip);
	spi_simulate_send_bytes(txbuf, COMMAND_HEAD_LEN+1);	
	DISABLE_SPISEL(chip);
}

/****************************************************************/
/*																*/
/* FUNCTION NAME: read_spi_status    							*/
/*																*/
/* INPUTS		: chip - the chip id		                	*/
/*				: 												*/      
/*																*/
/* RETURN		: spi status							 		*/
/*																*/
/* NOTE			: read the spi status register 					*/				
/*																*/
/****************************************************************/
static UINT8 read_spi_status(int chip)
{
	
	txbuf[0] = NORMAL_SPI_CMD | SPI_READ |SPI_CHIP(chip);
	txbuf[1] = STAT_REG;
	ENABLE_SPISEL(chip);
	spi_simulate_send_bytes(txbuf, COMMAND_HEAD_LEN);
	rxbuf = 0x00;
	spi_simulate_recv_bytes(&rxbuf, 1);
	DISABLE_SPISEL(chip);
	
	return rxbuf;
}

/****************************************************************/
/*																*/
/* FUNCTION NAME: spi_simulate_read_bytes_cont					*/
/*																*/
/* INPUTS		: pbuf - pointer to wherr to store data             */
/*				: len  - data length							*/      
/*																*/
/* RETURN		: void	 								 		*/
/*																*/
/* NOTE			: read the spi data register 					*/
/*              : store the data in pbuf[0:reg_len-1]                */
/*																*/
/****************************************************************/

static void spi_simulate_read_bytes_cont(int chip, char *pbuf, int reg_len)
{
	/*receive the data from date register 0xf0 */
	txbuf[0] = NORMAL_SPI_CMD | SPI_READ | SPI_CHIP(chip);	/* cmd */
	txbuf[1] = DATA_REG;	                                /* page address */	
	
	ENABLE_SPISEL(chip);
	spi_simulate_send_bytes(txbuf, COMMAND_HEAD_LEN);
	spi_simulate_recv_bytes(pbuf, reg_len);	
	DISABLE_SPISEL(chip);
}


/****************************************************************/
/*																*/
/* FUNCTION NAME: spi_simulate_send_bytes						*/
/*																*/
/* INPUTS		: nbyte - number of data byte to be sent		*/
/*              : buf -  base address of transmit buffer             */
/*																*/
/* RETURN		: the status of broadcom SPI			 		*/
/*																*/
/* NOTE			: send a frame to the SPI slave                   */
/*              : The frame is buf[0 : nbyte-1]                    */
/*																*/
/****************************************************************/

static int spi_simulate_send_bytes(UINT8 *buf, UINT8 nbyte )
{
  UINT8 send;
  UINT8 mask; 
  UINT8 i=0;   
 

  for(i=0; i<nbyte; ++i)
  {
    send = buf[i];      
    SPI_SIM_LOG("sending byte:0x%x\n", send);
    for( mask=0x80; mask!=0; mask=mask>>1 )
    {
      LOW_SPICLK; 
      spi_delay();
      if( mask & send )
      {
      	HIGH_SPIMOSI;
      }
      else
      {
      	LOW_SPIMOSI;      
      }
      spi_delay();
      HIGH_SPICLK; 
      spi_delay();
      spi_delay();
    } 
    spi_delay();
    spi_delay();
   /* SPI_SIM_LOG("send = 0x%2x\n", send);*/
  }  
 
  return BCM_OK;
}

/****************************************************************/
/*																*/
/* FUNCTION NAME: spi_simulate_recv_bytes						*/
/*																*/
/* INPUTS		: nbyte - number of data byte to be sent		*/
/*              : buf -  base address of receive buffer             */
/*																*/
/* RETURN		: the status of broadcom SPI			 		*/
/*																*/
/* NOTE			: receive a frame from the SPI slave               */
/*              : The frame is saved in buf[0 : nbyte-1]           */
/*																*/
/****************************************************************/
static int  spi_simulate_recv_bytes( UINT8 *buf, UINT8 nbyte )
{
  UINT8 recv;
  UINT8 mask;
  UINT8 i = 0;   
 
  for(i=0; i < nbyte; ++i)
  {     
      for( mask=0x80, recv=0; mask!=0; mask=mask>>1 )  
      {
        LOW_SPICLK;        
		spi_delay();		
		spi_delay();
		HIGH_SPICLK; 
        spi_delay();       
        if( IS_HIGH_SPIMISO )
        {
        	recv |= mask;        	
        } 
		spi_delay();
      }  
	 spi_delay();
	 spi_delay();
      buf[i] = recv; 
      SPI_SIM_LOG("recv = 0x%2x\n", recv);
  }  
 
  return BCM_OK;
}

/****************************************************************/
/*																*/
/* FUNCTION NAME: spi_delay										*/
/*																*/
/* INPUTS		: void											*/
/*																*/
/* RETURN		: void            						 		*/
/*																*/
/* NOTE			: delay 5us to keep the clock 	                */
/*              : then the switch chip will recognise clock   	*/
/*																*/
/****************************************************************/
#if 1
static void spi_delay(void)
{

 	/*SPI_SIM_LOG(" SPI_FLAG = 0x%4x\n", SPI_FLAG);*/
	int i=1000;
	while(i--);

}
#else
static void spi_delay(void)
{	    
     UINT delay = 5;                 /* length of time in uS to delay */   
     
    register UINT oldval;               /* decrementer value */
    register UINT newval;               /* decrementer value */
    register UINT totalDelta;           /* Dec. delta for entire delay period */
    register UINT decElapsed;           /* cumulative decrementer ticks */

    /*
     * Calculate delta of decrementer ticks for desired elapsed time.
     * The macro DEC_CLOCK_FREQ MUST REFLECT THE PROPER 6xx BUS SPEED.
     */

    totalDelta = ((DEC_CLOCK_FREQ / 4) / 1000000) * delay;

    /*
     * Now keep grabbing decrementer value and incrementing "decElapsed" until
     * we hit the desired delay value.  Compensate for the fact that we may
     * read the decrementer at 0xffffffff before the interrupt service
     * routine has a chance to set in the rollover value.
     */

    decElapsed = 0;

    oldval = vxDecGet ();

    while (decElapsed < totalDelta)
    {
        newval = vxDecGet ();

        if ( DELTA(oldval,newval) < 1000 )
            decElapsed += DELTA(oldval,newval);  /* no rollover */
        else
            if (newval > oldval)
                decElapsed += abs((int)oldval);  /* rollover */

        oldval = newval;
   }
        
}

#endif

#ifdef SPI_SIM_DEBUG
void spi_simulate_test(ULONG flag)
{

	SPI_SIM_LOG("old SPI_FLAG = 0x%8x\n", SPI_FLAG);
	SPI_FLAG = flag;
	SPI_SIM_LOG("new  SPI_FLAG = 0x%8x\n", SPI_FLAG);
	HIGH_SPICLK;
	SPI_SIM_LOG("high clk is 0x%x\n", SPI_FLAG);
	LOW_SPICLK;
	SPI_SIM_LOG("low clk is 0x%x\n", SPI_FLAG);
	DISABLE_SPISEL(0);
	SPI_SIM_LOG("high ss is 0x%x\n", SPI_FLAG);
	ENABLE_SPISEL(0);
	SPI_SIM_LOG("low ss is 0x%x\n", SPI_FLAG);
	HIGH_SPIMOSI;
	SPI_SIM_LOG("high mosi is 0x%x\n", SPI_FLAG);
	LOW_SPIMOSI;
	SPI_SIM_LOG("low mosi is 0x%x\n", SPI_FLAG);
	
	if (IS_HIGH_SPIMISO)
		SPI_SIM_LOG("out = 1\n", 1);
    
}

UINT8 read_spi_page(int chip)
{
	
	txbuf[0] = NORMAL_SPI_CMD | SPI_READ |SPI_CHIP(chip);
	txbuf[1] = PAGE_REG;
	ENABLE_SPISEL(chip);
	spi_simulate_send_bytes(txbuf, COMMAND_HEAD_LEN);
	rxbuf = 0x00;
	spi_simulate_recv_bytes(&rxbuf, 1);
	DISABLE_SPISEL(chip);
	
	return rxbuf;
}


#endif




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -