Ravi
Ravi

Reputation: 29

how to write .hex file on internal Flash ROM of microcontroller?

I want to write a ".hex" file of size 28 kb flash on to that Internal flashROM of size 32 kb.The above mentioned question is for initialising the flashROM. What i am doing for writing that file and the code is mentioned below:

Please let me know where I am going wrong. The code is given below.

int a,b; int size,last_chunk; FILE *file; char *buffer1,name[20]; unsigned long fileLen;

file = fopen("flashROM.hex", "rb+");
if (!file)
{
    fprintf(stderr, "Unable to open file %s", name);
    return;
}

fseek(file, 0, SEEK_END);
fileLen=ftell(file);
printf("the file length is:%d\n",fileLen);
fseek(file, 0, SEEK_SET);

//Allocate memory
buffer1 =(char *)malloc(fileLen+1);

if (!buffer1)
{
    fprintf(stderr, "Memory error!");
            fclose(file);
    return;
}

//Read file contents into buffer
fread(buffer1, fileLen, 1, file);
/* We have to divide the entire file into chunks of 256 bytes*/
size = fileLen/256;
printf("\nsize = %d\n",size);
last_chunk = fileLen%256;
printf("\nlast chunk = %d bytes\n",last_chunk);

address = 0x0000;
printf("File upgradation should start from :%.4x",address);

for(a=0;a<=size;a++)
{
    write(fd,&buffer1,size);
    printf("Iteration=[%d]\t Data=[%x]\n",a,*buffer1);
    usleep(5000);
}
for(b=0;b<=last_chunk;b++)
{
    write(fd,&buffer1,1);
    usleep(5000);
}

After executing the binary of above mentioned program, my result is mentioned below:

    Writing upgrade file
    the file length is:30855

    size = 120

    last chunk = 135 bytes
    File upgradation should start from :0000
    Iteration=[0]    Data=[3a]
    Iteration=[1]    Data=[3a]
    Iteration=[2]    Data=[3a]
    Iteration=[3]    Data=[3a]
    Iteration=[4]    Data=[3a]
    Iteration=[5]    Data=[3a]
    Iteration=[6]    Data=[3a]
    Iteration=[7]    Data=[3a]

I don't know, why the data is always "3a", its not clear. Please let me know where i have done wrong in programming.

Upvotes: 0

Views: 5779

Answers (2)

simonc
simonc

Reputation: 42175

Try using "wb+" as your mode for fopen

Opening the file as writable will create it if it doesn't already exist.

The code where you write data also looks suspect. You're passing a pointer to a pointer to a buffer into write rather than a simple pointer to your buffer. I also don't see the pointer getting incremented so you're writing the same data repeatedly.

You could try replacing your writing code with something like the following:

char* ptr = buffer1;
for(a=0;a<=size;a++)
{
    write(fd,ptr,size);
    ptr+=size;
    printf("Iteration=[%d]\t Data=[%x]\n",a,*buffer1);
    usleep(5000);
}
for(b=0;b<=last_chunk;b++)
{
    write(fd,ptr,1);
    usleep(5000);
}

Upvotes: 2

starblue
starblue

Reputation: 56772

You need a special tool that reads the .hex file and does whatever is necessary to write it into the flash memory of your controller (JTAG, talk to a bootloader via whatever means of communication, ...).

The tool depends on your specific microcontroller family. 8051 is not enough information, there is a huge variety of 8051s from many vendors.

Upvotes: 5

Related Questions