@@ -201,7 +201,8 @@ OSPIFBlockDevice::OSPIFBlockDevice(PinName io0, PinName io1, PinName io2, PinNam
201201 int clock_mode, int freq)
202202 : _ospi(io0, io1, io2, io3, io4, io5, io6, io7, sclk, csel, dqs, clock_mode), _csel(csel), _freq(freq),
203203 _init_ref_count(0 ),
204- _is_initialized(false )
204+ _is_initialized(false ),
205+ _soft_reset_mode(OSPIF_SOFT_RESET_UNSUPPORTED)
205206{
206207 _unique_device_status = add_new_csel_instance (csel);
207208
@@ -389,14 +390,7 @@ int OSPIFBlockDevice::deinit()
389390 _wait_flag = NOT_STARTED;
390391#endif
391392
392- change_mode (OSPIF_OPI_MODE_SPI);
393-
394- // Disable Device for Writing
395- ospi_status_t status = _ospi_send_general_command (OSPIF_INST_WRDI, OSPI_NO_ADDRESS_COMMAND, NULL , 0 , NULL , 0 );
396- if (status != OSPI_STATUS_OK) {
397- tr_error (" Write Disable failed" );
398- result = OSPIF_BD_ERROR_DEVICE_ERROR;
399- }
393+ result = _soft_reset ();
400394
401395 _is_initialized = false ;
402396
@@ -746,10 +740,6 @@ int OSPIFBlockDevice::change_mode(int mode)
746740 tr_error (" Writing Config Register2 Failed" );
747741 return -1 ;
748742 }
749-
750- // Configure BUS Mode to 1_1_1
751- _ospi.configure_format (OSPI_CFG_BUS_SINGLE, OSPI_CFG_INST_SIZE_8, OSPI_CFG_BUS_SINGLE, OSPI_CFG_ADDR_SIZE_32,
752- OSPI_CFG_BUS_SINGLE, 0 , OSPI_CFG_BUS_SINGLE, 0 );
753743 }
754744
755745 _ospi.configure_format (OSPI_CFG_BUS_SINGLE, OSPI_CFG_INST_SIZE_8, OSPI_CFG_BUS_SINGLE, OSPI_CFG_ADDR_SIZE_32,
@@ -793,15 +783,11 @@ int OSPIFBlockDevice::change_mode(int mode)
793783
794784 if (OSPI_STATUS_OK == _ospi_send_general_command (OSPIF_INST_WRCR2, OSPIF_CR2_OPI_EN_ADDR, &config_reg2,
795785 1 , NULL , 0 )) {
796- tr_debug (" OPI mode enable - Writing Config Register2 Success: value = 0x%x" , config_reg2);
786+ tr_debug (" OPI mode disable - Writing Config Register2 Success: value = 0x%x" , config_reg2);
797787 } else {
798- tr_error (" OPI mode enable - Writing Config Register2 failed" );
788+ tr_error (" OPI mode disable - Writing Config Register2 failed" );
799789 return -1 ;
800790 }
801-
802- // Configure BUS Mode to 1_1_1
803- _ospi.configure_format (OSPI_CFG_BUS_SINGLE, OSPI_CFG_INST_SIZE_8, OSPI_CFG_BUS_SINGLE, OSPI_CFG_ADDR_SIZE_32,
804- OSPI_CFG_BUS_SINGLE, 0 , OSPI_CFG_BUS_SINGLE, 0 );
805791 }
806792
807793 _ospi.configure_format (OSPI_CFG_BUS_SINGLE, OSPI_CFG_INST_SIZE_8, OSPI_CFG_BUS_SINGLE, OSPI_CFG_ADDR_SIZE_32,
@@ -834,32 +820,8 @@ int OSPIFBlockDevice::change_mode(int mode)
834820 _ospi.configure_format (_inst_width, _inst_size, _address_width, _address_size, OSPI_CFG_BUS_SINGLE,
835821 0 , _data_width, 0 );
836822 } else if (mode == OSPIF_OPI_MODE_SPI) {
837- // Write new Status Register Setup
838- if (_set_write_enable () != 0 ) {
839- tr_error (" Write Enabe failed" );
840- return -1 ;
841- }
842-
843- config_reg2 = 0x00 ;
844-
845- if (OSPI_STATUS_OK == _ospi_send_general_command (OSPIF_INST_WRCR2, OSPIF_CR2_OPI_EN_ADDR, &config_reg2,
846- 1 , NULL , 0 )) {
847- tr_debug (" OPI mode enable - Writing Config Register2 Success: value = 0x%x" , config_reg2);
848- } else {
849- tr_error (" OPI mode enable - Writing Config Register2 failed" );
850- return -1 ;
851- }
852- _read_instruction = OSPIF_INST_READ_4B;
853- _dummy_cycles = 0 ;
854-
855- _inst_width = OSPI_CFG_BUS_SINGLE;
856- _inst_size = OSPI_CFG_INST_SIZE_8;
857- _address_width = OSPI_CFG_BUS_SINGLE;
858- _address_size = OSPI_CFG_ADDR_SIZE_32;
859- _data_width = OSPI_CFG_BUS_SINGLE;
860-
861- _ospi.configure_format (_inst_width, _inst_size, _address_width, _address_size, OSPI_CFG_BUS_SINGLE,
862- 0 , _data_width, _dummy_cycles);
823+ // Perform a soft reset to switch to SPI mode as suggested by Copilot
824+ status = _soft_reset ();
863825 }
864826 return status;
865827}
@@ -1364,8 +1326,6 @@ int OSPIFBlockDevice::_sfdp_detect_and_enable_4byte_addressing(uint8_t *basic_pa
13641326
13651327int OSPIFBlockDevice::_sfdp_detect_reset_protocol_and_reset (uint8_t *basic_param_table_ptr)
13661328{
1367- int status = OSPIF_BD_ERROR_OK;
1368-
13691329#if RESET_SEQUENCE_FROM_SFDP
13701330 uint8_t examined_byte = basic_param_table_ptr[OSPIF_BASIC_PARAM_TABLE_SOFT_RESET_BYTE];
13711331
@@ -1374,38 +1334,68 @@ int OSPIFBlockDevice::_sfdp_detect_reset_protocol_and_reset(uint8_t *basic_param
13741334#endif
13751335
13761336#if !MBED_CONF_OSPIF_ENABLE_AND_RESET // i.e. direct reset, or determined from SFDP
1377- // Issue instruction 0xF0 to reset the device
1378- ospi_status_t ospi_status = _ospi_send_general_command (0xF0 , OSPI_NO_ADDRESS_COMMAND, // Send reset instruction
1379- NULL , 0 , NULL , 0 );
1380- status = (ospi_status == OSPI_STATUS_OK) ? OSPIF_BD_ERROR_OK : OSPIF_BD_ERROR_PARSING_FAILED;
1337+ _soft_reset_mode = OSPIF_DIERCT_SOFT_RESET;
13811338#endif
13821339
13831340#if RESET_SEQUENCE_FROM_SFDP
13841341 } else if (examined_byte & SOFT_RESET_ENABLE_AND_RESET_INST_BITMASK) {
13851342#endif
13861343
13871344#if !MBED_CONF_OSPIF_DIRECT_RESET // i.e. enable and reset, or determined from SFDP
1388- // Issue instruction 66h to enable resets on the device
1389- // Then issue instruction 99h to reset the device
1390- ospi_status_t ospi_status = _ospi_send_general_command (0x66 , OSPI_NO_ADDRESS_COMMAND, // Send reset enable instruction
1391- NULL , 0 , NULL , 0 );
1392- if (ospi_status == OSPI_STATUS_OK) {
1393- ospi_status = _ospi_send_general_command (0x99 , OSPI_NO_ADDRESS_COMMAND, // Send reset instruction
1394- NULL , 0 , NULL , 0 );
1395- }
1396- status = (ospi_status == OSPI_STATUS_OK) ? OSPIF_BD_ERROR_OK : OSPIF_BD_ERROR_PARSING_FAILED;
1345+ _soft_reset_mode = OSPIF_ENABLE_AND_SOFT_RESET;
13971346#endif
13981347
13991348#if RESET_SEQUENCE_FROM_SFDP
14001349 } else {
14011350 // Soft reset either is not supported or requires direct control over data lines
1402- tr_error (" Failed to determine soft reset sequence. If your device has a legacy SFDP table, please manually set enable-and-reset or direct-reset." );
1403-
1404- status = OSPIF_BD_ERROR_PARSING_FAILED;
1351+ _soft_reset_mode = OSPIF_SOFT_RESET_UNSUPPORTED;
14051352 }
14061353#endif
14071354
1355+ return _soft_reset ();
1356+ }
1357+
1358+ int OSPIFBlockDevice::_soft_reset ()
1359+ {
1360+ int status = OSPIF_BD_ERROR_OK;
1361+ ospi_status_t ospi_status = OSPI_STATUS_OK;
1362+
1363+ switch (_soft_reset_mode) {
1364+ case OSPIF_SOFT_RESET_UNSUPPORTED:
1365+ status = OSPIF_BD_ERROR_PARSING_FAILED;
1366+ break ;
1367+ case OSPIF_DIERCT_SOFT_RESET:
1368+ // Issue instruction 0xF0 to reset the device
1369+ ospi_status = _ospi_send_general_command (0xF0 , OSPI_NO_ADDRESS_COMMAND, // Send reset instruction
1370+ NULL , 0 , NULL , 0 );
1371+ status = (ospi_status == OSPI_STATUS_OK) ? OSPIF_BD_ERROR_OK : OSPIF_BD_ERROR_PARSING_FAILED;
1372+ break ;
1373+ case OSPIF_ENABLE_AND_SOFT_RESET:
1374+ // Issue instruction 66h to enable resets on the device
1375+ // Then issue instruction 99h to reset the device
1376+ ospi_status = _ospi_send_general_command (0x66 , OSPI_NO_ADDRESS_COMMAND, // Send reset enable instruction
1377+ NULL , 0 , NULL , 0 );
1378+ if (ospi_status == OSPI_STATUS_OK) {
1379+ ospi_status = _ospi_send_general_command (0x99 , OSPI_NO_ADDRESS_COMMAND, // Send reset instruction
1380+ NULL , 0 , NULL , 0 );
1381+ }
1382+ status = (ospi_status == OSPI_STATUS_OK) ? OSPIF_BD_ERROR_OK : OSPIF_BD_ERROR_PARSING_FAILED;
1383+ break ;
1384+ }
14081385 if (status == OSPIF_BD_ERROR_OK) {
1386+ // Set SPI format after soft reset
1387+ _read_instruction = OSPIF_INST_READ_4B;
1388+ _dummy_cycles = 0 ;
1389+
1390+ _inst_width = OSPI_CFG_BUS_SINGLE;
1391+ _inst_size = OSPI_CFG_INST_SIZE_8;
1392+ _address_width = OSPI_CFG_BUS_SINGLE;
1393+ _address_size = OSPI_CFG_ADDR_SIZE_32;
1394+ _data_width = OSPI_CFG_BUS_SINGLE;
1395+
1396+ _ospi.configure_format (_inst_width, _inst_size, _address_width, _address_size, OSPI_CFG_BUS_SINGLE,
1397+ 0 , _data_width, _dummy_cycles);
1398+
14091399 if (false == _is_mem_ready ()) {
14101400 tr_error (" Device not ready, reset failed" );
14111401 status = OSPIF_BD_ERROR_READY_FAILED;
0 commit comments