Skip to content

Commit 6f163c0

Browse files
committed
sam: add HID library
1 parent b99bbc8 commit 6f163c0

File tree

4 files changed

+289
-2
lines changed

4 files changed

+289
-2
lines changed

libraries/HID/HID.cpp

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/* Copyright (c) 2015, Arduino LLC
2+
**
3+
** Original code (pre-library): Copyright (c) 2011, Peter Barrett
4+
**
5+
** Permission to use, copy, modify, and/or distribute this software for
6+
** any purpose with or without fee is hereby granted, provided that the
7+
** above copyright notice and this permission notice appear in all copies.
8+
**
9+
** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10+
** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11+
** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
12+
** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
13+
** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
14+
** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
15+
** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
16+
** SOFTWARE.
17+
*/
18+
19+
#include "USB/PluggableUSB.h"
20+
#include "HID.h"
21+
22+
HID_ HID;
23+
24+
static uint8_t HID_ENDPOINT_INT;
25+
26+
//================================================================================
27+
//================================================================================
28+
29+
// HID report descriptor
30+
31+
#define LSB(_x) ((_x) & 0xFF)
32+
#define MSB(_x) ((_x) >> 8)
33+
34+
#define RAWHID_USAGE_PAGE 0xFFC0
35+
#define RAWHID_USAGE 0x0C00
36+
#define RAWHID_TX_SIZE 64
37+
#define RAWHID_RX_SIZE 64
38+
39+
static uint8_t HID_INTERFACE;
40+
41+
HIDDescriptor _hidInterface;
42+
43+
static HIDDescriptorListNode* rootNode = NULL;
44+
static uint8_t sizeof_hidReportDescriptor = 0;
45+
static uint8_t modules_count = 0;
46+
//================================================================================
47+
//================================================================================
48+
// Driver
49+
50+
uint8_t _hid_protocol = 1;
51+
uint8_t _hid_idle = 1;
52+
53+
int HID_GetInterface(uint8_t* interfaceNum)
54+
{
55+
interfaceNum[0] += 1; // uses 1
56+
_hidInterface =
57+
{
58+
D_INTERFACE(HID_INTERFACE,1,3,0,0),
59+
D_HIDREPORT(sizeof_hidReportDescriptor),
60+
D_ENDPOINT(USB_ENDPOINT_IN (HID_ENDPOINT_INT),USB_ENDPOINT_TYPE_INTERRUPT,0x40,0x01)
61+
};
62+
return USBD_SendControl(0,&_hidInterface,sizeof(_hidInterface));
63+
}
64+
65+
int HID_GetDescriptor(int8_t t)
66+
{
67+
if (HID_REPORT_DESCRIPTOR_TYPE == t) {
68+
HIDDescriptorListNode* current = rootNode;
69+
int total = 0;
70+
while(current != NULL) {
71+
total += USBD_SendControl(0,current->cb->descriptor,current->cb->length);
72+
current = current->next;
73+
}
74+
return total;
75+
} else {
76+
return 0;
77+
}
78+
}
79+
80+
void HID_::AppendDescriptor(HIDDescriptorListNode *node)
81+
{
82+
if (modules_count == 0) {
83+
rootNode = node;
84+
} else {
85+
HIDDescriptorListNode *current = rootNode;
86+
while(current->next != NULL) {
87+
current = current->next;
88+
}
89+
current->next = node;
90+
}
91+
modules_count++;
92+
sizeof_hidReportDescriptor += node->cb->length;
93+
}
94+
95+
void HID_::SendReport(uint8_t id, const void* data, int len)
96+
{
97+
uint8_t p[64];
98+
const uint8_t *d = reinterpret_cast<const uint8_t *>(data);
99+
100+
p[0] = id;
101+
for (uint32_t i=0; i<len; i++)
102+
p[i+1] = d[i];
103+
USBD_Send(HID_TX, p, len+1);
104+
}
105+
106+
bool HID_Setup(USBSetup& setup, uint8_t i)
107+
{
108+
if (HID_INTERFACE != i) {
109+
return false;
110+
} else {
111+
uint8_t r = setup.bRequest;
112+
uint8_t requestType = setup.bmRequestType;
113+
if (REQUEST_DEVICETOHOST_CLASS_INTERFACE == requestType)
114+
{
115+
if (HID_GET_REPORT == r)
116+
{
117+
//HID_GetReport();
118+
return true;
119+
}
120+
if (HID_GET_PROTOCOL == r)
121+
{
122+
//Send8(_hid_protocol); // TODO
123+
return true;
124+
}
125+
}
126+
127+
if (REQUEST_HOSTTODEVICE_CLASS_INTERFACE == requestType)
128+
{
129+
if (HID_SET_PROTOCOL == r)
130+
{
131+
_hid_protocol = setup.wValueL;
132+
return true;
133+
}
134+
135+
if (HID_SET_IDLE == r)
136+
{
137+
_hid_idle = setup.wValueL;
138+
return true;
139+
}
140+
}
141+
return false;
142+
}
143+
}
144+
145+
HID_::HID_(void)
146+
{
147+
static uint8_t endpointType[1];
148+
149+
endpointType[0] = EP_TYPE_INTERRUPT_IN;
150+
151+
static PUSBCallbacks cb = {
152+
.setup = &HID_Setup,
153+
.getInterface = &HID_GetInterface,
154+
.getDescriptor = &HID_GetDescriptor,
155+
.numEndpoints = 1,
156+
.numInterfaces = 1,
157+
.endpointType = endpointType,
158+
};
159+
160+
static PUSBListNode node(&cb);
161+
162+
HID_ENDPOINT_INT = PUSB_AddFunction(&node, &HID_INTERFACE);
163+
}
164+
165+
int HID_::begin(void)
166+
{
167+
return 0;
168+
}

libraries/HID/HID.h

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,93 @@
1-
// HID.h
1+
/*
2+
HID.h
23
3-
// placeholder waiting for HID library to be DUE compatible
4+
Copyright (c) 2015, Arduino LLC
5+
Original code (pre-library): Copyright (c) 2011, Peter Barrett
6+
7+
This library is free software; you can redistribute it and/or
8+
modify it under the terms of the GNU Lesser General Public
9+
License as published by the Free Software Foundation; either
10+
version 2.1 of the License, or (at your option) any later version.
11+
12+
This library is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
Lesser General Public License for more details.
16+
17+
You should have received a copy of the GNU Lesser General Public
18+
License along with this library; if not, write to the Free Software
19+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20+
*/
21+
22+
#ifndef HID_h
23+
#define HID_h
24+
25+
#include <stdint.h>
26+
#include <Arduino.h>
27+
28+
#define _USING_HID
29+
30+
//================================================================================
31+
//================================================================================
32+
// HID 'Driver'
33+
34+
#define HID_GET_REPORT 0x01
35+
#define HID_GET_IDLE 0x02
36+
#define HID_GET_PROTOCOL 0x03
37+
#define HID_SET_REPORT 0x09
38+
#define HID_SET_IDLE 0x0A
39+
#define HID_SET_PROTOCOL 0x0B
40+
41+
#define HID_HID_DESCRIPTOR_TYPE 0x21
42+
#define HID_REPORT_DESCRIPTOR_TYPE 0x22
43+
#define HID_PHYSICAL_DESCRIPTOR_TYPE 0x23
44+
45+
typedef struct __attribute__((packed)) {
46+
uint8_t length;
47+
const void* descriptor;
48+
} HID_Descriptor;
49+
50+
class HIDDescriptorListNode {
51+
public:
52+
HIDDescriptorListNode *next = NULL;
53+
const HID_Descriptor * cb;
54+
HIDDescriptorListNode(const HID_Descriptor *ncb) {cb = ncb;}
55+
};
56+
57+
class HID_
58+
{
59+
public:
60+
HID_(void);
61+
int begin(void);
62+
void SendReport(uint8_t id, const void* data, int len);
63+
void AppendDescriptor(HIDDescriptorListNode* node);
64+
};
65+
66+
typedef struct
67+
{
68+
uint8_t len; // 9
69+
uint8_t dtype; // 0x21
70+
uint8_t addr;
71+
uint8_t versionL; // 0x101
72+
uint8_t versionH; // 0x101
73+
uint8_t country;
74+
uint8_t desctype; // 0x22 report
75+
uint8_t descLenL;
76+
uint8_t descLenH;
77+
} HIDDescDescriptor;
78+
79+
typedef struct
80+
{
81+
InterfaceDescriptor hid;
82+
HIDDescDescriptor desc;
83+
EndpointDescriptor in;
84+
} HIDDescriptor;
85+
86+
#define HID_TX HID_ENDPOINT_INT
87+
88+
#define D_HIDREPORT(_descriptorLength) \
89+
{ 9, 0x21, 0x1, 0x1, 0, 1, 0x22, _descriptorLength, 0 }
90+
91+
#define WEAK __attribute__ ((weak))
92+
93+
#endif

libraries/HID/keywords.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#######################################
2+
# Syntax Coloring Map HID
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
HID KEYWORD1
10+
11+
#######################################
12+
# Methods and Functions (KEYWORD2)
13+
#######################################
14+
begin KEYWORD2
15+
SendReport KEYWORD2
16+
AppendDescriptor KEYWORD2
17+
18+
#######################################
19+
# Constants (LITERAL1)
20+
#######################################
21+
HID_TX LITERAL1

libraries/HID/library.properties

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name=HID
2+
version=1.0
3+
author=Arduino
4+
maintainer=Arduino <[email protected]>
5+
sentence=Module for PluggableUSB infrastructure. Exposes an API for devices like Keyboards, Mice and Gamepads
6+
paragraph=
7+
url=http://www.arduino.cc/en/Reference/HID
8+
architectures=sam

0 commit comments

Comments
 (0)