I want to store some data into flash. Whether there are relevant application notes or program code? Thanks
I want to store some data into flash. Whether there are relevant application notes or program code? Thanks
Do you use softdevice or not ? If you use softdevice please have a look at this fds usage here.
If you don't use softdevice, please have a look at the SDK example \examples\peripheral\flashwrite
Do you use softdevice or not ? If you use softdevice please have a look at this fds usage here.
If you don't use softdevice, please have a look at the SDK example \examples\peripheral\flashwrite
use softdevice, this test code:link text
static uint8_t fs_callback_flag;
static void fs_evt_handler(fs_evt_t const * const evt, fs_ret_t result)
{
if (result != FS_SUCCESS)
{
bsp_indication_set(BSP_INDICATE_FATAL_ERROR);
}
else
{
NRF_LOG_INFO("fstorage command successfully completed\n\r");
fs_callback_flag = 0;
}
}
static void power_manage(void)
{
uint32_t err_code = sd_app_evt_wait();
APP_ERROR_CHECK(err_code);
}
void fstorage_test(void)
{
static char char_data[5];
static char special_char_data[5];
static uint32_t data;
uint32_t flash_data[4];
FS_REGISTER_CFG(fs_config_t fs_config) =
{
.callback = fs_evt_handler,
.num_pages = NUM_PAGES_SN,
.priority = 0xFD
};
fs_ret_t ret = fs_init();
if (ret != FS_SUCCESS)
{
bsp_indication_set(BSP_INDICATE_FATAL_ERROR);
}
// Erase one page (page 0).
NRF_LOG_INFO("Erasing a flash page at address 0x%X\r\n", (uint32_t)fs_config.p_start_addr);
fs_callback_flag = 1;
ret = fs_erase(&fs_config, fs_config.p_start_addr, 1, NULL);
if (ret != FS_SUCCESS)
{
bsp_indication_set(BSP_INDICATE_FATAL_ERROR);
}
while(fs_callback_flag == 1) {power_manage();}
//Read the first 4 words of the page
NRF_LOG_INFO("Reading from flash address 0x%X: ", (uint32_t)fs_config.p_start_addr);
for(int i=0; i<4; i++)
{
flash_data[i] = *(fs_config.p_start_addr + i);
NRF_LOG_INFO("%X ", flash_data[i]);
}
//Write first word
data = 0x4455AABB;
NRF_LOG_INFO("Writing data 0x%X to address 0x%X\r\n", data, (uint32_t)fs_config.p_start_addr);
fs_callback_flag = 1;
ret = fs_store(&fs_config, fs_config.p_start_addr, &data, 1, NULL); //Write data to memory address 0x0003F00. Check it with command: nrfjprog --memrd 0x0003F000 --n 16
if (ret != FS_SUCCESS)
{
bsp_indication_set(BSP_INDICATE_FATAL_ERROR);
}
while(fs_callback_flag == 1) {power_manage();}
//Write second word
data = 0;
strcpy(char_data, "buzz");
char_data[4] = '\0';
data |= (char_data[0] << 24);
data |= (char_data[1] << 16);
data |= (char_data[2] << 8);
data |= (char_data[3] << 0);
NRF_LOG_INFO("Writing string '%s' (0x%X) to address 0x%X\r\n", (uint32_t)char_data, data, (uint32_t)fs_config.p_start_addr + 4);
fs_callback_flag = 1;
ret = fs_store(&fs_config, fs_config.p_start_addr + 1, &data, 1, NULL); //Write data to memory address 0x0003F000. Check it with command: nrfjprog --memrd 0x0003F000 --n 16
if (ret != FS_SUCCESS)
{
bsp_indication_set(BSP_INDICATE_FATAL_ERROR);
}
while(fs_callback_flag == 1) {power_manage();}
//Write third word
data = 0;
strcpy(special_char_data,"~!@#");
special_char_data[4] = '\0';
data |= (special_char_data[0] << 24);
data |= (special_char_data[1] << 16);
data |= (special_char_data[2] << 8);
data |= (special_char_data[3] << 0);
NRF_LOG_INFO("Writing string '%s' (0x%X) to address 0x%X\r\n", (uint32_t)special_char_data, data, (uint32_t)fs_config.p_start_addr + 8);
fs_callback_flag = 1;
ret = fs_store(&fs_config, fs_config.p_start_addr + 2, &data, 1, NULL); //Write data to memory address 0x0003F000. Check it with command: nrfjprog --memrd 0x0003F000 --n 16
if (ret != FS_SUCCESS)
{
bsp_indication_set(BSP_INDICATE_FATAL_ERROR);
}
while(fs_callback_flag == 1) {power_manage();}
//Read the first 4 words of the page and print in hex format
NRF_LOG_INFO("Reading from flash address 0x%X (hex): ", (uint32_t)fs_config.p_start_addr);
for(int i=0; i<4; i++)
{
flash_data[i] = *(fs_config.p_start_addr + i);
NRF_LOG_INFO("%X ", flash_data[i]);
}
//Read the first 4 words of the page and print in ascii format
NRF_LOG_INFO("Reading from flash address 0x%X (ascii): ", (uint32_t)fs_config.p_start_addr);
for(int i=0; i<4; i++)
{
char characters[4];
flash_data[i] = *(fs_config.p_start_addr + i);
characters[0] = flash_data[i] >> 24;
characters[1] = flash_data[i] >> 16;
characters[2] = flash_data[i] >> 8;
characters[3] = flash_data[i] >> 0;
NRF_LOG_INFO("%c%c%c%c ", characters[0], characters[1], characters[2], characters[3]);
}
}
Called directly in the main.c, no problem。 If, called in a callback function, the application stops running
Can you check what is the error ? is it hardfault ?
You need to make sure the priority of the interrupt where you call the function is APP_LOW so that you can call flash API. Otherwise you have to use app scheduler to call the flash's functions in main context.