支持 CP210X的USB Host 库-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1412|回复: 1

支持 CP210X的USB Host 库

[复制链接]
发表于 2021-11-20 20:14 | 显示全部楼层 |阅读模式
来自 USB_Host_Shield_Library_2.0-CP210x

USB_Host_Shield_Library_2.0-CP210x.zip (399.03 KB, 下载次数: 2)

我在 ESP32 上测试过其中的 Loopback 工作正常

  1. #include <CP210x.h>
  2. #include <usbhub.h>

  3. #include "pgmstrings.h"
  4. #include <SPI.h>

  5. class CP210xAsync : public CP210xAsyncOper
  6. {
  7.   public:
  8.     uint8_t OnInit(CP210x *pftdi);
  9. };

  10. uint8_t CP210xAsync::OnInit(CP210x *cp210x)
  11. {
  12.   uint8_t rcode = 0;
  13.   rcode = cp210x->IFCEnable();

  14.   if (rcode)
  15.   {
  16.     return rcode;
  17.   }

  18.   rcode = cp210x->SetBaudRate(115200);

  19.   if (rcode)
  20.   {
  21.     return rcode;
  22.   }

  23.   rcode = cp210x->SetDataBits(8);
  24.   if (rcode)
  25.   {
  26.     return rcode;
  27.   }

  28.   rcode = cp210x->SetStopBits(CP210X_STOP_BITS_1);
  29.   if (rcode)
  30.   {
  31.     return rcode;
  32.   }

  33.   rcode = cp210x->SetParity(CP210X_PARITY_NONE);
  34.   if (rcode)
  35.   {
  36.     return rcode;
  37.   }

  38.   rcode = cp210x->SetFlowControl(CP210X_FLOW_CONTROL_OFF);
  39.   if (rcode)
  40.   {
  41.     return rcode;
  42.   }


  43.   return rcode;
  44. }

  45. USB Usb;
  46. //USBHub Hub(&Usb);
  47. CP210xAsync cp210xAsync;
  48. CP210x cp210x(&Usb, &cp210xAsync);

  49. void setup()
  50. {
  51.   Serial.begin( 115200 );
  52. #if !defined(MIPSEL)
  53.   while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
  54. #endif
  55.   Serial.println("Start");

  56.   if (Usb.Init() == -1)
  57.     Serial.println("OSC did not start.");

  58.   delay( 200 );
  59. }

  60. void loop()
  61. {
  62.   Usb.Task();

  63.   if ( Usb.getUsbTaskState() == USB_STATE_RUNNING )
  64.   {
  65.     uint8_t  rcode;
  66.     char strbuf[] = "DEADBEEF";
  67.     //char strbuf[] = "The quick brown fox jumps over the lazy dog";
  68.     //char strbuf[] = "This string contains 61 character to demonstrate FTDI buffers"; //add one symbol to it to see some garbage
  69.     //Serial.print(".");

  70.     rcode = cp210x.SndData(strlen(strbuf), (uint8_t*)strbuf);

  71.     if (rcode)
  72.       ErrorMessage<uint8_t>(PSTR("SndData"), rcode);

  73.     delay(50);

  74.     uint8_t  buf[64];

  75.     for (uint8_t i = 0; i < 64; i++)
  76.       buf[i] = 0;

  77.     uint16_t rcvd = 64;
  78.     rcode = cp210x.RcvData(&rcvd, buf);

  79.     if (rcode && rcode != hrNAK)
  80.       ErrorMessage<uint8_t>(PSTR("Ret"), rcode);

  81.     // The device reserves the first two bytes of data
  82.     //   to contain the current values of the modem and line status registers.
  83.     if (rcvd > 2)
  84.       Serial.print((char*)(buf));

  85.     delay(10);
  86.   }
  87. }
复制代码


 楼主| 发表于 2021-11-20 20:23 | 显示全部楼层
774667738.jpg

使用的是为 FireBeetle设计的 USB Host 板,具体在下面可以看到

https://mc.dfrobot.com.cn/thread-309553-1-1.html
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-11-28 09:26 , Processed in 0.077833 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表