<?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: How to preserve state between resets?</title>
    <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/How-to-preserve-state-between-resets/m-p/613051#M36342</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;This will return the &amp;nbsp;current stack pointer value:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;static __inline__ void *sp_get(void)&lt;BR /&gt;{&lt;BR /&gt; void *sp;&lt;/P&gt;&lt;P&gt;__asm__ __volatile__ ("mrs %0, msp" : "=r"(sp));&lt;/P&gt;&lt;P&gt;return( sp );&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;However you probably actually want a constant from the linker script. &amp;nbsp;Probably not what you really want either.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;I move the .noinit section to the start of RAM so it is in a known place so that I can communicate between bootloader and the app at startup.&lt;BR /&gt;Simpy moving .noinit to before everything else gets rid of all of the 'growth' and messing with stack discussed at AVRFreaks.&lt;/P&gt;&lt;P&gt;The GPIOx section also simulates some of the XMega address space so I can share code between the Kenitis M0+ and an XMega.&lt;BR /&gt;&lt;BR /&gt;Make sure that the NOINIT space is more than you need but not by much. &amp;nbsp;Allocating to much wastes RAM and using more than allocated makes hair pulling bugs. &amp;nbsp;0x200/512 bytes is what I use in my project:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;MEMORY&lt;BR /&gt;{&lt;BR /&gt;VECTORS (rx) : ORIGIN = 0x0, LENGTH = 0x000000C0&lt;BR /&gt; BOOTCFG (r) : ORIGIN = 0x000003C0, LENGTH = 64&lt;BR /&gt; FLASHCFG (r) : ORIGIN = 0x00000400, LENGTH = 16&lt;BR /&gt; FLASH (rx) : ORIGIN = 0x00000410, LENGTH = (256K - 1K - 0x410) /* Flash size - EEPROM Sim size - vectors and Flash Config bytes */&lt;BR /&gt; EEPROMSIM (rx) : ORIGIN = (256K - 1K), LENGTH = 1K&lt;/P&gt;&lt;P&gt;BOOTLOADER (rx) : ORIGIN = 0x1C000000, LENGTH = 16K&lt;/P&gt;&lt;P&gt;NOINIT (rwx) : ORIGIN = 0x1FFFE000, LENGTH = 0x200 /* Space for variables that do not get zeroed at C startup */&lt;BR /&gt; GPIOx (rwx) : ORIGIN = 0x1FFFE200, LENGTH = 0x10 /* Space to simulate AVR GPIOx */&lt;BR /&gt; RAM (rwx) : ORIGIN = 0x1FFFE210, LENGTH = (32K - 0x200 - 0x10) /* Internal SRAM. flash_kinetis_cmd executes out of RAM */&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 19 Apr 2017 13:27:31 GMT</pubDate>
    <dc:creator>bobpaddock</dc:creator>
    <dc:date>2017-04-19T13:27:31Z</dc:date>
    <item>
      <title>How to preserve state between resets?</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/How-to-preserve-state-between-resets/m-p/613044#M36335</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi guys,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My application firmware enumerates as a keyboard USB device, or as a BusPal protocol translator USB device. I must be able to switch between the two. I could reenumerate from the application software as the other USB device without resetting the MCU, but there is a lot of state involved in the firmware, and I'd rather not maintain the state upon switches.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Instead, I'd much rather reset the MCU to clean its state, and enumerate as the desired device. But for this to work, I have to store some state, so that the application firmware can branch to the USB keyboard routine vs the BusPal routine.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Is there any way to save state across resets? For example by using a register, SRAM or any other means? I used to use AVR microcontrollers and &lt;A href="http://www.avrfreaks.net/forum/transferring-state-boot-section-application-section"&gt;this was possible with AVRs&lt;/A&gt;. What about the K22? Also, what kind of reset should I use? Software reset, watchdog reset, or anything else?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you for your help!&lt;/P&gt;&lt;P&gt;- Laci&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 15 Apr 2017 13:15:18 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/How-to-preserve-state-between-resets/m-p/613044#M36335</guid>
      <dc:creator>laszlomonda</dc:creator>
      <dc:date>2017-04-15T13:15:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to preserve state between resets?</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/How-to-preserve-state-between-resets/m-p/613045#M36336</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Laci,&lt;/P&gt;&lt;P&gt;If I remember correctly, there is a peripheral register bit/flag which can you tell the reset reason (say power-on reset or software reset, or watchdog). What I have used in some of my projects is to have a SRAM area with my status (protected with a CRC) which I read right after reset (before the startup code would initialize the SRAM). That way I can 'carry over' state information between resets.&lt;/P&gt;&lt;P&gt;Other than that, I'm usually programming the on-chip internal flash or EEPROM (see &lt;A class="link-titled" href="https://mcuoneclipse.com/2014/05/31/configuration-data-using-the-internal-flash-instead-of-an-external-eeprom/" title="https://mcuoneclipse.com/2014/05/31/configuration-data-using-the-internal-flash-instead-of-an-external-eeprom/"&gt;Configuration Data: Using the Internal FLASH instead of an external EEPROM | MCU on Eclipse&lt;/A&gt;&amp;nbsp;) or using an external SRAM or EEPROM for this. There can be other options too.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I hop the helps,&lt;/P&gt;&lt;P&gt;Erich&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 15 Apr 2017 13:39:26 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/How-to-preserve-state-between-resets/m-p/613045#M36336</guid>
      <dc:creator>BlackNight</dc:creator>
      <dc:date>2017-04-15T13:39:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to preserve state between resets?</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/How-to-preserve-state-between-resets/m-p/613046#M36337</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Erich,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Yes, know that it's possible to query the source of the reset via the Reset Control Module, but it&amp;nbsp;isn't a whole lot of information to be transferred.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm much more interested about using the internal SRAM to transfer state across resets. How to do that?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;&lt;P&gt;Laci&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 16 Apr 2017 18:54:51 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/How-to-preserve-state-between-resets/m-p/613046#M36337</guid>
      <dc:creator>laszlomonda</dc:creator>
      <dc:date>2017-04-16T18:54:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to preserve state between resets?</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/How-to-preserve-state-between-resets/m-p/613047#M36338</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Laci,&lt;/P&gt;&lt;P&gt;the trick is to avoid that the normal C/C++ startup code is erasing the memory with the information (by default, the startup code initializes all memory used).&lt;/P&gt;&lt;P&gt;One way is described in &lt;A class="link-titled" href="https://mcuoneclipse.com/2014/04/19/gnu-linker-can-you-not-initialize-my-variable/" title="https://mcuoneclipse.com/2014/04/19/gnu-linker-can-you-not-initialize-my-variable/"&gt;GNU Linker, can you NOT Initialize my Variable? | MCU on Eclipse&lt;/A&gt;&amp;nbsp;.&lt;/P&gt;&lt;P&gt;Or simply hook your code into the part right after the reset (in the startup code), this depends on your environment/toolchain/etc.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I hope this helps,&lt;/P&gt;&lt;P&gt;Erich&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 16 Apr 2017 20:32:06 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/How-to-preserve-state-between-resets/m-p/613047#M36338</guid>
      <dc:creator>BlackNight</dc:creator>
      <dc:date>2017-04-16T20:32:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to preserve state between resets?</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/How-to-preserve-state-between-resets/m-p/613048#M36339</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Erich,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In your true style, of course there's an MCU on Eclipse article about this topic. :smileyhappy:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks a ton! It's super useful, and makes perfect sense.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Have a nice day, and keep it up!&lt;/P&gt;&lt;P&gt;Laci&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 17 Apr 2017 10:04:42 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/How-to-preserve-state-between-resets/m-p/613048#M36339</guid>
      <dc:creator>laszlomonda</dc:creator>
      <dc:date>2017-04-17T10:04:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to preserve state between resets?</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/How-to-preserve-state-between-resets/m-p/613049#M36340</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;With GCC the normal zeroing of a variable by the C start up code can be disabled:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;#define NO_INIT_GCC __attribute__ ((section (".noinit")))&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;static uint16_t your_var_u16_n NO_INIT_GCC;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Such variables are collected in the .noinit section.&lt;BR /&gt;Checker that your linker script has such a section, how big it is and where it is located.&lt;/P&gt;&lt;P&gt;By default it is usually after the .data section.&lt;/P&gt;&lt;P&gt;I move it to be the first section in RAM to make sharing with a bootloader easier.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Check the 'Reset' state to make sure the variable does get initialized to a valid value for your application as its value will be random otherwise.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Also be aware that some of the lowest power states do not preserve the RAM.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 17 Apr 2017 19:18:09 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/How-to-preserve-state-between-resets/m-p/613049#M36340</guid>
      <dc:creator>bobpaddock</dc:creator>
      <dc:date>2017-04-17T19:18:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to preserve state between resets?</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/How-to-preserve-state-between-resets/m-p/613050#M36341</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you so much, Bob!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I think your solution is the most elegant one, although my case is more complicated than usually, because I want to transfer&amp;nbsp;data between the bootloader and the application firmware which allocate memory differently.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;There's an&amp;nbsp;&lt;A href="http://www.avrfreaks.net/comment/1077386#comment-1077386"&gt;extremely relevant suggestion&lt;/A&gt;&amp;nbsp;which worked perfectly on AVRs. I'm trying to implement the same on Kinetis, but I &lt;A _jive_internal="true" href="https://community.nxp.com/message/897892"&gt;cannot access the stack pointer yet&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks again!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 18 Apr 2017 20:24:36 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/How-to-preserve-state-between-resets/m-p/613050#M36341</guid>
      <dc:creator>laszlomonda</dc:creator>
      <dc:date>2017-04-18T20:24:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to preserve state between resets?</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/How-to-preserve-state-between-resets/m-p/613051#M36342</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;This will return the &amp;nbsp;current stack pointer value:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;static __inline__ void *sp_get(void)&lt;BR /&gt;{&lt;BR /&gt; void *sp;&lt;/P&gt;&lt;P&gt;__asm__ __volatile__ ("mrs %0, msp" : "=r"(sp));&lt;/P&gt;&lt;P&gt;return( sp );&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;However you probably actually want a constant from the linker script. &amp;nbsp;Probably not what you really want either.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;I move the .noinit section to the start of RAM so it is in a known place so that I can communicate between bootloader and the app at startup.&lt;BR /&gt;Simpy moving .noinit to before everything else gets rid of all of the 'growth' and messing with stack discussed at AVRFreaks.&lt;/P&gt;&lt;P&gt;The GPIOx section also simulates some of the XMega address space so I can share code between the Kenitis M0+ and an XMega.&lt;BR /&gt;&lt;BR /&gt;Make sure that the NOINIT space is more than you need but not by much. &amp;nbsp;Allocating to much wastes RAM and using more than allocated makes hair pulling bugs. &amp;nbsp;0x200/512 bytes is what I use in my project:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;MEMORY&lt;BR /&gt;{&lt;BR /&gt;VECTORS (rx) : ORIGIN = 0x0, LENGTH = 0x000000C0&lt;BR /&gt; BOOTCFG (r) : ORIGIN = 0x000003C0, LENGTH = 64&lt;BR /&gt; FLASHCFG (r) : ORIGIN = 0x00000400, LENGTH = 16&lt;BR /&gt; FLASH (rx) : ORIGIN = 0x00000410, LENGTH = (256K - 1K - 0x410) /* Flash size - EEPROM Sim size - vectors and Flash Config bytes */&lt;BR /&gt; EEPROMSIM (rx) : ORIGIN = (256K - 1K), LENGTH = 1K&lt;/P&gt;&lt;P&gt;BOOTLOADER (rx) : ORIGIN = 0x1C000000, LENGTH = 16K&lt;/P&gt;&lt;P&gt;NOINIT (rwx) : ORIGIN = 0x1FFFE000, LENGTH = 0x200 /* Space for variables that do not get zeroed at C startup */&lt;BR /&gt; GPIOx (rwx) : ORIGIN = 0x1FFFE200, LENGTH = 0x10 /* Space to simulate AVR GPIOx */&lt;BR /&gt; RAM (rwx) : ORIGIN = 0x1FFFE210, LENGTH = (32K - 0x200 - 0x10) /* Internal SRAM. flash_kinetis_cmd executes out of RAM */&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 19 Apr 2017 13:27:31 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/How-to-preserve-state-between-resets/m-p/613051#M36342</guid>
      <dc:creator>bobpaddock</dc:creator>
      <dc:date>2017-04-19T13:27:31Z</dc:date>
    </item>
  </channel>
</rss>

