Skip to content

Commit

Permalink
tinyusb: Make automatic start of USB optional
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
kasjer committed Feb 5, 2024
1 parent 1eb3e09 commit 129d67e
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 2 deletions.
2 changes: 1 addition & 1 deletion hw/usb/tinyusb/msc_fat_view/pkg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pkg.deps:
- "@apache-mynewt-core/sys/coredump"
- "@mcuboot/boot/bootutil"

pkg.init.!BOOT_LOADER:
pkg.init.'(!BOOT_LOADER && TINYUSB_AUTO_START!=0)':
msc_fat_view_pkg_init: $before:tinyusb_start

pkg.init.'!BOOT_LOADER && MSC_FAT_VIEW_COREDUMP_FILES':
Expand Down
4 changes: 3 additions & 1 deletion hw/usb/tinyusb/pkg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ pkg.deps:
- "@apache-mynewt-core/kernel/os"
- "@apache-mynewt-core/hw/usb/tinyusb/tinyusb_sdk"

pkg.init.OS_SCHEDULING:
pkg.init.'(OS_SCHEDULING!=0 && TINYUSB_AUTO_START!=0)':
tinyusb_start: 'MYNEWT_VAL(USBD_SYSINIT_STAGE)'

pkg.deps.TINYUSB_SHELL:
- "@apache-mynewt-core/hw/usb/tinyusb/shell"
pkg.deps.USBD_STD_DESCRIPTORS:
- "@apache-mynewt-core/hw/usb/tinyusb/std_descriptors"
pkg.deps.'MCU_TARGET == "nRF52840"':
Expand Down
33 changes: 33 additions & 0 deletions hw/usb/tinyusb/shell/pkg.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

pkg.name: hw/usb/tinyusb/shell
pkg.description: Shell for TinyUSB
pkg.author: "Apache Mynewt <[email protected]>"
pkg.homepage: "http://mynewt.apache.org/"
pkg.keywords:
- usb
- tinyusb

pkg.deps:
- "@apache-mynewt-core/kernel/os"
- "@apache-mynewt-core/hw/usb/tinyusb"

pkg.init:
tinyusb_cli_init: 'MYNEWT_VAL(SHELL_SYSINIT_STAGE) + 1'
62 changes: 62 additions & 0 deletions hw/usb/tinyusb/shell/src/tinyusb_cli.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#include <os/mynewt.h>
#include <shell/shell.h>
#include <tinyusb/tinyusb.h>
#include <device/usbd.h>
#include <tusb.h>

static int
usb_cli_start_cmd(const struct shell_cmd *cmd, int argc, char **argv,
struct streamer *streamer)
{
if (!tusb_inited()) {
tinyusb_start();
} else if (!tud_connected()) {
tud_connect();
}

return 0;
}

static int
usb_cli_stop_cmd(const struct shell_cmd *cmd, int argc, char **argv,
struct streamer *streamer)
{
if (tud_connected()) {
tud_disconnect();
}

return 0;
}

static const struct shell_cmd usb_cli_commands[] = {
SHELL_CMD_EXT("start", usb_cli_start_cmd, NULL),
SHELL_CMD_EXT("stop", usb_cli_stop_cmd, NULL),
{ },
};

int
tinyusb_cli_init(void)
{
shell_register("usb", usb_cli_commands);

return 0;
}
10 changes: 10 additions & 0 deletions hw/usb/tinyusb/syscfg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ syscfg.defs:
TINYUSB:
description: Constant value
value: 1
TINYUSB_AUTO_START:
description: >
Start USB stack during sysinit stage USBD_SYSINIT_STAGE.
If not set tinyusb_start() can be called manually by application code
later.
value: 1
TINYUSB_SHELL:
description: >
Enable shell for TinyUSB
value: 0

USBD_TASK_PRIORITY:
description: >
Expand Down

0 comments on commit 129d67e

Please sign in to comment.