#include "TestSPI1.h"
#include <stdio.h>
/* Private define ------------------------------------------------------------*/
//#define PRINT_INFO 1
/*******************************************************************************
* Function Name : MSD0_spi_read_write
* Description : None
* Input : - data:
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
int SPI1_Read_Write(uint8_t data)
{
//< Loop while DR register in not emplty
while (SPI_I2S_GetFlagStatus(SPIX, SPI_I2S_FLAG_TXE) == RESET);
//< Send byte through the SPI1 peripheral
SPI_I2S_SendData(SPIX, data);
//< Wait to receive a byte
while (SPI_I2S_GetFlagStatus(SPIX, SPI_I2S_FLAG_RXNE) == RESET);
// < Return the byte read from the SPI bus
return SPI_I2S_ReceiveData(SPIX);
}
/*******************************************************************************
* Function Name : MSD0_SPI_Configuration
* Description : SD Card SPI Configuration
* Input : None
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void SPI1_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//SPI模块时钟使能
SPIX_CLK_INIT(SPIX_CLK, ENABLE);
//IO口时钟使能
RCC_AHB1PeriphClockCmd(SPIX_SCK_GPIO_CLK | SPIX_MISO_GPIO_CLK |
SPIX_MOSI_GPIO_CLK | SPIX_CS_GPIO_CLK, ENABLE);
//设置SPI引脚利用功能
GPIO_PinAFConfig(SPIX_SCK_GPIO_PORT, SPIX_SCK_SOURCE, SPIX_SCK_AF);
GPIO_PinAFConfig(SPIX_MISO_GPIO_PORT, SPIX_MISO_SOURCE, SPIX_MISO_AF);
GPIO_PinAFConfig(SPIX_MOSI_GPIO_PORT, SPIX_MOSI_SOURCE, SPIX_MOSI_AF);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
/*!< SPI SCK pin configuration */
GPIO_InitStructure.GPIO_Pin = SPIX_SCK_PIN;
GPIO_Init(SPIX_SCK_GPIO_PORT, &GPIO_InitStructure);
/*!< SPI MOSI pin configuration */
GPIO_InitStructure.GPIO_Pin = SPIX_MOSI_PIN;
GPIO_Init(SPIX_MOSI_GPIO_PORT, &GPIO_InitStructure);
/*!< SPI MISO pin configuration */
GPIO_InitStructure.GPIO_Pin = SPIX_MISO_PIN;
GPIO_Init(SPIX_MISO_GPIO_PORT, &GPIO_InitStructure);
/*!< Configure sFLASH Card CS pin in output pushpull mode ********************/
GPIO_InitStructure.GPIO_Pin = SPIX_CS_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(SPIX_CS_GPIO_PORT, &GPIO_InitStructure);
//关闭片选
SPI1_disable();
//设置SPI接口为低俗模式
SPI1_SetSpeed(0);
//使能SPI模块
SPI_Cmd(SPIX, ENABLE);
}
/*******************************************************************************
* Function Name : MSD0_SPIHighSpeed
* Description : SD Card Speed Set
* Input : - b_high: 1 = 18MHz, 0 = 281.25Hz
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void SPI1_SetSpeed(uint8_t b_high)
{
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;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
/* Speed select */
if(b_high == 0)
{
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
}
else
{
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
}
SPI_Init(SPIX, &SPI_InitStructure);
}
评论0
最新资源