您的位置:笔笔发 技术资料 接口相关技术 正文
内容搜索
笔笔发:技术产业信息平台----成就有识之士创业梦想的殿堂
热门内容
推荐内容
GPIO模拟SPI总线(4线模式)


先给大家把源代码贴上来吧,等有时间了好好整理一下。

在大家做之前,先给大家说下模拟常识。

1。由于GPIO的相应速度有限,所以模拟的SPI速度有限,我这里大概是1.7M。所以GPIO模拟SPI只适合用于SPI设备控制和少量低速率数据传输。一般,SPI可以到26M。

2。通用性差,需要按照操作的SPI设备提供的SPI时序来模拟,不想专用SPI硬件接口,可以配置多种时序。

3。一定要那示波器来抓取模拟的读写时序,和SPI设备手册一一对照。

/*************************************************************************/
/*                                                                                                                      */
/* FILE NAME                                                                                                 */
/*      drv_spi.c                                                                                                */
/*                                                                                                                      */
/* DESCRIPTION                                                                                          */
/*      This file contains the basic spi function by using GPIO.              */
/*                                                                                                                      */
/*************************************************************************/

#include "target.h" // This is the GPIO define of your board

static u32 SPI_CS_PIN;
static u32 SPI_CLK_PIN;
static u32 SPI_DOUT_PIN;
static u32 SPI_DIN_PIN;
static u32 SPI_BUS;

#define USE_SPI_BUSY_PIN  0

// 20 ---> 195 KHz
// 10 ---> 355 KHz
#define DELAY 10

void spiDelay(u16 iCount)
{
    u16 i;
    for(i=0;i<iCount;i++); 
}

// SPI Internal Interface API

/* -------------------------------------------------------------------------*
 * Function   : spiWrBitHigh                                                *
 * Description: This function is used to transfer a High bit to SPI bus.    *
 * Parameters : None                                                        *
 * Return     : None                                                        *
 * -------------------------------------------------------------------------*/
void spiWrBitHigh(void)
{
    hal_gpio_SetBit(SPI_DOUT_PIN);
    hal_gpio_ClrBit(SPI_CLK_PIN);
    spiDelay(10);
    hal_gpio_SetBit(SPI_CLK_PIN);
    spiDelay(10);
    hal_gpio_ClrBit(SPI_CLK_PIN);
}

/* -------------------------------------------------------------------------*
 * Function   : spiWrBitLow                                                 *
 * Description: This function is used to transfer a Low bit to SPI bus.     *
 * Parameters : None                                                        *
 * Return     : None                                                        *
 * -------------------------------------------------------------------------*/
void spiWrBitLow(void)
{
    hal_gpio_ClrBit(SPI_DOUT_PIN);
    hal_gpio_ClrBit(SPI_CLK_PIN);
    spiDelay(10);
    hal_gpio_SetBit(SPI_CLK_PIN);
    spiDelay(10);
    hal_gpio_ClrBit(SPI_CLK_PIN);
}

// I2C Extern Interface API

/* -------------------------------------------------------------------------*/
/* Function   : spi_Open                                                    */
/* Description: Init SPI port.                                              */
/* Parameters : GPIO bit which defined as SPI bus.                          */
/* Return     : TRUE if OK.                                                 */
/*              FALSE if Wrong.                                             */
/* -------------------------------------------------------------------------*/
bool spi_Open(u8 spi_cs,u8 spi_dout,u8 spi_din,u8 spi_clk)
{
 spi_fprintf((TSTDOUT,"init SPI ports..."));
 SPI_CS_PIN   = 1 << spi_cs;
 SPI_DOUT_PIN = 1 << spi_dout;
 SPI_DIN_PIN  = 1 << spi_din;
 SPI_CLK_PIN  = 1 << spi_clk;
 SPI_BUS = SPI_CS_PIN | SPI_DOUT_PIN |SPI_DIN_PIN | SPI_CLK_PIN;
 
    //if ((SPI_BUS & USED_GPIO)!= SPI_BUS)
    //{
    //    spi_fprintf((TSTDOUT, "SPI BUS is 0x%x.",SPI_BUS));
    //    spi_fprintf((TSTDOUT, "SPI BUS is 0x%x.",USED_GPIO));
    //    spi_fprintf((TSTDOUT, "SPI BUS is 0x%x.",(SPI_BUS & USED_GPIO)));
    //    spi_fprintf((TSTDOUT, "SPI GPIO Used Wrong in Board Config."));
    //    return(FALSE);
    //}
 
 // Set the GPIO direction of SPI interface
 hal_gpio_SetOut(SPI_CS_PIN);
 hal_gpio_SetOut(SPI_DOUT_PIN);
 hal_gpio_SetOut(SPI_CLK_PIN);
 hal_gpio_SetIn(SPI_DIN_PIN);
 //hal_gpio_SetIn(SPI_BUSY_PIN);

 // Set the initialize status of each SPI interface
 hal_gpio_ClrBit(SPI_CS_PIN);
 hal_gpio_ClrBit(SPI_CLK_PIN);
 hal_gpio_ClrBit(SPI_DOUT_PIN);
 
 return(TRUE);
}

/* -------------------------------------------------------------------------*/
/* Function   : spi_Close                                                   */
/* Description: Close SPI port.                                             */
/* Parameters : None                                                        */
/* Return     : TRUE if OK.                                                 */
/*              FALSE if Wrong.                                             */
/* -------------------------------------------------------------------------*/
bool spi_Close(void)
{
 return(TRUE);
}

/* -------------------------------------------------------------------------*/
/* Function   : spi_GetData                                                 */
/* Description: This function is used to Get data from SPI bus.             */
/* Parameters : iRecvData -- Data pointer to be stored                      */
/* Return     : TRUE if OK.                                                 */
/*              FALSE if Wrong.                                             */
/* -------------------------------------------------------------------------*/
bool spi_GetData(u16 * iRecvData)
{
    s16 iCount = 0;

#if USE_SPI_BUSY_PIN
    u32 i = 0;
#endif
 
    *iRecvData = 0;
 
    // Make sure that the initialize status of SPI CLK is low
    hal_gpio_ClrBit(SPI_CLK_PIN);
    // Make sure that the initialize status of SPI DOUT is low
    hal_gpio_ClrBit(SPI_DOUT_PIN);
 
 spiDelay(10);
 
#if USE_SPI_BUSY_PIN
    while (i < 0xfffff)
    {
        if(hal_gpio_GetVal(SPI_BUSY_PIN) == 0) break;
  i++;
 }
 if(i == 0xfffff) return (FALSE);
#endif

    for(iCount = 15;iCount >= 0;iCount --)
 {
        hal_gpio_SetBit(SPI_CLK_PIN);
        spiDelay(10);
  
  if(hal_gpio_GetVal(SPI_DIN_PIN)) (*iRecvData)|=(1<<iCount);
  
  hal_gpio_ClrBit(SPI_CLK_PIN);
  spiDelay(10);
 }
 
 return(TRUE);
}

/* -------------------------------------------------------------------------*/
/* Function   : spi_SendData                                                */
/* Description: This function is used to Send data to SPI bus.              */
/* Parameters : iSendData -- 8 bit data to be sent                          */
/* Return     : TRUE if OK.                                                 */
/*              FALSE if Wrong.                                             */
/* -------------------------------------------------------------------------*/
bool spi_SendData(u8 iSendData)
{
    s8 iCount = 0;

    for(iCount=7; iCount>=0; iCount--)
    {
     if(iSendData & (1<<iCount)) spiWrBitHigh();
  else spiWrBitLow();
 }
 
 return (TRUE);
}
 


 
点这里复制本页地址发送给您QQ/MSN上的好友
相关文章

UART
韦根协议
RS485总线
SPI总线
I2C总线概要及用GPIO模拟I2C
89C51 单片机I/O 口模拟串行通信的实现方法
CAN总线简介
USB接口
IEEE1394
6800接口与8080接口的区别
IIC总线的控制技术
基于PCI总线的GP-IB接口电路设计与实现

相关评论


本文章所属分类:首页 技术资料 接口相关技术


反馈意见和建议