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

NRF_FSTORAGE_DEF issue with nrf52832

Hello,

I have some trouble to write/read data in flash region,

I am using FICR region to save data, if trying the command : nrfjprog --memrd 0x10000010 --n 0x10 I can see : 0x10000010: 00001000 00000080 FFFFFFFE FFFFFFFF |................|

So I setup NRF_FSTORAGE_DEF function as follows,

NRF_FSTORAGE_DEF(nrf_fstorage_t fstorage) = { /* Set a handler for fstorage events. */ .evt_handler = fstorage_evt_handler,

/* These below are the boundaries of the flash space assigned to this instance of fstorage.
 * You must set these manually, even at runtime, before nrf_fstorage_init() is called.
 * The function nrf5_flash_end_addr_get() can be used to retrieve the last address on the
 * last page of flash available to write data. */
**.start_addr = 0x0007F000,
.end_addr   = 0x0007FFFF,**

};

after that, if I add more service with battery or device information or Custom service, the code can't do well not normal..... I can't write any data, and can read the previous data at that time,

please help to let me know the method to setup the memory region, how to do ?

thankful for your support in advance,

thanks. I attach the code for your understanding, please refer it,

#include <stdbool.h>
#include <stdio.h>
#include "nrf.h"
#include "bsp.h"
#include "app_error.h"
#include "nrf_nvmc.h"
#include "nordic_common.h"

#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"

#include "app_timer.h"
#include "nrf_drv_clock.h"

#include "nrf_fstorage_sd.h"//love_1211

#define FLASHWRITE_EXAMPLE_MAX_STRING_LEN       (62u)
#define FLASHWRITE_EXAMPLE_BLOCK_VALID          (0xA55A5AA5)
#define FLASHWRITE_EXAMPLE_BLOCK_INVALID        (0xA55A0000)
#define FLASHWRITE_EXAMPLE_BLOCK_NOT_INIT       (0xFFFFFFFF)

//love_1211
//refer to infocenter.nordicsemi.com/index.jsp

static void fstorage_evt_handler(nrf_fstorage_evt_t * p_evt);

NRF_FSTORAGE_DEF(nrf_fstorage_t fstorage) =
{
    /* Set a handler for fstorage events. */
    .evt_handler = fstorage_evt_handler,

    /* These below are the boundaries of the flash space assigned to this instance of fstorage.
     * You must set these manually, even at runtime, before nrf_fstorage_init() is called.
     * The function nrf5_flash_end_addr_get() can be used to retrieve the last address on the
     * last page of flash available to write data. */
    .start_addr = 0x0007F000,
    .end_addr   = 0x0007FFFF,
};

/* Dummy data to write to flash. */
static uint32_t m_data          = 0xBADC0FFE;
//static char     m_hello_world1[] = "helloworld4";
//static char     m_hello_world2[] = "helloworld5";
//static char     m_hello_world3[] = "helloworld6";
static char     m_hello_world1[] = "helloworld7";
static char     m_hello_world2[] = "helloworld8";
static char     m_hello_world3[] = "helloworld9";

static void fstorage_evt_handler(nrf_fstorage_evt_t * p_evt)
{
    if (p_evt->result != NRF_SUCCESS)
    {
//        printf("--> Event received: ERROR while executing an fstorage operation. \r\n");
        return;
    }

    switch (p_evt->id)
    {
        case NRF_FSTORAGE_EVT_WRITE_RESULT:
        {
//            printf("--> Event received: wrote %d bytes at address 0x%x. \r\n", p_evt->len, p_evt->addr);
        } break;

        case NRF_FSTORAGE_EVT_ERASE_RESULT:
        {
//            printf("--> Event received: erased %d page from address 0x%x. \r\n", p_evt->len, p_evt->addr);
        } break;

        default:
            break;
    }
}

/**@brief   Helper function to obtain the last address on the last page of the on-chip flash that
 *          can be used to write user data.
 */
//static uint32_t nrf5_flash_end_addr_get()
//{
//    uint32_t const bootloader_addr = NRF_UICR->NRFFW[0];
//    uint32_t const page_sz         = NRF_FICR->CODEPAGESIZE;
//    uint32_t const code_sz         = NRF_FICR->CODESIZE;
//
//    return (bootloader_addr != 0xFFFFFFFF ?
//            bootloader_addr : (code_sz * page_sz));
//}

//static void print_flash_info(nrf_fstorage_t * p_fstorage)
//{
////    printf("========| flash info |======== \r\n");
////    printf("erase unit: \t%d bytes \r\n",      p_fstorage->p_flash_info->erase_unit);
////    printf("program unit: \t%d bytes \r\n",    p_fstorage->p_flash_info->program_unit);
////    printf("============================== \r\n");
//}

/**@brief   Sleep until an event is received. */
//static void power_manage(void)
//{
//    (void) sd_app_evt_wait();
//}

void wait_for_flash_ready(nrf_fstorage_t const * p_fstorage)
{
    /* While fstorage is busy, sleep and wait for an event. */
    while (nrf_fstorage_is_busy(p_fstorage))
    {
//        power_manage();
    }
}

void flash_test(void)
{//nrfjprog --memrd 0x0007F000 --n 0x70  nrfjprog --memrd 0x10000010 --n 0x10
	ret_code_t rc;
	nrf_fstorage_api_t * p_fs_api;
	
//	printf("SoftDevice is present. \r\n");
//	printf("Initializing nrf_fstorage_sd implementation... \r\n");
	/* Initialize an fstorage instance using the nrf_fstorage_sd backend.
	* nrf_fstorage_sd uses the SoftDevice to write to flash. This implementation can safely be
	* used whenever there is a SoftDevice, regardless of its status (enabled/disabled). */
	p_fs_api = &nrf_fstorage_sd;
	
	rc = nrf_fstorage_init(&fstorage, p_fs_api, NULL);
	APP_ERROR_CHECK(rc);
	
//	print_flash_info(&fstorage);
//	
//	/* It is possible to set the start and end addresses of an fstorage instance at runtime.
//	* They can be set multiple times, should it be needed. The helper function below can
//	* be used to determine the last address on the last page of flash memory available to
//	* store data. */
//	(void) nrf5_flash_end_addr_get();
	
	rc = nrf_fstorage_erase(&fstorage, 0x0007F000, 1, NULL);
	
	/* Let's write to flash. */
//	printf("Writing \"%x\" to flash. \r\n", m_data);
//	m_data = 0x12345678;//love_1211	

	rc = nrf_fstorage_erase(&fstorage, 0x0007F500, 1, NULL);
	
	rc = nrf_fstorage_write(&fstorage, 0x0007F500, m_hello_world1, sizeof(m_hello_world1), NULL);
	APP_ERROR_CHECK(rc);
	
	wait_for_flash_ready(&fstorage);

	m_data = 0x98765432;//0xDEADBE12;
		
	rc = nrf_fstorage_erase(&fstorage, 0x0007F600, 1, NULL);	
//	printf("Writing \"%x\" to flash. \r\n", m_data);
	rc = nrf_fstorage_write(&fstorage, 0x0007F600, &m_data, sizeof(m_data), NULL);
	APP_ERROR_CHECK(rc);
		
//	m_data = 0xDEADBEEF;
	
	rc = nrf_fstorage_erase(&fstorage, 0x0007F700, 1, NULL);	
//	printf("Writing \"%x\" to flash. \r\n", m_data);
	rc = nrf_fstorage_write(&fstorage, 0x0007F700, m_hello_world2, sizeof(m_hello_world2), NULL);
	APP_ERROR_CHECK(rc);
	
	wait_for_flash_ready(&fstorage);
	
	rc = nrf_fstorage_erase(&fstorage, 0x0007F800, 1, NULL);	
//	printf("Writing \"%s\" to flash. \r\n", m_hello_world);
	rc = nrf_fstorage_write(&fstorage, 0x0007F800, m_hello_world3, sizeof(m_hello_world3), NULL);
	APP_ERROR_CHECK(rc);
	
	wait_for_flash_ready(&fstorage);
	
	uint8_t    flash_data1[256] = {0};
	rc = nrf_fstorage_read(&fstorage, 0x0007F500, flash_data1, sizeof(flash_data1));
	APP_ERROR_CHECK(rc);
	
        printf("%s \r\n", flash_data1);

	uint8_t    flash_data23[4] = {0};
	rc = nrf_fstorage_read(&fstorage, 0x0007F600, flash_data23, sizeof(flash_data23));
	APP_ERROR_CHECK(rc);
	
	for(int i = 0; i < 4; i++)
        	printf("%x \r\n", flash_data23[i]);
                
	uint8_t    flash_data2[256] = {0};
	rc = nrf_fstorage_read(&fstorage, 0x0007F700, flash_data2, sizeof(flash_data2));
	APP_ERROR_CHECK(rc);
	
        printf("%s \r\n", flash_data2);
        
	uint8_t    flash_data3[256] = {0};
	rc = nrf_fstorage_read(&fstorage, 0x0007F800, flash_data3, sizeof(flash_data3));
	APP_ERROR_CHECK(rc);
	
        printf("%s \r\n", flash_data3);                
}
Parents Reply
  • Hi Edmond,

    • The flashwrite example show case direct access to flash using NVMC peripheral. This won't work if you have the softdevice running.

    • fstorage is the low level library showing how to access flash via fstorage API. This works with and without softdevice.

    • fds: Is high level library where data can be organized as a file, and has feature for queuing operation, wear leveling, garbage collection etc.

    If you just want to store just a few bytes of data, you can think of using fstorage. If you need higher level or file system, you use fds. Please read the documentation on infocenter.

Children
No Data
Related