[STM32] Initial use of SPI to read and write FLASH W25Q64

hardware connection

Insert image description here

(1) SS (Slave Select): Slave device selection signal line, often called chip select signal line. Each slave device has an independent NSS signal line. When the host wants to select a slave device, the slave device The NSS signal line is set to low level, the slave device is selected, that is, the chip select is valid, and then the host starts SPI communication with the selected slave device. Therefore, SPI communication starts with the NSS line being set low and ends with the NSS line being pulled high.

(2) SCK (Serial Clock): Clock signal line, used for communication data synchronization. It is generated by the communication host and determines the communication rate. Different devices support different maximum clock frequencies. For example, the maximum SPI clock frequency of STM32 is fpclk/2. When communicating between two devices, the communication rate is limited by the low-speed device. .

(3) MOSI (Master Output, Slave Input): Master device output/slave device input pin.

(4) MISO (Master Input,, Slave Output): Master device input/slave device output pin.

letter of agreement

Insert image description here

起始信号: The NSS signal line changes from high to low, which is the starting signal of SPI communication.
数据有效性:The data of MOSI and MISO 上升沿are output during the change of SCK 下降沿时被采样. That is, at the falling edge of SCK , the data of MOSI and MISO are valid . When the level is high, it means data "1", and when it is low, it means data "0".
CPOL/CPHA 及通讯模式: Generally speaking, the clock status of SCK and the data sampling time when the bus is idle are determined based on the configuration of CPOL/CPHA in the CR register. Modes 0 and 3 are generally used.
Insert image description here

Programming essentials

(1) Initialize the target pin and port clock used for communication;
(2) Enable the clock of the SPI peripheral;
(3) Configure the mode, address, rate and other parameters of the SPI peripheral and enable the SPI peripheral;
(4) Write basic SPI byte-based sending and receiving functions;
(5) Write functions for FLASH erasure, read and write operations;
(6) Write test programs to verify read and write data.

Configure GPIO multiplexing

//GPIO 配置
GPIO_InitTypeDef GPIO_InitStructure;
NSS片选引脚配置为普通GPIO
MISO MOSI SLK引脚配置为复用推挽输出
GPIO_Init(FLASH_SPI_MOSI_PORT, &GPIO_InitStructure);

Configure SPI

SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd (SPI1, ENABLE );

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_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(FLASH_SPIx, &SPI_InitStructure);

SPI_Cmd(FLASH_SPIx, ENABLE);

Use SPi to read and write FLASH W25Q64

Send and receive a byte of data using SPI

 #define Dummy_Byte  0xFF
u8 SPI_FLASH_SendByte(u8 byte)
{
    
    
	SPITimeout = SPIT_FLAG_TIMEOUT;
	/* 等待发送缓冲区为空,TXE 事件 */
	//读取SR寄存器 相关位
	while (SPI_I2S_GetFlagStatus(FLASH_SPIx, SPI_I2S_FLAG_TXE) == RESET)
	{
    
    
		if ((SPITimeout--) == 0) return SPI_TIMEOUT_UserCallback(0);	
	}
	
	/* 写入数据寄存器,把要写入的数据写入发送缓冲区 */
	//与大多数写DR寄存器操作一致,写动作会自动清空DR寄存器
	SPI_I2S_SendData(FLASH_SPIx, byte);

	SPITimeout = SPIT_FLAG_TIMEOUT;
	/* 等待接收缓冲区非空,RXNE 事件 */
	while (SPI_I2S_GetFlagStatus(FLASH_SPIx, SPI_I2S_FLAG_RXNE) == RESET)
	{
    
    
		if ((SPITimeout--) == 0) return SPI_TIMEOUT_UserCallback(1);
	}
	/* 读取数据寄存器,获取接收缓冲区数据 */
	//与大多数读DR寄存器操作一致,读动作会自动清空DR寄存器
	return SPI_I2S_ReceiveData(FLASH_SPIx);

}

Commands to control Flash

The FLASH chip has customized many instructions. We control the STM32 and use the SPI bus to send instructions to the FLASH chip. The FLASH chip will perform corresponding operations after receiving it.

Guess you like

Origin blog.csdn.net/apythonlearner/article/details/132604162