为了让CSR867x的开发更容易,现与思度科技联合推出CSR867x学习板【淘宝链接:思度科技CSR开发板】。
技术交流QQ群号:743434463
开发板会员QQ群号:725398389(凭订单号入群,赠PPT、项目源码、视频教程)
——————————正文分割线———————————–
#1. DFU简介
##2.1. 详细执行过程
这个章节列出了在使用DFU机制时的选择和限制:
##2.2. DFU升级工具
DFU升级工具有两种:
##2.3. PS Keys
PS Keys的类型有很多种:
##2.4. DFU事件序列
DFU事件序列如下:
#3. 生成DFU文件
官方手册的步骤比较详细,但每次生成代码需要手动操作。我写了一个批处理文件,实现了编译、烧录、生成DFU、生成release文件一键操作。
##3.1. 设置CSR安装路径
:: set CSR install path
set dfutoolspath="C:\ADK4.0.0\tools\bin"
set adkpath="C:\ADK4.0.0"
set debugtransport=SPITRANS=USB SPIPORT=0
set adkversion=adk4.0.0
这里的debugtransport指向USB-SPI调试器,在执行批处理文件过程中,确保PC与编程器已经连接。
##3.2. 设置全局变量
:: set project path
set ReleasePath=released
set ReleaseVersion=0.0.13
set ReleasePackageName=BT%ReleaseVersion%
set ReleaseNoteName=ReleaseNote_BT%ReleaseVersion%
set projectpath=parasol_v%ReleaseVersion%
set projectmakefile=Speaker.parasol_released
set projectname=speaker
set projectpsr1=sink_system_csr8670
set projectpsr2=Speaker_with_TWS_CNS10001v4
set projectpsr3=Music5_bt_%ReleaseVersion%
set dfupsrfw=example_DFU_fw_Music5_bt_%ReleaseVersion%
set dfupsrvm=example_DFU_vm_Music5_bt_%ReleaseVersion%
projectmakefile需要与工程的编译文件对应。
##3.3. 创建release文件夹
:: create package dir
rd /s /Q %ReleasePackageName%
mkdir %ReleasePackageName%
mkdir %ReleasePackageName%\"BT_Program file"
mkdir %ReleasePackageName%\"BT_Source code"
mkdir %ReleasePackageName%\"BT_Upgrade file"
##3.4. 更新调试接口
:: update debug transport in XIP files
cscript //NoLogo //B xip_set_transport.vbs %adkpath%\apps\%projectpath%\%projectname%.xip "[%debugtransport%]"
cscript //NoLogo //B xip_set_transport.vbs %adkpath%\kalimba\apps\a2dp_sink\sbc_decoder.xip "[%debugtransport%]"
cscript //NoLogo //B xip_set_transport.vbs %adkpath%\kalimba\apps\a2dp_sink\aptx_decoder.xip "[%debugtransport%]"
cscript //NoLogo //B xip_set_transport.vbs %adkpath%\kalimba\apps\a2dp_sink\aptx_acl_sprint_decoder.xip "[%debugtransport%]"
cscript //NoLogo //B xip_set_transport.vbs %adkpath%\kalimba\apps\a2dp_sink\aac_decoder.xip "[%debugtransport%]"
set origpath=%cd%
DSP工程的调试接口需要设置,否则编译时会报错找不到调试器。
##3.5. 编译DSP工程
echo. *** compile DSP app ***
cd /D %adkpath%\kalimba\apps\a2dp_sink
call %adkpath%\xide\bin\xipbuild.exe -f sbc_decoder.xip
cd /D %adkpath%\kalimba\apps\a2dp_sink
call %adkpath%\xide\bin\xipbuild.exe -f aptx_decoder.xip
cd /D %adkpath%\kalimba\apps\a2dp_sink
call %adkpath%\xide\bin\xipbuild.exe -f aptx_acl_sprint_decoder.xip
cd /D %adkpath%\kalimba\apps\a2dp_sink
call %adkpath%\xide\bin\xipbuild.exe -f aac_decoder.xip
每个VM工程包含一个主工程和多个DSP子工程。
##3.6. 复制psr文件
cd /D %origpath%
call %adkpath%\tools\bin\copyfile %adkpath%\apps\%projectpath%\configurations\%projectpsr3%.psr %ReleasePackageName%\BT_Upgrade file\%projectpsr3%.psr"
这里的psr3其实包含了三个部分:psr1、psr2、与项目有关的配置改动。
psr1和psr2是CSR默认的配置。
##3.7. 生成未加密的工厂烧录文件
cd /D %adkpath%\apps\%projectpath%
call %adkpath%\xide\bin\xipbuild.exe -f %projectname%.xip
echo. *** erase board ***
call %adkpath%\tools\bin\BlueFlashCmd.exe -TRANS "%debugtransport%" erase
echo. *** delete the image directory before copying files into it ***
echo. *** this will also flash the unsigned image onto the board
echo. *** prevent voice file from delect ***
call rmdir /S /Q headers_temp
call rmdir /S /Q prompts_temp
call rmdir /S /Q refname_temp
del /f /s /Q audio_prompt_config_temp.csr
mkdir headers_temp
mkdir prompts_temp
mkdir refname_temp
xcopy /E image\headers headers_temp
xcopy /E image\prompts prompts_temp
xcopy /E image\refname refname_temp
copy /b image\audio_prompt_config.csr audio_prompt_config_temp.csr
call rmdir /S /Q image
mkdir image
xcopy /E headers_temp "image\headers\"
xcopy /E prompts_temp "image\prompts\"
xcopy /E refname_temp "image\refname\"
copy /b audio_prompt_config_temp.csr "image\audio_prompt_config.csr"
call rmdir /S /Q headers_temp
call rmdir /S /Q prompts_temp
call rmdir /S /Q refname_temp
del /f /s /Q audio_prompt_config_temp.csr
%adkpath%\tools\bin\make -R BLUELAB=%adkpath%\tools -f %projectmakefile%.mak flash
cd /D %origpath%
echo. *** merge psr file to board ***
call %adkpath%\tools\bin\pscli.exe -TRANS "%debugtransport%" merge "%ReleasePackageName%\BT_Upgrade file\%projectpsr3%.psr"
这里完成了4件事:
在执行第3部之前,需要首先删除image文件夹。image文件夹的截图如下:
image文件夹由如下部分组成:
headers文件夹内的文件数量与pskey中设定的语音提示音数量是对应的。
语音提示音文件夹是通过sink configuration tool生成的,然后通过编译工程来添加到最终生成的image.fs文件里。所以在删除image文件夹之前需要把相关内容缓存一下,并在生成image之前再次放回到image文件夹。
把image和psr一起烧录进目标板后,就可以读出完整的工厂烧录文件。
echo. *** cold reset ***
call %adkpath%\tools\bin\pscli.exe -TRANS "%debugtransport%" cold_reset
echo. *** dump file from board ***
cd /D %origpath%\%ReleasePackageName%\"BT_Program file"
call %adkpath%\tools\bin\BlueFlashCmd.exe -TRANS "%debugtransport%" dump %projectpsr3%
##3.10. 压缩源码
echo. *** copy library file to BT_Source code ***
cd /D %origpath%
xcopy /E %adkversion% "%ReleasePackageName%\BT_Source code\BT_Source code\"
echo. *** copy source file to BT_Source code ***
xcopy /E %adkpath%\apps\%projectpath% "%ReleasePackageName%\BT_Source code\BT_Source code\Parasol_TWS_Code\"
echo. *** compress code ***
call "C:\Program Files\WinRAR\rar.exe" a -ep1 -o+ -ibck %ReleasePackageName%\"BT_Source code"\BT_SourceCode.rar "%ReleasePackageName%
\BT_Source code\BT_Source code"
rd /s /Q "%ReleasePackageName%\BT_Source code\BT_Source code\"
这里首先把有修改的库文件和源码一起拷贝到目标文件夹下,然后调用了WinRAR提供的压缩程序用来生成压缩文件。
##3.11. 生成已加密的image文件
echo. *** generate keys ***
cd /D %origpath%\dfu_key
copy /b keys.private.key %origpath%\%ReleasePackageName%\"BT_Upgrade file"\keys.private.key
copy /b keys.public.key %origpath%\%ReleasePackageName%\"BT_Upgrade file"\keys.public.key
cd /D %origpath%\%ReleasePackageName%\"BT_Upgrade file"
:: call %dfutoolspath%\dfukeygenerate.exe -o keys
echo. *** sign firmware ***
copy /b %adkpath%\firmware\assisted\unified\gordon\loader_unsigned.xdv loader_unsigned.xdv
copy /b %adkpath%\firmware\assisted\unified\gordon\loader_unsigned.xpv loader_unsigned.xpv
copy /b %adkpath%\firmware\assisted\unified\gordon\stack_unsigned.xdv stack_unsigned.xdv
copy /b %adkpath%\firmware\assisted\unified\gordon\stack_unsigned.xpv stack_unsigned.xpv
call %dfutoolspath%\dfukeyinsert -v -o loader_signed -l loader_unsigned.xdv -ks keys.public.key
call %dfutoolspath%\dfusign -v -o stack_signed -s stack_unsigned.xpv -ks keys.private.key
echo. *** copy image.fs to BT_Upgrade file ***
call %adkpath%\tools\bin\copyfile %adkpath%\apps\%projectpath%\image.fs app.fs
echo. *** sign app ***
call %dfutoolspath%\dfukeyinsert -v -o image_signed -ps %projectpsr3%.psr -ka keys.public.key
call %dfutoolspath%\dfusign -v -o image_signed -h app.fs -ka keys.private.key
echo. *** build binary ***
call %adkpath%\tools\bin\vmbuilder -size 16064 merge.xpv stack_signed.xpv image_signed.fs
上述代码完成了如下几件事:
##3.12. 生成已加密的工厂烧录文件
echo. *** merge signed xpv file to board ***
call %adkpath%\tools\bin\BlueFlashCmd.exe -TRANS "%debugtransport%" erase
call %adkpath%\tools\bin\BlueFlashCmd.exe -TRANS "%debugtransport%" merge
echo. *** merge signed psr file to board ***
call %adkpath%\tools\bin\pscli.exe -TRANS "%debugtransport%" merge image_signed.psr
echo. *** cold reset ***
call %adkpath%\tools\bin\pscli.exe -TRANS "%debugtransport%" cold_reset
echo. *** dump signed file from board ***
cd /D %origpath%\%ReleasePackageName%\"BT_Program file"
call %adkpath%\tools\bin\BlueFlashCmd.exe -TRANS "%debugtransport%" dump %projectpsr3%_signed
如果设备出厂时烧录的是已加密的烧录文件,后续DFU升级就可以更改被保护的PS Keys,否则只能更改未被保护的PS Keys。
##3.13. 生成DFU文件
echo. *** sign PSKEYs to be included in the DFU image ***
cd /D %origpath%\%ReleasePackageName%\"BT_Upgrade file"
copy %origpath%\%dfupsrvm%.psr app_vm.psr
copy %origpath%\%dfupsrfw%.psr app_fw.psr
call %dfutoolspath%\dfusign -v -o dfu_vm_signed -pa app_vm.psr -ka keys.private.key
call %dfutoolspath%\dfusign -v -o dfu_fw_signed -ps app_fw.psr -ks keys.private.key
echo. *** generate the DFU file ***
call %dfutoolspath%\dfubuild -v -pedantic -f %projectpsr3%.dfu -uv 0x0a12 -up 0x0001 -ui "ADK DFU" -s stack_signed.xpv -d
stack_signed.xdv -h image_signed.fs -p3 . dfu_fw_signed.stack.psr dfu_vm_signed.app.psr
echo. *** delete middle files ***
cd /D %origpath%\%ReleasePackageName%\"BT_Upgrade file"
del /f /s /Q dfu_fw_signed.stack.psr
del /f /s /Q dfu_vm_signed.app.psr
del /f /s /Q image_signed.fs
del /f /s /Q image_signed.psr
del /f /s /Q loader_signed.xdv
del /f /s /Q loader_unsigned.xdv
del /f /s /Q merge.xdv
del /f /s /Q stack_signed.xdv
del /f /s /Q stack_unsigned.xdv
del /f /s /Q loader_signed.xpv
del /f /s /Q loader_unsigned.xpv
del /f /s /Q merge.xpv
del /f /s /Q stack_signed.xpv
del /f /s /Q stack_unsigned.xpv
DFU文件由以下部分组成:
DFU文件中的psr会覆盖掉设备原有的psr。
##3.14. 打包release文件
echo. *** copy release note ***
copy %origpath%\%ReleaseNoteName%.xlsx %ReleaseNoteName%.xlsx
echo. *** package all files ***
cd /D %origpath%
call "C:\Program Files\WinRAR\rar.exe" a -ep1 -o+ -ibck %ReleasePackageName%.rar %ReleasePackageName%
#4. 总结
// --------MODE0--------------------------------------------------------
// BOOTMODE_KEY_LIST_0: Overwrite :
// HOST_INTRFACE (1F9)
// USB PRODUCT ID (2bf)
&04B0 = 01F9 02BF
// BOOTMODE_KEY_TABLE_0+0: PSKEY_HOST_INTERFACE = USB
&04B8 = 0002
// BOOTMODE_KEY_TABLE_0+1: USB PID = 0xffff (DFU)
&04B9 = ffff
// PSKEY_INITIAL_BOOTMODE
&03cd = 0001