Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8374

General • Re: Redirect UART from pico debug probe to gpio 16 and 17

$
0
0
What's the point to have defaults, and then going to change them?
Is the code more readable and predictable, or easier to change?

I avoid using defaults as much as possible.
Depends on your goals. Maybe you changed a lot of defaults so a board header is easier to use with the default PICO symbols.

As far as UART, uart.h has these defaults:

Code:

// PICO_CONFIG: PICO_DEFAULT_UART, Define the default UART used for printf etc, min=0, max=1, default=Usually provided via board header, group=hardware_uart// PICO_CONFIG: PICO_DEFAULT_UART_TX_PIN, Define the default UART TX pin, min=0, max=47 on RP2350B, 29 otherwise, default=Usually provided via board header, group=hardware_uart// PICO_CONFIG: PICO_DEFAULT_UART_RX_PIN, Define the default UART RX pin, min=0, max=47 on RP2350B, 29 otherwise, default=Usually provided via board header, group=hardware_uart// PICO_CONFIG: PICO_DEFAULT_UART_BAUD_RATE, Define the default UART baudrate, max=921600, default=115200, group=hardware_uart#ifndef PICO_DEFAULT_UART_BAUD_RATE#define PICO_DEFAULT_UART_BAUD_RATE 115200   ///< Default baud rate#endif
So, for a learning example, perhaps some macros to let the programmer choose new defaults. I like to have an area that I can uncomment to change defaults then use the same symbol for the code, and assign the default value to my custom symbols if not. Yes, one could just write:

Code:

stdio_uart_init_full(uart0, 921600, 16, 17);
Perhaps that's the most simple way to illustrate the method.
Here is SDK default oriented macro stuff:
main.c

Code:

#include <stdio.h>#include "pico/stdlib.h"// Uncomment for custom settings / comment to restore defaults#define UART_ID uart0#define UART_TX_PIN 16#define UART_RX_PIN 17#define UART_BAUD_RATE 921600// --------------------------------------------------#define UART_NUMBER     uart_get_index(UART_ID)#ifndef UART_ID#define UART_ID PICO_DEFAULT_UART#endif#ifndef UART_BAUD_RATE#define UART_BAUD_RATE PICO_DEFAULT_UART_BAUD_RATE#endif#ifndef UART_TX_PIN#define UART_TX_PIN PICO_DEFAULT_UART_TX_PIN#endif#ifndef UART_RX_PIN#define UART_RX_PIN PICO_DEFAULT_UART_RX_PIN#endifint main() {    stdio_uart_init_full(UART_ID, UART_BAUD_RATE, UART_TX_PIN, UART_RX_PIN);    while(1) {        sleep_ms(1000);        printf("Hello from standard out over UART%d on pins %d (TX) and %d (RX) at %d baud!\n", UART_NUMBER, UART_TX_PIN, UART_RX_PIN, UART_BAUD_RATE);    }}
CMakeLists.txt

Code:

set(PROJECT uartstdio)cmake_minimum_required(VERSION 3.12)include($ENV{PICO_SDK_PATH}/external/pico_sdk_import.cmake)project(${PROJECT} C CXX ASM)pico_sdk_init()add_executable(${PROJECT} main.c)target_link_libraries(${PROJECT} pico_stdlib)pico_add_extra_outputs(${PROJECT})pico_enable_stdio_usb(${PROJECT} 0)pico_enable_stdio_uart(${PROJECT} 1)
So, whatever floats your boat!

Statistics: Posted by breaker — Sun Jan 25, 2026 7:04 pm



Viewing all articles
Browse latest Browse all 8374

Trending Articles