ChibiOS  0.0.0
m25q.h
Go to the documentation of this file.
1 /*
2  ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio.
3 
4  This file is part of ChibiOS.
5 
6  ChibiOS is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 3 of the License, or
9  (at your option) any later version.
10 
11  ChibiOS is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 /**
21  * @file m25q.h
22  * @brief Micron serial flash driver header.
23  *
24  * @addtogroup M25Q
25  * @ingroup EX_MICRON
26  * @{
27  */
28 
29 #ifndef M25Q_H
30 #define M25Q_H
31 
32 #include "hal_jesd216_flash.h"
33 
34 /*===========================================================================*/
35 /* Driver constants. */
36 /*===========================================================================*/
37 
38 /**
39  * @name Version identification
40  * @{
41  */
42 /**
43  * @brief M25Q driver version string.
44  */
45 #define EX_M25Q_VERSION "1.0.0"
46 
47 /**
48  * @brief M25Q driver version major number.
49  */
50 #define EX_M25Q_MAJOR 1
51 
52 /**
53  * @brief M25Q driver version minor number.
54  */
55 #define EX_M25Q_MINOR 0
56 
57 /**
58  * @brief M25Q driver version patch number.
59  */
60 #define EX_M25Q_PATCH 0
61 /** @} */
62 
63 /**
64  * @name Command codes
65  * @{
66  */
67 #define M25Q_CMD_RESET_ENABLE 0x66
68 #define M25Q_CMD_RESET_MEMORY 0x99
69 #define M25Q_CMD_READ_ID 0x9F
70 #define M25Q_CMD_MULTIPLE_IO_READ_ID 0xAF
71 #define M25Q_CMD_READ_DISCOVERY_PARAMETER 0x5A
72 #define M25Q_CMD_READ 0x03
73 #define M25Q_CMD_FAST_READ 0x0B
74 #define M25Q_CMD_WRITE_ENABLE 0x06
75 #define M25Q_CMD_WRITE_DISABLE 0x04
76 #define M25Q_CMD_READ_STATUS_REGISTER 0x05
77 #define M25Q_CMD_WRITE_STATUS_REGISTER 0x01
78 #define M25Q_CMD_READ_LOCK_REGISTER 0xE8
79 #define M25Q_CMD_WRITE_LOCK_REGISTER 0xE5
80 #define M25Q_CMD_READ_FLAG_STATUS_REGISTER 0x70
81 #define M25Q_CMD_CLEAR_FLAG_STATUS_REGISTER 0x50
82 #define M25Q_CMD_READ_NV_CONFIGURATION_REGISTER 0xB5
83 #define M25Q_CMD_WRITE_NV_CONFIGURATION_REGISTER 0xB1
84 #define M25Q_CMD_READ_V_CONF_REGISTER 0x85
85 #define M25Q_CMD_WRITE_V_CONF_REGISTER 0x81
86 #define M25Q_CMD_READ_ENHANCED_V_CONF_REGISTER 0x65
87 #define M25Q_CMD_WRITE_ENHANCED_V_CONF_REGISTER 0x61
88 #define M25Q_CMD_PAGE_PROGRAM 0x02
89 #define M25Q_CMD_SUBSECTOR_ERASE 0x20
90 #define M25Q_CMD_SECTOR_ERASE 0xD8
91 #define M25Q_CMD_BULK_ERASE 0xC7
92 #define M25Q_CMD_PROGRAM_ERASE_RESUME 0x7A
93 #define M25Q_CMD_PROGRAM_ERASE_SUSPEND 0x75
94 #define M25Q_CMD_READ_OTP_ARRAY 0x4B
95 #define M25Q_CMD_PROGRAM_OTP_ARRAY 0x42
96 /** @} */
97 
98 /**
99  * @name Flags status register bits
100  * @{
101  */
102 #define M25Q_FLAGS_PROGRAM_ERASE 0x80U
103 #define M25Q_FLAGS_ERASE_SUSPEND 0x40U
104 #define M25Q_FLAGS_ERASE_ERROR 0x20U
105 #define M25Q_FLAGS_PROGRAM_ERROR 0x10U
106 #define M25Q_FLAGS_VPP_ERROR 0x08U
107 #define M25Q_FLAGS_PROGRAM_SUSPEND 0x04U
108 #define M25Q_FLAGS_PROTECTION_ERROR 0x02U
109 #define M25Q_FLAGS_RESERVED 0x01U
110 #define M25Q_FLAGS_ALL_ERRORS (M25Q_FLAGS_ERASE_ERROR | \
111  M25Q_FLAGS_PROGRAM_ERROR | \
112  M25Q_FLAGS_VPP_ERROR | \
113  M25Q_FLAGS_PROTECTION_ERROR)
114 /** @} */
115 
116 /*===========================================================================*/
117 /* Driver pre-compile time settings. */
118 /*===========================================================================*/
119 
120 /**
121  * @name Configuration options
122  * @{
123  */
124 /**
125  * @brief Number of dummy cycles for fast read (1..15).
126  * @details This is the number of dummy cycles to be used for fast read
127  * operations.
128  */
129 #if !defined(M25Q_READ_DUMMY_CYCLES) || defined(__DOXYGEN__)
130 #define M25Q_READ_DUMMY_CYCLES 8
131 #endif
132 
133 /**
134  * @brief Switch QSPI bus width on initialization.
135  * @details A bus width initialization is performed by writing the
136  * Enhanced Volatile Configuration Register. If the flash
137  * device is configured using the Non Volatile Configuration
138  * Register then this option is not required.
139  * @note This option is only valid in QSPI bus modes.
140  */
141 #if !defined(M25Q_SWITCH_WIDTH) || defined(__DOXYGEN__)
142 #define M25Q_SWITCH_WIDTH TRUE
143 #endif
144 
145 /**
146  * @brief Delays insertions.
147  * @details If enabled this options inserts delays into the flash waiting
148  * routines releasing some extra CPU time for threads with lower
149  * priority, this may slow down the driver a bit however.
150  */
151 #if !defined(M25Q_NICE_WAITING) || defined(__DOXYGEN__)
152 #define M25Q_NICE_WAITING TRUE
153 #endif
154 
155 /**
156  * @brief Uses 4kB sub-sectors rather than 64kB sectors.
157  */
158 #if !defined(M25Q_USE_SUB_SECTORS) || defined(__DOXYGEN__)
159 #define M25Q_USE_SUB_SECTORS FALSE
160 #endif
161 
162 /**
163  * @brief Supported JEDEC manufacturer identifiers.
164  */
165 #if !defined(M25Q_SUPPORTED_MANUFACTURE_IDS) || defined(__DOXYGEN__)
166 #define M25Q_SUPPORTED_MANUFACTURE_IDS {0x20}
167 #endif
168 
169 /**
170  * @brief Supported memory type identifiers.
171  */
172 #if !defined(M25Q_SUPPORTED_MEMORY_TYPE_IDS) || defined(__DOXYGEN__)
173 #define M25Q_SUPPORTED_MEMORY_TYPE_IDS {0xBA, 0xBB}
174 #endif
175 
176 /**
177  * @brief Size of the compare buffer.
178  * @details This buffer is allocated in the stack frame of the function
179  * @p flashVerifyErase() and its size must be a power of two.
180  * Larger buffers lead to better verify performance but increase
181  * stack usage for that function.
182  */
183 #if !defined(M25Q_COMPARE_BUFFER_SIZE) || defined(__DOXYGEN__)
184 #define M25Q_COMPARE_BUFFER_SIZE 32
185 #endif
186 /** @} */
187 
188 /*===========================================================================*/
189 /* Derived constants and error checks. */
190 /*===========================================================================*/
191 
192 #if (M25Q_READ_DUMMY_CYCLES < 1) || (M25Q_READ_DUMMY_CYCLES > 15)
193 #error "invalid M25Q_READ_DUMMY_CYCLES value (1..15)"
194 #endif
195 
196 #if (M25Q_COMPARE_BUFFER_SIZE & (M25Q_COMPARE_BUFFER_SIZE - 1)) != 0
197 #error "invalid M25Q_COMPARE_BUFFER_SIZE value"
198 #endif
199 
200 /*===========================================================================*/
201 /* Driver data structures and types. */
202 /*===========================================================================*/
203 
204 /**
205  * @brief Type of a M25Q configuration structure.
206  */
207 typedef struct {
208  _jesd216_config
209 } M25QConfig;
210 
211 /**
212  * @brief @p M25Q specific methods.
213  */
214 #define _m25q_methods \
215  _jesd216_flash_methods
216 
217 /**
218  * @extends JESD216FlashVMT
219  *
220  * @brief @p M25Q virtual methods table.
221  */
224 };
225 
226 /**
227  * @extends JESD216Flash
228  *
229  * @brief Type of M25Q flash class.
230  */
231 typedef struct {
232  /**
233  * @brief M25QDriver Virtual Methods Table.
234  */
235  const struct M25QDriverVMT *vmt;
237  /**
238  * @brief Current configuration data.
239  */
241  /**
242  * @brief Device ID and unique ID.
243  */
244  uint8_t device_id[20];
245 } M25QDriver;
246 
247 /*===========================================================================*/
248 /* Driver macros. */
249 /*===========================================================================*/
250 
251 /*===========================================================================*/
252 /* External declarations. */
253 /*===========================================================================*/
254 
255 #ifdef __cplusplus
256 extern "C" {
257 #endif
258  void m25qObjectInit(M25QDriver *devp);
259  void m25qStart(M25QDriver *devp, const M25QConfig *config);
260  void m25qStop(M25QDriver *devp);
261 #if (JESD216_BUS_MODE != JESD216_BUS_MODE_SPI) || defined(__DOXYGEN__)
262 #if (QSPI_SUPPORTS_MEMMAP == TRUE) || defined(__DOXYGEN__)
263  void m25qMemoryMap(M25QDriver *devp, uint8_t ** addrp);
264  void m25qMemoryUnmap(M25QDriver *devp);
265 #endif /* QSPI_SUPPORTS_MEMMAP == TRUE */
266 #endif /* JESD216_BUS_MODE != JESD216_BUS_MODE_SPI */
267 #ifdef __cplusplus
268 }
269 #endif
270 
271 #endif /* M25Q_H */
272 
273 /** @} */
274 
void m25qMemoryMap(M25QDriver *devp, uint8_t **addrp)
Enters the memory Mapping mode.
Definition: m25q.c:773
void m25qObjectInit(M25QDriver *devp)
Initializes an instance.
Definition: m25q.c:627
const struct M25QDriverVMT * vmt
M25QDriver Virtual Methods Table.
Definition: m25q.h:235
JESD216 compliant flash driver class header.
void m25qStart(M25QDriver *devp, const M25QConfig *config)
Configures and activates N25Q128 driver.
Definition: m25q.c:644
#define _m25q_methods
M25Q specific methods.
Definition: m25q.h:214
#define _jesd216_flash_data
JESD215Flash specific data.
Type of M25Q flash class.
Definition: m25q.h:231
void m25qStop(M25QDriver *devp)
Deactivates the N25Q128 driver.
Definition: m25q.c:735
M25Q virtual methods table.
Definition: m25q.h:222
_jesd216_flash_data const M25QConfig * config
Current configuration data.
Definition: m25q.h:240
void m25qMemoryUnmap(M25QDriver *devp)
Leaves the memory Mapping mode.
Definition: m25q.c:821
Type of a M25Q configuration structure.
Definition: m25q.h:207