新手上路
- 积分
- 45
- 金钱
- 45
- 注册时间
- 2023-2-5
- 在线时间
- 8 小时
|
1金钱
static void SPI_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* 使能与 SPI有关的时钟 */
FLASH_SPI_APBxClock_FUN( FLASH_SPI_CLK, ENABLE );
FLASH_SPI_GPIO_APBxClock_FUN ( FLASH_SPI_GPIO_CLK, ENABLE );
/* SCK MOSI MISO*/
GPIO_InitStructure.GPIO_Pin = FLASH_SPI_SCK_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 推挽复用输出
GPIO_Init(FLASH_SPI_SCK_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = FLASH_SPI_MOSI_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 推挽复用输出
GPIO_Init(FLASH_SPI_MOSI_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = FLASH_SPI_MISO_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
GPIO_Init(FLASH_SPI_MISO_PORT, &GPIO_InitStructure);
//初始化CS引脚,使用软件控制,所以直接设置成推挽输出
GPIO_InitStructure.GPIO_Pin = FLASH_SPI_CS_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // 推挽输出
GPIO_Init(FLASH_SPI_CS_PORT, &GPIO_InitStructure);
FLASH_SPI_CS_HIGH;
}
/**
* @brief SPI 工作模式配置
* @param 无
* @retval 无
*/
static void SPI_Mode_Config(void)
{
SPI_InitTypeDef SPI_InitStructure;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
//spi使用模式3
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CRCPolynomial = 0; //不使用CRC功能
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //软件控制
SPI_Init(FLASH_SPIx,&SPI_InitStructure); //写入配置到寄存器
SPI_Cmd(FLASH_SPIx,ENABLE); //使能SPI
}
/**
* @brief I2C 外设(EEPROM)初始化
* @param 无
* @retval 无
*/
void SPI_FLASH_Init(void)
{
SPI_GPIO_Config();
SPI_Mode_Config();
// I2C_Mode_Configu();
}
//发送一个字节
uint8_t SPI_FLASH_Send_Byte(uint8_t data)
{
SPITimeout = SPIT_FLAG_TIMEOUT;
//检查并等待TX缓冲区为空
while(SPI_I2S_GetFlagStatus(FLASH_SPIx,SPI_I2S_FLAG_TXE)==RESET) //reset表示发送缓冲非空
{
if((SPITimeout--) == 0) return SPI_TIMEOUT_UserCallback(0);
}
//程序执行到此处,TX缓冲区已空
SPI_I2S_SendData(FLASH_SPIx,data);
SPITimeout = SPIT_FLAG_TIMEOUT;
//检查并等待至TX缓冲区为非空
while(SPI_I2S_GetFlagStatus(FLASH_SPIx,SPI_I2S_FLAG_RXNE)==RESET) //1表示接收缓冲非空
{
if((SPITimeout--) == 0) return SPI_TIMEOUT_UserCallback(0);
}
//程序执行到此处,说明效据发送完毕,并接收到一字字节
return SPI_I2S_ReceiveData(FLASH_SPIx);
}
uint8_t SPI_FLASH_Read_Byte(void)
{
return SPI_FLASH_Send_Byte(0x00);
}
//读取ID号
uint32_t SPI_Read_ID(void)
{
uint32_t flash_id;
//片选使能
FLASH_SPI_CS_LOW;
SPI_FLASH_Send_Byte(READ_JEDEG_ID);
flash_id=SPI_FLASH_Send_Byte(DUMMY);
flash_id <<=8;
flash_id |= SPI_FLASH_Send_Byte(DUMMY);
flash_id <<=8;
flash_id |= SPI_FLASH_Send_Byte(DUMMY);
FLASH_SPI_CS_HIGH;
return flash_id;
}
/**
* @brief Basic management of the timeout situation.
* @param errorCode:错误代码,可以用来定位是哪个环节出错.
* @retval 返回0,表示SPI读取失败.
*/
static uint32_t SPI_TIMEOUT_UserCallback(uint8_t errorCode)
{
/* Block communication and all processes */
FLASH_ERROR("I2C 等待超时!errorCode = %d",errorCode);
return 0;
}
/********************************************
|
|