STM32 CubeMX自定义代码生成

5

我使用 STM32CubeMX 并尝试 生成我的自定义代码(UM1718第141页),其中包含使用的外设表格。通过将外设句柄和实例根据预期使用目的进行分组,可以很容易地在 Flash 中拥有常量表格。例如,这个头文件:

some.h:

/* TIM table */
#define EM_TIM1     0       // index
#define EM_TIM6     1

#define TIM_CNT     2       // count

#define TIM_0_INST      TIM1        // table of instaces
#define TIM_1_INST      TIM6

extern TIM_HandleTypeDef htim1;     // table of handles
extern TIM_HandleTypeDef htim6;

#define TIM_0_HNDL      htim1       // table of handles
#define TIM_1_HNDL      htim6

struct tim_tab_s {
    TIM_TypeDef * inst;
    TIM_HandleTypeDef * hndl;
};

extern struct tim_tab_s const tim_tab[TIM_CNT];

我已经使用这个自定义模板生成:

some_h.ftl:

[#ftl]
[#list configs as dt]
[#assign data = dt]
[#assign peripheralParams =dt.peripheralParams]
[#assign peripheralGPIOParams =dt.peripheralGPIOParams]
[#assign peripheralDMAParams =dt.peripheralDMAParams]
[#assign peripheralNVICParams =dt.peripheralNVICParams]
[#assign usedIPs =dt.usedIPs]
[#assign ip_pref = "EM_"]
[#-- ip desrcibe grpName  varName instType       hndlType --]
[#assign ip_tim =["TIM",  "htim", "TIM_TypeDef", "TIM_HandleTypeDef" ]]
[#assign ip_grps = [ip_tim]]
[#list ip_grps as ip_group]
    [#assign ip_grp = ip_group[0]]
    [#assign ip_var = ip_group[1]]
    [#assign ip_instType = ip_group[2]]
    [#assign ip_hndlType = ip_group[3]]
    /* ${ip_grp} table */
    [#assign ip_id = 0]                      [#-- IPs Index --]
    [#list usedIPs as ip]
        [#if peripheralParams.get(ip).entrySet()?size>0&&ip?contains(ip_grp)]
            #define ${ip_pref}${ip} ${ip_id}[#if ip_id == 0]  // index[/#if]
            [#assign ip_id = ip_id + 1]
        [/#if]
    [/#list][#-- list IPs --]
    #n
    #define ${ip_grp}_CNT ${ip_id}  // count
    #n
    [#assign ip_id = 0]               [#-- IPs Instaces --]
    [#list usedIPs as ip]
        [#if peripheralParams.get(ip).entrySet()?size>0&&ip?contains(ip_grp)]
            #define ${ip_grp}_${ip_id}_INST ${ip}[#if ip_id == 0]  // table of instaces[/#if]
            [#assign ip_id = ip_id + 1]
        [/#if]
    [/#list][#-- list IPs --]
    #n
    [#assign ip_id = 0]               [#-- IPs HAL handles declar --]
    [#list usedIPs as ip]
        [#if peripheralParams.get(ip).entrySet()?size>0&&ip?    contains(ip_grp)]
            extern ${ip_hndlType} ${ip_var}${ip.replace(ip_grp,"")};[#if ip_id == 0] // table of handles[/#if]
            [#assign ip_id = ip_id + 1]
        [/#if]
    [/#list][#-- list IPs --]
    #n
    [#assign ip_id = 0]                [#-- IPs HAL handles --]
    [#list usedIPs as ip]
        [#if peripheralParams.get(ip).entrySet()?size>0&&ip?contains(ip_grp)]
            #define ${ip_grp}_${ip_id}_HNDL ${ip_var}${ip.replace(ip_grp,"")}[#if ip_id == 0] // table of handles[/#if]
            [#assign ip_id = ip_id + 1]
        [/#if]
    [/#list][#-- list IPs --]
[/#list][#-- list ip_grps --]
[/#list][#-- list configs --]

我直到现在都没有使用过freemarker。
我的问题是我找不到从这个数据模型中提取更多信息的方法:
- 关于句柄类型和名称(我只是简单地声明了它 `[#assign ip_tim =["TIM", "htim", "TIM_TypeDef", "TIM_HandleTypeDef" ]]`), - 关于未与任何外设相关联但已初始化并具有标签的GPIO(我对此一无所知)。
如UM1718所述:

The user template file must be compatible with STM32CubeMX data model. This means that the template must start with the following lines:

[#ftl]
[#list configs as dt]
[#assign data = dt]
[#assign peripheralParams =dt.peripheralParams]
[#assign peripheralGPIOParams =dt.peripheralGPIOParams]
[#assign usedIPs =dt.usedIPs]

and end with

[/#list]

这是否意味着没有办法提取必要的信息(主要是关于GPIO)?

或者我该如何检查?我尝试过this one来描述数据模型,但只找到了GPIO端口列表。


如果没有记录数据模型包含的内容,您可以检查调用FreeMarker的Java代码以及它传递给它的内容。寻找“Template.process”调用可能有助于找到它。或者,您可以尝试转储数据模型:https://dev59.com/K2Mk5IYBdhLWcg3wtgMR?lq=1 - ddekany
@ddekany,我没有STM32CubeMX源代码的访问权限,正如我在问题中所写的那样:我已经尝试以与您所说相同的方式转储数据模型(请参见我的问题的最后一行)。因此,我理解只有两种方法,而且它们都不能帮助我? - imbearr
你可以在Java中检查数据模型。在freemarker.core.Environment.process上设置断点,然后查看rootDataModel字段。它将是某种TemplateModel,但在其中你可能会找到(部分)原始的Java对象。如果你没有STM32CubeMX的源代码,像JAD这样的反编译器可能会有所帮助。 - ddekany
我尝试使用JAD反编译CubeMX,但我对Java还是新手,没有成功。 - imbearr
抱歉,它不是JAD,而是JD... http://jd.benow.ca/ - ddekany
1个回答

0

这是我一个月前向ST支持部门提出的问题单的回复。现在,这是对我的问题的一个实际答案。

尊敬的客户,

很遗憾,这个功能相当新,实验性的,并且文档不太完善,所以我无法帮助您解决这个问题。API也可能会在未来的版本中进行更改。如果我理解正确,您可以通过手动定义数组来解决此问题。

最好的问候,ST MCU支持团队

我正在等待新版本的发布!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接