H753_CPP_W25Q_SPI_Flash_Read_Write_01 1.0
STM32H753 W25Q SPI flash (Adafruit 5632/5633/5634) read/write tutorial
Loading...
Searching...
No Matches
W25QFlashSPI.hpp
1/*
2 * W25QFlashSPI.hpp
3 *
4 * STM32 HAL SPI driver for the Winbond W25Q-series QSPI DIP breakout boards
5 * (Adafruit 5632/5633/5634 -- W25Q16/W25Q64/W25Q128), operated in standard
6 * single-line SPI mode.
7 *
8 * Adapted from Adafruit_SPIFlash (https://github.com/adafruit/Adafruit_SPIFlash,
9 * MIT License, Copyright (c) 2019 hathach for Adafruit Industries) as a design
10 * reference for the command set and driver structure -- this is an independent
11 * STM32 HAL implementation, not a direct port of Adafruit's Arduino source
12 * (which depends on Arduino-only types this toolchain doesn't have).
13 *
14 * Created on: Aug 30, 2026
15 * Author: johng
16 */
17
18#ifndef W25Q_W25QFLASHSPI_HPP_
19#define W25Q_W25QFLASHSPI_HPP_
20
21#include "main.h"
22#include "w25q_commands.h"
23
54public:
64 explicit W25QFlashSPI(uint32_t spiTimeoutMs = 100);
65
80 bool Init();
81
98 bool readJEDECID(uint32_t &id);
99
107 bool readStatus(uint8_t &status1, uint8_t &status2);
108
114 bool waitUntilReady(uint32_t timeout_ms = 100);
115
123 bool eraseSector(uint32_t addr);
124
157 bool eraseRange(uint32_t addr, uint32_t len);
158
166 bool eraseBlock(uint32_t addr);
167
176 bool eraseChip();
177
186 bool readBuffer(uint32_t addr, uint8_t *buf, uint32_t len);
187
201 bool writeBuffer(uint32_t addr, const uint8_t *data, uint32_t len);
202
203private:
204 SPI_HandleTypeDef *_hspi;
205
207 uint32_t _spiTimeoutMs;
208
224 void csEnable();
225
227 void csDisable();
228
233 HAL_StatusTypeDef writeEnable();
234
241 HAL_StatusTypeDef runCommand(uint8_t cmd);
242
251 HAL_StatusTypeDef readCommand(uint8_t cmd, uint8_t *resp, uint32_t len);
252
261 HAL_StatusTypeDef writeCommand(uint8_t cmd, const uint8_t *data, uint32_t len);
262
270 HAL_StatusTypeDef eraseCommand(uint8_t cmd, uint32_t addr);
271
278 void fillAddress(uint8_t *buf, uint32_t addr);
279};
280
281#endif /* W25Q_W25QFLASHSPI_HPP_ */
STM32 HAL SPI1 driver for a Winbond W25Q-series flash chip (Adafruit 5632/5633/5634),...
bool eraseSector(uint32_t addr)
Erases the single 4KB sector containing the given address.
W25QFlashSPI(uint32_t spiTimeoutMs=100)
Constructs the driver.
bool Init()
Resets the chip and verifies communication via the JEDEC ID.
bool eraseRange(uint32_t addr, uint32_t len)
Erases every sector spanned by [addr, addr + len), computing the sector boundaries automatically – th...
bool readBuffer(uint32_t addr, uint8_t *buf, uint32_t len)
Reads len bytes starting at addr into buf.
bool readStatus(uint8_t &status1, uint8_t &status2)
Reads both status registers.
bool eraseBlock(uint32_t addr)
Erases the single 64KB block containing the given address.
bool waitUntilReady(uint32_t timeout_ms=100)
Polls Status Register 1's BUSY bit until clear or timeout.
bool eraseChip()
Erases the entire chip. Can take tens of seconds on larger capacities – verify the timeout against th...
bool writeBuffer(uint32_t addr, const uint8_t *data, uint32_t len)
Writes len bytes starting at addr, paging internally at 256-byte boundaries as the chip's Page Progra...
bool readJEDECID(uint32_t &id)
Reads the 3-byte JEDEC ID (manufacturer, memory type, capacity).
: Header for main.c file. This file contains the common defines of the application.