pico-sdk icon indicating copy to clipboard operation
pico-sdk copied to clipboard

using wifi on PicoW while using serial on UART0 via PicoProbe does not maintain connection on a chip reset

Open gherlein opened this issue 5 months ago • 3 comments

This may be something I don't grok about how the UART and the wifi chip interact. Here's the setup NOT using wifi:

I have a PicoProbe connected to SWD and to UART0 (GP0 and GP1). Anything I printf comes across just great, and the serial connection survives a chip flash or reset. For example, I can do this:

openocd -f interface/cmsis-dap.cfg -f target/rp2040.cfg -c "adapter speed 5000" -c "init ; reset halt ; rp2040.core1 arp_reset assert 0 ; rp2040.core0 arp_reset assert 0 ; exit"

it's magic - the board resets and I have serial maintained across the link.

UNLESS I also use anything in pico_cyw43_arch_lwip_threadsafe_background - even blinking the LED. What happens is that I have to kill and restart the serial connection to get it to work again.

I'm not deep enough into this all to understand, but it seems like something in that library is resetting the UART somehow?

Anyone able to explain this? The code is below

/**
 * Copyright (c) 2024 Gregory C Herlein
 * Derived from sample code at https://www.iopress.info/index.php/books/master-the-raspberry-pi-pico-in-c-wifi-with-lwip-mbedtls/9-programs/73-picocprogramswifi?showall=1
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include "pico/stdlib.h"
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include "pico/stdlib.h"
#include "pico/binary_info.h"
#include "pico/cyw43_arch.h"

#include "credentials.h"

int setup(uint32_t country, const char *ssid, const char *pass, uint32_t auth)
{
    if (cyw43_arch_init_with_country(country))
    {
        return 1;
    }
    cyw43_arch_enable_sta_mode();
    if (cyw43_arch_wifi_connect_blocking(ssid, pass, auth))
    {
        return 2;
    }
    printf("IP: %s\n",
           ip4addr_ntoa(netif_ip_addr4(netif_default)));
    printf("Mask: %s\n",
           ip4addr_ntoa(netif_ip_netmask4(netif_default)));
    printf("Gateway: %s\n",
           ip4addr_ntoa(netif_ip_gw4(netif_default)));
    printf("Host Name: %s\n",
           netif_get_hostname(netif_default));
}

int main()
{
    uint32_t auth = CYW43_AUTH_WPA2_MIXED_PSK;
    // workaround for a hardware debug problem
    // https://forums.raspberrypi.com/viewtopic.php?t=363914
    // also in openocd you need to us -c "set USE_CORE 0"  before rp2040.cfg is loaded per https://github.com/raspberrypi/debugprobe/issues/45
    timer_hw->dbgpause = 0;
    sleep_ms(150);

    stdio_init_all();
    setup(country, ssid, pass, auth);

    while (true)
    {
        printf("on\n");
        cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1);
        sleep_ms(500);
        printf("off\n");
        cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0);
        sleep_ms(500);
    }
}

cmake_minimum_required(VERSION 3.13)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

include(pico_sdk_import.cmake)

project(main C CXX ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
set(PICO_BOARD pico_w)

pico_sdk_init()

add_executable(main    
    main.c
)

pico_enable_stdio_usb(main 0)
pico_enable_stdio_uart(main 1)

target_include_directories(main PRIVATE
        ${CMAKE_CURRENT_LIST_DIR}
)

pico_add_extra_outputs(main)

target_link_libraries(main
	pico_stdlib
    pico_cyw43_arch_lwip_threadsafe_background 
    #pico_cyw43_arch_none
    # other libs as needed
)

add_custom_target(flash
    COMMAND /bin/bash ../flash main.elf
    DEPENDS main
)
add_custom_target(reset
    COMMAND /bin/bash ../reset
    DEPENDS main
)

(Edited by lurch to put the code inside triple-backticks)

gherlein avatar Sep 01 '24 17:09 gherlein