头文件
soft_spi.h
#ifndef __SOFT_SPI_H
#define __SOFT_SPI_H
#ifdef __cplusplus
extern "C" {
#endif
#include "sys.h"
#if SYSTEM_SUPPORT_OS
#include "maintask.h"
#endif
//引脚定义
#define SoftSpi_MISO_PIN GPIO_Pin_8
#define SoftSpi_MISO_GPIO_PORT GPIOB
#define SoftSpi_MOSI_PIN GPIO_Pin_9
#define SoftSpi_MOSI_GPIO_PORT GPIOB
#define SoftSpi_CLK_PIN GPIO_Pin_10
#define SoftSpi_CLK_GPIO_PORT GPIOB
//使用位带操作
#define SoftSpi_MISO PBin(8)
#define SoftSpi_MOSI PBout(9)
#define SoftSpi_CLK PBout(10)
//外设时钟使能
#define SoftSpi_CLK_ENABLE() RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE)
//SPI模式选择
//0:CPOL=0(空闲时低电平),CPHA=0(奇数边沿采样),MSB先行
//1:CPOL=0(空闲时低电平),CPHA=1(偶数边沿采样),MSB先行
//2:CPOL=1(空闲时高电平),CPHA=0(奇数边沿采样),MSB先行
//3:CPOL=1(空闲时高电平),CPHA=1(偶数边沿采样),MSB先行
//4:CPOL=0(空闲时低电平),CPHA=0(奇数边沿采样),LSB先行
//5:CPOL=0(空闲时低电平),CPHA=1(偶数边沿采样),LSB先行
//6:CPOL=1(空闲时高电平),CPHA=0(奇数边沿采样),LSB先行
//7:CPOL=1(空闲时高电平),CPHA=1(偶数边沿采样),LSB先行
#define SoftSpiMode 0
void SoftSpi_Init(void);
uint8_t SoftSpi_ReadWriteByte(uint8_t dat);
#ifdef __cplusplus
}
#endif
#endif
C文件
soft_spi.c
#include "soft_spi/soft_spi.h"
#include "delay.h"
//根据具体的SPI通信速度设置延时
//#define SoftSpi_Delay() delay_us(1)
#define SoftSpi_Delay() //定义为空,不延时
/**
* @brief 软件SPI配置
*/
void SoftSpi_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SoftSpi_CLK_ENABLE();//使能GPIO时钟
//输入配置
GPIO_InitStructure.GPIO_Pin = SoftSpi_MISO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//普通输入模式
// GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(SoftSpi_MISO_GPIO_PORT, &GPIO_InitStructure);//初始化GPIO
//输出配置
GPIO_InitStructure.GPIO_Pin = SoftSpi_MOSI_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通输出模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(SoftSpi_MOSI_GPIO_PORT, &GPIO_InitStructure);//初始化GPIO
GPIO_InitStructure.GPIO_Pin = SoftSpi_CLK_PIN;
GPIO_Init(SoftSpi_CLK_GPIO_PORT, &GPIO_InitStructure);//初始化GPIO
SoftSpi_MOSI = 1;
#if SoftSpiMode==0||SoftSpiMode==1||SoftSpiMode==4||SoftSpiMode==5
SoftSpi_CLK = 0;//模式0、1、4、5,空闲的时候为低电平
#else
SoftSpi_CLK = 1;//模式2、3、6、7,空闲的时候为高电平
#endif
}
/**
* @brief SPI读写一个字节
* @param dat -> 要写的一个字节数据
* @retval 读到的数据
*/
uint8_t SoftSpi_ReadWriteByte(uint8_t dat)
{
uint8_t reveDat,i;
reveDat = 0;
#if SoftSpiMode==0 //0:CPOL=0(空闲时低电平),CPHA=0(奇数边沿采样),MSB先行
for(i=0;i<8;i++)
{
if(dat&0x80) SoftSpi_MOSI=1;
else SoftSpi_MOSI=0;
dat*=2;
SoftSpi_CLK = 1;
SoftSpi_Delay();
reveDat*=2;
if(SoftSpi_MISO) reveDat++;
SoftSpi_CLK = 0;
SoftSpi_Delay();
}
#elif SoftSpiMode==1 //1:CPOL=0(空闲时低电平),CPHA=1(偶数边沿采样),MSB先行
for(i=0;i<8;i++)
{
SoftSpi_CLK = 1;
if(dat&0x80) SoftSpi_MOSI=1;
else SoftSpi_MOSI=0;
dat*=2;
SoftSpi_Delay();
SoftSpi_CLK = 0;
reveDat*=2;
if(SoftSpi_MISO) reveDat++;
SoftSpi_Delay();
}
#elif SoftSpiMode==2 //2:CPOL=1(空闲时高电平),CPHA=0(奇数边沿采样),MSB先行
for(i=0;i<8;i++)
{
if(dat&0x80) SoftSpi_MOSI=1;
else SoftSpi_MOSI=0;
dat*=2;
SoftSpi_Delay();
SoftSpi_CLK = 0;
reveDat*=2;
if(SoftSpi_MISO) reveDat++;
SoftSpi_Delay();
SoftSpi_CLK = 1;
}
#elif SoftSpiMode==3 //3:CPOL=1(空闲时高电平),CPHA=1(偶数边沿采样),MSB先行
for(i=0;i<8;i++)
{
SoftSpi_CLK = 0;
if(dat&0x80) SoftSpi_MOSI=1;
else SoftSpi_MOSI=0;
dat*=2;
SoftSpi_Delay();
SoftSpi_CLK = 1;
reveDat*=2;
if(SoftSpi_MISO) reveDat++;
SoftSpi_Delay();
}
#elif SoftSpiMode==4 //4:CPOL=0(空闲时低电平),CPHA=0(奇数边沿采样),LSB先行
for(i=0;i<8;i++)
{
if(dat&0x01) SoftSpi_MOSI=1;
else SoftSpi_MOSI=0;
dat/=2;
SoftSpi_CLK = 1;
SoftSpi_Delay();
reveDat/=2;
if(SoftSpi_MISO) reveDat+=8;
SoftSpi_CLK = 0;
SoftSpi_Delay();
}
#elif SoftSpiMode==5 //5:CPOL=0(空闲时低电平),CPHA=1(偶数边沿采样),LSB先行
for(i=0;i<8;i++)
{
SoftSpi_CLK = 1;
if(dat&0x01) SoftSpi_MOSI=1;
else SoftSpi_MOSI=0;
dat/=2;
SoftSpi_Delay();
SoftSpi_CLK = 0;
reveDat/=2;
if(SoftSpi_MISO) reveDat+=8;
SoftSpi_Delay();
}
#elif SoftSpiMode==6 //6:CPOL=1(空闲时高电平),CPHA=0(奇数边沿采样),LSB先行
for(i=0;i<8;i++)
{
if(dat&0x01) SoftSpi_MOSI=1;
else SoftSpi_MOSI=0;
dat/=2;
SoftSpi_Delay();
SoftSpi_CLK = 0;
reveDat/=2;
if(SoftSpi_MISO) reveDat+=8;
SoftSpi_Delay();
SoftSpi_CLK = 1;
}
#elif SoftSpiMode==7 //7:CPOL=1(空闲时高电平),CPHA=1(偶数边沿采样),LSB先行
for(i=0;i<8;i++)
{
SoftSpi_CLK = 0;
if(dat&0x01) SoftSpi_MOSI=1;
else SoftSpi_MOSI=0;
dat/=2;
SoftSpi_Delay();
SoftSpi_CLK = 1;
reveDat/=2;
if(SoftSpi_MISO) reveDat+=8;
SoftSpi_Delay();
}
#endif
return reveDat;
}