瀏覽代碼

RAWHID Support

Needed HID Parser to support Bidirectional Transfers

The HidParser code was setup such that the claim for a report, the caller could say I want to claim the whole thinig and allowed callback functions for processing of in buffer and out buffer.

Allow RawHID to contribute Transfer_t

Since RawHID may need more resources than most, maybe it should contribute the additional structures

The constructor for a RAWHID object allows you to specify the top usage
that it wishes to connect to.  I used this for example to be able to
connect to a Teensy with the RAWHID associated with emulating the
Serial object.

If a HID Input class says that it wants to claim the whole interface, I
reuse the buffer associated with holding the HID descriptor and use it
for output buffers.
main
Kurt Eckhardt 7 年之前
父節點
當前提交
8aa67ff939
共有 10 個文件被更改,包括 305 次插入30 次删除
  1. +55
    -5
      USBHost_t36.h
  2. +56
    -2
      examples/Mouse/Mouse.ino
  3. +0
    -0
      examples/Mouse/defs.h
  4. +0
    -0
      examples/SerialTest/defs.h
  5. +56
    -11
      hid.cpp
  6. +4
    -4
      joystick.cpp
  7. +4
    -4
      keyboardHIDExtras.cpp
  8. +6
    -0
      keywords.txt
  9. +4
    -4
      mouse.cpp
  10. +120
    -0
      rawhid.cpp

+ 55
- 5
USBHost_t36.h 查看文件

@@ -80,6 +80,7 @@ class USBHost;
typedef struct Device_struct Device_t;
typedef struct Pipe_struct Pipe_t;
typedef struct Transfer_struct Transfer_t;
typedef enum { CLAIM_NO=0, CLAIM_REPORT, CLAIM_INTERFACE} hidclaim_t;

// All USB device drivers inherit use these classes.
// Drivers build user-visible functionality on top
@@ -269,11 +270,12 @@ protected:
static void disconnect_Device(Device_t *dev);
static void enumeration(const Transfer_t *transfer);
static void driver_ready_for_device(USBDriver *driver);
static volatile bool enumeration_busy;
public: // Maybe others may want/need to contribute memory example HID devices may want to add transfers.
static void contribute_Devices(Device_t *devices, uint32_t num);
static void contribute_Pipes(Pipe_t *pipes, uint32_t num);
static void contribute_Transfers(Transfer_t *transfers, uint32_t num);
static void contribute_String_Buffers(strbuf_t *strbuf, uint32_t num);
static volatile bool enumeration_busy;
private:
static void isr();
static void convertStringDescriptorToASCIIString(uint8_t string_index, Device_t *dev, const Transfer_t *transfer);
@@ -469,6 +471,8 @@ private:

// Device drivers may inherit from this base class, if they wish to receive
// HID input data fully decoded by the USBHIDParser driver
class USBHIDParser;

class USBHIDInput {
public:
operator bool() { return (mydevice != nullptr); }
@@ -482,7 +486,9 @@ public:
{ return ((mydevice == nullptr) || (mydevice->strbuf == nullptr)) ? nullptr : &mydevice->strbuf->buffer[mydevice->strbuf->iStrings[strbuf_t::STR_ID_SERIAL]]; }

private:
virtual bool claim_collection(Device_t *dev, uint32_t topusage);
virtual hidclaim_t claim_collection(USBHIDParser *driver, Device_t *dev, uint32_t topusage);
virtual bool hid_process_in_data(const Transfer_t *transfer) {return false;}
virtual bool hid_process_out_data(const Transfer_t *transfer) {return false;}
virtual void hid_input_begin(uint32_t topusage, uint32_t type, int lgmin, int lgmax);
virtual void hid_input_data(uint32_t usage, int32_t value);
virtual void hid_input_end();
@@ -576,10 +582,12 @@ private:

//--------------------------------------------------------------------------


class USBHIDParser : public USBDriver {
public:
USBHIDParser(USBHost &host) { init(); }
static void driver_ready_for_hid_collection(USBHIDInput *driver);
bool sendPacket(const uint8_t *buffer);
protected:
enum { TOPUSAGE_LIST_LEN = 4 };
enum { USAGE_LIST_LEN = 24 };
@@ -595,6 +603,14 @@ protected:
USBHIDInput * find_driver(uint32_t topusage);
void parse(uint16_t type_and_report_id, const uint8_t *data, uint32_t len);
void init();

// Atempt for RAWhid to take over processing of data
//
uint16_t inSize(void) {return in_size;}
uint16_t outSize(void) {return out_size;}

uint8_t activeSendMask(void) {return txstate;}

private:
Pipe_t *in_pipe;
Pipe_t *out_pipe;
@@ -611,6 +627,10 @@ private:
Pipe_t mypipes[3] __attribute__ ((aligned(32)));
Transfer_t mytransfers[4] __attribute__ ((aligned(32)));
strbuf_t mystring_bufs[1];
uint8_t txstate = 0;
uint8_t *tx1 = nullptr;
uint8_t *tx2 = nullptr;
bool hid_driver_claimed_control_ = false;
};

//--------------------------------------------------------------------------
@@ -686,7 +706,7 @@ public:
enum {MAX_KEYS_DOWN=4};
// uint32_t buttons() { return buttons_; }
protected:
virtual bool claim_collection(Device_t *dev, uint32_t topusage);
virtual hidclaim_t claim_collection(USBHIDParser *driver, Device_t *dev, uint32_t topusage);
virtual void hid_input_begin(uint32_t topusage, uint32_t type, int lgmin, int lgmax);
virtual void hid_input_data(uint32_t usage, int32_t value);
virtual void hid_input_end();
@@ -716,7 +736,7 @@ public:
int getWheel() { return wheel; }
int getWheelH() { return wheelH; }
protected:
virtual bool claim_collection(Device_t *dev, uint32_t topusage);
virtual hidclaim_t claim_collection(USBHIDParser *driver, Device_t *dev, uint32_t topusage);
virtual void hid_input_begin(uint32_t topusage, uint32_t type, int lgmin, int lgmax);
virtual void hid_input_data(uint32_t usage, int32_t value);
virtual void hid_input_end();
@@ -742,7 +762,7 @@ public:
uint32_t getButtons() { return buttons; }
int getAxis(uint32_t index) { return (index < (sizeof(axis)/sizeof(axis[0]))) ? axis[index] : 0; }
protected:
virtual bool claim_collection(Device_t *dev, uint32_t topusage);
virtual hidclaim_t claim_collection(USBHIDParser *driver, Device_t *dev, uint32_t topusage);
virtual void hid_input_begin(uint32_t topusage, uint32_t type, int lgmin, int lgmax);
virtual void hid_input_data(uint32_t usage, int32_t value);
virtual void hid_input_end();
@@ -1204,5 +1224,35 @@ private:
uint16_t wheelCircumference; // default is WHEEL_CIRCUMFERENCE (2122cm)
};

//--------------------------------------------------------------------------

class RawHIDController : public USBHIDInput {
public:
RawHIDController(USBHost &host, uint32_t usage = 0) : fixed_usage_(usage) { init(); }
uint32_t usage(void) {return usage_;}
void attachReceive(bool (*f)(uint32_t usage, const uint8_t *data, uint32_t len)) {receiveCB = f;}
bool sendPacket(const uint8_t *buffer);
protected:
virtual hidclaim_t claim_collection(USBHIDParser *driver, Device_t *dev, uint32_t topusage);
virtual bool hid_process_in_data(const Transfer_t *transfer);
virtual bool hid_process_out_data(const Transfer_t *transfer);
virtual void hid_input_begin(uint32_t topusage, uint32_t type, int lgmin, int lgmax);
virtual void hid_input_data(uint32_t usage, int32_t value);
virtual void hid_input_end();
virtual void disconnect_collection(Device_t *dev);
private:
void init();
USBHIDParser *driver_;
enum { MAX_PACKET_SIZE = 64 };
bool (*receiveCB)(uint32_t usage, const uint8_t *data, uint32_t len) = nullptr;
uint8_t collections_claimed = 0;
//volatile bool hid_input_begin_ = false;
uint32_t fixed_usage_;
uint32_t usage_ = 0;

// See if we can contribute transfers
Transfer_t mytransfers[2] __attribute__ ((aligned(32)));

};

#endif

+ 56
- 2
examples/Mouse/Mouse.ino 查看文件

@@ -19,6 +19,8 @@ USBHIDParser hid4(myusb);
USBHIDParser hid5(myusb);
MouseController mouse1(myusb);
JoystickController joystick1(myusb);
RawHIDController rawhid1(myusb);
RawHIDController rawhid2(myusb, 0xffc90004);

USBDriver *drivers[] = {&hub1, &hub2, &hub3, &hub4, &keyboard1, &keyboard2, &hid1, &hid2, &hid3, &hid4, &hid5};
#define CNT_DEVICES (sizeof(drivers)/sizeof(drivers[0]))
@@ -26,9 +28,9 @@ const char * driver_names[CNT_DEVICES] = {"Hub1","Hub2", "Hub3", "Hub4" "KB1", "
bool driver_active[CNT_DEVICES] = {false, false, false, false};

// Lets also look at HID Input devices
USBHIDInput *hiddrivers[] = {&mouse1, &joystick1};
USBHIDInput *hiddrivers[] = {&mouse1, &joystick1, &rawhid1, &rawhid2};
#define CNT_HIDDEVICES (sizeof(hiddrivers)/sizeof(hiddrivers[0]))
const char * hid_driver_names[CNT_DEVICES] = {"Mouse1","Joystick1"};
const char * hid_driver_names[CNT_DEVICES] = {"Mouse1","Joystick1", "RawHid1", "RawHid2"};
bool hid_driver_active[CNT_DEVICES] = {false, false};


@@ -42,6 +44,9 @@ void setup()
keyboard2.attachPress(OnPress);
hidextras.attachPress(OnHIDExtrasPress);
hidextras.attachRelease(OnHIDExtrasRelease);

rawhid1.attachReceive(OnReceiveHidData);
rawhid2.attachReceive(OnReceiveHidData);
}


@@ -123,6 +128,20 @@ void loop()
Serial.println();
joystick1.joystickDataClear();
}

// See if we have some RAW data
if (rawhid1) {
int ch;
uint8_t buffer[64];
uint8_t count_chars = 0;
memset(buffer, 0, sizeof(buffer));
if (Serial.available()) {
while (((ch = Serial.read()) != -1) && (count_chars < sizeof(buffer))) {
buffer[count_chars++] = ch;
}
rawhid1.sendPacket(buffer);
}
}
}


@@ -421,3 +440,38 @@ void OnHIDExtrasRelease(uint32_t top, uint16_t key)
Serial.print(") key release:");
Serial.println(key, HEX);
}

bool OnReceiveHidData(uint32_t usage, const uint8_t *data, uint32_t len) {
// Called for maybe both HIDS for rawhid basic test. One is for the Teensy
// to output to Serial. while still having Raw Hid...
if (usage == 0xffc90004) {
// Lets trim off trailing null characters.
while ((len > 0) && (data[len-1] == 0)) {
len--;
}
if (len) {
Serial.print("RawHid Serial: ");
Serial.write(data, len);
}
} else {
Serial.print("RawHID data: ");
Serial.println(usage, HEX);
while (len) {
uint8_t cb = (len > 16)? 16 : len;
const uint8_t *p = data;
uint8_t i;
for (i = 0; i < cb; i++) {
Serial.printf("%02x ", *p++);
}
Serial.print(": ");
for (i = 0; i < cb; i++) {
Serial.write(((*data >= ' ')&&(*data <= '~'))? *data : '.');
data++;
}
len -= cb;
Serial.println();
}
}

return true;
}

+ 0
- 0
examples/Mouse/defs.h 查看文件


+ 0
- 0
examples/SerialTest/defs.h 查看文件


+ 56
- 11
hid.cpp 查看文件

@@ -128,21 +128,19 @@ bool USBHIDParser::claim(Device_t *dev, int type, const uint8_t *descriptors, ui
if (((endpoint1 & 0xF0) == 0x80) && ((endpoint2 & 0xF0) == 0)) {
// first endpoint is IN, second endpoint is OUT
in_pipe = new_Pipe(dev, 3, endpoint1 & 0x0F, 1, size1, interval1);
//out_pipe = new_Pipe(dev, 3, endpoint2, 0, size2, interval2);
out_pipe = NULL; // TODO; fixme
out_pipe = new_Pipe(dev, 3, endpoint2, 0, size2, interval2);
in_size = size1;
out_size = size2;
} else if (((endpoint1 & 0xF0) == 0) && ((endpoint2 & 0xF0) == 0x80)) {
// first endpoint is OUT, second endpoint is IN
in_pipe = new_Pipe(dev, 3, endpoint2 & 0x0F, 1, size2, interval2);
//out_pipe = new_Pipe(dev, 3, endpoint1, 0, size1, interval1);
out_pipe = NULL; // TODO; fixme
out_pipe = new_Pipe(dev, 3, endpoint1, 0, size1, interval1);
in_size = size2;
out_size = size1;
} else {
return false;
}
//out_pipe->callback_function = out_callback;
out_pipe->callback_function = out_callback;
}
in_pipe->callback_function = in_callback;
for (uint32_t i=0; i < TOPUSAGE_LIST_LEN; i++) {
@@ -185,6 +183,7 @@ void USBHIDParser::in_callback(const Transfer_t *transfer)

void USBHIDParser::out_callback(const Transfer_t *transfer)
{
//println("USBHIDParser:: out_callback (static)");
if (transfer->driver) {
((USBHIDParser*)(transfer->driver))->out_data(transfer);
}
@@ -215,14 +214,22 @@ void USBHIDParser::in_data(const Transfer_t *transfer)
Serial.println(); */

print("HID: ");
print(use_report_id);
print(" - ");
print_hexbytes(transfer->buffer, transfer->length);
const uint8_t *buf = (const uint8_t *)transfer->buffer;
uint32_t len = transfer->length;
if (use_report_id == false) {
parse(0x0100, buf, len);
} else {
if (len > 1) {
parse(0x0100 | buf[0], buf + 1, len - 1);

// See if the first top report wishes to bypass the
// parse...
if (!(topusage_drivers[0] && topusage_drivers[0]->hid_process_in_data(transfer))) {

if (use_report_id == false) {
parse(0x0100, buf, len);
} else {
if (len > 1) {
parse(0x0100 | buf[0], buf + 1, len - 1);
}
}
}
queue_Data_Transfer(in_pipe, report, in_size, this);
@@ -231,8 +238,44 @@ void USBHIDParser::in_data(const Transfer_t *transfer)

void USBHIDParser::out_data(const Transfer_t *transfer)
{
println("USBHIDParser:out_data called (instance)");
// A packet completed. lets mark it as done and call back
// to top reports handler. We unmark our checkmark to
// handle case where they may want to queue up another one.
if (transfer->buffer == tx1) txstate &= ~1;
if (transfer->buffer == tx2) txstate &= ~2;
if (topusage_drivers[0]) {
topusage_drivers[0]->hid_process_out_data(transfer);
}
}

bool USBHIDParser::sendPacket(const uint8_t *buffer) {
if (!out_size || !out_pipe) return false;
if (!tx1) {
// Was not init before, for now lets put it at end of descriptor
// TODO: should verify that either don't exceed overlap descsize
// Or that we have taken over this device
tx1 = &descriptor[sizeof(descriptor) - out_size];
tx2 = tx1 - out_size;
}
if ((txstate & 3) == 3) return false; // both transmit buffers are full
uint8_t *p = tx1;
if ((txstate & 1) == 0) {
txstate |= 1;
} else {
txstate |= 2;
p = tx2;
}
// copy the users data into our out going buffer
memcpy(p, buffer, out_size);
println("USBHIDParser Send packet");
print_hexbytes(buffer, out_size);
queue_Data_Transfer(out_pipe, p, out_size, this);
println(" Queue_data transfer returned");
return true;
}


// This no-inputs parse is meant to be used when we first get the
// HID report descriptor. It finds all the top level collections
// and allows drivers to claim them. This is always where we
@@ -333,9 +376,11 @@ USBHIDInput * USBHIDParser::find_driver(uint32_t topusage)
{
println("find_driver");
USBHIDInput *driver = available_hid_drivers_list;
hidclaim_t claim_type;
while (driver) {
println(" driver ", (uint32_t)driver, HEX);
if (driver->claim_collection(device, topusage)) {
if ((claim_type = driver->claim_collection(this, device, topusage)) != CLAIM_NO) {
if (claim_type == CLAIM_INTERFACE) hid_driver_claimed_control_ = true;
return driver;
}
driver = driver->next;

+ 4
- 4
joystick.cpp 查看文件

@@ -26,16 +26,16 @@



bool JoystickController::claim_collection(Device_t *dev, uint32_t topusage)
hidclaim_t JoystickController::claim_collection(USBHIDParser *driver, Device_t *dev, uint32_t topusage)
{
// only claim Desktop/Joystick and Desktop/Gamepad
if (topusage != 0x10004 && topusage != 0x10005) return false;
if (topusage != 0x10004 && topusage != 0x10005) return CLAIM_NO;
// only claim from one physical device
if (mydevice != NULL && dev != mydevice) return false;
if (mydevice != NULL && dev != mydevice) return CLAIM_NO;
mydevice = dev;
collections_claimed++;
anychange = true; // always report values on first read
return true;
return CLAIM_REPORT;
}

void JoystickController::disconnect_collection(Device_t *dev)

+ 4
- 4
keyboardHIDExtras.cpp 查看文件

@@ -27,19 +27,19 @@
#define TOPUSAGE_SYS_CONTROL 0x10080
#define TOPUSAGE_CONSUMER_CONTROL 0x0c0001

bool KeyboardHIDExtrasController::claim_collection(Device_t *dev, uint32_t topusage)
hidclaim_t KeyboardHIDExtrasController::claim_collection(USBHIDParser *driver, Device_t *dev, uint32_t topusage)
{
// Lets try to claim a few specific Keyboard related collection/reports
//Serial.printf("KBH Claim %x\n", topusage);
if ((topusage != TOPUSAGE_SYS_CONTROL)
&& (topusage != TOPUSAGE_CONSUMER_CONTROL)
) return false;
) return CLAIM_NO;
// only claim from one physical device
//Serial.println("KeyboardHIDExtrasController claim collection");
if (mydevice != NULL && dev != mydevice) return false;
if (mydevice != NULL && dev != mydevice) return CLAIM_NO;
mydevice = dev;
collections_claimed_++;
return true;
return CLAIM_REPORT;
}

void KeyboardHIDExtrasController::disconnect_collection(Device_t *dev)

+ 6
- 0
keywords.txt 查看文件

@@ -9,6 +9,7 @@ USBSerial KEYWORD1
AntPlus KEYWORD1
JoystickController KEYWORD1
KeyboardHIDExtrasController KEYWORD1
RawHIDController KEYWORD1

# Common Functions
Task KEYWORD2
@@ -86,3 +87,8 @@ USBHOST_SERIAL_8N1 LITERAL1
USBHOST_SERIAL_8N2 LITERAL1
USBHOST_SERIAL_8E1 LITERAL1
USBHOST_SERIAL_8O1 LITERAL1

# RAWHid
usage KEYWORD2
attachReceive KEYWORD2
sendPacket KEYWORD2

+ 4
- 4
mouse.cpp 查看文件

@@ -26,15 +26,15 @@



bool MouseController::claim_collection(Device_t *dev, uint32_t topusage)
hidclaim_t MouseController::claim_collection(USBHIDParser *driver, Device_t *dev, uint32_t topusage)
{
// only claim Desktop/Mouse
if (topusage != 0x10002) return false;
if (topusage != 0x10002) return CLAIM_NO;
// only claim from one physical device
if (mydevice != NULL && dev != mydevice) return false;
if (mydevice != NULL && dev != mydevice) return CLAIM_NO;
mydevice = dev;
collections_claimed++;
return true;
return CLAIM_REPORT;
}

void MouseController::disconnect_collection(Device_t *dev)

+ 120
- 0
rawhid.cpp 查看文件

@@ -0,0 +1,120 @@
/* USB EHCI Host for Teensy 3.6
* Copyright 2017 Paul Stoffregen (paul@pjrc.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <Arduino.h>
#include "USBHost_t36.h" // Read this header first for key info

void RawHIDController::init()
{
USBHost::contribute_Transfers(mytransfers, sizeof(mytransfers)/sizeof(Transfer_t));
USBHIDParser::driver_ready_for_hid_collection(this);
}

hidclaim_t RawHIDController::claim_collection(USBHIDParser *driver, Device_t *dev, uint32_t topusage)
{
// only claim RAWHID devices currently: 16c0:0486
#ifdef USBHOST_PRINT_DEBUG
Serial.printf("Rawhid Claim: %x:%x usage: %x\n", dev->idVendor, dev->idProduct, topusage);
#endif

if ((dev->idVendor != 0x16c0 || (dev->idProduct) != 0x486)) return CLAIM_NO;
if (mydevice != NULL && dev != mydevice) return CLAIM_NO;
if (usage_) return CLAIM_NO; // Only claim one
if (fixed_usage_ && (fixed_usage_ != topusage)) return CLAIM_NO; // See if we want specific one and if so is it this one
mydevice = dev;
collections_claimed++;
usage_ = topusage;
driver_ = driver; // remember the driver.
return CLAIM_INTERFACE; // We wa
}

void RawHIDController::disconnect_collection(Device_t *dev)
{
if (--collections_claimed == 0) {
mydevice = NULL;
usage_ = 0;
}
}

bool RawHIDController::hid_process_in_data(const Transfer_t *transfer)
{
#ifdef USBHOST_PRINT_DEBUG
Serial.printf("RawHIDController::hid_process_in_data: %x\n", usage_);
#endif

if (receiveCB) {
return (*receiveCB)(usage_, (const uint8_t *)transfer->buffer, transfer->length);
}
return true;
}

bool RawHIDController::hid_process_out_data(const Transfer_t *transfer)
{
#ifdef USBHOST_PRINT_DEBUG
Serial.printf("RawHIDController::hid_process_out_data: %x\n", usage_);
#endif
return true;
}

bool RawHIDController::sendPacket(const uint8_t *buffer)
{
if (!driver_) return false;
return driver_->sendPacket(buffer);
}



void RawHIDController::hid_input_begin(uint32_t topusage, uint32_t type, int lgmin, int lgmax)
{
// These should not be called as we are claiming the whole interface and not
// allowing the parse to happen
#ifdef USBHOST_PRINT_DEBUG
Serial.printf("RawHID::hid_input_begin %x %x %x %x\n", topusage, type, lgmin, lgmax);
#endif
//hid_input_begin_ = true;
}

void RawHIDController::hid_input_data(uint32_t usage, int32_t value)
{
// These should not be called as we are claiming the whole interface and not
// allowing the parse to happen
#ifdef USBHOST_PRINT_DEBUG
Serial.printf("RawHID: usage=%X, value=%d", usage, value);
if ((value >= ' ') && (value <='~')) Serial.printf("(%c)", value);
Serial.println();
#endif
}

void RawHIDController::hid_input_end()
{
// These should not be called as we are claiming the whole interface and not
// allowing the parse to happen
#ifdef USBHOST_PRINT_DEBUG
Serial.println("RawHID::hid_input_end");
#endif
// if (hid_input_begin_) {
// hid_input_begin_ = false;
// }
}



Loading…
取消
儲存