Skip to content

Commit

Permalink
Implement USB serial support
Browse files Browse the repository at this point in the history
  • Loading branch information
sashacmc committed Dec 5, 2024
1 parent 006ae4d commit 55cbfc8
Show file tree
Hide file tree
Showing 8 changed files with 412 additions and 6 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ set(Z_FEATURE_LINK_TCP 1 CACHE STRING "Toggle TCP links")
set(Z_FEATURE_LINK_BLUETOOTH 0 CACHE STRING "Toggle Bluetooth links")
set(Z_FEATURE_LINK_WS 0 CACHE STRING "Toggle WebSocket links")
set(Z_FEATURE_LINK_SERIAL 0 CACHE STRING "Toggle Serial links")
set(Z_FEATURE_LINK_SERIAL_USB 0 CACHE STRING "Toggle Serial USB links")
set(Z_FEATURE_SCOUTING_UDP 1 CACHE STRING "Toggle UDP scouting")
set(Z_FEATURE_LINK_UDP_MULTICAST 1 CACHE STRING "Toggle UDP multicast links")
set(Z_FEATURE_LINK_UDP_UNICAST 1 CACHE STRING "Toggle UDP unicast links")
Expand Down
26 changes: 25 additions & 1 deletion examples/rpi_pico/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ set(WIFI_PASSWORD "" CACHE STRING "WiFi Password")
set(ZENOH_CONFIG_MODE "client" CACHE STRING "ZENOH_CONFIG_MODE")
set(ZENOH_CONFIG_CONNECT CACHE STRING "ZENOH_CONFIG_CONNECT")
set(ZENOH_CONFIG_LISTEN CACHE STRING "ZENOH_CONFIG_LISTEN")
option(ZENOH_USB_UART "Enable USB UART" OFF)

message(STATUS "PICO_BOARD: ${PICO_BOARD}")
message(STATUS "WIFI_SSID: ${WIFI_SSID}")
Expand All @@ -26,6 +27,7 @@ endif()
message(STATUS "ZENOH_CONFIG_MODE: ${ZENOH_CONFIG_MODE}")
message(STATUS "ZENOH_CONFIG_CONNECT: ${ZENOH_CONFIG_CONNECT}")
message(STATUS "ZENOH_CONFIG_LISTEN: ${ZENOH_CONFIG_LISTEN}")
message(STATUS "ZENOH_USB_UART: ${ZENOH_USB_UART}")

configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/include/config.h.in"
Expand All @@ -52,6 +54,16 @@ else()
set(WIFI_LIB "")
endif()

if(ZENOH_USB_UART)
set(USB_LIBS
tinyusb_host
tinyusb_device
tinyusb_board
)
else()
set(USB_LIBS "")
endif()

add_compile_definitions(LWIP_TIMEVAL_PRIVATE=0)
pico_sdk_init()

Expand All @@ -67,6 +79,11 @@ if (NOT WIFI_SUPPORT_ENABLED)
endif()

declare_cache_var(Z_FEATURE_LINK_SERIAL 1 STRING "Serial support")
if(ZENOH_USB_UART)
declare_cache_var(Z_FEATURE_LINK_SERIAL_USB 1 STRING "Serial USB support")
else()
declare_cache_var(Z_FEATURE_LINK_SERIAL_USB 0 STRING "Serial USB support")
endif()

set(BUILD_SHARED_LIBS OFF)
set(WITH_RPI_PICO ON)
Expand All @@ -79,6 +96,7 @@ target_link_libraries(zenohpico_static
pico_rand
FreeRTOS-Kernel-Heap4
${WIFI_LIB}
${USB_LIBS}
)

# Configure build
Expand All @@ -98,6 +116,7 @@ target_link_libraries(main
pico_rand
FreeRTOS-Kernel-Heap4
${WIFI_LIB}
${USB_LIBS}
)

function(add_example name)
Expand All @@ -110,12 +129,17 @@ function(add_example name)
FreeRTOS-Kernel-Heap4
zenohpico::lib
${WIFI_LIB}
${USB_LIBS}
)
target_include_directories(${name} PRIVATE
${CMAKE_CURRENT_LIST_DIR}
${CMAKE_CURRENT_LIST_DIR}/include
)
pico_enable_stdio_usb(${name} 1)
if(ZENOH_USB_UART)
pico_enable_stdio_uart(${name} 1)
else()
pico_enable_stdio_usb(${name} 1)
endif()
pico_add_extra_outputs(${name})
endfunction()

Expand Down
111 changes: 111 additions & 0 deletions examples/rpi_pico/include/tusb_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 Ha Thach (tinyusb.org)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

#ifndef _TUSB_CONFIG_H_
#define _TUSB_CONFIG_H_

#ifdef __cplusplus
extern "C" {
#endif

//--------------------------------------------------------------------+
// Board Specific Configuration
//--------------------------------------------------------------------+

// RHPort number used for device can be defined by board.mk, default to port 0
#ifndef BOARD_TUD_RHPORT
#define BOARD_TUD_RHPORT 0
#endif

// RHPort max operational speed can defined by board.mk
#ifndef BOARD_TUD_MAX_SPEED
#define BOARD_TUD_MAX_SPEED OPT_MODE_DEFAULT_SPEED
#endif

//--------------------------------------------------------------------
// COMMON CONFIGURATION
//--------------------------------------------------------------------

// defined by board.mk
#ifndef CFG_TUSB_MCU
#error CFG_TUSB_MCU must be defined
#endif

#ifndef CFG_TUSB_OS
#define CFG_TUSB_OS OPT_OS_NONE
#endif

#ifndef CFG_TUSB_DEBUG
#define CFG_TUSB_DEBUG 0
#endif

// Enable Device stack
#define CFG_TUD_ENABLED 1

// Default is max speed that hardware controller could support with on-chip PHY
#define CFG_TUD_MAX_SPEED BOARD_TUD_MAX_SPEED

/* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment.
* Tinyusb use follows macros to declare transferring memory so that they can be put
* into those specific section.
* e.g
* - CFG_TUSB_MEM SECTION : __attribute__ (( section(".usb_ram") ))
* - CFG_TUSB_MEM_ALIGN : __attribute__ ((aligned(4)))
*/
#ifndef CFG_TUSB_MEM_SECTION
#define CFG_TUSB_MEM_SECTION
#endif

#ifndef CFG_TUSB_MEM_ALIGN
#define CFG_TUSB_MEM_ALIGN __attribute__((aligned(4)))
#endif

//--------------------------------------------------------------------
// DEVICE CONFIGURATION
//--------------------------------------------------------------------

#ifndef CFG_TUD_ENDPOINT0_SIZE
#define CFG_TUD_ENDPOINT0_SIZE 64
#endif

//------------- CLASS -------------//
#define CFG_TUD_CDC 2
#define CFG_TUD_MSC 0
#define CFG_TUD_HID 0
#define CFG_TUD_MIDI 0
#define CFG_TUD_VENDOR 0

// CDC FIFO size of TX and RX
#define CFG_TUD_CDC_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
#define CFG_TUD_CDC_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)

// CDC Endpoint transfer buffer size, more is faster
#define CFG_TUD_CDC_EP_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)

#ifdef __cplusplus
}
#endif

#endif /* _TUSB_CONFIG_H_ */
1 change: 1 addition & 0 deletions include/zenoh-pico/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#define Z_FEATURE_LINK_BLUETOOTH 0
#define Z_FEATURE_LINK_WS 0
#define Z_FEATURE_LINK_SERIAL 0
#define Z_FEATURE_LINK_SERIAL_USB 0
#define Z_FEATURE_SCOUTING_UDP 1
#define Z_FEATURE_LINK_UDP_MULTICAST 1
#define Z_FEATURE_LINK_UDP_UNICAST 1
Expand Down
1 change: 1 addition & 0 deletions include/zenoh-pico/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#define Z_FEATURE_LINK_BLUETOOTH @Z_FEATURE_LINK_BLUETOOTH@
#define Z_FEATURE_LINK_WS @Z_FEATURE_LINK_WS@
#define Z_FEATURE_LINK_SERIAL @Z_FEATURE_LINK_SERIAL@
#define Z_FEATURE_LINK_SERIAL_USB @Z_FEATURE_LINK_SERIAL_USB@
#define Z_FEATURE_SCOUTING_UDP @Z_FEATURE_SCOUTING_UDP@
#define Z_FEATURE_LINK_UDP_MULTICAST @Z_FEATURE_LINK_UDP_MULTICAST@
#define Z_FEATURE_LINK_UDP_UNICAST @Z_FEATURE_LINK_UDP_UNICAST@
Expand Down
7 changes: 7 additions & 0 deletions include/zenoh-pico/system/platform/rpi_pico.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,11 @@ typedef struct {
}
#endif

#if Z_FEATURE_LINK_SERIAL_USB == 1
void _z_usb_uart_init();
void _z_usb_uart_deinit();
void _z_usb_uart_write(const uint8_t *buf, int length);
uint8_t _z_usb_uart_getc();
#endif

#endif // ZENOH_PICO_SYSTEM_RPI_PICO_TYPES_H
32 changes: 27 additions & 5 deletions src/system/rpi_pico/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -533,11 +533,15 @@ z_result_t _z_open_serial_from_pins(_z_sys_net_socket_t *sock, uint32_t txpin, u
z_result_t _z_open_serial_from_dev(_z_sys_net_socket_t *sock, char *dev, uint32_t baudrate) {
z_result_t ret = _Z_RES_OK;

if (!sock || baudrate == 0) {
return _Z_ERR_INVALID;
sock->_serial = NULL;

#if Z_FEATURE_LINK_SERIAL_USB == 1
if (strcmp("usb", dev) == 0) {
_z_usb_uart_init();
return ret;
}
#endif

sock->_serial = NULL;
uint32_t txpin = 0;
uint32_t rxpin = 0;

Expand Down Expand Up @@ -584,15 +588,27 @@ z_result_t _z_listen_serial_from_dev(_z_sys_net_socket_t *sock, char *dev, uint3
return ret;
}

void _z_close_serial(_z_sys_net_socket_t *sock) { uart_deinit(sock->_serial); }
void _z_close_serial(_z_sys_net_socket_t *sock) {
if (sock->_serial != NULL) {
uart_deinit(sock->_serial);
} else {
#if Z_FEATURE_LINK_SERIAL_USB == 1
_z_usb_uart_deinit();
#endif
}
}

size_t _z_read_serial(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len) {
z_result_t ret = _Z_RES_OK;

uint8_t *before_cobs = (uint8_t *)z_malloc(_Z_SERIAL_MAX_COBS_BUF_SIZE);
size_t rb = 0;
for (size_t i = 0; i < _Z_SERIAL_MAX_COBS_BUF_SIZE; i++) {
#if Z_FEATURE_LINK_SERIAL_USB == 1
before_cobs[i] = (sock._serial == NULL) ? _z_usb_uart_getc() : uart_getc(sock._serial);
#else
before_cobs[i] = uart_getc(sock._serial);
#endif

rb = rb + (size_t)1;
if (before_cobs[i] == (uint8_t)0x00) {
Expand Down Expand Up @@ -677,7 +693,13 @@ size_t _z_send_serial(const _z_sys_net_socket_t sock, const uint8_t *ptr, size_t
uint8_t *after_cobs = (uint8_t *)z_malloc(_Z_SERIAL_MAX_COBS_BUF_SIZE);
ssize_t twb = _z_cobs_encode(before_cobs, i, after_cobs);
after_cobs[twb] = 0x00; // Manually add the COBS delimiter
uart_write_blocking(sock._serial, after_cobs, twb + (ssize_t)1);
if (sock._serial == NULL) {
#if Z_FEATURE_LINK_SERIAL_USB == 1
_z_usb_uart_write(after_cobs, twb + (ssize_t)1);
#endif
} else {
uart_write_blocking(sock._serial, after_cobs, twb + (ssize_t)1);
}

z_free(before_cobs);
z_free(after_cobs);
Expand Down
Loading

0 comments on commit 55cbfc8

Please sign in to comment.