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

📄 zlg_avalon_spi.c

📁 SD卡的SPI驱动程序
💻 C
字号:
/****************************************Copyright (c)**************************************************
**                               Guangzou ZLG-MCU Development Co.,LTD.
**                                      Research centre
**                                 http://www.zlgmcu.com
**
**--------------File Info-------------------------------------------------------------------------------
** File name:       zlg_avalon_spi.c
** Latest modified Date:  2005-12-19
** Latest Version:        1.0
** Descriptions:          SPI operations are implemented here
**
**------------------------------------------------------------------------------------------------------
** Created by:      Jing.Zhang
** Created date:    2005-12-19
** Version:         1.0
** Descriptions:    The original version
**
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
********************************************************************************************************/
#define  __SPI_GLOBAL__
#include "alt_types.h"

#include "altera_avalon_spi_regs.h"
#include "zlg_avalon_spi.h"

/*********************************************************************************************************
** Function name:    zlg_avalon_spi_send_byte
**
** Descriptions:     Send a byte through SPI master core. ss signal should be asserted
**                   and de-asserted explicitly. this function is not currently safe if 
**                   called in a multi-threaded environment, something above must perform 
**                   locking to make it safe if more than one thread intends to use it.  
**
** input parameters:   write_length -- write_length number of data will be written out
**                     write_data   -- a pointer to write buffer
**            
** Returned value:     0, seccussful;
**                     ERR, errors occurs.
**         
** Used global variables: None
** Calling modules:       
**
** Created by:        Jing.Zhang
** Created Date:      2005/12/08
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
int zlg_avalon_spi_send_byte(alt_u32 write_length,const alt_u8* write_data)
{
  alt_u32 status;
  alt_u32 i;
  volatile alt_u32 read_data;
    
  for(i=0; i<write_length; i++)
  {   //Wait...until the txdata register is empty.
    do
    {
      status = IORD_ALTERA_AVALON_SPI_STATUS(SD_SPI_BASE);
    }
    while ((status & ALTERA_AVALON_SPI_STATUS_TRDY_MSK) == 0);
        //Send data
    IOWR_ALTERA_AVALON_SPI_TXDATA(SD_SPI_BASE, *write_data++);
        read_data = IORD_ALTERA_AVALON_SPI_RXDATA(SD_SPI_BASE);

    // Wait until the interface has finished transmitting
    do
    {
      status = IORD_ALTERA_AVALON_SPI_STATUS(SD_SPI_BASE);
    }
    while ((status & ALTERA_AVALON_SPI_STATUS_TMT_MSK) == 0);
    // Clear the RRDY
    read_data = IORD_ALTERA_AVALON_SPI_RXDATA(SD_SPI_BASE);
  }
  
    //Check the status of SPI operation
    status = IORD_ALTERA_AVALON_SPI_STATUS(SD_SPI_BASE);
  if((status&ALTERA_AVALON_SPI_STATUS_E_MSK) == 0)
  {
    return 0;
  }
  else
  {
    return ERR;
  }
}

/*********************************************************************************************************
** Function name:    zlg_avalon_spi_recv_byte
**
** Descriptions:     Receive a byte through SPI master core. ss signal should be asserted
**                     and de-asserted explicitly. this function is not currently safe if 
**                     called in a multi-threaded environment, something above must perform 
**                     locking to make it safe if more than one thread intends to use it.  
**
** input parameters:   read_length -- read_length number of data will be read 
**                     read_data   -- a pointer to read buffer
**            
** Returned value:     0, seccussful;
**                     ERR, errors occurs.
**         
** Used global variables: None
** Calling modules:       
**
** Created by:        Jing.Zhang
** Created Date:      2005/12/08
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
int zlg_avalon_spi_recv_byte(alt_u32 read_length,alt_u8* read_data)
{
  alt_u32 status;
  alt_u32 i;
  volatile alt_u32 tmp;

  // Clear the RRDY
  tmp = IORD_ALTERA_AVALON_SPI_RXDATA(SD_SPI_BASE);
  for(i=0; i<read_length; i++)
  {   //Wait...until the txdata register is empty.
    do
    {
      status = IORD_ALTERA_AVALON_SPI_STATUS(SD_SPI_BASE);
    }
    while ((status & ALTERA_AVALON_SPI_STATUS_TRDY_MSK) == 0);
    //Send 0xff for read data in
    IOWR_ALTERA_AVALON_SPI_TXDATA(SD_SPI_BASE, 0xff);
    // Wait until the data has been received
    do
    {
      status = IORD_ALTERA_AVALON_SPI_STATUS(SD_SPI_BASE);
    }
    while ((status&ALTERA_AVALON_SPI_STATUS_RRDY_MSK) == 0);
    tmp = IORD_ALTERA_AVALON_SPI_RXDATA(SD_SPI_BASE);
    *read_data++ = (alt_u8) tmp;    
  }
    //Check the status of SPI operation
    status = IORD_ALTERA_AVALON_SPI_STATUS(SD_SPI_BASE);
  if((status&ALTERA_AVALON_SPI_STATUS_E_MSK) == 0)
  {
    return 0;
  }
  else
  {
    return ERR;
  }

}

⌨️ 快捷键说明

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