Skip to content

Commit 8cf65d4

Browse files
committed
Initial commit
0 parents  commit 8cf65d4

36 files changed

+7847
-0
lines changed

library.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=USBHostMbed5
2+
version=0.0.1
3+
author=Arduino
4+
maintainer=Arduino <[email protected]>
5+
sentence=Porting mbed5 usbhost library
6+
paragraph=Porting mbed5 usbhost library
7+
category=Communication
8+
url=
9+
architectures=mbed,mbed_portenta

src/USBHost/IUSBEnumerator.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* mbed USBHost Library
2+
* Copyright (c) 2006-2013 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef IUSBENUMERATOR_H_
18+
#define IUSBENUMERATOR_H_
19+
20+
#include "stdint.h"
21+
#include "USBEndpoint.h"
22+
23+
/*
24+
Generic interface to implement for "smart" USB enumeration
25+
*/
26+
27+
class IUSBEnumerator
28+
{
29+
public:
30+
virtual void setVidPid(uint16_t vid, uint16_t pid) = 0;
31+
virtual bool parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) = 0; //Must return true if the interface should be parsed
32+
virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) = 0; //Must return true if the endpoint will be used
33+
};
34+
35+
#endif /*IUSBENUMERATOR_H_*/
36+

src/USBHost/USBDeviceConnected.cpp

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/* mbed USBHost Library
2+
* Copyright (c) 2006-2013 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "USBDeviceConnected.h"
18+
#include "dbg.h"
19+
20+
USBDeviceConnected::USBDeviceConnected() {
21+
init();
22+
}
23+
24+
void USBDeviceConnected::init() {
25+
hub_nb = 0;
26+
port = 0;
27+
vid = 0;
28+
pid = 0;
29+
nb_interf = 0;
30+
enumerated = false;
31+
activeAddr = false;
32+
sizeControlEndpoint = 8;
33+
device_class = 0;
34+
device_subclass = 0;
35+
proto = 0;
36+
speed = false;
37+
for (int i = 0; i < MAX_INTF; i++) {
38+
memset((void *)&intf[i], 0, sizeof(INTERFACE));
39+
intf[i].in_use = false;
40+
for (int j = 0; j < MAX_ENDPOINT_PER_INTERFACE; j++) {
41+
intf[i].ep[j] = NULL;
42+
strcpy(intf[i].name, "Unknown");
43+
}
44+
}
45+
hub_parent = NULL;
46+
hub = NULL;
47+
nb_interf = 0;
48+
}
49+
50+
INTERFACE * USBDeviceConnected::getInterface(uint8_t index) {
51+
if (index >= MAX_INTF)
52+
return NULL;
53+
54+
if (intf[index].in_use)
55+
return &intf[index];
56+
57+
return NULL;
58+
}
59+
60+
bool USBDeviceConnected::addInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) {
61+
if ((intf_nb >= MAX_INTF) || (intf[intf_nb].in_use)) {
62+
return false;
63+
}
64+
intf[intf_nb].in_use = true;
65+
intf[intf_nb].intf_class = intf_class;
66+
intf[intf_nb].intf_subclass = intf_subclass;
67+
intf[intf_nb].intf_protocol = intf_protocol;
68+
intf[intf_nb].nb_endpoint = 0;
69+
return true;
70+
}
71+
72+
bool USBDeviceConnected::addEndpoint(uint8_t intf_nb, USBEndpoint * ept) {
73+
if ((intf_nb >= MAX_INTF) || (intf[intf_nb].in_use == false) || (intf[intf_nb].nb_endpoint >= MAX_ENDPOINT_PER_INTERFACE)) {
74+
return false;
75+
}
76+
intf[intf_nb].nb_endpoint++;
77+
78+
for (int i = 0; i < MAX_ENDPOINT_PER_INTERFACE; i++) {
79+
if (intf[intf_nb].ep[i] == NULL) {
80+
intf[intf_nb].ep[i] = ept;
81+
return true;
82+
}
83+
}
84+
return false;
85+
}
86+
87+
void USBDeviceConnected::init(uint8_t hub_, uint8_t port_, bool lowSpeed_) {
88+
USB_DBG("init dev: %p", this);
89+
init();
90+
hub_nb = hub_;
91+
port = port_;
92+
speed = lowSpeed_;
93+
}
94+
95+
void USBDeviceConnected::disconnect() {
96+
for(int i = 0; i < MAX_INTF; i++) {
97+
if (intf[i].detach) intf[i].detach.call();
98+
}
99+
init();
100+
}
101+
102+
103+
USBEndpoint * USBDeviceConnected::getEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir, uint8_t index) {
104+
if (intf_nb >= MAX_INTF) {
105+
return NULL;
106+
}
107+
for (int i = 0; i < MAX_ENDPOINT_PER_INTERFACE; i++) {
108+
if ((intf[intf_nb].ep[i]->getType() == type) && (intf[intf_nb].ep[i]->getDir() == dir)) {
109+
if(index) {
110+
index--;
111+
} else {
112+
return intf[intf_nb].ep[i];
113+
}
114+
}
115+
}
116+
return NULL;
117+
}
118+
119+
USBEndpoint * USBDeviceConnected::getEndpoint(uint8_t intf_nb, uint8_t index) {
120+
if ((intf_nb >= MAX_INTF) || (index >= MAX_ENDPOINT_PER_INTERFACE)) {
121+
return NULL;
122+
}
123+
return intf[intf_nb].ep[index];
124+
}

src/USBHost/USBDeviceConnected.h

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
/* mbed USBHost Library
2+
* Copyright (c) 2006-2013 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef USBDEVICECONNECTED_H
18+
#define USBDEVICECONNECTED_H
19+
20+
#include "stdint.h"
21+
#include "USBHost/USBEndpoint.h"
22+
#include "USBHost/USBHostConf.h"
23+
#include "rtos.h"
24+
#include "Callback.h"
25+
26+
class USBHostHub;
27+
28+
typedef struct {
29+
bool in_use;
30+
uint8_t nb_endpoint;
31+
uint8_t intf_class;
32+
uint8_t intf_subclass;
33+
uint8_t intf_protocol;
34+
USBEndpoint * ep[MAX_ENDPOINT_PER_INTERFACE];
35+
mbed::Callback<void()> detach;
36+
char name[10];
37+
} INTERFACE;
38+
39+
/**
40+
* USBDeviceConnected class
41+
*/
42+
class USBDeviceConnected
43+
{
44+
public:
45+
46+
/**
47+
* Constructor
48+
*/
49+
USBDeviceConnected();
50+
51+
/**
52+
* Attach an USBEndpoint to this device
53+
*
54+
* @param intf_nb interface number
55+
* @param ep pointeur on the USBEndpoint which will be attached
56+
* @returns true if successful, false otherwise
57+
*/
58+
bool addEndpoint(uint8_t intf_nb, USBEndpoint * ep);
59+
60+
/**
61+
* Retrieve an USBEndpoint by its TYPE and DIRECTION
62+
*
63+
* @param intf_nb the interface on which to lookup the USBEndpoint
64+
* @param type type of the USBEndpoint looked for
65+
* @param dir direction of the USBEndpoint looked for
66+
* @param index the index of the USBEndpoint whitin the interface
67+
* @returns pointer on the USBEndpoint if found, NULL otherwise
68+
*/
69+
USBEndpoint * getEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir, uint8_t index = 0);
70+
71+
/**
72+
* Retrieve an USBEndpoint by its index
73+
*
74+
* @param intf_nb interface number
75+
* @param index index of the USBEndpoint
76+
* @returns pointer on the USBEndpoint if found, NULL otherwise
77+
*/
78+
USBEndpoint * getEndpoint(uint8_t intf_nb, uint8_t index);
79+
80+
/**
81+
* Add a new interface to this device
82+
*
83+
* @param intf_nb interface number
84+
* @param intf_class interface class
85+
* @param intf_subclass interface subclass
86+
* @param intf_protocol interface protocol
87+
* @returns true if successful, false otherwise
88+
*/
89+
bool addInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol);
90+
91+
/**
92+
* Get a specific interface
93+
*
94+
* @param index index of the interface to be fetched
95+
* @returns interface
96+
*/
97+
INTERFACE * getInterface(uint8_t index);
98+
99+
/**
100+
* Attach a member function to call when a the device has been disconnected
101+
*
102+
* @param intf_nb interface number
103+
* @param tptr pointer to the object to call the member function on
104+
* @param mptr pointer to the member function to be called
105+
*/
106+
template<typename T>
107+
inline void onDisconnect(uint8_t intf_nb, T* tptr, void (T::*mptr)(void)) {
108+
if ((mptr != NULL) && (tptr != NULL)) {
109+
intf[intf_nb].detach = mbed::callback(tptr, mptr);
110+
}
111+
}
112+
113+
/**
114+
* Attach a callback called when the device has been disconnected
115+
*
116+
* @param intf_nb interface number
117+
* @param fn function pointer
118+
*/
119+
inline void onDisconnect(uint8_t intf_nb, void (*fn)(void)) {
120+
if (fn != NULL) {
121+
intf[intf_nb].detach = fn;
122+
}
123+
}
124+
125+
/**
126+
* Disconnect the device by calling a callback function registered by a driver
127+
*/
128+
void disconnect();
129+
130+
// setters
131+
void init(uint8_t hub, uint8_t port, bool lowSpeed);
132+
inline void setAddress(uint8_t addr_) { addr = addr_; };
133+
inline void setVid(uint16_t vid_) { vid = vid_; };
134+
inline void setPid(uint16_t pid_) { pid = pid_; };
135+
inline void setClass(uint8_t device_class_) { device_class = device_class_; };
136+
inline void setSubClass(uint8_t device_subclass_) { device_subclass = device_subclass_; };
137+
inline void setProtocol(uint8_t pr) { proto = pr; };
138+
inline void setSizeControlEndpoint(uint32_t size) { sizeControlEndpoint = size; };
139+
inline void activeAddress(bool active) { activeAddr = active; };
140+
inline void setEnumerated() { enumerated = true; };
141+
inline void setNbIntf(uint8_t nb_intf) {nb_interf = nb_intf; };
142+
inline void setHubParent(USBHostHub * hub) { hub_parent = hub; };
143+
inline void setName(const char * name_, uint8_t intf_nb) { strcpy(intf[intf_nb].name, name_); };
144+
145+
//getters
146+
inline uint8_t getPort() { return port; };
147+
inline uint8_t getHub() { return hub_nb; };
148+
inline uint8_t getAddress() { return addr; };
149+
inline uint16_t getVid() { return vid; };
150+
inline uint16_t getPid() { return pid; };
151+
inline uint8_t getClass() { return device_class; };
152+
inline uint8_t getSubClass() { return device_subclass; };
153+
inline uint8_t getProtocol() { return proto; };
154+
inline bool getSpeed() { return speed; };
155+
inline uint32_t getSizeControlEndpoint() { return sizeControlEndpoint; };
156+
inline bool isActiveAddress() { return activeAddr; };
157+
inline bool isEnumerated() { return enumerated; };
158+
inline USBHostHub * getHubParent() { return hub_parent; };
159+
inline uint8_t getNbIntf() { return nb_interf; };
160+
inline const char * getName(uint8_t intf_nb) { return intf[intf_nb].name; };
161+
162+
// in case this device is a hub
163+
USBHostHub * hub;
164+
165+
private:
166+
USBHostHub * hub_parent;
167+
168+
INTERFACE intf[MAX_INTF];
169+
uint32_t sizeControlEndpoint;
170+
uint8_t hub_nb;
171+
uint8_t port;
172+
uint16_t vid;
173+
uint16_t pid;
174+
uint8_t addr;
175+
uint8_t device_class;
176+
uint8_t device_subclass;
177+
uint8_t proto;
178+
bool speed;
179+
volatile bool activeAddr;
180+
volatile bool enumerated;
181+
uint8_t nb_interf;
182+
183+
void init();
184+
};
185+
186+
#endif

0 commit comments

Comments
 (0)