stm32 SPI通信[操作寄存器+库函数]

    SPI(Serial Peripheral Interface–串行外设接口) 总线系统是一种同步串行外设接口,它可以使MCU与各种外围设备以串行方式进行通信以交换信息。 SPI是Freescale(原 Motorola)公司首先在其处理器上定义的。
    SPI是一种高速、主从式、全双工、同步传输的通信总线,SPI总线在物理层体现为四根传输线:


MOSI (Master Output Slaver Input) – 主器件数据输出,从器件数据输入 MISO (Master Input Slaver Output)  – 主器件数据输入,从器件数据输出
SCLK – 时钟信号,由主器件产生 NSS – 从器件使能信号,由主器件控制,有的IC会标注为CS(Chip select)
    CS线用于控制片选信号,当一个SPI从设备的CS线识别到了预先规定的片选电平,则该设备被选中。显然可以通过CS线,完成“一主多从”的SPI网络架设,在进行“一主一从”的SPI通信时,SPI并不是必须的。
    
    SPI总线传输数据时,由主机的SCLK线提供时钟脉冲,从机被动的接收时钟脉冲。主机在数据发送前,将数据写入数据寄存器,等待时钟脉冲移位输出,每个脉冲周期传输1位数据。 从机在主机的数据发送中,依靠主机的时钟,将从机的数据寄存器内容移位发送。所以要实现主从数据交换,在时钟信号前,主机 从机都必须先将数据写入数据寄存器,并且从机必须在主机前写入,然后由主机的SCLK时钟驱动发送。 不注意这个问题很容易造成SPI接收的数据错位。
    这样的全双工、同步传输完全依赖于 主机控制的时钟线SCLK,而且SCLK上只有数据传输的时候才有时钟信号。主机向从机发送数据不会有问题,但是如果从机主动向主机发送数据呢? 
    从机要发送数据,必须要有SCLK的时钟,所以只能主机发送一个DUMMY(哑巴)字节,产生时钟,来实现和从机的数据交换。 从设备只能被动发送数据,无法主动发送数据。
    本例实现 通过将STM32上的2个SPI接口对接,进行一个简单的数据交换。使用SPI1作为主设备,SPI2作为从设备,通过串口查看数据通信的情况。
实现结果如下:
spi.png
直接操作寄存器
首先配置SPI主机的频率
SPI1设备属于高速设备,隶属APB2总线,最大时钟72Mhz;
SPI2属于低速设备,隶属APB1总线,最大36Mhz。
在控制寄存器中设置时钟分频值,设置时钟极性和相位等。程序中有注释,详见代码:
User/main.c  (system.h 和 stm32f10x_it.h 等相关代码参照 stm32 直接操作寄存器开发环境配置
#include <stm32f10x_lib.h>
#include “system.h”
#include “usart.h”
#include “spi.h”

#define LED1 PAout(4)
#define LED2 PAout(5)
#define LED3 PAout(6)
#define LED4 PAout(7)

void Gpio_Init(void);

#define BufferSize 32

u8 SPI1_Buffer_Tx[BufferSize] =
{
0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x10,
0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,
0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,0x20
};

u8 SPI2_Buffer_Tx[BufferSize] =
{
0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,
0x59,0x5A,0x5B,0x5C,0x5D,0x5E,0x5F,0x60,
0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,
0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F,0x70
};

u8 SPI1_Buffer_Rx[BufferSize] = {0xFF};
u8 SPI2_Buffer_Rx[BufferSize] = {0xFF};
u8 Tx_Counter = 0;
u8 Rx_Counter = 0;

int main(void)
{
u8 k=0;

Rcc_Init(9); //系统时钟设置

Usart1_Init(72,9600);

Nvic_Init(1,0,SPI1_IRQChannel,4); //设置抢占优先级为1,响应优先级为0,中断分组为4
Nvic_Init(1,1,SPI2_IRQChannel,4); //设置抢占优先级为1,响应优先级为1,中断分组为4

Gpio_Init();

Spi_Init(SPI1);
Spi_Init(SPI2);

while(Tx_Counter < BufferSize)
{

Spi_Write(SPI2,SPI2_Buffer_Tx[Tx_Counter]); //必须先将从设备数据写入数据寄存器,等待时钟同步

Spi_Write(SPI1,SPI1_Buffer_Tx[Tx_Counter]); //主设备将数据写入数据寄存器,触发同步时钟,让主从数据寄存器由此时钟发送

SPI2_Buffer_Rx[Rx_Counter] = Spi_Read(SPI2);

SPI1_Buffer_Rx[Rx_Counter] = Spi_Read(SPI1);

Tx_Counter++;
Rx_Counter++;

}

printf(“\r\n The SPI1 has sended data below : \r\n”);

while(k<BufferSize)
{
printf(“ %0.2d \r “,SPI1_Buffer_Tx[k]);
k++;
}

printf(“\r\n The SPI2 has received data below : \r\n”);

k=0;

while(k<BufferSize)
{
printf(“ %0.2d \r “,SPI2_Buffer_Rx[k]);
k++;
}

k=0;

printf(“\r\n The SPI2 has sended data below : \r\n”);

while(k<BufferSize)
{
printf(“ %0.2d \r “,SPI2_Buffer_Tx[k]);
k++;
}

printf(“\r\n The SPI1 has received data below : \r\n”);

k=0;

while(k<BufferSize)
{
printf(“ %0.2d \r “,SPI1_Buffer_Rx[k]);
k++;
}

while(1);
}

void Gpio_Init(void)
{
RCC->APB2ENR |= 1<<2; //使能PORTA时钟
RCC->APB2ENR |= 1<<3; //使能PORTB时钟;

//SPI1 I/O设置

GPIOA->CRL &= 0x000FFFFF; //PA 5,6,7 复用
GPIOA->CRL |= 0xBBB00000;

//SPI2 I/O设置

GPIOB->CRH &= 0x000FFFFF; //PB 13,14,15 复用
GPIOB->CRH |= 0xBBB00000;

//USART1 串口I/O设置

GPIOA -> CRH &= 0xFFFFF00F; //设置USART1 的Tx(PA.9)为第二功能推挽,50MHz;Rx(PA.10)为浮空输入
GPIOA -> CRH |= 0x000008B0;

}
User/stm32f10x_it.c
#include “stm32f10x_it.h”
#include “system.h”
#include “stdio.h”

#define LED1 PAout(4)
#define LED2 PAout(5)
#define LED3 PAout(6)
#define LED4 PAout(7)

void SPI1_IRQHandler()
{
if(SPI1->SR & 1<<7) //SPI正忙于通信,或者发送缓冲非空
{
printf(“SPI1 is Busy”);
}

if(SPI1->SR & 1<<6) // 出现溢出错误
{
printf(“SPI1 is Overrun”);
}

if(SPI1->SR & 1<<5) //出现模式错误
{
printf(“SPI1 is Mode fault”);
}

if(SPI1->SR & 1<<4) //收到的CRC值和SPI_RXCRCR寄存器中的值不匹配
{
printf(“SPI1 is CRC Error”);
}

printf(“SPI1 Error”);

}

void SPI2_IRQHandler()
{
printf(“SPI2 Error”);

}
Library/src/spi.c
#include “spi.h”

//SPI初始化函数
//SPI1主机模式,SPI2从机模式,8bit数据格式,时钟空闲保持为低,数据采样从第二个时钟边沿开始,波特率 fPCLK/32
//先发送LSB(最低有效位),禁止硬件CRC校验
void Spi_Init(SPI_TypeDef SPIx)
{
if(SPIx == SPI1){

RCC -> APB2ENR |= 1<<12; //SPI1时钟使能
RCC -> APB2RSTR |= 1<<12; //复位SPI1寄存器
RCC -> APB2RSTR &= ~(1<<12); //复位结束SPI1寄存器

SPIx -> CR1 |= 1<<2; //主设备选择 0:配置为从设备 1:配置为主设备
SPIx -> CR1 |= 1<<8; //SSI位,要保持主机模式,必须NSS 接到高电平信号

}else if(SPIx == SPI2){
RCC -> APB1ENR |= 1<<14; //SPI2时钟使能
RCC -> APB1RSTR |= 1<<14; //复位SPI2寄存器
RCC -> APB1RSTR &= ~(1<<14); //复位结束SPI2寄存器
SPIx -> CR1 |= 0<<2; //主设备选择 0:配置为从设备 1:配置为主设备
//SPIx -> CR1 |= 0<<8; //SSI位,要保持主机模式,必须NSS 接到高电平信号
}

SPIx -> CR1 |= 0<<10; //设置全双工模式 0:全双工(发送和接收) 1:禁止输出(只接收模式)
SPIx -> CR1 |= 0<<11; //数据帧格式 0:使用8位数据帧格式进行发送/接收 1:使用16位数据帧格式进行发送/接收
SPIx -> CR1 |= 1<<7; //帧格式 0:先发送MSB 1:先发送LSB

//配置NSS为GPIO输出口控制从设备片选
SPIx -> CR1 |= 1<<9; //软件从设备管理 当此位(SSM)被置位时,NSS引脚上的电平由SSI位的值决定。

SPIx -> CR1 |= 0<<1; //配置时钟极性 0: 空闲状态时,SCK保持低电平 1: 空闲状态时,SCK保持高电平
SPIx -> CR1 |= 1<<0; //时钟相位 0: 数据采样从第一个时钟边沿开始 1: 数据采样从第二个时钟边沿开始

SPIx -> CR1 |= 4<<3; //波特率控制[5:3] 000: fPCLK/2 001: fPCLK/4 010: fPCLK/8 011: fPCLK/16 100: fPCLK/32
// 101: fPCLK/64 110: fPCLK/128 111: fPCLK/256

//SPIx -> CR2 |= 1<<7; //发送缓冲区空中断使能
//SPIx -> CR2 |= 1<<6; //接收缓冲区非空中断使能
SPIx -> CR2 |= 1<<5; //错误中断使能

SPIx -> CR1 |= 1<<6; //SPI设备使能

}

void Spi_Write(SPI_TypeDef
SPIx,u8 data)
{
//while((SPIx->SR&1<<1) == 0); //等待发送缓冲为空置位

SPIx->DR = data;

Spi_Delay(3); //必须稍作延时
}

u8 Spi_Read(SPI_TypeDef SPIx)
{

//while((SPIx->SR&1<<0) == 1); //等待接收缓冲非空置位

return SPIx->DR;

}

void Spi_Delay(u32 us)
{
u32 time=100
us/7;
while(–time);
}
Library/inc/spi.h
#include <stm32f10x_lib.h>

void Spi_Init(SPI_TypeDef SPIx);
void Spi_Write(SPI_TypeDef
SPIx,u8 data);
u8 Spi_Read(SPI_TypeDef SPIx);
void Spi_Delay(u32 us);
操作库函数
main.c
#include “stm32f10x.h”
#include “stdio.h”
#include “string.h”

#define PRINTF_ON 1

void RCC_Configuration(void);
void GPIO_Configuration(void);
void USART_Configuration(void);
void SPI_Configuration(void);

#define BufferSize 32

#define delay() for(i=0;i<200;i++)

SPI_InitTypeDef SPI_InitStructure;

u8 SPI1_Buffer_Tx[BufferSize] =
{
0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x10,
0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,
0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,0x20
};

u8 SPI2_Buffer_Tx[BufferSize] =
{
0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,
0x59,0x5A,0x5B,0x5C,0x5D,0x5E,0x5F,0x60,
0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,
0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F,0x70
};

u8 SPI1_Buffer_Rx[BufferSize+1] = {0};
u8 SPI2_Buffer_Rx[BufferSize] = {0};
u8 Tx_Counter = 0;
u8 Rx_Counter = 0;
u8 k=0,i=0;

int main(void)
{
RCC_Configuration();
GPIO_Configuration();
USART_Configuration();
SPI_Configuration();

while(Tx_Counter < BufferSize)
{
SPI_I2S_SendData(SPI2,SPI2_Buffer_Tx[Tx_Counter]); //必须从机先发送数据
//while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE)==RESET); //如果spi2 还有发送缓存则等待发送完成

SPI_I2S_SendData(SPI1,SPI1_Buffer_Tx[Tx_Counter]);

while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_RXNE) == RESET); //没有接收缓存则等待
SPI2_Buffer_Rx[Rx_Counter] = SPI_I2S_ReceiveData(SPI2);

while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE) ==RESET);

SPI1_Buffer_Rx[Rx_Counter] = SPI_I2S_ReceiveData(SPI1);

Tx_Counter++;
Rx_Counter++;
}

printf(“\r\n The SPI1 has sended data below : \r\n”);

while(k<BufferSize)
{
printf(“ %0.2d \r “,SPI1_Buffer_Tx[k]);
k++;
}

printf(“\r\n The SPI2 has received data below : \r\n”);

k=0;

while(k<BufferSize)
{
printf(“ %0.2d \r “,SPI2_Buffer_Rx[k]);
k++;
}

k=0;

printf(“\r\n The SPI2 has sended data below : \r\n”);

while(k<BufferSize)
{
printf(“ %0.2d \r “,SPI2_Buffer_Tx[k]);
k++;
}

printf(“\r\n The SPI1 has received data below : \r\n”);

k=0;

while(k<BufferSize)
{
printf(“ %0.2d \r “,SPI1_Buffer_Rx[k]);
k++;
}

while(1);
}

void SPI_Configuration(void)
{

SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;

SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_Init(SPI1,&SPI_InitStructure);

SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
SPI_Init(SPI2,&SPI_InitStructure);

SPI_Cmd(SPI1,ENABLE);
SPI_Cmd(SPI2,ENABLE);

}

void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA , &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB , &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA , &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA , &GPIO_InitStructure);
}

void RCC_Configuration(void)
{
/ 定义枚举类型变量 HSEStartUpStatus /
ErrorStatus HSEStartUpStatus;

/
复位系统时钟设置/
RCC_DeInit();
/
开启HSE/
RCC_HSEConfig(RCC_HSE_ON);
/
等待HSE起振并稳定/
HSEStartUpStatus = RCC_WaitForHSEStartUp();
/
判断HSE起是否振成功,是则进入if()内部 /
if(HSEStartUpStatus == SUCCESS)
{
/
选择HCLK(AHB)时钟源为SYSCLK 1分频 /
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/
选择PCLK2时钟源为 HCLK(AHB) 1分频 /
RCC_PCLK2Config(RCC_HCLK_Div1);
/
选择PCLK1时钟源为 HCLK(AHB) 2分频 /
RCC_PCLK1Config(RCC_HCLK_Div2);
/
设置FLASH延时周期数为2 /
FLASH_SetLatency(FLASH_Latency_2);
/
使能FLASH预取缓存 /
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/
选择锁相环(PLL)时钟源为HSE 1分频,倍频数为9,则PLL输出频率为 8MHz 9 = 72MHz /
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/ 使能PLL /
RCC_PLLCmd(ENABLE);
/ 等待PLL输出稳定 /
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
/ 选择SYSCLK时钟源为PLL /
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/ 等待PLL成为SYSCLK时钟源 /
while(RCC_GetSYSCLKSource() != 0x08);
}
/ 打开APB2总线上的GPIOA时钟/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_USART1|RCC_APB2Periph_SPI1, ENABLE);

//RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);
//RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR|RCC_APB1Periph_BKP|RCC_APB1Periph_WWDG, ENABLE);

}

void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;

USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
USART_ClockInit(USART1 , &USART_ClockInitStructure);

USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
USART_Init(USART1,&USART_InitStructure);

USART_Cmd(USART1,ENABLE);
}

#if PRINTF_ON

int fputc(int ch,FILE *f)
{
USART_SendData(USART1,(u8) ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC) == RESET);
return ch;
}

#endif