STM32 W25Qxx QuadSPI Driver  0.1a
https://github.com/Crazy-Geeks/STM32-W25Q-QSPI
w25q_mem.h
Go to the documentation of this file.
1 
15 #ifndef W25Q_QSPI_W25Q_MEM_H_
16 #define W25Q_QSPI_W25Q_MEM_H_
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 #include "libs.h"
23 
35 // YOUR CHIP'S SETTINGS
37 #define MEM_FLASH_SIZE 256U // 256 MB-bit
39 #define MEM_BLOCK_SIZE 64U // 64 KB: 256 pages
41 #define MEM_SBLOCK_SIZE 32U // 32 KB: 128 pages
43 #define MEM_SECTOR_SIZE 4U // 4 KB : 16 pages
45 #define MEM_PAGE_SIZE 256U // 256 byte : 1 page
47 #define BLOCK_COUNT (MEM_FLASH_SIZE * 2) // 512 blocks
49 #define SECTOR_COUNT (BLOCK_COUNT * 16) // 8192 sectors
51 #define PAGE_COUNT (SECTOR_COUNT * 16) // 131'072 pages
52 
61 typedef enum{
62  W25Q_OK = 0,
63  W25Q_BUSY = 1,
68 }W25Q_STATE;
79 typedef struct{
80  bool BUSY;
81  bool WEL;
82  bool QE;
83  bool SUS;
84  bool ADS;
85  bool ADP;
86  bool SLEEP;
91 W25Q_STATE W25Q_Init(void);
92 
94 W25Q_STATE W25Q_ReadStatusReg(u8_t *reg_data, u8_t reg_num);
95 W25Q_STATE W25Q_WriteStatusReg(u8_t reg_data, u8_t reg_num);
97 W25Q_STATE W25Q_IsBusy(void);
98 
99 W25Q_STATE W25Q_ReadSByte(i8_t *buf, u8_t pageShift, u32_t pageNum);
100 W25Q_STATE W25Q_ReadByte(u8_t *buf, u8_t pageShift, u32_t pageNum);
101 W25Q_STATE W25Q_ReadSWord(i16_t *buf, u8_t pageShift, u32_t pageNum);
102 W25Q_STATE W25Q_ReadWord(u16_t *buf, u8_t pageShift, u32_t pageNum);
103 W25Q_STATE W25Q_ReadSLong(i32_t *buf, u8_t pageShift, u32_t pageNum);
104 W25Q_STATE W25Q_ReadLong(u32_t *buf, u8_t pageShift, u32_t pageNum);
105 W25Q_STATE W25Q_ReadData(u8_t *buf, u16_t len, u8_t pageShift, u32_t pageNum);
106 W25Q_STATE W25Q_ReadRaw(u8_t *buf, u16_t data_len, u32_t rawAddr);
107 W25Q_STATE W25Q_SingleRead(u8_t *buf, u32_t len, u32_t Addr);
108 
110 W25Q_STATE W25Q_EraseBlock(u32_t BlockAddr, u8_t size);
112 
113 W25Q_STATE W25Q_ProgramSByte(i8_t buf, u8_t pageShift, u32_t pageNum);
114 W25Q_STATE W25Q_ProgramByte(u8_t buf, u8_t pageShift, u32_t pageNum);
115 W25Q_STATE W25Q_ProgramSWord(i16_t buf, u8_t pageShift, u32_t pageNum);
116 W25Q_STATE W25Q_ProgramWord(u16_t buf, u8_t pageShift, u32_t pageNum);
117 W25Q_STATE W25Q_ProgramSLong(i32_t buf, u8_t pageShift, u32_t pageNum);
118 W25Q_STATE W25Q_ProgramLong(u32_t buf, u8_t pageShift, u32_t pageNum);
119 W25Q_STATE W25Q_ProgramData(u8_t *buf, u16_t len, u8_t pageShift, u32_t pageNum);
120 W25Q_STATE W25Q_ProgramRaw(u8_t *buf, u16_t data_len, u32_t rawAddr);
121 
123 
126 
127 W25Q_STATE W25Q_Sleep(void);
128 W25Q_STATE W25Q_WakeUP(void);
129 
135 
137 W25Q_STATE W25Q_ProgSecurityRegisters(u8_t *buf, u8_t numReg, u8_t byteAddr);
138 W25Q_STATE W25Q_ReadSecurityRegisters(u8_t *buf, u8_t numReg, u8_t byteAddr);
139 
140 W25Q_STATE W25Q_BlockReadOnly(u32_t Addr, bool enable);
141 W25Q_STATE W25Q_BlockReadOnlyCheck(bool *state, u32_t Addr);
142 W25Q_STATE W25Q_GlobalReadOnly(bool enable);
143 
144 W25Q_STATE W25Q_SwReset(bool force);
145 
146 
152 #define W25Q_WRITE_ENABLE 0x06U
153 #define W25Q_WRITE_DISABLE 0x04U
154 #define W25Q_ENABLE_VOLATILE_SR 0x50U
155 #define W25Q_READ_SR1 0x05U
156 #define W25Q_READ_SR2 0x35U
157 #define W25Q_READ_SR3 0x15U
158 #define W25Q_WRITE_SR1 0x01U
159 #define W25Q_WRITE_SR2 0x31U
160 #define W25Q_WRITE_SR3 0x11U
161 #define W25Q_READ_EXT_ADDR_REG 0xC8U
162 #define W25Q_WRITE_EXT_ADDR_REG 0xC8U
163 #define W25Q_ENABLE_4B_MODE 0xB7U
164 #define W25Q_DISABLE_4B_MODE 0xE9U
165 #define W25Q_READ_DATA 0x03U
166 #define W25Q_READ_DATA_4B 0x13U
167 #define W25Q_FAST_READ 0x0BU
168 #define W25Q_FAST_READ_4B 0x0CU
169 #define W25Q_FAST_READ_DUAL_OUT 0x3BU
170 #define W25Q_FAST_READ_DUAL_OUT_4B 0x3CU
171 #define W25Q_FAST_READ_QUAD_OUT 0x6BU
172 #define W25Q_FAST_READ_QUAD_OUT_4B 0x6CU
173 #define W25Q_FAST_READ_DUAL_IO 0xBBU
174 #define W25Q_FAST_READ_DUAL_IO_4B 0xBCU
175 #define W25Q_FAST_READ_QUAD_IO 0xEBU
176 #define W25Q_FAST_READ_QUAD_IO_4B 0xECU
177 #define W25Q_SET_BURST_WRAP 0x77U
178 #define W25Q_PAGE_PROGRAM 0x02U
179 #define W25Q_PAGE_PROGRAM_4B 0x12U
180 #define W25Q_PAGE_PROGRAM_QUAD_INP 0x32U
181 #define W25Q_PAGE_PROGRAM_QUAD_INP_4B 0x34U
182 #define W25Q_SECTOR_ERASE 0x20U
183 #define W25Q_SECTOR_ERASE_4B 0x21U
184 #define W25Q_32KB_BLOCK_ERASE 0x52U
185 #define W25Q_64KB_BLOCK_ERASE 0xD8U
186 #define W25Q_64KB_BLOCK_ERASE_4B 0xDCU
187 #define W25Q_CHIP_ERASE 0xC7U
188 //#define W25Q_CHIP_ERASE 0x60U ///< another way to erase chip
189 #define W25Q_ERASEPROG_SUSPEND 0x75U
190 #define W25Q_ERASEPROG_RESUME 0x7AU
191 #define W25Q_POWERDOWN 0xB9U
192 #define W25Q_POWERUP 0xABU
193 #define W25Q_DEVID 0xABU
194 #define W25Q_FULLID 0x90U
195 #define W25Q_FULLID_DUAL_IO 0x92U
196 #define W25Q_FULLID_QUAD_IO 0x94U
197 #define W25Q_READ_UID 0x4BU
198 #define W25Q_READ_JEDEC_ID 0x9FU
199 #define W25Q_READ_SFDP 0x5AU
200 #define W25Q_ERASE_SECURITY_REG 0x44U
201 #define W25Q_PROG_SECURITY_REG 0x42U
202 #define W25Q_READ_SECURITY_REG 0x48U
203 #define W25Q_IND_BLOCK_LOCK 0x36U
204 #define W25Q_IND_BLOCK_UNLOCK 0x39U
205 #define W25Q_READ_BLOCK_LOCK 0x3DU
206 #define W25Q_GLOBAL_LOCK 0x7EU
207 #define W25Q_GLOBAL_UNLOCK 0x98U
208 #define W25Q_ENABLE_RST 0x66U
209 #define W25Q_RESET 0x99U
211 
213 
214 #ifdef __cplusplus
215 }
216 #endif
217 
218 #endif /* W25Q_QSPI_W25Q_MEM_H_ */
W25Q_STATE W25Q_ReadData(u8_t *buf, u16_t len, u8_t pageShift, u32_t pageNum)
Read any 8-bit data.
Definition: w25q_mem.c:464
W25Q_STATE W25Q_ReadStatusReg(u8_t *reg_data, u8_t reg_num)
Read status register to variable.
Definition: w25q_mem.c:156
W25Q_STATE W25Q_ReadJEDECID(u8_t *buf)
Read ID by JEDEC Standards.
Definition: w25q_mem.c:1228
W25Q_STATE W25Q_ReadFullID(u8_t *buf)
Read full chip ID (Manufacturer ID + Device ID)
Definition: w25q_mem.c:1204
W25Q_STATE W25Q_Sleep(void)
Set low current consumption.
Definition: w25q_mem.c:1078
W25Q_STATE W25Q_ReadSLong(i32_t *buf, u8_t pageShift, u32_t pageNum)
Read signed 32-bit variable.
Definition: w25q_mem.c:418
W25Q_STATE W25Q_ProgramSByte(i8_t buf, u8_t pageShift, u32_t pageNum)
Program signed 8-bit variable.
Definition: w25q_mem.c:601
W25Q_STATE W25Q_EraseChip(void)
Erase all chip.
Definition: w25q_mem.c:931
W25Q_STATE W25Q_ProgramData(u8_t *buf, u16_t len, u8_t pageShift, u32_t pageNum)
Program any 8-bit data.
Definition: w25q_mem.c:715
W25Q_STATE W25Q_ReadStatusStruct(W25Q_STATUS_REG *status)
Read all status registers to struct.
Definition: w25q_mem.c:262
W25Q_STATE W25Q_EraseSector(u32_t SectAddr)
Erase 4KB Sector.
Definition: w25q_mem.c:800
W25Q_STATE W25Q_ProgramLong(u32_t buf, u8_t pageShift, u32_t pageNum)
Program 32-bit variable.
Definition: w25q_mem.c:694
W25Q_STATE W25Q_ProgSecurityRegisters(u8_t *buf, u8_t numReg, u8_t byteAddr)
Program security register.
Definition: w25q_mem.c:1273
W25Q_STATE W25Q_ReadID(u8_t *buf)
Read chip ID.
Definition: w25q_mem.c:1163
W25Q_STATE W25Q_EraseBlock(u32_t BlockAddr, u8_t size)
Erase 32KB/64KB Sector.
Definition: w25q_mem.c:858
W25Q_STATE W25Q_BlockReadOnlyCheck(bool *state, u32_t Addr)
Check block's/sector's read-only lock status.
Definition: w25q_mem.c:1320
W25Q_STATE W25Q_SwReset(bool force)
Software reset.
Definition: w25q_mem.c:1350
W25Q_STATE W25Q_ReadSecurityRegisters(u8_t *buf, u8_t numReg, u8_t byteAddr)
Read security register.
Definition: w25q_mem.c:1287
W25Q_STATE W25Q_ReadByte(u8_t *buf, u8_t pageShift, u32_t pageNum)
Read 8-bit variable.
Definition: w25q_mem.c:353
W25Q_STATE W25Q_ReadUID(u8_t *buf)
Read unique chip ID.
Definition: w25q_mem.c:1216
W25Q_STATE W25Q_ReadSByte(i8_t *buf, u8_t pageShift, u32_t pageNum)
Read signed 8-bit variable.
Definition: w25q_mem.c:332
W25Q_STATE W25Q_ReadRaw(u8_t *buf, u16_t data_len, u32_t rawAddr)
Read data from raw addr.
Definition: w25q_mem.c:483
W25Q_STATE W25Q_WakeUP(void)
Wake the chip up from sleep mode.
Definition: w25q_mem.c:1118
W25Q_STATE W25Q_ReadSWord(i16_t *buf, u8_t pageShift, u32_t pageNum)
Read signed 16-bit variable.
Definition: w25q_mem.c:374
W25Q_STATE W25Q_ProgramSWord(i16_t buf, u8_t pageShift, u32_t pageNum)
Program signed 16-bit variable.
Definition: w25q_mem.c:637
W25Q_STATE W25Q_ProgramRaw(u8_t *buf, u16_t data_len, u32_t rawAddr)
Program data to raw addr.
Definition: w25q_mem.c:734
W25Q_STATE W25Q_SingleRead(u8_t *buf, u32_t len, u32_t Addr)
Read data from raw addr by single line.
Definition: w25q_mem.c:536
W25Q_STATE W25Q_GlobalReadOnly(bool enable)
Set read-only param to all chip.
Definition: w25q_mem.c:1332
W25Q_STATE W25Q_EnableVolatileSR(void)
Make Status Register Volatile.
Definition: w25q_mem.c:144
W25Q_STATE W25Q_ProgResume(void)
Resume Programm/Erase operation.
Definition: w25q_mem.c:1026
W25Q_STATE
W25Q Return State Lib's functions status returns.
Definition: w25q_mem.h:61
W25Q_STATE W25Q_WriteStatusReg(u8_t reg_data, u8_t reg_num)
Write status register from variable.
Definition: w25q_mem.c:206
W25Q_STATE W25Q_ReadSFDPRegister(u8_t *buf)
Read device descriptor (SFDP Standard)
Definition: w25q_mem.c:1240
W25Q_STATE W25Q_IsBusy(void)
Check chip's busy status.
Definition: w25q_mem.c:303
W25Q_STATE W25Q_ReadWord(u16_t *buf, u8_t pageShift, u32_t pageNum)
Read 16-bit variable.
Definition: w25q_mem.c:396
W25Q_STATE W25Q_ProgSuspend(void)
Pause Programm/Erase operation.
Definition: w25q_mem.c:986
W25Q_STATE W25Q_EraseSecurityRegisters(u8_t numReg)
Erase security register.
Definition: w25q_mem.c:1259
W25Q_STATE W25Q_Init(void)
Initalize function.
Definition: w25q_mem.c:72
W25Q_STATE W25Q_BlockReadOnly(u32_t Addr, bool enable)
Individual block/sector read-only lock.
Definition: w25q_mem.c:1307
W25Q_STATE W25Q_ProgramWord(u16_t buf, u8_t pageShift, u32_t pageNum)
Program 16-bit variable.
Definition: w25q_mem.c:656
W25Q_STATE W25Q_ProgramByte(u8_t buf, u8_t pageShift, u32_t pageNum)
Program 8-bit variable.
Definition: w25q_mem.c:619
W25Q_STATE W25Q_ReadLong(u32_t *buf, u8_t pageShift, u32_t pageNum)
Read 32-bit variable.
Definition: w25q_mem.c:440
W25Q_STATE W25Q_SetBurstWrap(u8_t WrapSize)
Set Burst with Wrap.
Definition: w25q_mem.c:588
W25Q_STATE W25Q_ProgramSLong(i32_t buf, u8_t pageShift, u32_t pageNum)
Program signed 32-bit variable.
Definition: w25q_mem.c:675
@ W25Q_BUSY
Chip busy.
Definition: w25q_mem.h:63
@ W25Q_OK
Chip OK - Execution fine.
Definition: w25q_mem.h:62
@ W25Q_CHIP_ERR
Chip error.
Definition: w25q_mem.h:65
@ W25Q_CHIP_IGNORE
Chip ignore state.
Definition: w25q_mem.h:67
@ W25Q_SPI_ERR
SPI Bus err.
Definition: w25q_mem.h:66
@ W25Q_PARAM_ERR
Function parameters error.
Definition: w25q_mem.h:64
Internal header for adding sys libs and defines.
int8_t i8_t
8-bit signed
Definition: libs.h:18
uint32_t u32_t
32-bit unsigned
Definition: libs.h:21
uint8_t u8_t
< Lib for sprintf, strlen, etc
Definition: libs.h:17
uint16_t u16_t
16-bit unsigned
Definition: libs.h:19
int32_t i32_t
32-bit signed
Definition: libs.h:22
int16_t i16_t
16-bit signed
Definition: libs.h:20
W25Q Status Registers @TODO: Mem protected recognition.
Definition: w25q_mem.h:79
bool QE
Quad SPI mode.
Definition: w25q_mem.h:82
bool BUSY
Erase/Write in progress.
Definition: w25q_mem.h:80
bool SUS
Suspend Status.
Definition: w25q_mem.h:83
bool SLEEP
Sleep Status.
Definition: w25q_mem.h:86
bool WEL
Write enable latch (1 - write allowed)
Definition: w25q_mem.h:81
bool ADP
Power-up addr mode.
Definition: w25q_mem.h:85
bool ADS
Current addr mode (0-3 byte / 1-4 byte)
Definition: w25q_mem.h:84