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

cyw43_arch_init() gets stuck

Open avey3421 opened this issue 1 month ago • 3 comments

I am trying to make an lcd display show the current time

Since the pico has its own rtc and time.h does not return the actual time, i want to set up Wifi so i get the epoch time from a website, i create a C/C++ project from the raspberry pi pico extention with the option of onboard LED, my code gets stuck on cyw43_arch_init() (infinite loop?)

my code is:


#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/cyw43_arch.h"
#include <string.h>
#include <stdlib.h>
#include "hardware/rtc.h"

#define data_out_port_0 6
#define data_out_port_1 7   
#define data_out_port_2 8
#define data_out_port_3 9
#define data_out_port_4 10
#define data_out_port_5 11
#define data_out_port_6 12
#define data_out_port_7 13
#define rw_port 16
#define e_port 18
#define rs_port 17



typedef struct{
    int DATA0, DATA1, DATA2, DATA3,
        DATA4, DATA5, DATA6, DATA7,
        RW, RS, E,character_count;  
}LCD;
void write_data(){  
    gpio_put(rs_port, 1);
    gpio_put(rw_port, 0); 
    gpio_put(e_port, 1);
    sleep_ms(15);
    gpio_put(e_port, 0);
}


void setup(LCD *lcd){
    lcd->character_count = 0;
    lcd->DATA0 = data_out_port_0;
    lcd->DATA1 = data_out_port_1;
    lcd->DATA2 = data_out_port_2;
    lcd->DATA3 = data_out_port_3;
    lcd->DATA4 = data_out_port_4;
    lcd->DATA5 = data_out_port_5;
    lcd->DATA6 = data_out_port_6;
    lcd->DATA7 = data_out_port_7;
    lcd->RW = rw_port;
    lcd->RS = rs_port;
    lcd->E = e_port;
    //init output ports
    for (int i = data_out_port_0; i <= data_out_port_7; i++) {
        gpio_init(i);
        gpio_set_dir(i, GPIO_OUT);
    }
    gpio_init(rw_port);
    gpio_set_dir(rw_port, GPIO_OUT);
    gpio_init(e_port);
    gpio_set_dir(e_port, GPIO_OUT);
    gpio_init(rs_port);
    gpio_set_dir(rs_port, GPIO_OUT);
}


void print_to_lcd(char* str,LCD *lcd){
    uint len = strlen(str);  
    // sending data to lcd
    uint32_t mask = 1<<data_out_port_0 | 1<<data_out_port_1 | 1<<data_out_port_2 | 1<<data_out_port_3 |
                    1<<data_out_port_4 | 1<<data_out_port_5 | 1<<data_out_port_6 | 1<<data_out_port_7;
    for (uint i = 0; i < len; i++) { 
        uint32_t data = str[i]<<6;
        gpio_put_masked(mask,data);
        write_data();
    }
    lcd->character_count += len;
}


void turn_on_display(LCD lcd){

     // appears to make lcd dimmer and 2 line display doesnt work with this
    // put 2 line mode first   
    
    gpio_put(e_port, 1);
    gpio_put(data_out_port_0, 0);
    gpio_put(data_out_port_1, 0);
    gpio_put(data_out_port_2, 0);
    gpio_put(data_out_port_3, 1); // this sets 2 line mode
    gpio_put(data_out_port_4, 1);
    gpio_put(data_out_port_5, 1);
    gpio_put(data_out_port_6, 0);
    gpio_put(data_out_port_7, 0);
    gpio_put(rw_port, 0);
    gpio_put(rs_port, 0);
    sleep_ms(15);
    gpio_put(e_port, 0);
    for (int i = data_out_port_0; i <= data_out_port_7; i++) {
        gpio_put(i, 0);
    }

    // display on, cursor on, blink off
    gpio_put(e_port, 1);
    gpio_put(data_out_port_0, 0);
    gpio_put(data_out_port_1, 1);
    gpio_put(data_out_port_2, 1);
    gpio_put(data_out_port_3, 1);
    for (int i = data_out_port_4; i<=data_out_port_7; i++) {
        gpio_put(i, 0);
    }
    gpio_put(rs_port, 0);
    gpio_put(rw_port, 0);
    sleep_ms(15);
    gpio_put(e_port, 0);
    for (int i = data_out_port_0; i<=data_out_port_3; i++) {
        gpio_put(i, 0);
    }     
}

void change_line(LCD *lcd){
    
    gpio_put(e_port, 1);
    gpio_put(data_out_port_0, 0);
    gpio_put(data_out_port_1, 0);
    gpio_put(data_out_port_2, 0);
    gpio_put(data_out_port_3, 1);
    gpio_put(data_out_port_4, 0);
    gpio_put(data_out_port_5, 1);
    gpio_put(data_out_port_6, 0);
    gpio_put(data_out_port_7, 1);
    gpio_put(rs_port, 0);
    gpio_put(rw_port, 0);
    sleep_ms(15);
    gpio_put(e_port, 0);

}

void clear(LCD lcd){
    gpio_put(e_port, 1);
    gpio_put(data_out_port_0, 1);
    for(int i = data_out_port_1 ; i <= data_out_port_7; i++){
        gpio_put(i, 0);
    }
    gpio_put(rs_port, 0);
    gpio_put(rw_port, 0);
    sleep_ms(15);
    gpio_put(e_port, 0);
}

void shift(LCD lcd){
    gpio_put(e_port, 1);
    gpio_put(data_out_port_2, 1);
    gpio_put(data_out_port_3, 1);
    gpio_put(data_out_port_4, 1);
    for(int i = data_out_port_5 ; i <= data_out_port_7; i++){
        gpio_put(i, 0);
    }
    gpio_put(rs_port, 0);
    gpio_put(rw_port, 0);
    sleep_ms(15);
    gpio_put(e_port, 0);

}

void cycle(LCD lcd,char *firstmessage,char *secondmessage){
    print_to_lcd(firstmessage, &lcd);
    change_line(&lcd);
    print_to_lcd(secondmessage, &lcd);
    sleep_ms(2000);
    for(int i = 0; i < 16; i++){
        shift(lcd);
        sleep_ms(800);
    }
    clear(lcd);
}

void shift_left(LCD lcd){
    gpio_put(e_port, 1);
    gpio_put(data_out_port_0, 0);
    gpio_put(data_out_port_1, 0);
    gpio_put(data_out_port_2, 0);
    gpio_put(data_out_port_3, 1);
    gpio_put(data_out_port_4, 1);
    for(int i = data_out_port_5 ; i <= data_out_port_7; i++){
        gpio_put(i, 0);
    }
    gpio_put(rs_port, 0);
    gpio_put(rw_port, 0);
    sleep_ms(15);
    gpio_put(e_port, 0);
    

}

void reverse_cycle(LCD lcd,char *firstmessage,char *secondmessage){
    print_to_lcd(firstmessage, &lcd);
    change_line(&lcd);
    print_to_lcd(secondmessage, &lcd);
    sleep_ms(1000);
    int scroll = strlen(firstmessage);
    for(int i = 0; i < 15; i++){
        shift_left(lcd);
        sleep_ms(750);
    }
    sleep_ms(1000);
    clear(lcd);
}

void check_for_special_date(datetime_t *t,char *special_message){
    if( t->day == 25 && t->month ==12 ){
        strcpy(special_message,"   Merry Xmas!");
    }
    else if( t->day == 1 && t->month ==1 ){
        strcpy(special_message," Happy New Year!");
    }
    else {
        strcpy(special_message,"    welcome!  ");
    }
}

void format(datetime_t *t, char* min, char* hour){
    char *mintemp = malloc(4*sizeof(char));
    char *hourtemp = malloc(4*sizeof(char));
    if (t->hour == 0){
        strcpy(hour,"0");
        strcat(hour,utoa(t->hour,hourtemp,10));
    }else {
        strcpy(hour,utoa(t->hour,hour,10));
    }
    if (t->min < 10){
        strcpy(min,"0");
        strcat(min,utoa(t->min,mintemp,10));
    }else strcpy(min,utoa(t->min,mintemp,10));
}




int main()
{
    LCD lcd;
    rtc_init(); 
    datetime_t t={
        .year  = 2025,
        .month = 12,
        .day   = 24,
        .dotw  = 5, 
        .hour  = 23,
        .min   = 59,
        .sec   = 57
    };
    setup(&lcd);
    turn_on_display(lcd);
    if (!rtc_set_datetime(&t)) return -1;
    sleep_ms(30);
    if (!rtc_get_datetime(&t)) return -1;


    // Initialise the Wi-Fi chip
    stdio_init_all();
    print_to_lcd("hello! ",&lcd);
    if (cyw43_arch_init()) {
        print_to_lcd("Wi-Fi init failed",&lcd);
        while(1){sleep_ms(1000);}
    }
    print_to_lcd("before connecting",&lcd);
    cyw43_arch_enable_sta_mode();
    char ssid[]="ssid";
    char pass[]="pass";
    if (cyw43_arch_wifi_connect_timeout_ms(ssid, pass, CYW43_AUTH_WPA2_AES_PSK, 1000)){
        print_to_lcd("connected!\n",&lcd);
    }
    print_to_lcd("error connecting",&lcd);
    while (true) {
        print_to_lcd("test",&lcd);
        sleep_ms(1000);
    }
}


cmake:

# Generated Cmake Pico project file

cmake_minimum_required(VERSION 3.13)

set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# Initialise pico_sdk from installed location
# (note this can come from environment, CMake cache etc)

# == DO NOT EDIT THE FOLLOWING LINES for the Raspberry Pi Pico VS Code Extension to work ==
if(WIN32)
    set(USERHOME $ENV{USERPROFILE})
else()
    set(USERHOME $ENV{HOME})
endif()
set(sdkVersion 2.2.0)
set(toolchainVersion 14_2_Rel1)
set(picotoolVersion 2.2.0-a4)
set(picoVscode ${USERHOME}/.pico-sdk/cmake/pico-vscode.cmake)
if (EXISTS ${picoVscode})
    include(${picoVscode})
endif()
# ====================================================================================
set(PICO_BOARD pico_w CACHE STRING "Board type")

# Pull in Raspberry Pi Pico SDK (must be before project)
include(pico_sdk_import.cmake)

project(onboard_led C CXX ASM)

# Initialise the Raspberry Pi Pico SDK
pico_sdk_init()

# Add executable. Default name is the project name, version 0.1

add_executable(onboard_led onboard_led.c )

pico_set_program_name(onboard_led "onboard_led")
pico_set_program_version(onboard_led "0.1")

# Modify the below lines to enable/disable output over UART/USB
pico_enable_stdio_uart(onboard_led 0)
pico_enable_stdio_usb(onboard_led 0)

# Add the standard library to the build
target_link_libraries(onboard_led
        pico_stdlib)

# Add the standard include files to the build
target_include_directories(onboard_led PRIVATE
        ${CMAKE_CURRENT_LIST_DIR}
)

# Add any user requested libraries
target_link_libraries(onboard_led 
        pico_cyw43_arch_lwip_threadsafe_background
        hardware_rtc
        )

pico_add_extra_outputs(onboard_led)


thanks if someone knows how to fix this! this is my first project im pretty much new to everything

avey3421 avatar Nov 24 '25 11:11 avey3421

Does a pico W examples work ok? Does the uart print any errors in debug? I can't spot the reason why it would not return. Are you sure it's not returning an error?

peterharperuk avatar Nov 24 '25 12:11 peterharperuk

Does a pico W examples work ok? Does the uart print any errors in debug? I can't spot the reason why it would not return. Are you sure it's not returning an error?

hey sorry for the late reply i boiled down the issue in the function lwip_init() in the 3rd party libraries under the lwip folder

lwip_init(void)
{
#ifndef LWIP_SKIP_CONST_CHECK
  int a = 0;
  LWIP_UNUSED_ARG(a);
  LWIP_ASSERT("LWIP_CONST_CAST not implemented correctly. Check your lwIP port.", LWIP_CONST_CAST(void *, &a) == &a);
#endif
#ifndef LWIP_SKIP_PACKING_CHECK
  LWIP_ASSERT("Struct packing not implemented correctly. Check your lwIP port.", sizeof(struct packed_struct_test) == PACKED_STRUCT_TEST_EXPECTED_SIZE);
#endif
  /* Modules initialization */
  stats_init();
#if !NO_SYS
  sys_init();
#endif /* !NO_SYS */
  mem_init();
  memp_init();
  pbuf_init();
  netif_init();
#if LWIP_IPV4
  ip_init();
#if LWIP_ARP
  etharp_init();
#endif /* LWIP_ARP */
#endif /* LWIP_IPV4 */
#if LWIP_RAW
  raw_init();
#endif /* LWIP_RAW */
#if LWIP_UDP
  udp_init();
#endif /* LWIP_UDP */
#if LWIP_TCP
  tcp_init();
#endif /* LWIP_TCP */
#if LWIP_IGMP
  igmp_init();
#endif /* LWIP_IGMP */
#if LWIP_DNS
  dns_init();
#endif /* LWIP_DNS */
#if PPP_SUPPORT
  ppp_init();
#endif
#if LWIP_TIMERS
  sys_timeouts_init();
#endif /* LWIP_TIMERS */
}

this function in specifically gets stuck

avey3421 avatar Nov 29 '25 23:11 avey3421

I'm not going to comment on why your code may not be working, but...

Since the pico has its own rtc and time.h does not return the actual time, i want to set up Wifi so i get the epoch time from a website

You might find https://github.com/raspberrypi/pico-examples/pull/716 to be very useful 🙂

lurch avatar Nov 30 '25 14:11 lurch