nRF52832中对flash都读写操作

nRF52832中对flash都读写操作

使用SDK 12.2.0.

nRF52832应用程序的空间从0x0001F000开始。Flash的结构图:

协议栈提供了对flash操作的API,在\components\libraries中的fdsfstorage.

fds(Flash Data Storage)是一种极简的、面向记录的文件系统,用于芯片的闪存。文件存储为可变长度记录的集合。FDS支持同步读操作和异步写操作(写、更新和删除)

Fstorage提供用来存储数据到flash、擦除flash的函数。


流程:


示例程序:

Main.c中添加程序;


#define NUM_PAGES 4             //分配四页

#define PAGE_SIZE_WORDS 256     //每页大小256字节

 

static uint8_t fs_callback_flag;      //回调函数标识


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

{

   if (result != FS_SUCCESS)

    {

       bsp_indication_set(BSP_INDICATE_FATAL_ERROR);

    }

                   else

                   {

                                     NRF_LOG_INFO("    fstorage command successfullycompleted   \n\r");

                                     fs_callback_flag = 0;

                   }

}

 

 

static void fstorage_test(void)

{

                   staticuint32_t data;

                   uint32_tflash_data[4];

        

                   FS_REGISTER_CFG(fs_config_tfs_config) =

                   {

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

                                     .num_pages= NUM_PAGES,      // Number of physicalflash pages required.

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

                   };

                  

                   fs_ret_tret = fs_init();

                   if(ret != FS_SUCCESS)

                   {

                                     bsp_indication_set(BSP_INDICATE_FATAL_ERROR);

                   }

                                     //Readthe first 4 words of the page

                   SEGGER_RTT_printf(0,"Dataread from flash address 0x%X: \r\n", (uint32_t)fs_config.p_start_addr);

                   for(inti=0; i<4; i++)

                   {

                                     flash_data[i]= *(fs_config.p_start_addr + i);

                                     SEGGER_RTT_printf(0,"%X", flash_data[i]);

                   }

                   SEGGER_printf("\r\n");

                   //Erase one page (page 0).

                   SEGGER_RTT_printf(0,"Erasinga flash page at address 0x%X\r\n", (uint32_t)fs_config.p_start_addr);

                   fs_callback_flag= 1;

                   ret= fs_erase(&fs_config, fs_config.p_start_addr, 1, NULL);

                   if(ret != FS_SUCCESS)

                   {

                                     bsp_indication_set(BSP_INDICATE_FATAL_ERROR);

                   }

                   while(fs_callback_flag== 1) 

                   {

                       power_manage();

                   }

                                    

                   data= 0xAAAAAAAA;

                   SEGGER_RTT_printf(0,"Writingdata 0x%X to address 0x%X\r\n", data, (uint32_t)fs_config.p_start_addr);

                   fs_callback_flag= 1;

                   ret= fs_store(&fs_config, fs_config.p_start_addr, &data, 1, NULL);      //Write data to memory 

                   if(ret != FS_SUCCESS)

                   {

                                     bsp_indication_set(BSP_INDICATE_FATAL_ERROR);

                   }

                   while(fs_callback_flag== 1)  { power_manage(); }

                  

                   data= 0xBBBBBBBB;

                   SEGGER_RTT_printf(0,"Writingdata 0x%X to address 0x%X\r\n", data, (uint32_t)fs_config.p_start_addr +4);

                   fs_callback_flag= 1;

                   ret= fs_store(&fs_config, fs_config.p_start_addr + 1, &data, 1,NULL);      //Write data to memoryaddress

                   if(ret != FS_SUCCESS)

                   {

                                     bsp_indication_set(BSP_INDICATE_FATAL_ERROR);

                   }

                   while(fs_callback_flag== 1)  { power_manage(); }

                  

                   //Readthe first 4 words of the page

                   SEGGER_RTT_printf(0,"Dataread from flash address 0x%X: \r\n", (uint32_t)fs_config.p_start_addr);

                   for(inti=0; i<4; i++)

                   {

                                     flash_data[i]= *(fs_config.p_start_addr + i);

                                     SEGGER_RTT_printf(0,"%X", flash_data[i]);

                   }

}


main()

{

   .........

   fstorage_test();

}


测试:




 


   


你可能感兴趣的:(nRF52832)