<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: .m_data_2 not initialized in Kinetis Microcontrollers</title>
    <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/m-data-2-not-initialized/m-p/1785233#M65909</link>
    <description>&lt;P&gt;Yeah, but how/why is that different from simply declaring it as follows?&lt;/P&gt;&lt;LI-CODE lang="c"&gt;uint8_t foo = 123;&lt;/LI-CODE&gt;&lt;P&gt;This foo is also declared in RAM, yet after power on it gets automatically assigned to 123. (If I am not mistaken, compiler inserts additional code that either assigns the value as an immediate, or copies the entire segment from flash.)&lt;/P&gt;</description>
    <pubDate>Tue, 09 Jan 2024 11:04:13 GMT</pubDate>
    <dc:creator>kat1</dc:creator>
    <dc:date>2024-01-09T11:04:13Z</dc:date>
    <item>
      <title>.m_data_2 not initialized</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/m-data-2-not-initialized/m-p/1783766#M65880</link>
      <description>&lt;P&gt;Hi!&lt;/P&gt;&lt;P&gt;Long story short: .m_data_2 memory segment is correctly initialized right after flashing, but is not getting initialized after powercycling.&lt;/P&gt;&lt;P&gt;This is with Kinetis&amp;nbsp;MK22FN512VLH12 ARM Cortex-M4.&lt;/P&gt;&lt;HR /&gt;&lt;P&gt;Long story:&lt;/P&gt;&lt;P&gt;I have inherited the responsibility for dealing with issues at &lt;A href="https://github.com/UltimateHackingKeyboard/firmware" target="_blank" rel="noopener"&gt;https://github.com/UltimateHackingKeyboard/firmware&lt;/A&gt;, while not having much actual experience setting toolchains for embedded/bare-metal C...&lt;/P&gt;&lt;P&gt;The MCU in question (Kinetis MK22FN512VLH12) has RAM divided into two data segments: m_data and m_data_2.&lt;/P&gt;&lt;P&gt;Assume following code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;__attribute__((section (".m_data_2"))) uint8_t foo = 123;

int main(void)
{
    PRINT(foo);
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;On first run after flashing, it correctly prints 123&lt;/LI&gt;&lt;LI&gt;If the board is powercycled, it randomly prints either 108 or 124.&lt;/LI&gt;&lt;LI&gt;If NVIC_SystemReset() is issued the same value gets printed as in the last run.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;Here are datasheets, in case they are of any help:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;A href="https://github.com/UltimateHackingKeyboard/datasheets/blob/master/mcu/K22P121M120SF7.pdf" target="_blank" rel="noopener"&gt;https://github.com/UltimateHackingKeyboard/datasheets/blob/master/mcu/K22P121M120SF7.pdf&lt;/A&gt;&lt;/LI&gt;&lt;LI&gt;&lt;A href="https://github.com/UltimateHackingKeyboard/datasheets/blob/master/mcu/K22P121M120SF7RM.pdf" target="_blank" rel="noopener"&gt;https://github.com/UltimateHackingKeyboard/datasheets/blob/master/mcu/K22P121M120SF7RM.pdf&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;Edit: fixed the MCU part number&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jan 2024 16:31:06 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/m-data-2-not-initialized/m-p/1783766#M65880</guid>
      <dc:creator>kat1</dc:creator>
      <dc:date>2024-01-10T16:31:06Z</dc:date>
    </item>
    <item>
      <title>Re: .m_data_2 not initialized</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/m-data-2-not-initialized/m-p/1784112#M65897</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I suppose that the line has issue.&lt;/P&gt;
&lt;LI-CODE lang="c"&gt;__attribute__((section (".m_data_2"))) uint8_t foo = 123;&lt;/LI-CODE&gt;
&lt;P&gt;The above line declares a variable uint8_t foo, and save it in SRAM section .m_data_2. But as you know that the data in SRAM will be lost after power off, so the value 123 is lost after power off.&lt;/P&gt;
&lt;P&gt;If you want to keep the value 123, the 123 must be saved in flash and copy to RAM.&lt;/P&gt;
&lt;P&gt;1)you can define:&lt;/P&gt;
&lt;LI-CODE lang="c"&gt;__attribute__((section (".m_data_2"))) uint8_t foo;&lt;/LI-CODE&gt;&lt;LI-CODE lang="c"&gt;int main(void)
{
    foo=123;
    PRINT(foo);
}&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2)you can use the line:&lt;/P&gt;
&lt;P&gt;uint8_t&amp;nbsp; foo=123;&lt;/P&gt;
&lt;LI-CODE lang="c"&gt;int main(void)
{
    PRINT(foo);
}&lt;/LI-CODE&gt;
&lt;P&gt;3)const uint8_t var=123;&lt;/P&gt;
&lt;LI-CODE lang="c"&gt;__attribute__((section (".m_data_2"))) uint8_t foo;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="c"&gt;int main(void)
{
    foo=var;
    PRINT(foo);
}&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope it can help you&lt;/P&gt;
&lt;P&gt;BR&lt;/P&gt;
&lt;P&gt;XiangJun Rong&lt;/P&gt;</description>
      <pubDate>Mon, 08 Jan 2024 06:27:23 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/m-data-2-not-initialized/m-p/1784112#M65897</guid>
      <dc:creator>xiangjun_rong</dc:creator>
      <dc:date>2024-01-08T06:27:23Z</dc:date>
    </item>
    <item>
      <title>Re: .m_data_2 not initialized</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/m-data-2-not-initialized/m-p/1784253#M65899</link>
      <description>&lt;P&gt;Thanks for reply!&lt;/P&gt;&lt;P&gt;So I take it that the .m_data_2 cannot be initialized on bootup automatically just as .m_data is?&lt;/P&gt;&lt;P&gt;I am still confused as to why that is so. I would expect that to be the default behavior, automatically inserted by the compiler somewhere before the main is called.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Jan 2024 08:44:59 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/m-data-2-not-initialized/m-p/1784253#M65899</guid>
      <dc:creator>kat1</dc:creator>
      <dc:date>2024-01-08T08:44:59Z</dc:date>
    </item>
    <item>
      <title>Re: .m_data_2 not initialized</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/m-data-2-not-initialized/m-p/1784789#M65902</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;The basic theory is that the RAM data is lost when the chip power off and on, that is why that the constant must be saved in flash. If it is a variable saved in RAM with an initialized constant, there is a copy from flash to RAM.&lt;/P&gt;
&lt;P&gt;You can not declare a variable in RAM with initialized constant data.&lt;/P&gt;
&lt;P&gt;BR&lt;/P&gt;
&lt;P&gt;XiangJun Rong&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jan 2024 02:56:31 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/m-data-2-not-initialized/m-p/1784789#M65902</guid>
      <dc:creator>xiangjun_rong</dc:creator>
      <dc:date>2024-01-09T02:56:31Z</dc:date>
    </item>
    <item>
      <title>Re: .m_data_2 not initialized</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/m-data-2-not-initialized/m-p/1785233#M65909</link>
      <description>&lt;P&gt;Yeah, but how/why is that different from simply declaring it as follows?&lt;/P&gt;&lt;LI-CODE lang="c"&gt;uint8_t foo = 123;&lt;/LI-CODE&gt;&lt;P&gt;This foo is also declared in RAM, yet after power on it gets automatically assigned to 123. (If I am not mistaken, compiler inserts additional code that either assigns the value as an immediate, or copies the entire segment from flash.)&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jan 2024 11:04:13 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/m-data-2-not-initialized/m-p/1785233#M65909</guid>
      <dc:creator>kat1</dc:creator>
      <dc:date>2024-01-09T11:04:13Z</dc:date>
    </item>
    <item>
      <title>Re: .m_data_2 not initialized</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/m-data-2-not-initialized/m-p/1785711#M65915</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;For the global variable&lt;/P&gt;
&lt;PRE class="lia-code-sample  language-c"&gt;&lt;CODE&gt;uint8_t foo = 123;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The compiler will save the 123 in flash, the foo is allocated in RAM, before calling main(), the 123 in flash is copied to foo in RAM by initializing code.&lt;/P&gt;
&lt;P&gt;Hope it can help you&lt;/P&gt;
&lt;P&gt;BR&lt;/P&gt;
&lt;P&gt;XiangJun Rong&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jan 2024 03:50:14 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/m-data-2-not-initialized/m-p/1785711#M65915</guid>
      <dc:creator>xiangjun_rong</dc:creator>
      <dc:date>2024-01-10T03:50:14Z</dc:date>
    </item>
    <item>
      <title>Re: .m_data_2 not initialized</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/m-data-2-not-initialized/m-p/1785967#M65923</link>
      <description>&lt;P&gt;Hi!&lt;/P&gt;&lt;P&gt;Would you be so kind as to elaborate on the reasons of this difference in handling of those two RAM regions?&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jan 2024 09:43:58 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/m-data-2-not-initialized/m-p/1785967#M65923</guid>
      <dc:creator>kat1</dc:creator>
      <dc:date>2024-01-10T09:43:58Z</dc:date>
    </item>
    <item>
      <title>Re: .m_data_2 not initialized</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/m-data-2-not-initialized/m-p/1786095#M65925</link>
      <description>This gets traced back to the 'C' standard.&lt;BR /&gt;Initialized variables that are global in scope or marked as 'static' in a function will be placed in a segment typically called 'data' placed in Flash. The start up code knows that it needs to load those 'data' variables.&lt;BR /&gt;&lt;BR /&gt;As there can be any number of segments with arbitrary names, the standard start up code does nothing with them. So the start up code and linker scripts need modified to treat other segments the way they treat the 'data' segment.</description>
      <pubDate>Wed, 10 Jan 2024 13:36:33 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/m-data-2-not-initialized/m-p/1786095#M65925</guid>
      <dc:creator>bobpaddock</dc:creator>
      <dc:date>2024-01-10T13:36:33Z</dc:date>
    </item>
    <item>
      <title>Re: .m_data_2 not initialized</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/m-data-2-not-initialized/m-p/1786161#M65926</link>
      <description>&lt;P&gt;Ah, thank you very much!&lt;/P&gt;&lt;P&gt;&amp;gt; So the start up code and linker scripts need modified&lt;/P&gt;&lt;P&gt;Any pointers on how to do that?&lt;/P&gt;&lt;P&gt;By "start up code", you mean a compiler-generated code, or an actual file which I need to modify by hand? (How do I indentify it?)&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jan 2024 15:39:14 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/m-data-2-not-initialized/m-p/1786161#M65926</guid>
      <dc:creator>kat1</dc:creator>
      <dc:date>2024-01-10T15:39:14Z</dc:date>
    </item>
    <item>
      <title>Re: .m_data_2 not initialized</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/m-data-2-not-initialized/m-p/1786175#M65927</link>
      <description>&lt;P&gt;The answer depends on the tool set you are using and its age.&lt;/P&gt;&lt;P&gt;What you are looking for is the code that happens right out of 'reset'.&lt;BR /&gt;Older code would be in assembler and end with .S.&lt;/P&gt;&lt;P&gt;Newer code is often&amp;nbsp; .c file.&lt;BR /&gt;&lt;BR /&gt;GCC linker scripts generally end with .ld.&lt;/P&gt;&lt;P&gt;I can't be more detailed because each tool, and generation of the tool, calls them something different in different locations.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jan 2024 16:08:22 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/m-data-2-not-initialized/m-p/1786175#M65927</guid>
      <dc:creator>bobpaddock</dc:creator>
      <dc:date>2024-01-10T16:08:22Z</dc:date>
    </item>
    <item>
      <title>Re: .m_data_2 not initialized</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/m-data-2-not-initialized/m-p/1786594#M65928</link>
      <description>&lt;P&gt;You are the best!&lt;/P&gt;&lt;P&gt;Took a while of further searching, experimenting and digging, but following two commits seem to have done the trick (at least nothing seems broken so far &lt;LI-EMOJI id="lia_grinning-face-with-sweat" title=":grinning_face_with_sweat:"&gt;&lt;/LI-EMOJI&gt;), in case anyone stumbles across this thread in the future..&lt;/P&gt;&lt;P&gt;- &lt;A href="https://github.com/UltimateHackingKeyboard/KSDK_2.0_MK22FN512xxx12/commit/b1f3446d5e380a3bb851a97d07c731125f232769" target="_blank"&gt;https://github.com/UltimateHackingKeyboard/KSDK_2.0_MK22FN512xxx12/commit/b1f3446d5e380a3bb851a97d07c731125f232769&lt;/A&gt;&lt;BR /&gt;- &lt;A href="https://github.com/UltimateHackingKeyboard/firmware/commit/1b1306efa6924ecec4ad9f31f9a47a7f9633abf4" target="_blank"&gt;https://github.com/UltimateHackingKeyboard/firmware/commit/1b1306efa6924ecec4ad9f31f9a47a7f9633abf4&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jan 2024 22:57:23 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/m-data-2-not-initialized/m-p/1786594#M65928</guid>
      <dc:creator>kat1</dc:creator>
      <dc:date>2024-01-10T22:57:23Z</dc:date>
    </item>
  </channel>
</rss>

