nrf51822 --- flash(操作单片机自带)
来源:互联网 发布:维多利亚 知乎 编辑:程序博客网 时间:2024/06/10 08:50
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. 清除数据读出来的数据是0XFFFFFFFb.读出来和写进去的数据一样。。
表示实验成功
参考 :http://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.sdk52.v0.9.0%2Flib_pstorage.html0 0
- nrf51822 --- flash(操作单片机自带)
- 【单片机笔记】STM8S系列单片机FLASH操作
- nrf51822 写flash 失败
- BLE-NRF51822教程7-带协议栈工程中使用flash
- BLE-NRF51822教程7-带协议栈工程中使用flash
- BLE-NRF51822教程7-带协议栈工程中使用flash
- BLE-NRF51822教程7-带协议栈工程中使用flash
- 51单片机驱动VS1003(模拟spi/自带SPI)
- 单片机MSP430 - MSP430+CC1101常用自带函数
- MSP430单片机学习(一)——Flash读写操作
- flash自带的10种过渡效果
- 基于ie内核,浏览器自带flash插件
- 禁用360浏览器6.2自带的Flash Player
- AS3文档类调用FLASH自带组件
- 用jdk自带包操作XML
- windows自带的一个死锁操作
- 操作oracle自带scott数据库
- 用jdk自带包操作XML
- HDU 3436 Queue-jumpers
- Stanford机器学习---第五讲. 神经网络的学习 Neural Networks learning
- 自定义控件三部曲之动画篇(十二)——animateLayoutChanges与LayoutTransition
- Android定时器
- 周总结3.13
- nrf51822 --- flash(操作单片机自带)
- 大数乘法
- 第五周项目2-游戏中的角色类(2)
- hdoj-2141-Can you find it?
- 第五周项目1:三角形类雏形(1)
- [LeetCode]Palindrome Linked List
- Java 封装 HDFS API 操作
- 生成boot.img的脚本
- 每天一个Linux命令