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

Flash initialization in custom section

Hi,

I am developing on a nRF52832 with S132 softdevice using Segger and SDK15.2.0. I am doing some tests with the Flash memory, and my goal is to have some const variables that point to a specific flash section, so they can be changed afterwards using nrfjprog.

I created a section called .cfg_data, by adding the corresponding line in the flash_placement.xml file:

<ProgramSection load="no" name=".cfg_data" start="$(CONFIG_FLASH_START)" size="$(CONFIG_FLASH_SIZE)" />

Also, I configured the Placement Macros in the Linker section of the project configuration, like this (just modified the flash_size and added CONFIG_FLASH):

FLASH_PH_START=0x0

FLASH_PH_SIZE=0x80000

RAM_PH_START=0x20000000

RAM_PH_SIZE=0x10000

FLASH_START=0x26000

FLASH_SIZE=0x59000

CONFIG_FLASH_START=0x7f000

CONFIG_FLASH_SIZE=0x1000

RAM_START=0x200022b8

RAM_SIZE=0xdd48

And finally, I declared a test variable in main.c to test if it worked:

static const uint32_t test_var __attribute__((section(".cfg_data"))) = 0x55;

Then, to check if it has been initialized, I check with nrfjprog:

nrfjprog --memrd 0x7F000 --n 1024

But I see the whole page uninitialized.

What am I missing?

Thanks in advance!

  • Hi Imanol.

    You cannot use attributes to write in flash locations, you can only write in RAM locations using attributes.

    But you can write to the flash in nrfjprog using for example:  nrfjprog.exe --memwr 0x7f000 --val 0x55

    You could take a look at the NVMC API as well.

    - Andreas

  • Thanks for your answer Andreas. I already have tried the nrfjprog. My goal was to have the flash initialized when programming the hex file, not having to do an additional step with the nrfjprog.

  • Hi Imanol.

    You can use the sd_flash_write() API.

    uint32_t sd_flash_write 	( 	uint32_t *  	p_dst,
    		uint32_t const *  	p_src,
    		uint32_t  	size 
    	) 		
    
    Flash Write.
    
    Commands to write a buffer to flash
    
    If the SoftDevice is enabled: This call initiates the flash access command, and its completion will be communicated to the application with exactly one of the following events:
    
        NRF_EVT_FLASH_OPERATION_SUCCESS - The command was successfully completed.
        NRF_EVT_FLASH_OPERATION_ERROR - The command could not be started.
    
    If the SoftDevice is not enabled no event will be generated, and this call will return NRF_SUCCESS when the write has been completed
    
    Note
    
            This call takes control over the radio and the CPU during flash erase and write to make sure that they will not interfere with the flash access. This means that all interrupts will be blocked for a predictable time (depending on the NVMC specification in the device's Product Specification and the command parameters).
            The data in the p_src buffer should not be modified before the NRF_EVT_FLASH_OPERATION_SUCCESS or the NRF_EVT_FLASH_OPERATION_ERROR have been received if the SoftDevice is enabled.
            This call will make the SoftDevice trigger a hardfault when the page is written, if it is protected.
    
    Parameters
        [in]	p_dst	Pointer to start of flash location to be written.
        [in]	p_src	Pointer to buffer with data to be written.
        [in]	size	Number of 32-bit words to write. Maximum size is the number of words in one flash page. See the device's Product Specification for details.
    
    Return values
        NRF_ERROR_INVALID_ADDR	Tried to write to a non existing flash address, or p_dst or p_src was unaligned.
        NRF_ERROR_BUSY	The previous command has not yet completed.
        NRF_ERROR_INVALID_LENGTH	Size was 0, or higher than the maximum allowed size.
        NRF_ERROR_FORBIDDEN	Tried to write to an address outside the application flash area.
        NRF_SUCCESS	The command was accepted. 

    You could also take a look at the examples\peripheral\flash_fstorage example, which uses the nrf_fstorage_write() API.

    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 
    	) 		
    
    Function for writing data to flash.
    
    Write len bytes from p_src to dest.
    
    When using SoftDevice implementation, the data is written by several calls to sd_flash_write if the length of the data exceeds NRF_FSTORAGE_SD_MAX_WRITE_SIZE bytes. Only one event is sent upon completion.
    
    Note
        The data to be written to flash must be kept in memory until the operation has terminated and an event is received.
    
    Parameters
        [in]	p_fs	The fstorage instance.
        [in]	dest	Address in flash memory where to write the data.
        [in]	p_src	Data to be written.
        [in]	len	Length of the data (in bytes).
        [in]	p_param	User-defined parameter passed to the event handler (may be NULL).
    
    Return values
        NRF_SUCCESS	If the operation was accepted.
        NRF_ERROR_NULL	If p_fs or p_src is NULL.
        NRF_ERROR_INVALID_STATE	If the module is not initialized.
        NRF_ERROR_INVALID_LENGTH	If len is zero or not a multiple of the program unit, or if it is otherwise invalid.
        NRF_ERROR_INVALID_ADDR	If the address dest is outside the flash memory boundaries specified in p_fs, or if it is unaligned.
        NRF_ERROR_NO_MEM	If no memory is available to accept the operation. When using the SoftDevice implementation, this error indicates that the internal queue of operations is full. 

    Hope this helps.

    - Andreas

  • Hi Andreas,

    This is in fact what I was already using. With this, the best I see I can do is just check if the flash page has been already been used, and if not, write some default data in it. But with this solution I get "double" data in flash, the default one (initializing the variables in the code) and the one that the user updates the specific flash section with.

    So still, not solving my problem of initializing flash from the beginning.

    However, I see I will have to stick to that for the moment...

    Thanks anyway!

Related