Skip to content

Commit da300ff

Browse files
author
Martin Crossley
committed
Further documentation and comment tidy ups
1 parent d040091 commit da300ff

File tree

3 files changed

+25
-20
lines changed

3 files changed

+25
-20
lines changed

pico_w/wifi/ntp_system_time/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ The example uses:
5252

5353
1. the [SNTP application](https://www.nongnu.org/lwip/2_0_x/group__sntp.html) provided by the lwIP network stack
5454
2. the Pico SDK high level "always on timer" abstraction: [pico_aon_timer](https://www.raspberrypi.com/documentation/pico-sdk/high_level.html#group_pico_aon_timer)
55-
3. a [POSIX timezone](https://ftp.gnu.org/old-gnu/Manuals/glibc-2.2.3/html_node/libc_431.html) to convert UTC to local time
55+
3. an optional [POSIX timezone](https://ftp.gnu.org/old-gnu/Manuals/glibc-2.2.3/html_node/libc_431.html) to convert UTC to local time
5656

5757
### lwIP SNTP
5858

5959
The lwIP SNTP app provides a straightforward way to obtain and process timestamps from a pool of NTP servers without the complexity of a full NTP implementation. The lwIP documentation covers the [configuration options](https://www.nongnu.org/lwip/2_0_x/group__sntp.html) but the comments in the [source code](https://github.com/lwip-tcpip/lwip/blob/master/src/apps/sntp/sntp.c) are also very helpful.
6060

61-
SNTP uses the **macros** `SNTP_GET_SYSTEM_TIME(sec, us)` and `SNTP_SET_SYSTEM_TIME(sec, us)` to call user-provided functions for accessing the system clock. The example defines the macros in `lwipopts.h` and the callbacks themselves are near the top of `ntp_system_time.c`.
61+
SNTP uses the **macros** `SNTP_GET_SYSTEM_TIME(sec, us)` and `SNTP_SET_SYSTEM_TIME_US(sec, us)` to call user-provided functions for accessing the system clock. The example defines the macros in `lwipopts.h` and the callbacks themselves are near the top of `ntp_system_time.c`.
6262

6363
Note that the example runs lwIP/SNTP from `pico_cyw43_arch` in _threadsafe background mode_ as described in [SDK Networking](https://www.raspberrypi.com/documentation/pico-sdk/networking.html#group_pico_cyw43_arch).
6464
If you reconfigure it to use _polling mode_ then your user code should periodically call `cyw43_arch_poll()`.
@@ -87,6 +87,6 @@ which means
8787
Normal time ("GMT") is UTC +0. Daylight-saving time ("BST") runs from 1am on the last Sunday in March to 2am on the last Sunday in October.
8888
```
8989

90-
The format to define your own POSIX timezone is pretty straightforward and can be found [here](https://ftp.gnu.org/old-gnu/Manuals/glibc-2.2.3/html_node/libc_431.html).
90+
The format to define your own POSIX timezone is pretty straightforward and can be found [here](https://ftp.gnu.org/old-gnu/Manuals/glibc-2.2.3/html_node/libc_431.html); or you can simply choose a pre-defined one from an online resource such as [this](https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv).
9191

9292
_Note that it is entirely optional to create a local timezone: without one the Pico SDK time-conversion functions will simply use UTC._

pico_w/wifi/ntp_system_time/lwipopts.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@
3434
#define SNTP_RETRY_TIMEOUT_EXP 1
3535
#define SNTP_MONITOR_SERVER_REACHABILITY 1
3636

37-
// configure SNTP to use our callback to set the system time
37+
//* configure SNTP to use our callback functions for reading and setting the system time
38+
#define SNTP_GET_SYSTEM_TIME(sec, us) sntp_get_system_time_us(&(sec), &(us))
3839
#define SNTP_SET_SYSTEM_TIME_US(sec, us) sntp_set_system_time_us(sec, us)
3940

40-
// declare our callback functions (they are defined in ntp_system_time.c)
41+
//* declare our callback functions (the implementations are in ntp_system_time.c)
4142
#include "stdint.h"
4243
void sntp_set_system_time_us(uint32_t sec, uint32_t us);
4344
void sntp_get_system_time_us(uint32_t *sec_ptr, uint32_t *us_ptr);
4445

46+
4547
#endif /* __LWIPOPTS_H__ */

pico_w/wifi/ntp_system_time/ntp_system_time.c

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
auto_init_mutex(aon_timer_mutex);
1717
static bool aon_timer_is_initialised = false;
1818

19-
// callback for lwIP/SNTP to set the aon_timer to UTC (see lwipopts.h)
20-
// this is called every time the application receives a valid NTP server response
19+
// callback for lwIP/SNTP to set the aon_timer to UTC
20+
// we configure SNTP to call this function when it receives a valid NTP timestamp
21+
// (see lwipopts.h)
2122
void sntp_set_system_time_us(uint32_t sec, uint32_t us) {
2223
static struct timespec ntp_ts;
2324
ntp_ts.tv_sec = sec;
@@ -40,7 +41,8 @@ void sntp_set_system_time_us(uint32_t sec, uint32_t us) {
4041
}
4142

4243
// callback for lwIP/SNTP to read system time (UTC) from the aon_timer
43-
// when it needs to (eg) calculate the roundtrip transmission delay
44+
// we configure SNTP to call this function to read the current UTC system time,
45+
// eg to calculate the roundtrip transmission delay (see lwipopts.h)
4446
void sntp_get_system_time_us(uint32_t *sec_ptr, uint32_t * us_ptr) {
4547
static struct timespec sys_ts;
4648
// we don't need exclusive access because we are on the background thread
@@ -91,8 +93,9 @@ int main() {
9193
struct timespec ts;
9294
struct tm tm;
9395

94-
// OPTIONAL: set the 'TZ' env variable to the local POSIX timezone (in this case Europe/London,
95-
// to create your own see https://ftp.gnu.org/old-gnu/Manuals/glibc-2.2.3/html_node/libc_431.html)
96+
// OPTIONAL: set the 'TZ' env variable to the local POSIX timezone (in this case Europe/London)
97+
// For the format see: https://ftp.gnu.org/old-gnu/Manuals/glibc-2.2.3/html_node/libc_431.html
98+
// or just copy one from (eg): https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv
9699
setenv("TZ", "BST0GMT,M3.5.0/1,M10.5.0/2", 1);
97100

98101
// If the environment contains a valid 'TZ' definition then functions like ctime(), localtime()
@@ -108,15 +111,14 @@ int main() {
108111

109112
// if you just want a string representation of the current time and you're not interested
110113
// in the individual date/time fields, then here you can simply call:
111-
// printf("%s", ctime(&(ts.tv_sec)));
112-
// The string produced is the same as `asctime()` (see below). Note that if you set 'TZ'
113-
// then the output will be in local time, otherwise UTC.
114-
115-
// you can unpack the raw UTC seconds count into individual date/time fields like this.
116-
// Again, if you set 'TZ' then the values will be in local time, otherwise UTC. Note
117-
// that by default `pico_localtime_r()` just calls `localtime_r()` from the standard 'C'
118-
// library, but the declaration is 'weak' so that a user can override it with their own
119-
// implementation if desired.
114+
115+
// if you don't need the date/time fields, you can call `ctime()` or one of its variants
116+
// here to convert the raw timer value into a string like "Mon Oct 27 22:06:08 2025\n".
117+
// If you have defined a valid 'TZ' the string will be in local time, otherwise UTC.
118+
//printf("%s", ctime(&(ts.tv_sec)));
119+
120+
// you can extract the date/time fields use `localtime()` or one of its variants. If you
121+
// have defined a valid 'TZ' then the field values will be in local time, otherwise UTC.
120122
pico_localtime_r(&(ts.tv_sec), &tm);
121123

122124
// display the name of the currently active local timeszone, if defined
@@ -127,7 +129,8 @@ int main() {
127129
printf("UTC: ");
128130
}
129131

130-
// display individual date/time fields in the form "Mon Oct 27 22:06:08 2025\n"
132+
// you can use `asctime()` and its variants to convert the date/time fields into a string
133+
// like: "Mon Oct 27 22:06:08 2025\n". If you need more flexibility consider `strftime()`
131134
printf("%s", asctime(&tm));
132135

133136
} else {

0 commit comments

Comments
 (0)