Skip to content
This repository was archived by the owner on Jun 8, 2021. It is now read-only.

Commit 98e6694

Browse files
author
Offir Kochalsky
committed
CR changes #7
1 parent 26b3e19 commit 98e6694

File tree

3 files changed

+21
-72
lines changed

3 files changed

+21
-72
lines changed

QSPIFBlockDevice.cpp

Lines changed: 9 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -64,24 +64,13 @@
6464
#define QSPIF_BASIC_PARAM_ERASE_TYPE_4_SIZE_BYTE 34
6565
#define QSPIF_BASIC_PARAM_4K_ERASE_TYPE_BYTE 1
6666

67-
6867
// Erase Types Per Region BitMask
6968
#define ERASE_BITMASK_TYPE4 0x08
70-
#define ERASE_BITMASK_TYPE3 0x04
71-
#define ERASE_BITMASK_TYPE2 0x02
7269
#define ERASE_BITMASK_TYPE1 0x01
73-
#define ERASE_BITMASK_NONE 0x00
7470
#define ERASE_BITMASK_ALL 0x0F
7571

7672
#define IS_MEM_READY_MAX_RETRIES 1000
7773

78-
// Debug Printouts
79-
#define QSPIF_DEBUG_ERROR 1
80-
#define QSPIF_DEBUG_WARNING 2
81-
#define QSPIF_DEBUG_INFO 3
82-
#define QSPIF_DEBUG_DEBUG 4
83-
#define QSPIF_DEBUG_TRACE 5
84-
8574
namespace mbed {
8675

8776
enum qspif_default_instructions {
@@ -104,7 +93,6 @@ static int local_math_power(int base, int exp);
10493

10594
/********* Public API Functions *********/
10695
/****************************************/
107-
10896
QSPIFBlockDevice::QSPIFBlockDevice(PinName io0, PinName io1, PinName io2, PinName io3, PinName sclk, PinName csel,
10997
qspif_polarity_mode clock_mode, int freq)
11098
: _qspi(io0, io1, io2, io3, sclk, csel, clock_mode), _device_size_bytes(0)
@@ -126,7 +114,6 @@ QSPIFBlockDevice::QSPIFBlockDevice(PinName io0, PinName io1, PinName io2, PinNam
126114
}
127115
}
128116

129-
130117
int QSPIFBlockDevice::init()
131118
{
132119

@@ -241,7 +228,6 @@ int QSPIFBlockDevice::deinit()
241228
return result;
242229
}
243230

244-
245231
int QSPIFBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
246232
{
247233

@@ -270,15 +256,14 @@ int QSPIFBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
270256

271257
}
272258

273-
274259
int QSPIFBlockDevice::program(const void *buffer, bd_addr_t addr, bd_size_t size)
275260
{
276261
qspi_status_t result = QSPI_STATUS_OK;
277262
bool program_failed = false;
278263
int status = QSPIF_BD_ERROR_OK;
279264
uint32_t offset = 0;
280265
uint32_t chunk = 0;
281-
bd_size_t writtenBytes = 0;
266+
bd_size_t written_bytes = 0;
282267

283268
tr_debug("DEBUG: program - Buff: 0x%x, addr: %llu, size: %llu", buffer, addr, size);
284269

@@ -287,7 +272,7 @@ int QSPIFBlockDevice::program(const void *buffer, bd_addr_t addr, bd_size_t size
287272
// Write on _page_size_bytes boundaries (Default 256 bytes a page)
288273
offset = addr % _page_size_bytes;
289274
chunk = (offset + size < _page_size_bytes) ? size : (_page_size_bytes - offset);
290-
writtenBytes = chunk;
275+
written_bytes = chunk;
291276

292277
_mutex.lock();
293278

@@ -299,8 +284,8 @@ int QSPIFBlockDevice::program(const void *buffer, bd_addr_t addr, bd_size_t size
299284
goto exit_point;
300285
}
301286

302-
result = _qspi_send_program_command(_prog_instruction, buffer, addr, &writtenBytes);
303-
if ( (result != QSPI_STATUS_OK) || (chunk != writtenBytes) ) {
287+
result = _qspi_send_program_command(_prog_instruction, buffer, addr, &written_bytes);
288+
if ( (result != QSPI_STATUS_OK) || (chunk != written_bytes) ) {
304289
tr_error("ERROR: Write failed");
305290
program_failed = true;
306291
status = QSPIF_BD_ERROR_DEVICE_ERROR;
@@ -318,8 +303,6 @@ int QSPIFBlockDevice::program(const void *buffer, bd_addr_t addr, bd_size_t size
318303
goto exit_point;
319304
}
320305
_mutex.unlock();
321-
322-
323306
}
324307

325308
exit_point:
@@ -330,10 +313,8 @@ int QSPIFBlockDevice::program(const void *buffer, bd_addr_t addr, bd_size_t size
330313
return status;
331314
}
332315

333-
334-
int QSPIFBlockDevice::erase(bd_addr_t addr, bd_size_t inSize)
316+
int QSPIFBlockDevice::erase(bd_addr_t addr, bd_size_t in_size)
335317
{
336-
337318
int type = 0;
338319
uint32_t chunk = 4096;
339320
unsigned int cur_erase_inst = _erase_instruction;
@@ -345,7 +326,7 @@ int QSPIFBlockDevice::erase(bd_addr_t addr, bd_size_t inSize)
345326
// Erase Types of selected region
346327
uint8_t bitfield = _region_erase_types_bitfield[region];
347328

348-
tr_debug("DEBUG: erase - addr: %llu, inSize: %llu", addr, inSize);
329+
tr_debug("DEBUG: erase - addr: %llu, inSize: %llu", addr, in_size);
349330

350331
// For each iteration erase the largest section supported by current region
351332
while (size > 0) {
@@ -404,8 +385,6 @@ int QSPIFBlockDevice::erase(bd_addr_t addr, bd_size_t inSize)
404385
return status;
405386
}
406387

407-
408-
409388
bd_size_t QSPIFBlockDevice::get_read_size() const
410389
{
411390
// Assuming all devices support 1byte read granularity
@@ -424,7 +403,6 @@ bd_size_t QSPIFBlockDevice::get_erase_size() const
424403
return _min_common_erase_size;
425404
}
426405

427-
428406
// Find minimal erase size supported by the region to which the address belongs to
429407
bd_size_t QSPIFBlockDevice::get_erase_size(bd_addr_t addr)
430408
{
@@ -455,19 +433,16 @@ bd_size_t QSPIFBlockDevice::get_erase_size(bd_addr_t addr)
455433
}
456434

457435
return (bd_size_t)min_region_erase_size;
458-
459436
}
460437

461438
bd_size_t QSPIFBlockDevice::size() const
462439
{
463440
return _device_size_bytes;
464441
}
465442

466-
467443
/*********************************************************/
468444
/********** SFDP Parsing and Detection Functions *********/
469445
/*********************************************************/
470-
471446
int QSPIFBlockDevice::_sfdp_parse_sector_map_table(uint32_t sector_map_table_addr, size_t sector_map_table_size)
472447
{
473448
uint8_t sector_map_table[SFDP_DEFAULT_BASIC_PARAMS_TABLE_SIZE_BYTES]; /* Up To 16 DWORDS = 64 Bytes */
@@ -527,7 +502,6 @@ int QSPIFBlockDevice::_sfdp_parse_sector_map_table(uint32_t sector_map_table_add
527502
return 0;
528503
}
529504

530-
531505
int QSPIFBlockDevice::_sfdp_parse_basic_param_table(uint32_t basic_table_addr, size_t basic_table_size)
532506
{
533507
uint8_t param_table[SFDP_DEFAULT_BASIC_PARAMS_TABLE_SIZE_BYTES]; /* Up To 16 DWORDS = 64 Bytes */
@@ -654,8 +628,6 @@ int QSPIFBlockDevice::_sfdp_parse_sfdp_headers(uint32_t& basic_table_addr, size_
654628
return 0;
655629
}
656630

657-
658-
659631
int QSPIFBlockDevice::_sfdp_set_qpi_enabled(uint8_t *basic_param_table_ptr)
660632
{
661633
uint8_t config_reg[1];
@@ -803,11 +775,9 @@ int QSPIFBlockDevice::_sfdp_set_quad_enabled(uint8_t *basic_param_table_ptr)
803775
return -1;
804776
}
805777

806-
807778
return 0;
808779
}
809780

810-
811781
int QSPIFBlockDevice::_sfdp_detect_page_size(uint8_t *basic_param_table_ptr)
812782
{
813783
// Page Size is specified by 4 Bits (N), calculated by 2^N
@@ -817,8 +787,6 @@ int QSPIFBlockDevice::_sfdp_detect_page_size(uint8_t *basic_param_table_ptr)
817787
return page_size;
818788
}
819789

820-
821-
822790
int QSPIFBlockDevice::_sfdp_detect_erase_types_inst_and_size(uint8_t *basic_param_table_ptr, unsigned int& erase4k_inst,
823791
unsigned int *erase_type_inst_arr, unsigned int *erase_type_size_arr)
824792
{
@@ -869,7 +837,6 @@ int QSPIFBlockDevice::_sfdp_detect_erase_types_inst_and_size(uint8_t *basic_para
869837
return 0;
870838
}
871839

872-
873840
int QSPIFBlockDevice::_sfdp_detect_best_bus_read_mode(uint8_t *basic_param_table_ptr, bool& set_quad_enable,
874841
bool& is_qpi_mode,
875842
unsigned int& read_inst)
@@ -957,7 +924,6 @@ int QSPIFBlockDevice::_sfdp_detect_best_bus_read_mode(uint8_t *basic_param_table
957924
return 0;
958925
}
959926

960-
961927
int QSPIFBlockDevice::_reset_flash_mem()
962928
{
963929
// Perform Soft Reset of the Device prior to initialization
@@ -1001,7 +967,6 @@ int QSPIFBlockDevice::_reset_flash_mem()
1001967
return status;
1002968
}
1003969

1004-
1005970
bool QSPIFBlockDevice::_is_mem_ready()
1006971
{
1007972
// Check Status Register Busy Bit to Verify the Device isn't Busy
@@ -1026,7 +991,6 @@ bool QSPIFBlockDevice::_is_mem_ready()
1026991
return mem_ready;
1027992
}
1028993

1029-
1030994
int QSPIFBlockDevice::_set_write_enable()
1031995
{
1032996
// Check Status Register Busy Bit to Verify the Device isn't Busy
@@ -1060,8 +1024,6 @@ int QSPIFBlockDevice::_set_write_enable()
10601024
return status;
10611025
}
10621026

1063-
1064-
10651027
/*********************************************/
10661028
/************* Utility Functions *************/
10671029
/*********************************************/
@@ -1086,7 +1048,6 @@ int QSPIFBlockDevice::_utils_find_addr_region(bd_size_t offset)
10861048

10871049
}
10881050

1089-
10901051
int QSPIFBlockDevice::_utils_iterate_next_largest_erase_type(uint8_t& bitfield, int size, int offset, int boundry)
10911052
{
10921053
// Iterate on all supported Erase Types of the Region to which the offset belong to.
@@ -1114,17 +1075,14 @@ int QSPIFBlockDevice::_utils_iterate_next_largest_erase_type(uint8_t& bitfield,
11141075

11151076
}
11161077

1117-
11181078
/***************************************************/
11191079
/*********** QSPI Driver API Functions *************/
11201080
/***************************************************/
1121-
11221081
qspi_status_t QSPIFBlockDevice::_qsp_set_frequency(int freq)
11231082
{
11241083
return _qspi.set_frequency(freq);
11251084
}
11261085

1127-
11281086
qspi_status_t QSPIFBlockDevice::_qspi_send_read_command(unsigned int read_inst, void *buffer, bd_addr_t addr,
11291087
bd_size_t size)
11301088
{
@@ -1140,7 +1098,6 @@ qspi_status_t QSPIFBlockDevice::_qspi_send_read_command(unsigned int read_inst,
11401098

11411099
}
11421100

1143-
11441101
qspi_status_t QSPIFBlockDevice::_qspi_send_program_command(unsigned int progInst, const void *buffer, bd_addr_t addr,
11451102
bd_size_t *size)
11461103
{
@@ -1155,15 +1112,14 @@ qspi_status_t QSPIFBlockDevice::_qspi_send_program_command(unsigned int progInst
11551112
return result;
11561113
}
11571114

1158-
1159-
qspi_status_t QSPIFBlockDevice::_qspi_send_erase_command(unsigned int eraseInst, bd_addr_t addr, bd_size_t size)
1115+
qspi_status_t QSPIFBlockDevice::_qspi_send_erase_command(unsigned int erase_inst, bd_addr_t addr, bd_size_t size)
11601116
{
11611117
// Send Erase Instruction command to driver
11621118
qspi_status_t result = QSPI_STATUS_OK;
11631119

1164-
tr_info("INFO: Inst: 0x%xh, addr: %llu, size: %llu", eraseInst, addr, size);
1120+
tr_info("INFO: Inst: 0x%xh, addr: %llu, size: %llu", erase_inst, addr, size);
11651121

1166-
result = _qspi.command_transfer(eraseInst, // command to send
1122+
result = _qspi.command_transfer(erase_inst, // command to send
11671123
(((int)addr) & 0x00FFF000), // Align addr to 4096
11681124
NULL, // do not transmit
11691125
0, // do not transmit
@@ -1178,7 +1134,6 @@ qspi_status_t QSPIFBlockDevice::_qspi_send_erase_command(unsigned int eraseInst,
11781134

11791135
}
11801136

1181-
11821137
qspi_status_t QSPIFBlockDevice::_qspi_send_general_command(unsigned int instruction, bd_addr_t addr,
11831138
const char *tx_buffer,
11841139
size_t tx_length, const char *rx_buffer, size_t rx_length)
@@ -1193,7 +1148,6 @@ qspi_status_t QSPIFBlockDevice::_qspi_send_general_command(unsigned int instruct
11931148
return status;
11941149
}
11951150

1196-
11971151
qspi_status_t QSPIFBlockDevice::_qspi_configure_format(qspi_bus_width_t inst_width, qspi_bus_width_t address_width,
11981152
qspi_address_size_t address_size, qspi_bus_width_t alt_width, qspi_alt_size_t alt_size, qspi_bus_width_t data_width,
11991153
int dummy_cycles)
@@ -1205,7 +1159,6 @@ qspi_status_t QSPIFBlockDevice::_qspi_configure_format(qspi_bus_width_t inst_wid
12051159
return status;
12061160
}
12071161

1208-
12091162
/*********************************************/
12101163
/************** Local Functions **************/
12111164
/*********************************************/

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,34 @@ https://www.jedec.org/system/files/docs/JESD216B.pdf
1313
#include "mbed.h"
1414
#include "QSPIFBlockDevice.h"
1515

16-
QSPIFBlockDevice blockD(MBED_CONF_QSPIF_QSPI_IO0,MBED_CONF_QSPIF_QSPI_IO1,MBED_CONF_QSPIF_QSPI_IO2,MBED_CONF_QSPIF_QSPI_IO3,
16+
QSPIFBlockDevice block_device(MBED_CONF_QSPIF_QSPI_IO0,MBED_CONF_QSPIF_QSPI_IO1,MBED_CONF_QSPIF_QSPI_IO2,MBED_CONF_QSPIF_QSPI_IO3,
1717
MBED_CONF_QSPIF_QSPI_CLK,MBED_CONF_QSPIF_QSPI_CS,0,MBED_CONF_QSPIF_QSPI_FREQ);
1818

1919

2020
int main() {
2121
printf("QSPI SFDP Flash Block Device example\n");
2222

2323
// Initialize the SPI flash device and print the memory layout
24-
blockD.init();
25-
bd_size_t sector_size_at_address_0 = blockD.get_erase_size(0);
24+
block_device.init();
25+
bd_size_t sector_size_at_address_0 = block_device.get_erase_size(0);
2626

27-
printf("QSPIF BD size: %llu\n", blockD.size());
28-
printf("QSPIF BD read size: %llu\n", blockD.get_read_size());
29-
printf("QSPIF BD program size: %llu\n", blockD.get_program_size());
27+
printf("QSPIF BD size: %llu\n", block_device.size());
28+
printf("QSPIF BD read size: %llu\n", block_device.get_read_size());
29+
printf("QSPIF BD program size: %llu\n", block_device.get_program_size());
3030

3131
printf("QSPIF BD erase size (at address 0): %llu\n", sector_size_at_address_0);
3232

3333
// Write "Hello World!" to the first block
3434
char *buffer = (char*) malloc(sector_size_at_address_0);
3535
sprintf(buffer, "Hello World!\n");
36-
blockD.erase(0, sector_size_at_address_0);
37-
blockD.program(buffer, 0, sector_size_at_address_0);
36+
block_device.erase(0, sector_size_at_address_0);
37+
block_device.program(buffer, 0, sector_size_at_address_0);
3838

3939
// Read back what was stored
40-
blockD.read(buffer, 0, sector_size_at_address_0);
40+
block_device.read(buffer, 0, sector_size_at_address_0);
4141
printf("%s", buffer);
4242

4343
// Deinitialize the device
44-
blockD.deinit();
44+
block_device.deinit();
4545
}
4646
```

TESTS/block_device/qspif/main.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,8 @@ static SingletonPtr<PlatformMutex> _mutex;
4242
// Mutex is protecting rand() per srand for buffer writing and verification.
4343
// Mutex is also protecting printouts for clear logs.
4444
// Mutex is NOT protecting Block Device actions: erase/program/read - which is the purpose of the multithreaded test!
45-
void basic_erase_program_read_test(
46-
QSPIFBlockDevice& blockD,
47-
bd_size_t block_size,
48-
uint8_t *write_block,
49-
uint8_t *read_block,
50-
unsigned addrwidth)
45+
void basic_erase_program_read_test(QSPIFBlockDevice& blockD, bd_size_t block_size, uint8_t *write_block,
46+
uint8_t *read_block, unsigned addrwidth)
5147
{
5248
int err = 0;
5349
_mutex->lock();

0 commit comments

Comments
 (0)