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

1.目的

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

2.分析

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

  首先来了解下flash的参数

nrf51822 --- flash(操作单片机自带)_第1张图片

   参考:《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

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_LOAD_OP_CODE: <span style="font-family: Arial;"> if (result == NRF_SUCCESS)</span>
           {
               // Store operation successful.
           }
           else
           {
               // Store 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(&<span style="font-family: Arial;">param</span><span style="font-family: Arial;">, &base_humitureflash_handle);</span>
		APP_ERROR_CHECK(err_code);
}<span style="font-family: Arial;">                             </span>
/*******************************************************************************
 * ???? :   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);


nrf51822 --- flash(操作单片机自带)_第2张图片

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

nrf51822 --- flash(操作单片机自带)_第3张图片

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

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

你可能感兴趣的:(nrf51822 --- flash(操作单片机自带))