CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug Reports on this forum. Send them to support@ccsinfo.com

Embed bin file (with slave IC firmware) into PIC hex

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
silelis



Joined: 12 Jun 2007
Posts: 68
Location: Poland, podlaskie district

View user's profile Send private message

Embed bin file (with slave IC firmware) into PIC hex
PostPosted: Mon Apr 29, 2019 5:57 am     Reply with quote

Hello,

I am developing DAB+ tuner based on si468x chip.

There is boot sequence where the host (in my case PIC18) have to upload (via spi or i2c) some firmware (in bin) to DAB+ chip every time chip boots.

I know that I can upload this bin to eeprom, read it (by i2c) write it to si468x. For some reason, reading this bin from PIC flash is a must (in other case I have to redesign a huge amount of hardware) and at this moment I only allow build bin into PIC hex.

My question is how to embed this bin file into PIC hex during compilation?

I consider to rewrite (manually bin to rom char[]) but there is huge possibility of mistake!!!

I am searching for some solution like this:
Quote:

rom char [] = { /location_of_bin/binary_file.bin};
// of course it is only idea not final code


Does CCS allow to attach/include bin into PIC hex automatically?
Ttelmah



Joined: 11 Mar 2010
Posts: 14617

View user's profile Send private message

PostPosted: Mon Apr 29, 2019 11:21 am     Reply with quote

No, but with a program that allows you to output a file as values, and
a text editor, it is pretty easy to automatically convert the bin data
to a suitable format.
PCM programmer



Joined: 06 Sep 2003
Posts: 20759

View user's profile Send private message

PostPosted: Mon Apr 29, 2019 1:18 pm     Reply with quote

WinHex will do this. Download the eval version:
http://www.x-ways.net/winhex.zip
1. Unzip and install it.
2. Load your file.
3. Go to the Edit menu, then Copy All, then select C source.
This will convert the loaded binary data into C source and copy it to
the Windows clipboard.
4. Open your text editor (CCS IDE or MPLAB or other) and paste the
C source array data into your program. Rename the array if needed.
silelis



Joined: 12 Jun 2007
Posts: 68
Location: Poland, podlaskie district

View user's profile Send private message

PostPosted: Tue Apr 30, 2019 5:10 am     Reply with quote

Great tool. Thank You for that.

My bin2C_char_table is like this. And this is correct. (y)

Code:

char data[940] = {
   0x03, 0x00, 0x00, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x56, 0x34, 0x12,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2C, 0x5F, 0xBE, 0x2F, 0x01, 0x00, 0x00, 0x00,
   0xEF, 0xBE, 0xAD, 0xDE, 0x50, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x3C, 0x20, 0x50, 0x01, 0x00, 0x00, 0x00, 0x6A, 0x21, 0xD6, 0x4F,
   0x89, 0xA5, 0x4C, 0xD3, 0xD4, 0x6E, 0x92, 0x9F, 0xA9, 0x31, 0x42, 0x2E, 0x20, 0x0F, 0xE2, 0x2B,
   0x7F, 0x72, 0xC2, 0xD7, 0xF0, 0x52, 0x7E, 0xD1, 0x99, 0x89, 0x86, 0x4F, 0x55, 0xE7, 0x6D, 0xDE,
   0x98, 0xFF, 0xD8, 0xCC, 0x71, 0xCF, 0xD2, 0x78, 0x62, 0x58, 0xA4, 0x01, 0xA7, 0x34, 0x6D, 0x73,
   0x91, 0x31, 0xFA, 0x15, 0x50, 0x22, 0x9B, 0x78, 0xB5, 0x49, 0x2A, 0xD1, 0x5A, 0xDE, 0x36, 0x53,
   0x85, 0xDD, 0x6D, 0x46, 0xB8, 0x06, 0xDE, 0xEC, 0x8F, 0x25, 0xDC, 0x6A, 0x16, 0x4B, 0xD8, 0x66,
   0x3D, 0x9A, 0xA2, 0x8D, 0x68, 0xED, 0x67, 0xCB, 0xD1, 0x12, 0xAE, 0x75, 0x85, 0x44, 0x5C, 0x9B,
   0x83, 0xC8, 0xD6, 0x37, 0x96, 0x92, 0x85, 0x9F, 0x12, 0xB3, 0x7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0x68, 0x79, 0x93, 0x11, 0x7D, 0xAB, 0x17,
   0x1A, 0x44, 0x24, 0xD8, 0xB5, 0xDB, 0xE0, 0x0F, 0x9C, 0x51, 0x99, 0x0A, 0x08, 0xDA, 0x90, 0x71,
   0xA5, 0x12, 0xAF, 0x12, 0xF3, 0x3C, 0xBC, 0x8E, 0x17, 0x27, 0x11, 0x16, 0xF8, 0x22, 0x40, 0xFD,
   0xBC, 0x2B, 0xBB, 0x78, 0x25, 0xA7, 0x04, 0xF5, 0x00, 0x00, 0x00, 0x00
};


But I have other considerations...

BTW my mcu is PIC 18f4520

If I compile code like this:

Code:

void main()
{
rom char data[] = {
   0x03, 0x00, 0x00, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x56, 0x34, 0x12,
   ..............
};
   while(TRUE)
   {
      //TODO: User Code
   }
}


RAM utilisation is 61% //takes too much RAM :/
ROM utilisation is 3%

Then I tried other options:
Code:

//FIRST code
void main()
{
rom int16 data[] = {
0x0300,0x00fe,0x0000,0x0000,0x0000,0x0000,0x7856,0x3412,
   ..............
};
   while(TRUE)
   {
      //TODO: User Code
   }
}

but in this case
RAM utilisation is still 61% //takes to much RAM, :/
ROM utilisation is still 3% //and there is no difference betewwn char and int16 - flash is 16 bits? :/

So I tried with #rom directive
Code:

//SECOND code
void main()
{
#rom char 0x1000 = {
0x03,0x00,0x00,0xfe,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x78,0x56,0x34,0x12,
......
};
   while(TRUE)
   {
      //TODO: User Code
   }
}

RAM utilisation is 0% //looks better because of RAM utilisation
ROM utilisation is 3%

Code:

//THIRD code
void main()
{
#rom int16 0x1000 = {
0x0300,0x00fe,0x0000,0x0000,0x0000,0x0000,0x7856,0x3412,
......
};
   while(TRUE)
   {
      //TODO: User Code
   }
}

RAM utilisation is still 0%
ROM utilisation is still 3% //as I remember from PICKIT2 software flash is "FFFF" 16 bits so I tried to "compres" char data to int16 but it does not take 50% rom efest as I assume. :(

So I have 3 more questions:
1) Why there is no difference (FLASH utilisation) between char table like 0x01, 0x02, 0x03x 0x04 and int16 table like 0x0102, 0x0304? I thought that flash is 16 bits? Do CCS care about flash utilisation and during compilations "pack" it?
2) How can determinie #rom char/int address during compilation useing compiler directives.
Let say to check:
a) max flash size
b) check if there is no OSCALC (some PICs have sutch data at the end),
c) know that my table is 940 in this case
d) #define rom_address = max_flash-table_space-pic_end_data.
Is it possible to do so?
3) If I want to send this #rom 0x1000 table via I2c or SPI I should read it as:
a) pointer to table:
Code:

char/int16 *table_pointer;
 table_pointer= 0x1000;

b) or read as program memory
Code:

READ_PROGRAM_MEMORY (address, dataptr, count );
Ttelmah



Joined: 11 Mar 2010
Posts: 14617

View user's profile Send private message

PostPosted: Tue Apr 30, 2019 7:00 am     Reply with quote

Look at the .lst file.

It is 'silly' that things declared with #rom don't get listed as ROM usage,
but it has always been the case. However they do show in the .lst.
At the end of the file, after the fuses it shows 'ROM data'.
silelis



Joined: 12 Jun 2007
Posts: 68
Location: Poland, podlaskie district

View user's profile Send private message

PostPosted: Mon May 06, 2019 6:41 am     Reply with quote

If someone need it, here is a simple C program (for PC/ MAC/ other desktop) that converts bin to array.

Do the same as WinHEX.

Code:

#include <stdio.h>
#include <assert.h>

int main(int argc, char** argv) {
   assert(argc == 2);
   char* fn = argv[1];
   FILE* f = fopen(fn, "rb");
   printf("char a[] = {\n");
   unsigned long n = 0;
   while(!feof(f)) {
      unsigned char c;
      if(fread(&c, 1, 1, f) == 0) break;
      printf("0x%.2X,", (int)c);
      ++n;
      if(n % 16 == 0) printf("\n");
   }
   fclose(f);
   printf("\b};\n");
}
silelis



Joined: 12 Jun 2007
Posts: 68
Location: Poland, podlaskie district

View user's profile Send private message

PostPosted: Thu May 23, 2019 6:30 am     Reply with quote

I have modified program so now it is writing to ".c" file directly. If Someone needs.

Code:

#include <cassert>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <sstream>
#include <iomanip>
#include <string>

using namespace std;

string intToHexString(int);

int main(int argc, char** argv) {
   assert(argc == 2);

   string inputFileName = argv[1];
   string readString;
   fstream fileInput;
   fileInput.open(inputFileName.c_str(), fstream::in | fstream::binary);

   cout<<"Hex2char_array v0.1."<<endl<<"by SileliS 2019"<<endl<<endl;
   if (!fileInput.is_open())
   {
      cout<<endl<<"Unable to open file: "<<inputFileName<<endl;
      cout<<"Please check whether file exists and you have read/write privilege.";
      exit(-1);
   }

   unsigned long n = 0;
   while(!fileInput.eof())
   {
      char c;
      fileInput.read(&c, 1);
      if(fileInput.fail()==1)   break;
      readString = readString + intToHexString( (int) (unsigned char) c ) + ",";
      ++n;
      if(n % 16 == 0) readString=readString+"\n";
   }
   fileInput.close();

   readString.resize (readString.size () - 1);
   readString = readString + "};\n";

   stringstream ss;
   ss <<n;
   string final_string;
   final_string = "char a[" + ss.str() + "] = {\n"+ readString;


   string newFileName = inputFileName.c_str();
   newFileName=newFileName+".c";
   fstream fileOutput;

   fileOutput.open(newFileName.c_str(), fstream::out);
   if (!fileOutput.is_open())
   {
      cout<<endl<<"Unable to open file: "<<newFileName<<endl;
      cout<<"Please check whether file exists and you have read/write privilege.";
      exit(-1);
   }
   cout<<final_string;
   fileOutput<<final_string;
   fileOutput.close();
}

string intToHexString(int intValue) {

    string hexStr;

    /// integer value to hex-string
    std::stringstream sstream;
    sstream << "0x"
            << std::setfill ('0') << std::setw(2)
    << std::hex << (int)intValue;

    hexStr= sstream.str();
    sstream.clear();    //clears out the stream-string

    return hexStr;
}
Ttelmah



Joined: 11 Mar 2010
Posts: 14617

View user's profile Send private message

PostPosted: Thu May 23, 2019 10:41 am     Reply with quote

As a comment (bit late), it is actually possible to embed the binary file
using #IMPORT. You use the RAW option, and it can return you a 'const'
which contains where the data has been stored.

So something like:

#IMPORT(FILE=DABDAT.bin, RAW, LOCATION=where_stored, SIZE=2)

will generate a constant called 'where_stored', which contains the location
where 'DABDAT.bin' has been placed in the ROM.
silelis



Joined: 12 Jun 2007
Posts: 68
Location: Poland, podlaskie district

View user's profile Send private message

PostPosted: Thu May 23, 2019 12:17 pm     Reply with quote

Great news. :-)
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group