nrf51822 --- flash(操作单片机自带)

1.目的

  操作单片机内置flash来保存数据

2.分析

   蓝牙内部很多东西用了flash来保存数据,比如绑定地址的数据都保存在flash,在实际应用中我们也经常要使用到flash来保存数据,比如手环,可以把计步数据,睡眠数据等等保存在单片机本来的flash ,以节约成本

  首先来了解下flash的参数


   参考:《nRF51822 PS v1.3 CN 20130903》


  

3.平台:

协议栈版本:SDK10.0.0

编译软件:keil 5.12

硬件平台:nrf51822最小系统

例子:SDK 10.0.0\examples\ble_peripheral\ble_app_uart\pca10028\s110\arm4

4.步骤

 1.首先初始化 uint32_t pstorage_init(void);这个函数在

   device_manager_init(erase_bonds);因为绑定信息里面需要用到flash。

   1.PSTORAGE_DATA_START_PAGES:这是参数是存数据的开始地址

   2.PSTORAGE_NUM_OF_PAGES :页的数量 这个是自定义存储快的数量这个至少要大于2,因为系统注册了一个用于保存绑定信息。我们也要注册自己的一块存储区域,所以至少要大于等于2  (用了多少个pstorage_register()为标准)

uint32_t pstorage_init(void)
{
    cmd_queue_init();

    m_next_app_instance = 0;
    m_next_page_addr    = PSTORAGE_DATA_START_ADDR;
    m_current_page_id   = 0;
    
    for (uint32_t index = 0; index < PSTORAGE_NUM_OF_PAGES; index++)
    {
        m_app_table[index].cb           = NULL;
        m_app_table[index].block_size   = 0;
        m_app_table[index].block_count  = 0;
    }

#ifdef PSTORAGE_RAW_MODE_ENABLE
    m_raw_app_table.cb           = NULL;
#endif //PSTORAGE_RAW_MODE_ENABLE

    m_state                     = STATE_IDLE;
    m_num_of_command_retries    = 0;
    m_flags                     = 0;
    m_num_of_bytes_written      = 0;
    m_flags                    |= MASK_MODULE_INITIALIZED;
       
    return NRF_SUCCESS;
}

2.自定义存储区域 :4*128


#ifndef _FLASH__H
#define __FLASH__H



#include "pstorage.h"
#include "pstorage_platform.h"
#include "nrf_error.h"
#include "app_error.h"

void register_humiture_flash(void)  ;
void load_humiture_flash(unsigned int *dat ,unsigned char size ,unsigned char Block_ID) ;
void clear_humitureflash_flash(uint8_t Block_ID)  ;
void storage_humitureflash_flash(unsigned int *dat ,unsigned char size,unsigned char Block_ID ) ;




#endif



#include "flash.h"


pstorage_handle_t base_humitureflash_handle;  
  
static void flash_test_pstorage_cb_handler(pstorage_handle_t * handle,  uint8_t op_code,  uint32_t result,  uint8_t * p_data,  uint32_t data_len)  
{  
    switch(op_code)  
    {  
           case PSTORAGE_STORE_OP_CODE:                                                                                                                                
				 if (result == NRF_SUCCESS)  
           {  
               // Store operation successful.  
           }  
           else  
           {  
               // Store operation failed.  
            }                                                                                                                                                                                                                                                                                                             break;
case PSTORAGE_LOAD_OP_CODE: if (result == NRF_SUCCESS) { // load operation successful. } else { // load operation failed.
} // Source memory can now be reused or freed. break; case PSTORAGE_UPDATE_OP_CODE: if (result == NRF_SUCCESS) { // Update operation successful. } else { // Update operation failed. } break; case PSTORAGE_CLEAR_OP_CODE: if (result == NRF_SUCCESS) { // Clear operation successful. } else { // Clear operation failed. } break; default: break; } } /******************************************************************************* * ???? : register_humiture_flash * ?? : ×¢²áÎÂʪ¶È´æ´¢Çø 48 *60 * * ? ? : void * ? ? : void * ? ? : void * ???? : 20160317 *******************************************************************************/ void register_humiture_flash(void) { uint32_t err_code; pstorage_module_param_t param; param.block_size = 128; param.block_count = 4; param.cb = flash_test_pstorage_cb_handler; err_code = pstorage_register(¶m, &base_humitureflash_handle); APP_ERROR_CHECK(err_code); }/******************************************************************************* * ???? : register_humiture_flash * ?? : ×¢²áÎÂʪ¶È´æ´¢Çø 48 *60 * * ? ? : void * ? ? : void * ? ? : void * ???? : 20160317 *******************************************************************************/ void load_humiture_flash(unsigned int *dat ,unsigned char size ,unsigned char Block_ID) { uint32_t err_code; pstorage_handle_t handle; err_code = pstorage_block_identifier_get(&base_humitureflash_handle,Block_ID,&handle); APP_ERROR_CHECK(err_code); err_code = pstorage_load((unsigned char *)dat,&handle,size,0); APP_ERROR_CHECK(err_code); } /******************************************************************************* * ???? : register_humiture_flash * ?? : ×¢²áÎÂʪ¶È´æ´¢Çø 48 *60 Çå³ýÄÇÒ»¸ö¿é * * ? ? : void * ? ? : void * ? ? : void * ???? : 20160317 *******************************************************************************/ void clear_humitureflash_flash(uint8_t Block_ID) { uint32_t err_code; pstorage_handle_t handle; err_code = pstorage_block_identifier_get(&base_humitureflash_handle,Block_ID,&handle); APP_ERROR_CHECK(err_code); err_code = pstorage_clear(&base_humitureflash_handle,512); APP_ERROR_CHECK(err_code); } /******************************************************************************* * ???? : register_humiture_flash * ?? : ×¢²áÎÂʪ¶È´æ´¢Çø 48 *60 * * ? ? : void * ? ? : void * ? ? : void * ???? : 20160317 *******************************************************************************/ void storage_humitureflash_flash(unsigned int *dat ,unsigned char size,unsigned char Block_ID ) { uint32_t err_code; pstorage_handle_t handle; err_code= pstorage_block_identifier_get(&base_humitureflash_handle, Block_ID,&handle); APP_ERROR_CHECK(err_code); err_code = pstorage_store(&handle,(unsigned char *)dat,size,0); APP_ERROR_CHECK(err_code); }


在main()中

	        uint32_t  Sorce[48] = {1,2,3,4,5,6};
                uint32_t  Sorce1[48] = {0};
	        register_humiture_flash();
		clear_humitureflash_flash(0);
		load_humiture_flash(Sorce1,40,0);
		storage_humitureflash_flash(Sorce,40,0);
		Sorce[1]= 10;
		load_humiture_flash(Sorce1,40,0);
		storage_humitureflash_flash(Sorce,40,1);
		load_humiture_flash(Sorce1,40,1);




a. 清除数据读出来的数据是0XFFFFFFF 

 b.读出来和写进去的数据一样。。

  表示实验成功
参考 :http://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.sdk52.v0.9.0%2Flib_pstorage.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值