usb_hooks.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // -----------------------------------------------------------------------------
  2. // USB Library "usblib"
  3. // Target: for lpc17xx
  4. // Author: Sychov A.
  5. // Date: 12/10/2018
  6. // Version: 1.0
  7. // -----------------------------------------------------------------------------
  8. // File: USB-hooks header
  9. // Version: 1.0
  10. // -----------------------------------------------------------------------------
  11. #ifndef USB_HOOKS_H
  12. #define USB_HOOKS_H
  13. #include "usb/usb_transfers.h"
  14. #include "usb/usb_spec.h"
  15. // USB-Callbacks (Hooks)
  16. // fUsbEpIrqHandler_t:
  17. // Callback-type function is called on each interrupt of any endpoint.
  18. // @EndpointStatus - 32-bit word of flags, where each bit represents
  19. // each physical endpoint.
  20. // Bit 0 - EP0RX
  21. // Bit 1 - EP0TX
  22. // Bit 2 - EP1RX
  23. // Bit 3 - EP1TX, and etc.
  24. // The value 1 in the bit means an interrupt occurred on apropriate endpoint.
  25. // The notification type function code must clear the interrupt in hardware itself.
  26. typedef void (* fUsbEpIrqHandler_t )( uint32_t EndpointStatus );
  27. // fUsbBusReset_t:
  28. // The notification type function is called on USB-bus reset event
  29. typedef void (* fUsbBusReset_t)();
  30. // fUsbSetIface_t:
  31. // The notification type function is called to inform the application of interface activating
  32. // The function must return 'true' if application able to activate specified interface, and 'false' otherwise.
  33. // In case of 'false', the USB-request causes request error.
  34. // The callee should take account the bAlternateSetting of specified @iface, active bAlternateSetting value can be requested by @fUsbGetIface_t
  35. typedef bool (* fUsbSetIface_t)( uint8_t cfgidx, uint8_t ifidx, const sUSBInterfaceDescriptor_t * ifdesc );
  36. // fUsbClrIface_t:
  37. // The notification type function is called to inform the application of interface de-activating
  38. // The callee should take account the bAlternateSetting of specified @iface, active bAlternateSetting value can be requested by @fUsbGetIface_t
  39. typedef void (* fUsbClrIface_t)( uint8_t cfgidx, uint8_t ifidx, const sUSBInterfaceDescriptor_t * ifdesc );
  40. // fUsbGetIface_t:
  41. // The request-type function is called to query the interface active alternate setting
  42. // If this callback is not specified, the alternate setting = 0 will be reported in GET_INTERFACE request
  43. typedef bool (* fUsbGetIface_t)( uint8_t cfgidx, uint8_t ifidx, uint8_t * pAltSetting );
  44. // fUsbSuspendEvent_t:
  45. // The notification type function is called on USB suspending/resuming event
  46. // @suspend - suspend state, true - USB-Controller is suspended, false - USB-Controller is resumed
  47. typedef void (* fUsbSuspendEvent_t)( bool suspend );
  48. // The data arrival event
  49. // This hook is called to process the portion of data by application
  50. // @pSetup - the setup packet caused the callback call
  51. // @rx - the transfer to read data from
  52. // @idx - the data index, equals to the number of bytes already sent in request
  53. // @bytesRemaining - the whole amount of bytes needed to receive more.
  54. // For control transfers (EP0) this number depends on the @wLength.
  55. // Note: @tx refers to the Transfer-object with small buffer with the size
  56. // less than @bytesRemaining. The user must read the bytes from transfer @tx
  57. // and return the read bytes count. This value is provided by usb_read_transfer().
  58. // The @bytesRemaining is amount of remaining bytes to receive incliding the bytes
  59. // queued into @rx transfer.
  60. typedef size_t (* fUsbCtlEpRx_t) ( const tUSBSetupPacket_t * pSetup, sUSBTransfer_t * rx, size_t idx, size_t bytesRemaining );
  61. // The data requesting event
  62. // This hook is called to provide the portion of data for the usb-core
  63. // @pSetup - the setup packet caused the callback call
  64. // @tx - the transfer to write data into
  65. // @idx - the data index, equals to the number of bytes already received in request
  66. // @bytesRemaining - the whole amount of bytes needed to send more.
  67. // For control transfers (EP0) this number depends on the @wLength.
  68. // Note: @rx refers to the Transfer-object with small buffer with the size
  69. // less than @bytesRemaining. The user must write not more than @bytesRemaining
  70. // in each callback call until the Transfer-buffer is full. Since the transfer
  71. // buffer fulls, the usb_write_transfer() returns 0.
  72. // The function must return the written bytes count into the transfer @tx
  73. // This value is provided by usb_write_transfer().
  74. typedef size_t (* fUsbCtlEpTx_t) ( const tUSBSetupPacket_t * pSetup, sUSBTransfer_t * tx, size_t idx, size_t bytesRemaining );
  75. // fUsbSetupRx_t:
  76. // The callback type function is called on each received SETUP packet in Control Endpoint.
  77. // The function is called twice: firstly before the packet processing in the kernel,
  78. // secondly afther the processing.
  79. // In case the callback function code processes the SETUP packet itself and wants to
  80. // intercept it and make the packet aren't processed in the kernel, the function must
  81. // return 'false' at first stage. Otherwise, in case the function returns 'true', it
  82. // should not process the packet, but can use the event as notification.
  83. // The second stage does not appear in case the first stage function returns 'false'.
  84. // Otherwise, the second stage function is called after the packet is processed in
  85. // kernel. It does not matter what value the function returns in this stage.
  86. // @bFirstStage - stage flag. For first stage - 'true', for second - 'false'
  87. // @success - the indicator whether the setup packet is processed correcly at second stage.
  88. // For first stage @success is always false.
  89. typedef bool (* fUsbSetupRx_t) ( const tUSBSetupPacket_t * pSetup, bool bFirstStage, bool success );
  90. typedef bool (* fUsbDataRx_t)( uint8_t bEpLogAddress,
  91. sUSBTransfer_t * rx );
  92. typedef bool (* fUsbDataTx_t)( uint8_t bEpLogAddress,
  93. sUSBTransfer_t * tx );
  94. // fUsbDataErr_t
  95. // Notify-type callback is called when a USB error occurred
  96. // @bConfValue - active configuration value
  97. // @bIntNumber - active interface number
  98. // @bEpLogAddress - active endpoint address
  99. // @error - error mask, see USB Error flags below.
  100. // To proceed, the function must return true.
  101. // Otherwise, if function returns false, the library may interrupt current operation
  102. typedef bool (* fUsbDataErr_t)( uint8_t bEpLogAddress,
  103. uint32_t error );
  104. typedef int8_t (* fUsbInit_t )(void);
  105. typedef int8_t (* fUsbDeInit_t )(void);
  106. typedef bool (* fUsbDataInit_t )( uint8_t epnum );
  107. typedef void (* fUsbDataDeInit_t )( uint8_t epnum );
  108. // Enumeration-Level hooks:
  109. typedef struct
  110. {
  111. fUsbSetupRx_t fUsbSetup; // optional
  112. fUsbCtlEpRx_t fUsbCtlEpRx; // optional
  113. fUsbCtlEpTx_t fUsbCtlEpTx; // optional
  114. fUsbEpIrqHandler_t fIrqHandler; // optional
  115. fUsbBusReset_t fResetEvent; // optional
  116. fUsbSuspendEvent_t fSuspendEvent; // optional
  117. fUsbSetIface_t fSetIface; // optional SetInterface/SetConfiguration request handler
  118. fUsbClrIface_t fClrIface; // optional SetInterface/SetConfiguration request handler
  119. fUsbGetIface_t fGetIface; // optional (GET_INTERFACE request)
  120. fUsbDataRx_t fDataRxHandler; // optional ( endpoints I/O )
  121. fUsbDataTx_t fDataTxHandler; // optional ( endpoints I/O )
  122. fUsbDataErr_t fDataErrHandler; // optional ( endpoints I/O )
  123. }
  124. sUSBHooks_t;
  125. typedef struct
  126. {
  127. fUsbInit_t fUsbInit;
  128. fUsbDeInit_t fUsbDeInit;
  129. fUsbSetupRx_t fUsbSetup;
  130. fUsbCtlEpRx_t fUsbCtlEpRx;
  131. fUsbCtlEpTx_t fUsbCtlEpTx;
  132. fUsbBusReset_t fResetEvent;
  133. }
  134. sUSBAppEntry_Control_t;
  135. typedef struct
  136. {
  137. fUsbDataInit_t fDataInitHandler; // optional
  138. fUsbDataRx_t fDataRxHandler; // optional
  139. fUsbDataTx_t fDataTxHandler; // optional
  140. fUsbDataErr_t fDataErrHandler; // optional
  141. fUsbDataDeInit_t fDataDeInitHandler; // optional
  142. }
  143. sUSBAppEntry_Data_t;
  144. // USB Error flags for @fUsbDataErr_t:
  145. #define USB_ERR_NOERR 0
  146. #define USB_ERR_RX (1<<0) // common receive error
  147. #define USB_ERR_TX (1<<1) // common transmit error
  148. #define USB_ERR_TX_NODATA (1<<2) // no data available to transmit
  149. #define USB_ERR_STALL (1<<3) // endpoint is stalled
  150. #endif