tusb.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. * This file is part of the TinyUSB stack.
  25. */
  26. #include "tusb_option.h"
  27. #if TUSB_OPT_HOST_ENABLED || TUSB_OPT_DEVICE_ENABLED
  28. #include "tusb.h"
  29. // TODO clean up
  30. #if TUSB_OPT_DEVICE_ENABLED
  31. #include "device/usbd_pvt.h"
  32. #endif
  33. bool tusb_init(void)
  34. {
  35. #if TUSB_OPT_DEVICE_ENABLED
  36. TU_ASSERT ( tud_init(TUD_OPT_RHPORT) ); // init device stack
  37. #endif
  38. #if TUSB_OPT_HOST_ENABLED
  39. TU_ASSERT( tuh_init(TUH_OPT_RHPORT) ); // init host stack
  40. #endif
  41. return true;
  42. }
  43. bool tusb_inited(void)
  44. {
  45. bool ret = false;
  46. #if TUSB_OPT_DEVICE_ENABLED
  47. ret = ret || tud_inited();
  48. #endif
  49. #if TUSB_OPT_HOST_ENABLED
  50. ret = ret || tuh_inited();
  51. #endif
  52. return ret;
  53. }
  54. /*------------------------------------------------------------------*/
  55. /* Debug
  56. *------------------------------------------------------------------*/
  57. #if CFG_TUSB_DEBUG
  58. #include <ctype.h>
  59. char const* const tusb_strerr[TUSB_ERROR_COUNT] = { ERROR_TABLE(ERROR_STRING) };
  60. static void dump_str_line(uint8_t const* buf, uint16_t count)
  61. {
  62. tu_printf(" |");
  63. // each line is 16 bytes
  64. for(uint16_t i=0; i<count; i++)
  65. {
  66. const char ch = buf[i];
  67. tu_printf("%c", isprint(ch) ? ch : '.');
  68. }
  69. tu_printf("|\r\n");
  70. }
  71. /* Print out memory contents
  72. * - buf : buffer
  73. * - count : number of item
  74. * - indent: prefix spaces on every line
  75. */
  76. void tu_print_mem(void const *buf, uint32_t count, uint8_t indent)
  77. {
  78. uint8_t const size = 1; // fixed 1 byte for now
  79. if ( !buf || !count )
  80. {
  81. tu_printf("NULL\r\n");
  82. return;
  83. }
  84. uint8_t const *buf8 = (uint8_t const *) buf;
  85. char format[] = "%00X";
  86. format[2] += 2*size;
  87. const uint8_t item_per_line = 16 / size;
  88. for(unsigned int i=0; i<count; i++)
  89. {
  90. unsigned int value=0;
  91. if ( i%item_per_line == 0 )
  92. {
  93. // Print Ascii
  94. if ( i != 0 )
  95. {
  96. dump_str_line(buf8-16, 16);
  97. }
  98. for(uint8_t s=0; s < indent; s++) tu_printf(" ");
  99. // print offset or absolute address
  100. tu_printf("%04X: ", 16*i/item_per_line);
  101. }
  102. memcpy(&value, buf8, size);
  103. buf8 += size;
  104. tu_printf(" ");
  105. tu_printf(format, value);
  106. }
  107. // fill up last row to 16 for printing ascii
  108. const uint32_t remain = count%16;
  109. uint8_t nback = (remain ? remain : 16);
  110. if ( remain )
  111. {
  112. for(uint32_t i=0; i< 16-remain; i++)
  113. {
  114. tu_printf(" ");
  115. for(int j=0; j<2*size; j++) tu_printf(" ");
  116. }
  117. }
  118. dump_str_line(buf8-nback, nback);
  119. }
  120. #endif
  121. #endif // host or device enabled