12 Replies Latest reply on Dec 30, 2016 2:45 PM by darius1

    sflash_write error fix (OTA)

    darius1

      sflash_write error fix, SDKs -all.

      Simple 2 lines error correction to resolve page write error at free address . This error I reported her: Possible OTA bug?

       

      int sflash_write( const sflash_handle_t* const handle, unsigned long device_address, /*@observer@*/ const void* const data_addr, unsigned int size )

      {

          int status;

          unsigned int write_size;

          unsigned int max_write_size = (unsigned int) 1;

          int enable_before_every_write = 1;

          const unsigned char* data_addr_ptr = (const unsigned char*) data_addr;

          unsigned char curr_device_address[3];

          uint8_t result;

       

          if ( handle->write_allowed != SFLASH_WRITE_ALLOWED )

          {

              return -1;

          }

       

          /* Some manufacturers support programming an entire page in one command. */

       

      #ifdef SFLASH_SUPPORT_MACRONIX_PARTS

          if ( SFLASH_MANUFACTURER( handle->device_id ) == SFLASH_MANUFACTURER_MACRONIX )

          {

              max_write_size = (unsigned int) 128;  /* TODO: this should be 256, but that causes write errors */

              enable_before_every_write = 1;

          }

      #endif /* ifdef SFLASH_SUPPORT_MACRONIX_PARTS */

      #ifdef SFLASH_SUPPORT_SST_PARTS

          if ( SFLASH_MANUFACTURER( handle->device_id ) == SFLASH_MANUFACTURER_SST )

          {

              max_write_size = (unsigned int) 1;

              enable_before_every_write = 1;

          }

      #endif /* ifdef SFLASH_SUPPORT_SST_PARTS */

      #ifdef SFLASH_SUPPORT_EON_PARTS

          if ( SFLASH_MANUFACTURER( handle->device_id ) == SFLASH_MANUFACTURER_EON )

          {

              max_write_size = (unsigned int) 1;

              enable_before_every_write = 1;

          }

      #endif /* ifdef SFLASH_SUPPORT_EON_PARTS */

      #ifdef SFLASH_SUPPORT_MICRON_PARTS

          if ( SFLASH_MANUFACTURER( handle->device_id ) == SFLASH_MANUFACTURER_MICRON )

          {

              max_write_size = (unsigned int) 128;

              enable_before_every_write = 1;

          }

      #endif /* ifdef SFLASH_SUPPORT_MICRON_PARTS */

       

       

          if ( enable_before_every_write == 0 )

          {

              status = sflash_write_enable( handle );

              if ( status != 0 )

              {

                  return status;

              }

          }

       

          /* Generic x-bytes-at-a-time write */

       

          while ( size > 0 )

          {

              write_size = ( size > max_write_size )? max_write_size : size;

      #if 1//darius, correcting write_size to properly write page block

              if( ( (device_address & 0xFF)+write_size-1) >= 0x100)

                  write_size =  (unsigned long)0x100 -(device_address & 0xFF);

      #endif

              curr_device_address[0] = (unsigned char) ( ( device_address & 0x00FF0000 ) >> 16 );

              curr_device_address[1] = (unsigned char) ( ( device_address & 0x0000FF00 ) >>  8 );

              curr_device_address[2] = (unsigned char) ( ( device_address & 0x000000FF ) >>  0 );