3.5.1 w800的flash读写操作
<p>本例仍然使用demo固件</p>
<p>确保宏定义<code>DEMO_FLASH</code>已经打开,在头文件<code>wm_demo.h</code>中设置,如下:</p>
<pre><code class="language-c">#define DEMO_CONSOLE DEMO_ON
#define DEMO_FLASH (DEMO_ON && DEMO_CONSOLE)</code></pre>
<p>固件运行后发送串口命令:<code>t-flash()</code>即可运行flash读写功能,运行后输出“success”则表示读取flash成功。</p>
<p>读写功能代码位于文件<code>wm_flash_demo.c</code>中,代码片段如下:</p>
<pre><code class="language-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;
}</code></pre>
<p>根据上面注释的分析可以看出,当读取的数据与写入的数据一致时串口输出“success”</p>
<p>flash读写主要用到sdk中的两个接口:<code>tls_fls_read</code>以及<code>tls_fls_write</code></p>
<p>读flash函数原型为:<code>int tls_fls_read(u32 addr, u8 *buf, u32 len)</code>,第一个参数是读取起始地址,第二个是读取缓存地址,第三个是读取长度</p>
<p>写flash函数原型为:<code>int tls_fls_write(u32 addr, u8 *buf, u32 len)</code>,第一个参数是写入起始地址,第二个是写入数据缓存地址,第三个是写入长度</p>