Skip to content

Commit a1c1ba2

Browse files
author
LuiCat
committed
Added setup files to customize Arduino
1 parent 951b5a2 commit a1c1ba2

File tree

3 files changed

+327
-0
lines changed

3 files changed

+327
-0
lines changed

setup/HID.cpp

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
Copyright (c) 2015, Arduino LLC
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 "HID.h"
20+
21+
#if defined(USBCON)
22+
23+
HID_& HID()
24+
{
25+
static HID_ obj;
26+
return obj;
27+
}
28+
29+
int HID_::getInterface(uint8_t* interfaceCount)
30+
{
31+
*interfaceCount += 1; // uses 1
32+
HIDDescriptor hidInterface = {
33+
D_INTERFACE(pluggedInterface, 1, USB_DEVICE_CLASS_HUMAN_INTERFACE, HID_SUBCLASS_NONE, HID_PROTOCOL_NONE),
34+
D_HIDREPORT(descriptorSize),
35+
D_ENDPOINT(USB_ENDPOINT_IN(pluggedEndpoint), USB_ENDPOINT_TYPE_INTERRUPT, USB_EP_SIZE, 0x01)
36+
};
37+
return USB_SendControl(0, &hidInterface, sizeof(hidInterface));
38+
}
39+
40+
int HID_::getDescriptor(USBSetup& setup)
41+
{
42+
// Check if this is a HID Class Descriptor request
43+
if (setup.bmRequestType != REQUEST_DEVICETOHOST_STANDARD_INTERFACE) { return 0; }
44+
if (setup.wValueH != HID_REPORT_DESCRIPTOR_TYPE) { return 0; }
45+
46+
// In a HID Class Descriptor wIndex cointains the interface number
47+
if (setup.wIndex != pluggedInterface) { return 0; }
48+
49+
int total = 0;
50+
HIDSubDescriptor* node;
51+
for (node = rootNode; node; node = node->next) {
52+
int res = USB_SendControl(TRANSFER_PGM, node->data, node->length);
53+
if (res == -1)
54+
return -1;
55+
total += res;
56+
}
57+
58+
// Reset the protocol on reenumeration. Normally the host should not assume the state of the protocol
59+
// due to the USB specs, but Windows and Linux just assumes its in report mode.
60+
protocol = HID_REPORT_PROTOCOL;
61+
62+
return total;
63+
}
64+
65+
uint8_t HID_::getShortName(char *name)
66+
{
67+
name[0] = 'H';
68+
name[1] = 'I';
69+
name[2] = 'D';
70+
name[3] = 'A' + (descriptorSize & 0x0F);
71+
name[4] = 'A' + ((descriptorSize >> 4) & 0x0F);
72+
return 5;
73+
}
74+
75+
void HID_::AppendDescriptor(HIDSubDescriptor *node)
76+
{
77+
if (!rootNode) {
78+
rootNode = node;
79+
} else {
80+
HIDSubDescriptor *current = rootNode;
81+
while (current->next) {
82+
current = current->next;
83+
}
84+
current->next = node;
85+
}
86+
descriptorSize += node->length;
87+
}
88+
89+
int HID_::SendReport(uint8_t id, const void* data, int len)
90+
{
91+
auto ret = USB_Send(pluggedEndpoint, &id, 1);
92+
if (ret < 0) return ret;
93+
auto ret2 = USB_Send(pluggedEndpoint | TRANSFER_RELEASE, data, len);
94+
if (ret2 < 0) return ret2;
95+
return ret + ret2;
96+
}
97+
98+
bool HID_::setup(USBSetup& setup)
99+
{
100+
if (pluggedInterface != setup.wIndex) {
101+
return false;
102+
}
103+
104+
uint8_t request = setup.bRequest;
105+
uint8_t requestType = setup.bmRequestType;
106+
107+
if (requestType == REQUEST_DEVICETOHOST_CLASS_INTERFACE)
108+
{
109+
if (request == HID_GET_REPORT) {
110+
// TODO: HID_GetReport();
111+
return true;
112+
}
113+
if (request == HID_GET_PROTOCOL) {
114+
// TODO: Send8(protocol);
115+
return true;
116+
}
117+
if (request == HID_GET_IDLE) {
118+
// TODO: Send8(idle);
119+
}
120+
}
121+
122+
if (requestType == REQUEST_HOSTTODEVICE_CLASS_INTERFACE)
123+
{
124+
if (request == HID_SET_PROTOCOL) {
125+
// The USB Host tells us if we are in boot or report mode.
126+
// This only works with a real boot compatible device.
127+
protocol = setup.wValueL;
128+
return true;
129+
}
130+
if (request == HID_SET_IDLE) {
131+
idle = setup.wValueL;
132+
return true;
133+
}
134+
if (request == HID_SET_REPORT)
135+
{
136+
//uint8_t reportID = setup.wValueL;
137+
//uint16_t length = setup.wLength;
138+
//uint8_t data[length];
139+
// Make sure to not read more data than USB_EP_SIZE.
140+
// You can read multiple times through a loop.
141+
// The first byte (may!) contain the reportID on a multreport.
142+
//USB_RecvControl(data, length);
143+
}
144+
}
145+
146+
return false;
147+
}
148+
149+
HID_::HID_(void) : PluggableUSBModule(1, 1, epType),
150+
rootNode(NULL), descriptorSize(0),
151+
protocol(HID_REPORT_PROTOCOL), idle(1)
152+
{
153+
epType[0] = EP_TYPE_INTERRUPT_IN;
154+
PluggableUSB().plug(this);
155+
}
156+
157+
int HID_::begin(void)
158+
{
159+
return 0;
160+
}
161+
162+
#endif /* if defined(USBCON) */

setup/HID.h

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
Copyright (c) 2015, Arduino LLC
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+
#ifndef HID_h
20+
#define HID_h
21+
22+
#include <stdint.h>
23+
#include <Arduino.h>
24+
#include "PluggableUSB.h"
25+
26+
#if defined(USBCON)
27+
28+
#define _USING_HID
29+
30+
// HID 'Driver'
31+
// ------------
32+
#define HID_GET_REPORT 0x01
33+
#define HID_GET_IDLE 0x02
34+
#define HID_GET_PROTOCOL 0x03
35+
#define HID_SET_REPORT 0x09
36+
#define HID_SET_IDLE 0x0A
37+
#define HID_SET_PROTOCOL 0x0B
38+
39+
#define HID_HID_DESCRIPTOR_TYPE 0x21
40+
#define HID_REPORT_DESCRIPTOR_TYPE 0x22
41+
#define HID_PHYSICAL_DESCRIPTOR_TYPE 0x23
42+
43+
// HID subclass HID1.11 Page 8 4.2 Subclass
44+
#define HID_SUBCLASS_NONE 0
45+
#define HID_SUBCLASS_BOOT_INTERFACE 1
46+
47+
// HID Keyboard/Mouse bios compatible protocols HID1.11 Page 9 4.3 Protocols
48+
#define HID_PROTOCOL_NONE 0
49+
#define HID_PROTOCOL_KEYBOARD 1
50+
#define HID_PROTOCOL_MOUSE 2
51+
52+
// Normal or bios protocol (Keyboard/Mouse) HID1.11 Page 54 7.2.5 Get_Protocol Request
53+
// "protocol" variable is used for this purpose.
54+
#define HID_BOOT_PROTOCOL 0
55+
#define HID_REPORT_PROTOCOL 1
56+
57+
// HID Request Type HID1.11 Page 51 7.2.1 Get_Report Request
58+
#define HID_REPORT_TYPE_INPUT 1
59+
#define HID_REPORT_TYPE_OUTPUT 2
60+
#define HID_REPORT_TYPE_FEATURE 3
61+
62+
typedef struct
63+
{
64+
uint8_t len; // 9
65+
uint8_t dtype; // 0x21
66+
uint8_t addr;
67+
uint8_t versionL; // 0x101
68+
uint8_t versionH; // 0x101
69+
uint8_t country;
70+
uint8_t desctype; // 0x22 report
71+
uint8_t descLenL;
72+
uint8_t descLenH;
73+
} HIDDescDescriptor;
74+
75+
typedef struct
76+
{
77+
InterfaceDescriptor hid;
78+
HIDDescDescriptor desc;
79+
EndpointDescriptor in;
80+
} HIDDescriptor;
81+
82+
class HIDSubDescriptor {
83+
public:
84+
HIDSubDescriptor *next = NULL;
85+
HIDSubDescriptor(const void *d, const uint16_t l) : data(d), length(l) { }
86+
87+
const void* data;
88+
const uint16_t length;
89+
};
90+
91+
class HID_ : public PluggableUSBModule
92+
{
93+
public:
94+
HID_(void);
95+
int begin(void);
96+
int SendReport(uint8_t id, const void* data, int len);
97+
int SendRaw(const void* data, int len);
98+
void AppendDescriptor(HIDSubDescriptor* node);
99+
void PrependDescriptor(HIDSubDescriptor* node);
100+
101+
protected:
102+
// Implementation of the PluggableUSBModule
103+
int getInterface(uint8_t* interfaceCount);
104+
int getDescriptor(USBSetup& setup);
105+
bool setup(USBSetup& setup);
106+
uint8_t getShortName(char* name);
107+
108+
private:
109+
uint8_t epType[1];
110+
111+
HIDSubDescriptor* rootNode;
112+
uint16_t descriptorSize;
113+
114+
uint8_t protocol;
115+
uint8_t idle;
116+
};
117+
118+
// Replacement for global singleton.
119+
// This function prevents static-initialization-order-fiasco
120+
// https://isocpp.org/wiki/faq/ctors#static-init-order-on-first-use
121+
HID_& HID();
122+
123+
#define D_HIDREPORT(length) { 9, 0x21, 0x01, 0x01, 0, 1, 0x22, lowByte(length), highByte(length) }
124+
125+
#endif // USBCON
126+
127+
#endif // HID_h

setup/boards.txt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
##############################################################
3+
# Nintendo Switch Controller for Micro/Leonardo #
4+
##############################################################
5+
6+
ns_con.name=Nintendo Switch Controller (Micro)
7+
8+
ns_con.vid.1=0x0F0D
9+
ns_con.pid.1=0x0092
10+
11+
ns_con.upload.tool=avrdude
12+
ns_con.upload.protocol=avr109
13+
ns_con.upload.maximum_size=28672
14+
ns_con.upload.maximum_data_size=2560
15+
ns_con.upload.speed=57600
16+
ns_con.upload.disable_flushing=true
17+
ns_con.upload.use_1200bps_touch=true
18+
ns_con.upload.wait_for_upload_port=true
19+
20+
ns_con.bootloader.tool=avrdude
21+
ns_con.bootloader.low_fuses=0xff
22+
ns_con.bootloader.high_fuses=0xd8
23+
ns_con.bootloader.extended_fuses=0xcb
24+
ns_con.bootloader.file=caterina/Caterina-Micro.hex
25+
ns_con.bootloader.unlock_bits=0x3F
26+
ns_con.bootloader.lock_bits=0x2F
27+
28+
ns_con.build.mcu=atmega32u4
29+
ns_con.build.f_cpu=16000000L
30+
ns_con.build.board=AVR_MICRO
31+
ns_con.build.core=arduino
32+
ns_con.build.variant=micro
33+
34+
ns_con.build.vid=0x0F0D
35+
ns_con.build.pid=0x0092
36+
ns_con.build.usb_manufacturer="HORI CO.,LTD."
37+
ns_con.build.usb_product="POKKEN CONTROLLER"
38+
ns_con.build.extra_flags={build.usb_flags}

0 commit comments

Comments
 (0)