21
21
22
22
#ifdef HAS_QSPI
23
23
24
+
25
+ extern " C" int mylogadd (const char *fmt, ...) ;
24
26
/* -------------------------------------------------------------------------- */
25
27
/* CONSTRUCTOR */
26
28
/* -------------------------------------------------------------------------- */
@@ -91,16 +93,20 @@ int QSPIFlashBlockDevice::program(const void *buffer, bd_addr_t addr, bd_size_t
91
93
return write (buffer, addr, _size);
92
94
}
93
95
96
+ #ifdef DEBUG_MSD
97
+ extern " C" int mylogadd (const char *fmt, ...) ;
98
+ #endif
99
+
94
100
/* -------------------------------------------------------------------------- */
95
101
/* OPEN */
96
102
/* -------------------------------------------------------------------------- */
97
103
int QSPIFlashBlockDevice::open () {
98
104
fsp_err_t rv = (fsp_err_t )BLOCK_DEVICE_OK;
99
- static bool opened = false ;
100
-
105
+
101
106
if (!opened) {
102
-
107
+
103
108
opened = true ;
109
+
104
110
R_IOPORT_PinCfg (NULL , g_pin_cfg[ck].pin , (uint32_t ) (IOPORT_CFG_PERIPHERAL_PIN | IOPORT_PERIPHERAL_QSPI));
105
111
R_IOPORT_PinCfg (NULL , g_pin_cfg[cs].pin , (uint32_t ) (IOPORT_CFG_PERIPHERAL_PIN | IOPORT_PERIPHERAL_QSPI));
106
112
R_IOPORT_PinCfg (NULL , g_pin_cfg[io0].pin , (uint32_t ) (IOPORT_CFG_PERIPHERAL_PIN | IOPORT_PERIPHERAL_QSPI));
@@ -191,17 +197,19 @@ int QSPIFlashBlockDevice::read(void *buffer, bd_addr_t add, bd_size_t _size) {
191
197
192
198
if (!opened) {
193
199
rv = (fsp_err_t )open ();
194
- }
195
-
196
- if (rv != BLOCK_DEVICE_OK) {
197
- return rv;
200
+ if (rv != BLOCK_DEVICE_OK) {
201
+ return rv;
202
+ }
198
203
}
199
204
200
205
if (buffer == nullptr ) {
201
206
rv = FSP_ERR_INVALID_ARGUMENT;
202
207
}
203
208
else {
204
209
if (is_address_correct (add+_size-1 )) {
210
+ #ifdef DEBUG_MSD
211
+ mylogadd (" QSPI READ %i, %i" , add, _size);
212
+ #endif
205
213
rv = (fsp_err_t )BLOCK_DEVICE_OK;
206
214
int64_t internal_size = _size;
207
215
uint32_t byte_left_in_bank = read_block_size - read_block_size % add;
@@ -231,6 +239,8 @@ int QSPIFlashBlockDevice::read(void *buffer, bd_addr_t add, bd_size_t _size) {
231
239
return (int )rv;
232
240
}
233
241
242
+ #define INTERNAL_BLOCK_SIZE 256
243
+
234
244
/* -------------------------------------------------------------------------- */
235
245
/* WRITE 'size' byte form buffer to the "logical" address specified by 'addr'
236
246
NOTE: buffer MUST be equal or greater than 'size' */
@@ -240,20 +250,21 @@ int QSPIFlashBlockDevice::write(const void *buffer, bd_addr_t add, bd_size_t _si
240
250
241
251
if (!opened) {
242
252
rv = (fsp_err_t )open ();
253
+ if (rv != BLOCK_DEVICE_OK) {
254
+ return rv;
255
+ }
243
256
}
244
257
245
- if (rv != BLOCK_DEVICE_OK) {
246
- return rv;
247
- }
248
-
249
-
250
258
if (buffer == nullptr ) {
251
259
rv = FSP_ERR_INVALID_ARGUMENT;
252
260
}
253
261
else {
254
262
if (is_address_correct (add+_size-1 )) {
263
+ #ifdef DEBUG_MSD
264
+ mylogadd (" QSPI WRITE %i, %i" , add, _size);
265
+ #endif
255
266
int64_t internal_size = _size;
256
- uint32_t bytes_left_in_page = write_block_size - (add % write_block_size );
267
+ uint32_t bytes_left_in_page = INTERNAL_BLOCK_SIZE - (add % INTERNAL_BLOCK_SIZE );
257
268
uint8_t *source = (uint8_t *)buffer;
258
269
rv = FSP_SUCCESS;
259
270
while (internal_size > 0 && rv == FSP_SUCCESS) {
@@ -277,7 +288,7 @@ int QSPIFlashBlockDevice::write(const void *buffer, bd_addr_t add, bd_size_t _si
277
288
internal_size -= bytes_to_write;
278
289
source += bytes_to_write;
279
290
add += bytes_to_write;
280
- bytes_left_in_page = write_block_size ;
291
+ bytes_left_in_page = INTERNAL_BLOCK_SIZE ;
281
292
if (rv == FSP_SUCCESS)
282
293
rv = get_flash_status ();
283
294
}
@@ -302,13 +313,11 @@ int QSPIFlashBlockDevice::erase(bd_addr_t add, bd_size_t _size) {
302
313
303
314
if (!opened) {
304
315
rv = (fsp_err_t )open ();
316
+ if (rv != BLOCK_DEVICE_OK) {
317
+ return rv;
318
+ }
305
319
}
306
320
307
- if (rv != BLOCK_DEVICE_OK) {
308
- return rv;
309
- }
310
-
311
-
312
321
if (is_address_correct (add+_size-1 )) {
313
322
int64_t internal_size = _size;
314
323
/* get the starting address of the block */
0 commit comments