2121#include < string.h>
2222
2323#include " mbed_trace.h"
24+ #include " mbed_debug.h"
2425#define TRACE_GROUP " SPIF"
2526using namespace mbed ;
2627
@@ -154,6 +155,8 @@ int SPIFBlockDevice::init()
154155 tr_error (" init - Unable to initialize flash memory, tests failed" );
155156 status = SPIF_BD_ERROR_DEVICE_ERROR;
156157 goto exit_point;
158+ } else {
159+ debug_if (MBED_CONF_SPIF_DRIVER_DEBUG, " DEBUG: Initialize flash memory OK\n " );
157160 }
158161
159162 /* Read Manufacturer ID (1byte), and Device ID (2bytes)*/
@@ -202,6 +205,8 @@ int SPIFBlockDevice::init()
202205 _region_high_boundary[0 ] = _device_size_bytes - 1 ;
203206
204207 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);
205210 if (0 != _sfdp_parse_sector_map_table (sector_map_table_addr, sector_map_table_size)) {
206211 tr_error (" init - Parse Sector Map Table Failed" );
207212 status = SPIF_BD_ERROR_PARSING_FAILED;
@@ -265,6 +270,7 @@ int SPIFBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
265270 }
266271
267272 int status = SPIF_BD_ERROR_OK;
273+ debug_if (MBED_CONF_SPIF_DRIVER_DEBUG, " DEBUG Read - Inst: 0x%xh" , _read_instruction);
268274 _mutex->lock ();
269275
270276 // 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)
290296 uint32_t offset = 0 ;
291297 uint32_t chunk = 0 ;
292298
299+ debug_if (MBED_CONF_SPIF_DRIVER_DEBUG, " DEBUG: program - Buff: 0x%lxh, addr: %llu, size: %llu" , (uint32_t )buffer, addr, size);
300+
293301 while (size > 0 ) {
294302
295303 // 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)
347355 // Erase Types of selected region
348356 uint8_t bitfield = _region_erase_types_bitfield[region];
349357
358+ debug_if (MBED_CONF_SPIF_DRIVER_DEBUG, " DEBUG: erase - addr: %llu, in_size: %llu" , addr, in_size);
359+
350360 if ((addr + in_size) > _device_size_bytes) {
351361 tr_error (" erase exceeds flash device size" );
352362 return SPIF_BD_ERROR_INVALID_ERASE_PARAMS;
@@ -367,6 +377,11 @@ int SPIFBlockDevice::erase(bd_addr_t addr, bd_size_t in_size)
367377 offset = addr % _erase_type_size_arr[type];
368378 chunk = ((offset + size) < _erase_type_size_arr[type]) ? size : (_erase_type_size_arr[type] - offset);
369379
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+
370385 _mutex->lock ();
371386
372387 if (_set_write_enable () != 0 ) {
@@ -550,6 +565,7 @@ spif_bd_error SPIFBlockDevice::_spi_send_program_command(int prog_inst, const vo
550565
551566spif_bd_error SPIFBlockDevice::_spi_send_erase_command (int erase_inst, bd_addr_t addr, bd_size_t size)
552567{
568+ debug_if (MBED_CONF_SPIF_DRIVER_DEBUG, " DEBUG: Erase Inst: 0x%xh, addr: %llu, size: %llu" , erase_inst, addr, size);
553569 addr = (((int )addr) & 0xFFFFF000 );
554570 _spi_send_general_command (erase_inst, addr, NULL , 0 , NULL , 0 );
555571 return SPIF_BD_ERROR_OK;
@@ -722,10 +738,13 @@ int SPIFBlockDevice::_sfdp_parse_sfdp_headers(uint32_t &basic_table_addr, size_t
722738 if (!(memcmp (&sfdp_header[0 ], " SFDP" , 4 ) == 0 && sfdp_header[5 ] == 1 )) {
723739 tr_error (" init - _verify SFDP signature and version Failed" );
724740 return -1 ;
741+ } else {
742+ debug_if (MBED_CONF_SPIF_DRIVER_DEBUG, " DEBUG: init - verified SFDP Signature and version Successfully" );
725743 }
726744
727745 // Discover Number of Parameter Headers
728746 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);
729748
730749 addr += SPIF_SFDP_HEADER_SIZE;
731750 data_length = SPIF_PARAM_HEADER_SIZE;
@@ -748,12 +767,14 @@ int SPIFBlockDevice::_sfdp_parse_sfdp_headers(uint32_t &basic_table_addr, size_t
748767
749768 if ((param_header[0 ] == 0 ) && (param_header[7 ] == 0xFF )) {
750769 // 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 );
751771 basic_table_addr = ((param_header[6 ] << 16 ) | (param_header[5 ] << 8 ) | (param_header[4 ]));
752772 // Supporting up to 64 Bytes Table (16 DWORDS)
753773 basic_table_size = ((param_header[3 ] * 4 ) < SFDP_DEFAULT_BASIC_PARAMS_TABLE_SIZE_BYTES) ? (param_header[3 ] * 4 ) : 64 ;
754774
755775 } else if ((param_header[0 ] == 81 ) && (param_header[7 ] == 0xFF )) {
756776 // 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 );
757778 sector_map_table_addr = ((param_header[6 ] << 16 ) | (param_header[5 ] << 8 ) | (param_header[4 ]));
758779 sector_map_table_size = param_header[3 ] * 4 ;
759780
@@ -772,6 +793,9 @@ unsigned int SPIFBlockDevice::_sfdp_detect_page_size(uint8_t *basic_param_table_
772793 // Page Size is specified by 4 Bits (N), calculated by 2^N
773794 int page_to_power_size = ((int )basic_param_table_ptr[SPIF_BASIC_PARAM_TABLE_PAGE_SIZE_BYTE]) >> 4 ;
774795 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);
775799 }
776800 return page_size;
777801}
@@ -793,6 +817,8 @@ int SPIFBlockDevice::_sfdp_detect_erase_types_inst_and_size(uint8_t *basic_param
793817 erase_type_inst_arr[i_ind] = 0xff ; // 0xFF default for unsupported type
794818 erase_type_size_arr[i_ind] = local_math_power (2 ,
795819 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]);
796822 if (erase_type_size_arr[i_ind] > 1 ) {
797823 // if size==1 type is not supported
798824 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
814840 }
815841 _region_erase_types_bitfield[0 ] |= bitfield; // If there's no region map, set region "0" types bitfield as defualt;
816842 }
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]);
818845 bitfield = bitfield << 1 ;
819846 }
820847 }
@@ -841,6 +868,7 @@ int SPIFBlockDevice::_sfdp_detect_best_bus_read_mode(uint8_t *basic_param_table_
841868 read_inst = basic_param_table_ptr[SPIF_BASIC_PARAM_TABLE_222_READ_INST_BYTE];
842869 _read_dummy_and_mode_cycles = (basic_param_table_ptr[SPIF_BASIC_PARAM_TABLE_222_READ_INST_BYTE - 1] >> 5)
843870 + (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);
844872 break;
845873 }
846874 }
@@ -850,17 +878,20 @@ int SPIFBlockDevice::_sfdp_detect_best_bus_read_mode(uint8_t *basic_param_table_
850878 read_inst = basic_param_table_ptr[SPIF_BASIC_PARAM_TABLE_122_READ_INST_BYTE];
851879 _read_dummy_and_mode_cycles = (basic_param_table_ptr[SPIF_BASIC_PARAM_TABLE_122_READ_INST_BYTE - 1] >> 5)
852880 + (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);
853882 break;
854883 }
855884 if (examined_byte & 0x01) {
856885 // Fast Read 1-1-2 Supported
857886 read_inst = basic_param_table_ptr[SPIF_BASIC_PARAM_TABLE_112_READ_INST_BYTE];
858887 _read_dummy_and_mode_cycles = (basic_param_table_ptr[SPIF_BASIC_PARAM_TABLE_112_READ_INST_BYTE - 1] >> 5)
859888 + (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);
860890 break;
861891 }
862892 */
863893 _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);
864895 } while (false );
865896
866897 return 0 ;
@@ -871,17 +902,21 @@ int SPIFBlockDevice::_reset_flash_mem()
871902 // Perform Soft Reset of the Device prior to initialization
872903 int status = 0 ;
873904 char status_value[2 ] = {0 };
905+ debug_if (MBED_CONF_SPIF_DRIVER_DEBUG, " INFO: _reset_flash_mem:\n " );
874906 // Read the Status Register from device
875907 if (SPIF_BD_ERROR_OK == _spi_send_general_command (SPIF_RDSR, SPI_NO_ADDRESS_COMMAND, NULL , 0 , status_value, 1 )) {
876908 // 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 ]);
877910 } else {
911+ debug_if (MBED_CONF_SPIF_DRIVER_DEBUG, " ERROR: Reading Status Register failed\n " );
878912 status = -1 ;
879913 }
880914
881915 if (0 == status) {
882916 // Send Reset Enable
883917 if (SPIF_BD_ERROR_OK == _spi_send_general_command (SPIF_RSTEN, SPI_NO_ADDRESS_COMMAND, NULL , 0 , NULL , 0 )) {
884918 // store received values in status_value
919+ debug_if (MBED_CONF_SPIF_DRIVER_DEBUG, " DEBUG: Sending RSTEN Success\n " );
885920 } else {
886921 tr_error (" Sending RSTEN failed" );
887922 status = -1 ;
@@ -891,6 +926,7 @@ int SPIFBlockDevice::_reset_flash_mem()
891926 // Send Reset
892927 if (SPIF_BD_ERROR_OK == _spi_send_general_command (SPIF_RST, SPI_NO_ADDRESS_COMMAND, NULL , 0 , NULL , 0 )) {
893928 // store received values in status_value
929+ debug_if (MBED_CONF_SPIF_DRIVER_DEBUG, " DEBUG: Sending RST Success\n " );
894930 } else {
895931 tr_error (" Sending RST failed" );
896932 status = -1 ;
0 commit comments