Skip to content

Commit 94814be

Browse files
committed
port PluggableUSB to sam core
1 parent 1baf58a commit 94814be

File tree

7 files changed

+208
-186
lines changed

7 files changed

+208
-186
lines changed

cores/arduino/USB/CDC.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "Arduino.h"
1818
#include "USBAPI.h"
1919
#include "Reset.h"
20+
#include "Print.h"
2021

2122
#ifdef CDC_ENABLED
2223

@@ -103,7 +104,7 @@ int WEAK CDC_GetOtherInterface(uint8_t* interfaceNum)
103104
return USBD_SendControl(0,&_cdcOtherInterface,sizeof(_cdcOtherInterface));
104105
}
105106

106-
bool WEAK CDC_Setup(Setup& setup)
107+
bool WEAK CDC_Setup(USBSetup& setup)
107108
{
108109
uint8_t r = setup.bRequest;
109110
uint8_t requestType = setup.bmRequestType;

cores/arduino/USB/PluggableUSB.cpp

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
PluggableUSB.cpp
3+
Copyright (c) 2015 Arduino LLC
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+
#include "USBAPI.h"
21+
#include "USBDesc.h"
22+
#include "PluggableUSB.h"
23+
24+
#ifdef PLUGGABLE_USB_ENABLED
25+
26+
#define MAX_MODULES 6
27+
28+
static uint8_t lastIf = CDC_ACM_INTERFACE + CDC_INTERFACE_COUNT;
29+
static uint8_t lastEp = CDC_FIRST_ENDPOINT + CDC_ENPOINT_COUNT;
30+
31+
extern uint32_t EndPoints[];
32+
33+
//PUSBCallbacks cbs[MAX_MODULES];
34+
static uint8_t modules_count = 0;
35+
36+
static PUSBListNode* rootNode = NULL;
37+
38+
int PUSB_GetInterface(uint8_t* interfaceNum)
39+
{
40+
int ret = 0;
41+
PUSBListNode* node = rootNode;
42+
for (uint8_t i=0; i<modules_count; i++) {
43+
ret = node->cb->getInterface(interfaceNum);
44+
node = node->next;
45+
}
46+
return ret;
47+
}
48+
49+
int PUSB_GetDescriptor(int8_t t)
50+
{
51+
int ret = 0;
52+
PUSBListNode* node = rootNode;
53+
for (uint8_t i=0; i<modules_count && ret == 0; i++) {
54+
ret = node->cb->getDescriptor(t);
55+
node = node->next;
56+
}
57+
return ret;
58+
}
59+
60+
bool PUSB_Setup(USBSetup& setup, uint8_t j)
61+
{
62+
bool ret = false;
63+
PUSBListNode* node = rootNode;
64+
for (uint8_t i=0; i<modules_count && ret == false; i++) {
65+
ret = node->cb->setup(setup, j);
66+
node = node->next;
67+
}
68+
return ret;
69+
}
70+
71+
int8_t PUSB_AddFunction(PUSBListNode *node, uint8_t* interface)
72+
{
73+
if (modules_count >= MAX_MODULES) {
74+
return 0;
75+
}
76+
77+
if (modules_count == 0) {
78+
rootNode = node;
79+
} else {
80+
PUSBListNode *current = rootNode;
81+
while(current->next != NULL) {
82+
current = current->next;
83+
}
84+
current->next = node;
85+
}
86+
87+
*interface = lastIf;
88+
lastIf += node->cb->numInterfaces;
89+
for ( uint8_t i = 0; i< node->cb->numEndpoints; i++) {
90+
EndPoints[lastEp] = node->cb->endpointType[i];
91+
lastEp++;
92+
}
93+
modules_count++;
94+
return lastEp - node->cb->numEndpoints;
95+
// restart USB layer???
96+
}
97+
98+
#endif

cores/arduino/USB/PluggableUSB.h

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
PluggableUSB.h
3+
Copyright (c) 2015 Arduino LLC
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 <cstddef>
25+
26+
#if defined(USBCON)
27+
28+
typedef struct __attribute__((packed))
29+
{
30+
bool (*setup)(USBSetup& setup, uint8_t i);
31+
int (*getInterface)(uint8_t* interfaceNum);
32+
int (*getDescriptor)(int8_t t);
33+
int8_t numEndpoints;
34+
int8_t numInterfaces;
35+
uint8_t *endpointType;
36+
} PUSBCallbacks;
37+
38+
typedef struct
39+
{
40+
uint8_t interface;
41+
uint8_t firstEndpoint;
42+
} PUSBReturn;
43+
44+
class PUSBListNode {
45+
public:
46+
PUSBListNode *next = NULL;
47+
PUSBCallbacks *cb;
48+
PUSBListNode(PUSBCallbacks *ncb) {cb = ncb;}
49+
};
50+
51+
int8_t PUSB_AddFunction(PUSBListNode *node, uint8_t *interface);
52+
53+
int PUSB_GetInterface(uint8_t* interfaceNum);
54+
55+
int PUSB_GetDescriptor(int8_t t);
56+
57+
bool PUSB_Setup(USBSetup& setup, uint8_t i);
58+
59+
void PUSB_Begin();
60+
61+
#endif
62+
63+
#endif

cores/arduino/USB/USBAPI.h

+8-104
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
#if defined __cplusplus
2323

2424
#include "RingBuffer.h"
25+
#include "Stream.h"
26+
#include <cstddef>
27+
28+
#define min(a, b) Min(a, b)
2529

2630
//================================================================================
2731
//================================================================================
@@ -64,97 +68,6 @@ class Serial_ : public Stream
6468
};
6569
extern Serial_ SerialUSB;
6670

67-
//================================================================================
68-
//================================================================================
69-
// Mouse
70-
71-
#define MOUSE_LEFT 1
72-
#define MOUSE_RIGHT 2
73-
#define MOUSE_MIDDLE 4
74-
#define MOUSE_ALL (MOUSE_LEFT | MOUSE_RIGHT | MOUSE_MIDDLE)
75-
76-
class Mouse_
77-
{
78-
private:
79-
uint8_t _buttons;
80-
void buttons(uint8_t b);
81-
public:
82-
Mouse_(void);
83-
void begin(void);
84-
void end(void);
85-
void click(uint8_t b = MOUSE_LEFT);
86-
void move(signed char x, signed char y, signed char wheel = 0);
87-
void press(uint8_t b = MOUSE_LEFT); // press LEFT by default
88-
void release(uint8_t b = MOUSE_LEFT); // release LEFT by default
89-
bool isPressed(uint8_t b = MOUSE_ALL); // check all buttons by default
90-
};
91-
extern Mouse_ Mouse;
92-
93-
//================================================================================
94-
//================================================================================
95-
// Keyboard
96-
97-
#define KEY_LEFT_CTRL 0x80
98-
#define KEY_LEFT_SHIFT 0x81
99-
#define KEY_LEFT_ALT 0x82
100-
#define KEY_LEFT_GUI 0x83
101-
#define KEY_RIGHT_CTRL 0x84
102-
#define KEY_RIGHT_SHIFT 0x85
103-
#define KEY_RIGHT_ALT 0x86
104-
#define KEY_RIGHT_GUI 0x87
105-
106-
#define KEY_UP_ARROW 0xDA
107-
#define KEY_DOWN_ARROW 0xD9
108-
#define KEY_LEFT_ARROW 0xD8
109-
#define KEY_RIGHT_ARROW 0xD7
110-
#define KEY_BACKSPACE 0xB2
111-
#define KEY_TAB 0xB3
112-
#define KEY_RETURN 0xB0
113-
#define KEY_ESC 0xB1
114-
#define KEY_INSERT 0xD1
115-
#define KEY_DELETE 0xD4
116-
#define KEY_PAGE_UP 0xD3
117-
#define KEY_PAGE_DOWN 0xD6
118-
#define KEY_HOME 0xD2
119-
#define KEY_END 0xD5
120-
#define KEY_CAPS_LOCK 0xC1
121-
#define KEY_F1 0xC2
122-
#define KEY_F2 0xC3
123-
#define KEY_F3 0xC4
124-
#define KEY_F4 0xC5
125-
#define KEY_F5 0xC6
126-
#define KEY_F6 0xC7
127-
#define KEY_F7 0xC8
128-
#define KEY_F8 0xC9
129-
#define KEY_F9 0xCA
130-
#define KEY_F10 0xCB
131-
#define KEY_F11 0xCC
132-
#define KEY_F12 0xCD
133-
134-
// Low level key report: up to 6 keys and shift, ctrl etc at once
135-
typedef struct
136-
{
137-
uint8_t modifiers;
138-
uint8_t reserved;
139-
uint8_t keys[6];
140-
} KeyReport;
141-
142-
class Keyboard_ : public Print
143-
{
144-
private:
145-
KeyReport _keyReport;
146-
void sendReport(KeyReport* keys);
147-
public:
148-
Keyboard_(void);
149-
void begin(void);
150-
void end(void);
151-
virtual size_t write(uint8_t k);
152-
virtual size_t press(uint8_t k);
153-
virtual size_t release(uint8_t k);
154-
virtual void releaseAll(void);
155-
};
156-
extern Keyboard_ Keyboard;
157-
15871
//================================================================================
15972
//================================================================================
16073
// Low level API
@@ -167,24 +80,15 @@ typedef struct
16780
uint8_t wValueH;
16881
uint16_t wIndex;
16982
uint16_t wLength;
170-
} Setup;
171-
172-
//================================================================================
173-
//================================================================================
174-
// HID 'Driver'
175-
176-
int HID_GetInterface(uint8_t* interfaceNum);
177-
int HID_GetDescriptor(int i);
178-
bool HID_Setup(Setup& setup);
179-
void HID_SendReport(uint8_t id, const void* data, uint32_t len);
83+
} USBSetup;
18084

18185
//================================================================================
18286
//================================================================================
18387
// MSC 'Driver'
18488

18589
int MSC_GetInterface(uint8_t* interfaceNum);
18690
int MSC_GetDescriptor(int i);
187-
bool MSC_Setup(Setup& setup);
91+
bool MSC_Setup(USBSetup& setup);
18892
bool MSC_Data(uint8_t rx,uint8_t tx);
18993

19094
//================================================================================
@@ -194,7 +98,7 @@ bool MSC_Data(uint8_t rx,uint8_t tx);
19498
int CDC_GetInterface(uint8_t* interfaceNum);
19599
int CDC_GetOtherInterface(uint8_t* interfaceNum);
196100
int CDC_GetDescriptor(int i);
197-
bool CDC_Setup(Setup& setup);
101+
bool CDC_Setup(USBSetup& setup);
198102

199103
//================================================================================
200104
//================================================================================
@@ -206,7 +110,7 @@ void USBD_InitControl(int end);
206110
int USBD_SendControl(uint8_t flags, const void* d, uint32_t len);
207111
int USBD_RecvControl(void* d, uint32_t len);
208112
int USBD_SendInterfaces(void);
209-
bool USBD_ClassInterfaceRequest(Setup& setup);
113+
bool USBD_ClassInterfaceRequest(USBSetup& setup);
210114

211115

212116
uint32_t USBD_Available(uint32_t ep);

0 commit comments

Comments
 (0)