找回密码
 立即注册

使用微信账号登录

只需一步,快速开始

CSR8675学习笔记:从外部Flash读取bin文件

2021-5-21 23:17| 发布者: csdn| 查看: 2462| 评论: 2|来自: CSDN

摘要: #1. 引言项目需要CSR8675能够boot两个功放芯片内部的DSP。为了满足这个需求,需要分几个步骤实现:将2个20KB的bin文件存储在CSR8675的外部Flash中。一边读取bin文件一边解析,并将解析得到的数据通过I2C写入功放芯片 ...
#1. 引言
项目需要CSR8675能够boot两个功放芯片内部的DSP。

为了满足这个需求,需要分几个步骤实现:

将2个20KB的bin文件存储在CSR8675的外部Flash中。
一边读取bin文件一边解析,并将解析得到的数据通过I2C写入功放芯片。
#2. 将文件存入外部Flash
##2.1. 外部Flash分区
创建分区文件:
ext_flash.ptn, File system
0, 1024K, RS, (erase) # Logical 0: Partition for DFU
1, 128K, RO, 2557_bin.xuv # Logical 1: Store 2557 bin file
2, 128K, RO, (erase) # Logical 2:

上述分区文件将外部Flash文件分为三段。0分区用于存储DFU升级文件,1分区用于存储bin文件,2分区暂时不用。

分区文件的写法可参考官方的nvscmd.chm。

##2.2. 打包bin文件
创建打包脚本ext_flash.ptn:

@echo off

echo. *************************************
echo. Update external flash start
echo. *************************************

:: set CSR install path
set adkpath=C:\ADK4.1
set dfutoolspath=%adkpath%\tools\bin

:: set project path
set ReleaseVersion=lemon_ext_flash_V0.1
set projectpath=%adkpath%\apps\lemon\ext_flash
set releasepath=%projectpath%\%ReleaseVersion%
set releasefilepath=%releasepath%\ext_flash_files
set releasexuvpath=%releasepath%\ext_flash_xuv

set debugtransport=SPITRANS=USB SPIPORT=0

del /f /s /Q %releasepath%
mkdir %releasepath%
mkdir %releasefilepath%
mkdir %releasexuvpath%

echo. *************************************
echo. Erase external flash
echo. *************************************
echo.

call %dfutoolspath%\nvscmd.exe -usb 0 erase

echo. *************************************
echo. Package files into file system
echo. *************************************
echo.
copy /b %projectpath%\2557_tuning.bin
call %dfutoolspath%\packfile.exe %releasefilepath% %releasexuvpath%\2557_bin.xuv
打包文件前需要先调用nvscmd擦除Flash。packfile.exe会将指定文件夹路径中的文件打包成.xuv后缀的文件。

##2.3. 写入外部Flash
运行下述脚本后,bin文件会写入外部Flash内的分区1。

echo. *************************************
echo. Burn external flash
echo. *************************************
echo.
copy /b %projectpath%\ext_flash.ptn %releasexuvpath%\ext_flash.ptn
call %dfutoolspath%\nvscmd.exe -usb 0 burn %releasexuvpath%\ext_flash.ptn all

#3. VM读取Flash中的文件
##3.1. 加载Flash分区
VM只能一次读取一个分区,在读取前需先加载。参考代码如下:

#define MOUNT_FILE_SYSTEM_SECTION_INDEX 1

/* mount sqif partition 1 */
if (!PartitionMountFilesystem(PARTITION_SERIAL_FLASH,
                            MOUNT_FILE_SYSTEM_SECTION_INDEX,
                            PARTITION_LOWER_PRIORITY)) 
{
    DEBUG_2557_ERR(("2557: Can't mount file system %d\n", MOUNT_FILE_SYSTEM_SECTION_INDEX));
    Panic();
}

##3.2. 读取Flash分区
VM中读取Flash中的文件需调用stream source API:

5.3 Source API


A source can be thought of as an infinite buffer divided into three sections:
Figure 5-2 Source buffer

■ The dropped section is data that the application has already processed and does not need the streams library to retain.
■ The readable section holds the data that the streams library has placed in the source and that the application is permitted to access.
■ The writeable section is where the library is still placing data.

In general, an application should only read from a source upon receipt of a MORE_DATA message. This indicates that there is data in the source which can be read. An application reading from a source must perform the following sequence:

Get the amount of readable data in the source, using SourceSize
Map the source into application memory, using SourceMap
Process the data received
Drop processed data, using SourceDrop If more data arrives at the source, the process is repeated.
NOTE: There is a strict limit on how big the readable section can grow, so applications should drop data as soon as possible. Failing to do so eventually chokes off the supply of new data. Even if the source is a data block in the firmware or the read-only filing system, only a limited amount (currently about 3 kB) can exist in the application address space at one time.

通过使用source API,我们可以得到1个最大3kB的内存窗口。利用这个窗口,可以实现bin文件一边读取一边解析,参考代码如下:
/* find 2557.bin file in file system */
file = FileFind(FILE_ROOT, file_name, strlen(file_name));
if (file == FILE_NONE)
{
	DEBUG_2557_ERR(("2557: Can't find bin file: %s\n",file_name));
	Panic();
}

pTAS2557->source = StreamFileSource(file);
if (pTAS2557->source == NULL)
{
	DEBUG_2557_ERR(("2557: Can't find stream source\n"));
	Panic();   
}

file_len = SourceSize(pTAS2557->source);
pData = (uint8_t *)SourceMap(pTAS2557->source);
pDataStart = pData;
    
/* PLL Name */
pDataBuf = malloc(64);
memcpy(pDataBuf, pData, 64);
DEBUG_2557(("2557: PLL Name = %s\n", pDataBuf));
free(pDataBuf);
pData += 64;

/* PLL Description */
n = strlen((const char *)pData);
pDataBuf = malloc(n+1);
memcpy(pDataBuf, pData, n+1);
DEBUG_2557(("2557: PLL Description = %s\n", pDataBuf));
free(pDataBuf);
pData += n+1;

SourceDrop(pTAS2557->source, (pData - pDataStart));
程序最后调用SourceDrop将已使用的数据段丢弃,在此调用SourceMap时,返回的指针指向下一个未使用的数据。循环往复得调用SourceMap和SourceDrop,并且正确丢弃已用数据,即可实现大于3KB的bin文件的连续处理了。

##3.3. 卸载Flash分区
读取完Flash分区后,即可卸载Flash分区。

if (!PartitionUnmountFilesystem(PARTITION_SERIAL_FLASH, MOUNT_FILE_SYSTEM_SECTION_INDEX))
{
	DEBUG_2557_ERR(("2557: Unmount file system fail\n"));
	Panic();   
}
#4. 从内部Flash读取文件
其实CSR8675内部的Flash也可以存储bin文件,方法是将bin文件放在工程的image路径下,工程在编译时会自动打包这个文件。VM可在运行时直接通过source API调用,无需加载外部Flash分区。

这种方法的缺点是导致image文件变大,增大了DFU文件,使OTA升级耗时变长。

也许有人会问,为什么不将bin文件以.h文件的形式保存在CSR8675的程序段的const区呢?因为CSR8675的const区只有24KB,无法存放40KB这样的bin文件。并且考虑到const区一般存储一些常用的数据表和debug打印字符串,留给bin文件的空间更少了。参考《VM Memory Mapping and Memory Usage》:

2.1.3 Constants region
The constants region maps to ROM/Flash memory used to store constant data that is required to persist for the duration of the program.
The constants region is limited to a maximum of 24 K words and is used for:
■ Constant global variables
■ String literals e.g. “Hello world”
■ Jump tables for switch statements
■ Initializers for non-constant global variables
The linker produces an error message if the 24 K limit is exceeded.

#5. 总结

在掌握了从外部Flash读取文件的方法后,可以实现很多有意义的功能。包括boot外部DSP,读取电话簿,播放特定音频文件等。

#6. 参考文档

CS-227835-AN-4-Using external FLASH on ADK.pdf
nvscmd.chm
Implementing sources in bluecore applications
ADK Audio Prompts Application Note
VM Memory Mapping and Memory Usage

来源:https://blog.csdn.net/wzz4420381/article/details/79179603
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
1

路过

雷人

握手

鲜花

鸡蛋

刚表态过的朋友 (1 人)

相关阅读

发表评论

最新评论

引用 Realmejt 2021-8-18 16:55
牛逼
引用 音魅蓝牙测试仪 2021-6-3 16:16
不错

查看全部评论(2)

小黑屋|手机版|我爱蓝牙网 - 52Bluetooth

GMT+8, 2024-4-27 23:33 , Processed in 0.203836 second(s), 33 queries , Gzip On, MemCached On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

返回顶部