灵动微课堂 (第274讲)|基于MM32F5333D7P的USB接口TinyUSB应用:Host设备的移植和U盘文件读写
<img src="https://mmbiz.qpic.cn/mmbiz_png/ua0uPM3vYPmXACd80kgknib8v57LLgHMVALJsm05QjnlSB7knJAWkiaIlwvh7bxYwiaBibXzstTxsfnL0LAibaca3vA/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"><p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">点击蓝字</span><span style="color: black;">添加“星标★”</span> 关注<span style="color: black;">咱们</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">1</span></strong></span><span style="color: black;">►</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">基本介绍</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">MM32F5330微<span style="color: black;">掌控</span>器搭载了由安谋科技授权的Armv8-M架构“星辰”STAR-MC1处理器,最高工作频率<span style="color: black;">达到</span>180MHz。内置了128KB Flash,32KB SRAM,并集<span style="color: black;">成为了</span>丰富的I/O端口和外设模块,<span style="color: black;">包含</span>ADC,DAC,模拟比较器,高级<span style="color: black;">按时</span>器,通用<span style="color: black;">按时</span>器,<span style="color: black;">基本</span><span style="color: black;">按时</span>器和低功耗<span style="color: black;">按时</span>器,还<span style="color: black;">包括</span>通信接口如I2C,I3C从机,SPI或I2S,UART,USART,低功耗UART,集<span style="color: black;">成为了</span>内部PHY的USB 2.0全速Device/Host<span style="color: black;">掌控</span>器,以及FlexCAN-FD接口。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">180MHz“星辰” STAR-MC1 处理器,内置单精度浮点运算单元 FPU,支持DSP 扩展</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">128KB Flash,32KB SRAM,集成丰富的 I/O 端口和外设模块</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">4 个UART,1 个 USART,1 个 LPUART</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1 个集成内部 PHY 的 USB 2.0</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1 个 FlexCAN-FD 接口</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2 个 I2C,1 个 I3C 从机,3 个 SPI 或 I2S</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2 个 3MSPS 12 位的 ADC, 1 个 12 位的 DAC</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2 个 16 位 Adv Timer, 6 个 GP Timer,1 个 LP Timer</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">工作电压:1.8V ~ 5.5V</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">工作温度范围:-40℃ ∼ +105℃</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">封装形式:LQFP64/48,QFN32</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">TinyUSB 是一个用于嵌入式系统的开源跨平台 USB 主机/设备协议栈,源码是托管在GitHub上面,<span style="color: black;">位置</span>是:<span style="color: black;">https://github.com/hathach/tinyusb</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">2</span></strong></span><span style="color: black;">►</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">TinyUSB Host基本移植介绍</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">之前<span style="color: black;">咱们</span>有讲过TinyUSB Device的移植,本期<span style="color: black;">咱们</span>就来介绍一下Host设备的移植。<span style="color: black;">一样</span>的<span style="color: black;">咱们</span>介绍的移植修改是基于TinyUSB examples\host下<span style="color: black;">已然</span>有参考示例的设备。将TinyUSB 从GitHub上克隆下来,example\host文件夹里面就有对应的示例,<span style="color: black;">这次</span><span style="color: black;">咱们</span>以msc_file_explorer为示例演示<span style="color: black;">怎样</span>移植一个host设备。</p><img src="https://mmbiz.qpic.cn/mmbiz_png/ua0uPM3vYPmXACd80kgknib8v57LLgHMVc0aqn5MjAP4PBIpy4PCWG2AVtibK7eu1b3ooPZMpbFZdOVkvdCiclwrw/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">图1 TinyUSB Host示例</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">将tinyusb 目录下examples\host\msc_file_explorer下的对应三个文件copy到例程TinyUSB_Host_MSC工程目录文件夹里面。</p><img src="https://mmbiz.qpic.cn/mmbiz_png/ua0uPM3vYPmXACd80kgknib8v57LLgHMVicJu5ZNZeAI2ic1pU84E9icXxfQgD3tBLAg1nicL9pSOcQulc4Oakb29XQ/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">图2 TinyUSB msc_file_explore文件</p><img src="https://mmbiz.qpic.cn/mmbiz_png/ua0uPM3vYPmXACd80kgknib8v57LLgHMVl2cRYyeZIJclcicia2dSSH8nd6c7h3XMvXfibLVt0XEAOFyT1Xic3bFqMQ/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">图3 TinyUSB_Host_MSC工程目录</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">MM32F5333有两个PLL,PLL1可以设置高主频180M给CPU和其他外设<span style="color: black;">运用</span>,PLL2<span style="color: black;">能够</span>单独配置给USB<span style="color: black;">运用</span>。<span style="color: black;">增多</span>一个USB时钟配置函数void USB_DeviceClockInit(void)和void SetPLL2ToUSB_HSE_96M(void),PLL2配置96M <span style="color: black;">选取</span>PLL2输入二分频到USB。有<span style="color: black;">运用</span>TU_LOG做串口输出,<span style="color: black;">能够</span>使能CFG_TUSB_DEBUG 为需要的输出等级,<span style="color: black;">同期</span>将#define tu_printf printf 改到串口输出,Keil Options->Target 勾选Use MicroLIB,并实现重定向函数。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">void SetPLL2ToUSB_HSE_96M(void)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">RCC->CR &= ~RCC_CR_PLL2ON_Msk;</p> <span style="color: black;">/* Reset PLL2SRC, PLL2PDIV, PLL2MUL, PLL2DIV bits */</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> RCC->PLL2CFGR &= ~RCC_PLL2CFGR_PLL2SRC_Msk;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">RCC->PLL2CFGR &= ~RCC_PLL2CFGR_PLL2PDIV_Msk;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> RCC->PLL2CFGR &= ~RCC_PLL2CFGR_PLL2MUL_Msk;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> RCC->PLL2CFGR &= ~RCC_PLL2CFGR_PLL2DIV_Msk;</p> <span style="color: black;">/* Config pll clock source*/</span>RCC->PLL2CFGR |= (<span style="color: black;">0x01</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">U << RCC_PLL2CFGR_PLL2SRC_Pos);</p> <span style="color: black;">/* set PLL2 CP Current Control Signals */</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> RCC->PLL2CFGR &= ~RCC_PLL2CFGR_PLL2ICTRL_Msk;</p> <span style="color: black;">if</span> (HSE_VALUE >= <span style="color: black;">8000000</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> {</p>RCC->PLL2CFGR |= (<span style="color: black;">0x03</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> << RCC_PLL2CFGR_PLL2ICTRL_Pos);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p> <span style="color: black;">else</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> {</p> RCC->PLL2CFGR |= (<span style="color: black;">0x01</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> << RCC_PLL2CFGR_PLL2ICTRL_Pos);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p> RCC->PLL2CFGR |= ((<span style="color: black;">0x17</span><< RCC_PLL2CFGR_PLL2MUL_Pos) | (<span style="color: black;">0x01</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> << RCC_PLL2CFGR_PLL2DIV_Pos));</p> <span style="color: black;">/* Enable PLL2 */</span> RCC->CR |= (<span style="color: black;">0x01</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">U << RCC_CR_PLL2ON_Pos);</p> <span style="color: black;">/* Wait till PLL2 is ready */</span> <span style="color: black;">while</span>((RCC->CR & RCC_CR_PLL2RDY_Msk) ==<span style="color: black;">0</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> {</p> __ASM(<span style="color: black;">"nop"</span>); <span style="color: black;">/* __NOP(); */</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">void</span> USB_DeviceClockInit(<span style="color: black;">void</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">) </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">/* Select USBCLK source */</span> RCC-><span style="color: black;">CFGR</span> |= <span style="color: black;">1</span> << <span style="color: black;">19</span>; <span style="color: black;">//USB CLK SEL PLL2</span>RCC-><span style="color: black;">CFGR</span> &= ~(<span style="color: black;">0x03</span> << <span style="color: black;">22</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p> RCC-><span style="color: black;">CFGR</span> |= <span style="color: black;">0x01</span> << <span style="color: black;">22</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> RCC_AHBPeriphClockCmd(RCC_AHBPeriph_USB_FS, ENABLE);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">添加tuh_hcd_port.c 接口函数文件。</p><img src="https://mmbiz.qpic.cn/mmbiz_png/ua0uPM3vYPmXACd80kgknib8v57LLgHMVonicE5nL1ibMeKYvIQ1awxhvg2JyV4sra2Bkgk26FDMIsYogF5w1hrqg/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">图4 Keil添加tuh_hcd_port.c文件</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">移植修改和之前Device设备基本流程一致,Keil工程按如下文件树添加对应文件。</p><img src="https://mmbiz.qpic.cn/mmbiz_png/ua0uPM3vYPmXACd80kgknib8v57LLgHMVJSMatrt3Ka8FanUQAZNFbHj3j3ZlGKGv9wq46rQ4ZWoLdibtAsZCqNg/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">图5 Keil工程添加对应.c文件</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">工程文件树如下:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1.TinyUSB_Host_MSC</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2. │</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3. ├─APP</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">4. │ main.c</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">5. │ mm32f5330_it.c</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">6. │ platform.c</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">7. │ msc_app.c</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">8. │ tinyusb_msc_file_explorer.c</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">9. │ tuh_hcd_port.c</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">10. │</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">11. ├─TinyUSB </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">12. │ tusb.c </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">13. │ tusb_fifo.c</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">14. │ hub.c</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">15. │ usbh.c</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">16. │ msc_host.c</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">17. │</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">18. └─FATFS</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">19. ff.c</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">20. ffsystem.c</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">21. ffunicode.c</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在tusb_config.h文件里面添加#define CFG_TUSB_MCU OPT_MCU_MM32F533X Tusb_mcu.h 文件里面<span style="color: black;">增多</span>:</p><span style="color: black;">//------------- MindMotion -------------//</span><span style="color: black;">#<span style="color: black;">elif</span>TU_CHECK_MCU(OPT_MCU_MM32F016X)</span> <span style="color: black;">#<span style="color: black;">define</span> TUP_DCD_ENDPOINT_MAX 8</span><span style="color: black;">#<span style="color: black;">elif</span> TU_CHECK_MCU(OPT_MCU_MM32F327X)</span> <span style="color: black;">#<span style="color: black;">define</span> TUP_DCD_ENDPOINT_MAX 16 </span><span style="color: black;">#<span style="color: black;">elif</span> TU_CHECK_MCU(OPT_MCU_MM32F533X)</span> <span style="color: black;">#<span style="color: black;">define</span>TUP_DCD_ENDPOINT_MAX 16</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在while(1)循环里面添加tuh_task(); <span style="color: black;">而后</span><span style="color: black;">处理</span>基本的编译问题后烧录板子<span style="color: black;">插进</span>U盘,将printf接上串口调试助手<span style="color: black;">就可</span>测试。</p><span style="color: black;"><span style="color: black;">void</span> <span style="color: black;">TinyUSB_MSC_File_Explorer_Sample</span><span style="color: black;">(<span style="color: black;">void</span>)</span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">printf</span>(<span style="color: black;">"\r\nTest %s \r\n"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">, __FUNCTION__);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> TinyUSB_Device_Configure();</p> <span style="color: black;">while</span> (<span style="color: black;">1</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> {</p> <span style="color: black;">// tinyusb host task</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> tuh_task();</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">插进</span>和拔出U盘都能看到”A MassStorage device is mounted”和”A MassStorage device is unmounted” 证明<span style="color: black;">已然</span>成功读取到U盘。</p><img src="https://mmbiz.qpic.cn/mmbiz_png/ua0uPM3vYPmXACd80kgknib8v57LLgHMVlEYRibRnDpAhyEWyeA1pq7enXHP1C8JicTJTh33qN5cOKm7iaQP0bDwmg/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">图6 <span style="color: black;">插进</span>和拔出U盘串口log打印</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">3</span></strong></span><span style="color: black;">►</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">U盘基本的文件读写测试</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">基于<span style="color: black;">以上</span>两点<span style="color: black;">咱们</span><span style="color: black;">已然</span>能<span style="color: black;">经过</span>USB设备识别到U盘了,<span style="color: black;">同期</span>TinyUSB lib\fatfs\source里面<span style="color: black;">亦</span><span style="color: black;">供给</span>了FatFs 文件操作的接口,下面<span style="color: black;">咱们</span>就基于这些文件操作接口函数对U盘里面的文件进行读写操作测试。</p><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">图7 ff.c FatFs 文件操作函数接口</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">首要</span>识别到U盘<span style="color: black;">咱们</span>先用f_mount 对U盘挂载,挂载成功之后<span style="color: black;">运用</span>f_getfree得到当前U盘空间等<span style="color: black;">关联</span>信息。</p> <span style="color: black;">printf</span>(<span style="color: black;">"test f_getfree:\r\n"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>fatfsCode = f_getfree((char const *)&driverNumberBuffer[<span style="color: black;">0</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">], (DWORD *)&freeClusterNumber, &fs);</p> <span style="color: black;">if</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> (fatfsCode)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> {</p> <span style="color: black;">printf</span>(<span style="color: black;">"error\r\n"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p> <span style="color: black;">return</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> false;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p> <span style="color: black;">if</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(fs->fs_type == FS_FAT12)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> {</p> <span style="color: black;">printf</span>(<span style="color: black;">" FAT type = FAT12\r\n"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p> <span style="color: black;">else</span> <span style="color: black;">if</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> (fs->fs_type == FS_FAT16)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> {</p> <span style="color: black;">printf</span>(<span style="color: black;">" FAT type = FAT16\r\n"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p> <span style="color: black;">else</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> {</p> <span style="color: black;">printf</span>(<span style="color: black;">" FAT type = FAT32\r\n"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p> <span style="color: black;">printf</span>(<span style="color: black;">" bytes per cluster = %d; number of clusters=%lu \r\n"</span>, fs->csize * <span style="color: black;">512</span>, fs->n_fatent - <span style="color: black;">2</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p> <span style="color: black;">printf</span>(<span style="color: black;">" The free size: %dKB, the total size:%dKB\r\n"</span>, (freeClusterNumber * (fs->csize) / <span style="color: black;">2</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">),</p> ((fs->n_fatent - <span style="color: black;">2</span>) * (fs->csize) / <span style="color: black;">2</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">));</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">按照</span><span style="color: black;">哪些</span>操作接口函数<span style="color: black;">咱们</span><span style="color: black;">能够</span>对U盘里面的文件进行读写操作,创建删除文件/文件夹,<span style="color: black;">插进</span>U盘后测试打印log如下:</p><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">图8 f_readdir 读取并列出U盘文件信息串口log打印</p><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">图9 U盘文件读写操作串口log打印</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">基于 MM32F5333D7P 的USB 接口 TinyUSB应用测试功能正常,测试在MM32F5330上能够实现对U盘里面的文件进行读写操作,创建删除文件/文件夹等操作。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">往期精彩</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">第273讲|<span style="color: black;">运用</span>MM32-Link MINI对M</span></a>M32G0001恢复出厂设置</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">第272讲|基于MM32F0140的I2C与24C02通信</span></a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">第271讲|基于MM32F0140的SPI与W25Q80通信</span></a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">第270讲|CLASS B内部时钟检测在MM32SPIN0280上的实现</span></a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">第269讲|MM32F5330内存<span style="color: black;">守护</span>单元(MPU)</span></a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">第268讲|MindSDK应用</span></a><span style="color: black;">基本</span>——ADC模块样例</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">第267讲|MindSDK应用<span style="color: black;">基本</span>——SPI模块样例</span></a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">第266讲|MM32F0163D7</span>P的ADC内部1.2V参考电压的<span style="color: black;">运用</span></a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">第265讲|基于MM32F0163D7P的USB Audio Class(UAC)音频设备</span></a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">第264讲|基于MM32F0163D7P的USB接口TinyUSB</span></a>应用:移植和新增设备(二)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">第263讲|基于MM32F0163D7P的USB接口TinyUSB应用:移植和新增设备(一)</span></a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">第262讲|基于MM32F0163D7P的I2S接口的音乐播放器实验</span></a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">第261讲|MindSDK应用<span style="color: black;">基本</span>——TIM模块样例</span></a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">第260讲|mm32-2nd-bootloader技术白皮书(</span></a>9)——OTA升级</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">第259讲|mm32-2nd-bootloader技术白皮书(8)——进阶:实现 Ymodem 更新代码</span></a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">第258讲|mm32-2nd-bootloader技术白皮书(7)——性能<span style="color: black;">评定</span></span></a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">第257讲|MM32F5270平台ADC注入通道的单周期采样的实现</span></a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">第256讲|MM32G0140 I</span></a>2C驱动EEPROM</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">第255讲|MM32SPIN0280内部时钟<span style="color: black;">捕捉</span><span style="color: black;">外边</span>HSE时</span>钟</a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">第254讲|Mini-F0160实现USB转3路虚拟串口</span></a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">第253讲|为MindSDK搭建Keil MDK<span style="color: black;">研发</span>环境</span></a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">第252讲|开源项目</span>:基于MM32F0160微<span style="color: black;">掌控</span>器的机械键盘</a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">第251讲|为MindSDK搭建armgcc</span></a>编译环境(Windows版)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">第250讲|mm32-2nd-bootloader技术白皮书(6)——总结及注</span>意事项</a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">第249讲|mm32-2nd-bootloader技术白皮书(5)——编译可在QSPI Flash上运行的程序</span></a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">第248讲|mm32-2nd-bootloader技术白皮书(4)——设计实现</span></a>简单的2nd bootloader</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">第247讲|mm32-2nd-bootloader技术白皮书(3)——设计实现QSPI Flash的下载算法</span></a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">第246讲|mm32-2nd-bootloader技术白皮书(2)——QSPI外设简介</span></a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">第245讲|mm32-2nd-bootloader技术白皮书(1)——配</span></a>置软硬件环境</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">第244讲|FlexCAN OTA</span></a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">第243讲|单独下载可执行文件到MM32F5微<span style="color: black;">掌控</span>器</span></a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">灵动微课堂大合辑</span></a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">公众号推送机制改版</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">感兴趣的动态频频“失联”?</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">简单两步点亮</span></strong><span style="color: black;"><strong style="color: blue;"><span style="color: black;">“星标★”</span></strong></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">再也不</span><span style="color: black;">错失</span>每一篇灵动动态!</span></strong></p><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">关于灵动</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">上海灵动微电子股份有限<span style="color: black;">机构</span>成立于 2011 年,是中国本土领先的通用 32 位 MCU <span style="color: black;">制品</span>及<span style="color: black;">处理</span><span style="color: black;">方法</span>供应商。<span style="color: black;">机构</span>基于 Arm Cortex-M 系列内核<span style="color: black;">研发</span>的 MM32 MCU <span style="color: black;">制品</span><span style="color: black;">持有</span> F/G/L/A/SPIN/W 六大系列,<span style="color: black;">日前</span>已量产近 300 款型号,累计交付超 4 亿颗,每年都有近亿台配备了灵动 MM32MCU 的优秀<span style="color: black;">制品</span>交付到客户手中,在本土通用 32 位 MCU <span style="color: black;">机构</span>中位居前列。</p><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">灵动客户涵盖智能工业、汽车电子、通信基建、医疗健康、智慧家电、物联网、个人设备、手机和电脑等应用<span style="color: black;">行业</span>。灵动是中国为数不多的<span style="color: black;">同期</span><span style="color: black;">得到</span>了 Arm-KEIL、IAR、SEGGER 官方支持的本土 MCU <span style="color: black;">机构</span>,并<span style="color: black;">创立</span>了独立、完整的通用 MCU 生态体系。灵动始终秉承着“诚信、承诺、创新、合作”的精神,为客户<span style="color: black;">供给</span>从硬件芯片到软件算法、从参考<span style="color: black;">方法</span>到系统设计的全方位支持。</p><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">灵动股份</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">微X</span>号:MindMotion-MMCU</p><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">长按识别二维码关注<span style="color: black;">咱们</span></strong></p><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">MORE</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">官网:<span style="color: black;">www.mm32mcu.com</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">微X</span>公众号:<span style="color: black;">灵动MM32MCU</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">灵动MM32MCU技术论坛:</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">bbs.21ic.com/iclist-696-1.html</span></strong></p>
请问、你好、求解、谁知道等。 “板凳”(第三个回帖的人) 对于这个问题,我有不同的看法... 软文发布平台 http://www.fok120.com/
页:
[1]