@@ -85,12 +85,10 @@ SingletonPtr<PlatformMutex> SPIFBlockDevice::_mutex;
8585// ***********************
8686// SPIF Block Device APIs
8787// ***********************
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)
9089 :
9190 _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 )
9492{
9593 _address_size = SPIF_ADDR_SIZE_3_BYTES;
9694 // Initial SFDP read tables are read with 8 dummy cycles
@@ -99,6 +97,7 @@ SPIFBlockDevice::SPIFBlockDevice(
9997 _write_dummy_and_mode_cycles = 0 ;
10098 _dummy_and_mode_cycles = _read_dummy_and_mode_cycles;
10199
100+ _sfdp_info.bptbl .device_size_bytes = 0 ;
102101 _sfdp_info.bptbl .legacy_erase_instruction = SPIF_INST_LEGACY_ERASE_DEFAULT;
103102 _sfdp_info.smptbl .regions_min_common_erase_size = 0 ;
104103 _sfdp_info.smptbl .region_cnt = 1 ;
@@ -188,9 +187,9 @@ int SPIFBlockDevice::init()
188187 }
189188
190189 /* *************************** 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 ;
194193
195194 if ((_sfdp_info.smptbl .addr != 0 ) && (0 != _sfdp_info.smptbl .size )) {
196195 tr_debug (" init - Parsing Sector Map Table - addr: 0x%" PRIx32 " h, Size: %d" , _sfdp_info.smptbl .addr ,
@@ -207,9 +206,9 @@ int SPIFBlockDevice::init()
207206 // Dummy And Mode Cycles Back default 0
208207 _dummy_and_mode_cycles = _write_dummy_and_mode_cycles;
209208 _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 );
211210
212- if (_device_size_bytes > (1 << 24 )) {
211+ if (_sfdp_info. bptbl . device_size_bytes > (1 << 24 )) {
213212 tr_debug (" Size is bigger than 16MB and thus address does not fit in 3 byte, switch to 4 byte address mode" );
214213 _spi_send_general_command (SPIF_4BEN, SPI_NO_ADDRESS_COMMAND, NULL , 0 , NULL , 0 );
215214 _address_size = SPIF_ADDR_SIZE_4_BYTES;
@@ -340,7 +339,7 @@ int SPIFBlockDevice::erase(bd_addr_t addr, bd_size_t in_size)
340339 bool erase_failed = false ;
341340 int status = SPIF_BD_ERROR_OK;
342341 // 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);
344343 if (region < 0 ) {
345344 tr_error (" no region found for address %llu" , addr);
346345 return SPIF_BD_ERROR_INVALID_ERASE_PARAMS;
@@ -350,7 +349,7 @@ int SPIFBlockDevice::erase(bd_addr_t addr, bd_size_t in_size)
350349
351350 tr_debug (" erase - addr: %llu, in_size: %llu" , addr, in_size);
352351
353- if ((addr + in_size) > _device_size_bytes ) {
352+ if ((addr + in_size) > _sfdp_info. bptbl . device_size_bytes ) {
354353 tr_error (" erase exceeds flash device size" );
355354 return SPIF_BD_ERROR_INVALID_ERASE_PARAMS;
356355 }
@@ -365,7 +364,7 @@ int SPIFBlockDevice::erase(bd_addr_t addr, bd_size_t in_size)
365364
366365 // iterate to find next Largest erase type ( a. supported by region, b. smaller than size)
367366 // 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 );
369368 cur_erase_inst = _sfdp_info.smptbl .erase_type_inst_arr [type];
370369 offset = addr % _sfdp_info.smptbl .erase_type_size_arr [type];
371370 chunk = ((offset + size) < _sfdp_info.smptbl .erase_type_size_arr [type]) ?
@@ -436,7 +435,7 @@ bd_size_t SPIFBlockDevice::get_erase_size() const
436435bd_size_t SPIFBlockDevice::get_erase_size (bd_addr_t addr) const
437436{
438437 // 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);
440439
441440 unsigned int min_region_erase_size = _sfdp_info.smptbl .regions_min_common_erase_size ;
442441 int8_t type_mask = SFDP_ERASE_BITMASK_TYPE1;
@@ -469,7 +468,7 @@ bd_size_t SPIFBlockDevice::size() const
469468 return 0 ;
470469 }
471470
472- return _device_size_bytes ;
471+ return _sfdp_info. bptbl . device_size_bytes ;
473472}
474473
475474int SPIFBlockDevice::get_erase_value () const
@@ -641,8 +640,8 @@ int SPIFBlockDevice::_sfdp_parse_basic_param_table(Callback<int(bd_addr_t, void
641640 (param_table[6 ] << 16 ) |
642641 (param_table[5 ] << 8 ) |
643642 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 );
646645
647646 // Set Default read/program/erase Instructions
648647 _read_instruction = SPIF_READ;
@@ -779,59 +778,3 @@ int SPIFBlockDevice::_set_write_enable()
779778 } while (false );
780779 return status;
781780}
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