Skip to content

Commit c44c298

Browse files
committed
Use async context for led in iperf example
Improve led handling by using the async context to turn the led on and off. Fix assert in poll mode when cyw43_arch_disable_sta_mode is called in interrupt context.
1 parent 2d44306 commit c44c298

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

pico_w/wifi/iperf/picow_iperf.c

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@
1919
#error IPERF_SERVER_IP not defined
2020
#endif
2121

22+
#if USE_LED
23+
// Invert led
24+
static void led_worker_fn(async_context_t *context, async_at_time_worker_t *worker) {
25+
// Invert the led
26+
static int led_on = true;
27+
led_on = !led_on;
28+
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, led_on);
29+
async_context_add_at_time_worker_in_ms(context, worker, 1000);
30+
}
31+
#endif
32+
2233
// Report IP results and exit
2334
static void iperf_report(void *arg, enum lwiperf_report_type report_type,
2435
const ip_addr_t *local_addr, u16_t local_port, const ip_addr_t *remote_addr, u16_t remote_port,
@@ -36,12 +47,12 @@ static void iperf_report(void *arg, enum lwiperf_report_type report_type,
3647
#endif
3748
}
3849

50+
// Note: This is called from an interrupt handler
3951
void key_pressed_func(void *param) {
4052
int key = getchar_timeout_us(0); // get any pending key press but don't wait
4153
if (key == 'd' || key == 'D') {
42-
cyw43_arch_lwip_begin();
43-
cyw43_arch_disable_sta_mode();
44-
cyw43_arch_lwip_end();
54+
bool *exit = (bool*)param;
55+
*exit = true;
4556
}
4657
}
4758

@@ -53,9 +64,6 @@ int main() {
5364
return 1;
5465
}
5566

56-
// Get notified if the user presses a key
57-
stdio_set_chars_available_callback(key_pressed_func, cyw43_arch_async_context());
58-
5967
cyw43_arch_enable_sta_mode();
6068
printf("Connecting to Wi-Fi... (press 'd' to disconnect)\n");
6169
if (cyw43_arch_wifi_connect_timeout_ms(WIFI_SSID, WIFI_PASSWORD, CYW43_AUTH_WPA2_AES_PSK, 30000)) {
@@ -77,23 +85,15 @@ int main() {
7785
#endif
7886
cyw43_arch_lwip_end();
7987

80-
while(cyw43_wifi_link_status(&cyw43_state, CYW43_ITF_STA) != CYW43_LINK_DOWN) {
88+
bool exit = false;
89+
stdio_set_chars_available_callback(key_pressed_func, &exit);
90+
8191
#if USE_LED
82-
static absolute_time_t led_time;
83-
static int led_on = true;
84-
85-
// Invert the led
86-
if (absolute_time_diff_us(get_absolute_time(), led_time) < 0) {
87-
led_on = !led_on;
88-
cyw43_gpio_set(&cyw43_state, 0, led_on);
89-
led_time = make_timeout_time_ms(1000);
90-
91-
// Check we can read back the led value
92-
bool actual_led_val = !led_on;
93-
cyw43_gpio_get(&cyw43_state, 0, &actual_led_val);
94-
assert(led_on == actual_led_val);
95-
}
92+
// start the led flashing
93+
async_at_time_worker_t led_worker = { .do_work = led_worker_fn };
94+
async_context_add_at_time_worker_in_ms(cyw43_arch_async_context(), &led_worker, 0);
9695
#endif
96+
while(!exit && cyw43_wifi_link_status(&cyw43_state, CYW43_ITF_STA) != CYW43_LINK_DOWN) {
9797
// the following #ifdef is only here so this same example can be used in multiple modes;
9898
// you do not need it in your code
9999
#if PICO_CYW43_ARCH_POLL
@@ -102,14 +102,15 @@ int main() {
102102
cyw43_arch_poll();
103103
// you can poll as often as you like, however if you have nothing else to do you can
104104
// choose to sleep until either a specified time, or cyw43_arch_poll() has work to do:
105-
cyw43_arch_wait_for_work_until(led_time);
105+
cyw43_arch_wait_for_work_until(at_the_end_of_time);
106106
#else
107107
// if you are not using pico_cyw43_arch_poll, then WiFI driver and lwIP work
108108
// is done via interrupt in the background. This sleep is just an example of some (blocking)
109109
// work you might be doing.
110110
sleep_ms(1000);
111111
#endif
112112
}
113+
cyw43_arch_disable_sta_mode();
113114

114115
cyw43_arch_deinit();
115116
printf("Test complete\n");

0 commit comments

Comments
 (0)