usb_application_enumspy.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include "core/config.h"
  2. #if CONFIG_AUTOMAT_MODE_USB_POWERBANK && CONFIG_AUTOMAT_MODE
  3. #include "usbapp/usb_application_enumspy.h"
  4. #include "usb/usb_bridge.h"
  5. #include "usb/usb_config.h"
  6. #include "usbd_vendor.h"
  7. #include "core/csect.h"
  8. #if CONFIG_AUTOMAT_MODE_USB_POWERBANK_ENUMTIMEOUT > 0
  9. #define ENUMERATE_TIMEOUT CONFIG_AUTOMAT_MODE_USB_POWERBANK_ENUMTIMEOUT
  10. #else
  11. #define ENUMERATE_TIMEOUT 5000
  12. #endif
  13. static uint32_t nEnumerateTimeout = 0; // Enumerate timeout counter, 0 = disabled.
  14. static bool bUsbEnumerated = false;
  15. static void USBApplicationEnumSpy_startEnumerateTimeout();
  16. static void USBApplicationEnumSpy_stopEnumerateTimeout();
  17. static void USBApplicationEnumSpy_Tick1ms();
  18. static bool USBApplicationEnumSpy_checkEnumerateTimeout();
  19. static int8_t fEnumSpyInit();
  20. static int8_t fEnumSpyDeInit();
  21. static void fEnumSpyReset();
  22. static bool fEnumSpySetup( const tUSBSetupPacket_t * pSetup, bool bFirstStage, bool success );
  23. static size_t fEnumSpyControlRx( const tUSBSetupPacket_t * pSetup, sUSBTransfer_t * rx, size_t idx, size_t bytesRemaining );
  24. static size_t fEnumSpyControlTx( const tUSBSetupPacket_t * pSetup, sUSBTransfer_t * tx, size_t idx, size_t bytesRemaining );
  25. const sUSBAppEntry_Control_t usbapplication_ACM_planarproto_enumspy = {
  26. .fUsbInit = fEnumSpyInit,
  27. .fUsbDeInit = fEnumSpyDeInit,
  28. .fUsbSetup = fEnumSpySetup,
  29. .fUsbCtlEpRx = fEnumSpyControlRx,
  30. .fUsbCtlEpTx = fEnumSpyControlTx,
  31. .fResetEvent = fEnumSpyReset,
  32. };
  33. const sUsbApplicationEnumSpyHandle_t UsbApplicationEnumSpyHandle = {
  34. .startEnumerateTimeout = USBApplicationEnumSpy_startEnumerateTimeout,
  35. .stopEnumerateTimeout = USBApplicationEnumSpy_stopEnumerateTimeout,
  36. .checkEnumerateTimeout = USBApplicationEnumSpy_checkEnumerateTimeout,
  37. .timerTickEventIrq1ms = USBApplicationEnumSpy_Tick1ms
  38. };
  39. static int8_t fEnumSpyInit()
  40. {
  41. bUsbEnumerated = false;
  42. nEnumerateTimeout = 0; // reset and disable counter
  43. return 0;
  44. }
  45. static int8_t fEnumSpyDeInit()
  46. {
  47. bUsbEnumerated = false;
  48. nEnumerateTimeout = 0; // reset and disable counter
  49. return 0;
  50. }
  51. static void fEnumSpyReset()
  52. {
  53. // USB Standard: "9.1.2 Bus Enumeration"
  54. // "The host then issues a port enable and reset command to that port."
  55. // "The hub performs the required reset processing for that port (see Section 11.5.1.5). When the reset
  56. // signal is released, the port has been enabled. The USB device is now in the Default state and can draw
  57. // no more than 100 mA from VBUS. All of its registers and state have been reset and it answers to the
  58. // default address."
  59. nEnumerateTimeout = 0; // reset and disable counter
  60. bUsbEnumerated = true; // Reset signal received: USB has been enumerated
  61. }
  62. static bool fEnumSpySetup( const tUSBSetupPacket_t * pSetup, bool bFirstStage, bool success )
  63. {
  64. return false;
  65. }
  66. static size_t fEnumSpyControlRx( const tUSBSetupPacket_t * pSetup, sUSBTransfer_t * rx, size_t idx, size_t bytesRemaining )
  67. {
  68. return 0;
  69. }
  70. static size_t fEnumSpyControlTx( const tUSBSetupPacket_t * pSetup, sUSBTransfer_t * tx, size_t idx, size_t bytesRemaining )
  71. {
  72. return 0;
  73. }
  74. // Reset and restart the Enumerate timeout
  75. static void USBApplicationEnumSpy_startEnumerateTimeout()
  76. {
  77. __DI__
  78. nEnumerateTimeout = 1; // 0 - disabled, 1 - initial value
  79. __EI__
  80. }
  81. // Stop Enumerate timeout
  82. static void USBApplicationEnumSpy_stopEnumerateTimeout()
  83. {
  84. __DI__
  85. nEnumerateTimeout = 0; // 0 - disabled, 1 - initial value
  86. __EI__
  87. }
  88. static void USBApplicationEnumSpy_Tick1ms()
  89. {
  90. if( (nEnumerateTimeout > 0) && (nEnumerateTimeout <= ENUMERATE_TIMEOUT) )
  91. {
  92. nEnumerateTimeout ++;
  93. }
  94. }
  95. // USBApplicationEnumSpy_checkEnumerateTimeout
  96. // Check if the host had not enumerate the device for a long time (ENUMERATE_TIMEOUT)
  97. // Returns: true in case the device had not been enumerated for a ENUMERATE_TIMEOUT ms
  98. static bool USBApplicationEnumSpy_checkEnumerateTimeout()
  99. {
  100. bool timeout = false;
  101. bool enumerated = false;
  102. __DI__
  103. // The timeout is considered as expired when the counter exceedes the ENUMERATE_TIMEOUT value.
  104. timeout = ( nEnumerateTimeout > ENUMERATE_TIMEOUT );
  105. enumerated = bUsbEnumerated;
  106. __EI__
  107. return (timeout && !enumerated);
  108. }
  109. #endif