-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pkg/tinyusb: implement stdio via CDC ACM
- Loading branch information
Showing
5 changed files
with
97 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
MODULE = stdio_tinyusb_cdc_acm | ||
|
||
include $(RIOTBASE)/Makefile.base |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright (C) 2022 ML!PA Consulting GmbH | ||
* | ||
* This file is subject to the terms and conditions of the GNU Lesser | ||
* General Public License v2.1. See the file LICENSE in the top level | ||
* directory for more details. | ||
*/ | ||
|
||
/** | ||
* @ingroup pkg_tinyusb | ||
* @{ | ||
* | ||
* @file | ||
* @brief CDC ACM stdio implementation for tinyUSB CDC ACM | ||
* | ||
* This file implements a USB CDC ACM callback and read/write functions. | ||
* | ||
* @author Benjamin Valentin <[email protected]> | ||
* | ||
* @} | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <sys/types.h> | ||
|
||
#include "tusb.h" | ||
#include "tinyusb.h" | ||
|
||
#if MODULE_VFS | ||
#include "vfs.h" | ||
#endif | ||
|
||
#ifdef MODULE_USB_BOARD_RESET | ||
#include "usb_board_reset_internal.h" | ||
#endif | ||
|
||
static mutex_t data_lock = MUTEX_INIT_LOCKED; | ||
|
||
void stdio_init(void) | ||
{ | ||
/* Initialize this side of the CDC ACM pipe */ | ||
#if MODULE_VFS | ||
vfs_bind_stdio(); | ||
#endif | ||
} | ||
|
||
#if IS_USED(MODULE_STDIO_AVAILABLE) | ||
int stdio_available(void) | ||
{ | ||
return tud_cdc_available(); | ||
} | ||
#endif | ||
|
||
ssize_t stdio_read(void* buffer, size_t len) | ||
{ | ||
mutex_lock(&data_lock); | ||
return tud_cdc_read(buffer, len); | ||
} | ||
|
||
ssize_t stdio_write(const void* buffer, size_t len) | ||
{ | ||
const char *start = buffer; | ||
|
||
while (tud_cdc_connected() && len) { | ||
size_t n = tud_cdc_write(buffer, len); | ||
buffer = (char *)buffer + n; | ||
len -= n; | ||
}; | ||
|
||
tud_cdc_write_flush(); | ||
|
||
return (char *)buffer - start; | ||
} | ||
|
||
void tud_cdc_rx_cb(uint8_t itf) | ||
{ | ||
(void)itf; | ||
|
||
mutex_unlock(&data_lock); | ||
} |