19

ZHT · 2022年01月24日

【XR806开发板试用】使用FDCM操作Flash记录开机次数

一、寻找系统分配的自定义用户数据地址

(1)XR806的Flash布局
1.png
如图1所示,FLASH的布局有两种:

1、没有开启OTA模式;Image1+Padding+Sysinfo
2、开启OTA模式;Image1+Padding+Sysinfo+OTA area +Image2 +Padding

2.png
3.png
如图2/3,当前工程采用wlan_ble_demo,未开启OTA模式,
查看device/xradio/xr806/xr_skylark/project/demo/wlan_ble_demo/gcc/Makefile文件,Flash的cfg文件为IMAGE_CFG := ./image_wlan_ble.cfg
4.png
(2)查看Sysinfo的地址
Sysinfo区域是一段用于存储用户自定义数据的区域,紧跟在Image1区域后面,通过device/xradio/xr806/xr_skylark/project/demo/wlan_ble_demo/prj_config.h得知Sysinfo的地址和长度

二、使用FDCM操作Sysinfo区域

FDCM模块与SDK中其他模块之间的关系如下图所示。Sysinfo,OTA和IMAGE等模块中都有使用到FDCM模块接口,保存其对应的数据。
6.png
注意:(1)FDCM操作的区域需要与Flash可擦除的Block对齐;
(2)FDCM模块管理的Flash区域不要与其他模块(如Image)使用的Flash区域产生冲突,避免相互影响
(3)用FDCM模块管理的Flash区域必须与Flash可擦除的block对齐,即区域起始地址与Flash擦除Block的边缘对齐,并且区域大小为Flash可擦除最小Block大小的整数倍

三、实现代码

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include "ohos_init.h"
#include "kernel/os/os.h"
#include "sys/fdcm.h"
#include "common/framework/platform_init.h"
#define FLASH_DEVICE_NUM         0
#define IoT_FLASH 1
#define FDCM_FLASH_START_ADDR  (1536*1024)
#define FDCM_SIZE   (4*1024)
static OS_Thread_t g_main_thread;

static void MainThread(void *arg)
{
   fdcm_handle_t *fdcm;
   uint8_t write_buf[100];
   uint8_t read_buf[100];

   fdcm = fdcm_open(FLASH_DEVICE_NUM, FDCM_FLASH_START_ADDR, FDCM_SIZE);
   if (fdcm == NULL) {
    printf("fdcm open fail.\n");
    return ;
   }
   printf("fdcm open success, flash addr:0x%x, flash size:%d\n", FDCM_FLASH_START_ADDR, FDCM_SIZE);
   printf("we can use fdcm to save info to flash.\n");
   fdcm_read(fdcm, &read_buf, sizeof(read_buf));   
   if(strlen(read_buf)==0)
   {
      printf("first power on \r\n");
      sprintf((char *)write_buf,"%d",1);
      fdcm_write(fdcm, &write_buf, sizeof(write_buf));
   }
   else
   {   
       printf("read_buf = %d\r\n",atoi((char *)read_buf));  
       sprintf(write_buf,"%d",atoi((char *)read_buf)+1);
       printf("write_buf = %d\r\n",atoi((char *)write_buf));
       fdcm_write(fdcm, &write_buf, sizeof(write_buf));
   } 
   LOS_Msleep(10);
   fdcm_read(fdcm, &read_buf, sizeof(read_buf));
   LOS_Msleep(20);
   printf("Power-on times:%s\r\n",read_buf);
   fdcm_close(fdcm);
}

void FlashTestMain(void)
{
    printf("FDCM Flash Test \r\n");
    if (OS_ThreadCreate(&g_main_thread, "MainThread", MainThread, NULL,
                OS_THREAD_PRIO_APP, 4 * 1024) != OS_OK) {
        printf("[ERR] Create MainThread Failed\n");
    }
}

SYS_RUN(FlashTestMain);

四:实验现象展示

====================================================================
    Hello! OpenHarmony!
    System tag : OpenHarmony 1.1.2_LTS
====================================================================
    
use default flash chip mJedec 0x0
[FD I]: mode: 0x10, freq: 96000000Hz, drv: 0
[FD I]: jedec: 0x0, suspend_support: 1
mode select:e

wlan information ===================================================
firmware:
    version : R0-XR_C07.08.52.65_02.84 May 27 2021 11:41:33-Y02.84 
    buffer  : 8
driver:
    version : XR_V02.05
mac address:
    in use        : cc:6e:48:3e:0d:01
    in use        : cc:6e:48:3e:0d:02
====================================================================

wlan mode:a
[VFS INF] LittleFS mount success.

platform information ===============================================
XR806 SDK v1.2.0  Jan 23 2022 09:49:52

heap space [0x223230, 0x24bc00), size 166352

cpu  clock 160000000 Hz
HF   clock  40000000 Hz

sdk option:
    XIP           : enable
    INT LF OSC    : enable
    SIP flash     : enable

mac address:
    efuse         : 80:74:84:05:bb:f9
    in use        : cc:6e:48:3e:0d:01
====================================================================

FDCM Flash Test 
Wifi Test Start

console init success
fdcm open success, flash addr:0x180000, flash size:4096
hiview init success.hello world!
we can use fdcm to save info to flash.
read_buf = 8
write_buf = 9
Power-on times:9
hello world!
====================================================================
    Hello! OpenHarmony!
    System tag : OpenHarmony 1.1.2_LTS
====================================================================
    
use default flash chip mJedec 0x0
[FD I]: mode: 0x10, freq: 96000000Hz, drv: 0
[FD I]: jedec: 0x0, suspend_support: 1
mode select:e

wlan information ===================================================
firmware:
    version : R0-XR_C07.08.52.65_02.84 May 27 2021 11:41:33-Y02.84 
    buffer  : 8
driver:
    version : XR_V02.05
mac address:
    in use        : cc:6e:48:3e:0d:01
    in use        : cc:6e:48:3e:0d:02
====================================================================

wlan mode:a
[VFS INF] LittleFS mount success.

platform information ===============================================
XR806 SDK v1.2.0  Jan 23 2022 09:49:52

heap space [0x223230, 0x24bc00), size 166352

cpu  clock 160000000 Hz
HF   clock  40000000 Hz

sdk option:
    XIP           : enable
    INT LF OSC    : enable
    SIP flash     : enable

mac address:
    efuse         : 80:74:84:05:bb:f9
    in use        : cc:6e:48:3e:0d:01
====================================================================

FDCM Flash Test 
Wifi Test Start

console init success
fdcm open success, flash addr:0x180000, flash size:4096
hiview init success.hello world!
we can use fdcm to save info to flash.
read_buf = 9
write_buf = 10
Power-on times:10
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
====================================================================
    Hello! OpenHarmony!
    System tag : OpenHarmony 1.1.2_LTS
====================================================================
    
use default flash chip mJedec 0x0
[FD I]: mode: 0x10, freq: 96000000Hz, drv: 0
[FD I]: jedec: 0x0, suspend_support: 1
mode select:e

wlan information ===================================================
firmware:
    version : R0-XR_C07.08.52.65_02.84 May 27 2021 11:41:33-Y02.84 
    buffer  : 8
driver:
    version : XR_V02.05
mac address:
    in use        : cc:6e:48:3e:0d:01
    in use        : cc:6e:48:3e:0d:02
====================================================================

wlan mode:a
[VFS INF] LittleFS mount success.

platform information ===============================================
XR806 SDK v1.2.0  Jan 23 2022 09:49:52

heap space [0x223230, 0x24bc00), size 166352

cpu  clock 160000000 Hz
HF   clock  40000000 Hz

sdk option:
    XIP           : enable
    INT LF OSC    : enable
    SIP flash     : enable

mac address:
    efuse         : 80:74:84:05:bb:f9
    in use        : cc:6e:48:3e:0d:01
====================================================================

FDCM Flash Test 
Wifi Test Start

console init success
fdcm open success, flash addr:0x180000, flash size:4096
hiview init success.hello world!
we can use fdcm to save info to flash.
read_buf = 10
write_buf = 11
Power-on times:11

五:遗留问题

(1)Image的max_size为1532K,设置FDCM的起始地址为(15321024)时,本次开机的写入读取未报错,但是数据未保存;FDCM的起始地址改为(15361024),则正常。
(2)如有错误,请回复指正
六:参考
(1)https://harmonyos.51cto.com/p...
(2)《XR806_FDCM中间件_开发指南》
(3)《XR806_Flash布局方案_开发指南》
推荐阅读
关注数
13823
内容数
139
全志XR806开发板相关的知识介绍以及应用专栏。
目录
极术微信服务号
关注极术微信号
实时接收点赞提醒和评论通知
安谋科技学堂公众号
关注安谋科技学堂
实时获取安谋科技及 Arm 教学资源
安谋科技招聘公众号
关注安谋科技招聘
实时获取安谋科技中国职位信息