<?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: Controlling data and function placement in memory in Kinetis Microcontrollers</title>
    <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Controlling-data-and-function-placement-in-memory/m-p/306363#M13040</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello Siavash:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;That thing with the @ was possible with our compiler for S08/ColdFire V1 MCUs, but things are not so straightforward with Kinetis. You need to play with the linker file (&lt;STRONG&gt;.ld&lt;/STRONG&gt;) and some function/variable attributes. Here an example:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Say you have a KL16 with 128 KB of flash, and you want to place a function at address 0x0001FF00 at the last part of flash. First in the linker file you would have something like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_text_macro jive_macro_code _jivemacro_uid_13961196187234232" jivemacro_uid="_13961196187234232" modifiedtitle="true"&gt;
&lt;P&gt;/* Specify the memory areas */&lt;/P&gt;
&lt;P&gt;MEMORY&lt;/P&gt;
&lt;P&gt;{&lt;/P&gt;
&lt;P&gt;&amp;nbsp; m_interrupts&amp;nbsp;&amp;nbsp;&amp;nbsp; (rx) : ORIGIN = 0x00000000, LENGTH = 0xC0&lt;/P&gt;
&lt;P&gt;&amp;nbsp; m_cfmprotrom&amp;nbsp;&amp;nbsp;&amp;nbsp; (rx) : ORIGIN = 0x00000400, LENGTH = 0x10&lt;/P&gt;
&lt;P&gt;&amp;nbsp; m_text&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (rx) : ORIGIN = 0x00000800, LENGTH = 128K - 0x800&lt;/P&gt;
&lt;P&gt;&amp;nbsp; m_data&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (rwx) : ORIGIN = 0x1FFFF000, LENGTH = 16K&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* SRAM */&lt;/P&gt;
&lt;P&gt;}&lt;/P&gt;

&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;We can reserve the last 256 bytes of flash (0x100) by creating a new memory segment called "my_flash_seg", and subtracting those bytes from the m_text segment:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_text_macro jive_macro_code _jivemacro_uid_13961198618209364" jivemacro_uid="_13961198618209364" modifiedtitle="true"&gt;
&lt;P&gt;/* Specify the memory areas */&lt;/P&gt;
&lt;P&gt;MEMORY&lt;/P&gt;
&lt;P&gt;{&lt;/P&gt;
&lt;P&gt;&amp;nbsp; m_interrupts&amp;nbsp;&amp;nbsp;&amp;nbsp; (rx) : ORIGIN = 0x00000000, LENGTH = 0xC0&lt;/P&gt;
&lt;P&gt;&amp;nbsp; m_cfmprotrom&amp;nbsp;&amp;nbsp;&amp;nbsp; (rx) : ORIGIN = 0x00000400, LENGTH = 0x10&lt;/P&gt;
&lt;P&gt;&amp;nbsp; m_text&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (rx) : ORIGIN = 0x00000800, LENGTH = 128K - 0x800 - 0x100&amp;nbsp; /* Subtract here the 0x100 */&lt;/P&gt;
&lt;P&gt;&amp;nbsp; my_flash_seg&amp;nbsp; (rx) : ORIGIN = 0x0001FF00, LENGTH = 0x100&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* This is my new segment */&lt;/P&gt;
&lt;P&gt;&amp;nbsp; m_data&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (rwx) : ORIGIN = 0x1FFFF000, LENGTH = 16K&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* SRAM */&lt;/P&gt;
&lt;P&gt;}&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;

&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This only reserves a space in memory, but then below in the same linker file inside of SECTIONS we need to create a new section which specifically allocates parts of our code or variables in the "my_flash_seg" segment. In this case I called the new section "my_section". Notice also the tag ".mysegment", this is the tag we will use during function or variable declaration.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_text_macro jive_macro_code _jivemacro_uid_1396121065158555" jivemacro_uid="_1396121065158555" modifiedtitle="true"&gt;
&lt;P&gt;/* Section created to relocate code in specific Flash address */&lt;/P&gt;
&lt;P&gt;&amp;nbsp; .my_section&lt;/P&gt;
&lt;P&gt;&amp;nbsp; {&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; . = ALIGN(4);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; *(.mysegment)&amp;nbsp;&amp;nbsp;&amp;nbsp; /* This is the tag you use in code */&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; . = ALIGN(4);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; } &amp;gt; my_flash_seg&lt;/P&gt;

&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This is all with the linker. The last step is to add an attribute to your function or variable declaration, using the tag mentioned before like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_text_macro jive_macro_code _jivemacro_uid_1396121583840462" jivemacro_uid="_1396121583840462"&gt;
&lt;P&gt;__attribute__ ((section(".mysegment"))) void my_func()&lt;/P&gt;
&lt;P&gt;{&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // ...&lt;/P&gt;
&lt;P&gt;}&lt;/P&gt;

&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;And that's it :smileylaugh:. It is a little complex, but once you do it a few times you get familiar with the procedure. For an example with RAM, check the next thread:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.nxp.com/message/364619" rel="nofollow noopener noreferrer" target="_blank"&gt;Re: Re: Syntax to allocate memory assignments to faster memory&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;And also a tutorial on this same topic by colleague Erich Styger:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="http://mcuoneclipse.com/2012/11/01/defining-variables-at-absolute-addresses-with-gcc/" rel="nofollow noopener noreferrer" title="http://mcuoneclipse.com/2012/11/01/defining-variables-at-absolute-addresses-with-gcc/" target="_blank"&gt;Defining Variables at Absolute Addresses with gcc | MCU on Eclipse&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;Jorge Gonzalez&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;-----------------------------------------------------------------------------------------------------------------------&lt;BR /&gt;Note: If this post answers your question, please click the Correct Answer button. Thank you!&lt;BR /&gt;-----------------------------------------------------------------------------------------------------------------------&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 29 Mar 2014 19:39:10 GMT</pubDate>
    <dc:creator>Jorge_Gonzalez</dc:creator>
    <dc:date>2014-03-29T19:39:10Z</dc:date>
    <item>
      <title>Controlling data and function placement in memory</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Controlling-data-and-function-placement-in-memory/m-p/306362#M13039</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I need to place a function in a specific address or segment in memory. I'm using GNU compiler in CodeWarrior for Kinetis KL16. &lt;/P&gt;&lt;P&gt;Just as an example, I was able to do this using MSP430 MCU as below:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;void f(void) @ "MYSEGMENT";&lt;/P&gt;&lt;P&gt;void g(void) @ "MYSEGMENT"&lt;/P&gt;&lt;P&gt;{&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I would appreciate any help in this regard. Thanks in advance.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 29 Mar 2014 15:41:27 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Controlling-data-and-function-placement-in-memory/m-p/306362#M13039</guid>
      <dc:creator>sfarzan</dc:creator>
      <dc:date>2014-03-29T15:41:27Z</dc:date>
    </item>
    <item>
      <title>Re: Controlling data and function placement in memory</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Controlling-data-and-function-placement-in-memory/m-p/306363#M13040</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello Siavash:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;That thing with the @ was possible with our compiler for S08/ColdFire V1 MCUs, but things are not so straightforward with Kinetis. You need to play with the linker file (&lt;STRONG&gt;.ld&lt;/STRONG&gt;) and some function/variable attributes. Here an example:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Say you have a KL16 with 128 KB of flash, and you want to place a function at address 0x0001FF00 at the last part of flash. First in the linker file you would have something like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_text_macro jive_macro_code _jivemacro_uid_13961196187234232" jivemacro_uid="_13961196187234232" modifiedtitle="true"&gt;
&lt;P&gt;/* Specify the memory areas */&lt;/P&gt;
&lt;P&gt;MEMORY&lt;/P&gt;
&lt;P&gt;{&lt;/P&gt;
&lt;P&gt;&amp;nbsp; m_interrupts&amp;nbsp;&amp;nbsp;&amp;nbsp; (rx) : ORIGIN = 0x00000000, LENGTH = 0xC0&lt;/P&gt;
&lt;P&gt;&amp;nbsp; m_cfmprotrom&amp;nbsp;&amp;nbsp;&amp;nbsp; (rx) : ORIGIN = 0x00000400, LENGTH = 0x10&lt;/P&gt;
&lt;P&gt;&amp;nbsp; m_text&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (rx) : ORIGIN = 0x00000800, LENGTH = 128K - 0x800&lt;/P&gt;
&lt;P&gt;&amp;nbsp; m_data&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (rwx) : ORIGIN = 0x1FFFF000, LENGTH = 16K&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* SRAM */&lt;/P&gt;
&lt;P&gt;}&lt;/P&gt;

&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;We can reserve the last 256 bytes of flash (0x100) by creating a new memory segment called "my_flash_seg", and subtracting those bytes from the m_text segment:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_text_macro jive_macro_code _jivemacro_uid_13961198618209364" jivemacro_uid="_13961198618209364" modifiedtitle="true"&gt;
&lt;P&gt;/* Specify the memory areas */&lt;/P&gt;
&lt;P&gt;MEMORY&lt;/P&gt;
&lt;P&gt;{&lt;/P&gt;
&lt;P&gt;&amp;nbsp; m_interrupts&amp;nbsp;&amp;nbsp;&amp;nbsp; (rx) : ORIGIN = 0x00000000, LENGTH = 0xC0&lt;/P&gt;
&lt;P&gt;&amp;nbsp; m_cfmprotrom&amp;nbsp;&amp;nbsp;&amp;nbsp; (rx) : ORIGIN = 0x00000400, LENGTH = 0x10&lt;/P&gt;
&lt;P&gt;&amp;nbsp; m_text&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (rx) : ORIGIN = 0x00000800, LENGTH = 128K - 0x800 - 0x100&amp;nbsp; /* Subtract here the 0x100 */&lt;/P&gt;
&lt;P&gt;&amp;nbsp; my_flash_seg&amp;nbsp; (rx) : ORIGIN = 0x0001FF00, LENGTH = 0x100&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* This is my new segment */&lt;/P&gt;
&lt;P&gt;&amp;nbsp; m_data&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (rwx) : ORIGIN = 0x1FFFF000, LENGTH = 16K&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* SRAM */&lt;/P&gt;
&lt;P&gt;}&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;

&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This only reserves a space in memory, but then below in the same linker file inside of SECTIONS we need to create a new section which specifically allocates parts of our code or variables in the "my_flash_seg" segment. In this case I called the new section "my_section". Notice also the tag ".mysegment", this is the tag we will use during function or variable declaration.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_text_macro jive_macro_code _jivemacro_uid_1396121065158555" jivemacro_uid="_1396121065158555" modifiedtitle="true"&gt;
&lt;P&gt;/* Section created to relocate code in specific Flash address */&lt;/P&gt;
&lt;P&gt;&amp;nbsp; .my_section&lt;/P&gt;
&lt;P&gt;&amp;nbsp; {&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; . = ALIGN(4);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; *(.mysegment)&amp;nbsp;&amp;nbsp;&amp;nbsp; /* This is the tag you use in code */&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; . = ALIGN(4);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; } &amp;gt; my_flash_seg&lt;/P&gt;

&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This is all with the linker. The last step is to add an attribute to your function or variable declaration, using the tag mentioned before like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_text_macro jive_macro_code _jivemacro_uid_1396121583840462" jivemacro_uid="_1396121583840462"&gt;
&lt;P&gt;__attribute__ ((section(".mysegment"))) void my_func()&lt;/P&gt;
&lt;P&gt;{&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // ...&lt;/P&gt;
&lt;P&gt;}&lt;/P&gt;

&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;And that's it :smileylaugh:. It is a little complex, but once you do it a few times you get familiar with the procedure. For an example with RAM, check the next thread:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.nxp.com/message/364619" rel="nofollow noopener noreferrer" target="_blank"&gt;Re: Re: Syntax to allocate memory assignments to faster memory&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;And also a tutorial on this same topic by colleague Erich Styger:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="http://mcuoneclipse.com/2012/11/01/defining-variables-at-absolute-addresses-with-gcc/" rel="nofollow noopener noreferrer" title="http://mcuoneclipse.com/2012/11/01/defining-variables-at-absolute-addresses-with-gcc/" target="_blank"&gt;Defining Variables at Absolute Addresses with gcc | MCU on Eclipse&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;Jorge Gonzalez&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;-----------------------------------------------------------------------------------------------------------------------&lt;BR /&gt;Note: If this post answers your question, please click the Correct Answer button. Thank you!&lt;BR /&gt;-----------------------------------------------------------------------------------------------------------------------&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 29 Mar 2014 19:39:10 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Controlling-data-and-function-placement-in-memory/m-p/306363#M13040</guid>
      <dc:creator>Jorge_Gonzalez</dc:creator>
      <dc:date>2014-03-29T19:39:10Z</dc:date>
    </item>
  </channel>
</rss>

