cancel
Showing results for 
Search instead for 
Did you mean: 

dat mismatch when reading .bin file in stm32 for FOTA

ykn
Senior

Hi, I want to implement FOTA in stm32, for that I am reading .bin file and send to the controller through UART. And Write that file to the flash memory,

All Ihave done this, but there mismatch data beetween .bin file data written on flash.

the reading and sending . bin file have done this using GCC compiler.

below the function code.

    int main(int argc, char *argv[])
{
  int comport;
  int bdrate   = 115200;       /* 115200 baud */
  char mode[]={'8','N','1',0}; /* *-bits, No parity, 1 stop bit */
  char bin_name[1024];
  int ex = 0;
  FILE *Fptr = NULL;
 
  do
  {
    if( argc <= 2 )
    {
      printf("Please feed the COM PORT number and the Application Image....!!!\n");
      printf("Example: .\\etx_ota_app.exe 8 ..\\..\\Application\\Debug\\Blinky.bin");
      ex = -1;
      break;
    }
 
    //get the COM port Number
    comport = atoi(argv[1]) -1;
    strcpy(bin_name, argv[2]);
   // ex = sizeof(ETX_OTA_HEADER_);
   // printf("Opening COM%d...\n", ex );
    printf("Opening COM%d...\n", comport+1 );
 
    if( RS232_OpenComport(comport, bdrate, mode, 0) )
    {
      printf("Can not open comport\n");
      ex = -1;
      break;
    }
 
    //send OTA Start command
    ex = send_ota_start(comport);
    if( ex < 0 )
    {
      printf("send_ota_start Err\n");
      break;
    }
 
    printf("Opening Binary file : %s\n", bin_name);
 
    Fptr = fopen(bin_name,"rb");
 
    if( Fptr == NULL )
    {
      printf("Can not open %s\n", bin_name);
      ex = -1;
      break;
    }
 
    fseek(Fptr, 0L, SEEK_END);
    uint32_t app_size = ftell(Fptr);
    fseek(Fptr, 0L, SEEK_SET);
 
    printf("File size = %d\n", app_size);
 
    //Send OTA Header
    meta_info ota_info;
    ota_info.package_size = app_size;
    ota_info.package_crc  = 0;          //TODO: Add CRC
 
    ex = send_ota_header( comport, &ota_info );
    if( ex < 0 )
    {
      printf("send_ota_header Err\n");
      break;
    }
 
    //read the full image
    if(fread( APP_BIN, 1, app_size, Fptr ) != app_size )
    {
      printf("App/FW read Error\n");
      ex = -1;
      break;
    }
    for(uint32_t i = 0; i < app_size; i++)
      {
    	  printf("%d\r\n",APP_BIN[i]);
      }
    uint16_t size = 0;
 
    for( uint32_t i = 0; i < app_size; )
    {
      if( ( app_size - i ) >= ETX_OTA_DATA_MAX_SIZE )
      {
        size = ETX_OTA_DATA_MAX_SIZE;
      }
      else
      {
        size = app_size - i;
      }
      //printf("%d\r\n",  size);
      //printf("[%d/%d]\r\n", i/ETX_OTA_DATA_MAX_SIZE, app_size/ETX_OTA_DATA_MAX_SIZE);
 
      ex = send_ota_data( comport, &APP_BIN[i], size );
      if( ex < 0 )
      {
        printf("send_ota_data Err [i=%d]\n", i);
        break;
      }
 
      i += size;
    }
 
    if( ex < 0 )
    {
      break;
    }
 
    //send OTA END command
    ex = send_ota_end(comport);
    if( ex < 0 )
    {
      printf("send_ota_end Err\n");
      break;
    }    
 
  } while (false);
 
  if(Fptr)
  {
    fclose(Fptr);
  }
 
  if( ex < 0 )
  {
    printf("OTA ERROR\n");
  }
  return(ex);
}

if any one has faced the same issue can guide me.

thanking you

3 REPLIES 3
ykn
Senior

this one is original data when i used online .bin to hex converter

0x00, 0x00, 0x08, 0x24, 0xA1, 0x0D, 0x10, 0x08, 0xED, 0x0A, 0x10, 0x08, 0xF3, 0x0A, 0x10, 0x08, 

this is the data when fread function used

0x00, 0x00, 0x08, 0x24, 0x4D, 0x0C, 0x10, 0x08, 0x99, 0x09, 0x10, 0x08, 0x9F, 0x09, 0x10, 0x08,

TomRett
Associate III

Did you succeed? I am having a similiar task where i want to read the data contained in the .dat file. Can you provide your source code for this?

And what's the CORRECT data?

Say if you use a file viewer / manager, or hex editor on the PC with the original file.

 

Watch for parts of the system that handle CR/LF differently, want 7-bit data in transport path.

You should be able to CRC, SHA256 or MD5 the file in the application itself via standard test tools, and understand what's valid.

Watch for needs of flash to write in 4-byte or 8-byte units of alignment / granularity.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..