ソースを参照

switch to raw usb

master
Robin Thoni 8年前
コミット
f163388027

+ 83
- 0
UsbRaw.cpp ファイルの表示

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

+ 51
- 0
UsbRaw.h ファイルの表示

@@ -0,0 +1,51 @@
1
+/*
2
+ * Based on Obdev's AVRUSB code and under the same license.
3
+ * Modified by Robin Thoni <robin@rthoni.com>
4
+ */
5
+#ifndef __UsbRaw_h__
6
+#define __UsbRaw_h__
7
+
8
+#include <avr/pgmspace.h>
9
+#include <avr/interrupt.h>
10
+#include <Arduino.h>
11
+
12
+#include "usbdrv.h"
13
+
14
+#define CALLBACK_ARGS usbRequest_t* rq, UsbRawDevice* dev, uchar* data, uchar len
15
+
16
+class UsbRawDevice {
17
+public:
18
+    UsbRawDevice();
19
+
20
+    void init();
21
+    
22
+    void poll();
23
+
24
+    void setData(const uchar* data, uchar len);
25
+
26
+    void setDataString(const char* data);
27
+
28
+    usbMsgLen_t _usbFunctionSetup(usbRequest_t* rq);
29
+
30
+    uchar _usbFunctionWrite(uchar *data, uchar len);
31
+
32
+    void setCallback(void (*callback)(CALLBACK_ARGS));
33
+
34
+private:
35
+
36
+    const uchar* _dataSend;
37
+
38
+    uchar _dataSendLen;
39
+
40
+    void (*_callback)(CALLBACK_ARGS);
41
+
42
+    uchar* _dataReceived;
43
+
44
+    uchar _dataReceivedLen;
45
+
46
+    usbRequest_t* _rq;
47
+};
48
+
49
+extern UsbRawDevice UsbRaw;
50
+
51
+#endif // __UsbRaw_h__

+ 0
- 54
UsbRawHid.h ファイルの表示

@@ -1,54 +0,0 @@
1
-/*
2
- * Based on Obdev's AVRUSB code and under the same license.
3
- * Modified by Robin Thoni <robin@rthoni.com>
4
- */
5
-#ifndef __UsbKeyboard_h__
6
-#define __UsbKeyboard_h__
7
-
8
-#include <avr/pgmspace.h>
9
-#include <avr/interrupt.h>
10
-#include <string.h>
11
-
12
-#include "usbdrv.h"
13
-
14
-// TODO: Work around Arduino 12 issues better.
15
-//#include <WConstants.h>
16
-//#undef int()
17
-
18
-typedef uint8_t byte;
19
-
20
-#define WAIT_USB do {           \
21
-  UsbRawHid.update();           \
22
-  if (!usbInterruptIsReady())   \
23
-  {                             \
24
-    return;                     \
25
-  }                             \
26
-} while(0)
27
-
28
-#define BUFFER_SIZE 64
29
-
30
-
31
-static uchar    idleRate;/* repeat rate for keyboards, never used for mice */
32
-
33
-class UsbRawHidDevice {
34
-public:
35
-    UsbRawHidDevice();
36
-
37
-    bool isUsbReady();
38
-    
39
-    void update() {
40
-        usbPoll();
41
-    }
42
-
43
-    void sendData(const char* data, unsigned size = 0);
44
-    
45
-    //private: TODO: Make friend?
46
-    byte reportBuffer[BUFFER_SIZE];    // buffer for HID reports [ 1 modifier byte + (len-1) key strokes]
47
-
48
-private:
49
-
50
-};
51
-
52
-#include "UsbRawHid.hxx"
53
-
54
-#endif // __UsbKeyboard_h__

+ 0
- 85
UsbRawHid.hxx ファイルの表示

@@ -1,85 +0,0 @@
1
-//
2
-// Created by robin on 1/8/16.
3
-//
4
-
5
-UsbRawHidDevice UsbRawHid = UsbRawHidDevice();
6
-
7
-
8
-/* We use a simplifed keyboard report descriptor which does not support the
9
- * boot protocol. We don't allow setting status LEDs and but we do allow
10
- * simultaneous key presses.
11
- * The report descriptor has been created with usb.org's "HID Descriptor Tool"
12
- * which can be downloaded from http://www.usb.org/developers/hidpage/.
13
- * Redundant entries (such as LOGICAL_MINIMUM and USAGE_PAGE) have been omitted
14
- * for the second INPUT item.
15
- */
16
-PROGMEM const char usbHidReportDescriptor[32] = {
17
-        0x06, 0x42, 0xff,
18
-        0x0A, 0x42, 0xff,
19
-        0xA1, 0x01,				// Collection 0x01
20
-        0x75, 0x08,				// report size = 8 bits
21
-        0x15, 0x00,				// logical minimum = 0
22
-        0x26, 0xFF, 0x00,			// logical maximum = 255
23
-        0x95, RAWHID_TX_SIZE,			// report count
24
-        0x09, 0x01,				// usage
25
-        0x81, 0x02,				// Input (array)
26
-        0x95, RAWHID_RX_SIZE,			// report count
27
-        0x09, 0x02,				// usage
28
-        0x91, 0x02,				// Output (array)
29
-        0xC0					// end collection
30
-};
31
-
32
-UsbRawHidDevice::UsbRawHidDevice()
33
-{
34
-    PORTD = 0; // TODO: Only for USB pins?
35
-    DDRD |= ~USBMASK;
36
-
37
-    cli();
38
-    usbDeviceDisconnect();
39
-    usbDeviceConnect();
40
-    usbInit();
41
-    sei();
42
-    //sendData("", 0);
43
-}
44
-
45
-bool UsbRawHidDevice::isUsbReady()
46
-{
47
-    update();
48
-    return usbInterruptIsReady();
49
-}
50
-
51
-void UsbRawHidDevice::sendData(const char* data, unsigned size)
52
-{
53
-    if (size > RAWHID_TX_SIZE || size == 0)
54
-    {
55
-        size = RAWHID_TX_SIZE;
56
-    }
57
-    memcpy(reportBuffer, data, size);
58
-    memset(reportBuffer + size, 0, RAWHID_TX_SIZE - size);
59
-
60
-    usbSetInterrupt((unsigned char*)(void *)&reportBuffer, sizeof(reportBuffer));
61
-}
62
-
63
-usbMsgLen_t usbFunctionSetup(uchar data[8])
64
-{
65
-    usbRequest_t    *rq = (usbRequest_t*)(void*)data;
66
-
67
-    /* The following requests are never used. But since they are required by
68
-     * the specification, we implement them in this example.
69
-     */
70
-    if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){    /* class request type */
71
-        if(rq->bRequest == USBRQ_HID_GET_REPORT){  /* wValue: ReportType (highbyte), ReportID (lowbyte) */
72
-            /* we only have one report type, so don't look at wValue */
73
-            usbMsgPtr = (unsigned char*)(void *)&UsbRawHid.reportBuffer;
74
-            return sizeof(UsbRawHid.reportBuffer);
75
-        }else if(rq->bRequest == USBRQ_HID_GET_IDLE){
76
-            usbMsgPtr = &idleRate;
77
-            return 1;
78
-        }else if(rq->bRequest == USBRQ_HID_SET_IDLE){
79
-            idleRate = rq->wValue.bytes[1];
80
-        }
81
-    }else{
82
-        /* no vendor specific requests implemented */
83
-    }
84
-    return 0;   /* default for not implemented requests: return no data back to host */
85
-}

examples/UsbRawHidDemo1/.gitignore → examples/UsbRawDemo1/.gitignore ファイルの表示


examples/UsbRawHidDemo1/CMakeLists.txt → examples/UsbRawDemo1/CMakeLists.txt ファイルの表示

@@ -1,6 +1,6 @@
1 1
 cmake_minimum_required(VERSION 2.8.4)
2 2
 set(CMAKE_TOOLCHAIN_FILE ${CMAKE_SOURCE_DIR}/cmake/ArduinoToolchain.cmake)
3
-set(PROJECT_NAME rawhid)
3
+set(PROJECT_NAME usbraw)
4 4
 project(${PROJECT_NAME})
5 5
 
6 6
 set(${CMAKE_PROJECT_NAME}_BOARD nano328)

examples/UsbRawHidDemo1/cmake/ArduinoToolchain.cmake → examples/UsbRawDemo1/cmake/ArduinoToolchain.cmake ファイルの表示


examples/UsbRawHidDemo1/cmake/Platform/Arduino.cmake → examples/UsbRawDemo1/cmake/Platform/Arduino.cmake ファイルの表示


+ 60
- 0
examples/UsbRawDemo1/main.ino ファイルの表示

@@ -0,0 +1,60 @@
1
+#define ARD_UTILS_DELAYMS
2
+#include "ArdUtils/ArdUtils.h"
3
+
4
+#include "UsbRaw.h"
5
+
6
+#define ledPin 13
7
+
8
+uchar buffer[50];
9
+
10
+void callback(CALLBACK_ARGS)
11
+{
12
+    if (rq->bRequest == 0)
13
+    {
14
+        digitalWrite(ledPin, !digitalRead(ledPin));
15
+    }
16
+    else if (rq->bRequest == 1)
17
+    {
18
+        digitalWrite(ledPin, 1);
19
+    }
20
+    else if (rq->bRequest == 4)
21
+    {
22
+        digitalWrite(ledPin, !digitalRead(ledPin));
23
+        /*if (!data)
24
+        {
25
+            dev->setData(0, USB_NO_MSG);
26
+        }
27
+        else
28
+        {
29
+            dev->setData(data, sizeof(data));
30
+        }*/
31
+        if (data)
32
+        {
33
+            dev->setDataString("response");
34
+        }
35
+    }
36
+    else
37
+    {
38
+        dev->setData(data, strlen((const char*)data) + 1);
39
+    }
40
+}
41
+
42
+void setup()
43
+{
44
+    strcpy((char*)buffer, "test");
45
+    UsbRaw.init();
46
+    UsbRaw.setCallback(callback);
47
+    pinMode(ledPin, OUTPUT);
48
+    digitalWrite(ledPin, HIGH);
49
+}
50
+
51
+void loop()
52
+{
53
+    UsbRaw.poll();
54
+    //WAIT_USB;
55
+
56
+    //UsbRawHid.sendData("test", 4);
57
+
58
+    //ArdUtils::delayMs(1000);
59
+    //digitalWrite(ledPin, !digitalRead(ledPin));
60
+}

+ 0
- 24
examples/UsbRawHidDemo1/main.ino ファイルの表示

@@ -1,24 +0,0 @@
1
-#define ARD_UTILS_DELAYMS
2
-#include "ArdUtils/ArdUtils.h"
3
-
4
-#define RAWHID_TX_SIZE       64
5
-#define RAWHID_RX_SIZE       64
6
-#include "UsbRawHid.h"
7
-
8
-#define ledPin 13
9
-
10
-void setup()
11
-{
12
-    pinMode (ledPin, OUTPUT);
13
-    digitalWrite (ledPin, HIGH);
14
-}
15
-
16
-void loop()
17
-{
18
-    WAIT_USB;
19
-
20
-    //UsbRawHid.sendData("test", 4);
21
-
22
-    ArdUtils::delayMs(1000);
23
-    digitalWrite(ledPin, !digitalRead(ledPin));
24
-}

+ 5
- 5
usbconfig.h ファイルの表示

@@ -122,7 +122,7 @@ section at the end of this file).
122 122
  * The value is in milliamperes. [It will be divided by two since USB
123 123
  * communicates power requirements in units of 2 mA.]
124 124
  */
125
-#define USB_CFG_IMPLEMENT_FN_WRITE      0
125
+#define USB_CFG_IMPLEMENT_FN_WRITE      1
126 126
 /* Set this to 1 if you want usbFunctionWrite() to be called for control-out
127 127
  * transfers. Set it to 0 if you don't need it and want to save a couple of
128 128
  * bytes.
@@ -212,7 +212,7 @@ section at the end of this file).
212 212
 
213 213
 /* -------------------------- Device Description --------------------------- */
214 214
 
215
-#define  USB_CFG_VENDOR_ID       0x42, 0x42
215
+#define  USB_CFG_VENDOR_ID       0xc0, 0x16
216 216
 /* USB vendor ID for the device, low byte first. If you have registered your
217 217
  * own Vendor ID, define it here. Otherwise you may use one of obdev's free
218 218
  * shared VID/PID pairs. Be sure to read USB-IDs-for-free.txt for rules!
@@ -221,7 +221,7 @@ section at the end of this file).
221 221
  * with libusb: 0x16c0/0x5dc.  Use this VID/PID pair ONLY if you understand
222 222
  * the implications!
223 223
  */
224
-#define  USB_CFG_DEVICE_ID       0x31, 0xe1
224
+#define  USB_CFG_DEVICE_ID       0xdc, 0x05
225 225
 /* This is the ID of the product, low byte first. It is interpreted in the
226 226
  * scope of the vendor ID. If you have registered your own VID with usb.org
227 227
  * or if you have licensed a PID from somebody else, define it here. Otherwise
@@ -265,7 +265,7 @@ section at the end of this file).
265 265
 /* See USB specification if you want to conform to an existing device class.
266 266
  * Class 0xff is "vendor specific".
267 267
  */
268
-#define USB_CFG_INTERFACE_CLASS     0x03  /* HID */ /* define class here if not at device level */
268
+#define USB_CFG_INTERFACE_CLASS     0
269 269
 #define USB_CFG_INTERFACE_SUBCLASS  0
270 270
 #define USB_CFG_INTERFACE_PROTOCOL  0
271 271
 /* See USB specification if you want to conform to an existing device class or
@@ -273,7 +273,7 @@ section at the end of this file).
273 273
  * HID class is 3, no subclass and protocol required (but may be useful!)
274 274
  * CDC class is 2, use subclass 2 and protocol 1 for ACM
275 275
  */
276
-#define USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH    32
276
+#define USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH    0
277 277
 /* Define this to the length of the HID report descriptor, if you implement
278 278
  * an HID device. Otherwise don't define it or define it to 0.
279 279
  * If you use this define, you must add a PROGMEM character array named

usbdrv.c → usbdrv.cpp ファイルの表示


読み込み中…
キャンセル
保存