This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Flash Memory Issue

Hi,
I have written these 3 functions to erase/read and write.

/////////////////////////////////////////////////////////////////////////////// SNV

const uint32_t f_addr = 0x000FF000; // Last page start address 255

// Create a pointer to that address
uint32_t * p_addr = (uint32_t *)f_addr; //cast f_addr to a pointer

uint32_t erase_SNV()
{
    NRF_LOG_INFO("Memory Erased");
    return sd_flash_page_erase(255);
}


uint32_t write_SNV(int address,uint8_t value)
{
    uint32_t val;
    val=value;
    NRF_LOG_INFO("Data Write in Flash");
    ret_code_t err_code;
    err_code = sd_flash_write(p_addr+address, &val, sizeof(val));
    APP_ERROR_CHECK(err_code);
    return err_code;
}

uint32_t read_SNV(int address)
{
     NRF_LOG_INFO("The Data read from flash is: %d", *(p_addr)+address);
     return *(p_addr)+address;
}


When I write any value from address 1 it gives following wrong output(0 1 2 3).
    write_SNV(1,11);
    read_SNV(1);
    write_SNV(2,12);
    read_SNV(2);
    write_SNV(3,13);
    read_SNV(3);

  //  read_SNV(0);
    read_SNV(1);
    read_SNV(2);
    read_SNV(3);
 



But When I start address from 0 it gives the right values.

    write_SNV(0,10);
    read_SNV(0);
    write_SNV(1,11);
    read_SNV(1);
    write_SNV(2,12);
    read_SNV(2);
    write_SNV(3,13);
    read_SNV(3);

    read_SNV(0);
    read_SNV(1);
    read_SNV(2);
    read_SNV(3);




How can I save values in rondom addresses?
Thanks!

  • Hi ,
    I have tested what you have said. It working. 
    Now I am trying to add this part to my project but getting the following Issue. Can you please check?

  • so some function returns an error that has the name NRF_ERROR_INVALID_LENGTH. Did you check what function that returned this value? Perhaps you can check the function description in the .h file that declares this function. Does that give any hints on why it may return that error?

    BR,

    Edvin

  • o some function returns an error that has the name NRF_ERROR_INVALID_LENGTH. Did you check what function that returned this value?

    Yes, I have checked and this is function returning an error.

        rc = nrf_fstorage_write(&fstorage, 0x3f000, SNV_Data, sizeof(SNV_Data), NULL);
        APP_ERROR_CHECK(rc);
    


    ret_code_t nrf_fstorage_write(nrf_fstorage_t const * p_fs,
                                  uint32_t               dest,
                                  void           const * p_src,
                                  uint32_t               len,
                                  void                 * p_context)
    {
        NRF_FSTORAGE_PARAM_CHECK(p_fs,        NRF_ERROR_NULL);
        NRF_FSTORAGE_PARAM_CHECK(p_src,       NRF_ERROR_NULL);
        NRF_FSTORAGE_PARAM_CHECK(p_fs->p_api, NRF_ERROR_INVALID_STATE);
        NRF_FSTORAGE_PARAM_CHECK(len,         NRF_ERROR_INVALID_LENGTH);
    
        /* Length must be a multiple of the program unit. */
        NRF_FSTORAGE_PARAM_CHECK(!(len % p_fs->p_flash_info->program_unit), NRF_ERROR_INVALID_LENGTH);
    
        /* Source and destination addresses must be word-aligned. */
        NRF_FSTORAGE_PARAM_CHECK(addr_is_aligned32(dest),                NRF_ERROR_INVALID_ADDR);
        NRF_FSTORAGE_PARAM_CHECK(addr_is_aligned32((uint32_t)p_src),     NRF_ERROR_INVALID_ADDR);
        NRF_FSTORAGE_PARAM_CHECK(addr_is_within_bounds(p_fs, dest, len), NRF_ERROR_INVALID_ADDR);
    
        return (p_fs->p_api)->write(p_fs, dest, p_src, len, p_context);
    }
    

    This is working perfectly fine in the example but not working here. 

  • Edvin said:
    check the function description in the .h file

     that snippet is not from the .h file. That is from the .c file. Check the file nrf_fstorage.h

  • Hi,
    I have checked in .h file.

    ret_code_t nrf_fstorage_write(nrf_fstorage_t const * p_fs,
                                  uint32_t               dest,
                                  void           const * p_src,
                                  uint32_t               len,
                                  void                 * p_param);
    
    
    /**@brief   Function for erasing flash pages.
     *
     * @details This function erases @p len pages starting from the page at address @p page_addr.
     *          The erase operation must be initiated on a page boundary.
     *
     * @param[in]   p_fs        The fstorage instance.
     * @param[in]   page_addr   Address of the page to erase.
     * @param[in]   len         Number of pages to erase.
     * @param[in]   p_param     User-defined parameter passed to the event handler (may be NULL).
     *
     * @retval  NRF_SUCCESS                 If the operation was accepted.
     * @retval  NRF_ERROR_NULL              If @p p_fs is NULL.
     * @retval  NRF_ERROR_INVALID_STATE     If the module is not initialized.
     * @retval  NRF_ERROR_INVALID_LENGTH    If @p len is zero.
     * @retval  NRF_ERROR_INVALID_ADDR      If the address @p page_addr is outside the flash memory
     *                                      boundaries specified in @p p_fs, or if it is unaligned.
     * @retval  NRF_ERROR_NO_MEM            If no memory is available to accept the operation.
     *                                      When using the @ref nrf_fstorage_sd, this error
     *                                      indicates that the internal queue of operations is full.
     */
    

    I am sending hard code value but still getting same issue.

        
        char      SNV_Data[]   ="Hello";
        
        NRF_LOG_INFO("Writing \"%s\" to flash.", SNV_Data);
        rc = nrf_fstorage_write(&fstorage, 0x3f000, SNV_Data, 5, NULL);
        APP_ERROR_CHECK(rc);
    
     

Related