@@ -85,12 +85,10 @@ SingletonPtr<PlatformMutex> SPIFBlockDevice::_mutex;
85
85
// ***********************
86
86
// SPIF Block Device APIs
87
87
// ***********************
88
- SPIFBlockDevice::SPIFBlockDevice (
89
- PinName mosi, PinName miso, PinName sclk, PinName csel, int freq)
88
+ SPIFBlockDevice::SPIFBlockDevice (PinName mosi, PinName miso, PinName sclk, PinName csel, int freq)
90
89
:
91
90
_spi(mosi, miso, sclk), _cs(csel), _prog_instruction(0 ), _erase_instruction(0 ),
92
- _page_size_bytes(0 ),
93
- _device_size_bytes(0 ), _init_ref_count(0 ), _is_initialized(false )
91
+ _page_size_bytes(0 ), _init_ref_count(0 ), _is_initialized(false )
94
92
{
95
93
_address_size = SPIF_ADDR_SIZE_3_BYTES;
96
94
// Initial SFDP read tables are read with 8 dummy cycles
@@ -99,6 +97,7 @@ SPIFBlockDevice::SPIFBlockDevice(
99
97
_write_dummy_and_mode_cycles = 0 ;
100
98
_dummy_and_mode_cycles = _read_dummy_and_mode_cycles;
101
99
100
+ _sfdp_info.bptbl .device_size_bytes = 0 ;
102
101
_sfdp_info.bptbl .legacy_erase_instruction = SPIF_INST_LEGACY_ERASE_DEFAULT;
103
102
_sfdp_info.smptbl .regions_min_common_erase_size = 0 ;
104
103
_sfdp_info.smptbl .region_cnt = 1 ;
@@ -188,9 +187,9 @@ int SPIFBlockDevice::init()
188
187
}
189
188
190
189
/* *************************** Parse Sector Map Table ***********************************/
191
- _sfdp_info.smptbl .region_size [0 ] =
192
- _device_size_bytes; // If there's no region map, we have a single region sized the entire device size
193
- _sfdp_info.smptbl .region_high_boundary [0 ] = _device_size_bytes - 1 ;
190
+ _sfdp_info.smptbl .region_size [0 ] = _sfdp_info. bptbl . device_size_bytes ;
191
+ // If there's no region map, we have a single region sized the entire device size
192
+ _sfdp_info.smptbl .region_high_boundary [0 ] = _sfdp_info. bptbl . device_size_bytes - 1 ;
194
193
195
194
if ((_sfdp_info.smptbl .addr != 0 ) && (0 != _sfdp_info.smptbl .size )) {
196
195
tr_debug (" init - Parsing Sector Map Table - addr: 0x%" PRIx32 " h, Size: %d" , _sfdp_info.smptbl .addr ,
@@ -207,9 +206,9 @@ int SPIFBlockDevice::init()
207
206
// Dummy And Mode Cycles Back default 0
208
207
_dummy_and_mode_cycles = _write_dummy_and_mode_cycles;
209
208
_is_initialized = true ;
210
- tr_debug (" Device size: %llu Kbytes" , _device_size_bytes / 1024 );
209
+ tr_debug (" Device size: %llu Kbytes" , _sfdp_info. bptbl . device_size_bytes / 1024 );
211
210
212
- if (_device_size_bytes > (1 << 24 )) {
211
+ if (_sfdp_info. bptbl . device_size_bytes > (1 << 24 )) {
213
212
tr_debug (" Size is bigger than 16MB and thus address does not fit in 3 byte, switch to 4 byte address mode" );
214
213
_spi_send_general_command (SPIF_4BEN, SPI_NO_ADDRESS_COMMAND, NULL , 0 , NULL , 0 );
215
214
_address_size = SPIF_ADDR_SIZE_4_BYTES;
@@ -340,7 +339,7 @@ int SPIFBlockDevice::erase(bd_addr_t addr, bd_size_t in_size)
340
339
bool erase_failed = false ;
341
340
int status = SPIF_BD_ERROR_OK;
342
341
// Find region of erased address
343
- int region = _utils_find_addr_region (addr, _sfdp_info. smptbl );
342
+ int region = sfdp_find_addr_region (addr, _sfdp_info);
344
343
if (region < 0 ) {
345
344
tr_error (" no region found for address %llu" , addr);
346
345
return SPIF_BD_ERROR_INVALID_ERASE_PARAMS;
@@ -350,7 +349,7 @@ int SPIFBlockDevice::erase(bd_addr_t addr, bd_size_t in_size)
350
349
351
350
tr_debug (" erase - addr: %llu, in_size: %llu" , addr, in_size);
352
351
353
- if ((addr + in_size) > _device_size_bytes ) {
352
+ if ((addr + in_size) > _sfdp_info. bptbl . device_size_bytes ) {
354
353
tr_error (" erase exceeds flash device size" );
355
354
return SPIF_BD_ERROR_INVALID_ERASE_PARAMS;
356
355
}
@@ -365,7 +364,7 @@ int SPIFBlockDevice::erase(bd_addr_t addr, bd_size_t in_size)
365
364
366
365
// iterate to find next Largest erase type ( a. supported by region, b. smaller than size)
367
366
// find the matching instruction and erase size chunk for that type.
368
- type = _utils_iterate_next_largest_erase_type (bitfield, size, (unsigned int )addr, region, _sfdp_info.smptbl );
367
+ type = sfdp_iterate_next_largest_erase_type (bitfield, size, (unsigned int )addr, region, _sfdp_info.smptbl );
369
368
cur_erase_inst = _sfdp_info.smptbl .erase_type_inst_arr [type];
370
369
offset = addr % _sfdp_info.smptbl .erase_type_size_arr [type];
371
370
chunk = ((offset + size) < _sfdp_info.smptbl .erase_type_size_arr [type]) ?
@@ -436,7 +435,7 @@ bd_size_t SPIFBlockDevice::get_erase_size() const
436
435
bd_size_t SPIFBlockDevice::get_erase_size (bd_addr_t addr) const
437
436
{
438
437
// Find region of current address
439
- int region = _utils_find_addr_region (addr, _sfdp_info. smptbl );
438
+ int region = sfdp_find_addr_region (addr, _sfdp_info);
440
439
441
440
unsigned int min_region_erase_size = _sfdp_info.smptbl .regions_min_common_erase_size ;
442
441
int8_t type_mask = SFDP_ERASE_BITMASK_TYPE1;
@@ -469,7 +468,7 @@ bd_size_t SPIFBlockDevice::size() const
469
468
return 0 ;
470
469
}
471
470
472
- return _device_size_bytes ;
471
+ return _sfdp_info. bptbl . device_size_bytes ;
473
472
}
474
473
475
474
int SPIFBlockDevice::get_erase_value () const
@@ -641,8 +640,8 @@ int SPIFBlockDevice::_sfdp_parse_basic_param_table(Callback<int(bd_addr_t, void
641
640
(param_table[6 ] << 16 ) |
642
641
(param_table[5 ] << 8 ) |
643
642
param_table[4 ]);
644
- _device_size_bytes = (density_bits + 1 ) / 8 ;
645
- tr_debug (" Density bits: %" PRIu32 " , device size: %llu bytes" , density_bits, _device_size_bytes );
643
+ sfdp_info. bptbl . device_size_bytes = (density_bits + 1 ) / 8 ;
644
+ tr_debug (" Density bits: %" PRIu32 " , device size: %llu bytes" , density_bits, sfdp_info. bptbl . device_size_bytes );
646
645
647
646
// Set Default read/program/erase Instructions
648
647
_read_instruction = SPIF_READ;
@@ -779,59 +778,3 @@ int SPIFBlockDevice::_set_write_enable()
779
778
} while (false );
780
779
return status;
781
780
}
782
-
783
- /* ********************************************/
784
- /* ************ Utility Functions *************/
785
- /* ********************************************/
786
- int SPIFBlockDevice::_utils_find_addr_region (bd_size_t offset, const sfdp_smptbl_info &smptbl) const
787
- {
788
- // Find the region to which the given offset belong to
789
- if ((offset > _device_size_bytes) || (smptbl.region_cnt == 0 )) {
790
- return -1 ;
791
- }
792
-
793
- if (smptbl.region_cnt == 1 ) {
794
- return 0 ;
795
- }
796
-
797
- for (int i_ind = smptbl.region_cnt - 2 ; i_ind >= 0 ; i_ind--) {
798
-
799
- if (offset > smptbl.region_high_boundary [i_ind]) {
800
- return (i_ind + 1 );
801
- }
802
- }
803
- return -1 ;
804
-
805
- }
806
-
807
- int SPIFBlockDevice::_utils_iterate_next_largest_erase_type (uint8_t &bitfield,
808
- int size,
809
- int offset,
810
- int region,
811
- sfdp_smptbl_info &smptbl)
812
- {
813
- // Iterate on all supported Erase Types of the Region to which the offset belong to.
814
- // Iterates from highest type to lowest
815
- uint8_t type_mask = SFDP_ERASE_BITMASK_TYPE4;
816
- int i_ind = 0 ;
817
- int largest_erase_type = 0 ;
818
- for (i_ind = 3 ; i_ind >= 0 ; i_ind--) {
819
- if (bitfield & type_mask) {
820
- largest_erase_type = i_ind;
821
- if ((size > (int )(smptbl.erase_type_size_arr [largest_erase_type])) &&
822
- ((_sfdp_info.smptbl .region_high_boundary [region] - offset)
823
- > (int )(smptbl.erase_type_size_arr [largest_erase_type]))) {
824
- break ;
825
- } else {
826
- bitfield &= ~type_mask;
827
- }
828
- }
829
- type_mask = type_mask >> 1 ;
830
- }
831
-
832
- if (i_ind == 4 ) {
833
- tr_error (" No erase type was found for current region addr" );
834
- }
835
- return largest_erase_type;
836
- }
837
-
0 commit comments