Welcome to STM32-BoardCar repository
13. Off-chip Flash (SPI)
Off-chip Flash
Off-chip Flash1、Software-Hardware2、Brief principle2.1、Hardware schematic diagram2.2、Physical connection diagram2.3、Principle of control3、Engineering configuration3.1、Notes3.2、Pin configuration4、Main function4.1、User function4.2、HAL library function parsing5、Experimental phenomenon
This tutorial demonstrates: using SPI (SPI2) to control the external Flash (W25Q64) to read and write data, and print the data through the serial port (USART1)
1、Software-Hardware
STM32F103CubeIDE
STM32 Robot Development board
USART: Chip internal peripheral
Key, external Flash (W25Q64) : onboard
Type-C cable or ST-Link
Download or simulate the program of the development board
2、Brief principle
2.1、Hardware schematic diagram

2.2、Physical connection diagram


2.3、Principle of control
Write, read and erase the data of external flash by key control, and print the data through the serial port.
- GPIO reading
By defining different functions for the key, the read and write operation of data in W25Q64 is realized.
| KEY(Schematic name) | Control pin | Function |
|---|---|---|
| KEY1 | PG3 | Add 1 to the count |
| KEY2 | PG4 | Reading count values |
| KEY3 | PG5 | Erasing data |
- External Flash
W25Q64 divides the capacity of 8M bytes into 128 blocks of 64K bytes each, and each block is further divided into 16 sectors of 4K bytes each. The minimum erasing unit for W25Q64 is one sector, which means that 4K bytes must be erased at a time.
Instruction list(truncated)

Instructions for detailed use of the reference [W25Q64JV_ English Reference Manual]
- SPI
Serial Peripheral Interface (SPI) is a high-speed, full-duplex, synchronous serial communication interface, which is often used to transfer data between microcontrollers, sensors and peripheral devices.
| SPI pins | Function |
|---|---|
| MISO | Master device data output line, from the master device to the slave device to send data |
| MOSI | The master device data input line transfers data from the slave device to the master device |
| SCLK | Clock signal line for timing control of synchronous data transmission |
| CS | The slave selection line is used to select a specific slave device to communicate with the master device |
- SPI transmission mode
Polarity of clock(CPOL):Controls the idle and active states of the clock signal (0: idle low, 1: idle high)
Phase of clock(CPHA):Sampling and transmission time of control data (0: sampling along odd hops, 1: sampling along even hops)
| Polarity of clock(CPOL) | Phase of clock(CPHA) | |
|---|---|---|
| 0 | 0 | SCL idle state low level; The data is sampled on the rising edge and transmitted on the falling edge |
| 0 | 1 | SCL idle state low level; Data is sampled on the falling edge and data is transmitted on the rising edge |
| 1 | 0 | SCL idle state high level; Data is sampled on the falling edge and data is transmitted on the rising edge |
| 1 | 1 | SCL idle state high level; The data is sampled on the rising edge and transmitted on the falling edge |

3、Engineering configuration
Project Configuration: Prompts for configuration options in the STM32CubeIDE project configuration process
3.1、Notes
Omitted project configuration: New project, chip selection, project configuration, SYS for pin configuration, RCC configuration, clock configuration, and project configuration content
The project configuration part, which is not omitted, is the key point to configure in this tutorial.
xxxxxxxxxxPlease refer to [2. Development environment construction and use: STM32CubeIDE installation and use] to understand how to configure the omitted parts of the project.
3.2、Pin configuration
- Configure the specified pin function
You can directly select the corresponding pin number in the pin view, and the corresponding option will appear when the mouse is left clicked

- GPIO

- SPI


- USART


NVIC
NVIC可修改中断优先级

- Generating code

4、Main function
This section mainly introduces the functional code written by users. ** Detailed code can be opened by yourself in the project file we provide, and enter the Bsp folder to view the source code. **
4.1、User function
function:Spi_Flash_ReadID
| Function prototypes | u16 Spi_Flash_ReadID(void) |
|---|---|
| Functional Description | Read the W25Q64 chip ID |
| Input parameters | None |
| Return value | ID number |
function:Spi_Flash_Erase_Sector
| Function prototypes | void Spi_Flash_Erase_Sector(u32 Dst_Addr) |
|---|---|
| Functional Description | Erases a sector |
| Input parameters | Sector address |
| Return value | None |
function:Spi_Flash_Write
| Function prototypes | void Spi_Flash_Write(u8 *pBuffer, u32 WriteAddr, u16 NumByteToWrite) |
|---|---|
| Functional Description | Writes data of the specified length starting at the specified address |
| Input parameters1 | Data storage area |
| Input parameters2 | Address to write to |
| Input parameters3 | Number of bytes written |
| Return value | None |
function:Spi_Flash_Read
| Function prototypes | void Spi_Flash_Read(u8 *pBuffer, u32 ReadAddr, u16 NumByteToRead) |
|---|---|
| Functional Description | Reads data of the specified length starting at the specified address |
| Input parameters1 | Data storage area |
| Input parameters2 | Read address |
| Input parameters3 | The number of bytes read |
| Return value | None |
4.2、HAL library function parsing
The HAL library functions that were covered in the previous tutorial will not be covered
xxxxxxxxxxIf you want to find the HAL library and LL library function analysis involved in the entire tutorial, you can view the documents in the folder [8. STM32 Manual: STM32F1_HAL Library and LL Library_User Manual]
function:HAL_SPI_Init
| Function prototypes | HAL_StatusTypeDef HAL_SPI_Init(SPI_HandleTypeDef *hspi) |
|---|---|
| Functional Description | Initialize the SPI parameters |
| Input parameters | hspi:SPI handle address |
| Return value | HAL status value:HAL_OK、HAL_ERROR、HAL_BUSY、HAL_TIMEOUT |
function:HAL_SPI_MspInit
| Function prototypes | void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi) |
|---|---|
| Functional Description | Initialize the peripheral clock, GPIO, and NVIC of SPI |
| Input parameters | hspi:SPI handle address |
| Return value | None |
function:HAL_SPI_MspDeInit
| Function prototypes | void HAL_SPI_MspDeInit(SPI_HandleTypeDef *hspi) |
|---|---|
| Functional Description | Uninitialize the SPI peripheral clock, GPIO, and NVIC |
| Input parameters | hspi:SPI handle address |
| Return value | None |
5、Experimental phenomenon
After downloading the program successfully, press the RESET button of the development board to open the serial debugging assistant to observe the phenomenon
xxxxxxxxxxFor program download, please refer to [2. Development environment construction and use: program download and simulation]
phenomenon:
Press RESET button: the serial debugging assistant will print the detection status of the external Flash chip;
Press KEY1: The serial debugging assistant will add 1 to the count value and write the value to the external Flash chip;
Press KEY2: The serial debugging assistant will output the numerical value stored in the specified sector of the external Flash chip;
Press KEY3: The serial debugging assistant will erase the specified sector data.
