<?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>Kinetis Microcontrollers中的主题 Re: Copy and execute code from RAM</title>
    <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Copy-and-execute-code-from-RAM/m-p/556548#M33713</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm still stuck on this issue; can someone please help? Someone from NXP?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you need more information to help solve it please let me know.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks in advance,&lt;/P&gt;&lt;P&gt;John&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 27 Jul 2016 10:34:59 GMT</pubDate>
    <dc:creator>johndemayor</dc:creator>
    <dc:date>2016-07-27T10:34:59Z</dc:date>
    <item>
      <title>Copy and execute code from RAM</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Copy-and-execute-code-from-RAM/m-p/556547#M33712</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm working with the KL17Z64.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I want to run code from RAM without touching the linker file. I'm very new in this area. The reason I don't want to change the linker file is because this RAM function will only be called in rare conditions - so I want to save resources and not allocate any memory by default. In the future I plan to use calloc() and free() for memory management but in the code below I've used a non-dynamic array (ramSpace[100]).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have looked at most forum posts regarding executing code from RAM, but I run into hard-fault issues and still have some questions;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;1. Is it at all possible to run code from RAM (copied from FLASH) without adapting the linker file?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;2. I've setup a sample project to try to copy a function to RAM, created a function pointer to the RAM location, changed the LSB of the address such that the mode is changed (saw that in this post: ) and then executed the RAM function. Note that I have not changed the linker file at all.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="c++" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14681624541831790 jive_text_macro" data-renderedposition="498_50_798_368" jivemacro_uid="_14681624541831790"&gt;&lt;P&gt;&lt;SPAN style="font-family: 'andale mono', times;"&gt;void ATO_RomFunc(void){&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'andale mono', times;"&gt;&amp;nbsp; while(1){&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'andale mono', times;"&gt;&amp;nbsp; __asm("NOP");&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'andale mono', times;"&gt;&amp;nbsp; }&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'andale mono', times;"&gt;}&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'andale mono', times;"&gt;int main(void) {&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'andale mono', times;"&gt;&amp;nbsp; uint8_t ramSpace[100];&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'andale mono', times;"&gt;&amp;nbsp; void (*ramFunction)(void);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'andale mono', times;"&gt;&amp;nbsp; /* Init board hardware. */&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'andale mono', times;"&gt;&amp;nbsp; BOARD_InitPins();&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'andale mono', times;"&gt;&amp;nbsp; // RAM test&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'andale mono', times;"&gt;&amp;nbsp; memcpy(ramSpace, ATO_RomFunc, 100); //Don't care for now if more code is copied than needed&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'andale mono', times;"&gt;&amp;nbsp; __disable_irq();&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'andale mono', times;"&gt;&amp;nbsp; ramFunction = (void (*)(void))ramSpace;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'andale mono', times;"&gt;&amp;nbsp; ramFunction++; // Add 1 address for thumb&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'andale mono', times;"&gt;&amp;nbsp; ramFunction();&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'andale mono', times;"&gt;}&lt;/SPAN&gt;&lt;/P&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I see that in the disassembly:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #3334ca;"&gt;62&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ramFunction();&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #3334ca;"&gt;00003fa4: 0x0000fb22&amp;nbsp;&amp;nbsp; movs r2, #251&amp;nbsp;&amp;nbsp; ; 0xfb&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #3334ca;"&gt;00003fa6: 0x00009200&amp;nbsp;&amp;nbsp; lsls r2, r2, #2&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #3334ca;"&gt;00003fa8: 0x0000d219&amp;nbsp;&amp;nbsp; adds r2, r2, r7&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #3334ca;"&gt;00003faa: 0x00001368&amp;nbsp;&amp;nbsp; ldr r3, [r2, #0]&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #3334ca;"&gt;00003fac: 0x00009847&amp;nbsp;&amp;nbsp; blx r3&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Where r3 has content 0x20002c0d: which lies inside the SRAM block of the memory map. So I conclude that at least the function pointer part works. Is this assumption correct? However, after the branch (blx command), the C debugger stops and the disassembly jumps to the following code:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #3334ca;"&gt;20002c0c: 0x0000b500&amp;nbsp;&amp;nbsp; lsls r5, r6, #2&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #3334ca;"&gt;20002c0e: 0x0000afc0&amp;nbsp;&amp;nbsp; stmia r0!, {r0, r1, r2, r3, r5, r7}&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #3334ca;"&gt;20002c10: 0x46fde780&amp;nbsp;&amp;nbsp; stc2l 0, cr8, [r6, #-924]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ; 0xfffffc64&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #3334ca;"&gt;20002c14: 0x0000b516&amp;nbsp;&amp;nbsp; asrs r5, r6, #26&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #3334ca;"&gt;20002c16: 0x00004fbd&amp;nbsp;&amp;nbsp; pop {r0, r1, r2, r3, r6, pc}&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #3334ca;"&gt;20002c18: 0x00004400&amp;nbsp;&amp;nbsp; lsls r4, r0, #1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #3334ca;"&gt;20002c1a: 0x0000af00&amp;nbsp;&amp;nbsp; lsls r7, r5, #2&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #3334ca;"&gt;20002c1c: 0x0000f0c8&amp;nbsp;&amp;nbsp; ldmia r0!, {r4, r5, r6, r7}&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #3334ca;"&gt;20002c1e: 0x0000fc14&amp;nbsp;&amp;nbsp; asrs r4, r7, #19&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #3334ca;"&gt;20002c20: 0x00004b18&amp;nbsp;&amp;nbsp; adds r3, r1, r1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #3334ca;"&gt;20002c22: 0x00001c04&amp;nbsp;&amp;nbsp; lsls r4, r3, #16&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #3334ca;"&gt;20002c24: 0x00002101&amp;nbsp;&amp;nbsp; lsls r1, r4, #4&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #3334ca;"&gt;20002c26: 0x22fff7d0&amp;nbsp;&amp;nbsp; vqadd.u32 &amp;lt;illegal reg q6.5&amp;gt;, q9, &amp;lt;illegal reg q11.5&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I can continue to step through it in instruction mode but it's leading to a hard-fault at the last line (0x20002c26). What is it doing here, and why? I expected it to loop indefinately.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;3. How can I set a breakpoint / debug the code running in RAM except for the hard-to-read disassembly? &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Can anyone help me get any further? Any help is appreciated,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Best regards, John&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 10 Jul 2016 14:55:53 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Copy-and-execute-code-from-RAM/m-p/556547#M33712</guid>
      <dc:creator>johndemayor</dc:creator>
      <dc:date>2016-07-10T14:55:53Z</dc:date>
    </item>
    <item>
      <title>Re: Copy and execute code from RAM</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Copy-and-execute-code-from-RAM/m-p/556548#M33713</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm still stuck on this issue; can someone please help? Someone from NXP?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you need more information to help solve it please let me know.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks in advance,&lt;/P&gt;&lt;P&gt;John&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 27 Jul 2016 10:34:59 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Copy-and-execute-code-from-RAM/m-p/556548#M33713</guid>
      <dc:creator>johndemayor</dc:creator>
      <dc:date>2016-07-27T10:34:59Z</dc:date>
    </item>
    <item>
      <title>Re: Copy and execute code from RAM</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Copy-and-execute-code-from-RAM/m-p/556549#M33714</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Doing this without changing the linker script requires writing Position Independent Code (PIC),.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The ARM is capable of doing PIC.&amp;nbsp; Not the easiest thing to write and requires changes to the build system.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;The exact details depend on your part and the tools.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 27 Jul 2016 12:10:19 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Copy-and-execute-code-from-RAM/m-p/556549#M33714</guid>
      <dc:creator>bobpaddock</dc:creator>
      <dc:date>2016-07-27T12:10:19Z</dc:date>
    </item>
    <item>
      <title>Re: Copy and execute code from RAM</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Copy-and-execute-code-from-RAM/m-p/556550#M33715</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;See also:&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.nxp.com/thread/321048"&gt;how to load function in flash while execute in ram&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Make sure you copy enough code (it certainly looks like your last instruction is 'off in the weeds!), AND include 'compiler stored 32 bit constants' beyond the end of the code module.&amp;nbsp; See that the 'disassembly' matches the 'native' Flash from which it was copied.&amp;nbsp; Yes, you will have to debug in said disassembly, as this 'copy process' breaks all the code-reference assumptions of the debug tool.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Particular hints:&lt;/P&gt;&lt;P&gt;Put the function-to-copy at the END of the code module it is in, to maximize your chance of catching any constants following the code.&lt;/P&gt;&lt;P&gt;At least for starters, make ramSpace 'static' and check your linker map to see that ramFunction() does indeed point to it.&amp;nbsp; Stack-space allocated at the 'outer level' of MAIN is effectively static anyway (allocated directly, and always there).&lt;/P&gt;&lt;P&gt;Since your disassembled-ram-code looks NOTHING like a 'while (1)' loop, I can only assume the memcpy did NOT get the result you desire.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 27 Jul 2016 15:36:16 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Copy-and-execute-code-from-RAM/m-p/556550#M33715</guid>
      <dc:creator>egoodii</dc:creator>
      <dc:date>2016-07-27T15:36:16Z</dc:date>
    </item>
  </channel>
</rss>

