You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UsbRaw.cpp 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // Created by robin on 1/8/16.
  3. //
  4. #include "UsbRaw.h"
  5. UsbRawDevice UsbRaw = UsbRawDevice();
  6. UsbRawDevice::UsbRawDevice()
  7. : _dataSend(0),
  8. _dataSendLen(0),
  9. _callback(0),
  10. _dataReceivePos(0),
  11. _dataReceiveLen(0)
  12. {
  13. }
  14. void UsbRawDevice::init()
  15. {
  16. PORTD = 0; // TODO: Only for USB pins?
  17. DDRD |= ~USBMASK;
  18. cli();
  19. usbDeviceDisconnect();
  20. usbDeviceConnect();
  21. usbInit();
  22. sei();
  23. }
  24. void UsbRawDevice::poll()
  25. {
  26. usbPoll();
  27. }
  28. void UsbRawDevice::setData(const uchar* data, uchar len)
  29. {
  30. _dataSend = data;
  31. _dataSendLen = len;
  32. }
  33. void UsbRawDevice::setDataString(const char* data)
  34. {
  35. setData((const uchar*)data, strlen(data) + 1);
  36. }
  37. void UsbRawDevice::setDataUsbNoMsg()
  38. {
  39. setData(0, USB_NO_MSG);
  40. }
  41. usbMsgLen_t UsbRawDevice::_usbFunctionSetup(usbRequest_t* rq)
  42. {
  43. _rq = *rq;
  44. _dataReceiveLen = min((uchar)rq->wLength.word, USB_RAW_DEVICE_BUFFER_SIZE);
  45. _dataReceivePos = 0;
  46. if (_callback)
  47. {
  48. _callback(rq, this, 0, 0);
  49. }
  50. usbMsgPtr = (uchar*)_dataSend;
  51. uchar len = _dataSendLen;
  52. setData(0, 0);
  53. return len;
  54. }
  55. uchar UsbRawDevice::_usbFunctionWrite(uchar *data, uchar len)
  56. {
  57. for (uchar i = 0; i < len && _dataReceivePos < _dataReceiveLen; ++i, ++_dataReceivePos)
  58. {
  59. _dataReceive[_dataReceivePos] = data[i];
  60. }
  61. if (_dataReceiveLen == _dataReceivePos)
  62. {
  63. if (_callback)
  64. {
  65. _callback(&_rq, this, _dataReceive, _dataReceiveLen);
  66. }
  67. return 1;
  68. }
  69. return 0;
  70. }
  71. void UsbRawDevice::setCallback(void (*callback)(CALLBACK_ARGS))
  72. {
  73. _callback = callback;
  74. }
  75. uchar usbFunctionWrite(uchar *data, uchar len)
  76. {
  77. return UsbRaw._usbFunctionWrite(data, len);
  78. }
  79. usbMsgLen_t usbFunctionSetup(uchar data[8])
  80. {
  81. return UsbRaw._usbFunctionSetup((usbRequest_t*)(void*)data);
  82. }