在操作nRF 52810 flash擦除时遇到一个问题:擦除结束芯片会出错重启。
原因:使能了协议栈的工程不能使用NVMC要使用fstorage 或 fds
注意
1、添加路径xxx\nRF5_SDK_15.3.0_59ac345\components\libraries\fstorage
2、sdk_config.h 修改:
#define NRF_FSTORAGE_ENABLED 1
3、每次写的字节数必须是4的倍数
4、写完要等待wait_for_flash_ready(&fstorage);
下面贴出我的fstorage测试示例
#ifndef __MY_FS_H_
#define __MY_FS_H_
extern void fs_test();
#endif
#include "my_fs.h"
#include "nrf_sdh.h"
#include "nrf_sdh_ble.h"
#include "nrf_fstorage_sd.h"
#include "nrf_fstorage.h"
#include "nrf.h"
#include "nrf_soc.h"
#include "nordic_common.h"
#include <stdint.h>
#include "app_error.h"
#include "nrf_delay.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
/********************************************************************************************************
*使能协议栈 用fs或fds的方式操作flash
*1、nRF52810片上flash擦写次数 10000次
*2、写和读都是32bit的,如写入0x123456芯片的存入是这样的 56 34 12 00,所以你可以将8bit的转成32bit的再存,高低位可以自己控制
*3、读数据的时候可以不用nrf_fstorage_read();读的数据多了,会报错,
直接用memcpy(),直接读地址,将地址里的数copy出来,这样就不会报错了。
*4、sdk_config.h 中需要修改:#define NRF_FSTORAGE_ENABLED 1
*****************************************************/
static void fstorage_evt_handler(nrf_fstorage_evt_t * p_evt);
static void print_flash_info(nrf_fstorage_t * p_fstorage);
static uint32_t nrf5_flash_end_addr_get();
void wait_for_flash_ready(nrf_fstorage_t const * p_fstorage);
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 = 0x2f000,
.end_addr = 0x30000,
};
//使能协议栈后 flash 操作的初始化函数
void ble_fs_init()
{
ret_code_t rc;
nrf_fstorage_api_t * p_fs_api;
/* 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();
}
//fstorage 功能 单元测试函数
void fs_test()
{
ret_code_t rc; //库函数返回值
uint8_t loop = 0;
/* Dummy data to write to flash. */
uint32_t m_data = 0x12ABCDEF;
char m_hello_world[] = "hello world";
uint8_t wr_buff[32] = {0}; //用于写测试的缓存
uint8_t rd_buff[64] = {0}; //用于读出来校验的缓存
enum TEST_FS_ADDR { //写入测试的地址
ERASE_ADDR = 0x2F000, //nRF 52810 的最后一页
WRITE_U32_ADDR = ERASE_ADDR,
WRITE_STRING_ADDR = WRITE_U32_ADDR + sizeof(m_data),
WRITE_BUFF_ADDR = WRITE_STRING_ADDR + sizeof(m_hello_world),
} ;
//0、fstorage 的初始化
ble_fs_init();
/* Let's write to flash. */
//1、测试写unsigned int 型整数 再读出 的 字节序
NRF_LOG_DEBUG("Writing int\"%x\" at 0x%08x.", m_data,WRITE_U32_ADDR);
rc = nrf_fstorage_write(&fstorage, WRITE_U32_ADDR, &m_data, sizeof(m_data), NULL);
APP_ERROR_CHECK(rc);
wait_for_flash_ready(&fstorage);
nrf_fstorage_read(&fstorage,WRITE_U32_ADDR,rd_buff,4);
NRF_LOG_DEBUG("rd int: 0x%02x 0x%02x 0x%02x 0x%02x",rd_buff[0],rd_buff[1],rd_buff[2],rd_buff[3]);
//2、测试写字符串 再读出 的 字节序
NRF_LOG_DEBUG("Writing string\"%s\" at 0x%08x.", m_hello_world,WRITE_STRING_ADDR);
rc = nrf_fstorage_write(&fstorage, WRITE_STRING_ADDR, m_hello_world, sizeof(m_hello_world), NULL);
APP_ERROR_CHECK(rc);
wait_for_flash_ready(&fstorage);
nrf_fstorage_read(&fstorage,WRITE_STRING_ADDR,rd_buff,sizeof(m_hello_world));
NRF_LOG_DEBUG("rd string: %s",rd_buff);
//3、测试写数组 再读出 的 字节序
memset(wr_buff,0,sizeof(wr_buff));
while(loop < sizeof(wr_buff) - 5)
{
wr_buff[loop] = loop;
loop++ ;
}
NRF_LOG_DEBUG("Writing buff[%d] at 0x%08x.",sizeof(wr_buff),WRITE_BUFF_ADDR);
NRF_LOG_HEXDUMP_DEBUG(wr_buff,sizeof(wr_buff));
rc = nrf_fstorage_write(&fstorage, WRITE_BUFF_ADDR, wr_buff, sizeof(wr_buff), NULL);
APP_ERROR_CHECK(rc); nrf_delay_ms(10);
wait_for_flash_ready(&fstorage);
nrf_fstorage_read(&fstorage,WRITE_BUFF_ADDR,rd_buff,sizeof(wr_buff));
NRF_LOG_DEBUG("rd buff:");
NRF_LOG_HEXDUMP_DEBUG(rd_buff,sizeof(wr_buff));
//4、测试删除一页
NRF_LOG_DEBUG("Before Erase.");
//nrf_fstorage_read(&fstorage,ERASE_ADDR,rd_buff,sizeof(rd_buff));
memcpy(rd_buff,(uint32_t *)ERASE_ADDR,sizeof(rd_buff));
NRF_LOG_HEXDUMP_DEBUG(rd_buff,sizeof(rd_buff));nrf_delay_ms(10);
NRF_LOG_DEBUG("Erase Page No.%02d",ERASE_ADDR/4096);
rc = nrf_fstorage_erase(&fstorage,ERASE_ADDR,1,NULL);
APP_ERROR_CHECK(rc);
wait_for_flash_ready(&fstorage);
NRF_LOG_DEBUG("Done.");
NRF_LOG_DEBUG("After Erase.");
nrf_fstorage_read(&fstorage,ERASE_ADDR,rd_buff,sizeof(rd_buff));
NRF_LOG_HEXDUMP_DEBUG(rd_buff,sizeof(rd_buff));
}
static void fstorage_evt_handler(nrf_fstorage_evt_t * p_evt)
{
if (p_evt->result != NRF_SUCCESS)
{
NRF_LOG_DEBUG("--> Event received: ERROR while executing an fstorage operation.");
return;
}
switch (p_evt->id)
{
case NRF_FSTORAGE_EVT_WRITE_RESULT:
{
NRF_LOG_DEBUG("--> Event received: wrote %d bytes at address 0x%x.",
p_evt->len, p_evt->addr);
} break;
case NRF_FSTORAGE_EVT_ERASE_RESULT:
{
NRF_LOG_DEBUG("--> Event received: erased %d page from address 0x%x.",
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));
}
/**@brief Sleep until an event is received. */
static void power_manage(void)
{
#ifdef SOFTDEVICE_PRESENT
(void) sd_app_evt_wait();
#else
__WFE();
#endif
}
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();
}
}
static void print_flash_info(nrf_fstorage_t * p_fstorage)
{
NRF_LOG_DEBUG("========| flash info |========");
NRF_LOG_DEBUG("erase unit: \t%d bytes", p_fstorage->p_flash_info->erase_unit);
NRF_LOG_DEBUG("program unit: \t%d bytes", p_fstorage->p_flash_info->program_unit);
NRF_LOG_DEBUG("==============================");
}