Skip to content

cyw43_arch_wifi_connect_timeout_ms() does not return when it connects to a network #2743

@avey3421

Description

@avey3421

I previously had an issue where lwip_init() got stuck and i fixed it. There was a part of code that was doing pointer arithmetic with a (uint8_t *) and a (uint16_t) value, so i changed it to a (uint16_t *) and it seems to work ok

After it enabling wifi station function works just fine but cyw43_arch_wifi_connect_timeout_ms() seems to not return WHEN IT CONNECTS, i can see the pico being connected in my mobile hotspot, maybe it gets stuck in an async function? I have no prior experience with parallel programming if thats the issue. If anybody knows how to help it would be really appreciated!

my c program:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "pico/stdlib.h"
#include "hardware/rtc.h"
#include "pico/cyw43_arch.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()
{
    gpio_init(22);
    gpio_set_dir(22,1);
    gpio_init(26);
    gpio_set_dir(26,1);
    gpio_init(2);
    gpio_set_dir(2,1);  
    gpio_init(4);
    gpio_set_dir(4,1);   // these are for debugging reasons  
    LCD lcd;
    stdio_init_all();
    setup(&lcd);
    turn_on_display(lcd);
    stdio_init_all();
    rtc_init();
    datetime_t t={
        .year  = 2025   ,
        .month = 11,
        .day   = 30,
        .dotw  = 6, 
        .hour  = 22,
        .min   = 41,
        .sec   = 40
    };
    if (cyw43_arch_init()){
        print_to_lcd("failed",&lcd);
        while(1){}
    }
    print_to_lcd("initialised",&lcd);
    cyw43_arch_enable_sta_mode();
    char ssid[]="ssid";
    char pass[]="pass";
    change_line(&lcd);
    if (cyw43_arch_wifi_connect_timeout_ms(ssid,pass, CYW43_AUTH_WPA2_AES_PSK,5000)){ // gets stuck when it connects
        change_line(&lcd);
        print_to_lcd("and not conneced",&lcd);
    } 
    print_to_lcd("and connected",&lcd);
    while (1){}
    if (!rtc_set_datetime(&t)) return -1;
    sleep_ms(30);
    if (!rtc_get_datetime(&t)) return -1;
    char *day = malloc(5*sizeof(char));
    char *month = malloc(5*sizeof(char));
    char *year = malloc(8*sizeof(char));
    char *min = malloc(5*sizeof(char));
    char *hour = malloc(5*sizeof(char));
    format(&t, min, hour);
    int prev_day = t.day;
    int prev_min = t.min;
    utoa(t.year,year,10);
    utoa(t.month,month,10);
    utoa(t.day,day,10);
    char *test =malloc(40*sizeof(char)); 
    char *test2=malloc(40*sizeof(char));
    strcpy(test2,"");
    if (strlen(day)==1 || strlen(day)==1){
        strcat(test2," ");
    }
    strcat(test2,"   ");
    //strcpy(test2,"   ");
    strcat(test2,day);
    strcat(test2,"/");
    strcat(test2,month);
    strcat(test2,"/");
    strcat(test2,year);
    strcat(test2,"       ");
    strcat(test2,hour);
    strcat(test2,":");
    strcat(test2,min);
    char *special_message = malloc(30*sizeof(char));
    check_for_special_date(&t,special_message);
    if (strcmp(special_message," ") != 0){
        strcpy(test,"    AUTH.csd   ");
        strcat(test,special_message);
    } else {
        strcpy(test,"    AUTH.csd       welcome!   "); 
    }
    while (true) {
        reverse_cycle(lcd,test,test2);
        if (!rtc_get_datetime(&t)) break;
        strcpy(test2,"");
        if (t.day!= prev_day){ 
            check_for_special_date(&t,special_message); 
            // checks each day (once every day so not a lot of unnecessary checks)
            strcpy(test,"    AUTH.csd   ");
            strcat(test,special_message);
            if (t.day < 10 || t.month <10){
                strcat(test2," ");  // so it lines up properly
            }
            utoa(t.year,year,10);
            utoa(t.month,month,10);
            utoa(t.day,day,10);
            prev_day = t.day;
        }
        if (t.min != prev_min){
            format(&t, min, hour);
            prev_min = t.min;
        }
        strcat(test2,"   ");
        strcat(test2,day);
        strcat(test2,"/");
        strcat(test2,month);
        strcat(test2,"/");
        strcat(test2,year);
        strcat(test2,"        ");
        strcat(test2,hour);
        strcat(test2,":");
        strcat(test2,min);
        
    }  
    print_to_lcd("  RTC ERROR!   ", &lcd);
    change_line(&lcd);
    print_to_lcd("  PLEASE RESET  ", &lcd);
    while(1){
        sleep_ms(5000);
    }
    return 0;
    
}

my cmake file:

# 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(lcd_panel 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(lcd_panel lcd_panel.c )

pico_set_program_name(lcd_panel "lcd_panel")
pico_set_program_version(lcd_panel "0.1")

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

# Add the standard library to the build
target_link_libraries(lcd_panel
        pico_stdlib
        hardware_rtc
)

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

# Add any user requested libraries
target_link_libraries(lcd_panel 
        hardware_rtc 
        pico_cyw43_arch_lwip_threadsafe_background
        pico_mbedtls
        )

pico_add_extra_outputs(lcd_panel)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions