Skip to content

Commit

Permalink
Use RTT as a module, rebased from raspberrypi#775, with compiler fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
adfernandes committed Jun 4, 2023
1 parent 5d47872 commit 67eb4bc
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@
[submodule "lib/btstack"]
path = lib/btstack
url = https://github.com/bluekitchen/btstack.git
[submodule "lib/rtt"]
path = lib/rtt
url = [email protected]:adfernandes/segger-rtt.git
1 change: 1 addition & 0 deletions lib/rtt
Submodule rtt added at 5efbf0
3 changes: 2 additions & 1 deletion src/rp2_common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ if (NOT PICO_BARE_METAL)
pico_add_subdirectory(pico_stdio)
pico_add_subdirectory(pico_stdio_semihosting)
pico_add_subdirectory(pico_stdio_uart)
pico_add_subdirectory(pico_stdio_rtt)

pico_add_subdirectory(cmsis)
pico_add_subdirectory(tinyusb)
Expand Down Expand Up @@ -84,4 +85,4 @@ set(CMAKE_EXECUTABLE_SUFFIX "${CMAKE_EXECUTABLE_SUFFIX}" PARENT_SCOPE)
pico_add_doxygen(${CMAKE_CURRENT_LIST_DIR})
pico_add_doxygen_exclude(${CMAKE_CURRENT_LIST_DIR}/cmsis)

pico_promote_common_scope_vars()
pico_promote_common_scope_vars()
6 changes: 3 additions & 3 deletions src/rp2_common/pico_stdio/include/pico/stdio.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ typedef struct stdio_driver stdio_driver_t;
/*! \brief Initialize all of the present standard stdio types that are linked into the binary.
* \ingroup pico_stdio
*
* Call this method once you have set up your clocks to enable the stdio support for UART, USB
* and semihosting based on the presence of the respective libraries in the binary.
* Call this method once you have set up your clocks to enable the stdio support for UART, USB,
* semihosting, and RTT based on the presence of the respective libraries in the binary.
*
* When stdio_usb is configured, this method can be optionally made to block, waiting for a connection
* via the variables specified in \ref stdio_usb_init (i.e. \ref PICO_STDIO_USB_CONNECT_WAIT_TIMEOUT_MS)
*
* \return true if at least one output was successfully initialized, false otherwise.
* \see stdio_uart, stdio_usb, stdio_semihosting
* \see stdio_uart, stdio_usb, stdio_semihosting, stdio_rtt
*/
bool stdio_init_all(void);

Expand Down
11 changes: 10 additions & 1 deletion src/rp2_common/pico_stdio/stdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
#include "pico/stdio_semihosting.h"
#endif

#if LIB_PICO_STDIO_RTT
#include "pico/stdio_rtt.h"
#endif

#define STDIO_HANDLE_STDIN 0
#define STDIO_HANDLE_STDOUT 1
#define STDIO_HANDLE_STDERR 2
Expand Down Expand Up @@ -295,6 +299,11 @@ bool stdio_init_all(void) {
rc = true;
#endif

#if LIB_PICO_STDIO_RTT
stdio_rtt_init();
rc = true;
#endif

#if LIB_PICO_STDIO_USB
rc |= stdio_usb_init();
#endif
Expand Down Expand Up @@ -331,7 +340,7 @@ void stdio_set_translate_crlf(stdio_driver_t *driver, bool enabled) {
// Suppress -Wunused-parameter
(void)driver;
(void)enabled;

panic_unsupported();
#endif
}
Expand Down
15 changes: 15 additions & 0 deletions src/rp2_common/pico_stdio_rtt/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
pico_add_library(pico_stdio_rtt)

target_sources(pico_stdio_rtt
INTERFACE
stdio_rtt.c
segger_rtt.c
)

target_include_directories(pico_stdio_rtt_headers
INTERFACE
./include
../../../lib/rtt/RTT
)

pico_mirrored_target_link_libraries(pico_stdio_rtt INTERFACE pico_stdio)
37 changes: 37 additions & 0 deletions src/rp2_common/pico_stdio_rtt/include/pico/stdio_rtt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/

#ifndef _PICO_STDIO_RTT_H
#define _PICO_STDIO_RTT_H

#include "pico/stdio.h"

/** \brief Support for stdin/stdout using SEGGER RTT
* \defgroup pico_stdio_rtt pico_stdio_rtt
* \ingroup pico_stdio
*
* Linking this library or calling `pico_enable_stdio_rtt(TARGET)` in the CMake (which
* achieves the same thing) will add RTT to the drivers used for standard output
*/

#ifdef __cplusplus
extern "C" {
#endif

extern stdio_driver_t stdio_rtt;

/*! \brief Explicitly initialize stdin/stdout over RTT and add it to the current set of stdin/stdout drivers
* \ingroup pico_stdio_rtt
*
* \note this method is automatically called by \ref stdio_init_all() if `pico_stdio_rtt` is included in the build
*/
void stdio_rtt_init(void);

#ifdef __cplusplus
}
#endif

#endif
16 changes: 16 additions & 0 deletions src/rp2_common/pico_stdio_rtt/segger_rtt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
We use this round-about way of including the Segger RTT source file
because we need to suppress the "cast-align" and "cast-qual" warnings,
and cmake will not let us change the COMPILE_OPTIONS for a single
source file if the target has one source file in the current directory tree
and another source file in a directory outside the current directory tree.
See https://gitlab.kitware.com/cmake/cmake/-/issues/20128 for more details.
*/
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-align"
#pragma GCC diagnostic ignored "-Wcast-qual"
#include "../../../lib/rtt/RTT/SEGGER_RTT.c"
#pragma GCC diagnostic pop
28 changes: 28 additions & 0 deletions src/rp2_common/pico_stdio_rtt/stdio_rtt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/

#include "pico/binary_info.h"
#include "pico/stdio/driver.h"
#include "pico/stdio_rtt.h"
#include "SEGGER_RTT.h"

void stdio_rtt_init(void) {
stdio_set_driver_enabled(&stdio_rtt, true);
stdio_bi_decl_if_func_used(bi_program_feature("RTT stdin / stdout"));
}

static void stdio_rtt_out_chars(const char *buf, int length) {
SEGGER_RTT_Write(0, buf, length);
}

static int stdio_rtt_in_chars(char *buf, int length) {
return SEGGER_RTT_Read(0, buf, length);
}

stdio_driver_t stdio_rtt = {
.out_chars = stdio_rtt_out_chars,
.in_chars = stdio_rtt_in_chars,
};
13 changes: 12 additions & 1 deletion src/rp2_common/pico_stdlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ option(PICO_STDIO_UART "Globally enable stdio UART" 1)
# PICO_CMAKE_CONFIG: PICO_STDIO_USB, OPTION: Globally enable stdio USB, default=0, group=pico_stdlib
option(PICO_STDIO_USB "Globally enable stdio USB" 0)
# PICO_CMAKE_CONFIG: PICO_STDIO_SEMIHOSTING, OPTION: Globally enable stdio semihosting, default=0, group=pico_stdlib
option(PICO_STDIO_SEMIHOSTING "Globally enable stdio semi-hosting" 0)
option(PICO_STDIO_SEMIHOSTING "Globally enable stdio semihosting" 0)
# PICO_CMAKE_CONFIG: PICO_STDIO_RTT, OPTION: Globally enable stdio RTT, default=0, group=pico_stdlib
option(PICO_STDIO_RTT "Globally enable stdio rtt" 0)

if (NOT TARGET pico_stdlib)
pico_add_impl_library(pico_stdlib)
Expand Down Expand Up @@ -33,6 +35,10 @@ if (NOT TARGET pico_stdlib)
set_target_properties(${TARGET} PROPERTIES PICO_TARGET_STDIO_SEMIHOSTING ${ENABLED})
endfunction()

function(pico_enable_stdio_rtt TARGET ENABLED)
set_target_properties(${TARGET} PROPERTIES PICO_TARGET_STDIO_RTT ${ENABLED})
endfunction()

if (TARGET pico_stdio_uart)
target_link_libraries(pico_stdlib INTERFACE $<IF:$<BOOL:$<IF:$<STREQUAL:$<TARGET_PROPERTY:PICO_TARGET_STDIO_UART>,>,${PICO_STDIO_UART},$<TARGET_PROPERTY:PICO_TARGET_STDIO_UART>>>,pico_stdio_uart,>)
target_link_libraries(pico_stdlib_headers INTERFACE $<IF:$<BOOL:$<IF:$<STREQUAL:$<TARGET_PROPERTY:PICO_TARGET_STDIO_UART>,>,${PICO_STDIO_UART},$<TARGET_PROPERTY:PICO_TARGET_STDIO_UART>>>,pico_stdio_uart_headers,>)
Expand All @@ -48,4 +54,9 @@ if (NOT TARGET pico_stdlib)
target_link_libraries(pico_stdlib_headers INTERFACE $<IF:$<BOOL:$<IF:$<STREQUAL:$<TARGET_PROPERTY:PICO_TARGET_STDIO_SEMIHOSTING>,>,${PICO_STDIO_SEMIHOSTING},$<TARGET_PROPERTY:PICO_TARGET_STDIO_SEMIHOSTING>>>,pico_stdio_semihosting_headers,>)
endif()

if (TARGET pico_stdio_rtt)
target_link_libraries(pico_stdlib INTERFACE $<IF:$<BOOL:$<IF:$<STREQUAL:$<TARGET_PROPERTY:PICO_TARGET_STDIO_RTT>,>,${PICO_STDIO_RTT},$<TARGET_PROPERTY:PICO_TARGET_STDIO_RTT>>>,pico_stdio_rtt,>)
target_link_libraries(pico_stdlib_headers INTERFACE $<IF:$<BOOL:$<IF:$<STREQUAL:$<TARGET_PROPERTY:PICO_TARGET_STDIO_RTT>,>,${PICO_STDIO_RTT},$<TARGET_PROPERTY:PICO_TARGET_STDIO_RTT>>>,pico_stdio_rtt_headers,>)
endif()

endif()

0 comments on commit 67eb4bc

Please sign in to comment.