usbtrace.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. ================
  2. USB Device Trace
  3. ================
  4. **USB Device Tracing Controls**. The NuttX USB device subsystem supports
  5. a fairly sophisticated tracing facility. The basic trace cabability is
  6. controlled by these NuttX configuration settings:
  7. - ``CONFIG_USBDEV_TRACE``: Enables USB tracing
  8. - ``CONFIG_USBDEV_TRACE_NRECORDS``: Number of trace entries to remember
  9. **Trace IDs**. The trace facility works like this: When enabled, USB
  10. events that occur in either the USB device driver or in the USB class
  11. driver are logged. These events are described in
  12. ``include/nuttx/usb/usbdev_trace.h``. The logged events are identified
  13. by a set of event IDs:
  14. ========================= ==================================
  15. ``TRACE_INIT_ID`` Initialization events
  16. ``TRACE_EP_ID`` Endpoint API calls
  17. ``TRACE_DEV_ID`` USB device API calls
  18. ``TRACE_CLASS_ID`` USB class driver API calls
  19. ``TRACE_CLASSAPI_ID`` Other class driver system API calls
  20. ``TRACE_CLASSSTATE_ID`` Track class driver state changes
  21. ``TRACE_INTENTRY_ID`` Interrupt handler entry
  22. ``TRACE_INTDECODE_ID`` Decoded interrupt event
  23. ``TRACE_INTEXIT_ID`` Interrupt handler exit
  24. ``TRACE_OUTREQQUEUED_ID`` Request queued for OUT endpoint
  25. ``TRACE_INREQQUEUED_ID`` Request queued for IN endpoint
  26. ``TRACE_READ_ID`` Read (OUT) action
  27. ``TRACE_WRITE_ID`` Write (IN) action
  28. ``TRACE_COMPLETE_ID`` Request completed
  29. ``TRACE_DEVERROR_ID`` USB controller driver error event
  30. ``TRACE_CLSERROR_ID`` USB class driver error event
  31. ========================= ==================================
  32. **Logged Events**. Each logged event is 32-bits in size and includes
  33. #. 8-bits of the trace ID (values associated with the above)
  34. #. 8-bits of additional trace ID data, and
  35. #. 16-bits of additional data.
  36. **8-bit Trace Data** The 8-bit trace data depends on the specific event
  37. ID. As examples,
  38. - For the USB serial and mass storage class, the 8-bit event data is
  39. provided in ``include/nuttx/usb/usbdev_trace.h``.
  40. - For the USB device driver, that 8-bit event data is provided within
  41. the USB device driver itself. So, for example, the 8-bit event data
  42. for the LPC1768 USB device driver is found in
  43. ``arch/arm/src/lpc17xx_40xx/lpc17_40_usbdev.c``.
  44. **16-bit Trace Data**. The 16-bit trace data provided additional context
  45. data relevant to the specific logged event.
  46. **Trace Control Interfaces**. Logging of each of these kinds events can
  47. be enabled or disabled using the interfaces described in
  48. ``include/nuttx/usb/usbdev_trace.h``.
  49. **Enabling USB Device Tracing**. USB device tracing will be configured
  50. if ``CONFIG_USBDEV`` and either of the following are set in the NuttX
  51. configuration file:
  52. - ``CONFIG_USBDEV_TRACE``, or
  53. - ``CONFIG_DEBUG_FEATURES and CONFIG_DEBUG_USB``
  54. **Log Data Sink**. The logged data itself may go to either (1) an
  55. internal circular buffer, or (2) may be provided on the console. If
  56. ``CONFIG_USBDEV_TRACE`` is defined, then the trace data will go to the
  57. circular buffer. The size of the circular buffer is determined by
  58. ``CONFIG_USBDEV_TRACE_NRECORDS``. Otherwise, the trace data goes to
  59. console.
  60. **Example**. Here is an example of USB trace output using
  61. ``apps/examples/usbserial`` for an LPC1768 platform with the following
  62. NuttX configuration settings:
  63. - ``CONFIG_DEBUG_FEATURES``, ``CONFIG_DEBUG_INFO``, ``CONFIG_USB``
  64. - ``CONFIG_EXAMPLES_USBSERIAL_TRACEINIT``,
  65. ``CONFIG_EXAMPLES_USBSERIAL_TRACECLASS``,
  66. ``CONFIG_EXAMPLES_USBSERIAL_TRACETRANSFERS``,
  67. ``CONFIG_EXAMPLES_USBSERIAL_TRACECONTROLLER``,
  68. ``CONFIG_EXAMPLES_USBSERIAL_TRACEINTERRUPTS``
  69. Console Output::
  70. ABDE
  71. usbserial_main: Registering USB serial driver
  72. uart_register: Registering /dev/ttyUSB0
  73. usbserial_main: Successfully registered the serial driver
  74. 1 Class API call 1: 0000
  75. 2 Class error: 19:0000
  76. usbserial_main: ERROR: Failed to open /dev/ttyUSB0 for reading: 107
  77. usbserial_main: Not connected. Wait and try again.
  78. 3 Interrupt 1 entry: 0039
  79. 4 Interrupt decode 7: 0019
  80. 5 Interrupt decode 32: 0019
  81. 6 Interrupt decode 6: 0019
  82. 7 Class disconnect(): 0000
  83. 8 Device pullup(): 0001
  84. 9 Interrupt 1 exit: 0000
  85. The numbered items are USB USB trace output. You can look in the file
  86. ``drivers/usbdev/usbdev_trprintf.c`` to see examctly how each output
  87. line is formatted. Here is how each line should be interpreted:
  88. == ==================== ================ ================================== =================
  89. N. USB EVENT ID 8-bit EVENT DATA MEANING 16-bit EVENT DATA
  90. 1 TRACE_CLASSAPI_ID1 1 USBSER_TRACECLASSAPI_SETUP1 0000
  91. 2 TRACE_CLSERROR_ID1 19 USBSER_TRACEERR_SETUPNOTCONNECTED1 0000
  92. 3 TRACE_INTENTRY_ID1 1 LPC17_40_TRACEINTID_USB2 0039
  93. 4 TRACE_INTDECODE_ID2 7 LPC17_40_TRACEINTID_DEVSTAT2 0019
  94. 5 TRACE_INTDECODE_ID2 32 LPC17_40_TRACEINTID_SUSPENDCHG2 0019
  95. 6 TRACE_INTDECODE_ID2 6 LPC17_40_TRACEINTID_DEVRESET2 0019
  96. 7 TRACE_CLASS_ID1 3 (See TRACE_CLASSDISCONNECT1) 0000
  97. 8 TRACE_DEV_ID1 6 (See TRACE_DEVPULLUP1) 0001
  98. 9 TRACE_INTEXIT_ID1 1 LPC17_40_TRACEINTID_USB2 0000
  99. == ==================== ================ ================================== =================
  100. NOTES:
  101. 1. See include/nuttx/usb/usbdev_trace.h
  102. 2. See arch/arm/src/lpc17xx_40xx/lpc17_40_usbdev.c
  103. In the above example you can see that:
  104. - **1**. The serial class USB setup method was called for the USB
  105. serial class. This is the corresponds to the following logic in
  106. ``drivers/usbdev/pl2303.c``:
  107. .. code-block:: c
  108. static int pl2303_setup(FAR struct uart_dev_s *dev)
  109. {
  110. ...
  111. usbtrace(PL2303_CLASSAPI_SETUP, 0);
  112. ...
  113. - **2**. An error occurred while processing the setup command because
  114. no configuration has yet been selected by the host. This corresponds
  115. to the following logic in ``drivers/usbdev/pl2303.c``:
  116. .. code-block:: c
  117. static int pl2303_setup(FAR struct uart_dev_s *dev)
  118. {
  119. ...
  120. /* Check if we have been configured */
  121. if (priv->config == PL2303_CONFIGIDNONE)
  122. {
  123. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_SETUPNOTCONNECTED), 0);
  124. return -ENOTCONN;
  125. }
  126. ...
  127. - **3-6**. Here is a USB interrupt that suspends and resets the device.
  128. - **7-8**. During the interrupt processing the serial class is
  129. disconnected
  130. - **9**. And the interrupt returns
  131. **USB Monitor**. The *USB monitor* is an application in the
  132. ``apps/system/usbmonitor`` that provides a convenient way to get debug
  133. trace output. If tracing is enabled, the USB device will save encoded
  134. trace output in in-memory buffer; if the USB monitor is also enabled,
  135. that trace buffer will be periodically emptied and dumped to the system
  136. logging device (the serial console in most configurations). The
  137. following are some of the relevant configuration options:
  138. =========================================== ===================================================
  139. Device Drivers -> USB Device Driver Support .
  140. ``CONFIG_USBDEV_TRACE=y`` Enable USB trace feature
  141. ``CONFIG_USBDEV_TRACE_NRECORDS=nnnn`` Buffer nnnn records in memory. If you lose trace data,
  142. . then you will need to increase the size of this buffer
  143. . (or increase the rate at which the trace buffer is emptied).
  144. ``CONFIG_USBDEV_TRACE_STRINGS=y`` Optionally, convert trace ID numbers to strings.
  145. . This feature may not be supported by all drivers.
  146. =========================================== ===================================================
  147. =========================================== ===================================================
  148. Application Configuration -> NSH LIbrary .
  149. ``CONFIG_NSH_USBDEV_TRACE=n`` Make sure that any built-in tracing from NSH is disabled.
  150. ``CONFIG_NSH_ARCHINIT=y`` Enable this option only if your board-specific logic
  151. . has logic to automatically start the USB monitor.
  152. . Otherwise the USB monitor can be started or stopped
  153. . with the usbmon_start and usbmon_stop commands from the NSH console.
  154. =========================================== ===================================================
  155. =============================================== ============================================
  156. Application Configuration -> System NSH Add-Ons .
  157. ``CONFIG_USBMONITOR=y`` Enable the USB monitor daemon
  158. ``CONFIG_USBMONITOR_STACKSIZE=nnnn`` Sets the USB monitor daemon stack size to nnnn. The default is 2KiB.
  159. ``CONFIG_USBMONITOR_PRIORITY=50`` Sets the USB monitor daemon priority to nnnn.
  160. . This priority should be low so that it does not
  161. . interfere with other operations, but not so low that
  162. . you cannot dump the buffered USB data sufficiently
  163. . rapidly. The default is 50.
  164. ``CONFIG_USBMONITOR_INTERVAL=nnnn`` Dump the buffered USB data every nnnn seconds.
  165. . If you lose buffered USB trace data, then dropping
  166. . this value will help by increasing the rate at which
  167. . the USB trace buffer is emptied.
  168. ``CONFIG_USBMONITOR_TRACEINIT=y`` Selects which USB event(s) that you want to be traced.
  169. ``CONFIG_USBMONITOR_TRACECLASS=y`` .
  170. ``CONFIG_USBMONITOR_TRACETRANSFERS=y`` .
  171. ``CONFIG_USBMONITOR_TRACECONTROLLER=y`` .
  172. ``CONFIG_USBMONITOR_TRACEINTERRUPTS=y`` .
  173. =============================================== ============================================
  174. NOTE: If USB debug output is also enabled, both outputs will appear on
  175. the serial console. However, the debug output will be asynchronous with
  176. the trace output and, hence, difficult to interpret.