Initial Release
This commit is contained in:
13
src/rp2_common/pico_stdio_uart/CMakeLists.txt
Normal file
13
src/rp2_common/pico_stdio_uart/CMakeLists.txt
Normal file
@ -0,0 +1,13 @@
|
||||
add_library(pico_stdio_uart INTERFACE)
|
||||
|
||||
target_sources(pico_stdio_uart INTERFACE
|
||||
${CMAKE_CURRENT_LIST_DIR}/stdio_uart.c
|
||||
)
|
||||
|
||||
target_include_directories(pico_stdio_uart INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include)
|
||||
|
||||
target_compile_definitions(pico_stdio_uart INTERFACE
|
||||
PICO_STDIO_UART=1
|
||||
)
|
||||
|
||||
target_link_libraries(pico_stdio_uart INTERFACE pico_stdio)
|
62
src/rp2_common/pico_stdio_uart/include/pico/stdio_uart.h
Normal file
62
src/rp2_common/pico_stdio_uart/include/pico/stdio_uart.h
Normal file
@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#ifndef _PICO_STDIO_UART_H
|
||||
#define _PICO_STDIO_UART_H
|
||||
|
||||
#include "pico/stdio.h"
|
||||
#include "hardware/uart.h"
|
||||
|
||||
/** \brief Support for stdin/stdout using UART
|
||||
* \defgroup pico_stdio_uart pico_stdio_uart
|
||||
* \ingroup pico_stdio
|
||||
*
|
||||
* Linking this library or calling `pico_enable_stdio_uart(TARGET)` in the CMake (which
|
||||
* achieves the same thing) will add UART to the drivers used for standard output
|
||||
*/
|
||||
|
||||
// PICO_CONFIG: PICO_STDIO_UART_DEFAULT_CRLF, Default state of CR/LF translation for UART output, type=bool, default=PICO_STDIO_DEFAULT_CRLF, group=pico_stdio_uart
|
||||
#ifndef PICO_STDIO_UART_DEFAULT_CRLF
|
||||
#define PICO_STDIO_UART_DEFAULT_CRLF PICO_STDIO_DEFAULT_CRLF
|
||||
#endif
|
||||
|
||||
extern stdio_driver_t stdio_uart;
|
||||
|
||||
/*! \brief Explicitly initialize stdin/stdout over UART and add it to the current set of stdin/stdout drivers
|
||||
* \ingroup pico_stdio_uart
|
||||
*
|
||||
* This method sets up PICO_DEFAULT_UART_TX_PIN for UART output (if defined), PICO_DEFAULT_UART_RX_PIN for input (if defined)
|
||||
* and configures the baud rate as PICO_DEFAULT_UART_BAUD_RATE.
|
||||
*
|
||||
* \note this method is automatically called by \ref stdio_init_all() if `pico_stdio_uart` is included in the build
|
||||
*/
|
||||
void stdio_uart_init();
|
||||
|
||||
/*! \brief Explicitly initialize stdout only (no stdin) over UART and add it to the current set of stdout drivers
|
||||
* \ingroup pico_stdio_uart
|
||||
*
|
||||
* This method sets up PICO_DEFAULT_UART_TX_PIN for UART output (if defined) , and configures the baud rate as PICO_DEFAULT_UART_BAUD_RATE
|
||||
*/
|
||||
void stdout_uart_init();
|
||||
|
||||
/*! \brief Explicitly initialize stdin only (no stdout) over UART and add it to the current set of stdin drivers
|
||||
* \ingroup pico_stdio_uart
|
||||
*
|
||||
* This method sets up PICO_DEFAULT_UART_RX_PIN for UART input (if defined) , and configures the baud rate as PICO_DEFAULT_UART_BAUD_RATE
|
||||
*/
|
||||
void stdin_uart_init();
|
||||
|
||||
/*! \brief Perform custom initialization initialize stdin/stdout over UART and add it to the current set of stdin/stdout drivers
|
||||
* \ingroup pico_stdio_uart
|
||||
*
|
||||
* \param uart the uart instance to use, \ref uart0 or \ref uart1
|
||||
* \param baud_rate the baud rate in Hz
|
||||
* \param tx_pin the UART pin to use for stdout (or -1 for no stdout)
|
||||
* \param rx_pin the UART pin to use for stdin (or -1 for no stdin)
|
||||
*/
|
||||
void stdio_uart_init_full(uart_inst_t *uart, uint baud_rate, int tx_pin, int rx_pin);
|
||||
|
||||
#endif
|
97
src/rp2_common/pico_stdio_uart/stdio_uart.c
Normal file
97
src/rp2_common/pico_stdio_uart/stdio_uart.c
Normal file
@ -0,0 +1,97 @@
|
||||
/*
|
||||
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#include "pico/stdio/driver.h"
|
||||
#include "pico/stdio_uart.h"
|
||||
#include "pico/binary_info.h"
|
||||
#include "hardware/gpio.h"
|
||||
|
||||
static uart_inst_t *uart_instance;
|
||||
|
||||
#if PICO_NO_BI_STDIO_UART
|
||||
#define stdio_bi_decl_if_func_used(x)
|
||||
#else
|
||||
#define stdio_bi_decl_if_func_used bi_decl_if_func_used
|
||||
#endif
|
||||
|
||||
void stdio_uart_init() {
|
||||
int tx_pin = -1;
|
||||
int rx_pin = -1;
|
||||
#if defined(PICO_DEFAULT_UART_TX_PIN) && PICO_DEFAULT_UART_TX_PIN != -1
|
||||
tx_pin = PICO_DEFAULT_UART_TX_PIN;
|
||||
#if defined(PICO_DEFAULT_UART_RX_PIN) && PICO_DEFAULT_UART_RX_PIN != -1
|
||||
rx_pin = PICO_DEFAULT_UART_RX_PIN;
|
||||
stdio_bi_decl_if_func_used(bi_program_feature("UART stdin / stdout"));
|
||||
bi_decl_if_func_used(bi_2pins_with_func(PICO_DEFAULT_UART_RX_PIN, PICO_DEFAULT_UART_TX_PIN, GPIO_FUNC_UART));
|
||||
#else
|
||||
stdio_bi_decl_if_func_used(bi_program_feature("UART stdout"));
|
||||
bi_decl_if_func_used(bi_1pin_with_func(PICO_DEFAULT_UART_TX_PIN, GPIO_FUNC_UART));
|
||||
#endif
|
||||
#elif defined(PICO_DEFAULT_UART_RX_PIN) && PICO_DEFAULT_UART_RX_PIN != -1
|
||||
rx_pin = PICO_DEFAULT_UART_RX_PIN;
|
||||
stdio_bi_decl_if_func_used(bi_program_feature("UART stdin"));
|
||||
bi_decl_if_func_used(bi_1pin_with_func(PICO_DEFAULT_UART_RX_PIN, GPIO_FUNC_UART));
|
||||
#endif
|
||||
#if !defined(PICO_DEFAULT_UART_BAUD_RATE) || !defined(uart_default)
|
||||
panic("UART baud rate undefined");
|
||||
#else
|
||||
stdio_uart_init_full(uart_default, PICO_DEFAULT_UART_BAUD_RATE, tx_pin, rx_pin);
|
||||
#endif
|
||||
}
|
||||
|
||||
void stdout_uart_init() {
|
||||
#ifdef PICO_DEFAULT_UART_TX_PIN
|
||||
bi_decl_if_func_used(bi_1pin_with_func(PICO_DEFAULT_UART_TX_PIN, GPIO_FUNC_UART));
|
||||
#if !defined(PICO_DEFAULT_UART_BAUD_RATE) || !defined(uart_default)
|
||||
panic("UART baud rate undefined");
|
||||
#else
|
||||
stdio_bi_decl_if_func_used(bi_program_feature("UART stdout"));
|
||||
stdio_uart_init_full(uart_default, PICO_DEFAULT_UART_BAUD_RATE, PICO_DEFAULT_UART_TX_PIN, -1);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
void stdin_uart_init() {
|
||||
#ifdef PICO_DEFAULT_UART_RX_PIN
|
||||
bi_decl_if_func_used(bi_1pin_with_func(PICO_DEFAULT_UART_RX_PIN, GPIO_FUNC_UART));
|
||||
#if !defined(PICO_DEFAULT_UART_BAUD_RATE) || !defined(uart_default)
|
||||
panic("UART baud rate undefined");
|
||||
#else
|
||||
stdio_bi_decl_if_func_used(bi_program_feature("UART stdin"));
|
||||
stdio_uart_init_full(uart_default, PICO_DEFAULT_UART_BAUD_RATE, -1, PICO_DEFAULT_UART_RX_PIN);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
void stdio_uart_init_full(struct uart_inst *uart, uint baud_rate, int tx_pin, int rx_pin) {
|
||||
uart_instance = uart;
|
||||
uart_init(uart_instance, baud_rate);
|
||||
if (tx_pin != -1) gpio_set_function(tx_pin, GPIO_FUNC_UART);
|
||||
if (rx_pin != -1) gpio_set_function(rx_pin, GPIO_FUNC_UART);
|
||||
stdio_set_driver_enabled(&stdio_uart, true);
|
||||
}
|
||||
|
||||
static void stdio_uart_out_chars(const char *buf, int length) {
|
||||
for (uint i = 0; i <length; i++) {
|
||||
uart_putc(uart_default, buf[i]);
|
||||
}
|
||||
}
|
||||
|
||||
int stdio_uart_in_chars(char *buf, int length) {
|
||||
int i=0;
|
||||
while (i<length && uart_is_readable(uart_default)) {
|
||||
buf[i++] = uart_getc(uart_default);
|
||||
}
|
||||
return i ? i : PICO_ERROR_NO_DATA;
|
||||
}
|
||||
|
||||
stdio_driver_t stdio_uart = {
|
||||
.out_chars = stdio_uart_out_chars,
|
||||
.in_chars = stdio_uart_in_chars,
|
||||
#if PICO_STDIO_ENABLE_CRLF_SUPPORT
|
||||
.crlf_enabled = PICO_STDIO_UART_DEFAULT_CRLF
|
||||
#endif
|
||||
};
|
Reference in New Issue
Block a user