c - 如何在单片机内部Flash ROM中写入.hex文件?

标签 c linux embedded

我想将一个大小为 28 kb 的“.hex”文件写入到那个大小为 32 kb 的 Internal flashROM 中。上面提到的问题是关于初始化 flashROM。我正在为编写该文件和下面提到的代码所做的工作:

  • 我有一个 Intel 十六进制格式的 .hex 文件,我必须读取和写入内部 At91(8051)微 Controller FlashROM。
  • 以“rb+”模式打开.hex 文件。
  • 获取文件的长度并将指针设置为起始地址(第零地址)。
  • 因为我需要一页一页地写那个文件,在我的例子中,页面大小是 256 字节,所以我有 将文件除以 256。
  • 之后,我尝试编写该文件。

请让我知道我要去哪里错了。代码如下。

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);
}

执行上述程序的二进制文件后,我的结果如下:

    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]

我不知道,为什么数据总是“3a”,不清楚。 请让我知道我在编程中哪里做错了。

最佳答案

您需要一个特殊的工具来读取 .hex 文件并执行任何必要的操作将其写入 Controller 的闪存(JTAG,通过任何通信方式与引导加载程序对话,. ..).

该工具取决于您的特定微 Controller 系列。 8051 的信息不够,许多供应商提供的 8051 种类繁多。

关于c - 如何在单片机内部Flash ROM中写入.hex文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13742432/

相关文章:

linux - 如何修复 Linux bash 的 'find' 命令中包含空格的路径

linux - 如何在不使用 etimes 的情况下获取进程启动后的秒数?

Linux命令服务通用

c - 此字符副本如何工作 : *(*((char**)p))++ = ch; in c

Linux内核 panic

c - 在C中如何同时检测输入是字符还是整数?

C - 在函数中将值存储在数组中

c - C中的链表程序

c - 为什么 WiringPiISR 在被触发时不阻塞主例程?

c - 查找字符串中最常见的一对字符