0

我已经通过 uart 为微控制器制作了一个 C 程序闪存映像。在这段代码中,我遵循一个协议,一切正常。但是当我通过 uart 将图像加载到微控制器时,写入工作正常但读取后被阻止读取 33 个字节。我正在逐字节写入和读取。加载图像的函数的声明和定义如下所示:

函数声明:

load_RAM_image(file_size, file_buff);

这里 file_size 是 unsigned int 并且它的值是 40,980KB 并且 file_buff 是指向图像缓冲区的 unsigned char 指针。

函数声明:

#define BYTE_WRITE 1

#define DELAY 10000

int load_RAM_image(unsigned int buff_size, unsigned char *buff)
{
    unsigned int count, num_Wbytes, num_Rbytes, byte;
    for (count = 0; count < buff_size;)  /* increase count value to every time */
    { 
        num_Wbytes = write( serial_fd, buff + count, BYTE_WRITE );
        if (num_Wbytes < 0)
        {
            fputs("write failed!\n", stderr);
            return -1;
        }

        tcdrain(serial_fd);
        usleep(DELAY);
        num_Rbytes = read( serial_fd, rbuff + count, BYTE_WRITE );
        if (num_Rbytes < 0)
        {
            if (errno == EAGAIN)
            {
                printf("SERIAL EAGAIN ERROR\n");
                return -EAGAIN;
            }
            else
            {
                printf("SERIAL read error %d %s\n", errno, strerror(errno));
            }
        }

        printf("wbuf[ %d ] = %02x   rbuff = %02x\n\n", 
               count /*+ byte*/, *(buff + count/* + byte*/), rbuff[count]);

        /* Compare the received byte from BAM */
        if (strncmp(buff + count, rbuff + count, BYTE_WRITE ) < 0)
        {
            printf("Error : RAM loading error(W != R)\n");
            return -1;                      
        }

        count += BYTE_WRITE  ;
        printf("count = %d\n", count);
    }

    return 0;
}
4

0 回答 0