Namespace: DadDrivers::cW25Q128 Files: W25Q128.h / W25Q128.cpp Directory: DAD_FORGE/Drivers Description: Complete implementation of the Winbond W25Q128JV (16MB) QSPI flash controller with Quad I/O and memory mapping support
📋 Class Description
The cW25Q128 class implements the iQSPI_FlashMemory interface for the Winbond W25Q128JV-IQ/JQ QSPI flash chip (16 MB, 16777216 bytes). It supports Quad I/O mode operations (4 input/output lines), direct memory mapping for fast CPU access, and all standard Winbond datasheet commands. Initialization includes software reset, status register configuration (block protection, driver strength, quad enable), and automatic Quad I/O mode activation. The class automatically handles transitions between memory-mapped and indirect modes based on required operations.
Initializes the QSPI controller and W25Q128JV flash memory. Performs a complete software reset (Enable Reset + Reset Device), configures status registers 1, 2, and 3 (disables block protection, enables Quad mode, configures driver strength), waits for all operations to complete (WaitWhileBusy), then activates memory-mapped mode. In DualMode, prepares configuration for two flash chips in parallel.
Parameter(s)
Â
phqspi
Pointer to the configured HAL QSPI handle
DualMode
Flag enabling dual flash mode (two W25Q128 chips in parallel), default false
MemoryMappedBaseAddress
Base address for memory mapping in CPU address space, default 0x90000000
Return
HAL status code: HAL_OK success, HAL_ERROR initialization failure
ModeMemoryMap
Element
Details
Method
HAL_StatusTypeDef ModeMemoryMap()
Description
Configures the flash memory in mapped mode for direct CPU access. Flushes CPU caches (ICache and DCache) on the mapped region. The CPU can then read the entire flash capacity as if it were RAM.
Parameter(s)
None
Return
HAL status code: HAL_OK success, HAL_ERROR configuration failure
ModeIndirect
Element
Details
Method
HAL_StatusTypeDef ModeIndirect()
Description
Restores indirect access mode after using memory mapping. Aborts the memory-mapped session, returns to standard command mode where each operation (READ, WRITE, ERASE) requires an explicit QSPI instruction. Required before any write or erase operation.
Parameter(s)
None
Return
HAL status code: HAL_OK success, HAL_ERROR configuration failure
Reads data from the W25Q128 flash memory in Quad I/O mode. The output buffer must be allocated by the caller. If in mapped mode, switches to indirect mode then returns to mapped mode after reading.
Parameter(s)
Â
pData
Pointer to the read buffer where data will be stored
Address
Starting read address in the flash memory (Memory-mapped address)
NbData
Number of bytes to read
Return
HAL status code: HAL_OK success, HAL_TIMEOUT read timeout, HAL_ERROR error
Writes data to the W25Q128 flash memory in Quad I/O mode. Uses the Quad Input Page Program command (0x32). If in mapped mode, switches to indirect mode then returns to mapped mode after writing.
Parameter(s)
Â
pData
Pointer to the write buffer containing data to be programmed
Address
Starting write address in the flash memory (Memory-mapped address)
NbData
Number of bytes to write
Return
HAL status code: HAL_OK success, HAL_TIMEOUT programming timeout, HAL_ERROR error
EraseBlock4K
Element
Details
Method
HAL_StatusTypeDef EraseBlock4K(uint32_t Address)
Description
Erases a 4 KB sector (or 8 KB in DualMode) of the W25Q128 flash memory. After erasure, all bits in the sector are set to 1.
Parameter(s)
Â
Address
Starting address of the sector to erase (Memory-mapped address)
Return
HAL status code: HAL_OK success, HAL_TIMEOUT erase timeout, HAL_ERROR error
EraseBlock32K
Element
Details
Method
HAL_StatusTypeDef EraseBlock32K(uint32_t Address)
Description
Erases a 32 KB block (or 64 KB in DualMode) of the W25Q128 flash memory. After erasure, all bits in the block are set to 1.
Parameter(s)
Â
Address
Starting address of the block to erase (Memory-mapped address)
Return
HAL status code: HAL_OK success, HAL_TIMEOUT erase timeout, HAL_ERROR error
EraseBlock64K
Element
Details
Method
HAL_StatusTypeDef EraseBlock64K(uint32_t Address)
Description
Erases a 64 KB block (or 128 KB in DualMode) of the W25Q128 flash memory. After erasure, all bits in the block are set to 1.
Parameter(s)
Â
Address
Starting address of the block to erase (Memory-mapped address)
Return
HAL status code: HAL_OK success, HAL_TIMEOUT erase timeout, HAL_ERROR error
EraseChip
Element
Details
Method
HAL_StatusTypeDef EraseChip()
Description
Erases the entire W25Q128JV flash chip (16 MB). After erasure, all bits in the flash memory are set to 1.
Parameter(s)
None
Return
HAL status code: HAL_OK success, HAL_TIMEOUT chip erase timeout, HAL_ERROR error
getSize
Element
Details
Method
uint32_t getSize() const
Description
Returns the total size of the W25Q128 flash memory: 16777216 bytes (16 MB). Constant value based on the W25Q128JV model. Useful for address validation and operation boundaries.
Parameter(s)
None
Return
uint32_t value: 16777216 (16 MB in bytes)
getFlashID
Element
Details
Method
HAL_StatusTypeDef getFlashID(FlashID* pID)
Description
Reads the W25Q128 device identification. Sends the Read ID command (0xAB) and reads the three identification bytes: Manufacturer ID (0xEF for Winbond), Device Type (0x40 or 0x70 depending on JV-IM/JV-IQ variant), Capacity (0x18 = 16 MB).
Parameter(s)
Â
pID
Pointer to the FlashID structure to be filled with identification
Return
HAL status code: HAL_OK success, HAL_ERROR read failed
🔒 Protected/Private Methods
No protected or private methods.
📦 Data Members (Variables)
Public Variables
No public variables.
Protected/Private Variables
Member
Type
Description
m_pQSPI
QSPI_HandleTypeDef*
Pointer to the HAL QSPI handle configured for the W25Q128 chip
m_MemoryMappedBaseAddress
uint32_t
Base address for memory mapping (0x90000000 by default)
m_MappedMode
bool
Flag indicating whether memory is in direct mapped mode (true) or indirect command mode (false)
m_DualMode
bool
Flag enabling dual flash mode (two W25Q128 chips in parallel)
💡 Usage Example
#include"W25Q128.h"usingnamespaceDadDrivers;intmain(){QSPI_HandleTypeDefhqspi;// Configured by HAL_QSPI_Init// W25Q128JV flash initializationcW25Q128flash;if(flash.Init(&hqspi,false,0x90000000)!=HAL_OK){Error_Handler();}// Identification verificationFlashIDid;if(flash.getFlashID(&id)==HAL_OK){printf("Manufacturer: 0x%02X\n",id.ManufactuerID);// 0xEF = Winbondprintf("Device ID: 0x%02X %02X\n",id.MemoryType,id.Capacity);// 0x4018 or 0x7018}// Data write (page size = 256 bytes)uint8_tdata[256]={0xAA,0x55,0x88,0x00};// Test patternif(flash.Write(data,0x90000000,256)==HAL_OK){printf("Write completed\n");}// Data read from flashuint8_tbuffer[256];if(flash.Read(buffer,0x90000000,256)==HAL_OK){printf("Read: %02X %02X %02X %02X\n",buffer[0],buffer[1],buffer[2],buffer[3]);}// 32K block eraseif(flash.EraseBlock32K(0x90000000)==HAL_OK){printf("32K block erased\n");}// Memory mapping for fast CPU accessif(flash.ModeMemoryMap()==HAL_OK){// Direct read from mapped address (as if it were RAM)uint8_t*mappedAddr=(uint8_t*)0x90000000;*mappedAddr=0xDEADBEEF;// Direct writeprintf("Read value: %02X\n",*mappedAddr);// Return to indirect mode before erase/programmingflash.ModeIndirect();}return0;}
DAD_FORGE/DSP - Dad Design DSP Library Copyright (c) 2024-2026 Dad Design.