AnDawn 发表于 2022-1-23 23:13

【BG22-EK4108A 蓝牙开发套件】 一 、信息互传失败+实现bg22ncp写入

<p>信息互传失败原因WCH</p>

<p>没有调通</p>

<p>通过官方例程还是无法读取不清楚为啥,</p>

<p>换了块板子还是不行具体情况以后再分析吧</p>

<p></p>

<p>/********************************** (C) COPYRIGHT *******************************<br />
&nbsp;* File Name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: EXAM11.C<br />
&nbsp;* Author &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : WCH<br />
&nbsp;* Version &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: V1.0<br />
&nbsp;* Date &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : 2020/08/11<br />
&nbsp;* Description &nbsp; &nbsp; &nbsp; &nbsp;: CH573 C语言的U盘目录文件枚举程序<br />
&nbsp;支持: FAT12/FAT16/FAT32<br />
&nbsp;注意包含 CHRV3UFI.LIB/USBHOST.C/DEBUG.C<br />
&nbsp;*******************************************************************************/</p>

<p>/** 不使用U盘文件系统库,需要在工程属性预编译中修改 DISK_LIB_ENABLE=0 &nbsp; &nbsp; &nbsp; &nbsp;*/<br />
/** U盘挂载USBhub下面,需要在工程属性预编译中修改 DISK_WITHOUT_USB_HUB=0 &nbsp;*/</p>

<p>#include &quot;CH58x_common.h&quot;<br />
#include &quot;CHRV3UFI.H&quot;</p>

<p>__attribute__((aligned(4))) &nbsp; UINT8 RxBuffer; &nbsp; &nbsp;// IN, must even address<br />
__attribute__((aligned(4))) &nbsp; UINT8 TxBuffer; &nbsp; &nbsp;// OUT, must even address</p>

<p>/* 检查操作状态,如果错误则显示错误代码并停机 */<br />
void mStopIfError( UINT8 iError )<br />
{<br />
&nbsp; if ( iError == ERR_SUCCESS )<br />
&nbsp; {<br />
&nbsp; &nbsp; return; /* 操作成功 */<br />
&nbsp; }<br />
&nbsp; printf( &quot;Error: %02X\n&quot;, ( UINT16 ) iError ); /* 显示错误 */<br />
&nbsp; /* 遇到错误后,应该分析错误码以及CHRV3DiskStatus状态,例如调用CHRV3DiskReady查询当前U盘是否连接,如果U盘已断开那么就重新等待U盘插上再操作,<br />
&nbsp; &nbsp;建议出错后的处理步骤:<br />
&nbsp; &nbsp;1、调用一次CHRV3DiskReady,成功则继续操作,例如Open,Read/Write等<br />
&nbsp; &nbsp;2、如果CHRV3DiskReady不成功,那么强行将从头开始操作(等待U盘连接,CHRV3DiskReady等) */<br />
&nbsp; while( 1 )<br />
&nbsp; {<br />
&nbsp; }<br />
}</p>

<p>int main()<br />
{<br />
&nbsp; UINT8 s, i;<br />
&nbsp; PUINT8 pCodeStr;<br />
&nbsp; UINT16 j;</p>

<p>&nbsp; SetSysClock( CLK_SOURCE_PLL_60MHz );</p>

<p>&nbsp; GPIOA_SetBits( GPIO_Pin_9 );<br />
&nbsp; GPIOA_ModeCfg( GPIO_Pin_8, GPIO_ModeIN_PU );<br />
&nbsp; GPIOA_ModeCfg( GPIO_Pin_9, GPIO_ModeOut_PP_5mA );<br />
&nbsp; UART1_DefInit();<br />
&nbsp; PRINT( &quot;Start @ChipID=%02X \n&quot;, R8_CHIP_ID );</p>

<p>&nbsp; pHOST_RX_RAM_Addr = RxBuffer;<br />
&nbsp; pHOST_TX_RAM_Addr = TxBuffer;<br />
&nbsp; USB_HostInit();<br />
&nbsp; CHRV3LibInit(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //初始化U盘程序库以支持U盘文件</p>

<p>&nbsp; FoundNewDev = 0;<br />
&nbsp; printf( &quot;Wait Device In\n&quot; );<br />
&nbsp; while( 1 )<br />
&nbsp; {<br />
&nbsp; &nbsp; s = ERR_SUCCESS;<br />
&nbsp; &nbsp; if ( R8_USB_INT_FG &amp; RB_UIF_DETECT ) &nbsp; &nbsp;// 如果有USB主机检测中断则处理<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; R8_USB_INT_FG = RB_UIF_DETECT; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 清连接中断标志<br />
&nbsp; &nbsp; &nbsp; s = AnalyzeRootHub(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 分析ROOT-HUB状态<br />
&nbsp; &nbsp; &nbsp; if ( s == ERR_USB_CONNECT )<br />
&nbsp; &nbsp; &nbsp; &nbsp; FoundNewDev = 1;<br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp;&nbsp;<br />
&nbsp; &nbsp; if ( FoundNewDev || s == ERR_USB_CONNECT )<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; // 有新的USB设备插入<br />
&nbsp; &nbsp; &nbsp; FoundNewDev = 0;<br />
&nbsp; &nbsp; &nbsp; mDelaymS( 200 ); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 由于USB设备刚插入尚未稳定,故等待USB设备数百毫秒,消除插拔抖动<br />
&nbsp; &nbsp; &nbsp; s = InitRootDevice(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 初始化USB设备<br />
&nbsp; &nbsp; &nbsp; if ( s == ERR_SUCCESS )<br />
&nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; printf( &quot;Start UDISK_demo @CHRV3UFI library\n&quot; );<br />
&nbsp; &nbsp; &nbsp; &nbsp; // U盘操作流程:USB总线复位、U盘连接、获取设备描述符和设置USB地址、可选的获取配置描述符,之后到达此处,由CHRV3子程序库继续完成后续工作<br />
&nbsp; &nbsp; &nbsp; &nbsp; CHRV3DiskStatus = DISK_USB_ADDR;<br />
&nbsp; &nbsp; &nbsp; &nbsp; for ( i = 0; i != 10; i++ )<br />
&nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf( &quot;Wait DiskReady\n&quot; );<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s = CHRV3DiskReady();<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( s == ERR_SUCCESS )<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mDelaymS( 50 );<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; if ( CHRV3DiskStatus &gt;= DISK_MOUNTED ) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //U盘准备好<br />
&nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* 读文件 */<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf( &quot;Open\n&quot; );<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strcpy( ( PCHAR ) mCmdParam.Open.mPathName, &quot;/C51/CHRV3HFT.C&quot; ); &nbsp; &nbsp; &nbsp; &nbsp; //设置要操作的文件名和路径<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s = CHRV3FileOpen(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//打开文件<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( s == ERR_MISS_DIR )<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf( &quot;不存在该文件夹则列出根目录所有文件\n&quot; );<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pCodeStr = ( PUINT8 ) &quot;/*&quot;;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pCodeStr = ( PUINT8 ) &quot;/C51/*&quot;; &nbsp; &nbsp;//列出\C51子目录下的的文件<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p>

<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf( &quot;List file %s\n&quot;, pCodeStr );<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for ( j = 0; j &lt; 10000; j++ ) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //限定10000个文件,实际上没有限制<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strcpy( ( PCHAR ) mCmdParam.Open.mPathName, ( PCCHAR ) pCodeStr ); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//搜索文件名,*为通配符,适用于所有文件或者子目录<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i = strlen( ( PCHAR ) mCmdParam.Open.mPathName );<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mCmdParam.Open.mPathName = 0xFF; &nbsp; &nbsp; &nbsp; &nbsp;//根据字符串长度将结束符替换为搜索的序号,从0到254,如果是0xFF即255则说明搜索序号在CHRV3vFileSize变量中<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CHRV3vFileSize = j; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//指定搜索/枚举的序号<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i = CHRV3FileOpen(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//打开文件,如果文件名中含有通配符*,则为搜索文件而不打开<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* CHRV3FileEnum 与 CHRV3FileOpen 的唯一区别是当后者返回ERR_FOUND_NAME时那么对应于前者返回ERR_SUCCESS */<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( i == ERR_MISS_FILE )<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break; &nbsp; &nbsp;//再也搜索不到匹配的文件,已经没有匹配的文件名<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( i == ERR_FOUND_NAME ) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //搜索到与通配符相匹配的文件名,文件名及其完整路径在命令缓冲区中<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf( &quot; &nbsp;match file %04d#: %s\n&quot;, ( unsigned int ) j, mCmdParam.Open.mPathName ); /* 显示序号和搜索到的匹配文件名或者子目录名 */<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue; /* 继续搜索下一个匹配的文件名,下次搜索时序号会加1 */<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //出错<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mStopIfError( i );<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i = CHRV3FileClose(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//关闭文件<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf( &quot;U盘演示完成\n&quot; );<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; &nbsp; else<br />
&nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf( &quot;U盘没有准备好 ERR =%02X\n&quot;, ( UINT16 ) s );<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; else<br />
&nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; printf( &quot;初始化U盘失败,请拔下U盘重试\n&quot; );<br />
&nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; mDelaymS( 100 ); &nbsp; &nbsp;// 模拟单片机做其它事<br />
&nbsp; &nbsp; SetUsbSpeed( 1 ); &nbsp; &nbsp;// 默认为全速<br />
&nbsp; }<br />
}</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>实现bg22写入</p>

<p>/***************************************************************************//**<br />
&nbsp;* @file<br />
&nbsp;* @brief main() function.<br />
&nbsp;*******************************************************************************<br />
&nbsp;* # License<br />
&nbsp;* &lt;b&gt;Copyright 2020 Silicon Laboratories Inc. www.silabs.com&lt;/b&gt;<br />
&nbsp;*******************************************************************************<br />
&nbsp;*<br />
&nbsp;* SPDX-License-Identifier: Zlib<br />
&nbsp;*<br />
&nbsp;* The licensor of this software is Silicon Laboratories Inc.<br />
&nbsp;*<br />
&nbsp;* This software is provided &#39;as-is&#39;, without any express or implied<br />
&nbsp;* warranty. In no event will the authors be held liable for any damages<br />
&nbsp;* arising from the use of this software.<br />
&nbsp;*<br />
&nbsp;* Permission is granted to anyone to use this software for any purpose,<br />
&nbsp;* including commercial applications, and to alter it and redistribute it<br />
&nbsp;* freely, subject to the following restrictions:<br />
&nbsp;*<br />
&nbsp;* 1. The origin of this software must not be misrepresented; you must not<br />
&nbsp;* &nbsp; &nbsp;claim that you wrote the original software. If you use this software<br />
&nbsp;* &nbsp; &nbsp;in a product, an acknowledgment in the product documentation would be<br />
&nbsp;* &nbsp; &nbsp;appreciated but is not required.<br />
&nbsp;* 2. Altered source versions must be plainly marked as such, and must not be<br />
&nbsp;* &nbsp; &nbsp;misrepresented as being the original software.<br />
&nbsp;* 3. This notice may not be removed or altered from any source distribution.<br />
&nbsp;*<br />
&nbsp;******************************************************************************/<br />
#include &quot;sl_component_catalog.h&quot;<br />
#include &quot;sl_system_init.h&quot;<br />
#include &quot;app.h&quot;<br />
#if defined(SL_CATALOG_POWER_MANAGER_PRESENT)<br />
#include &quot;sl_power_manager.h&quot;<br />
#endif // SL_CATALOG_POWER_MANAGER_PRESENT<br />
#if defined(SL_CATALOG_KERNEL_PRESENT)<br />
#include &quot;sl_system_kernel.h&quot;<br />
#else // SL_CATALOG_KERNEL_PRESENT<br />
#include &quot;sl_system_process_action.h&quot;<br />
#endif // SL_CATALOG_KERNEL_PRESENT</p>

<p>int main(void)<br />
{<br />
&nbsp; // Initialize Silicon Labs device, system, service(s) and protocol stack(s).<br />
&nbsp; // Note that if the kernel is present, processing task(s) will be created by<br />
&nbsp; // this call.<br />
&nbsp; sl_system_init();</p>

<p>&nbsp; // Initialize the application. For example, create periodic timer(s) or<br />
&nbsp; // task(s) if the kernel is present.<br />
&nbsp; app_init();</p>

<p>#if defined(SL_CATALOG_KERNEL_PRESENT)<br />
&nbsp; // Start the kernel. Task(s) created in app_init() will start running.<br />
&nbsp; sl_system_kernel_start();<br />
#else // SL_CATALOG_KERNEL_PRESENT<br />
&nbsp; while (1) {<br />
&nbsp; &nbsp; // Do not remove this call: Silicon Labs components process action routine<br />
&nbsp; &nbsp; // must be called from the super loop.<br />
&nbsp; &nbsp; sl_system_process_action();</p>

<p>&nbsp; &nbsp; // Application process.<br />
&nbsp; &nbsp; app_process_action();</p>

<p>#if defined(SL_CATALOG_POWER_MANAGER_PRESENT)<br />
&nbsp; &nbsp; // Let the CPU go to sleep if the system allows it.<br />
&nbsp; &nbsp; sl_power_manager_sleep();<br />
#endif<br />
&nbsp; }<br />
#endif // SL_CATALOG_KERNEL_PRESENT<br />
}</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>BG22ncp写入名称问题为啥名称后面老是加Example郁闷/?????</p>

<p></p>

<p>有知道为啥后面多名称的可以留言给我,谢谢啦!!!</p>
页: [1]
查看完整版本: 【BG22-EK4108A 蓝牙开发套件】 一 、信息互传失败+实现bg22ncp写入