3.5.1 w800的flash读写操作
本例仍然使用demo固件
确保宏定义DEMO_FLASH
已经打开,在头文件wm_demo.h
中设置,如下:
#define DEMO_CONSOLE DEMO_ON
#define DEMO_FLASH (DEMO_ON && DEMO_CONSOLE)
固件运行后发送串口命令:t-flash()
即可运行flash读写功能,运行后输出“success”则表示读取flash成功。
读写功能代码位于文件wm_flash_demo.c
中,代码片段如下:
#define TEST_FLASH_BUF_SIZE 4000
int flash_demo(void)
{
u8 *write_buf = NULL;
u8 *read_buf = NULL;
u16 i;
tls_fls_init(); //initialize flash driver
write_buf = tls_mem_alloc(TEST_FLASH_BUF_SIZE); //写入数据缓存
if (NULL == write_buf)
{
printf("\nmalloc write buf error\n");
return WM_FAILED;
}
for (i = 0; i < TEST_FLASH_BUF_SIZE; i ++)
{
write_buf[i] = i + 1; //初始化写入数据
}
tls_fls_write(0x1F0303, write_buf, 1247); /**verifying cross sector writing*/
tls_fls_write(0x1F0303 + 1247, write_buf + 1247, 2571);
tls_fls_write(0x1F0303 + 1247 + 2571, write_buf + 1247 + 2571, 182); //缓存数据写入flash
read_buf = tls_mem_alloc(TEST_FLASH_BUF_SIZE); //申请读取flash的缓存
if (NULL == read_buf)
{
printf("\nmalloc read buf error\n");
tls_mem_free(write_buf);
return WM_FAILED;
}
memset(read_buf, 0, TEST_FLASH_BUF_SIZE);
tls_fls_read(0x1F0303, read_buf, TEST_FLASH_BUF_SIZE); //从之前写入数据的首地址0x1F0303开始读取TEST_FLASH_BUF_SIZE大小的数据到读取缓存中
if (0 == memcmp(write_buf, read_buf, TEST_FLASH_BUF_SIZE))
{
printf("\nsuccess\n"); //比较读取缓存和写入缓存的数据,如果读出的数据和写入数据一致,则输出“success”
}
else
{
printf("\nfail\n");
}
tls_mem_free(write_buf);
tls_mem_free(read_buf);
return WM_SUCCESS;
}
根据上面注释的分析可以看出,当读取的数据与写入的数据一致时串口输出“success”
flash读写主要用到sdk中的两个接口:tls_fls_read
以及tls_fls_write
读flash函数原型为:int tls_fls_read(u32 addr, u8 *buf, u32 len)
,第一个参数是读取起始地址,第二个是读取缓存地址,第三个是读取长度
写flash函数原型为:int tls_fls_write(u32 addr, u8 *buf, u32 len)
,第一个参数是写入起始地址,第二个是写入数据缓存地址,第三个是写入长度