Skip to content

Commit 129d67e

Browse files
committed
tinyusb: Make automatic start of USB optional
So far TinyUSB stack was starting during system init stage. Now it is possible for the application to start it at later time by manually call tinyusb_start() function. Additionally shell commands are added to test functionality: /usb start /usb stop Signed-off-by: Jerzy Kasenberg <[email protected]>
1 parent 1eb3e09 commit 129d67e

File tree

5 files changed

+109
-2
lines changed

5 files changed

+109
-2
lines changed

hw/usb/tinyusb/msc_fat_view/pkg.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pkg.deps:
3333
- "@apache-mynewt-core/sys/coredump"
3434
- "@mcuboot/boot/bootutil"
3535

36-
pkg.init.!BOOT_LOADER:
36+
pkg.init.'(!BOOT_LOADER && TINYUSB_AUTO_START!=0)':
3737
msc_fat_view_pkg_init: $before:tinyusb_start
3838

3939
pkg.init.'!BOOT_LOADER && MSC_FAT_VIEW_COREDUMP_FILES':

hw/usb/tinyusb/pkg.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ pkg.deps:
3333
- "@apache-mynewt-core/kernel/os"
3434
- "@apache-mynewt-core/hw/usb/tinyusb/tinyusb_sdk"
3535

36-
pkg.init.OS_SCHEDULING:
36+
pkg.init.'(OS_SCHEDULING!=0 && TINYUSB_AUTO_START!=0)':
3737
tinyusb_start: 'MYNEWT_VAL(USBD_SYSINIT_STAGE)'
3838

39+
pkg.deps.TINYUSB_SHELL:
40+
- "@apache-mynewt-core/hw/usb/tinyusb/shell"
3941
pkg.deps.USBD_STD_DESCRIPTORS:
4042
- "@apache-mynewt-core/hw/usb/tinyusb/std_descriptors"
4143
pkg.deps.'MCU_TARGET == "nRF52840"':

hw/usb/tinyusb/shell/pkg.yml

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
20+
pkg.name: hw/usb/tinyusb/shell
21+
pkg.description: Shell for TinyUSB
22+
pkg.author: "Apache Mynewt <[email protected]>"
23+
pkg.homepage: "http://mynewt.apache.org/"
24+
pkg.keywords:
25+
- usb
26+
- tinyusb
27+
28+
pkg.deps:
29+
- "@apache-mynewt-core/kernel/os"
30+
- "@apache-mynewt-core/hw/usb/tinyusb"
31+
32+
pkg.init:
33+
tinyusb_cli_init: 'MYNEWT_VAL(SHELL_SYSINIT_STAGE) + 1'
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#include <os/mynewt.h>
21+
#include <shell/shell.h>
22+
#include <tinyusb/tinyusb.h>
23+
#include <device/usbd.h>
24+
#include <tusb.h>
25+
26+
static int
27+
usb_cli_start_cmd(const struct shell_cmd *cmd, int argc, char **argv,
28+
struct streamer *streamer)
29+
{
30+
if (!tusb_inited()) {
31+
tinyusb_start();
32+
} else if (!tud_connected()) {
33+
tud_connect();
34+
}
35+
36+
return 0;
37+
}
38+
39+
static int
40+
usb_cli_stop_cmd(const struct shell_cmd *cmd, int argc, char **argv,
41+
struct streamer *streamer)
42+
{
43+
if (tud_connected()) {
44+
tud_disconnect();
45+
}
46+
47+
return 0;
48+
}
49+
50+
static const struct shell_cmd usb_cli_commands[] = {
51+
SHELL_CMD_EXT("start", usb_cli_start_cmd, NULL),
52+
SHELL_CMD_EXT("stop", usb_cli_stop_cmd, NULL),
53+
{ },
54+
};
55+
56+
int
57+
tinyusb_cli_init(void)
58+
{
59+
shell_register("usb", usb_cli_commands);
60+
61+
return 0;
62+
}

hw/usb/tinyusb/syscfg.yml

+10
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ syscfg.defs:
2121
TINYUSB:
2222
description: Constant value
2323
value: 1
24+
TINYUSB_AUTO_START:
25+
description: >
26+
Start USB stack during sysinit stage USBD_SYSINIT_STAGE.
27+
If not set tinyusb_start() can be called manually by application code
28+
later.
29+
value: 1
30+
TINYUSB_SHELL:
31+
description: >
32+
Enable shell for TinyUSB
33+
value: 0
2434

2535
USBD_TASK_PRIORITY:
2636
description: >

0 commit comments

Comments
 (0)