<?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: Saving struct to flash memory using FlashProgram  in Kinetis Microcontrollers</title>
    <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Saving-struct-to-flash-memory-using-FlashProgram/m-p/427672#M24709</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I don't know what FlashProgram wants.&amp;nbsp; Would it want &amp;amp;first?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;However using structures in this way is not recommended due to how the compiler might pack the structures.&lt;/P&gt;&lt;P&gt;A structure may have hidden bytes to maintain alignment.&amp;nbsp; ARM natively normally aligns to four byte boundaries.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;offsetof() in &amp;lt;stddef.h&amp;gt; can be helpful in (de)serializing structures for storage and transmission.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 26 Oct 2015 12:40:04 GMT</pubDate>
    <dc:creator>bobpaddock</dc:creator>
    <dc:date>2015-10-26T12:40:04Z</dc:date>
    <item>
      <title>Saving struct to flash memory using FlashProgram</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Saving-struct-to-flash-memory-using-FlashProgram/m-p/427671#M24708</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello, I have a following problem. I want to save my own struct using the flash_demo example from SDK.&lt;/P&gt;&lt;P&gt;I am using FlashProgram function for that purpose.&lt;/P&gt;&lt;P&gt;What have I tried:&lt;/P&gt;&lt;P&gt;Here is my code (the rest of SDK flash_demo remains unchanged):&lt;/P&gt;&lt;PRE __default_attr="c++" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14457987126657600 jive_text_macro" data-renderedposition="92_8_1232_628" jivemacro_uid="_14457987126657600"&gt;&lt;P&gt;typedef struct&lt;/P&gt;&lt;P&gt;{&lt;/P&gt;&lt;P&gt;&amp;nbsp; int x, y;&lt;/P&gt;&lt;P&gt;&amp;nbsp; char name[40];&lt;/P&gt;&lt;P&gt;} myStruct;&lt;/P&gt;&lt;P&gt;int main(void)&lt;/P&gt;&lt;P&gt;{&lt;/P&gt;&lt;P&gt;/*rest of example code*/&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="line-height: 1.5;"&gt;myStruct first = { .x = 1, .y = 123, .name = "George" };&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="line-height: 1.5;"&gt;/*rest of example code*/&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; /********************************************************************&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; *&amp;nbsp;&amp;nbsp; For devices without SWAP, program some data for demo purposes&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; *********************************************************************/&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; destination = flashSSDConfig.PFlashBase + (flashSSDConfig.PFlashSize - 6*FTFx_PSECTOR_SIZE);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; end = flashSSDConfig.PFlashBase + (flashSSDConfig.PFlashSize - 4*FTFx_PSECTOR_SIZE);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; /*for (i = 0; i &amp;lt; BUFFER_SIZE_BYTE; i++)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Set source buffer&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; program_buffer[i] = i + 100;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }*/&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; size = sizeof (myStruct);//BUFFER_SIZE_BYTE;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* message for user */&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; PRINTF("\n\n\r----&amp;gt;Running FlashProgram() and FlashProgramCheck()...");&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; while ((destination + (size)) &amp;lt; end)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ret = FlashProgram(&amp;amp;flashSSDConfig, destination, size, \&lt;/P&gt;&lt;P&gt;&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;&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;&amp;nbsp;&amp;nbsp; first, g_FlashLaunchCommand);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (FTFx_OK != ret)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ErrorTrap(ret);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;P&gt;/*rest of example code*/&lt;/P&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Unfortunately this gives an error:&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;D:/Freescale/SDK/examples/frdmkl43z/demo_apps/flash_demo/main.c:354:9: error: incompatible type for argument 4 of 'FlashProgram'&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;1. Does it mean I am not able to save struct in flash memory? (somewhat contrary to what Mike Butcher said here -&amp;gt; &lt;A href="https://community.nxp.com/thread/332608" rel="nofollow noopener noreferrer" target="_blank"&gt;Reading flash - why not by normal memory access?&lt;/A&gt; )&lt;/P&gt;&lt;P&gt;2. Is there any way to transform any structure to uint8_t* which is required by FlashProgram?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I will be very grateful for any suggestions!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 25 Oct 2015 18:46:39 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Saving-struct-to-flash-memory-using-FlashProgram/m-p/427671#M24708</guid>
      <dc:creator>piotrcerba</dc:creator>
      <dc:date>2015-10-25T18:46:39Z</dc:date>
    </item>
    <item>
      <title>Re: Saving struct to flash memory using FlashProgram</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Saving-struct-to-flash-memory-using-FlashProgram/m-p/427672#M24709</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I don't know what FlashProgram wants.&amp;nbsp; Would it want &amp;amp;first?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;However using structures in this way is not recommended due to how the compiler might pack the structures.&lt;/P&gt;&lt;P&gt;A structure may have hidden bytes to maintain alignment.&amp;nbsp; ARM natively normally aligns to four byte boundaries.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;offsetof() in &amp;lt;stddef.h&amp;gt; can be helpful in (de)serializing structures for storage and transmission.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 26 Oct 2015 12:40:04 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Saving-struct-to-flash-memory-using-FlashProgram/m-p/427672#M24709</guid>
      <dc:creator>bobpaddock</dc:creator>
      <dc:date>2015-10-26T12:40:04Z</dc:date>
    </item>
    <item>
      <title>Re: Saving struct to flash memory using FlashProgram</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Saving-struct-to-flash-memory-using-FlashProgram/m-p/427673#M24710</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello Piotr Cerba:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Bob Paddock is correct, the &lt;STRONG&gt;FlashProgram()&lt;/STRONG&gt; function of the driver expects a pointer to a raw array of bytes, not a structure. You can cast the parameter to match the &lt;STRONG&gt;uint8_t*&lt;/STRONG&gt; expected by the driver, like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14460763799896832 jive_text_macro" data-renderedposition="113.44999694824219_8_1050_32" jivemacro_uid="_14460763799896832" modifiedtitle="true"&gt;&lt;P&gt;FlashProgram(&amp;amp;flashSSDConfig, destAdrss, sizeof(first), (uint8_t*)&amp;amp;first, g_FlashLaunchCommand);&lt;/P&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This is what I get in KDS with GCC using your code (except that I used address 0x80000) and considering that the ARM Cortex-M core in Kinetis uses little endianness:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="lia-inline-image-display-wrapper" image-alt="pastedImage_5.png"&gt;&lt;IMG alt="pastedImage_5.png" src="https://community.nxp.com/t5/image/serverpage/image-id/25563i4A0EC96E26B97945/image-size/large?v=v2&amp;amp;px=999" title="pastedImage_5.png" /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Since the structure is stored in Flash in the same order as the variable in RAM, then you can retrieve the data from flash by using a simple pointer to the same kind of structure, something like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14460797503283977 jive_text_macro" data-renderedposition="406.45001220703125_8_1050_368" jivemacro_uid="_14460797503283977" modifiedtitle="true"&gt;&lt;P&gt;typedef struct&lt;/P&gt;&lt;P&gt;{&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int x, y;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; char name[40];&lt;/P&gt;&lt;P&gt;} myStruct, *myStructPtr;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;//...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;myStruct first = { .custom = "ON", .x = 1, .y = 123, .name = "George" };&lt;/P&gt;&lt;P&gt;myStructPtr firstPtr = (myStructPtr)0x80000;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;//...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;destAdrss = 0x80000;&lt;/P&gt;&lt;P&gt;FlashProgram(&amp;amp;flashSSDConfig, destAdrss, sizeof(first), (uint8_t*)&amp;amp;first, g_FlashLaunchCommand);&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;//Erasing "first"&lt;/P&gt;&lt;P&gt;first.x = 0;&lt;/P&gt;&lt;P&gt;first.y = 0;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;//Recovering "first"&lt;/P&gt;&lt;P&gt;first.x = firstPtr -&amp;gt; x;&lt;/P&gt;&lt;P&gt;first.y = firstPtr -&amp;gt; y;&lt;/P&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;:smileyalert: Note that the core processor is 32-bit (4 bytes), but the smallest programming size for the flash memory controller in K64 (FTFE) is 8-bytes (phrase). Therefore the flash driver expects that the &lt;STRONG&gt;size&lt;/STRONG&gt; parameter is a multiple of 8 and the &lt;STRONG&gt;dest&lt;/STRONG&gt; address parameter is aligned to an 8-bytes boundary. The size of your structure is 40 + 4 + 4 = 48 bytes, which is good, but if you add more members and the size is not a multiple of 8, then you may need to add padding bytes, considering that the compiler may add its own padding bytes to a member which is not 4-bytes long, as mentioned by Bob.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I hope this information is helpful.&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>Mon, 02 Nov 2020 13:19:47 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Saving-struct-to-flash-memory-using-FlashProgram/m-p/427673#M24710</guid>
      <dc:creator>Jorge_Gonzalez</dc:creator>
      <dc:date>2020-11-02T13:19:47Z</dc:date>
    </item>
  </channel>
</rss>

