沃航产品全部支持ota更新,但是考虑到ota的风险,小沃写了个自动生产ota文件的工具,所用的算法是modbus-rtu所用的校验算法。话不多说,代码如下:
#include <stdio.h>
#define uint8_t unsigned char
#define uint16_t unsigned short
int main (int argc, char *argv[]) {
uint8_t file_buff[128]; // 这里可以调小,但是要大于文件名长度加5。
if (argc < 2) {
printf("parameter error!\n");
return -1;
}
FILE *fp1, *fp2;
if ((fp1 = fopen(argv[1], "rb")) == NULL) {
printf("%s open fail!\n", argv[1]);
return -2;
}
sprintf(file_buff, "%s.ota", argv[1]);
if ((fp2 = fopen(file_buff, "wb")) == NULL) {
printf("%s create fail!\n", file_buff);
return -3;
}
uint16_t crc = 0xffff; // 16位crc寄存器预置
size_t size;
do {
size = fread(file_buff, 1, sizeof(file_buff), fp1);
for (int i = 0 ; i < size ; i++) { // 循环计算每个数据
crc ^= file_buff[i]; // 将八位数据与crc寄存器亦或,然后存入crc寄存器
for (uint8_t j = 0; j < 8; j++) { // 循环计算数据的
if (crc & 0x0001) { // 判断右移出的是不是1,如果是1则与多项式进行异或。
crc >>= 1; // 将数据右移一位
crc ^= 0xa001; // 与上面的多项式进行异或
} else { // 如果不是1,则直接移出
crc >>= 1; // 将数据右移一位
}
}
}
fwrite(file_buff, 1, size, fp2);
} while (size != 0);
printf("file crc16 is 0x%04x\n", crc);
fclose(fp1);
file_buff[0] = crc;
file_buff[1] = crc >> 8;
fwrite(file_buff, 1, 2, fp2);
fclose(fp2);
return 0;
}使用时只需要将文件以命令行参数的形式送个本程序或是将文件直接拖到程序上。
最后会生成一个以.ota结尾的文件,就是新的文件了。
otafile.exe img.bin
文章作者:沃航科技