树莓派Pico_串口

本串口程序程序默认uart0

 程序调用链:

        stdio_init_all -> stdio_uart_init -> uart_init 


#include <stdio.h>
#include "pico/stdlib.h"

int main() {
    stdio_init_all();
    while (true) {
        printf("Hello, world!\n");
        sleep_ms(1000);
    }
}
bool stdio_init_all(void) {
    // todo add explicit custom, or registered although you can call stdio_enable_driver explicitly anyway
    // These are well known ones

    bool rc = false;
#if LIB_PICO_STDIO_UART
    stdio_uart_init();
    rc = true;
#endif

#if LIB_PICO_STDIO_SEMIHOSTING
    stdio_semihosting_init();
    rc = true;
#endif

#if LIB_PICO_STDIO_USB
    rc |= stdio_usb_init();
#endif
    return rc;
}
void stdio_uart_init() {
    uart_init(uart_default, 0);
}

 此处uart_default被定义为:#define  uart_default  uart0

在uart_init函数里面进行uart参数的初始化

uint uart_init(uart_inst_t *uart, uint baudrate) {
    invalid_params_if(UART, uart != uart0 && uart != uart1);

    if (clock_get_hz(clk_peri) == 0) {
        return 0;
    }

    uart_reset(uart);
    uart_unreset(uart);

#if PICO_UART_ENABLE_CRLF_SUPPORT
    uart_set_translate_crlf(uart, PICO_UART_DEFAULT_CRLF);
#endif

    // Any LCR writes need to take place before enabling the UART
    uint baud = uart_set_baudrate(uart, baudrate);
    uart_set_format(uart, 8, 1, UART_PARITY_NONE);

    // Enable FIFOs (must be before setting UARTEN, as this is an LCR access)
    hw_set_bits(&uart_get_hw(uart)->lcr_h, UART_UARTLCR_H_FEN_BITS);
    // Enable the UART, both TX and RX
    uart_get_hw(uart)->cr = UART_UARTCR_UARTEN_BITS | UART_UARTCR_TXE_BITS | UART_UARTCR_RXE_BITS;
    // Always enable DREQ signals -- no harm in this if DMA is not listening
    uart_get_hw(uart)->dmacr = UART_UARTDMACR_TXDMAE_BITS | UART_UARTDMACR_RXDMAE_BITS;

    return baud;
}

测试:需要连接usb->ttl串口模块,波特率设置为115200,至于为什么,自己分析源码吧,我这就不在分析了,打开串口调试助手就可以看到打印的信息了

麻烦点个关注了

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-18 21:04:02       91 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-18 21:04:02       97 阅读
  3. 在Django里面运行非项目文件

    2023-12-18 21:04:02       78 阅读
  4. Python语言-面向对象

    2023-12-18 21:04:02       88 阅读

热门阅读

  1. selenium学习

    2023-12-18 21:04:02       57 阅读
  2. Elasticsearch Spring Data集成-05

    2023-12-18 21:04:02       44 阅读
  3. Redis系列之事务机制

    2023-12-18 21:04:02       58 阅读
  4. RC4系列

    2023-12-18 21:04:02       45 阅读
  5. gcov在嵌入式设备上使用说明更新

    2023-12-18 21:04:02       57 阅读
  6. C++入门(2)

    2023-12-18 21:04:02       43 阅读
  7. 高效运营的企业电脑监控软件有哪些?

    2023-12-18 21:04:02       62 阅读