Skip to content

Commit

Permalink
Add serial port support
Browse files Browse the repository at this point in the history
  • Loading branch information
sashacmc committed Dec 4, 2024
1 parent bbb6ad9 commit b04f08f
Show file tree
Hide file tree
Showing 19 changed files with 311 additions and 53 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Currently, zenoh-pico provides support for the following (RT)OSs and protocols:
| **OpenCR** | UDP (unicast and multicast), TCP | IPv4 | WiFi |
| **Emscripten** | Websocket | IPv4, IPv6 | WiFi, Ethernet |
| **FreeRTOS-Plus-TCP** | UDP (unicast), TCP | IPv4 | Ethernet |
|**Raspberry Pi Pico W**| UDP (unicast and multicast), TCP | IPv4 | WiFi |
|**Raspberry Pi Pico W**| UDP (unicast and multicast), TCP | IPv4 | WiFi, Serial |

Check the website [zenoh.io](http://zenoh.io) and the [roadmap](https://github.com/eclipse-zenoh/roadmap) for more detailed information.

Expand Down
16 changes: 7 additions & 9 deletions examples/rpi_pico_w/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ message(STATUS "ZENOH_CONFIG_MODE: ${ZENOH_CONFIG_MODE}")
message(STATUS "ZENOH_CONFIG_CONNECT: ${ZENOH_CONFIG_CONNECT}")
message(STATUS "ZENOH_CONFIG_LISTEN: ${ZENOH_CONFIG_LISTEN}")

configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/include/config.h.in"
"${CMAKE_CURRENT_SOURCE_DIR}/include/config.h"
)

set(CMAKE_C_STANDARD 11)

# Include Raspberry Pi Pico SDK
Expand Down Expand Up @@ -46,6 +51,7 @@ set(WITH_RPI_PICO_W ON)
configure_include_project(ZENOHPICO zenohpico zenohpico::lib "../.." zenohpico "https://github.com/eclipse-zenoh/zenoh-pico" "")

target_link_libraries(zenohpico_static
hardware_uart
pico_stdlib
pico_cyw43_arch_lwip_sys_freertos
FreeRTOS-Kernel-Heap4
Expand All @@ -59,10 +65,6 @@ target_include_directories(zenohpico_static PRIVATE

add_library(main STATIC main.c)

target_compile_definitions(main PRIVATE
WIFI_SSID=\"${WIFI_SSID}\"
WIFI_PASSWORD=\"${WIFI_PASSWORD}\"
)
target_include_directories(main PRIVATE
${CMAKE_CURRENT_LIST_DIR}
${CMAKE_CURRENT_LIST_DIR}/include
Expand All @@ -77,16 +79,12 @@ function(add_example name)
add_executable(${name} ${name}.c)
target_link_libraries(${name}
main
hardware_uart
pico_stdlib
pico_cyw43_arch_lwip_sys_freertos
FreeRTOS-Kernel-Heap4
zenohpico::lib
)
target_compile_definitions(${name} PRIVATE
ZENOH_CONFIG_MODE=\"${ZENOH_CONFIG_MODE}\"
ZENOH_CONFIG_CONNECT=\"${ZENOH_CONFIG_CONNECT}\"
ZENOH_CONFIG_LISTEN=\"${ZENOH_CONFIG_LISTEN}\"
)
target_include_directories(${name} PRIVATE
${CMAKE_CURRENT_LIST_DIR}
${CMAKE_CURRENT_LIST_DIR}/include
Expand Down
10 changes: 10 additions & 0 deletions examples/rpi_pico_w/include/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef CONFIG_H
#define CONFIG_H

#define WIFI_SSID ""
#define WIFI_PASSWORD ""
#define ZENOH_CONFIG_MODE "client"
#define ZENOH_CONFIG_CONNECT "serial/uart1_0#baudrate=38400"
#define ZENOH_CONFIG_LISTEN ""

#endif // CONFIG_H
10 changes: 10 additions & 0 deletions examples/rpi_pico_w/include/config.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef CONFIG_H
#define CONFIG_H

#define WIFI_SSID "@WIFI_SSID@"
#define WIFI_PASSWORD "@WIFI_PASSWORD@"
#define ZENOH_CONFIG_MODE "@ZENOH_CONFIG_MODE@"
#define ZENOH_CONFIG_CONNECT "@ZENOH_CONFIG_CONNECT@"
#define ZENOH_CONFIG_LISTEN "@ZENOH_CONFIG_LISTEN@"

#endif // CONFIG_H
23 changes: 16 additions & 7 deletions examples/rpi_pico_w/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
//

#include "FreeRTOS.h"
#include "config.h"
#include "pico/cyw43_arch.h"
#include "pico/stdlib.h"
#include "task.h"
Expand All @@ -35,19 +36,27 @@ void print_ip_address() {

void main_task(void *params) {
(void)params;

vTaskDelay(pdMS_TO_TICKS(1000));

if (cyw43_arch_init()) {
printf("Failed to initialise\n");
return;
}

cyw43_arch_enable_sta_mode();
printf("Connecting to Wi-Fi...\n");
if (cyw43_arch_wifi_connect_timeout_ms(WIFI_SSID, WIFI_PASSWORD, CYW43_AUTH_WPA2_AES_PSK, WIFI_TIMEOUT) == 0) {
printf("Wi-Fi connected.\n");
print_ip_address();
app_main();
if (strlen(WIFI_SSID) != 0) {
cyw43_arch_enable_sta_mode();
printf("Connecting to Wi-Fi...\n");
if (cyw43_arch_wifi_connect_timeout_ms(WIFI_SSID, WIFI_PASSWORD, CYW43_AUTH_WPA2_AES_PSK, WIFI_TIMEOUT) == 0) {
printf("Wi-Fi connected.\n");
print_ip_address();
app_main();
} else {
printf("Failed to connect Wi-Fi\n");
}
} else {
printf("Failed to connect Wi-Fi\n");
printf("Offline mode\n");
app_main();
}

printf("Terminate.\n");
Expand Down
7 changes: 4 additions & 3 deletions examples/rpi_pico_w/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
#include <stdio.h>
#include <zenoh-pico.h>

#include "config.h"

#if Z_FEATURE_QUERY == 1

#define IFACE "#iface=lo" // Not used by this platform, but should be present
#define KEYEXPR "demo/example/**"
#define VALUE ""

Expand Down Expand Up @@ -52,8 +53,8 @@ void app_main(void) {
zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, ZENOH_CONFIG_CONNECT);
}
if (strcmp(ZENOH_CONFIG_LISTEN, "") != 0) {
printf("Listen endpoint: %s\n", ZENOH_CONFIG_LISTEN IFACE);
zp_config_insert(z_loan_mut(config), Z_CONFIG_LISTEN_KEY, ZENOH_CONFIG_LISTEN IFACE);
printf("Listen endpoint: %s\n", ZENOH_CONFIG_LISTEN);
zp_config_insert(z_loan_mut(config), Z_CONFIG_LISTEN_KEY, ZENOH_CONFIG_LISTEN);
}

printf("Opening %s session ...\n", ZENOH_CONFIG_MODE);
Expand Down
7 changes: 4 additions & 3 deletions examples/rpi_pico_w/z_pub.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
#include <stdio.h>
#include <zenoh-pico.h>

#include "config.h"

#if Z_FEATURE_PUBLICATION == 1

#define IFACE "#iface=lo" // Not used by this platform, but should be present
#define KEYEXPR "demo/example/zenoh-pico-pub"
#define VALUE "[RPI] Pub from Zenoh-Pico!"

Expand All @@ -30,8 +31,8 @@ void app_main(void) {
zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, ZENOH_CONFIG_CONNECT);
}
if (strcmp(ZENOH_CONFIG_LISTEN, "") != 0) {
printf("Listen endpoint: %s\n", ZENOH_CONFIG_LISTEN IFACE);
zp_config_insert(z_loan_mut(config), Z_CONFIG_LISTEN_KEY, ZENOH_CONFIG_LISTEN IFACE);
printf("Listen endpoint: %s\n", ZENOH_CONFIG_LISTEN);
zp_config_insert(z_loan_mut(config), Z_CONFIG_LISTEN_KEY, ZENOH_CONFIG_LISTEN);
}

printf("Opening %s session ...\n", ZENOH_CONFIG_MODE);
Expand Down
7 changes: 4 additions & 3 deletions examples/rpi_pico_w/z_pub_st.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
#include <stdio.h>
#include <zenoh-pico.h>

#include "config.h"

#if Z_FEATURE_PUBLICATION == 1

#define IFACE "#iface=lo" // Not used by this platform, but should be present
#define KEYEXPR "demo/example/zenoh-pico-pub"
#define VALUE "[RPI] Pub from Zenoh-Pico!"

Expand All @@ -30,8 +31,8 @@ void app_main(void) {
zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, ZENOH_CONFIG_CONNECT);
}
if (strcmp(ZENOH_CONFIG_LISTEN, "") != 0) {
printf("Listen endpoint: %s\n", ZENOH_CONFIG_LISTEN IFACE);
zp_config_insert(z_loan_mut(config), Z_CONFIG_LISTEN_KEY, ZENOH_CONFIG_LISTEN IFACE);
printf("Listen endpoint: %s\n", ZENOH_CONFIG_LISTEN);
zp_config_insert(z_loan_mut(config), Z_CONFIG_LISTEN_KEY, ZENOH_CONFIG_LISTEN);
}

printf("Opening %s session ...\n", ZENOH_CONFIG_MODE);
Expand Down
7 changes: 4 additions & 3 deletions examples/rpi_pico_w/z_pub_thr.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
#include <stdio.h>
#include <zenoh-pico.h>

#include "config.h"

#if Z_FEATURE_PUBLICATION == 1

#define IFACE "#iface=lo" // Not used by this platform, but should be present
#define KEYEXPR "test/thr"
#define PAYLOAD_SIZE 8

Expand All @@ -39,8 +40,8 @@ void app_main(void) {
zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, ZENOH_CONFIG_CONNECT);
}
if (strcmp(ZENOH_CONFIG_LISTEN, "") != 0) {
printf("Listen endpoint: %s\n", ZENOH_CONFIG_LISTEN IFACE);
zp_config_insert(z_loan_mut(config), Z_CONFIG_LISTEN_KEY, ZENOH_CONFIG_LISTEN IFACE);
printf("Listen endpoint: %s\n", ZENOH_CONFIG_LISTEN);
zp_config_insert(z_loan_mut(config), Z_CONFIG_LISTEN_KEY, ZENOH_CONFIG_LISTEN);
}

printf("Opening %s session ...\n", ZENOH_CONFIG_MODE);
Expand Down
7 changes: 4 additions & 3 deletions examples/rpi_pico_w/z_pull.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
#include <stdio.h>
#include <zenoh-pico.h>

#include "config.h"

#if Z_FEATURE_SUBSCRIPTION == 1

#define IFACE "#iface=lo" // Not used by this platform, but should be present
#define KEYEXPR "demo/example/**"

const size_t INTERVAL = 5000;
Expand All @@ -32,8 +33,8 @@ void app_main(void) {
zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, ZENOH_CONFIG_CONNECT);
}
if (strcmp(ZENOH_CONFIG_LISTEN, "") != 0) {
printf("Listen endpoint: %s\n", ZENOH_CONFIG_LISTEN IFACE);
zp_config_insert(z_loan_mut(config), Z_CONFIG_LISTEN_KEY, ZENOH_CONFIG_LISTEN IFACE);
printf("Listen endpoint: %s\n", ZENOH_CONFIG_LISTEN);
zp_config_insert(z_loan_mut(config), Z_CONFIG_LISTEN_KEY, ZENOH_CONFIG_LISTEN);
}

printf("Opening %s session ...\n", ZENOH_CONFIG_MODE);
Expand Down
7 changes: 4 additions & 3 deletions examples/rpi_pico_w/z_put.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
#include <stdio.h>
#include <zenoh-pico.h>

#include "config.h"

#if Z_FEATURE_PUBLICATION == 1

#define IFACE "#iface=lo" // Not used by this platform, but should be present
#define KEYEXPR "demo/example/zenoh-pico-pub"
#define VALUE "[RPI] Pub from Zenoh-Pico!"

Expand All @@ -30,8 +31,8 @@ void app_main(void) {
zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, ZENOH_CONFIG_CONNECT);
}
if (strcmp(ZENOH_CONFIG_LISTEN, "") != 0) {
printf("Listen endpoint: %s\n", ZENOH_CONFIG_LISTEN IFACE);
zp_config_insert(z_loan_mut(config), Z_CONFIG_LISTEN_KEY, ZENOH_CONFIG_LISTEN IFACE);
printf("Listen endpoint: %s\n", ZENOH_CONFIG_LISTEN);
zp_config_insert(z_loan_mut(config), Z_CONFIG_LISTEN_KEY, ZENOH_CONFIG_LISTEN);
}

printf("Opening %s session ...\n", ZENOH_CONFIG_MODE);
Expand Down
7 changes: 4 additions & 3 deletions examples/rpi_pico_w/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
#include <stdio.h>
#include <zenoh-pico.h>

#include "config.h"

#if Z_FEATURE_QUERYABLE == 1

#define IFACE "#iface=lo" // Not used by this platform, but should be present
#define KEYEXPR "demo/example/zenoh-pico-queryable"
#define VALUE "[RPI] Queryable from Zenoh-Pico!"

Expand Down Expand Up @@ -56,8 +57,8 @@ void app_main(void) {
zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, ZENOH_CONFIG_CONNECT);
}
if (strcmp(ZENOH_CONFIG_LISTEN, "") != 0) {
printf("Listen endpoint: %s\n", ZENOH_CONFIG_LISTEN IFACE);
zp_config_insert(z_loan_mut(config), Z_CONFIG_LISTEN_KEY, ZENOH_CONFIG_LISTEN IFACE);
printf("Listen endpoint: %s\n", ZENOH_CONFIG_LISTEN);
zp_config_insert(z_loan_mut(config), Z_CONFIG_LISTEN_KEY, ZENOH_CONFIG_LISTEN);
}

printf("Opening %s session ...\n", ZENOH_CONFIG_MODE);
Expand Down
2 changes: 2 additions & 0 deletions examples/rpi_pico_w/z_scout.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include <unistd.h>
#include <zenoh-pico.h>

#include "config.h"

void fprintzid(FILE *stream, z_id_t zid) {
unsigned int zidlen = _z_id_len(zid);
if (zidlen == 0) {
Expand Down
7 changes: 4 additions & 3 deletions examples/rpi_pico_w/z_sub.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
#include <stdio.h>
#include <zenoh-pico.h>

#include "config.h"

#if Z_FEATURE_SUBSCRIPTION == 1

#define IFACE "#iface=lo" // Not used by this platform, but should be present
#define KEYEXPR "demo/example/**"

void data_handler(z_loaned_sample_t *sample, void *ctx) {
Expand All @@ -40,8 +41,8 @@ void app_main(void) {
zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, ZENOH_CONFIG_CONNECT);
}
if (strcmp(ZENOH_CONFIG_LISTEN, "") != 0) {
printf("Listen endpoint: %s\n", ZENOH_CONFIG_LISTEN IFACE);
zp_config_insert(z_loan_mut(config), Z_CONFIG_LISTEN_KEY, ZENOH_CONFIG_LISTEN IFACE);
printf("Listen endpoint: %s\n", ZENOH_CONFIG_LISTEN);
zp_config_insert(z_loan_mut(config), Z_CONFIG_LISTEN_KEY, ZENOH_CONFIG_LISTEN);
}

printf("Opening %s session ...\n", ZENOH_CONFIG_MODE);
Expand Down
7 changes: 4 additions & 3 deletions examples/rpi_pico_w/z_sub_st.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
#include <stdio.h>
#include <zenoh-pico.h>

#include "config.h"

#if Z_FEATURE_SUBSCRIPTION == 1

#define IFACE "#iface=lo" // Not used by this platform, but should be present
#define KEYEXPR "demo/example/zenoh-pico-pub"
#define VALUE "[RPI] Pub from Zenoh-Pico!"
int msg_nb = 0;
Expand All @@ -43,8 +44,8 @@ void app_main(void) {
zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, ZENOH_CONFIG_CONNECT);
}
if (strcmp(ZENOH_CONFIG_LISTEN, "") != 0) {
printf("Listen endpoint: %s\n", ZENOH_CONFIG_LISTEN IFACE);
zp_config_insert(z_loan_mut(config), Z_CONFIG_LISTEN_KEY, ZENOH_CONFIG_LISTEN IFACE);
printf("Listen endpoint: %s\n", ZENOH_CONFIG_LISTEN);
zp_config_insert(z_loan_mut(config), Z_CONFIG_LISTEN_KEY, ZENOH_CONFIG_LISTEN);
}

printf("Opening %s session ...\n", ZENOH_CONFIG_MODE);
Expand Down
7 changes: 4 additions & 3 deletions examples/rpi_pico_w/z_sub_thr.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
#include <unistd.h>
#include <zenoh-pico.h>

#include "config.h"

#if Z_FEATURE_SUBSCRIPTION == 1

#define IFACE "#iface=lo" // Not used by this platform, but should be present
#define KEYEXPR "test/thr"

#define PACKET_NB 1000000
Expand Down Expand Up @@ -78,8 +79,8 @@ void app_main(void) {
zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, ZENOH_CONFIG_CONNECT);
}
if (strcmp(ZENOH_CONFIG_LISTEN, "") != 0) {
printf("Listen endpoint: %s\n", ZENOH_CONFIG_LISTEN IFACE);
zp_config_insert(z_loan_mut(config), Z_CONFIG_LISTEN_KEY, ZENOH_CONFIG_LISTEN IFACE);
printf("Listen endpoint: %s\n", ZENOH_CONFIG_LISTEN);
zp_config_insert(z_loan_mut(config), Z_CONFIG_LISTEN_KEY, ZENOH_CONFIG_LISTEN);
}

printf("Opening %s session ...\n", ZENOH_CONFIG_MODE);
Expand Down
2 changes: 1 addition & 1 deletion include/zenoh-pico/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#define Z_FEATURE_LINK_TCP 1
#define Z_FEATURE_LINK_BLUETOOTH 0
#define Z_FEATURE_LINK_WS 0
#define Z_FEATURE_LINK_SERIAL 0
#define Z_FEATURE_LINK_SERIAL 1
#define Z_FEATURE_SCOUTING_UDP 1
#define Z_FEATURE_LINK_UDP_MULTICAST 1
#define Z_FEATURE_LINK_UDP_UNICAST 1
Expand Down
9 changes: 9 additions & 0 deletions include/zenoh-pico/system/platform/rpi_pico_w.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@

#include "FreeRTOS.h"
#include "event_groups.h"
#if Z_FEATURE_LINK_TCP == 1 || Z_FEATURE_LINK_UDP_MULTICAST == 1 || Z_FEATURE_LINK_UDP_UNICAST == 1
#include "lwip/ip4_addr.h"
#endif
#if Z_FEATURE_LINK_SERIAL == 1
#include "hardware/gpio.h"
#include "hardware/uart.h"
#endif
#include "semphr.h"

#ifdef __cplusplus
Expand Down Expand Up @@ -51,6 +57,9 @@ typedef struct {
union {
#if Z_FEATURE_LINK_TCP == 1 || Z_FEATURE_LINK_UDP_MULTICAST == 1 || Z_FEATURE_LINK_UDP_UNICAST == 1
int _fd;
#endif
#if Z_FEATURE_LINK_SERIAL == 1
uart_inst_t *_serial;
#endif
};
} _z_sys_net_socket_t;
Expand Down
Loading

0 comments on commit b04f08f

Please sign in to comment.