Skip to content

Commit 4775d0a

Browse files
committed
Simplification of QSPI Block Device
Former-commit-id: 003ca6c
1 parent 0fa201b commit 4775d0a

File tree

4 files changed

+79
-105
lines changed

4 files changed

+79
-105
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.idea
22
cores/arduino/mydebug.cpp
33
libraries/Storage/.development
4+
cores/arduino/mydebug.cpp.donotuse

libraries/BlockDevices/QSPIFlashBlockDevice.cpp

Lines changed: 64 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -186,114 +186,88 @@ int QSPIFlashBlockDevice::close() {
186186

187187

188188

189-
#define READ_PAGE_SIZE 0x4000000
189+
190190
/* -------------------------------------------------------------------------- */
191191
/* READ at the "logical" address 'add' 'size' bytes and copy them at the address
192192
specified by buffer
193193
NOTE: buffer MUST be equal or greater than 'size' */
194194
/* -------------------------------------------------------------------------- */
195195
int QSPIFlashBlockDevice::read(void *buffer, bd_addr_t add, bd_size_t _size) {
196-
fsp_err_t rv = FSP_ERR_INVALID_ADDRESS;
196+
fsp_err_t rv = (fsp_err_t)BLOCK_DEVICE_OK;;
197+
198+
if(!is_valid_read(add,_size)) {
199+
return (int)FSP_ERR_INVALID_ADDRESS;
200+
}
197201

198202
if(!opened) {
199203
rv = (fsp_err_t)open();
200204
if(rv != BLOCK_DEVICE_OK) {
201-
return rv;
205+
return (int)FSP_ERR_NOT_OPEN;
202206
}
203207
}
204208

205209
if(buffer == nullptr) {
206-
rv = FSP_ERR_INVALID_ARGUMENT;
210+
return (int)FSP_ERR_INVALID_ARGUMENT;
207211
}
208-
else {
209-
if(is_address_correct(add+_size-1)) {
210-
#ifdef DEBUG_MSD
211-
mylogadd("QSPI READ %i, %i", add, _size);
212-
#endif
213-
rv = (fsp_err_t)BLOCK_DEVICE_OK;
214-
int64_t internal_size = _size;
215-
uint32_t byte_left_in_bank = read_block_size - read_block_size % add;
216-
uint8_t *dest = (uint8_t *)buffer;
217-
while(internal_size > 0) {
218-
uint32_t bank = add / READ_PAGE_SIZE;
219-
#ifdef QSPI_FLASH_DEBUG
220-
Serial.print("+++++ READ Selected bank: ");
221-
Serial.println(bank);
222-
#endif
223-
uint32_t address = base_address + (add % READ_PAGE_SIZE);
224-
#ifdef QSPI_FLASH_DEBUG
225-
Serial.print("+++++ READ physical address: ");
226-
Serial.println(address,HEX);
227-
#endif
228-
R_QSPI_BankSet(&ctrl, bank);
229-
uint32_t bytes_to_copy = (internal_size > byte_left_in_bank) ? byte_left_in_bank : internal_size;
230-
memcpy(dest,(uint8_t *)address, bytes_to_copy);
231-
internal_size -= bytes_to_copy;
232-
add += bytes_to_copy;
233-
dest += bytes_to_copy;
234-
byte_left_in_bank = read_block_size;
235-
}
236-
}
212+
213+
/* check if start and end belongs to 2 different sector
214+
(should never happens because the READ_PAGE_SIZE is very BIG) */
215+
while( (add / READ_PAGE_SIZE) != ((add + _size - 1) / READ_PAGE_SIZE)) {
216+
uint32_t bytes_left_in_page = READ_PAGE_SIZE - add % READ_PAGE_SIZE;
217+
218+
uint32_t bank = add / READ_PAGE_SIZE;
219+
uint32_t address = base_address + (add % READ_PAGE_SIZE);
220+
rv = R_QSPI_BankSet(&ctrl, bank);
221+
memcpy((uint8_t *)(buffer),(uint8_t *)address, bytes_left_in_page);
222+
add += bytes_left_in_page;
223+
_size -= bytes_left_in_page;
237224
}
238225

226+
/* set bank */
227+
uint32_t bank = add / READ_PAGE_SIZE;
228+
uint32_t address = base_address + (add % READ_PAGE_SIZE);
229+
rv = R_QSPI_BankSet(&ctrl, bank);
230+
memcpy((uint8_t *)(buffer),(uint8_t *)address, _size);
231+
239232
return (int)rv;
240233
}
241234

242-
#define INTERNAL_BLOCK_SIZE 256
235+
243236

244237
/* -------------------------------------------------------------------------- */
245238
/* WRITE 'size' byte form buffer to the "logical" address specified by 'addr'
246239
NOTE: buffer MUST be equal or greater than 'size' */
247240
/* -------------------------------------------------------------------------- */
248241
int QSPIFlashBlockDevice::write(const void *buffer, bd_addr_t add, bd_size_t _size) {
249-
fsp_err_t rv = FSP_ERR_INVALID_ADDRESS;
250-
242+
fsp_err_t rv = (fsp_err_t)BLOCK_DEVICE_OK;;
243+
244+
if(!is_valid_program(add,_size)) {
245+
return (int)FSP_ERR_INVALID_ADDRESS;
246+
}
247+
251248
if(!opened) {
252249
rv = (fsp_err_t)open();
253250
if(rv != BLOCK_DEVICE_OK) {
254-
return rv;
251+
return (int)FSP_ERR_NOT_OPEN;
255252
}
256253
}
257254

258255
if(buffer == nullptr) {
259-
rv = FSP_ERR_INVALID_ARGUMENT;
256+
return (int)FSP_ERR_INVALID_ARGUMENT;
260257
}
261-
else {
262-
if(is_address_correct(add+_size-1)) {
263-
#ifdef DEBUG_MSD
264-
mylogadd("QSPI WRITE %i, %i", add, _size);
265-
#endif
266-
int64_t internal_size = _size;
267-
uint32_t bytes_left_in_page = INTERNAL_BLOCK_SIZE - (add % INTERNAL_BLOCK_SIZE);
268-
uint8_t *source = (uint8_t *)buffer;
269-
rv = FSP_SUCCESS;
270-
while(internal_size > 0 && rv == FSP_SUCCESS) {
271-
272-
uint32_t bank = add / READ_PAGE_SIZE;
273-
#ifdef QSPI_FLASH_DEBUG
274-
Serial.print("+++++ WRITE Selected bank: ");
275-
Serial.println(bank);
276-
#endif
277-
278-
uint32_t address = base_address + (add % READ_PAGE_SIZE);
279-
#ifdef QSPI_FLASH_DEBUG
280-
Serial.print("+++++ WRITE physical address: ");
281-
Serial.println(address,HEX);
282-
#endif
283-
284-
R_QSPI_BankSet(&ctrl, bank);
285-
uint32_t bytes_to_write = (internal_size > bytes_left_in_page) ? bytes_left_in_page : internal_size;
286-
287-
rv = R_QSPI_Write(&ctrl, source, (uint8_t*)address, bytes_to_write);
288-
internal_size -= bytes_to_write;
289-
source += bytes_to_write;
290-
add += bytes_to_write;
291-
bytes_left_in_page = INTERNAL_BLOCK_SIZE;
292-
if(rv == FSP_SUCCESS)
293-
rv = get_flash_status();
294-
}
258+
259+
uint32_t num_of_blocks = (_size / WRITE_INTERNAL_BLOCK_SIZE);
260+
for(int i = 0; i < num_of_blocks && rv == FSP_SUCCESS; i++) {
261+
/* set bank */
262+
uint32_t bank = add / READ_PAGE_SIZE;
263+
uint32_t address = base_address + ((add + i * WRITE_INTERNAL_BLOCK_SIZE) % READ_PAGE_SIZE);
264+
R_QSPI_BankSet(&ctrl, bank);
265+
rv = R_QSPI_Write(&ctrl, (uint8_t *)(buffer + (i * WRITE_INTERNAL_BLOCK_SIZE)), (uint8_t*)address, WRITE_INTERNAL_BLOCK_SIZE);
266+
if(rv == FSP_SUCCESS) {
267+
rv = get_flash_status();
295268
}
296269
}
270+
297271
return (int)rv;
298272
}
299273

@@ -309,42 +283,32 @@ bool QSPIFlashBlockDevice::is_address_correct(bd_addr_t add) {
309283
/* -------------------------------------------------------------------------- */
310284
int QSPIFlashBlockDevice::erase(bd_addr_t add, bd_size_t _size) {
311285

312-
fsp_err_t rv = FSP_ERR_INVALID_ADDRESS;
313-
286+
fsp_err_t rv = (fsp_err_t)BLOCK_DEVICE_OK;;
287+
288+
if(!is_valid_erase(add,_size)) {
289+
return (int)FSP_ERR_INVALID_ADDRESS;
290+
}
291+
314292
if(!opened) {
315293
rv = (fsp_err_t)open();
316294
if(rv != BLOCK_DEVICE_OK) {
317-
return rv;
295+
return (int)FSP_ERR_NOT_OPEN;
318296
}
319297
}
320298

321-
if(is_address_correct(add+_size-1)) {
322-
int64_t internal_size = _size;
323-
/* get the starting address of the block */
324-
uint32_t erase_block_address = erase_block_size * (add / erase_block_size);
325-
uint32_t byte_left_in_erase_block = erase_block_size - (add % erase_block_size);
326-
rv = FSP_SUCCESS;
327-
while(internal_size > 0 && rv == FSP_SUCCESS) {
328-
uint32_t bank = erase_block_address / READ_PAGE_SIZE;
329-
#ifdef QSPI_FLASH_DEBUG
330-
Serial.print("+++++ ERASE Selected bank: ");
331-
Serial.println(bank);
332-
#endif
333-
uint32_t address = base_address + (erase_block_address % READ_PAGE_SIZE);
334-
#ifdef QSPI_FLASH_DEBUG
335-
Serial.print("+++++ ERASE physical address: ");
336-
Serial.println(address,HEX);
337-
#endif
338-
R_QSPI_BankSet(&ctrl, bank);
339-
rv = R_QSPI_Erase(&ctrl, (uint8_t *)address, erase_block_size);
340-
erase_block_address += byte_left_in_erase_block;
341-
internal_size -= byte_left_in_erase_block;
342-
byte_left_in_erase_block = erase_block_size;
343-
if(rv == FSP_SUCCESS)
344-
rv = get_flash_status();
299+
uint32_t num_of_blocks = (_size / erase_block_size);
300+
301+
for(int i = 0; i < num_of_blocks && rv == FSP_SUCCESS; i++) {
302+
/* set bank */
303+
uint32_t bank = add / READ_PAGE_SIZE;
304+
uint32_t address = base_address + ((add + i * erase_block_size) % READ_PAGE_SIZE);
305+
R_QSPI_BankSet(&ctrl, bank);
306+
rv = R_QSPI_Erase(&ctrl, (uint8_t *)address, erase_block_size);
307+
if(rv == FSP_SUCCESS) {
308+
rv = get_flash_status();
345309
}
346310
}
347-
311+
348312
return (int)rv;
349313
}
350314

libraries/BlockDevices/QSPIFlashBlockDevice.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@
6161
/* default memory value */
6262
#define DEFAULT_MEM_VAL (0xFF)
6363

64+
65+
#define WRITE_INTERNAL_BLOCK_SIZE 256
66+
#define READ_PAGE_SIZE 0x4000000
67+
6468
/* -------------------------------------------------------------------------- */
6569
/* CLASS QSPIFlashBlockDevice - to access micro internal flash */
6670
/* -------------------------------------------------------------------------- */
@@ -124,6 +128,16 @@ class QSPIFlashBlockDevice : public BlockDevice {
124128
virtual bd_size_t size() const override;
125129
virtual const char *get_type() const override;
126130
virtual int get_erase_value() const override { return 0xFF; }
131+
132+
virtual bool is_valid_read(bd_addr_t addr, bd_size_t size) const override {
133+
return (addr + size <= this->size());
134+
}
135+
136+
virtual bool is_valid_program(bd_addr_t addr, bd_size_t size) const override{
137+
return ( addr % WRITE_INTERNAL_BLOCK_SIZE == 0 && size % WRITE_INTERNAL_BLOCK_SIZE == 0 && addr + size <= this->size());
138+
}
139+
140+
127141

128142
};
129143

libraries/BlockDevices/SDCardBlockDevice.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -354,17 +354,12 @@ int SDCardBlockDevice::read(void *buffer, bd_addr_t add, bd_size_t _size) {
354354
return rv;
355355
}
356356

357-
358-
359357
if(open() == BLOCK_DEVICE_OK) {
360358
if(SDCardBlockDevice::initialized) {
361359
uint32_t num_of_blocks = (_size / read_block_size);
362360
uint32_t start_add_of_block = (add / read_block_size);
363361
rv = FSP_SUCCESS;
364362
for(int i = 0; i < num_of_blocks && rv == FSP_SUCCESS; i++) {
365-
366-
367-
368363
rv = R_SDHI_Read (&ctrl, (uint8_t *)(buffer + (i * read_block_size)), start_add_of_block + i, 1);
369364
if(rv == FSP_SUCCESS) {
370365
rv = wait_for_completition();

0 commit comments

Comments
 (0)