[转]普通GPIO口模拟SPI通信协议
来源:互联网 发布:数据标准化是什么意思 编辑:程序博客网 时间:2024/05/24 07:00
在工作中偶尔会遇到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 modetypedef 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 typetypedef enum SPIType{ SPIMaster, SPISlave,}SPIType;// Define SPI attributetypedef 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 prototypesvoid 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 pinsSpi_t Spi_0 = { .ui_CS = CS, .ui_SCLK = SCLK, .ui_MOSI = MOSI, .ui_MISO = MISO, .spiMode = Mode_1, .spiType = SPIMaster,};/*Brief: SPI protocol initiateInput: p_Spi, which spi useOutput: NoneReturn: NoneAuthor: Andy Lai*/void v_SPIInitSimulate(Spi_t* p_Spi){ assert(p_Spi != NULL);
<span class="hljs-keyword">if</span>(p_Spi->spiMode == SPIMaster){ ioport_set_pin_dir(p_Spi->ui_CS, IOPORT_DIR_OUTPUT); ioport_set_pin_dir(p_Spi->ui_SCLK, IOPORT_DIR_OUTPUT); ioport_set_pin_dir(p_Spi->ui_MOSI, IOPORT_DIR_OUTPUT); ioport_set_pin_dir(p_Spi->ui_MISO, IOPORT_DIR_INPUT);}<span class="hljs-keyword">else</span>{ ioport_set_pin_dir(p_Spi->ui_CS, IOPORT_DIR_INPUT); ioport_set_pin_dir(p_Spi->ui_SCLK, IOPORT_DIR_INPUT); ioport_set_pin_dir(p_Spi->ui_MOSI, IOPORT_DIR_INPUT); ioport_set_pin_dir(p_Spi->ui_MISO, IOPORT_DIR_OUTPUT);}pio_set_pin_high(p_Spi->ui_CS);<span class="hljs-keyword">switch</span>(p_Spi->spiMode){<span class="hljs-keyword">case</span> Mode_1:<span class="hljs-keyword">case</span> Mode_2: pio_set_pin_low(p_Spi->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->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->ui_CS);}<span class="hljs-keyword">else</span>{ pio_set_pin_high(p_Spi->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->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->ui_SCLK); <span class="hljs-keyword">for</span>(i = <span class="hljs-number">7</span>; i >= <span class="hljs-number">0</span>; i--) { pio_set_pin_low(p_Spi->ui_SCLK); SPIDelay; pio_set_pin_high(p_Spi->ui_SCLK); <span class="hljs-keyword">if</span>(uc_Bt & (<span class="hljs-number">1</span> << i)) { pio_set_pin_high(p_Spi->ui_MOSI); } <span class="hljs-keyword">else</span> { pio_set_pin_low(p_Spi->ui_MOSI); } SPIDelay; } pio_set_pin_low(p_Spi->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->ui_SCLK); <span class="hljs-keyword">for</span>(i = <span class="hljs-number">7</span>; i >= <span class="hljs-number">0</span>; i--) { pio_set_pin_high(p_Spi->ui_SCLK); <span class="hljs-keyword">if</span>(uc_Bt & (<span class="hljs-number">1</span> << i)) { pio_set_pin_high(p_Spi->ui_MOSI); } <span class="hljs-keyword">else</span> { pio_set_pin_low(p_Spi->ui_MOSI); } SPIDelay; pio_set_pin_low(p_Spi->ui_SCLK); SPIDelay; } pio_set_pin_low(p_Spi->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->ui_SCLK); <span class="hljs-keyword">for</span>(i = <span class="hljs-number">7</span>; i >= <span class="hljs-number">0</span>; i--) { pio_set_pin_high(p_Spi->ui_SCLK); <span class="hljs-keyword">if</span>(uc_Bt & (<span class="hljs-number">1</span> << i)) { pio_set_pin_high(p_Spi->ui_MOSI); } <span class="hljs-keyword">else</span> { pio_set_pin_low(p_Spi->ui_MOSI); } SPIDelay; pio_set_pin_low(p_Spi->ui_SCLK); SPIDelay; } pio_set_pin_high(p_Spi->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->ui_SCLK); <span class="hljs-keyword">for</span>(i = <span class="hljs-number">7</span>; i >= <span class="hljs-number">0</span>; i--) { pio_set_pin_low(p_Spi->ui_SCLK); <span class="hljs-keyword">if</span>(uc_Bt & (<span class="hljs-number">1</span> << i)) { pio_set_pin_high(p_Spi->ui_MOSI); } <span class="hljs-keyword">else</span> { pio_set_pin_low(p_Spi->ui_MOSI); } SPIDelay; pio_set_pin_high(p_Spi->ui_SCLK); SPIDelay; } pio_set_pin_high(p_Spi->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 > <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 < 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->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->ui_SCLK); <span class="hljs-keyword">for</span>(i = <span class="hljs-number">0</span>; i < <span class="hljs-number">8</span>; i++) { pio_set_pin_low(p_Spi->ui_SCLK); SPIDelay; pio_set_pin_high(p_Spi->ui_SCLK); uc_ReadData = uc_ReadData << <span class="hljs-number">1</span>; uc_ReadData |= pio_get_pin_value(p_Spi->ui_MISO); SPIDelay; } pio_set_pin_low(p_Spi->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->ui_SCLK); <span class="hljs-keyword">for</span>(i = <span class="hljs-number">0</span>; i < <span class="hljs-number">8</span>; i++) { pio_set_pin_high(p_Spi->ui_SCLK); SPIDelay; pio_set_pin_low(p_Spi->ui_SCLK); uc_ReadData = uc_ReadData << <span class="hljs-number">1</span>; uc_ReadData |= pio_get_pin_value(p_Spi->ui_MISO); SPIDelay; } pio_set_pin_low(p_Spi->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->ui_SCLK); <span class="hljs-keyword">for</span>(i = <span class="hljs-number">0</span>; i < <span class="hljs-number">8</span>; i++) { pio_set_pin_high(p_Spi->ui_SCLK); SPIDelay; pio_set_pin_low(p_Spi->ui_SCLK); uc_ReadData = uc_ReadData << <span class="hljs-number">1</span>; uc_ReadData |= pio_get_pin_value(p_Spi->ui_MISO); SPIDelay; } pio_set_pin_high(p_Spi->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->ui_SCLK); <span class="hljs-keyword">for</span>(i = <span class="hljs-number">0</span>; i < <span class="hljs-number">8</span>; i++) { pio_set_pin_low(p_Spi->ui_SCLK); SPIDelay; pio_set_pin_high(p_Spi->ui_SCLK); uc_ReadData = uc_ReadData << <span class="hljs-number">1</span>; uc_ReadData |= pio_get_pin_value(p_Spi->ui_MISO); SPIDelay; } pio_set_pin_high(p_Spi->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 > <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 < 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>);
}
- [转]普通GPIO口模拟SPI通信协议
- 普通GPIO模拟SPI通信协议(软件SPI)
- 普通GPIO模拟SPI通信协议(软件SPI)
- GPIO口模拟SPI
- GPIO口模拟SPI - 驱动W25Qxx
- GPIO模拟SPI
- GPIO模拟SPI
- GPIO模拟SPI
- 总结GPIO模拟SPI
- GPIO模拟SPI
- MCU GPIO模拟spi
- GPIO模拟SPI
- gpio模拟SPI
- 普通GPIO口线模拟I2C
- GPIO模拟SPI通讯接口
- linux SPI驱动 - 模拟gpio
- A20,gpio模拟spi 通信
- GPIO模拟SPI接口代码
- Openssl-1
- IntelliJ IDEA下自动生成Hibernate映射文件以及实体类
- eclipse service client都做了哪些事
- 希尔排序
- 38. Count and Say —— Java
- [转]普通GPIO口模拟SPI通信协议
- HBase scan setBatch和setCaching的区别
- python里怎么终止程序的执行
- WOJ1032-Find the Max NORM
- Linux中Makefile的使用
- 快速排序
- 我以亲身经历告诉你崔廉正不是骗子
- 【SPSS】统计功能与模块对照表
- linux查看日志