Skip to content

Commit e7cda3c

Browse files
committed
Initial move of USB APIs and make PluggableUSB generic
1 parent cf53821 commit e7cda3c

File tree

4 files changed

+90
-28
lines changed

4 files changed

+90
-28
lines changed

Diff for: api/ArduinoAPI.h

+11
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,21 @@
3232
#include "IPAddress.h"
3333
#include "Print.h"
3434
#include "Printable.h"
35+
#include "PluggableUSB.h"
3536
#include "Server.h"
3637
#include "String.h"
3738
#include "Stream.h"
3839
#include "Udp.h"
40+
#include "USBAPI.h"
3941
#endif
4042

43+
/* Standard C library includes */
44+
#include <stdlib.h>
45+
#include <stdbool.h>
46+
#include <string.h>
47+
#include <math.h>
48+
49+
// Misc Arduino core functions
50+
#include "Common.h"
51+
4152
#endif

Diff for: api/PluggableUSB.cpp

+3-19
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@
2020
#include "USBAPI.h"
2121
#include "PluggableUSB.h"
2222

23-
#if defined(USBCON)
24-
#ifdef PLUGGABLE_USB_ENABLED
25-
26-
extern _EP_BUFFER_TYPE _EP_BUFFER_NAME[];
27-
2823
int PluggableUSB_::getInterface(uint8_t* interfaceCount)
2924
{
3025
int sent = 0;
@@ -72,7 +67,7 @@ bool PluggableUSB_::setup(USBSetup& setup)
7267

7368
bool PluggableUSB_::plug(PluggableUSBModule *node)
7469
{
75-
if ((lastEp + node->numEndpoints) > USB_ENDPOINTS) {
70+
if ((lastEp + node->numEndpoints) > totalEP) {
7671
return false;
7772
}
7873

@@ -90,7 +85,7 @@ bool PluggableUSB_::plug(PluggableUSBModule *node)
9085
node->pluggedEndpoint = lastEp;
9186
lastIf += node->numInterfaces;
9287
for (uint8_t i = 0; i < node->numEndpoints; i++) {
93-
_EP_BUFFER_NAME[lastEp] = node->endpointType[i];
88+
*(unsigned int*)(epBuffer(lastEp)) = node->endpointType[i];
9489
lastEp++;
9590
}
9691
return true;
@@ -101,15 +96,4 @@ PluggableUSB_& PluggableUSB()
10196
{
10297
static PluggableUSB_ obj;
10398
return obj;
104-
}
105-
106-
PluggableUSB_::PluggableUSB_() : lastIf(CDC_ACM_INTERFACE + CDC_INTERFACE_COUNT),
107-
lastEp(CDC_FIRST_ENDPOINT + CDC_ENPOINT_COUNT),
108-
rootNode(NULL)
109-
{
110-
// Empty
111-
}
112-
113-
#endif
114-
115-
#endif /* if defined(USBCON) */
99+
}

Diff for: api/PluggableUSB.h

+6-9
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,14 @@
2222

2323
#include "USBAPI.h"
2424
#include <stdint.h>
25+
#include <stddef.h>
2526

2627
// core need to define
27-
// EP_BUFFER_TYPE
28-
// EP_BUFFER_NAME
29-
30-
#if defined(USBCON)
28+
void* epBuffer(unsigned int n); // -> returns a poointer to the Nth element of the EP buffer structure
3129

3230
class PluggableUSBModule {
3331
public:
34-
PluggableUSBModule(uint8_t numEps, uint8_t numIfs, EP_BUFFER_TYPE *epType) :
32+
PluggableUSBModule(uint8_t numEps, uint8_t numIfs, unsigned int *epType) :
3533
numEndpoints(numEps), numInterfaces(numIfs), endpointType(epType)
3634
{ }
3735

@@ -46,7 +44,7 @@ class PluggableUSBModule {
4644

4745
const uint8_t numEndpoints;
4846
const uint8_t numInterfaces;
49-
const EP_BUFFER_TYPE *endpointType;
47+
const unsigned int *endpointType;
5048

5149
PluggableUSBModule *next = NULL;
5250

@@ -66,13 +64,12 @@ class PluggableUSB_ {
6664
uint8_t lastIf;
6765
uint8_t lastEp;
6866
PluggableUSBModule* rootNode;
67+
uint8_t totalEP;
6968
};
7069

7170
// Replacement for global singleton.
7271
// This function prevents static-initialization-order-fiasco
7372
// https://isocpp.org/wiki/faq/ctors#static-init-order-on-first-use
7473
PluggableUSB_& PluggableUSB();
7574

76-
#endif
77-
78-
#endif
75+
#endif

Diff for: api/USBAPI.h

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
USBAPI.h
3+
Copyright (c) 2005-2014 Arduino. 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 __USBAPI__
21+
#define __USBAPI__
22+
23+
#include <stdint.h>
24+
25+
class USBDevice_
26+
{
27+
public:
28+
USBDevice_();
29+
bool configured();
30+
31+
void attach();
32+
void detach(); // Serial port goes down too...
33+
void poll();
34+
bool wakeupHost(); // returns false, when wakeup cannot be processed
35+
};
36+
extern USBDevice_ USBDevice;
37+
38+
//================================================================================
39+
//================================================================================
40+
// Low level API
41+
42+
typedef struct __attribute__((packed))
43+
{
44+
uint8_t bmRequestType;
45+
uint8_t bRequest;
46+
uint8_t wValueL;
47+
uint8_t wValueH;
48+
uint16_t wIndex;
49+
uint16_t wLength;
50+
} USBSetup;
51+
52+
//================================================================================
53+
//================================================================================
54+
55+
#define TRANSFER_PGM 0x80
56+
#define TRANSFER_RELEASE 0x40
57+
#define TRANSFER_ZERO 0x20
58+
59+
int USB_SendControl(uint8_t flags, const void* d, int len);
60+
int USB_RecvControl(void* d, int len);
61+
int USB_RecvControlLong(void* d, int len);
62+
63+
uint8_t USB_Available(uint8_t ep);
64+
uint8_t USB_SendSpace(uint8_t ep);
65+
int USB_Send(uint8_t ep, const void* data, int len); // blocking
66+
int USB_Recv(uint8_t ep, void* data, int len); // non-blocking
67+
int USB_Recv(uint8_t ep); // non-blocking
68+
void USB_Flush(uint8_t ep);
69+
70+
#endif

0 commit comments

Comments
 (0)