STM32 CubeMX custom code generation
<p>I'm use <a href="https://my.st.com/content/my_st_com/en/products/development-tools/software-development-tools/stm32-software-development-tools/stm32-configurators-and-code-generators/stm32cubemx.html#getsoftware-scroll" rel="nofollow noreferrer">STM32CubeMX</a> and trying to <a href="https://my.st.com/content/ccc/resource/technical/document/user_manual/10/c5/1a/43/3a/70/43/7d/DM00104712.pdf/files/DM00104712.pdf/jcr:content/translations/en.DM00104712.pdf" rel="nofollow noreferrer">generate my custom code</a> <em>(UM1718 page 141)</em> with tables of used periferals. It is easy to have constant tables in flash with periferals handles and instances grouped by intended use. For example this header file:</p>

<p><strong>some.h:</strong></p>

<pre><code>/* 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];
</code></pre>

<p>I have generate with this custom template:</p>

<p><strong>some_h.ftl:</strong></p>

<pre><code>[#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&gt;0&amp;&amp;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&gt;0&amp;&amp;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&gt;0&amp;&amp;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&gt;0&amp;&amp;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 --]
</code></pre>

<p>I havn't work with freemarker until this moment. </p>

<p>And <strong>my problem that I can't found way to extract more information</strong> from this data model: </p>

<ul>
<li><strong>about handles types and names</strong> (I have simply declared it <code>[#assign ip_tim =["TIM",  "htim", "TIM_TypeDef", "TIM_HandleTypeDef" ]]</code>), </li>
<li><strong>about GPIOs that not associated with any periferals, but initialized and have lables</strong> (I have no idea there).</li>
</ul>

<p>As described in UM1718:</p>

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

<pre><code>[#ftl]
[#list configs as dt]
[#assign data = dt]
[#assign peripheralParams =dt.peripheralParams]
[#assign peripheralGPIOParams =dt.peripheralGPIOParams]
[#assign usedIPs =dt.usedIPs]
</code></pre>
  
  <p>and end with</p>

<pre><code>[/#list]
</code></pre>
</blockquote>

<p><strong>Is this mean that there no way to extract necessary information (mostly about GPIOs)?</strong></p>

<p><strong>Or how I can check this?</strong> I have try <a href="https://stackoverflow.com/questions/1497777/freemarker-iterating-over-hashmap-keys/1497802#1497802">this one</a> to describe data model but found only list of GPIO ports.</p>