21
21
#include < string.h>
22
22
23
23
#include " mbed_trace.h"
24
+ #include " mbed_debug.h"
24
25
#define TRACE_GROUP " SPIF"
25
26
using namespace mbed ;
26
27
@@ -154,6 +155,8 @@ int SPIFBlockDevice::init()
154
155
tr_error (" init - Unable to initialize flash memory, tests failed" );
155
156
status = SPIF_BD_ERROR_DEVICE_ERROR;
156
157
goto exit_point;
158
+ } else {
159
+ debug_if (MBED_CONF_SPIF_DRIVER_DEBUG, " DEBUG: Initialize flash memory OK\n " );
157
160
}
158
161
159
162
/* Read Manufacturer ID (1byte), and Device ID (2bytes)*/
@@ -202,6 +205,8 @@ int SPIFBlockDevice::init()
202
205
_region_high_boundary[0 ] = _device_size_bytes - 1 ;
203
206
204
207
if ((sector_map_table_addr != 0 ) && (0 != sector_map_table_size)) {
208
+ debug_if (MBED_CONF_SPIF_DRIVER_DEBUG, " DEBUG: init - Parsing Sector Map Table - addr: 0x%lxh, Size: %d" , sector_map_table_addr,
209
+ sector_map_table_size);
205
210
if (0 != _sfdp_parse_sector_map_table (sector_map_table_addr, sector_map_table_size)) {
206
211
tr_error (" init - Parse Sector Map Table Failed" );
207
212
status = SPIF_BD_ERROR_PARSING_FAILED;
@@ -265,6 +270,7 @@ int SPIFBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
265
270
}
266
271
267
272
int status = SPIF_BD_ERROR_OK;
273
+ debug_if (MBED_CONF_SPIF_DRIVER_DEBUG, " DEBUG Read - Inst: 0x%xh" , _read_instruction);
268
274
_mutex->lock ();
269
275
270
276
// Set Dummy Cycles for Specific Read Command Mode
@@ -290,6 +296,8 @@ int SPIFBlockDevice::program(const void *buffer, bd_addr_t addr, bd_size_t size)
290
296
uint32_t offset = 0 ;
291
297
uint32_t chunk = 0 ;
292
298
299
+ debug_if (MBED_CONF_SPIF_DRIVER_DEBUG, " DEBUG: program - Buff: 0x%lxh, addr: %llu, size: %llu" , (uint32_t )buffer, addr, size);
300
+
293
301
while (size > 0 ) {
294
302
295
303
// Write on _page_size_bytes boundaries (Default 256 bytes a page)
@@ -347,6 +355,8 @@ int SPIFBlockDevice::erase(bd_addr_t addr, bd_size_t in_size)
347
355
// Erase Types of selected region
348
356
uint8_t bitfield = _region_erase_types_bitfield[region];
349
357
358
+ debug_if (MBED_CONF_SPIF_DRIVER_DEBUG, " DEBUG: erase - addr: %llu, in_size: %llu" , addr, in_size);
359
+
350
360
if ((addr + in_size) > _device_size_bytes) {
351
361
tr_error (" erase exceeds flash device size" );
352
362
return SPIF_BD_ERROR_INVALID_ERASE_PARAMS;
@@ -367,6 +377,11 @@ int SPIFBlockDevice::erase(bd_addr_t addr, bd_size_t in_size)
367
377
offset = addr % _erase_type_size_arr[type];
368
378
chunk = ((offset + size) < _erase_type_size_arr[type]) ? size : (_erase_type_size_arr[type] - offset);
369
379
380
+ debug_if (MBED_CONF_SPIF_DRIVER_DEBUG, " DEBUG: erase - addr: %llu, size:%d, Inst: 0x%xh, chunk: %lu , " ,
381
+ addr, size, cur_erase_inst, chunk);
382
+ debug_if (MBED_CONF_SPIF_DRIVER_DEBUG, " DEBUG: erase - Region: %d, Type:%d" ,
383
+ region, type);
384
+
370
385
_mutex->lock ();
371
386
372
387
if (_set_write_enable () != 0 ) {
@@ -550,6 +565,7 @@ spif_bd_error SPIFBlockDevice::_spi_send_program_command(int prog_inst, const vo
550
565
551
566
spif_bd_error SPIFBlockDevice::_spi_send_erase_command (int erase_inst, bd_addr_t addr, bd_size_t size)
552
567
{
568
+ debug_if (MBED_CONF_SPIF_DRIVER_DEBUG, " DEBUG: Erase Inst: 0x%xh, addr: %llu, size: %llu" , erase_inst, addr, size);
553
569
addr = (((int )addr) & 0xFFFFF000 );
554
570
_spi_send_general_command (erase_inst, addr, NULL , 0 , NULL , 0 );
555
571
return SPIF_BD_ERROR_OK;
@@ -722,10 +738,13 @@ int SPIFBlockDevice::_sfdp_parse_sfdp_headers(uint32_t &basic_table_addr, size_t
722
738
if (!(memcmp (&sfdp_header[0 ], " SFDP" , 4 ) == 0 && sfdp_header[5 ] == 1 )) {
723
739
tr_error (" init - _verify SFDP signature and version Failed" );
724
740
return -1 ;
741
+ } else {
742
+ debug_if (MBED_CONF_SPIF_DRIVER_DEBUG, " DEBUG: init - verified SFDP Signature and version Successfully" );
725
743
}
726
744
727
745
// Discover Number of Parameter Headers
728
746
int number_of_param_headers = (int )(sfdp_header[6 ]) + 1 ;
747
+ debug_if (MBED_CONF_SPIF_DRIVER_DEBUG, " DEBUG: number of Param Headers: %d" , number_of_param_headers);
729
748
730
749
addr += SPIF_SFDP_HEADER_SIZE;
731
750
data_length = SPIF_PARAM_HEADER_SIZE;
@@ -748,12 +767,14 @@ int SPIFBlockDevice::_sfdp_parse_sfdp_headers(uint32_t &basic_table_addr, size_t
748
767
749
768
if ((param_header[0 ] == 0 ) && (param_header[7 ] == 0xFF )) {
750
769
// Found Basic Params Table: LSB=0x00, MSB=0xFF
770
+ debug_if (MBED_CONF_SPIF_DRIVER_DEBUG, " DEBUG: Found Basic Param Table at Table: %d" , i_ind + 1 );
751
771
basic_table_addr = ((param_header[6 ] << 16 ) | (param_header[5 ] << 8 ) | (param_header[4 ]));
752
772
// Supporting up to 64 Bytes Table (16 DWORDS)
753
773
basic_table_size = ((param_header[3 ] * 4 ) < SFDP_DEFAULT_BASIC_PARAMS_TABLE_SIZE_BYTES) ? (param_header[3 ] * 4 ) : 64 ;
754
774
755
775
} else if ((param_header[0 ] == 81 ) && (param_header[7 ] == 0xFF )) {
756
776
// Found Sector Map Table: LSB=0x81, MSB=0xFF
777
+ debug_if (MBED_CONF_SPIF_DRIVER_DEBUG, " DEBUG: Found Sector Map Table at Table: %d" , i_ind + 1 );
757
778
sector_map_table_addr = ((param_header[6 ] << 16 ) | (param_header[5 ] << 8 ) | (param_header[4 ]));
758
779
sector_map_table_size = param_header[3 ] * 4 ;
759
780
@@ -772,6 +793,9 @@ unsigned int SPIFBlockDevice::_sfdp_detect_page_size(uint8_t *basic_param_table_
772
793
// Page Size is specified by 4 Bits (N), calculated by 2^N
773
794
int page_to_power_size = ((int )basic_param_table_ptr[SPIF_BASIC_PARAM_TABLE_PAGE_SIZE_BYTE]) >> 4 ;
774
795
page_size = local_math_power (2 , page_to_power_size);
796
+ debug_if (MBED_CONF_SPIF_DRIVER_DEBUG, " DEBUG: Detected Page Size: %d" , page_size);
797
+ } else {
798
+ debug_if (MBED_CONF_SPIF_DRIVER_DEBUG, " DEBUG: Using Default Page Size: %d" , page_size);
775
799
}
776
800
return page_size;
777
801
}
@@ -793,6 +817,8 @@ int SPIFBlockDevice::_sfdp_detect_erase_types_inst_and_size(uint8_t *basic_param
793
817
erase_type_inst_arr[i_ind] = 0xff ; // 0xFF default for unsupported type
794
818
erase_type_size_arr[i_ind] = local_math_power (2 ,
795
819
basic_param_table_ptr[SPIF_BASIC_PARAM_ERASE_TYPE_1_SIZE_BYTE + 2 * i_ind]); // Size given as 2^N
820
+ debug_if (MBED_CONF_SPIF_DRIVER_DEBUG, " DEBUG: Erase Type(A) %d - Inst: 0x%xh, Size: %d" , (i_ind + 1 ), erase_type_inst_arr[i_ind],
821
+ erase_type_size_arr[i_ind]);
796
822
if (erase_type_size_arr[i_ind] > 1 ) {
797
823
// if size==1 type is not supported
798
824
erase_type_inst_arr[i_ind] = basic_param_table_ptr[SPIF_BASIC_PARAM_ERASE_TYPE_1_BYTE + 2 * i_ind];
@@ -814,7 +840,8 @@ int SPIFBlockDevice::_sfdp_detect_erase_types_inst_and_size(uint8_t *basic_param
814
840
}
815
841
_region_erase_types_bitfield[0 ] |= bitfield; // If there's no region map, set region "0" types bitfield as defualt;
816
842
}
817
-
843
+ debug_if (MBED_CONF_SPIF_DRIVER_DEBUG, " INFO: Erase Type %d - Inst: 0x%xh, Size: %d" , (i_ind + 1 ),
844
+ erase_type_inst_arr[i_ind], erase_type_size_arr[i_ind]);
818
845
bitfield = bitfield << 1 ;
819
846
}
820
847
}
@@ -841,6 +868,7 @@ int SPIFBlockDevice::_sfdp_detect_best_bus_read_mode(uint8_t *basic_param_table_
841
868
read_inst = basic_param_table_ptr[SPIF_BASIC_PARAM_TABLE_222_READ_INST_BYTE];
842
869
_read_dummy_and_mode_cycles = (basic_param_table_ptr[SPIF_BASIC_PARAM_TABLE_222_READ_INST_BYTE - 1] >> 5)
843
870
+ (basic_param_table_ptr[SPIF_BASIC_PARAM_TABLE_222_READ_INST_BYTE - 1] & 0x1F);
871
+ debug_if(MBED_CONF_SPIF_DRIVER_DEBUG, "\nDEBUG: Read Bus Mode set to 2-2-2, Instruction: 0x%xh", read_inst);
844
872
break;
845
873
}
846
874
}
@@ -850,17 +878,20 @@ int SPIFBlockDevice::_sfdp_detect_best_bus_read_mode(uint8_t *basic_param_table_
850
878
read_inst = basic_param_table_ptr[SPIF_BASIC_PARAM_TABLE_122_READ_INST_BYTE];
851
879
_read_dummy_and_mode_cycles = (basic_param_table_ptr[SPIF_BASIC_PARAM_TABLE_122_READ_INST_BYTE - 1] >> 5)
852
880
+ (basic_param_table_ptr[SPIF_BASIC_PARAM_TABLE_122_READ_INST_BYTE - 1] & 0x1F);
881
+ debug_if(MBED_CONF_SPIF_DRIVER_DEBUG, "\nDEBUG: Read Bus Mode set to 1-2-2, Instruction: 0x%xh", read_inst);
853
882
break;
854
883
}
855
884
if (examined_byte & 0x01) {
856
885
// Fast Read 1-1-2 Supported
857
886
read_inst = basic_param_table_ptr[SPIF_BASIC_PARAM_TABLE_112_READ_INST_BYTE];
858
887
_read_dummy_and_mode_cycles = (basic_param_table_ptr[SPIF_BASIC_PARAM_TABLE_112_READ_INST_BYTE - 1] >> 5)
859
888
+ (basic_param_table_ptr[SPIF_BASIC_PARAM_TABLE_112_READ_INST_BYTE - 1] & 0x1F);
889
+ debug_if(MBED_CONF_SPIF_DRIVER_DEBUG, "\nDEBUG: Read Bus Mode set to 1-1-2, Instruction: 0x%xh", _read_instruction);
860
890
break;
861
891
}
862
892
*/
863
893
_read_dummy_and_mode_cycles = 0 ;
894
+ debug_if (MBED_CONF_SPIF_DRIVER_DEBUG, " \n DEBUG: Read Bus Mode set to 1-1-1, Instruction: 0x%xh" , read_inst);
864
895
} while (false );
865
896
866
897
return 0 ;
@@ -871,17 +902,21 @@ int SPIFBlockDevice::_reset_flash_mem()
871
902
// Perform Soft Reset of the Device prior to initialization
872
903
int status = 0 ;
873
904
char status_value[2 ] = {0 };
905
+ debug_if (MBED_CONF_SPIF_DRIVER_DEBUG, " INFO: _reset_flash_mem:\n " );
874
906
// Read the Status Register from device
875
907
if (SPIF_BD_ERROR_OK == _spi_send_general_command (SPIF_RDSR, SPI_NO_ADDRESS_COMMAND, NULL , 0 , status_value, 1 )) {
876
908
// store received values in status_value
909
+ debug_if (MBED_CONF_SPIF_DRIVER_DEBUG, " DEBUG: Reading Status Register Success: value = 0x%x\n " , (int )status_value[0 ]);
877
910
} else {
911
+ debug_if (MBED_CONF_SPIF_DRIVER_DEBUG, " ERROR: Reading Status Register failed\n " );
878
912
status = -1 ;
879
913
}
880
914
881
915
if (0 == status) {
882
916
// Send Reset Enable
883
917
if (SPIF_BD_ERROR_OK == _spi_send_general_command (SPIF_RSTEN, SPI_NO_ADDRESS_COMMAND, NULL , 0 , NULL , 0 )) {
884
918
// store received values in status_value
919
+ debug_if (MBED_CONF_SPIF_DRIVER_DEBUG, " DEBUG: Sending RSTEN Success\n " );
885
920
} else {
886
921
tr_error (" Sending RSTEN failed" );
887
922
status = -1 ;
@@ -891,6 +926,7 @@ int SPIFBlockDevice::_reset_flash_mem()
891
926
// Send Reset
892
927
if (SPIF_BD_ERROR_OK == _spi_send_general_command (SPIF_RST, SPI_NO_ADDRESS_COMMAND, NULL , 0 , NULL , 0 )) {
893
928
// store received values in status_value
929
+ debug_if (MBED_CONF_SPIF_DRIVER_DEBUG, " DEBUG: Sending RST Success\n " );
894
930
} else {
895
931
tr_error (" Sending RST failed" );
896
932
status = -1 ;
0 commit comments