Skip to content

Commit d7540a6

Browse files
committed
USBHost: start porting to C++
1 parent 5654035 commit d7540a6

File tree

5 files changed

+243
-0
lines changed

5 files changed

+243
-0
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/*
2+
_______ _ _ _____ ____
3+
|__ __| | | | |/ ____| _ \
4+
| | ___ ___ _ __ _ _| | | | (___ | |_) |
5+
| |/ _ \/ _ \ '_ \| | | | | | |\___ \| _ <
6+
| | __/ __/ | | | |_| | |__| |____) | |_) |
7+
|_|\___|\___|_| |_|\__, |\____/|_____/|____/
8+
__/ |
9+
|___/
10+
11+
TeenyUSB - light weight usb stack for STM32 micro controllers
12+
13+
Copyright (c) 2019 XToolBox - admin@xtoolbox.org
14+
www.tusb.org
15+
16+
Permission is hereby granted, free of charge, to any person obtaining a copy
17+
of this software and associated documentation files (the "Software"), to deal
18+
in the Software without restriction, including without limitation the rights
19+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20+
copies of the Software, and to permit persons to whom the Software is
21+
furnished to do so, subject to the following conditions:
22+
23+
The above copyright notice and this permission notice shall be included in all
24+
copies or substantial portions of the Software.
25+
26+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32+
SOFTWARE.
33+
*/
34+
35+
#include "USBHost.h"
36+
37+
USBHost usb;
38+
39+
static int process_key(tusbh_ep_info_t* ep, const uint8_t* key);
40+
41+
static const tusbh_boot_key_class_t cls_boot_key = {
42+
.backend = &tusbh_boot_keyboard_backend,
43+
//.on_key = process_key
44+
};
45+
46+
static const tusbh_boot_mouse_class_t cls_boot_mouse = {
47+
.backend = &tusbh_boot_mouse_backend,
48+
// .on_mouse = process_mouse
49+
};
50+
51+
static const tusbh_hid_class_t cls_hid = {
52+
.backend = &tusbh_hid_backend,
53+
//.on_recv_data = process_hid_recv,
54+
//.on_send_done = process_hid_sent,
55+
};
56+
57+
static const tusbh_hub_class_t cls_hub = {
58+
.backend = &tusbh_hub_backend,
59+
};
60+
61+
static const tusbh_vendor_class_t cls_vendor = {
62+
.backend = &tusbh_vendor_backend,
63+
//.transfer_done = process_vendor_xfer_done
64+
};
65+
66+
int msc_ff_mount(tusbh_interface_t* interface, int max_lun, const tusbh_block_info_t* blocks);
67+
int msc_ff_unmount(tusbh_interface_t* interface);
68+
69+
static const tusbh_msc_class_t cls_msc_bot = {
70+
.backend = &tusbh_msc_bot_backend,
71+
// .mount = msc_ff_mount,
72+
// .unmount = msc_ff_unmount,
73+
};
74+
75+
static const tusbh_cdc_acm_class_t cls_cdc_acm = {
76+
.backend = &tusbh_cdc_acm_backend,
77+
};
78+
79+
static const tusbh_cdc_rndis_class_t cls_cdc_rndis = {
80+
.backend = &tusbh_cdc_rndis_backend,
81+
};
82+
83+
static const tusbh_class_reg_t class_table[] = {
84+
(tusbh_class_reg_t)&cls_boot_key,
85+
(tusbh_class_reg_t)&cls_boot_mouse,
86+
(tusbh_class_reg_t)&cls_hub,
87+
(tusbh_class_reg_t)&cls_msc_bot,
88+
(tusbh_class_reg_t)&cls_cdc_acm,
89+
(tusbh_class_reg_t)&cls_cdc_rndis,
90+
(tusbh_class_reg_t)&cls_hid,
91+
(tusbh_class_reg_t)&cls_vendor,
92+
0,
93+
};
94+
95+
void setup()
96+
{
97+
Serial1.begin(115200);
98+
usb.Init(class_table);
99+
}
100+
101+
void loop() {
102+
usb.Task();
103+
}
104+
105+
#define MOD_CTRL (0x01 | 0x10)
106+
#define MOD_SHIFT (0x02 | 0x20)
107+
#define MOD_ALT (0x04 | 0x40)
108+
#define MOD_WIN (0x08 | 0x80)
109+
110+
#define LED_NUM_LOCK 1
111+
#define LED_CAPS_LOCK 2
112+
#define LED_SCROLL_LOCK 4
113+
114+
#define stdin_recvchar Serial1.write
115+
116+
static uint8_t key_leds;
117+
static const char knum[] = "1234567890";
118+
static const char ksign[] = "!@#$%^&*()";
119+
static const char tabA[] = "\t -=[]\\#;'`,./";
120+
static const char tabB[] = "\t _+{}|~:\"~<>?";
121+
// route the key event to stdin
122+
static int process_key(tusbh_ep_info_t* ep, const uint8_t* keys)
123+
{
124+
printf("\n");
125+
uint8_t modify = keys[0];
126+
uint8_t key = keys[2];
127+
uint8_t last_leds = key_leds;
128+
if (key >= KEY_A && key <= KEY_Z) {
129+
char ch = 'A' + key - KEY_A;
130+
if ( (!!(modify & MOD_SHIFT)) == (!!(key_leds & LED_CAPS_LOCK)) ) {
131+
ch += 'a' - 'A';
132+
}
133+
stdin_recvchar(ch);
134+
} else if (key >= KEY_1 && key <= KEY_0) {
135+
if (modify & MOD_SHIFT) {
136+
stdin_recvchar(ksign[key - KEY_1]);
137+
} else {
138+
stdin_recvchar(knum[key - KEY_1]);
139+
}
140+
} else if (key >= KEY_TAB && key <= KEY_SLASH) {
141+
if (modify & MOD_SHIFT) {
142+
stdin_recvchar(tabB[key - KEY_TAB]);
143+
} else {
144+
stdin_recvchar(tabA[key - KEY_TAB]);
145+
}
146+
} else if (key == KEY_ENTER) {
147+
stdin_recvchar('\r');
148+
} else if (key == KEY_CAPSLOCK) {
149+
key_leds ^= LED_CAPS_LOCK;
150+
} else if (key == KEY_NUMLOCK) {
151+
key_leds ^= LED_NUM_LOCK;
152+
} else if (key == KEY_SCROLLLOCK) {
153+
key_leds ^= LED_SCROLL_LOCK;
154+
}
155+
156+
if (key_leds != last_leds) {
157+
tusbh_set_keyboard_led(ep, key_leds);
158+
}
159+
return 0;
160+
}

libraries/USBHOST/examples/Shell/Shell.ino

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,8 @@ static void command_loop(void)
275275
}
276276
}
277277

278+
#include "usb_phy_api.h"
279+
278280
tusbh_msg_q_t* mq;
279281

280282
void setup()

libraries/USBHOST/src/USBHOST.h

Whitespace-only changes.

libraries/USBHOST/src/USBHost.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "USBHost.h"
2+
3+
uint32_t USBHost::Init(const tusbh_class_reg_t class_table[]) {
4+
get_usb_phy()->deinit();
5+
6+
mq = tusbh_mq_create();
7+
tusbh_mq_init(mq);
8+
9+
#if defined(USB_CORE_ID_FS)
10+
_fs = tusb_get_host(USB_CORE_ID_FS);
11+
HOST_PORT_POWER_ON_FS();
12+
root_fs.mq = mq;
13+
root_fs.id = "FS";
14+
root_fs.support_classes = class_table;
15+
tusb_host_init(_fs, &root_fs);
16+
tusb_open_host(_fs);
17+
#else
18+
(void)root_fs;
19+
_fs = 0;
20+
#endif
21+
22+
#if defined(USB_CORE_ID_HS)
23+
_hs = tusb_get_host(USB_CORE_ID_HS);
24+
HOST_PORT_POWER_ON_HS();
25+
root_hs.mq = mq;
26+
root_hs.id = "HS";
27+
root_hs.support_classes = class_table;
28+
tusb_host_init(_hs, &root_hs);
29+
tusb_open_host(_hs);
30+
#else
31+
(void)root_hs;
32+
hs = 0;
33+
#endif
34+
}
35+
36+
uint32_t USBHost::Task() {
37+
tusbh_msg_loop(mq);
38+
}
39+
40+
extern "C" {
41+
// host need accurate delay
42+
void tusb_delay_ms(uint32_t ms)
43+
{
44+
delayMicroseconds(ms*1000);
45+
}
46+
}

libraries/USBHOST/src/USBHost.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
extern "C" {
2+
#include "teeny_usb.h"
3+
#include "class/host/tusbh.h"
4+
#include "class/host/tusbh_vendor.h"
5+
#include "class/host/tusbh_hub.h"
6+
#include "class/host/tusbh_hid.h"
7+
#include "class/host/tusbh_msc.h"
8+
#include "class/host/tusbh_cdc_acm.h"
9+
#include "class/host/tusbh_cdc_rndis.h"
10+
#include "string.h"
11+
#include "class/host/usb_key_code.h"
12+
}
13+
14+
#include "mbed.h"
15+
#include "usb_phy_api.h"
16+
17+
class USBHost {
18+
public:
19+
uint32_t Init(const tusbh_class_reg_t class_table[]);
20+
uint32_t Task();
21+
22+
tusb_host_t* fs() {
23+
return _fs;
24+
};
25+
tusb_host_t* hs() {
26+
return _hs;
27+
};
28+
29+
private:
30+
tusbh_msg_q_t* mq;
31+
tusb_host_t* _fs;
32+
tusb_host_t* _hs;
33+
tusbh_root_hub_t root_fs;
34+
tusbh_root_hub_t root_hs;
35+
};

0 commit comments

Comments
 (0)