usb_application_temp.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include "usbapp/usb_application_temp.h"
  2. #include "usb/usb_bridge.h"
  3. #include "usb/usb_config.h"
  4. #include "usbd_vendor.h"
  5. #include "app/thermo/tsensor.h"
  6. #include "app/thermo/NTCtsensor.h"
  7. #include "app/nfm/nfm_base_excls.h"
  8. #define PROTO_REQ_GET_TEMPERATURE 0x01 // ïðî÷èòàòü óñðåäíåííóþ òåìïåðàòóðó
  9. #define PROTO_REQ_GET_INTERNAL_TEMPERATURE 0x01 // Ïðî÷èòàòü âíóòðåííþþ òåìïåðàòóðó óñòðîéñòâà
  10. #define PROTO_REQ_GET_EXTERNAL_TEMPERATURE 0x02 // Ïðî÷èòàòü âíåøíþþ òåìïåðàòóðó óñòðîéñòâà
  11. #define PROTO_REQ_GET_TEMPERATURE_COEFF 0x03 // Ïðî÷èòàòü òåìïåðàòóðíûé êîýôôèöèåíò
  12. #define PROTO_REQ_SET_TEMPERATURE_COEFF 0x04 // Çàïèñàòü òåìïåðàòóðíûé êîýôôèöèåíò
  13. static int8_t fThermoAppInit();
  14. static int8_t fThermoAppDeInit();
  15. static void fThermoAppReset();
  16. static bool fThermoAppSetup( const tUSBSetupPacket_t * pSetup, bool bFirstStage, bool success );
  17. static size_t fThermoAppControlRx( const tUSBSetupPacket_t * pSetup, sUSBTransfer_t * rx, size_t idx, size_t bytesRemaining );
  18. static size_t fThermoAppControlTx( const tUSBSetupPacket_t * pSetup, sUSBTransfer_t * tx, size_t idx, size_t bytesRemaining );
  19. const sUSBAppEntry_Control_t usbapplication_ACM_planarproto_temp = {
  20. .fUsbInit = fThermoAppInit,
  21. .fUsbDeInit = fThermoAppDeInit,
  22. .fUsbSetup = fThermoAppSetup,
  23. .fUsbCtlEpRx = fThermoAppControlRx,
  24. .fUsbCtlEpTx = fThermoAppControlTx,
  25. .fResetEvent = fThermoAppReset,
  26. };
  27. static int8_t fThermoAppInit()
  28. {
  29. return 0;
  30. }
  31. static int8_t fThermoAppDeInit()
  32. {
  33. return 0;
  34. }
  35. static void fThermoAppReset()
  36. {
  37. }
  38. static bool fThermoAppSetup( const tUSBSetupPacket_t * pSetup, bool bFirstStage, bool success )
  39. {
  40. switch ( pSetup->bRequest )
  41. {
  42. case PROTO_REQ_GET_TEMPERATURE:
  43. {
  44. switch ( pSetup->wIndex )
  45. {
  46. case PROTO_REQ_GET_INTERNAL_TEMPERATURE:
  47. {
  48. #if CONFIG_TSENSOR_EMULATE_FOR_USB
  49. return true;
  50. #else
  51. return ThermoSensor.GetReady();
  52. #endif
  53. }
  54. break;
  55. case PROTO_REQ_GET_EXTERNAL_TEMPERATURE:
  56. {
  57. return NtcThermoSensor.GetReady();
  58. }
  59. break;
  60. case PROTO_REQ_GET_TEMPERATURE_COEFF:
  61. {
  62. return true;
  63. }
  64. break;
  65. case PROTO_REQ_SET_TEMPERATURE_COEFF:
  66. {
  67. NFMClass->methods.tempCoefficient.setTempCoeff(pSetup->wValue);
  68. return true;
  69. }
  70. break;
  71. }
  72. }
  73. break;
  74. }
  75. return false;
  76. }
  77. static size_t fThermoAppControlRx( const tUSBSetupPacket_t * pSetup, sUSBTransfer_t * rx, size_t idx, size_t bytesRemaining )
  78. {
  79. return 0;
  80. }
  81. static size_t fThermoAppControlTx( const tUSBSetupPacket_t * pSetup, sUSBTransfer_t * tx, size_t idx, size_t bytesRemaining )
  82. {
  83. switch ( pSetup->bRequest )
  84. {
  85. case PROTO_REQ_GET_TEMPERATURE:
  86. {
  87. switch ( pSetup->wIndex )
  88. {
  89. case PROTO_REQ_GET_INTERNAL_TEMPERATURE:
  90. {
  91. tTSensorTemp temp;
  92. #if CONFIG_TSENSOR_EMULATE_FOR_USB
  93. temp = -1;
  94. usb_push_transfer( tx, &temp, sizeof(temp) );
  95. #else
  96. if( 0 == idx && sizeof(temp) == bytesRemaining )
  97. {
  98. temp = ThermoSensor.GetTempAVG();
  99. usb_push_transfer( tx, &temp, sizeof(temp) );
  100. }
  101. #endif
  102. }
  103. break;
  104. case PROTO_REQ_GET_EXTERNAL_TEMPERATURE:
  105. {
  106. float temp = NFMClass->methods.getAverageExtTemperature();
  107. if( 0 == idx && sizeof(temp) == bytesRemaining )
  108. {
  109. usb_push_transfer( tx, &temp, sizeof(temp) );
  110. }
  111. return true;
  112. }
  113. break;
  114. case PROTO_REQ_GET_TEMPERATURE_COEFF:
  115. {
  116. uint16_t tempCoeff = 0;
  117. if( 0 == idx && sizeof(tempCoeff) == bytesRemaining )
  118. {
  119. NFMClass->methods.tempCoefficient.getTempCoeff(&tempCoeff);
  120. usb_push_transfer( tx, &tempCoeff, sizeof(tempCoeff) );
  121. }
  122. return true;
  123. }
  124. break;
  125. // case PROTO_REQ_SET_TEMPERATURE_COEFF:
  126. // {
  127. // NFMClass->methods.tempCoefficient.setTempCoeff(pSetup->wValue);
  128. // return true;
  129. // }
  130. // break;
  131. }
  132. }
  133. break;
  134. }
  135. return usb_count_transfer( tx );
  136. }