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.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. _dataReceived(0),
  11. _dataReceivedLen(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));
  36. }
  37. usbMsgLen_t UsbRawDevice::_usbFunctionSetup(usbRequest_t* rq)
  38. {
  39. _rq = rq;
  40. if (_callback)
  41. {
  42. _callback(rq, this, 0, 0);
  43. }
  44. usbMsgPtr = (uchar*)_dataSend;
  45. uchar len = _dataSendLen;
  46. setData(0, 0);
  47. return len;
  48. }
  49. uchar UsbRawDevice::_usbFunctionWrite(uchar *data, uchar len)
  50. {
  51. free(_dataReceived);
  52. _dataReceived = (uchar*)memcpy(malloc(len), data, _dataReceivedLen = len);
  53. if (_callback)
  54. {
  55. _callback(_rq, this, _dataReceived, len);
  56. }
  57. return 1;
  58. }
  59. void UsbRawDevice::setCallback(void (*callback)(CALLBACK_ARGS))
  60. {
  61. _callback = callback;
  62. }
  63. uchar usbFunctionWrite(uchar *data, uchar len)
  64. {
  65. return UsbRaw._usbFunctionWrite(data, len);
  66. }
  67. usbMsgLen_t usbFunctionSetup(uchar data[8])
  68. {
  69. return UsbRaw._usbFunctionSetup((usbRequest_t*)(void*)data);
  70. }