main.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2019 Ha Thach (tinyusb.org)
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. *
  24. */
  25. /* This example current worked and tested with following controller
  26. * - Sony DualShock 4 [CUH-ZCT2x] VID = 0x054c, PID = 0x09cc
  27. */
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include "bsp/board.h"
  32. #include "tusb.h"
  33. //--------------------------------------------------------------------+
  34. // MACRO CONSTANT TYPEDEF PROTYPES
  35. //--------------------------------------------------------------------+
  36. void led_blinking_task(void);
  37. /*------------- MAIN -------------*/
  38. int main(void)
  39. {
  40. board_init();
  41. printf("TinyUSB Host HID Controller Example\r\n");
  42. tusb_init();
  43. while (1)
  44. {
  45. // tinyusb host task
  46. tuh_task();
  47. led_blinking_task();
  48. }
  49. return 0;
  50. }
  51. //--------------------------------------------------------------------+
  52. // TinyUSB Callbacks
  53. //--------------------------------------------------------------------+
  54. // English
  55. #define LANGUAGE_ID 0x0409
  56. //uint8_t usb_buf[256] TU_ATTR_ALIGNED(4);
  57. TU_ATTR_ALIGNED(4)
  58. tusb_desc_device_t desc_device;
  59. static void _convert_utf16le_to_utf8(const uint16_t *utf16, size_t utf16_len, uint8_t *utf8, size_t utf8_len) {
  60. // TODO: Check for runover.
  61. (void)utf8_len;
  62. // Get the UTF-16 length out of the data itself.
  63. for (size_t i = 0; i < utf16_len; i++) {
  64. uint16_t chr = utf16[i];
  65. if (chr < 0x80) {
  66. *utf8++ = chr & 0xff;
  67. } else if (chr < 0x800) {
  68. *utf8++ = (uint8_t)(0xC0 | (chr >> 6 & 0x1F));
  69. *utf8++ = (uint8_t)(0x80 | (chr >> 0 & 0x3F));
  70. } else {
  71. // TODO: Verify surrogate.
  72. *utf8++ = (uint8_t)(0xE0 | (chr >> 12 & 0x0F));
  73. *utf8++ = (uint8_t)(0x80 | (chr >> 6 & 0x3F));
  74. *utf8++ = (uint8_t)(0x80 | (chr >> 0 & 0x3F));
  75. }
  76. // TODO: Handle UTF-16 code points that take two entries.
  77. }
  78. }
  79. // Count how many bytes a utf-16-le encoded string will take in utf-8.
  80. static int _count_utf8_bytes(const uint16_t *buf, size_t len) {
  81. size_t total_bytes = 0;
  82. for (size_t i = 0; i < len; i++) {
  83. uint16_t chr = buf[i];
  84. if (chr < 0x80) {
  85. total_bytes += 1;
  86. } else if (chr < 0x800) {
  87. total_bytes += 2;
  88. } else {
  89. total_bytes += 3;
  90. }
  91. // TODO: Handle UTF-16 code points that take two entries.
  92. }
  93. return total_bytes;
  94. }
  95. static void utf16_to_utf8(uint16_t *temp_buf, size_t buf_len) {
  96. size_t utf16_len = ((temp_buf[0] & 0xff) - 2) / sizeof(uint16_t);
  97. size_t utf8_len = _count_utf8_bytes(temp_buf + 1, utf16_len);
  98. _convert_utf16le_to_utf8(temp_buf + 1, utf16_len, (uint8_t *) temp_buf, sizeof(uint16_t) * buf_len);
  99. ((uint8_t*) temp_buf)[utf8_len] = '\0';
  100. }
  101. void print_device_descriptor(uint8_t daddr, tuh_xfer_t* xfer)
  102. {
  103. if ( XFER_RESULT_SUCCESS != xfer->result )
  104. {
  105. printf("Failed to get device descriptor\r\n");
  106. return;
  107. }
  108. printf("Device %u: ID %04x:%04x\r\n", daddr, desc_device.idVendor, desc_device.idProduct);
  109. printf("Device Descriptor:\r\n");
  110. printf(" bLength %u\r\n" , desc_device.bLength);
  111. printf(" bDescriptorType %u\r\n" , desc_device.bDescriptorType);
  112. printf(" bcdUSB %04x\r\n" , desc_device.bcdUSB);
  113. printf(" bDeviceClass %u\r\n" , desc_device.bDeviceClass);
  114. printf(" bDeviceSubClass %u\r\n" , desc_device.bDeviceSubClass);
  115. printf(" bDeviceProtocol %u\r\n" , desc_device.bDeviceProtocol);
  116. printf(" bMaxPacketSize0 %u\r\n" , desc_device.bMaxPacketSize0);
  117. printf(" idVendor 0x%04x\r\n" , desc_device.idVendor);
  118. printf(" idProduct 0x%04x\r\n" , desc_device.idProduct);
  119. printf(" bcdDevice %04x\r\n" , desc_device.bcdDevice);
  120. uint16_t temp_buf[128];
  121. printf(" iManufacturer %u " , desc_device.iManufacturer);
  122. if (XFER_RESULT_SUCCESS == tuh_descriptor_get_manufacturer_string_sync(daddr, LANGUAGE_ID, temp_buf, TU_ARRAY_SIZE(temp_buf)) )
  123. {
  124. utf16_to_utf8(temp_buf, TU_ARRAY_SIZE(temp_buf));
  125. printf((const char*) temp_buf);
  126. }
  127. printf("\r\n");
  128. printf(" iProduct %u " , desc_device.iProduct);
  129. if (XFER_RESULT_SUCCESS == tuh_descriptor_get_product_string_sync(daddr, LANGUAGE_ID, temp_buf, TU_ARRAY_SIZE(temp_buf)))
  130. {
  131. utf16_to_utf8(temp_buf, TU_ARRAY_SIZE(temp_buf));
  132. printf((const char*) temp_buf);
  133. }
  134. printf("\r\n");
  135. printf(" iSerialNumber %u " , desc_device.iSerialNumber);
  136. if (XFER_RESULT_SUCCESS == tuh_descriptor_get_serial_string_sync(daddr, LANGUAGE_ID, temp_buf, TU_ARRAY_SIZE(temp_buf)))
  137. {
  138. utf16_to_utf8(temp_buf, TU_ARRAY_SIZE(temp_buf));
  139. printf((const char*) temp_buf);
  140. }
  141. printf("\r\n");
  142. printf(" bNumConfigurations %u\r\n" , desc_device.bNumConfigurations);
  143. }
  144. // Invoked when device is mounted (configured)
  145. void tuh_mount_cb (uint8_t daddr)
  146. {
  147. printf("Device attached, address = %d\r\n", daddr);
  148. // Get Device Descriptor using asynchronous API
  149. tuh_descriptor_get_device(daddr, &desc_device, 18, print_device_descriptor, 0);
  150. }
  151. /// Invoked when device is unmounted (bus reset/unplugged)
  152. void tuh_umount_cb(uint8_t daddr)
  153. {
  154. printf("Device removed, address = %d\r\n", daddr);
  155. }
  156. //--------------------------------------------------------------------+
  157. // Blinking Task
  158. //--------------------------------------------------------------------+
  159. void led_blinking_task(void)
  160. {
  161. const uint32_t interval_ms = 1000;
  162. static uint32_t start_ms = 0;
  163. static bool led_state = false;
  164. // Blink every interval ms
  165. if ( board_millis() - start_ms < interval_ms) return; // not enough time
  166. start_ms += interval_ms;
  167. board_led_write(led_state);
  168. led_state = 1 - led_state; // toggle
  169. }