Skip to content

Commit 1ffca94

Browse files
committed
add PluggableUSB module
1 parent 54d2db8 commit 1ffca94

File tree

5 files changed

+162
-11
lines changed

5 files changed

+162
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
2+
3+
/* 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 "USBAPI.h"
20+
#include "PluggableUSB.h"
21+
22+
#if defined(USBCON)
23+
#ifdef PLUGGABLE_USB_ENABLED
24+
25+
#define MAX_MODULES 6
26+
27+
static u8 startIf = CDC_ACM_INTERFACE + CDC_INTERFACE_COUNT;
28+
static u8 firstEp = CDC_FIRST_ENDPOINT + CDC_ENPOINT_COUNT;
29+
30+
static u8 lastIf = CDC_ACM_INTERFACE + CDC_INTERFACE_COUNT;
31+
static u8 lastEp = CDC_FIRST_ENDPOINT + CDC_ENPOINT_COUNT;
32+
33+
extern u8 _initEndpoints[];
34+
35+
PUSBCallbacks cbs[MAX_MODULES];
36+
u8 modules_count = 0;
37+
38+
int PUSB_GetInterface(u8* interfaceNum)
39+
{
40+
int ret = 0;
41+
for (u8 i=0; i<modules_count; i++) {
42+
ret = cbs[i].getInterface(interfaceNum);
43+
}
44+
return ret;
45+
}
46+
47+
int PUSB_GetDescriptor(int t)
48+
{
49+
int ret = 0;
50+
for (u8 i=0; i<modules_count && ret == 0; i++) {
51+
ret = cbs[i].getDescriptor(t);
52+
}
53+
return ret;
54+
}
55+
56+
bool PUSB_Setup(Setup& setup, u8 j)
57+
{
58+
bool ret = false;
59+
for (u8 i=0; i<modules_count && ret == false; i++) {
60+
ret = cbs[i].setup(setup, j);
61+
}
62+
return ret;
63+
}
64+
65+
int PUSB_AddFunction(PUSBCallbacks *cb, u8* interface)
66+
{
67+
if (modules_count >= MAX_MODULES) {
68+
return 0;
69+
}
70+
cbs[modules_count] = *cb;
71+
72+
*interface = lastIf;
73+
lastIf++;
74+
for ( u8 i = 0; i< cb->numEndpoints; i++) {
75+
_initEndpoints[lastEp] = cb->endpointType[i];
76+
lastEp++;
77+
}
78+
modules_count++;
79+
return lastEp-1;
80+
// restart USB layer???
81+
}
82+
83+
#endif
84+
85+
#endif /* if defined(USBCON) */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
IPAddress.h - Base class that provides IPAddress
3+
Copyright (c) 2011 Adrian McEwen. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#ifndef PUSB_h
21+
#define PUSB_h
22+
23+
#include "USBAPI.h"
24+
#include <stdint.h>
25+
26+
#if defined(USBCON)
27+
28+
typedef struct
29+
{
30+
bool (*setup)(Setup& setup, u8 i);
31+
int (*getInterface)(u8* interfaceNum);
32+
int (*getDescriptor)(int t);
33+
int numEndpoints;
34+
u8 endpointType[6];
35+
} PUSBCallbacks;
36+
37+
typedef struct
38+
{
39+
u8 interface;
40+
u8 firstEndpoint;
41+
} PUSBReturn;
42+
43+
int PUSB_AddFunction(PUSBCallbacks *cb, u8 *interface);
44+
45+
int PUSB_GetInterface(u8* interfaceNum);
46+
47+
int PUSB_GetDescriptor(int t);
48+
49+
bool PUSB_Setup(Setup& setup, u8 i);
50+
51+
void PUSB_Begin();
52+
53+
#endif
54+
55+
#endif

hardware/arduino/avr/cores/arduino/USBCore.cpp

+19-10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818

1919
#include "USBAPI.h"
20+
#include "PluggableUSB.h"
2021

2122
#if defined(USBCON)
2223

@@ -323,8 +324,14 @@ u8 _initEndpoints[] =
323324
EP_TYPE_BULK_IN, // CDC_ENDPOINT_IN
324325
#endif
325326

326-
#ifdef HID_ENABLED
327-
EP_TYPE_INTERRUPT_IN // HID_ENDPOINT_INT
327+
#ifdef PLUGGABLE_USB_ENABLED
328+
//allocate 6 endpoints and remove const so they can be changed by the user
329+
0,
330+
0,
331+
0,
332+
0,
333+
0,
334+
0,
328335
#endif
329336
};
330337

@@ -365,9 +372,8 @@ bool ClassInterfaceRequest(Setup& setup)
365372
return CDC_Setup(setup);
366373
#endif
367374

368-
#ifdef HID_ENABLED
369-
if (HID_INTERFACE == i)
370-
return HID_Setup(setup);
375+
#ifdef PLUGGABLE_USB_ENABLED
376+
return PUSB_Setup(setup, i);
371377
#endif
372378
return false;
373379
}
@@ -447,8 +453,8 @@ int SendInterfaces()
447453
total = CDC_GetInterface(&interfaces);
448454
#endif
449455

450-
#ifdef HID_ENABLED
451-
total += HID_GetInterface(&interfaces);
456+
#ifdef PLUGGABLE_USB_ENABLED
457+
PUSB_GetInterface(&interfaces);
452458
#endif
453459

454460
return interfaces;
@@ -477,14 +483,17 @@ u8 _cdcComposite = 0;
477483
static
478484
bool SendDescriptor(Setup& setup)
479485
{
486+
int ret;
480487
u8 t = setup.wValueH;
481488
if (USB_CONFIGURATION_DESCRIPTOR_TYPE == t)
482489
return SendConfiguration(setup.wLength);
483490

484491
InitControl(setup.wLength);
485-
#ifdef HID_ENABLED
486-
if (HID_REPORT_DESCRIPTOR_TYPE == t)
487-
return HID_GetDescriptor(t);
492+
#ifdef PLUGGABLE_USB_ENABLED
493+
ret = PUSB_GetDescriptor(t);
494+
if (ret != 0) {
495+
return (ret > 0 ? true : false);
496+
}
488497
#endif
489498

490499
const u8* desc_addr = 0;

hardware/arduino/avr/cores/arduino/USBCore.h

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#ifndef __USBCORE_H__
1919
#define __USBCORE_H__
2020

21+
#include "USBAPI.h"
22+
2123
// Standard requests
2224
#define GET_STATUS 0
2325
#define CLEAR_FEATURE 1

hardware/arduino/avr/cores/arduino/USBDesc.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818

1919
#define CDC_ENABLED
20-
#define HID_ENABLED
20+
#define PLUGGABLE_USB_ENABLED
2121

2222

2323
#ifdef CDC_ENABLED

0 commit comments

Comments
 (0)