Skip to content

Commit 53556e5

Browse files
authored
New example for ntp_client with LwIP Socket API (#434)
Implemented a new example derived from the existing ntp_client example, but this time using the LwIP Socket API and FreeRTOS NO_SYS=0.
1 parent aef4834 commit 53556e5

File tree

6 files changed

+272
-1
lines changed

6 files changed

+272
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ App|Description
143143
[picow_freertos_iperf_server_sys](pico_w/wifi/freertos/iperf) | Runs an "iperf" server for WiFi speed testing under FreeRTOS in NO_SYS=0 (i.e. full FreeRTOS integration) mode. The LED is blinked in another task
144144
[picow_freertos_ping_nosys](pico_w/wifi/freertos/ping) | Runs the lwip-contrib/apps/ping test app under FreeRTOS in NO_SYS=1 mode.
145145
[picow_freertos_ping_sys](pico_w/wifi/freertos/ping) | Runs the lwip-contrib/apps/ping test app under FreeRTOS in NO_SYS=0 (i.e. full FreeRTOS integration) mode. The test app uses the lwIP _socket_ API in this case.
146+
[picow_freertos_ntp_client_socket](pico_w/wifi/freertos/ntp_client_socket) | Connects to an NTP server using the LwIP Socket API with FreeRTOS in NO_SYS=0 (i.e. full FreeRTOS integration) mode.
146147

147148
### Pico W Bluetooth
148149

pico_w/wifi/freertos/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ else()
55

66
add_subdirectory(iperf)
77
add_subdirectory(ping)
8-
endif()
8+
add_subdirectory(ntp_client_socket)
9+
endif()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
add_executable(picow_freertos_ntp_client_socket
2+
picow_freertos_ntp_client_socket.c
3+
)
4+
target_compile_definitions(picow_freertos_ntp_client_socket PRIVATE
5+
WIFI_SSID=\"${WIFI_SSID}\"
6+
WIFI_PASSWORD=\"${WIFI_PASSWORD}\"
7+
NO_SYS=0 # don't want NO_SYS (generally this would be in your lwipopts.h)
8+
LWIP_SOCKET=1 # we need the socket API (generally this would be in your lwipopts.h)
9+
)
10+
target_include_directories(picow_freertos_ntp_client_socket PRIVATE
11+
${CMAKE_CURRENT_LIST_DIR}
12+
${CMAKE_CURRENT_LIST_DIR}/.. # for our common FreeRTOSConfig
13+
${CMAKE_CURRENT_LIST_DIR}/../.. # for our common lwipopts
14+
)
15+
target_link_libraries(picow_freertos_ntp_client_socket
16+
pico_cyw43_arch_lwip_sys_freertos
17+
pico_stdlib
18+
FreeRTOS-Kernel-Heap4 # FreeRTOS kernel and dynamic heap
19+
)
20+
pico_add_extra_outputs(picow_freertos_ntp_client_socket)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* FreeRTOS V202111.00
3+
* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
* this software and associated documentation files (the "Software"), to deal in
7+
* the Software without restriction, including without limitation the rights to
8+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
* the Software, and to permit persons to whom the Software is furnished to do so,
10+
* subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in all
13+
* copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
*
22+
* http://www.FreeRTOS.org
23+
* http://aws.amazon.com/freertos
24+
*
25+
* 1 tab == 4 spaces!
26+
*/
27+
28+
#ifndef FREERTOS_CONFIG_H
29+
#define FREERTOS_CONFIG_H
30+
31+
// This example uses a common include to avoid repetition
32+
#include "FreeRTOSConfig_examples_common.h"
33+
34+
#endif /* FREERTOS_CONFIG_H */
35+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#ifndef _LWIPOPTS_H
2+
#define _LWIPOPTS_H
3+
4+
// Generally you would define your own explicit list of lwIP options
5+
// (see https://www.nongnu.org/lwip/2_1_x/group__lwip__opts.html)
6+
//
7+
// This example uses a common include to avoid repetition
8+
#include "lwipopts_examples_common.h"
9+
10+
#if !NO_SYS
11+
#define TCPIP_THREAD_STACKSIZE 1024
12+
#define DEFAULT_THREAD_STACKSIZE 1024
13+
#define DEFAULT_RAW_RECVMBOX_SIZE 8
14+
#define DEFAULT_UDP_RECVMBOX_SIZE 8
15+
#define DEFAULT_TCP_RECVMBOX_SIZE 8
16+
#define TCPIP_MBOX_SIZE 8
17+
#define LWIP_TIMEVAL_PRIVATE 0
18+
19+
// not necessary, can be done either way
20+
#define LWIP_TCPIP_CORE_LOCKING_INPUT 1
21+
22+
#define LWIP_SO_RCVTIMEO 1
23+
#endif
24+
25+
#endif
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
/**
2+
* Copyright (c) 2022 Raspberry Pi (Trading) Ltd.
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
#include "pico/cyw43_arch.h"
8+
#include "pico/stdlib.h"
9+
10+
#include "lwip/ip4_addr.h"
11+
#include "lwip/dns.h"
12+
#include "lwip/pbuf.h"
13+
#include "lwip/udp.h"
14+
#include "lwip/sockets.h"
15+
#include "lwip/netdb.h"
16+
17+
#include "FreeRTOS.h"
18+
#include "task.h"
19+
20+
#ifndef RUN_FREERTOS_ON_CORE
21+
#define RUN_FREERTOS_ON_CORE 0
22+
#endif
23+
24+
#define TEST_TASK_PRIORITY ( tskIDLE_PRIORITY + 1UL )
25+
26+
#define NTP_SERVER ("pool.ntp.org")
27+
#define NTP_MSG_LEN (48)
28+
#define NTP_PORT (123)
29+
// seconds between 1 Jan 1900 and 1 Jan 1970
30+
#define NTP_DELTA (2208988800)
31+
#define ntpEST_TIME (30 * 1000)
32+
#define NTP_FAIL_TIME (10)
33+
34+
void main_task(__unused void *params) {
35+
if (cyw43_arch_init()) {
36+
printf("failed to initialise\n");
37+
vTaskDelete(NULL);
38+
}
39+
cyw43_arch_enable_sta_mode();
40+
printf("Connecting to Wi-Fi...\n");
41+
if (cyw43_arch_wifi_connect_timeout_ms(WIFI_SSID, WIFI_PASSWORD, CYW43_AUTH_WPA2_AES_PSK, 30000)) {
42+
printf("failed to connect.\n");
43+
vTaskDelete(NULL);
44+
} else {
45+
printf("Connected.\n");
46+
}
47+
48+
while(true) {
49+
const struct addrinfo ntp_info = {
50+
.ai_flags = 0,
51+
.ai_family = AF_UNSPEC,
52+
.ai_socktype = SOCK_DGRAM,
53+
.ai_protocol = 0,
54+
};
55+
56+
struct addrinfo *dns_result = NULL;
57+
printf("Trying DNS...\n");
58+
int err = getaddrinfo(NTP_SERVER, "123", &ntp_info, &dns_result);
59+
if (err != 0)
60+
{
61+
printf("DNS lookup failed with error: %d\n", err);
62+
continue;
63+
}
64+
#ifndef INET6_ADDRSTRLEN
65+
char address[INET_ADDRSTRLEN] = {0};
66+
#else
67+
char address[INET6_ADDRSTRLEN] = {0};
68+
#endif
69+
ip_addr_t ip_addr;
70+
#if LWIP_IPV4
71+
if (dns_result->ai_addr->sa_family == AF_INET) {
72+
const struct sockaddr_in *sock_addr = (struct sockaddr_in*)dns_result->ai_addr;
73+
inet_addr_to_ip4addr(ip_2_ip4(&ip_addr), &sock_addr->sin_addr);
74+
IP_SET_TYPE(&ip_addr, IPADDR_TYPE_V4);
75+
}
76+
#endif
77+
#if LWIP_IPV6
78+
if (dns_result->ai_addr->sa_family == AF_INET6) {
79+
const struct sockaddr_in6 *sock_addr = (struct sockaddr_in6*)dns_result->ai_addr;
80+
inet6_addr_to_ip6addr(ip_2_ip6(&ip_addr), &sock_addr->sin6_addr);
81+
IP_SET_TYPE(&ip_addr, IPADDR_TYPE_V6);
82+
}
83+
#endif
84+
inet_ntop(dns_result->ai_family, &ip_addr, address, sizeof(address));
85+
printf("Got DNS response: %s\n", address);
86+
87+
int sock = socket(dns_result->ai_family, dns_result->ai_socktype, dns_result->ai_protocol);
88+
if (sock == -1)
89+
{
90+
printf("Failed to allocate socket");
91+
freeaddrinfo(dns_result);
92+
continue;
93+
}
94+
95+
struct timeval timeout = {
96+
.tv_sec = NTP_FAIL_TIME,
97+
};
98+
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
99+
err = connect(sock, dns_result->ai_addr, dns_result->ai_addrlen);
100+
freeaddrinfo(dns_result);
101+
if (err == -1)
102+
{
103+
printf("Failed to connect to NTP server");
104+
close(sock);
105+
continue;
106+
}
107+
108+
unsigned char request[NTP_MSG_LEN] = {0x1b, 0};
109+
int amount = send(sock, request, NTP_MSG_LEN, 0);
110+
if (amount != NTP_MSG_LEN)
111+
{
112+
printf("We were unable to send the complete NTP request");
113+
close(sock);
114+
continue;
115+
}
116+
117+
amount = recv(sock, request, NTP_MSG_LEN, 0);
118+
close(sock);
119+
if (amount != NTP_MSG_LEN)
120+
{
121+
printf("We did not receive a complete NTP response");
122+
continue;
123+
}
124+
125+
uint8_t mode = request[0] & 0x7;
126+
uint8_t stratum = request[1];
127+
128+
// Check the result
129+
if (amount == NTP_MSG_LEN &&
130+
mode == 0x4 &&
131+
stratum != 0)
132+
{
133+
uint8_t seconds_buf[4] = {};
134+
memcpy(seconds_buf, request + 40, 4);
135+
uint32_t seconds_since_1900 = seconds_buf[0] << 24 | seconds_buf[1] << 16 | seconds_buf[2] << 8 | seconds_buf[3];
136+
uint32_t seconds_since_1970 = seconds_since_1900 - NTP_DELTA;
137+
time_t epoch = seconds_since_1970;
138+
struct tm *utc = gmtime(&epoch);
139+
printf("got ntp response: %02d/%02d/%04d %02d:%02d:%02d\n", utc->tm_mday, utc->tm_mon + 1, utc->tm_year + 1900,
140+
utc->tm_hour, utc->tm_min, utc->tm_sec);
141+
}
142+
143+
// This assumes one tick is one ms, which is true for the default configuration
144+
vTaskDelay(ntpEST_TIME);
145+
}
146+
147+
cyw43_arch_deinit();
148+
}
149+
150+
void vLaunch( void) {
151+
TaskHandle_t task;
152+
xTaskCreate(main_task, "TestMainThread", 4096, NULL, TEST_TASK_PRIORITY, &task);
153+
154+
#if NO_SYS && configUSE_CORE_AFFINITY && configNUM_CORES > 1
155+
// we must bind the main task to one core (well at least while the init is called)
156+
// (note we only do this in NO_SYS mode, because cyw43_arch_freertos
157+
// takes care of it otherwise)
158+
vTaskCoreAffinitySet(task, 1);
159+
#endif
160+
161+
/* Start the tasks and timer running. */
162+
vTaskStartScheduler();
163+
}
164+
165+
int main( void )
166+
{
167+
stdio_init_all();
168+
169+
/* Configure the hardware ready to run the demo. */
170+
const char *rtos_name;
171+
#if ( portSUPPORT_SMP == 1 )
172+
rtos_name = "FreeRTOS SMP";
173+
#else
174+
rtos_name = "FreeRTOS";
175+
#endif
176+
177+
#if ( portSUPPORT_SMP == 1 ) && ( configNUM_CORES == 2 )
178+
printf("Starting %s on both cores:\n", rtos_name);
179+
vLaunch();
180+
#elif ( RUN_FREERTOS_ON_CORE == 1 )
181+
printf("Starting %s on core 1:\n", rtos_name);
182+
multicore_launch_core1(vLaunch);
183+
while (true);
184+
#else
185+
printf("Starting %s on core 0:\n", rtos_name);
186+
vLaunch();
187+
#endif
188+
return 0;
189+
}

0 commit comments

Comments
 (0)