Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

USBSerial_BigBuffer - T4.x (multi devices) This delta originated to do the work similar to what was done for the MIDI code base and support two versions of the object. All of the code was renamed to a new base class USBSerialBase which has everything, except the main buffer. Two sub-classes were made that the only difference is the size of the buffer to hold all of the USB RX and TX information. This started off to try to support an FT2232H device which transers 512 bytes at a time instead of what most of our devices were transfering which is 64 bytes. While doing this, I found that we have a similar issue with the T4.x boards which now transfer 512 bytes as well and our Serial object would not handle it. So there are two sublasses: ``` USBSerial - for those up to 64 byte transfers USBSerial_BigBuffer - for up to 512 bytes ``` Note: by default the biguffer version will only handle those devices > 64 bytes, but you can pass in an optional parameter of the minimum size, so doing something like: ``` USBSerial_BigBuffer userial(myusb, 1); ``` Will handle all up to 512. Also the FTDI2232H supports two USB to Serial objects, and our code was setup that in most cases including when we put in VID:PID pair in our table, the code would claim would claim the object at device level so only one USB Serial adapter. So changed table to support saying for this VID:PID claim at interface level. It now works, but not sure for the device I have as it is a USB to Serial and JTAG and not sure how to test 2nd one. But while doing this created a test sketch for Multiple Host USB Serial objects and the two are claimed by this. I also created a new version of the Teensy USBToSerial example sketch that if you configure USB type to Dual Serial or Triple Serial will output SerialUSB1 to Serial2 and SerialUSB2 to Serial3, which works, and then tried T4 configured for Dual plugged into T4.1 with above sketch and could talk to both USB Serial adapters. The Claim code was rearranged a lot, so hopefully did not break anything. But tested with T4 as mentioned, both Single and Dual Serial T3.2 with Single Serial, plus: Serial, Joystick, Keyboard... FTDI - USB to Serial adapter (Sparkfun) CP2104 - Adafruit PL2303 - some clones ... So hopefully most things are still working
4 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. // Simple test of USB Host Mouse/Keyboard
  2. //
  3. // This example is in the public domain
  4. #include "USBHost_t36.h"
  5. #define USBBAUD 115200
  6. uint32_t baud = USBBAUD;
  7. uint32_t format = USBHOST_SERIAL_8N1;
  8. USBHost myusb;
  9. USBHub hub1(myusb);
  10. USBHub hub2(myusb);
  11. USBHIDParser hid1(myusb);
  12. USBHIDParser hid2(myusb);
  13. USBHIDParser hid3(myusb);
  14. // There is now two versions of the USBSerial class, that are both derived from a common Base class
  15. // The difference is on how large of transfers that it can handle. This is controlled by
  16. // the device descriptor, where up to now we handled those up to 64 byte USB transfers.
  17. // But there are now new devices that support larger transfer like 512 bytes. This for example
  18. // includes the Teensy 4.x boards. For these we need the big buffer version.
  19. // uncomment one of the following defines for userial
  20. USBSerial userial(myusb); // works only for those Serial devices who transfer <=64 bytes (like T3.x, FTDI...)
  21. //USBSerial_BigBuffer userial(myusb, 1); // Handles anything up to 512 bytes
  22. //USBSerial_BigBuffer userial(myusb); // Handles up to 512 but by default only for those > 64 bytes
  23. USBDriver *drivers[] = {&hub1, &hub2, &hid1, &hid2, &hid3, &userial};
  24. #define CNT_DEVICES (sizeof(drivers)/sizeof(drivers[0]))
  25. const char * driver_names[CNT_DEVICES] = {"Hub1", "Hub2", "HID1", "HID2", "HID3", "USERIAL1" };
  26. bool driver_active[CNT_DEVICES] = {false, false, false, false};
  27. void setup()
  28. {
  29. pinMode(13, OUTPUT);
  30. pinMode(2, OUTPUT);
  31. pinMode(3, OUTPUT);
  32. for (int i = 0; i < 5; i++) {
  33. digitalWrite(2, HIGH);
  34. delayMicroseconds(50);
  35. digitalWrite(2, LOW);
  36. delayMicroseconds(50);
  37. }
  38. while (!Serial && (millis() < 5000)) ; // wait for Arduino Serial Monitor
  39. Serial.println("\n\nUSB Host Testing - Serial");
  40. myusb.begin();
  41. Serial1.begin(115200); // We will echo stuff Through Serial1...
  42. }
  43. void loop()
  44. {
  45. digitalWrite(13, !digitalRead(13));
  46. myusb.Task();
  47. // Print out information about different devices.
  48. for (uint8_t i = 0; i < CNT_DEVICES; i++) {
  49. if (*drivers[i] != driver_active[i]) {
  50. if (driver_active[i]) {
  51. Serial.printf("*** Device %s - disconnected ***\n", driver_names[i]);
  52. driver_active[i] = false;
  53. } else {
  54. Serial.printf("*** Device %s %x:%x - connected ***\n", driver_names[i], drivers[i]->idVendor(), drivers[i]->idProduct());
  55. driver_active[i] = true;
  56. const uint8_t *psz = drivers[i]->manufacturer();
  57. if (psz && *psz) Serial.printf(" manufacturer: %s\n", psz);
  58. psz = drivers[i]->product();
  59. if (psz && *psz) Serial.printf(" product: %s\n", psz);
  60. psz = drivers[i]->serialNumber();
  61. if (psz && *psz) Serial.printf(" Serial: %s\n", psz);
  62. // If this is a new Serial device.
  63. if (drivers[i] == &userial) {
  64. // Lets try first outputting something to our USerial to see if it will go out...
  65. userial.begin(baud);
  66. }
  67. }
  68. }
  69. }
  70. if (Serial.available()) {
  71. Serial.println("Serial Available");
  72. while (Serial.available()) {
  73. int ch = Serial.read();
  74. if (ch == '$') {
  75. BioloidTest();
  76. while (Serial.read() != -1);
  77. } else if (ch == '#') {
  78. // Lets see if we have a baud rate specified here...
  79. uint32_t new_baud = 0;
  80. for(;;) {
  81. ch = Serial.read();
  82. if ((ch < '0') || (ch > '9'))
  83. break;
  84. new_baud = new_baud*10 + ch - '0';
  85. }
  86. // See if the user is specifying a format: 8n1, 7e1, 7e2, 8n2
  87. // Note this is Quick and very dirty code...
  88. //
  89. if (ch == ',') {
  90. char command_line[10];
  91. ch = Serial.read();
  92. while (ch == ' ') Serial.read(); // ignore any spaces.
  93. uint8_t cb = 0;
  94. while ((ch > ' ') && (cb < sizeof(command_line))) {
  95. command_line[cb++] = ch;
  96. ch = Serial.read();
  97. }
  98. command_line[cb] = '\0';
  99. if (CompareStrings(command_line, "8N1")) format = USBHOST_SERIAL_8N1;
  100. else if (CompareStrings(command_line, "8N2")) format = USBHOST_SERIAL_8N2;
  101. else if (CompareStrings(command_line, "7E1")) format = USBHOST_SERIAL_7E1;
  102. else if (CompareStrings(command_line, "7O1")) format = USBHOST_SERIAL_7O1;
  103. }
  104. Serial.println("\n*** Set new Baud command ***\n do userial.end()");
  105. digitalWriteFast(2, HIGH);
  106. userial.end(); // Do the end statement;
  107. digitalWriteFast(2, LOW);
  108. if (new_baud) {
  109. baud = new_baud;
  110. Serial.print(" New Baud: ");
  111. Serial.println(baud);
  112. Serial.print(" Format: ");
  113. Serial.println(format, HEX);
  114. digitalWriteFast(3, HIGH);
  115. userial.begin(baud, format);
  116. digitalWriteFast(3, LOW);
  117. Serial.println(" Completed ");
  118. } else {
  119. Serial.println(" New Baud 0 - leave disabled");
  120. }
  121. while (Serial.read() != -1);
  122. } else {
  123. userial.write(ch);
  124. }
  125. }
  126. }
  127. while (Serial1.available()) {
  128. // Serial.println("Serial1 Available");
  129. Serial1.write(Serial1.read());
  130. }
  131. while (userial.available()) {
  132. // Serial.println("USerial Available");
  133. Serial.write(userial.read());
  134. }
  135. }
  136. bool CompareStrings(const char *sz1, const char *sz2) {
  137. while (*sz2 != 0) {
  138. if (toupper(*sz1) != toupper(*sz2))
  139. return false;
  140. sz1++;
  141. sz2++;
  142. }
  143. return true; // end of string so show as match
  144. }
  145. //#define ID_MASTER 200
  146. #define ID_MASTER 0xfd
  147. // Extract stuff from Bioloid library..
  148. #define AX12_BUFFER_SIZE 128
  149. #define COUNTER_TIMEOUT 12000
  150. /** Instruction Set **/ #define AX_PING 1
  151. #define AX_READ_DATA 2
  152. #define AX_WRITE_DATA 3
  153. #define AX_REG_WRITE 4
  154. #define AX_ACTION 5
  155. #define AX_RESET 6
  156. #define AX_SYNC_WRITE 131
  157. #define AX_TORQUE_ENABLE 24
  158. #define AX_LED 25
  159. #define AX_CW_COMPLIANCE_MARGIN 26
  160. #define AX_CCW_COMPLIANCE_MARGIN 27
  161. #define AX_CW_COMPLIANCE_SLOPE 28
  162. #define AX_CCW_COMPLIANCE_SLOPE 29
  163. #define AX_GOAL_POSITION_L 30
  164. #define AX_GOAL_POSITION_H 31
  165. #define AX_GOAL_SPEED_L 32
  166. #define AX_GOAL_SPEED_H 33
  167. #define AX_TORQUE_LIMIT_L 34
  168. #define AX_TORQUE_LIMIT_H 35
  169. #define AX_PRESENT_POSITION_L 36
  170. #define AX_PRESENT_POSITION_H 37
  171. void BioloidTest() {
  172. uint8_t master_id = 200;
  173. Serial.println("\n*** Bioloid Test ***");
  174. if (ax12GetRegister(master_id, 0, 1) != -1) {
  175. Serial.println("Controller found at 200");
  176. } else {
  177. Serial.println("Controller not at 200 try 0xfd");
  178. master_id = 0xfd;
  179. if (ax12GetRegister(master_id, 0, 1) != -1) {
  180. Serial.println("Controller found at 0xfd");
  181. } else {
  182. Serial.println("Controller not found");
  183. }
  184. }
  185. for (uint8_t reg = 0; reg < 10; reg++) {
  186. myusb.Task();
  187. Serial.print(ax12GetRegister(master_id, reg, 1), HEX);
  188. Serial.print(" ");
  189. }
  190. Serial.println();
  191. // Now assuming we found controller...
  192. // May need to turn on power on controller
  193. ax12SetRegister(master_id, AX_TORQUE_ENABLE, 1);
  194. delay(2);
  195. // Lets see if we can get the current position for any servo
  196. for (int i = 0; i < 254; i++) {
  197. int servo_pos = ax12GetRegister(i, AX_PRESENT_POSITION_L, 2);
  198. if (servo_pos != -1) {
  199. Serial.printf("Servo: %d Pos: %d\n", i, servo_pos);
  200. }
  201. }
  202. }
  203. unsigned char ax_rx_buffer[AX12_BUFFER_SIZE];
  204. int ax12GetRegister(int id, int regstart, int length) {
  205. // 0xFF 0xFF ID LENGTH INSTRUCTION PARAM... CHECKSUM
  206. int return_value;
  207. digitalWriteFast(2, HIGH);
  208. int checksum = ~((id + 6 + regstart + length) % 256);
  209. userial.write(0xFF);
  210. userial.write(0xFF);
  211. userial.write(id);
  212. userial.write(4); // length
  213. userial.write(AX_READ_DATA);
  214. userial.write(regstart);
  215. userial.write(length);
  216. userial.write(checksum);
  217. userial.flush(); // make sure the data goes out.
  218. if (ax12ReadPacket(length + 6) > 0) {
  219. // ax12Error = ax_rx_buffer[4];
  220. if (length == 1)
  221. return_value = ax_rx_buffer[5];
  222. else
  223. return_value = ax_rx_buffer[5] + (ax_rx_buffer[6] << 8);
  224. } else {
  225. digitalWriteFast(3, !digitalReadFast(3));
  226. return_value = -1;
  227. }
  228. digitalWriteFast(2, LOW);
  229. return return_value;
  230. }
  231. void ax12SetRegister(int id, int regstart, int data){
  232. int checksum = ~((id + 4 + AX_WRITE_DATA + regstart + (data&0xff)) % 256);
  233. userial.write(0xFF);
  234. userial.write(0xFF);
  235. userial.write(id);
  236. userial.write(4); // length
  237. userial.write(AX_WRITE_DATA);
  238. userial.write(regstart);
  239. userial.write(data&0xff);
  240. // checksum =
  241. userial.write(checksum);
  242. userial.flush();
  243. //ax12ReadPacket();
  244. }
  245. int ax12ReadPacket(int length) {
  246. unsigned long ulCounter;
  247. unsigned char offset, checksum;
  248. unsigned char *psz;
  249. unsigned char *pszEnd;
  250. int ch;
  251. offset = 0;
  252. psz = ax_rx_buffer;
  253. pszEnd = &ax_rx_buffer[length];
  254. while (userial.read() != -1) ;
  255. uint32_t ulStart = millis();
  256. // Need to wait for a character or a timeout...
  257. do {
  258. ulCounter = COUNTER_TIMEOUT;
  259. while ((ch = userial.read()) == -1) {
  260. if ((millis() - ulStart) > 10) {
  261. //if (!--ulCounter) {
  262. Serial.println("Timeout");
  263. return 0; // Timeout
  264. }
  265. }
  266. } while (ch != 0xff) ;
  267. *psz++ = 0xff;
  268. while (psz != pszEnd) {
  269. ulCounter = COUNTER_TIMEOUT;
  270. while ((ch = userial.read()) == -1) {
  271. //Serial.printf("Read ch: %x\n", ch);
  272. if (!--ulCounter) {
  273. return 0; // Timeout
  274. }
  275. }
  276. *psz++ = (unsigned char)ch;
  277. }
  278. checksum = 0;
  279. for (offset = 2; offset < length; offset++)
  280. checksum += ax_rx_buffer[offset];
  281. if (checksum != 255) {
  282. return 0;
  283. } else {
  284. return 1;
  285. }
  286. }