新闻  |   论坛  |   博客  |   在线研讨会
NRF52832---FLASH存储用户数据(掉电保存)
电子禅石 | 2018-01-19 09:50:51    阅读:1511   发布文章

NRF52832---FLASH存储用户数据(掉电保存)

硬件使用: NRF52832QFAAB0FLASH  512K  RAM 64KB

协议栈:S132  4.0.2

SDK:  nRF5 SDK v13.0.0

参考资源:

1http://blog.csdn.net/zxzkyking1/article/details/72845355

2http://infocenter.nordicsemi.com/index.jsp   Experimental: Flash Storage

交流qq: 1732700158 可以一起讨论52832相关开发的点点滴滴。

 

1.问题:实际项目中有很多时候,需要存储用户的设置信息,比如广播名称的修改,发射功率的修改,广播间隔的修改,密码修改,是否已经与设备绑定。这时候就需要用到FLASH来存储用户数据,达到掉电也保存配置数据。这里用Nrf52832 FLASH 配合协议栈 来实现最基本的用户数据的存储,擦除,读取。

2.关键点:存储的地址是初始化时候自动分配的。还有就是擦除读取,在主程序中进行,不要放到各种回调函数中进行,比如 nus在收到擦除的命令,给一个标志,在主程序中进行擦除动作。

QQ截图20180119094927.png

typedef struct

{

    /**@brief   The beginning of the flash space assigned to the application which registered this

     *          configuration. This field is set by @ref fs_init. It can also be set manually.

     */

    uint32_t const * p_start_addr;

 

    /**@brief   The end of the flash space assigned to the application which registered this

     *          configuration. This field is set by @ref fs_init. It can also be set manually.

     */

    uint32_t const * p_end_addr;

 

    fs_cb_t  const   callback;    //!< Callback to run when a flash operation has completed.

    uint8_t  const   num_pages;   //!< The number of flash pages requested.

 

    /**@brief   The priority with which fstorage should assign flash pages to this application,

     *          with respect to other applications. Applications with higher priority will be

     *          assigned flash pages with a higher memory address. The highest priority is

     *          reserved. Must be unique among configurations.

     */

    uint8_t  const   priority;

} fs_config_t;

 

3.步骤:

static uint8_t fs_callback_flag;

注册使用:

 

FS_REGISTER_CFG(fs_config_t fs_config) =

 {

    .callback  = fs_evt_handler, // Function for event callbacks.

    .num_pages = NUM_PAGES,      // Number of physical flash pages required.

    .priority  = 0xFE            // Priority for flash usage.

 };

回调函数:

static void fs_evt_handler(fs_evt_t const * const evt, fs_ret_t result)

{

    if (result != FS_SUCCESS)

    {

        // An error occurred.

    }

                   else

                   {

                            fs_callback_flag=0;

                   }

}

void user_info_save_init(void)

{

        

         fs_ret_t ret = fs_init();

   if (ret != FS_SUCCESS)

  {

    // An error occurred.

  }

 

}

// Retrieve the address of a page.

static uint32_t const * address_of_page(uint16_t page_num)

{

    return fs_config.p_start_addr + (page_num * PAGE_SIZE_WORDS);

}

 

void user_info_erase(void)

{

          fs_ret_t ret;

          fs_callback_flag= 1;

          ret = fs_erase(&fs_config, address_of_page(0), 1,NULL);

 if (ret != FS_SUCCESS)

 {

    NRF_LOG_INFO("%d\r\n",ret);

 }

  while(fs_callback_flag== 1)

 {

   power_manage();

 }

}

void user_info_store(void)

{

                   fs_ret_t ret;

           fs_callback_flag= 1;

  ret = fs_store(&fs_config, fs_config.p_start_addr, (uint32_t*)&app_status, sizeof(app_status),NULL);

if (ret != FS_SUCCESS)

{

    // An error occurred.

}

 while(fs_callback_flag== 1)

 {

   power_manage();

 }

}

 

void user_info_read(void)//only invoke once when powen on

{

         //memcpy(&test_app_status,fs_config.p_start_addr,sizeof(app_status));

           memcpy(&app_status,fs_config.p_start_addr,sizeof(app_status));

           if((app_status.BondStatus ==0xFF)||(app_status.BeepStatus=0))

                   {

                            memset(&app_status,0,sizeof(app_status));

                   }

}


参与讨论
登录后参与讨论
推荐文章
最近访客