stm32cubemx IAP升级(六)

stm32cubemx IAP升级- 将bootloader.bin settings.bin App.bin打包成一个bin

量产的时候三个bin文件烧录确实不方便,那我们写个程序将这三个bin文件打包成一个bin,直接从0x08000000处烧录即可。

代码如下

#include 
#include 

#define BOOT_SECTOR_ADDR                0x08000000                  // BOOT sector start address 
#define BOOT_SECTOR_SIZE                0x5000                      // BOOT sector size
#define SETTING_SECTOR_ADDR             0x08005000                  // SETTING sector start address 
#define SETTING_SECTOR_SIZE             0x2000                      // SETTING sector size
#define APP_START_SECTOR_ADDR           0x08007000                  // APP sector start address  
#define APP_END_SECTOR_ADDR             0x08013000                  // APP sector start address  

#define APP_SECTOR_SIZE                 0xC800                      // APP sector size
#define DOWNLOAD_START_SECTOR_ADDR      0x08013800                  // Download sector start address
#define DOWNLOAD_END_SECTOR_ADDR        0x0801F800                  // Download sector start address

#define DOWNLOAD_SECTOR_SIZE            0xC800                      // Download sector size   

#define ALL_BIN_LENGTH (20 + 8 + 50) * 1024

int copy_bootloader_bin(unsigned char *p_buff)
{   
    FILE *file = fopen("../../../BinFiles/BootLoader.bin","rb");
    unsigned char r_buff[BOOT_SECTOR_SIZE] = {0};
    size_t r_size;
    size_t file_size;

    if (file == NULL)
    {
        printf("fopen BootLoader error\n");
        return -1;
    }

    fseek(file,0,SEEK_END);
    file_size = ftell(file);
    fseek(file,0,SEEK_SET);

    r_size = fread(r_buff,sizeof(unsigned char),sizeof(r_buff),file);
    if (r_size != file_size)
    {
        printf("read bootloader size == %ld\n",r_size);
    }
    else
    {
        printf("read bootloader file ok\n" );
    }
    memcpy(p_buff,r_buff,r_size);

    fclose(file);
    return 0;
}

int copy_settings_bin(unsigned char *p_buff)
{

    FILE *file = fopen("../../../BinFiles/settings.bin","rb");
    unsigned char r_buff[SETTING_SECTOR_SIZE] = {0};
    size_t r_size;
    size_t file_size;

    if (file == NULL)
    {
        printf("fopen Application error\n");
        return -1;
    }

    fseek(file,0,SEEK_END);
    file_size = ftell(file);
    fseek(file,0,SEEK_SET);

    r_size = fread(r_buff,sizeof(unsigned char),sizeof(r_buff),file);
    if (r_size != file_size)
    {
        printf("read settings size == %ld\n",r_size);
    }
    else
    {
        printf("read settings file ok\n" );
    }
    p_buff += (BOOT_SECTOR_SIZE);
    memcpy(p_buff,r_buff,r_size);

    fclose(file);
    return 0;

}

int copy_application_bin(unsigned char *p_buff)
{

    FILE *file = fopen("../../../BinFiles/Application.bin","rb");
    unsigned char r_buff[APP_SECTOR_SIZE] = {0};
    size_t r_size;
    size_t file_size;

    if (file == NULL)
    {
        printf("fopen Application error\n");
        return -1;
    }

    fseek(file,0,SEEK_END);
    file_size = ftell(file);
    fseek(file,0,SEEK_SET);

    r_size = fread(r_buff,sizeof(unsigned char),sizeof(r_buff),file);
    if (r_size != file_size)
    {
        printf("read Application size == %ld\n",r_size);
    }
    else
    {
        printf("read Application file ok\n" );
    }

    p_buff += (BOOT_SECTOR_SIZE + SETTING_SECTOR_SIZE);
    memcpy(p_buff,r_buff,r_size);

    fclose(file);
    return 0;
}

int main(int argc,char *argv[])
{
    
    int ret;
    unsigned char all_data[ALL_BIN_LENGTH] = {0};
    size_t w_size = 0;

    ret = copy_bootloader_bin(all_data);

    if (ret != 0)
    {
        printf("copy_bootloader_bin error\n");
        return -1;
    }

    ret = copy_settings_bin(all_data);

    if (ret != 0)
    {
        printf("copy_settings_bin error\n");
        return -1;
    }

    ret = copy_application_bin(all_data);

    if (ret != 0)
    {
        printf("copy_application_bin error\n");
        return -1;
    }

    FILE *file = fopen("../../../BinFiles/all.bin","wb");

    if (file == NULL)
    {
        printf("fopen all.bin error\n");
        return -1;
    }

    w_size = fwrite(all_data,1,(BOOT_SECTOR_SIZE + SETTING_SECTOR_SIZE + APP_SECTOR_SIZE),file);
    fclose(file);

    if (w_size == BOOT_SECTOR_SIZE+SETTING_SECTOR_SIZE+APP_SECTOR_SIZE)
    {
        printf("Create bin is Ok\n");
    }
    else
    {
        printf("Create bin error\n");
        return -1;
    }
    return 0;
}

代码编译完,生成一个allinone的二进制执行文件,运行后会将特定文件夹里的bootloader.bin settings.bin application.bin打包生成一个all.bin.

最后直接烧录all.bin即可。

整个工程下载后,可以在顶层目录make clean;make即可.

你可能感兴趣的:(stm32,Iap升级,stm32,单片机,嵌入式硬件)