stm32cubemx IAP升级(五)

stm32cubemx IAP升级- 如何写初始值到setting区域

Setting区其实就是个8k大小的文件。我们可以用8k大小的数组,使用流按照二进制方式创建一个bin文件,然后将要生产用的原始数据写到这个bin文件里,最后按照0x08005000的地址烧录进去即可。

代码里演示的比较复杂,有一部分数据是从ini配置文件里独取出来,然后写到setting数据里的。
但整体的思想是这样的

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include 
#include 

#include "iniparser.h"

typedef enum{

    MI_FALSE  =   0,
    MI_TRUE   =   1,

}MI_BOOL;

typedef enum{
    
    MI_SUCCESS,
    MI_FAILURE, 
}MI_Status;

typedef unsigned char       MI_U8;
typedef char                MI_S8;
typedef unsigned short      MI_U16;
typedef short               MI_S16;
typedef unsigned int        MI_U32;


typedef int                 MI_S32;
typedef float               MI_FLOAT;
typedef double              MI_DOUBLE;
typedef void                MI_VOID;

typedef char                MI_CHAR;

#define MI_NULL             NULL

#define     COLOR_NONE                      "\033[0m"
#define     FONT_COLOR_RED                  "\033[0;31m"
#define     FONT_COLOR_BLUE                 "\033[1;34m"
#define     FONT_COLOR_YELLOW               "\033[0;33m"
#define     BACKGROUND_COLOR_RED            "\033[41m"    
#define     BG_RED_FONT_YELLOW              "\033[41;33m"

#define     SETTINGS_START_ADDR             0x08005000 // 整个settings的起始地址
#define     SETTINGS_END_ADDR               0x08007000 // 整个settings的结束地址
#define     SYSTEM_INFO_START_ADDR          0x08005000 // 系统信息的起始地址 位于第12个页
#define     SYSTEM_INFO_END_ADDR            0x08005800 // 系统信息的结束地址 位于第12个页
#define     AMP_INFO_START_ADDR             0x08005800 // 功放信息的起始地址 位于第12个页
#define     AMP_INFO_END_ADDR               0x08007000 // 功放信息的结束地址 位于第12个页
#define     YDA174_SSO_DATA_LENGHT                  288
#define     YDA174_S3D_DATA_LENGTH                  306



typedef enum
{
    YDA_SSO_POSITION_LEFT       = 0x00,
    YDA_SSO_POSITION_MIDDLE     = 0x01,
    YDA_SSO_POSITION_RIGHT      = 0x02,
    YDA_SSO_POSITION_NUM        = 0x03, 

}YDA174_SSO_Position;

typedef enum{

    AMP_LR_CHANNEL_TYPE         = 0x00,
    AMP_SKY_CHANNEL_TYPE        = 0x01,
    AMP_CENTER_CHANNEL_TYPE     = 0x02,
    AMP_BASS_CHANNEL_TYPE       = 0x03,
    AMP_ROUND_CHANNEL_TYPE      = 0x04,

    AMP_CHANNEL_TYPE_MAX_NUM    = 0x5,
}MI_Amp_Channel_Type;


typedef enum {

    AMP_S3D_HALL_TYPE       = 0x00,
    AMP_S3D_NATURAL_TYPE    = 0x01,
    AMP_S3D_STADIUM_TYPE    = 0x02,
    AMP_S3D_TYPE_NUM        = 0x03,
}MI_Amp_S3d_Type;

typedef struct 
{
    /* data */
    MI_U8   bChannelIsMute[AMP_CHANNEL_TYPE_MAX_NUM];
    MI_U8   bChannelGainValue[AMP_CHANNEL_TYPE_MAX_NUM];
    MI_U8   u8S3dData[AMP_S3D_TYPE_NUM][YDA174_S3D_DATA_LENGTH];

    MI_U8     bSsoOn;
    MI_U8     enSsoPosition;
    MI_U8     enS3dType;  
}MI_GainMuteInfo;


/**
 * 系统信息结构体 与setting.h 里要同步
*/
typedef struct system_info
{
    /* data */
    char model_name[64];
    char band_name[12];
    char soft_version[36];
    char settings_version[36];
    char app_version[36];
    char boot_version[36];

    int build_date;
    int build_time;

    unsigned char  update_flag;
    MI_GainMuteInfo gain_mute_info;

}MI_SystemInfo;


// 功放eq参数,按型号不同

#define YDA174_PEQ_DATA_LENGTH                  210
#define YDA174_PEQ_ON_OFF_DATA_LENGTH           4

#define YDA174_CROSSOVER_DATA_LENGHT            90
#define YDA174_CHANNEL_WAY_DATA_LENGTH          8
#define YDA174_CORSSOVER_ON_OFF_DATA_LENGTH     8     

#define YDA174_POWER_LIMIT_DATA_LENGTH          66
#define YDA174_POWER_LIMIT_ON_OFF_DATA_LENGTH   8

#define YDA174_SSO_ON_OFF_DATA_LENGTH           4



#define NTP8928_SECTION_ZERO_LENGHT             12
#define NTP8928_SECTION_ONE_LENGHT              150
#define NTP8928_SECTION_TWO_LENGHT              36
#define NTP8928_SECTION_THREE_LENGHT            400
#define NTP8928_SECTION_FOUR_LENGHT             2
#define NTP8928_SECTION_FIVE_LENGHT             150
#define NTP8928_SECTION_SIX_LENGHT              50
#define NTP8928_SECTION_SEVEN_LENGHT            24
#define NTP8928_SECTION_EIGHT_LENGHT            12

#define INI_PATH       "../../Amp/AmpFiles/"  

typedef struct
{
    /* data */
    MI_U8 u8CorssData[YDA174_CROSSOVER_DATA_LENGHT];
    MI_U8 u8PowerLimitData[YDA174_POWER_LIMIT_DATA_LENGTH];
    MI_U8 u8PeqData[YDA174_PEQ_DATA_LENGTH];
    MI_U8 u8SsoData[YDA_SSO_POSITION_NUM][YDA174_SSO_DATA_LENGHT];
}Yda174_Amp_Data;

typedef struct 
{
    /* data */
    MI_U8 u8Section0[NTP8928_SECTION_ZERO_LENGHT];
    MI_U8 u8Section1[NTP8928_SECTION_ONE_LENGHT];
    MI_U8 u8Section2[NTP8928_SECTION_TWO_LENGHT];
    MI_U8 u8Section3[NTP8928_SECTION_THREE_LENGHT];
    MI_U8 u8Section4[NTP8928_SECTION_FOUR_LENGHT];
    MI_U8 u8Section5[NTP8928_SECTION_FIVE_LENGHT]; 
    MI_U8 u8Section6[NTP8928_SECTION_SIX_LENGHT];
    MI_U8 u8Section7[NTP8928_SECTION_SEVEN_LENGHT];
    MI_U8 u8Section8[NTP8928_SECTION_EIGHT_LENGHT];

}Ntp8928_Amp_Data;

typedef enum yda_eq_data
{
    YDA174_AMP_LR_CHANNEL       = 0x00,
    YDA174_AMP_SKY_CHANNEL      = 0x01,
    YDA172_AMP_CHANNEL_MAX_NUM  = 0x02,

}MI_Yda_Amp_Channel_Type;

typedef enum 
{
    NTP_AMP_CENTER_CHANNEL          = 0x00,
    NTP_AMP_SURROUND_CHANNEL        = 0x01,
    NTP_AMP_BASS_CHANNEL            = 0x02,

    NTP_AMP_CHANNEL_MAX_NUM         = 0x03,
}MI_Ntp_Amp_Channel_Type;

typedef struct amp_type
{
    Yda174_Amp_Data stYdaAmpData[YDA172_AMP_CHANNEL_MAX_NUM];
    Ntp8928_Amp_Data stNtpAmpData[NTP_AMP_CHANNEL_MAX_NUM];

}Amp_Data;

void print_register_value(MI_U8 *p_buff,MI_U16 len)
{
    for(int i=0;itm_year + 1900,p->tm_mon + 1,p->tm_mday,
                                            p->tm_hour + 8,p->tm_min,p->tm_sec);

    return 0;
}

int scan_dir(const char *dirname, char *folder_name)
{
    DIR *dir;
    struct dirent *de;
    dir = opendir(dirname);
    if(dir == NULL)
    {
        printf("opendir error\n");
        return -1;
    }
    
    while((de = readdir(dir))) {
        if(de->d_name[0] == '.' &&
           (de->d_name[1] == '\0' ||
            (de->d_name[1] == '.' && de->d_name[2] == '\0')) || 
             strstr(de->d_name,"._") != NULL || strstr(de->d_name,".DS") != NULL 
             || strstr(de->d_name,"_") != NULL)
            continue;

            memcpy(folder_name,de->d_name,strlen(de->d_name));
            break;  //只取第一个文件夹名字
    }
    closedir(dir);
    return 0;
}

int string_parse(const char *p_buff,unsigned char *p_data)
{
    char *every_data = NULL;
    while (p_buff && p_buff != NULL) 
    {
        every_data = strsep((char **)&p_buff, ",");
		sscanf(every_data,"%x",(unsigned int *)p_data);
        p_data++;
	}
    return 0;
}

int string_parse2(const char *p_buff,unsigned char *p_data)
{
    char *every_data = NULL;
    while (p_buff && p_buff != NULL) 
    {
        every_data = strsep((char **)&p_buff, " ");
		sscanf(every_data,"%x",(unsigned int *)p_data);
        p_data++;
	}
    return 0;
}

int set_ntp8928_eq_data(char *path, Ntp8928_Amp_Data *amp_data)
{
    dictionary * dic = MI_NULL;
    dic = iniparser_load(path);

    if (dic == NULL)
    {
        printf(FONT_COLOR_YELLOW" open %s error\n"COLOR_NONE,path);
        return -1;
    }

    const char *section_0_data = dictionary_get(dic, "eq_param:section_0", "");
    string_parse(section_0_data,amp_data->u8Section0);
 
    const char *section_1_data = dictionary_get(dic, "eq_param:section_1", "");
    string_parse(section_1_data,amp_data->u8Section1);

    const char *section_2_data = dictionary_get(dic, "eq_param:section_2", "");
    string_parse(section_2_data,amp_data->u8Section2);

    const char *section_3_data = dictionary_get(dic, "eq_param:section_3", "");
    string_parse(section_3_data,amp_data->u8Section3);

    const char *section_4_data = dictionary_get(dic, "eq_param:section_4", "");
    string_parse(section_4_data,amp_data->u8Section4);

    const char *section_5_data = dictionary_get(dic, "eq_param:section_5", "");
    string_parse(section_5_data,amp_data->u8Section5);

    const char *section_6_data = dictionary_get(dic, "eq_param:section_6", "");
    string_parse(section_6_data,amp_data->u8Section6);

    const char *section_7_data = dictionary_get(dic, "eq_param:section_7", "");
    string_parse(section_7_data,amp_data->u8Section7);

    const char *section_8_data = dictionary_get(dic, "eq_param:section_8", "");
    string_parse(section_8_data,amp_data->u8Section8);

    // for(int i=0;iu8Section0);i++)
    //     printf("%x\n",amp_data->u8Section0[i]);
    
    iniparser_freedict(dic);

    return 0;
}

int set_yda174_sky_sso_data(char *path,Yda174_Amp_Data *yda_amp_data)
{
    dictionary * dic = MI_NULL;
    dic = iniparser_load(path);

    if (dic == NULL)
    {
        printf(FONT_COLOR_YELLOW" open %s error\n"COLOR_NONE,path);
        return -1;
    }

    const char *sky_sso_left = dictionary_get(dic, "sso:sso_sky_left_data", "");
    string_parse2(sky_sso_left,yda_amp_data->u8SsoData[YDA_SSO_POSITION_LEFT]);

    const char *sky_sso_middle = dictionary_get(dic, "sso:sso_sky_middle_data", "");
    string_parse2(sky_sso_middle,yda_amp_data->u8SsoData[YDA_SSO_POSITION_MIDDLE]);

    const char *sky_sso_right = dictionary_get(dic, "sso:sso_sky_right_data", "");
    string_parse2(sky_sso_right,yda_amp_data->u8SsoData[YDA_SSO_POSITION_RIGHT]);

    //print_register_value(yda_amp_data->u8SsoData[YDA_SSO_POSITION_RIGHT],288);

    iniparser_freedict(dic);

    return 0;
}

int set_yda174_lr_sso_data(char *path,Yda174_Amp_Data *yda_amp_data)
{
    dictionary * dic = MI_NULL;
    dic = iniparser_load(path);

    if (dic == NULL)
    {
        printf(FONT_COLOR_YELLOW" open %s error\n"COLOR_NONE,path);
        return -1;
    }

    const char *lr_sso_left = dictionary_get(dic, "sso:sso_left_data", "");
    string_parse2(lr_sso_left,yda_amp_data->u8SsoData[YDA_SSO_POSITION_LEFT]);

    //print_register_value(yda_amp_data->u8SsoData[YDA_SSO_POSITION_LEFT],288);

    const char *lr_sso_middle = dictionary_get(dic, "sso:sso_middle_data", "");
    string_parse2(lr_sso_middle,yda_amp_data->u8SsoData[YDA_SSO_POSITION_MIDDLE]);

    const char *lr_sso_right = dictionary_get(dic, "sso:sso_right_data", "");
    string_parse2(lr_sso_right,yda_amp_data->u8SsoData[YDA_SSO_POSITION_RIGHT]);

    //print_register_value(yda_amp_data->u8SsoData[YDA_SSO_POSITION_RIGHT],288);

    iniparser_freedict(dic);

    return 0;
}

int set_yda174_eq_data(char *path, Yda174_Amp_Data *yda_amp_data)
{
    dictionary * dic = MI_NULL;
    dic = iniparser_load(path);

    if (dic == NULL)
    {
        printf(FONT_COLOR_YELLOW" open %s error\n"COLOR_NONE,path);
        return -1;
    }

    const char *lr_cf_data = dictionary_get(dic, "eq_param:cf_data_1", "");
    string_parse(lr_cf_data,yda_amp_data->u8CorssData);

    // for(int i=0;iu8PowerLimitData);  

    // for(int i=0;iu8PeqData);  

    // for(int i=0;iu8PeqData);i++)
    //     printf("%x\n",yda_amp_data->u8PeqData[i]);
    
    iniparser_freedict(dic);

    return 0;
}

int set_yda174_s3d_data(char *path, MI_GainMuteInfo *info)
{

    dictionary * dic = MI_NULL;
    dic = iniparser_load(path);

    if (dic == NULL)
    {
        printf(FONT_COLOR_YELLOW" open %s error\n"COLOR_NONE,path);
        return -1;
    }

    const char *hall_data = dictionary_get(dic, "s3d:s3d_hall_data", "");
    string_parse2(hall_data,info->u8S3dData[AMP_S3D_HALL_TYPE]);

    const char *nature_data = dictionary_get(dic, "s3d:s3d_natural_data", "");
    string_parse2(nature_data,info->u8S3dData[AMP_S3D_NATURAL_TYPE]);

    const char *stadium_data = dictionary_get(dic, "s3d:s3d_stadium_data", "");
    string_parse2(stadium_data,info->u8S3dData[AMP_S3D_STADIUM_TYPE]);

    //print_register_value(info->u8S3dData[2],306);

    iniparser_freedict(dic);
    return 0;

}

int main(int argc,char *argv[])
{
    int ret = 0;
    
    MI_SystemInfo system_info;
    Amp_Data amp_data;
    char current_time[64] = {0};
    FILE *file;
    size_t w_size;
    char lr_path[48] = {0};
    char sky_path[48] = {0};
    char center_path[64] = {0};
    char bass_path[64] = {0};
    char sur_path[64] = {0};
    char sso_path[64] = {0};
    char s3d_path[64] = {0};

    printf("system info size %lu\n",sizeof(system_info));
    printf("amp info size %lu\n",sizeof(amp_data));
    printf("amp MI_GainMuteInfo size %lu\n",sizeof(MI_GainMuteInfo));
    printf("amp MI_GainMuteInfo size %lu\n",sizeof(system_info.gain_mute_info.u8S3dData));
    printf("amp MI_GainMuteInfo size %lu\n",sizeof(system_info.gain_mute_info.bChannelGainValue));
    printf("amp MI_GainMuteInfo size %lu\n",sizeof(system_info.gain_mute_info.bChannelIsMute));
    printf("amp MI_GainMuteInfo size %lu\n",sizeof(system_info.gain_mute_info.bSsoOn));
    printf("amp MI_GainMuteInfo size %lu\n",sizeof(system_info.gain_mute_info.enSsoPosition));
    printf("amp MI_GainMuteInfo size %lu\n",sizeof(system_info.gain_mute_info.enS3dType));

    memset(&system_info,0,sizeof(system_info));
    memset(&_data,0,sizeof(amp_data));

    ret = scan_dir("../../Amp/AmpFiles",system_info.model_name);
    if (ret == 0)
    {
        printf(FONT_COLOR_YELLOW"scan_dir:find Amp Ini Folder :%s\n"COLOR_NONE, system_info.model_name);
    }
    else
    {
        printf(FONT_COLOR_RED"scan_dir:con't find Amp Ini Folder :%s\n"COLOR_NONE, system_info.model_name);
        return -1;
    }
    get_current_times(current_time);
    memcpy(system_info.settings_version,current_time,strlen(current_time));
    memcpy(system_info.soft_version,current_time,strlen(current_time));
    memcpy(system_info.app_version,current_time,strlen(current_time));
    memcpy(system_info.boot_version,current_time,strlen(current_time));

    printf(FONT_COLOR_YELLOW"amp version %s\n"COLOR_NONE,system_info.soft_version);

    system_info.update_flag = 0;
    system_info.build_date = 2345;
    system_info.build_time = 2345;

    system_info.gain_mute_info.bChannelIsMute[AMP_LR_CHANNEL_TYPE] = 0;
    system_info.gain_mute_info.bChannelIsMute[AMP_SKY_CHANNEL_TYPE] = 0;
    system_info.gain_mute_info.bChannelIsMute[AMP_BASS_CHANNEL_TYPE] = 0;
    system_info.gain_mute_info.bChannelIsMute[AMP_CENTER_CHANNEL_TYPE] = 0;
    system_info.gain_mute_info.bChannelIsMute[AMP_ROUND_CHANNEL_TYPE] = 0;

    system_info.gain_mute_info.bChannelGainValue[AMP_LR_CHANNEL_TYPE] = 0;
    system_info.gain_mute_info.bChannelGainValue[AMP_SKY_CHANNEL_TYPE] = 0;
    system_info.gain_mute_info.bChannelGainValue[AMP_BASS_CHANNEL_TYPE] = 0;
    system_info.gain_mute_info.bChannelGainValue[AMP_CENTER_CHANNEL_TYPE] = 0;
    system_info.gain_mute_info.bChannelGainValue[AMP_ROUND_CHANNEL_TYPE] = 0;
    sprintf(s3d_path,"%s%s%s",INI_PATH,system_info.model_name,"/Yda174_s3d.ini");
    set_yda174_s3d_data(s3d_path,&system_info.gain_mute_info);
    
    system_info.gain_mute_info.bSsoOn = 1;
    system_info.gain_mute_info.enSsoPosition = YDA_SSO_POSITION_MIDDLE;
    system_info.gain_mute_info.enS3dType = AMP_S3D_NATURAL_TYPE;

    printf("get system info = %d\n",system_info.gain_mute_info.bSsoOn);
    printf("get system info = %d\n",system_info.gain_mute_info.enSsoPosition);
    printf("get system info = %d\n",system_info.build_date);

    sprintf(lr_path,"%s%s%s",INI_PATH,system_info.model_name,"/Yda174_eq.ini");
    sprintf(sky_path,"%s%s%s",INI_PATH,system_info.model_name,"/Yda174_Sky_eq.ini");
    sprintf(center_path,"%s%s%s",INI_PATH,system_info.model_name,"/Ntp8928_Middle_eq.ini");
    sprintf(bass_path,"%s%s%s",INI_PATH,system_info.model_name,"/Ntp8928_Woofer1_eq.ini");
    sprintf(sur_path,"%s%s%s",INI_PATH,system_info.model_name,"/Ntp8928_Round_eq.ini");
    sprintf(sso_path,"%s%s%s",INI_PATH,system_info.model_name,"/Yda174_sso.ini");

    set_yda174_eq_data(lr_path,&_data.stYdaAmpData[YDA174_AMP_LR_CHANNEL]);
    set_yda174_lr_sso_data(sso_path,&_data.stYdaAmpData[YDA174_AMP_LR_CHANNEL]);
    
    set_yda174_eq_data(sky_path,&_data.stYdaAmpData[YDA174_AMP_SKY_CHANNEL]);
    set_yda174_sky_sso_data(sso_path,&_data.stYdaAmpData[YDA174_AMP_SKY_CHANNEL]);

    set_ntp8928_eq_data(center_path,&_data.stNtpAmpData[NTP_AMP_CENTER_CHANNEL]);
    set_ntp8928_eq_data(bass_path,&_data.stNtpAmpData[NTP_AMP_BASS_CHANNEL]);
    set_ntp8928_eq_data(sur_path,&_data.stNtpAmpData[NTP_AMP_SURROUND_CHANNEL]);


    // print_register_value(amp_data.stYdaAmpData[YDA174_AMP_LR_CHANNEL].u8CorssData,YDA174_CROSSOVER_DATA_LENGHT);
    // print_register_value(amp_data.stYdaAmpData[YDA174_AMP_LR_CHANNEL].u8PowerLimitData,YDA174_POWER_LIMIT_DATA_LENGTH);
    // print_register_value(amp_data.stYdaAmpData[YDA174_AMP_LR_CHANNEL].u8PeqData,YDA174_PEQ_DATA_LENGTH);
    // for(int i=0;i

上面的代码编译出来是个在电脑终端运行的bin文件,运行后在对应目录下会生成settings.bin文件。

当然这个文件里的结构体数据要跟stm32 从flash里读取数据时候的结构体完全一样,且地址也要对应的上,不然数据可能会错乱掉的。

你可能感兴趣的:(stm32,Iap升级,stm32)