使用AT415 OTG实现MassStorage和CDC复合设备(AT32 MCU)

使用AT415 OTG实现MassStorage和CDC复合设备

/****************************************************************/
1.硬件环境AT32F415系列IC
硬件连接:例程中MassStorage设备通过SD卡实现,测试时需要飞线测试,具体连接关系如下:
PC8 ----->SDIO_D0 (外接SD卡)PC9 ----->SDIO_D1
PC10 ----->SDIO_D2
PC11 ----->SDIO_D3
PC12 ----->SDIO_SCK
PD2 ----->SDIO_CMD

2.软件环境
KEIL5、使用AT32 OTG库

3.验证
下载并运行,枚举成功后在PC端设备管理器可看到:
在这里插入图片描述
程序源码可参考如下路劲:
https://bbs.21ic.com/forum.php?mod=viewthread&tid=3064224&page=1&extra=#pid11780692

ESP32 S3是一款具有高性能,低功耗和可靠性的芯片。它可以轻松地与其他设备进行通信,并支持各种通信接口,例如UART,SPI,I2C,SDIO等。在本文中,我们将讨论ESP32 S3的OTG Mass Storage示例。 OTG(On-The-Go)是一种通信协议,可以使两个设备直接进行通信,而无需通过主机设备进行通信。在ESP32 S3中,OTG接口支持两种模式:设备模式和主机模式。在设备模式下,ESP32 S3可以作为一个设备连接到另一个主机设备上。在主机模式下,ESP32 S3可以作为主机设备连接到其他设备上。 Mass Storage是一种通信协议,可以将设备作为存储设备连接到计算机或其他设备上。在ESP32 S3中,我们可以使用OTG接口将ESP32 S3作为Mass Storage设备连接到计算机上,以便在计算机上读取和写入ESP32 S3的数据。 下面是ESP32 S3 OTG Mass Storage示例的代码: ```c #include <Arduino.h> #include <USBHost_t36.h> #include <SD.h> USBHost myusb; File file; void setup() { Serial.begin(115200); if (!SD.begin(4)) { Serial.println("SD card initialization failed!"); while (1); } Serial.println("SD card initialization done."); if (myusb.begin()) { Serial.println("USB Host Shield started."); } else { Serial.println("USB Host Shield failed to start."); while (1); } } void loop() { USBDevice connected_device = myusb.getConnectedDevice(); if (connected_device.devAddress()) { Serial.print("Device connected: "); Serial.println(connected_device.devAddress(), HEX); Serial.print("Vendor ID: "); Serial.println(connected_device.idVendor(), HEX); Serial.print("Product ID: "); Serial.println(connected_device.idProduct(), HEX); if (connected_device.idVendor() == 0x0781 && connected_device.idProduct() == 0x5581) { // SanDisk Cruzer Blade Serial.println("SanDisk Cruzer Blade detected."); if (myusb.setDeviceConfiguration(connected_device.devAddress(), 1)) { Serial.println("Device configuration set."); } else { Serial.println("Failed to set device configuration."); } if (myusb.setDeviceInterface(connected_device.devAddress(), 0, 0)) { Serial.println("Device interface set."); } else { Serial.println("Failed to set device interface."); } file = SD.open("/test.txt"); if (file) { Serial.println("File opened."); int file_size = file.size(); uint8_t *buf = new uint8_t[file_size]; file.read(buf, file_size); myusb.write(connected_device.devAddress(), 0x02, 0, buf, file_size); delete[] buf; Serial.println("Data written to device."); file.close(); } else { Serial.println("Failed to open file."); } } } delay(1000); } ``` 在上面的示例中,我们使用USBHost库和SD库。首先,我们在setup()函数中初始化SD卡和USB Host Shield。然后,在loop()函数中,我们使用myusb.getConnectedDevice()函数获取连接的设备,并检查它是否为我们所需的SanDisk Cruzer Blade。 如果检测到SanDisk Cruzer Blade,则使用myusb.setDeviceConfiguration()和myusb.setDeviceInterface()函数将设备配置为Mass Storage设备。然后,我们打开SD卡上的test.txt文件,并将其内容读取到一个缓冲区中。最后,我们使用myusb.write()函数将缓冲区中的数据写入连接的设备。 请注意,这只是一个简单的示例。在实际应用中,您可能需要处理更多的设备和文件类型,并且需要更完整的错误处理和调试功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值