我想写一个大小为28 kb闪存的".hex"文件到大小为32 kb的内部闪存ROM.上面提到的问题是初始化flashROM.我正在做什么来编写该文件和代码如下所述:
请让我知道我哪里出错了.代码如下.
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);
}
Run Code Online (Sandbox Code Playgroud)
执行上述程序的二进制文件后,我的结果如下:
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]
Run Code Online (Sandbox Code Playgroud)
我不知道,为什么数据总是"3a",其不清楚.请让我知道我在编程方面做错了什么.
您需要一个特殊的工具来读取.hex文件并执行将其写入控制器闪存所需的任何内容(JTAG,通过任何通信方式与引导加载程序通信,......).
该工具取决于您的特定微控制器系列.8051是不够的信息,来自许多供应商的8051s种类繁多.