<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>FDS Write: Wrong Written Data</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/33848/fds-write-wrong-written-data</link><description>Good Afternoon, 
 
 I&amp;#39;m writing some config data on an empty record the first time I turn on my board and updating that same record every next time I turn on the board. 
 While the writing of the first data is correctly written with fds_record_write(</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 29 May 2018 15:36:39 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/33848/fds-write-wrong-written-data" /><item><title>RE: FDS Write: Wrong Written Data</title><link>https://devzone.nordicsemi.com/thread/133881?ContentTypeID=1</link><pubDate>Tue, 29 May 2018 15:36:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:16eec72e-dc72-4b12-a79c-605307d73908</guid><dc:creator>jloliveira</dc:creator><description>&lt;p&gt;Thank you Rune, that worked.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: FDS Write: Wrong Written Data</title><link>https://devzone.nordicsemi.com/thread/130129?ContentTypeID=1</link><pubDate>Fri, 27 Apr 2018 13:35:36 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:696ad2d2-bb06-442c-9d5e-2bf709b9cfdf</guid><dc:creator>Rune Holmgren</dc:creator><description>&lt;p&gt;Sorry, I should have realized this when first looking at your post. The problem is not the flash, but rather that you are trying to print strings which are not null terminated.&lt;/p&gt;
&lt;p&gt;The reason you are successfully able to write and later print strings that are not a length divisible by 4 is that the flash memory will be padded with &amp;#39;0&amp;#39;s. When you write &amp;quot;tes&amp;quot; to flash, the actual flash will be padded with a 0 byte at the end. In flash the following values will be present [116, 101, 115, 0]. To C this is the string &amp;quot;tes&amp;quot; with the final byte telling sprintf that the string has ended. When you write the string &amp;quot;test&amp;quot; to flash, the actual content of flash will be [116, 101, 115, 116] with no null terminator. When you send a reference to this flash address to the logging framework, nrf_log will attempt to print all the bytes it can find until a &amp;#39;0&amp;#39; is found. This may be an incredibly long string and the buffers overflow and cause the device to hang.&lt;/p&gt;
&lt;p&gt;To resolve the issue, simply ensure that the data you are writing to flash is either null terminated, or that you add a null terminator after reading it out.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://devzone.nordicsemi.com/resized-image/__size/1040x1124/__key/support-attachments/beef5d1b77644c448dabff31668f3a47-7535d71311764d8dbcf173e6a3af9a6d/printing.PNG" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;In this image, I have changed the printing to print specifically the first 4 characters present in flash. As you can see the word &amp;quot;test&amp;quot; is present, but there is no null terminator. You can&amp;#39;t send this string as is to sprintf&amp;nbsp;without adding a null terminator.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;br /&gt;Rune Holmgren&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: FDS Write: Wrong Written Data</title><link>https://devzone.nordicsemi.com/thread/130093?ContentTypeID=1</link><pubDate>Fri, 27 Apr 2018 11:27:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:868f4b00-81cd-4f2e-ba53-e8390e41d54e</guid><dc:creator>jloliveira</dc:creator><description>&lt;p&gt;Hi Rune,&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;What I meant is for you to please to consider writing a string with 4 bytes, such as &amp;quot;test&amp;quot; or &amp;quot;test1234&amp;quot;.&lt;/p&gt;
&lt;p&gt;When reading it back and printing frec.p_data&amp;nbsp;as you did with print all it should halt the application - at least on my side it does, everytime.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thank you again,&lt;/p&gt;
&lt;p&gt;Jo&amp;atilde;o&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: FDS Write: Wrong Written Data</title><link>https://devzone.nordicsemi.com/thread/130091?ContentTypeID=1</link><pubDate>Fri, 27 Apr 2018 11:18:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f8100e78-a1ff-4ee5-b4b8-b3623c2fd766</guid><dc:creator>Rune Holmgren</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Very sorry for the late reply.&lt;/p&gt;
&lt;p&gt;I have tried reproducing this issue as you described, but there seems to be no problem writing the string to flash and reading it back. I added the log statement as you described and wrote the string and read it back without any issues. I performed my test on SDK 15.&lt;/p&gt;
&lt;p&gt;I also discussed the issue with one of the developers working on FDS, and he was not familiar with the issue you are describing. Am I understanding it correctly, you are not getting any error while writing the record, but when you read it back it&amp;#39;s not working as expected?&lt;/p&gt;
&lt;p&gt;&lt;img src="https://devzone.nordicsemi.com/resized-image/__size/1040x1083/__key/support-attachments/beef5d1b77644c448dabff31668f3a47-7535d71311764d8dbcf173e6a3af9a6d/working.PNG" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;Any information you have which may help us recreating the issue is very welcome.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;br /&gt;Rune Holmgren&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: FDS Write: Wrong Written Data</title><link>https://devzone.nordicsemi.com/thread/129955?ContentTypeID=1</link><pubDate>Mon, 23 Apr 2018 15:45:17 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:be008c90-84c0-49bc-a6c6-9f6804d300ed</guid><dc:creator>jloliveira</dc:creator><description>&lt;p&gt;Dear Rune,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Do you have any news regarding my issue reported above?&lt;/p&gt;
&lt;p&gt;I need an answer urgently.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Thank you,&lt;/p&gt;
&lt;p&gt;João&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: FDS Write: Wrong Written Data</title><link>https://devzone.nordicsemi.com/thread/129954?ContentTypeID=1</link><pubDate>Sun, 15 Apr 2018 22:36:40 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:68e55f4a-ee46-4406-965e-f24ab99d0ef8</guid><dc:creator>jloliveira</dc:creator><description>&lt;p&gt;Hi again Rune,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;After struggling a lot I just found out that the problem occurs when I try to write any userid string that has a number of bytes multiple of 4 (that is 4bytes, 8bytes, 12bytes, etc). It writes without any error but when I try to read that flash data afterwards the app gets stuck without reporting erros.&lt;/p&gt;
&lt;p&gt;I found it very weird and tested it then in the flash_fds example directly and the same issue occurs.&lt;/p&gt;
&lt;p&gt;Just try to write any multiple of 4 bytes string with the command &amp;quot;write fid key &amp;quot;multiple_of_4_bytes_string&amp;quot;&amp;quot; and then read the data by calling the &amp;quot;print all&amp;quot; command but with a NRF_LOG_INFO(&amp;quot;%s&amp;quot;,&amp;nbsp;(char*)frec.p_data) line (or any other line that makes use of frec.p_data) inside the&amp;nbsp;print_all_cmd function.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I guess this is a very weird bug in the FDS API that might&amp;nbsp;be&amp;nbsp;related&amp;nbsp;to word-unalignment in flash.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Looking forward to hear about your clarification&amp;nbsp;about this issue,&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;João&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: FDS Write: Wrong Written Data</title><link>https://devzone.nordicsemi.com/thread/129953?ContentTypeID=1</link><pubDate>Thu, 12 Apr 2018 18:05:24 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:773eae92-6b35-42fe-b018-cb3b2003d8f5</guid><dc:creator>jloliveira</dc:creator><description>&lt;p&gt;Dear Rune,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Thank you for your reply, I also used a static variable to make sure it is word-aligned but still required to declare __ALIGN(4) in the DEVICE struct declaration itself&amp;nbsp;in order to make sure it never returns&amp;nbsp;FDS_ERR_UNALIGNED_ADDR when writing to flash, as&amp;nbsp;flash_write_config_record() is called from different scopes (from different .c files).&lt;/p&gt;
&lt;p&gt;In the meantime I also created read/write fds functions for a userid in char* format. As these are also called from different scopes, in order to guarantee no word-alignment errors, the&amp;nbsp;flash_write_userid_record() function copies the userid2write data into an __ALIGN(4) static char* variable to be written into flash.&lt;/p&gt;
&lt;p&gt;As per&amp;nbsp;flash_read_userid(),&amp;nbsp;I had to use a pointer to char* pointing to the flash data char* to return it.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Although&amp;nbsp;every read/write call seem to work fine, for some reason I can&amp;#39;t identify some rare times the app crashes when calling&amp;nbsp;flash_read_userid() just after setting up the BLE connection without any assert, and the flash record seem to get corrupted as I can&amp;#39;t write to it anymore unless I delete it before.&lt;/p&gt;
&lt;p&gt;Is there anything I&amp;#39;m doing wrong with the read/write user id functions and calls, or could it be colliding with the BLE radio activity somehow?&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Find the new code below:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/* Declarations in flash.h */

/* File ID and Key used for the configuration record. */

#define CONFIG_FILE     (0xF010)
#define CONFIG_REC_KEY  (0x7010)

/* File ID and Key used for the userid record. */

#define USERID_FILE     (0xF050)
#define USERID_REC_KEY  (0x7050)

typedef struct
{
	uint8_t enable;
	uint8_t time_adv;
}ADVERTISE;

typedef struct
{
	uint8_t state;
	ADVERTISE adv2;
}__ALIGN(4) DEVICE;


/* ========= flash.c ==========

ret_code_t flash_read_config(DEVICE * p_cfg)
{
    ret_code_t read_success = FDS_ERR_NOT_FOUND;

    fds_record_desc_t desc = {0};
    fds_find_token_t  tok  = {0};

    //Loop until all records with the given key and file ID have been found.
    while (fds_record_find(CONFIG_FILE, CONFIG_REC_KEY, &amp;amp;desc, &amp;amp;tok) == FDS_SUCCESS)
    {
        ret_code_t rc;
        fds_flash_record_t frec = {0};

        rc = fds_record_open(&amp;amp;desc, &amp;amp;frec);

        switch (rc)
        {
            case FDS_SUCCESS:
                break;

            case FDS_ERR_CRC_CHECK_FAILED:
                #ifdef _UART_FLASH_
                NRF_LOG_INFO(&amp;quot;error: CRC check failed!&amp;quot;);
                #endif
                continue;

            case FDS_ERR_NOT_FOUND:
                #ifdef _UART_FLASH_
                NRF_LOG_INFO(&amp;quot;error: record not found!&amp;quot;);
                #endif
                continue;

            default:
            {
                #ifdef _UART_FLASH_
                NRF_LOG_INFO(&amp;quot;error: unexpected error %s.&amp;quot;,
                                fds_err_str[rc]);
                #endif

                continue;
            }
        }

        //memcpy to pass p_cfg pointer with flash data from frec.p_data
        memcpy(p_cfg, frec.p_data, sizeof(DEVICE));

        NRF_LOG_INFO(&amp;quot;FLASH READ CONFIG 00%x0%x0%x @ %x:&amp;quot;,
        p_cfg-&amp;gt;adv2.time_adv, p_cfg-&amp;gt;adv2.enable, p_cfg-&amp;gt;state, tok.p_addr);
        NRF_LOG_INFO(&amp;quot;state: %d\r\n&amp;quot; 
                      &amp;quot;adv2.enable: %d\r\n&amp;quot;
                      &amp;quot;adv2.time_adv: %d\r\n&amp;quot;,
                      p_cfg-&amp;gt;state, p_cfg-&amp;gt;adv2.enable, p_cfg-&amp;gt;adv2.time_adv);

        rc = fds_record_close(&amp;amp;desc);
        APP_ERROR_CHECK(rc);

        read_success = FDS_SUCCESS; //if found and read record
    }

    return read_success;
}

void flash_write_config_record(DEVICE * config2write)
{
    ret_code_t rc;

    fds_record_desc_t desc = {0};
    fds_find_token_t  tok  = {0};

    // Copy write data to static word-aligned variable.
    uint32_t const len_cfg = sizeof(config2write);
    __ALIGN(4) static DEVICE flash_write_data;
    memcpy(&amp;amp;flash_write_data, config2write, len_cfg);

    //CONFIG_FILE and CONFIG_REC_KEY defined in flash.h
    rc = fds_record_find(CONFIG_FILE, CONFIG_REC_KEY, &amp;amp;desc, &amp;amp;tok);
    if (rc == FDS_SUCCESS) //record exists =&amp;gt; update it
    {
        /* A config file is in flash. Let&amp;#39;s update it. */
        fds_flash_record_t config = {0};

        /* Open the record and read its contents. */
        rc = fds_record_open(&amp;amp;desc, &amp;amp;config);
        APP_ERROR_CHECK(rc);

        //compare existing config in flash with new one      
        NRF_LOG_INFO(&amp;quot;CONFIG IN FLASH:\r\n&amp;quot;
                  &amp;quot;state: %d\r\n&amp;quot; 
                  &amp;quot;adv2.enable: %d\r\n&amp;quot;
                  &amp;quot;adv2.time_adv: %d\r\n&amp;quot;,
                  ((DEVICE*)config.p_data)-&amp;gt;state, ((DEVICE*)config.p_data)-&amp;gt;adv2.enable, ((DEVICE*)config.p_data)-&amp;gt;adv2.time_adv);
        
        NRF_LOG_INFO(&amp;quot;NEW CONFIG TO UPDATE:\r\n&amp;quot;
                      &amp;quot;state: %d\r\n&amp;quot; 
                      &amp;quot;adv2.enable: %d\r\n&amp;quot;
                      &amp;quot;adv2.time_adv: %d\r\n&amp;quot;,
                      flash_write_data.state, flash_write_data.adv2.enable, flash_write_data.adv2.time_adv);

        uint8_t ret = memcmp(&amp;amp;flash_write_data, config.p_data, len_cfg);
        
        // Close the record when done reading. 
        rc = fds_record_close(&amp;amp;desc);
        APP_ERROR_CHECK(rc);
        
        if(ret != 0) //if different update config in flash
        {
            record_update(CONFIG_FILE, CONFIG_REC_KEY, desc, &amp;amp;flash_write_data, len_cfg);

            NRF_LOG_INFO(&amp;quot;===== UPDATED FDS FLASH =====);

            m_write_finished = false;
        }
        else
        {
             #ifdef _UART_FLASH_
             NRF_LOG_INFO(&amp;quot;FLASH: same config data as previous, nothing to be done:\r\n&amp;quot;
                      &amp;quot;state: %d\r\n&amp;quot; 
                      &amp;quot;adv2.enable: %d\r\n&amp;quot;
                      &amp;quot;adv2.time_adv: %d\r\n&amp;quot;,
                      ((DEVICE*)config.p_data)-&amp;gt;state, ((DEVICE*)config.p_data)-&amp;gt;adv2.enable, ((DEVICE*)config.p_data)-&amp;gt;adv2.time_adv);
             #endif
        }
    }
    else
    {
        /* System config not found; write a new one. */
        NRF_LOG_INFO(&amp;quot;NEW CONFIG TO WRITE:\r\n&amp;quot;
                      &amp;quot;state: %d\r\n&amp;quot; 
                      &amp;quot;adv2.enable: %d\r\n&amp;quot;
                      &amp;quot;adv2.time_adv: %d\r\n&amp;quot;,
                      flash_write_data.state, flash_write_data.adv2.enable, flash_write_data.adv2.time_adv);

        record_write(CONFIG_FILE, CONFIG_REC_KEY, &amp;amp;flash_write_data, len_cfg);

        NRF_LOG_INFO(&amp;quot;===== WROTE FDS FLASH =====&amp;quot;);

        m_write_finished = false;
    }
}

//NOTE: Need Pointer to Pointer for userid to return the char* in the address pointing to freq.p_data because it is an array of chars and therefore can&amp;#39;t use memcpy to copy freq.p_data and return because it loses scope
static void record_write(uint32_t fid,
                         uint32_t key,
                         void const * p_data,
                         uint32_t len)
{
    fds_record_t const rec =
    {
        .file_id           = fid,
        .key               = key,
        .data.p_data       = p_data,
        .data.length_words = (len + 3) / sizeof(uint32_t)
    };

    #ifdef _UART_FLASH_
    NRF_LOG_INFO(&amp;quot;--&amp;gt; writing record to flash...\r\n&amp;quot;
                    &amp;quot;file: 0x%x, key: 0x%x, \&amp;quot;%s\&amp;quot;, len: %u bytes&amp;quot;,
                    fid, key, p_data, len);
    #endif

    ret_code_t rc = fds_record_write(NULL, &amp;amp;rec);

    #ifdef _UART_FLASH_
    if (rc != FDS_SUCCESS)
    {
        NRF_LOG_INFO(&amp;quot;--&amp;gt; error: fds_record_write() returned %s.&amp;quot;,
                        fds_err_str[rc]);
    }
    #endif
}

static void record_update(uint32_t fid,
                         uint32_t key,
                         fds_record_desc_t desc, 
                         void const * p_data,
                         uint32_t len)
{
    fds_record_t const rec =
    {
        .file_id           = fid,
        .key               = key,
        .data.p_data       = p_data,
        .data.length_words = (len + 3) / sizeof(uint32_t)
    };

    #ifdef _UART_FLASH_
    NRF_LOG_INFO(&amp;quot;--&amp;gt; updating record in flash...\r\n&amp;quot;
                    &amp;quot;file: 0x%x, key: 0x%x, \&amp;quot;%s\&amp;quot;, len: %u bytes&amp;quot;,
                    fid, key, p_data, len);
    #endif

    ret_code_t rc = fds_record_update(&amp;amp;desc, &amp;amp;rec);

    #ifdef _UART_FLASH_
    if (rc != FDS_SUCCESS)
    {
        NRF_LOG_INFO(&amp;quot;--&amp;gt; error: fds_record_write() returned %s.&amp;quot;,
                        fds_err_str[rc]);
    }
    #endif
}

ret_code_t flash_read_userid(char ** userid)
{
    ret_code_t read_success = FDS_ERR_NOT_FOUND;

    fds_record_desc_t desc = {0};
    fds_find_token_t  tok  = {0};

    //Loop until all records with the given key and file ID have been found.
    while (fds_record_find(USERID_FILE, USERID_REC_KEY, &amp;amp;desc, &amp;amp;tok) == FDS_SUCCESS)
    {
        ret_code_t rc;
        fds_flash_record_t frec = {0};

        rc = fds_record_open(&amp;amp;desc, &amp;amp;frec);
        switch (rc)
        {
            case FDS_SUCCESS:
                break;

            case FDS_ERR_CRC_CHECK_FAILED:
                #ifdef _UART_FLASH_
                NRF_LOG_INFO(&amp;quot;error: CRC check failed!&amp;quot;);
                #endif
                continue;

            case FDS_ERR_NOT_FOUND:
                #ifdef _UART_FLASH_
                NRF_LOG_INFO(&amp;quot;error: record not found!&amp;quot;);
                #endif
                continue;

            default:
            {
                #ifdef _UART_FLASH_
                NRF_LOG_INFO(&amp;quot;error: unexpecte error %s.&amp;quot;,
                                fds_err_str[rc]);
                #endif

                continue;
            }
        }

        //Memcpy frec.p_data address and point to copy&amp;#39;s address
        uint32_t const len_str = strlen((char*)frec.p_data)+1; //NOTE: +1 to include NULL char in end of string
        __ALIGN(4) static uint8_t * flash_read_data;
        flash_read_data = malloc(sizeof(uint8_t) * len_str);
        memset(flash_read_data, 0x00, sizeof(flash_read_data));
        memcpy(flash_read_data, frec.p_data, len_str);
        
        *userid = (char*)flash_read_data;

        rc = fds_record_close(&amp;amp;desc);
        APP_ERROR_CHECK(rc);

        read_success = FDS_SUCCESS; //if found and read record
    }

    return read_success;
}

void flash_write_userid_record(char * userid2write)
{
    ret_code_t rc;

    fds_record_desc_t desc = {0};
    fds_find_token_t  tok  = {0};

    // Copy write data to static word-aligned variable.
    uint32_t const len_str = strlen(userid2write)+1; //NOTE: +1 to include NULL char in end of string
    __ALIGN(4) static char * flash_write_data;
    flash_write_data = malloc(sizeof(char) * len_str);
    memset(flash_write_data, 0x00, sizeof(flash_write_data));
    memcpy(flash_write_data, userid2write, len_str);

    //CONFIG_FILE and CONFIG_REC_KEY defined in flash.h
    rc = fds_record_find(USERID_FILE, USERID_REC_KEY, &amp;amp;desc, &amp;amp;tok);
    if (rc == FDS_SUCCESS) //record exists =&amp;gt; update it
    {
        /// A userid file is in flash. Let&amp;#39;s update it.
        fds_flash_record_t userid = {0};

        // Open the record and read its contents. 
        rc = fds_record_open(&amp;amp;desc, &amp;amp;userid);
        APP_ERROR_CHECK(rc);
 
        //compare existing config in flash with new one      
        //NOTE: need to copy flash data to static var to hold the data correctly, and __ALIGNED(4) to guarantee word-alignment
        uint32_t const len_str = strlen((char*)userid.p_data)+1; //NOTE: +1 to include NULL char in end of string
        __ALIGN(4) static char * flash_read_data;
        flash_read_data = malloc(sizeof(char) * len_str);
        memset(flash_read_data, 0x00, sizeof(flash_read_data));
        memcpy(flash_read_data, userid.p_data, len_str);
        

        //__ALIGN(4) static char * flash_read_data;
        //flash_read_data = (char*)userid.p_data;

        #ifdef _UART_FLASH_
        NRF_LOG_INFO(&amp;quot;NEW USERID TO UPDATE: %s&amp;quot;, userid2write);
        NRF_LOG_INFO(&amp;quot;USERID IN FLASH     : %s&amp;quot;, flash_read_data);
        #endif

        //NOTE: Use strlen to compare strings until end of first string
        uint8_t ret = memcmp(userid2write, flash_read_data, len_str);

        // Close the record when done reading. 
        rc = fds_record_close(&amp;amp;desc);
        APP_ERROR_CHECK(rc);

        if(ret != 0) //if different update config in flash
        {
            // Write the updated record to flash.
            #ifdef _UART_FLASH_            
            NRF_LOG_INFO(&amp;quot;FLASH TO UPDATE W/ NEW USERID: %s&amp;quot;, flash_write_data);       
            #endif

            record_update(USERID_FILE, USERID_REC_KEY, desc, flash_write_data, len_str);

            #ifdef _UART_FLASH_
            NRF_LOG_INFO(&amp;quot;===== UPDATED FDS FLASH W/ USERID =====&amp;quot;);
            #endif

            m_write_finished = false;
        }
        else
        {
             #ifdef _UART_FLASH_
             NRF_LOG_INFO(&amp;quot;FLASH: same userid data as previous, nothing to be done&amp;quot;);
             #endif
        }
    }
    else
    {
        // System config not found; write a new one.
        #ifdef _UART_FLASH_
        NRF_LOG_INFO(&amp;quot;FLASH TO WRITE NEW USERID: %s&amp;quot;, flash_write_data);
        #endif

        record_write(USERID_FILE, USERID_REC_KEY, flash_write_data, len_str);

        #ifdef _UART_FLASH_
        NRF_LOG_INFO(&amp;quot;===== WROTE FDS FLASH =====&amp;quot;);
        #endif

        m_write_finished = false;
    }
}
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Thank you,&lt;/p&gt;
&lt;p&gt;João&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: FDS Write: Wrong Written Data</title><link>https://devzone.nordicsemi.com/thread/129952?ContentTypeID=1</link><pubDate>Wed, 11 Apr 2018 08:03:12 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:70afe036-eea5-48ce-a157-a6572a99d5f4</guid><dc:creator>Rune Holmgren</dc:creator><description>&lt;p&gt;Sorry for the late reply.&lt;/p&gt;
&lt;p&gt;The flash in the nRF5 series can only handle whole words, so you need to make sure all data is aligned with 4-byte words. My guess would be that you are having problems with &amp;quot;config2write&amp;quot; not being word aligned. In my code above I had it stored as a static variable, so my compiler will just put it in RAM at some suitable address and there will never be a problem. If I were to store it in a struct or array of some kind it&amp;#39;s possible to get into a situation where the data is not aligned.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;br /&gt;Rune Holmgren&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: FDS Write: Wrong Written Data</title><link>https://devzone.nordicsemi.com/thread/129951?ContentTypeID=1</link><pubDate>Fri, 06 Apr 2018 11:41:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:af550194-e47f-49bf-888e-6d563ab2a6e4</guid><dc:creator>jloliveira</dc:creator><description>&lt;p&gt;Hi Rune,&lt;/p&gt;
&lt;p&gt;Thank you for your help.&lt;/p&gt;
&lt;p&gt;I tried the code directly in the flash_fds example as per your code and it works fine.&lt;/p&gt;
&lt;p&gt;Yet, when I use exactly the same code in my app I&amp;#39;m struggling with code alignment erros with the&amp;nbsp;fds_record_update() returning error&amp;nbsp;FDS_ERR_UNALIGNED_ADDR. I guess the same could happen in your code eventually.&amp;nbsp;How can I guarantee that the config2write address I&amp;#39;m trying to save on flash is word aligned?&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Thank you,&lt;/p&gt;
&lt;p&gt;João&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: FDS Write: Wrong Written Data</title><link>https://devzone.nordicsemi.com/thread/129950?ContentTypeID=1</link><pubDate>Wed, 04 Apr 2018 14:24:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ea76f6f7-96bb-4b8e-9949-83bf873ebcb2</guid><dc:creator>Rune Holmgren</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I tested the flash part of your code by copying it over into the fds&amp;nbsp;example in SDK15.0.0. I added some logging of the state struct member to see the initial write, value on boot and update writes. As you can see below where I started with an empty device and pressed reset twice, I first write the initial value of 1, then I update it to 2 and after that, the value is 2 already so no new update is performed.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://devzone.nordicsemi.com/resized-image/__size/780x1029/__key/support-attachments/beef5d1b77644c448dabff31668f3a47-7535d71311764d8dbcf173e6a3af9a6d/flash_5F00_test_5F00_results.PNG" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;Here is the main.c file I used, the rest is just the FDS example in SDK15.0.0 unaltered:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/**
 * Copyright (c) 2017 - 2018, Nordic Semiconductor ASA
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form, except as embedded into a Nordic
 *    Semiconductor ASA integrated circuit in a product or a software update for
 *    such product, must reproduce the above copyright notice, this list of
 *    conditions and the following disclaimer in the documentation and/or other
 *    materials provided with the distribution.
 * 
 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
 *    contributors may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 * 
 * 4. This software, with or without modification, must only be used with a
 *    Nordic Semiconductor ASA integrated circuit.
 * 
 * 5. Any software provided in binary form under this license must not be reverse
 *    engineered, decompiled, modified and/or disassembled.
 * 
 * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA &amp;quot;AS IS&amp;quot; AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 */

#include &amp;lt;stdint.h&amp;gt;
#include &amp;lt;string.h&amp;gt;
#include &amp;quot;nrf.h&amp;quot;
#include &amp;quot;nordic_common.h&amp;quot;
#ifdef SOFTDEVICE_PRESENT
#include &amp;quot;nrf_sdh.h&amp;quot;
#include &amp;quot;nrf_sdh_ble.h&amp;quot;
#else
#include &amp;quot;nrf_drv_clock.h&amp;quot;
#endif
#include &amp;quot;fds.h&amp;quot;
#include &amp;quot;app_timer.h&amp;quot;
#include &amp;quot;app_error.h&amp;quot;
#include &amp;quot;nrf_cli.h&amp;quot;
#include &amp;quot;fds_example.h&amp;quot;

#define NRF_LOG_MODULE_NAME app
#include &amp;quot;nrf_log.h&amp;quot;
#include &amp;quot;nrf_log_ctrl.h&amp;quot;


/* A tag identifying the SoftDevice BLE configuration. */
#define APP_BLE_CONN_CFG_TAG    1


/* Array to map FDS return values to strings. */
char const * fds_err_str[] =
{
    &amp;quot;FDS_SUCCESS&amp;quot;,
    &amp;quot;FDS_ERR_OPERATION_TIMEOUT&amp;quot;,
    &amp;quot;FDS_ERR_NOT_INITIALIZED&amp;quot;,
    &amp;quot;FDS_ERR_UNALIGNED_ADDR&amp;quot;,
    &amp;quot;FDS_ERR_INVALID_ARG&amp;quot;,
    &amp;quot;FDS_ERR_NULL_ARG&amp;quot;,
    &amp;quot;FDS_ERR_NO_OPEN_RECORDS&amp;quot;,
    &amp;quot;FDS_ERR_NO_SPACE_IN_FLASH&amp;quot;,
    &amp;quot;FDS_ERR_NO_SPACE_IN_QUEUES&amp;quot;,
    &amp;quot;FDS_ERR_RECORD_TOO_LARGE&amp;quot;,
    &amp;quot;FDS_ERR_NOT_FOUND&amp;quot;,
    &amp;quot;FDS_ERR_NO_PAGES&amp;quot;,
    &amp;quot;FDS_ERR_USER_LIMIT_REACHED&amp;quot;,
    &amp;quot;FDS_ERR_CRC_CHECK_FAILED&amp;quot;,
    &amp;quot;FDS_ERR_BUSY&amp;quot;,
    &amp;quot;FDS_ERR_INTERNAL&amp;quot;,
};

/* Array to map FDS events to strings. */
static char const * fds_evt_str[] =
{
    &amp;quot;FDS_EVT_INIT&amp;quot;,
    &amp;quot;FDS_EVT_WRITE&amp;quot;,
    &amp;quot;FDS_EVT_UPDATE&amp;quot;,
    &amp;quot;FDS_EVT_DEL_RECORD&amp;quot;,
    &amp;quot;FDS_EVT_DEL_FILE&amp;quot;,
    &amp;quot;FDS_EVT_GC&amp;quot;,
};

/* Dummy configuration data. */
static configuration_t m_dummy_cfg =
{
    .config1_on  = false,
    .config2_on  = true,
    .boot_count  = 0x0,
    .device_name = &amp;quot;dummy&amp;quot;,
};

/* A record containing dummy configuration data. */
static fds_record_t const m_dummy_record =
{
    .file_id           = CONFIG_FILE,
    .key               = CONFIG_REC_KEY,
    .data.p_data       = &amp;amp;m_dummy_cfg,
    /* The length of a record is always expressed in 4-byte units (words). */
    .data.length_words = (sizeof(m_dummy_cfg) + 3) / sizeof(uint32_t),
};

/* Keep track of the progress of a delete_all operation. */
static struct
{
    bool delete_next;   //!&amp;lt; Delete next record.
    bool pending;       //!&amp;lt; Waiting for an fds FDS_EVT_DEL_RECORD event, to delete the next record.
} m_delete_all;

/* Flag to check fds initialization. */
static bool volatile m_fds_initialized;


static void fds_evt_handler(fds_evt_t const * p_evt)
{
    NRF_LOG_GREEN(&amp;quot;Event: %s received (%s)&amp;quot;,
                  fds_evt_str[p_evt-&amp;gt;id],
                  fds_err_str[p_evt-&amp;gt;result]);

    switch (p_evt-&amp;gt;id)
    {
        case FDS_EVT_INIT:
            if (p_evt-&amp;gt;result == FDS_SUCCESS)
            {
                m_fds_initialized = true;
            }
            break;

        case FDS_EVT_WRITE:
        {
            if (p_evt-&amp;gt;result == FDS_SUCCESS)
            {
                NRF_LOG_INFO(&amp;quot;Record ID:\t0x%04x&amp;quot;,  p_evt-&amp;gt;write.record_id);
                NRF_LOG_INFO(&amp;quot;File ID:\t0x%04x&amp;quot;,    p_evt-&amp;gt;write.file_id);
                NRF_LOG_INFO(&amp;quot;Record key:\t0x%04x&amp;quot;, p_evt-&amp;gt;write.record_key);
            }
        } break;

        case FDS_EVT_DEL_RECORD:
        {
            if (p_evt-&amp;gt;result == FDS_SUCCESS)
            {
                NRF_LOG_INFO(&amp;quot;Record ID:\t0x%04x&amp;quot;,  p_evt-&amp;gt;del.record_id);
                NRF_LOG_INFO(&amp;quot;File ID:\t0x%04x&amp;quot;,    p_evt-&amp;gt;del.file_id);
                NRF_LOG_INFO(&amp;quot;Record key:\t0x%04x&amp;quot;, p_evt-&amp;gt;del.record_key);
            }
            m_delete_all.pending = false;
        } break;

        default:
            break;
    }
}


/**@brief   Begin deleting all records, one by one. */
void delete_all_begin(void)
{
    m_delete_all.delete_next = true;
}


/**@brief   Process a delete all command.
 *
 * Delete records, one by one, until no records are left.
 */
void delete_all_process(void)
{
    if (   m_delete_all.delete_next
        &amp;amp; !m_delete_all.pending)
    {
        NRF_LOG_INFO(&amp;quot;Deleting next record.&amp;quot;);

        m_delete_all.delete_next = record_delete_next();
        if (!m_delete_all.delete_next)
        {
            NRF_LOG_CYAN(&amp;quot;No records left to delete.&amp;quot;);
        }
    }
}


#ifdef SOFTDEVICE_PRESENT
/**@brief   Function for initializing the SoftDevice and enabling the BLE stack. */
static void ble_stack_init(void)
{
    ret_code_t rc;
    uint32_t   ram_start;

    /* Enable the SoftDevice. */
    rc = nrf_sdh_enable_request();
    APP_ERROR_CHECK(rc);

    rc = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &amp;amp;ram_start);
    APP_ERROR_CHECK(rc);

    rc = nrf_sdh_ble_enable(&amp;amp;ram_start);
    APP_ERROR_CHECK(rc);
}
#else
static void clock_init(void)
{
    /* Initialize the clock. */
    ret_code_t rc = nrf_drv_clock_init();
    APP_ERROR_CHECK(rc);

    nrf_drv_clock_lfclk_request(NULL);

    /* Wait for the clock to be ready. */
    while (!nrf_clock_lf_is_running()) {;}
}
#endif


/**@brief   Initialize the timer. */
static void timer_init(void)
{
    ret_code_t err_code = app_timer_init();
    APP_ERROR_CHECK(err_code);
}


/**@brief   Initialize logging. */
static void log_init(void)
{
    ret_code_t rc = NRF_LOG_INIT(NULL);
    APP_ERROR_CHECK(rc);
}


/**@brief   Sleep until an event is received. */
static void power_manage(void)
{
#ifdef SOFTDEVICE_PRESENT
    (void) sd_app_evt_wait();
#else
    __WFE();
#endif
}


/**@brief   Wait for fds to initialize. */
static void wait_for_fds_ready(void)
{
    while (!m_fds_initialized)
    {
        power_manage();
    }
}


typedef struct
{
    uint8_t enable;
    uint8_t time_adv;
}ADVERTISE;

typedef struct
{
    uint8_t state;
    ADVERTISE adv2;
}DEVICE;

static DEVICE config2write;

void flash_write_record_setup(DEVICE * config2write)
{
    ret_code_t rc;
    fds_record_desc_t desc = {0};
    fds_find_token_t  tok  = {0};

    rc = fds_record_find(CONFIG_FILE, CONFIG_REC_KEY, &amp;amp;desc, &amp;amp;tok);

    if (rc == FDS_SUCCESS)
    {
        config2write-&amp;gt;state = 2;

        /* A config file is in flash. Let&amp;#39;s update it. */
        fds_flash_record_t config = {0};

        /* Open the record and read its contents. */
        rc = fds_record_open(&amp;amp;desc, &amp;amp;config);
        APP_ERROR_CHECK(rc);

        DEVICE * tmp = (DEVICE*)config.p_data;
        NRF_LOG_INFO(&amp;quot;Current file content: %d .&amp;quot;, tmp-&amp;gt;state);

        //compare existing config in flash with new one
        uint8_t ret = memcmp(config2write, config.p_data, sizeof(DEVICE));

        /* Close the record when done reading. */
        rc = fds_record_close(&amp;amp;desc);
        APP_ERROR_CHECK(rc);
 
        NRF_LOG_INFO(&amp;quot;NEW  : %d&amp;quot;, ((uint32_t*)config2write)[0]);
        NRF_LOG_INFO(&amp;quot;FLASH: %d&amp;quot;, ((uint32_t*)config.p_data)[0]);

        if(ret != 0) //if different update config in flash
        {
            NRF_LOG_INFO(&amp;quot;Updating state value to 2&amp;quot;);
            config2write-&amp;gt;state = 2;
            /* Write the updated record to flash. */
            fds_record_t const rec =
            {
                .file_id           = CONFIG_FILE,
                .key               = CONFIG_REC_KEY,
                .data.p_data       = config2write,
                .data.length_words = (sizeof(DEVICE) + 3) / sizeof(uint32_t)
            };

            rc = fds_record_update(&amp;amp;desc, &amp;amp;rec);
            APP_ERROR_CHECK(rc);
        }
        else
        {
            /* Same config data as previous, nothing to be done */
        }
    }
    else
    {
        /* System config not found; write a new one. */
        NRF_LOG_INFO(&amp;quot;Writing config file...&amp;quot;);

        fds_record_t const rec =
        {
            .file_id           = CONFIG_FILE,
            .key               = CONFIG_REC_KEY,
            .data.p_data       = config2write,
            .data.length_words = (sizeof(DEVICE) + 3) / sizeof(uint32_t)
        };

  
        DEVICE * tmp = (DEVICE*)rec.data.p_data;
        NRF_LOG_INFO(&amp;quot;Wrinting state value for the first time with value: %d .&amp;quot;, tmp-&amp;gt;state);

        rc = fds_record_write(&amp;amp;desc, &amp;amp;rec);
        APP_ERROR_CHECK(rc);
    }
}


int main(void)
{
    ret_code_t rc;

#ifdef SOFTDEVICE_PRESENT
    ble_stack_init();
#else
    clock_init();
#endif

    timer_init();
    log_init();
    cli_init();

    NRF_LOG_INFO(&amp;quot;FDS example started.&amp;quot;)

    /* Register first to receive an event when initialization is complete. */
    (void) fds_register(fds_evt_handler);

    NRF_LOG_INFO(&amp;quot;Initializing fds...&amp;quot;);

    rc = fds_init();
    APP_ERROR_CHECK(rc);

    /* Wait for fds to initialize. */
    wait_for_fds_ready();

    NRF_LOG_INFO(&amp;quot;Available commands:&amp;quot;);
    NRF_LOG_INFO(&amp;quot;- print all\t\tprint records&amp;quot;);
    NRF_LOG_INFO(&amp;quot;- print config\tprint configuration&amp;quot;);
    NRF_LOG_INFO(&amp;quot;- update\t\tupdate configuration&amp;quot;);
    NRF_LOG_INFO(&amp;quot;- stat\t\tshow statistics&amp;quot;);
    NRF_LOG_INFO(&amp;quot;- write\t\twrite a new record&amp;quot;);
    NRF_LOG_INFO(&amp;quot;- delete\t\tdelete a record&amp;quot;);
    NRF_LOG_INFO(&amp;quot;- delete_all\tdelete all records&amp;quot;);
    NRF_LOG_INFO(&amp;quot;- gc\t\trun garbage collection&amp;quot;);

    NRF_LOG_INFO(&amp;quot;Reading flash usage statistics...&amp;quot;);

    fds_stat_t stat = {0};

    rc = fds_stat(&amp;amp;stat);
    APP_ERROR_CHECK(rc);

    NRF_LOG_INFO(&amp;quot;Found %d valid records.&amp;quot;, stat.valid_records);
    NRF_LOG_INFO(&amp;quot;Found %d dirty records (ready to be garbage collected).&amp;quot;, stat.dirty_records);

    config2write.state = 1;
    config2write.adv2.enable = 12;
    config2write.adv2.time_adv = 13;


    flash_write_record_setup(&amp;amp;config2write);

    cli_start();

    /* Enter main loop. */
    for (;;)
    {
        if (!NRF_LOG_PROCESS())
        {
            power_manage();
        }
        cli_process();
        delete_all_process();
    }
}


/**
 * @}
 */
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Basically, it seems like the code you sent me is working.&lt;/p&gt;
&lt;p&gt;My guess is that your &amp;quot;config2write&amp;quot; variable is in the wrong scope. You have to keep this variable unchanged until you get the callback from FDS. I just use a static variable in my implementation above. If this does not resolve the issue for you, please compare my code above to your own and if you still can&amp;#39;t find a cause feel free to send me a reply below, preferably with your full source code as a zip.&lt;/p&gt;
&lt;p&gt;A final note: Since this is during initialization it&amp;#39;s most likely not going to be a problem, but you open, close and read the record. You have to change the order so that you open, read and close it instead.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;br /&gt;Rune Holmgren&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>