usb_application_interfaceswitch.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #define USB_APPLICATION_INTERFACESWITCH_C
  2. #include "core/config.h"
  3. #include "usbapp/usb_application_interfaceswitch.h"
  4. #include "usb/usb_bridge.h"
  5. #include "usb/usb_config.h"
  6. #include "usbd_vendor.h"
  7. #include "my_assert.h"
  8. #if CONFIG_NFMBASECLASS
  9. #include "app/nfm/nfm_base.h"
  10. #else
  11. #error This feature can not be implemented without CONFIG_NFMBASECLASS
  12. #endif
  13. #define PROTO_REQ_INTERFACE_SWITCH 0xBF // ïåðåêëþ÷èòü èíòåðôåéñ íà USBTMC
  14. static int8_t fInterfaceSwitchAppInit();
  15. static int8_t fInterfaceSwitchAppDeInit();
  16. static void fInterfaceSwitchAppReset();
  17. static bool fInterfaceSwitchAppSetup( const tUSBSetupPacket_t * pSetup, bool bFirstStage, bool success );
  18. static size_t fInterfaceSwitchAppControlRx( const tUSBSetupPacket_t * pSetup, sUSBTransfer_t * rx, size_t idx, size_t bytesRemaining );
  19. static size_t fInterfaceSwitchAppControlTx( const tUSBSetupPacket_t * pSetup, sUSBTransfer_t * tx, size_t idx, size_t bytesRemaining );
  20. const sUSBAppEntry_Control_t usbapplication_ACM_planarproto_interfaceswitch = {
  21. .fUsbInit = fInterfaceSwitchAppInit,
  22. .fUsbDeInit = fInterfaceSwitchAppDeInit,
  23. .fUsbSetup = fInterfaceSwitchAppSetup,
  24. .fUsbCtlEpRx = fInterfaceSwitchAppControlRx,
  25. .fUsbCtlEpTx = fInterfaceSwitchAppControlTx,
  26. .fResetEvent = fInterfaceSwitchAppReset,
  27. };
  28. static int8_t fInterfaceSwitchAppInit()
  29. {
  30. #if !CONFIG_USB_USBTMC_ENABLE
  31. my_assert(false); // USBTMC is not enabled in configuration
  32. #endif
  33. return 0;
  34. }
  35. static int8_t fInterfaceSwitchAppDeInit()
  36. {
  37. return 0;
  38. }
  39. static void fInterfaceSwitchAppReset()
  40. {
  41. }
  42. static bool fInterfaceSwitchAppSetup( const tUSBSetupPacket_t * pSetup, bool bFirstStage, bool success )
  43. {
  44. switch( pSetup->bRequest )
  45. {
  46. case PROTO_REQ_INTERFACE_SWITCH:
  47. {
  48. #if !CONFIG_USB_OVERRIDE_IFACE_VENDOR
  49. return true; // request supported, pass to the TX stage
  50. #endif
  51. }
  52. break;
  53. }
  54. return false;
  55. }
  56. static size_t fInterfaceSwitchAppControlRx( const tUSBSetupPacket_t * pSetup, sUSBTransfer_t * rx, size_t idx, size_t bytesRemaining )
  57. {
  58. return 0;
  59. }
  60. static size_t fInterfaceSwitchAppControlTx( const tUSBSetupPacket_t * pSetup, sUSBTransfer_t * tx, size_t idx, size_t bytesRemaining )
  61. {
  62. switch( pSetup->bRequest )
  63. {
  64. case PROTO_REQ_INTERFACE_SWITCH: // read the raw lowlevel control code
  65. {
  66. uint8_t status = 0;
  67. #if CONFIG_USB_USBTMC_ENABLE
  68. #if !CONFIG_USB_OVERRIDE_IFACE_VENDOR
  69. bool rc = NFMClass->methods.usbInterface.setInterface( eNFM_IfaceUSBTMC );
  70. #if CONFIG_REBOOT_FEATURE
  71. if( rc ) RebootRequest();
  72. #endif
  73. status = ((rc)?'\1':'\0');
  74. #endif
  75. #endif
  76. if( 0 == idx && sizeof(uint8_t) == bytesRemaining )
  77. {
  78. usb_push_transfer( tx, &status, sizeof( status ) );
  79. }
  80. }
  81. break;
  82. }
  83. return usb_count_transfer( tx );
  84. }