[转]普通GPIO口模拟SPI通信协议


在工作中偶尔会遇到SPI不够用的情况,而我们又要去使用SPI通信协议,此时就需要我们自己去模拟SPI通信协议。我们知道SPI通信协议有四种模式,它们分别如下所示:
这里写图片描述
下面是我基于ATSAM4SD16B芯片在Atmel Studio上用普通GPIO模拟的SPI通信协议的代码:

#include "ioport.h"
#include "pio.h"
#include "delay.h"
#include "SAM4S_FSA.h"
#include <assert.h>

// Define 4 SPI pins
#define CS       IOPORT_CREATE_PIN(PIOA, 8)
#define SCLK     IOPORT_CREATE_PIN(PIOA, 7)
#define MOSI     IOPORT_CREATE_PIN(PIOA, 23)
#define MISO     IOPORT_CREATE_PIN(PIOA, 20)

#define SPIDelay  delay_us(1)

// Define SPI communication mode
typedef enum SPIMode
{
    Mode_1,   /* Clock Polarity is 0 and Clock Phase is 0 */
    Mode_2,   /* Clock Polarity is 0 and Clock Phase is 1 */
    Mode_3,   /* Clock Polarity is 1 and Clock Phase is 0 */
    Mode_4,   /* Clock Polarity is 1 and Clock Phase is 1 */
}SPIMode;

// Define SPI type
typedef enum SPIType
{
    SPIMaster,
    SPISlave,
}SPIType;

// Define SPI attribute
typedef struct SpiStruct
{
    unsigned int ui_CS;
    unsigned int ui_SCLK;
    unsigned int ui_MOSI;
    unsigned int ui_MISO;
    SPIMode spiMode;
    SPIType spiType;
}Spi_t;

// Function prototypes
void v_SPIInitSimulate(Spi_t* p_Spi);
void v_CSIsEnableSimulate(Spi_t* p_Spi, int i_IsEnable);
void v_SPIWriteSimulate(Spi_t* p_Spi, unsigned char* puc_Data, int i_DataLength);
void v_SPIReadSimulate(Spi_t* p_Spi, unsigned char* puc_Data, int i_DataLength);

// Define SPI pins
Spi_t Spi_0 = 
{
    .ui_CS = CS,
    .ui_SCLK = SCLK,
    .ui_MOSI = MOSI,
    .ui_MISO = MISO,
    .spiMode = Mode_1,
    .spiType = SPIMaster,
};

/*
Brief: SPI protocol initiate
Input: p_Spi, which spi use
Output: None
Return: None
Author: Andy Lai
*/
void v_SPIInitSimulate(Spi_t* p_Spi)
{
    assert(p_Spi != NULL);

<span class="hljs-keyword">if</span>(p_Spi-&gt;spiMode == SPIMaster)
{
    ioport_set_pin_dir(p_Spi-&gt;ui_CS, IOPORT_DIR_OUTPUT);
    ioport_set_pin_dir(p_Spi-&gt;ui_SCLK, IOPORT_DIR_OUTPUT);
    ioport_set_pin_dir(p_Spi-&gt;ui_MOSI, IOPORT_DIR_OUTPUT);
    ioport_set_pin_dir(p_Spi-&gt;ui_MISO, IOPORT_DIR_INPUT);
}
<span class="hljs-keyword">else</span>
{
    ioport_set_pin_dir(p_Spi-&gt;ui_CS, IOPORT_DIR_INPUT);
    ioport_set_pin_dir(p_Spi-&gt;ui_SCLK, IOPORT_DIR_INPUT);
    ioport_set_pin_dir(p_Spi-&gt;ui_MOSI, IOPORT_DIR_INPUT);
    ioport_set_pin_dir(p_Spi-&gt;ui_MISO, IOPORT_DIR_OUTPUT);
}

pio_set_pin_high(p_Spi-&gt;ui_CS);
<span class="hljs-keyword">switch</span>(p_Spi-&gt;spiMode)
{
<span class="hljs-keyword">case</span> Mode_1:
<span class="hljs-keyword">case</span> Mode_2:
    pio_set_pin_low(p_Spi-&gt;ui_SCLK);
    <span class="hljs-keyword">break</span>;
<span class="hljs-keyword">case</span> Mode_3:
<span class="hljs-keyword">case</span> Mode_4:
    pio_set_pin_high(p_Spi-&gt;ui_SCLK);
    <span class="hljs-keyword">break</span>;
}

}

/*
Brief: CS low level signal enable and high level signal disable
Input: (1)p_Spi, which spi use
(2)i_IsEnable, Chip select(Slave select) enable flag
Output: None
Return: None
Author: Andy Lai
*/

void v_CSIsEnableSimulate(Spi_t* p_Spi, int i_IsEnable)
{
assert(p_Spi != NULL);

<span class="hljs-keyword">if</span>(i_IsEnable)
{
    pio_set_pin_low(p_Spi-&gt;ui_CS);
}
<span class="hljs-keyword">else</span>
{
    pio_set_pin_high(p_Spi-&gt;ui_CS);
}

}

/*
Brief: Use SPI to write a byte data
Input: (1)p_Spi, which spi use
(2)uc_Bt, write byte data
Output: None
Return: None
Author: Andy Lai
*/

static void v_SPIWriteByte(Spi_t* p_Spi, unsigned char uc_Bt)
{
int i = 0;

assert(p_Spi != <span class="hljs-literal">NULL</span>);

<span class="hljs-keyword">switch</span>(p_Spi-&gt;spiMode)
{
<span class="hljs-keyword">case</span> Mode_1: <span class="hljs-comment">/* Clock Polarity is 0 and Clock Phase is 0 */</span>
    pio_set_pin_low(p_Spi-&gt;ui_SCLK);
    <span class="hljs-keyword">for</span>(i = <span class="hljs-number">7</span>; i &gt;= <span class="hljs-number">0</span>; i--)
    {
        pio_set_pin_low(p_Spi-&gt;ui_SCLK);
        SPIDelay;
        pio_set_pin_high(p_Spi-&gt;ui_SCLK);
        <span class="hljs-keyword">if</span>(uc_Bt &amp; (<span class="hljs-number">1</span> &lt;&lt; i))
        {
            pio_set_pin_high(p_Spi-&gt;ui_MOSI);
        }
        <span class="hljs-keyword">else</span>
        {
            pio_set_pin_low(p_Spi-&gt;ui_MOSI);
        }
        SPIDelay;
    }
    pio_set_pin_low(p_Spi-&gt;ui_SCLK);
    <span class="hljs-keyword">break</span>;

<span class="hljs-keyword">case</span> Mode_2: <span class="hljs-comment">/* Clock Polarity is 0 and Clock Phase is 1 */</span>
    pio_set_pin_low(p_Spi-&gt;ui_SCLK);
    <span class="hljs-keyword">for</span>(i = <span class="hljs-number">7</span>; i &gt;= <span class="hljs-number">0</span>; i--)
    {
        pio_set_pin_high(p_Spi-&gt;ui_SCLK);
        <span class="hljs-keyword">if</span>(uc_Bt &amp; (<span class="hljs-number">1</span> &lt;&lt; i))
        {
            pio_set_pin_high(p_Spi-&gt;ui_MOSI);
        }
        <span class="hljs-keyword">else</span>
        {
            pio_set_pin_low(p_Spi-&gt;ui_MOSI);
        }
        SPIDelay;
        pio_set_pin_low(p_Spi-&gt;ui_SCLK);
        SPIDelay;
    }
    pio_set_pin_low(p_Spi-&gt;ui_SCLK);
    <span class="hljs-keyword">break</span>;

<span class="hljs-keyword">case</span> Mode_3: <span class="hljs-comment">/* Clock Polarity is 1 and Clock Phase is 0 */</span>
    pio_set_pin_high(p_Spi-&gt;ui_SCLK);
    <span class="hljs-keyword">for</span>(i = <span class="hljs-number">7</span>; i &gt;= <span class="hljs-number">0</span>; i--)
    {
        pio_set_pin_high(p_Spi-&gt;ui_SCLK);
        <span class="hljs-keyword">if</span>(uc_Bt &amp; (<span class="hljs-number">1</span> &lt;&lt; i))
        {
            pio_set_pin_high(p_Spi-&gt;ui_MOSI);
        }
        <span class="hljs-keyword">else</span>
        {
            pio_set_pin_low(p_Spi-&gt;ui_MOSI);
        }
        SPIDelay;
        pio_set_pin_low(p_Spi-&gt;ui_SCLK);
        SPIDelay;
    }
    pio_set_pin_high(p_Spi-&gt;ui_SCLK);
    <span class="hljs-keyword">break</span>;

<span class="hljs-keyword">case</span> Mode_4: <span class="hljs-comment">/* Clock Polarity is 1 and Clock Phase is 1 */</span>
    pio_set_pin_high(p_Spi-&gt;ui_SCLK);
    <span class="hljs-keyword">for</span>(i = <span class="hljs-number">7</span>; i &gt;= <span class="hljs-number">0</span>; i--)
    {
        pio_set_pin_low(p_Spi-&gt;ui_SCLK);
        <span class="hljs-keyword">if</span>(uc_Bt &amp; (<span class="hljs-number">1</span> &lt;&lt; i))
        {
            pio_set_pin_high(p_Spi-&gt;ui_MOSI);
        }
        <span class="hljs-keyword">else</span>
        {
            pio_set_pin_low(p_Spi-&gt;ui_MOSI);
        }
        SPIDelay;
        pio_set_pin_high(p_Spi-&gt;ui_SCLK);
        SPIDelay;
    }
    pio_set_pin_high(p_Spi-&gt;ui_SCLK);
    <span class="hljs-keyword">break</span>;

<span class="hljs-keyword">default</span>:
    <span class="hljs-keyword">break</span>;
}

}

/*
Brief: Use SPI protocol to write data
Input: (1)p_Spi, which spi use
(2)puc_Data, write data string
(3)i_DataLength, write data length
Output: None
Return: None
Author: Andy Lai
*/

void v_SPIWriteSimulate(Spi_t* p_Spi, unsigned char* puc_Data, int i_DataLength)
{
int i = 0;

assert(p_Spi != <span class="hljs-literal">NULL</span>);
assert(puc_Data != <span class="hljs-literal">NULL</span>);
assert(i_DataLength &gt; <span class="hljs-number">0</span>);

v_CSIsEnableSimulate(p_Spi, <span class="hljs-number">1</span>);
delay_us(<span class="hljs-number">8</span>);

<span class="hljs-comment">// Write data</span>
<span class="hljs-keyword">for</span>(i = <span class="hljs-number">0</span>; i &lt; i_DataLength; i++)
{
    v_SPIWriteByte(p_Spi, puc_Data[i]);
}

delay_us(<span class="hljs-number">8</span>);
v_CSIsEnableSimulate(p_Spi, <span class="hljs-number">0</span>);

}

/*
Brief: Read a byte data from SPI
Input: p_Spi, which spi use
Output: None
Return: Read data
Author: Andy Lai
*/

static unsigned char uc_SPIReadByte(Spi_t* p_Spi)
{
int i = 0;
unsigned char uc_ReadData = 0;

assert(p_Spi != <span class="hljs-literal">NULL</span>);

<span class="hljs-keyword">switch</span>(p_Spi-&gt;spiMode)
{
<span class="hljs-keyword">case</span> Mode_1: <span class="hljs-comment">/* Clock Polarity is 0 and Clock Phase is 0 */</span>
    pio_set_pin_low(p_Spi-&gt;ui_SCLK);
    <span class="hljs-keyword">for</span>(i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">8</span>; i++)
    {
        pio_set_pin_low(p_Spi-&gt;ui_SCLK);
        SPIDelay;
        pio_set_pin_high(p_Spi-&gt;ui_SCLK);
        uc_ReadData = uc_ReadData &lt;&lt; <span class="hljs-number">1</span>;
        uc_ReadData |= pio_get_pin_value(p_Spi-&gt;ui_MISO);
        SPIDelay;
    }
    pio_set_pin_low(p_Spi-&gt;ui_SCLK);
    <span class="hljs-keyword">break</span>;

<span class="hljs-keyword">case</span> Mode_2: <span class="hljs-comment">/* Clock Polarity is 0 and Clock Phase is 1 */</span>
    pio_set_pin_low(p_Spi-&gt;ui_SCLK);
    <span class="hljs-keyword">for</span>(i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">8</span>; i++)
    {
        pio_set_pin_high(p_Spi-&gt;ui_SCLK);
        SPIDelay;
        pio_set_pin_low(p_Spi-&gt;ui_SCLK);
        uc_ReadData = uc_ReadData &lt;&lt; <span class="hljs-number">1</span>;
        uc_ReadData |= pio_get_pin_value(p_Spi-&gt;ui_MISO);
        SPIDelay;
    }
    pio_set_pin_low(p_Spi-&gt;ui_SCLK);
    <span class="hljs-keyword">break</span>;

<span class="hljs-keyword">case</span> Mode_3: <span class="hljs-comment">/* Clock Polarity is 1 and Clock Phase is 0 */</span>
    pio_set_pin_high(p_Spi-&gt;ui_SCLK);
    <span class="hljs-keyword">for</span>(i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">8</span>; i++)
    {
        pio_set_pin_high(p_Spi-&gt;ui_SCLK);
        SPIDelay;
        pio_set_pin_low(p_Spi-&gt;ui_SCLK);
        uc_ReadData = uc_ReadData &lt;&lt; <span class="hljs-number">1</span>;
        uc_ReadData |= pio_get_pin_value(p_Spi-&gt;ui_MISO);
        SPIDelay;
    }
    pio_set_pin_high(p_Spi-&gt;ui_SCLK);
    <span class="hljs-keyword">break</span>;

<span class="hljs-keyword">case</span> Mode_4:  <span class="hljs-comment">/* Clock Polarity is 1 and Clock Phase is 1 */</span>
    pio_set_pin_high(p_Spi-&gt;ui_SCLK);
    <span class="hljs-keyword">for</span>(i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">8</span>; i++)
    {
        pio_set_pin_low(p_Spi-&gt;ui_SCLK);
        SPIDelay;
        pio_set_pin_high(p_Spi-&gt;ui_SCLK);
        uc_ReadData = uc_ReadData &lt;&lt; <span class="hljs-number">1</span>;
        uc_ReadData |= pio_get_pin_value(p_Spi-&gt;ui_MISO);
        SPIDelay;
    }
    pio_set_pin_high(p_Spi-&gt;ui_SCLK);
    <span class="hljs-keyword">break</span>;

<span class="hljs-keyword">default</span>:
    <span class="hljs-keyword">break</span>;
}

<span class="hljs-keyword">return</span> uc_ReadData;

}

/*
Brief: Use SPI to read data
Input: (1)p_Spi, which SPI use;
(2)i_DataLength, the length of data that need to read
Output: puc_Data, need to get data
Return: None
Author: Andy Lai
*/

void v_SPIReadSimulate(Spi_t* p_Spi, unsigned char* puc_Data, int i_DataLength)
{
int i = 0;

assert(p_Spi != <span class="hljs-literal">NULL</span>);
assert(i_DataLength &gt; <span class="hljs-number">0</span>);

v_CSIsEnableSimulate(p_Spi, <span class="hljs-number">1</span>);
delay_us(<span class="hljs-number">8</span>);

<span class="hljs-comment">// Read data</span>
<span class="hljs-keyword">for</span>(i = <span class="hljs-number">0</span>; i &lt; i_DataLength; i++)
{
    puc_Data[i] = uc_SPIReadByte(p_Spi);
}

delay_us(<span class="hljs-number">8</span>);
v_CSIsEnableSimulate(p_Spi, <span class="hljs-number">0</span>);

}

    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值