- #include "spi.h"
-
//注意,如果我们 交换 主从机,需要再次配置 主从机模式,修改 SPI_Configuration
// 这个程序还没有修改
-
void SPI_Configuration(void)
-
{
-
SPI_InitTypeDef SPI_InitStructure;
-
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;//全双工模式
-
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;//配置为主机
-
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;//8位数据帧
-
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;//时钟低时活动
-
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;//数据在第二个边沿捕获
-
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;//内部NSS信号由SSI控制
-
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;//预分频4
-
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;//传输的第一位是低位
-
SPI_InitStructure.SPI_CRCPolynomial = 7; //用于crc计算
-
SPI_Init(SPIy, &SPI_InitStructure); //初始化 SPI1
-
-
/* SPIz Config -------------------------------------------------------------*/
-
SPI_InitStructure.SPI_Mode = SPI_Mode_Slave; //SPI2 设置 从机
-
SPI_Init(SPIz, &SPI_InitStructure); //初始化 SPI2
-
-
SPI_Cmd(SPIy, ENABLE); //使能 SPIy = SPI1
-
SPI_Cmd(SPIz, ENABLE); //使能 SIPz = SPI2
-
-
//定义在 了 rcc中
-
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//SPI1
-
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//SPI2
-
// RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
-
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
-
-
}
-
-
void SPI_GPIO_Configuration(uint16_t SPIy_Mode, uint16_t SPIz_Mode)
-
{
-
GPIO_InitTypeDef GPIO_InitStructure;
-
-
/* Configure SPIy pins: SCK, MISO and MOSI ---------------------------------*/
-
GPIO_InitStructure.GPIO_Pin = SPIy_PIN_SCK | SPIy_PIN_MOSI;
-
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
-
-
if(SPIy_Mode == SPI_Mode_Master)
-
{
-
/* Configure SCK and MOSI pins as Alternate Function Push Pull */
-
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
-
}
-
else
-
{
-
/* Configure SCK and MOSI pins as Input Floating */
-
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
-
}
-
-
GPIO_Init(SPIy_GPIO, &GPIO_InitStructure);
-
GPIO_InitStructure.GPIO_Pin = SPIy_PIN_MISO;
-
-
if(SPIy_Mode == SPI_Mode_Master)
-
{
-
/* Configure MISO pin as Input Floating */
-
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
-
}
-
else
-
{
-
/* Configure MISO pin as Alternate Function Push Pull */
-
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
-
}
-
-
GPIO_Init(SPIy_GPIO, &GPIO_InitStructure);
-
-
/* Configure SPIz pins: SCK, MISO and MOSI ---------------------------------*/
-
GPIO_InitStructure.GPIO_Pin = SPIz_PIN_SCK | SPIz_PIN_MOSI;
-
-
if(SPIz_Mode == SPI_Mode_Slave)
-
{
-
/* Configure SCK and MOSI pins as Input Floating */
-
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
-
}
-
else
-
{
-
/* Configure SCK and MOSI pins as Alternate Function Push Pull */
-
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
-
}
-
-
GPIO_Init(SPIz_GPIO, &GPIO_InitStructure);
-
GPIO_InitStructure.GPIO_Pin = SPIz_PIN_MISO;
-
-
if(SPIz_Mode == SPI_Mode_Slave)
-
{
-
/* Configure MISO pin as Alternate Function Push Pull */
-
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
-
}
-
else
-
{ /* Configure MISO pin as Input Floating */
-
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
-
}
-
-
GPIO_Init(SPIz_GPIO, &GPIO_InitStructure);
-
}
-
-
//主设备 写数据到 从设备
-
void Master_SPI_Write_Byte(SPI_TypeDef* SPIx_Master, u8 byte)
-
{
-
while(SPI_I2S_GetFlagStatus(SPIx_Master, SPI_I2S_FLAG_TXE) == RESET);
-
SPI_I2S_SendData(SPIx_Master, byte);
-
}
-
-
//从设备 读取数据 from 主设备
-
u8 Slave_SPI_Read_Byte(SPI_TypeDef* SPIx_Slave)
-
{
-
while (SPI_I2S_GetFlagStatus(SPIx_Slave, SPI_I2S_FLAG_RXNE) == RESET);
-
return SPI_I2S_ReceiveData(SPIx_Slave);
-
}
-
-
//从设备 写数据到 主设备
-
void Slave_SPI_Write_Byte(SPI_TypeDef* SPIx_Slave, u8 byte)
-
{
-
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
-
SPI_I2S_SendData(SPIx_Slave, byte);
-
}
-
-
//主设备 读取数据 from 从设备
-
u8 Master_SPI_Read_Byte(SPI_TypeDef* SPIx_Master)
-
{
-
//写 哑元字节dumy
-
// Master_SPI_Write_Byte(SPIx_Master, 0x00);
-
SPI_I2S_SendData(SPIx_Master, 0x00);//产生读时的clk
-
while(SPI_I2S_GetFlagStatus(SPIx_Master, SPI_I2S_FLAG_RXNE) == RESET);
-
return SPI_I2S_ReceiveData(SPIx_Master);
-
}