Skip to content

Commit dfd4119

Browse files
facchinmsandeepmistry
authored andcommitted
port PluggableUSB to sam core
1 parent 77a7103 commit dfd4119

File tree

7 files changed

+208
-186
lines changed

7 files changed

+208
-186
lines changed

hardware/arduino/sam/cores/arduino/USB/CDC.cpp

Lines changed: 2 additions & 1 deletion
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;
Lines changed: 98 additions & 0 deletions
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
Lines changed: 63 additions & 0 deletions
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

hardware/arduino/sam/cores/arduino/USB/USBAPI.h

Lines changed: 8 additions & 104 deletions
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
//================================================================================
@@ -86,97 +90,6 @@ class Serial_ : public Stream
8690
};
8791
extern Serial_ SerialUSB;
8892

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

203107
//================================================================================
204108
//================================================================================
205109
// MSC 'Driver'
206110

207111
int MSC_GetInterface(uint8_t* interfaceNum);
208112
int MSC_GetDescriptor(int i);
209-
bool MSC_Setup(Setup& setup);
113+
bool MSC_Setup(USBSetup& setup);
210114
bool MSC_Data(uint8_t rx,uint8_t tx);
211115

212116
//================================================================================
@@ -216,7 +120,7 @@ bool MSC_Data(uint8_t rx,uint8_t tx);
216120
int CDC_GetInterface(uint8_t* interfaceNum);
217121
int CDC_GetOtherInterface(uint8_t* interfaceNum);
218122
int CDC_GetDescriptor(int i);
219-
bool CDC_Setup(Setup& setup);
123+
bool CDC_Setup(USBSetup& setup);
220124

221125
//================================================================================
222126
//================================================================================
@@ -228,7 +132,7 @@ void USBD_InitControl(int end);
228132
int USBD_SendControl(uint8_t flags, const void* d, uint32_t len);
229133
int USBD_RecvControl(void* d, uint32_t len);
230134
int USBD_SendInterfaces(void);
231-
bool USBD_ClassInterfaceRequest(Setup& setup);
135+
bool USBD_ClassInterfaceRequest(USBSetup& setup);
232136

233137

234138
uint32_t USBD_Available(uint32_t ep);

0 commit comments

Comments
 (0)