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

Problem writing/reading from flash

Hi,

I'm having problem with flash storage. First it's flashed with the writing code uncommented. Then I reflash it with the writing code commented like in the snippet posted below.

But when it's reset or disconnected from power. The flash is erased.

What i'm I doing wrong?

#include <stdbool.h>
#include <stdio.h>
#include "nrf.h"
#include "bsp.h"
#include "app_error.h"
#include "nordic_common.h"
#define NRF_LOG_MODULE_NAME "APP"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_delay.h"

static void flash_page_erase(uint32_t * page_address)
{
    // Turn on flash erase enable and wait until the NVMC is ready:
    NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Een << NVMC_CONFIG_WEN_Pos);

    while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
    {
        // Do nothing.
    }

    // Erase page:
    NRF_NVMC->ERASEPAGE = (uint32_t)page_address;

    while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
    {
        // Do nothing.
    }

    // Turn off flash erase enable and wait until the NVMC is ready:
    NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos);

    while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
    {
        // Do nothing.
    }
}

static void flash_word_write(uint32_t * address, uint32_t value)
{
    // Turn on flash write enable and wait until the NVMC is ready:
    NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos);

    while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
    {
        // Do nothing.
    }

    *address = value;

    while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
    {
        // Do nothing.
    }

    // Turn off flash write enable and wait until the NVMC is ready:
    NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos);

    while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
    {
        // Do nothing.
    }
}

int main(void)
{
    uint32_t err_code;

    err_code = NRF_LOG_INIT(NULL);
    APP_ERROR_CHECK(err_code);

    NRF_LOG_INFO("Flashwrite example\r\n");

    while (true)
    {
				uint32_t address = 0x1000108C;
				uint32_t valueReturn = 0; 
				
				//uint32_t value = 0x12345678;
				//flash_page_erase(&address);
				//flash_word_write(&address, value);

        while (true)
        {
						nrf_delay_ms(1000);
            
						memcpy(&valueReturn, &address, 1024);
					
						NRF_LOG_INFO("'%x' was read from flash\r\n\r\n", valueReturn);
            NRF_LOG_FLUSH();
        }
    }
}

Thanks!

Parents
  • You are not using pointers correctly. The address has to be defined and used in a different way

    int main(void)
    {
        uint32_t err_code;
    
        err_code = NRF_LOG_INIT(NULL);
        APP_ERROR_CHECK(err_code);
    
        NRF_LOG_INFO("Flashwrite example\r\n");
    
        while (true)
        {
    				uint32_t *address = (uint32_t *)0x1000108C;
    				uint32_t valueReturn = 0; 
    				
    				uint32_t value = 0x12345678;
    				flash_page_erase(address);
    				flash_word_write(address, value);
    
            while (true)
            {
    						nrf_delay_ms(1000);
                
    						memcpy(&valueReturn, address, 1024);
    					
    						NRF_LOG_INFO("'%x' was read from flash\r\n\r\n", valueReturn);
                NRF_LOG_FLUSH();
            }
        }
    }
    

Reply
  • You are not using pointers correctly. The address has to be defined and used in a different way

    int main(void)
    {
        uint32_t err_code;
    
        err_code = NRF_LOG_INIT(NULL);
        APP_ERROR_CHECK(err_code);
    
        NRF_LOG_INFO("Flashwrite example\r\n");
    
        while (true)
        {
    				uint32_t *address = (uint32_t *)0x1000108C;
    				uint32_t valueReturn = 0; 
    				
    				uint32_t value = 0x12345678;
    				flash_page_erase(address);
    				flash_word_write(address, value);
    
            while (true)
            {
    						nrf_delay_ms(1000);
                
    						memcpy(&valueReturn, address, 1024);
    					
    						NRF_LOG_INFO("'%x' was read from flash\r\n\r\n", valueReturn);
                NRF_LOG_FLUSH();
            }
        }
    }
    

Children
No Data
Related