samples: posix: gettimeofday: Demo gettimeofday() returning correct time
Achieved using CONFIG_NET_CONFIG_CLOCK_SNTP_INIT option, i.e. querying time over network using SNTP. Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
This commit is contained in:
parent
106a0f7306
commit
4e116873f5
7 changed files with 117 additions and 0 deletions
|
@ -22,6 +22,7 @@ Samples and Demos
|
|||
display/*
|
||||
shields/*
|
||||
portability/*
|
||||
posix/*
|
||||
gui/*
|
||||
video/*
|
||||
|
||||
|
|
9
samples/posix/gettimeofday/CMakeLists.txt
Normal file
9
samples/posix/gettimeofday/CMakeLists.txt
Normal file
|
@ -0,0 +1,9 @@
|
|||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
cmake_minimum_required(VERSION 3.13.1)
|
||||
|
||||
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
|
||||
project(gettimeofday)
|
||||
|
||||
#target_include_directories(app PRIVATE $ENV{ZEPHYR_BASE}/include/posix)
|
||||
target_sources(app PRIVATE src/main.c)
|
4
samples/posix/gettimeofday/Makefile.posix
Normal file
4
samples/posix/gettimeofday/Makefile.posix
Normal file
|
@ -0,0 +1,4 @@
|
|||
# This makefile builds the sample for a POSIX system, like Linux
|
||||
|
||||
gettimeofday: src/main.c
|
||||
$(CC) $^ -o $@
|
33
samples/posix/gettimeofday/README.rst
Normal file
33
samples/posix/gettimeofday/README.rst
Normal file
|
@ -0,0 +1,33 @@
|
|||
.. _posix-gettimeofday-sample:
|
||||
|
||||
POSIX gettimeofday() with clock initialization over SNTP
|
||||
########################################################
|
||||
|
||||
Overview
|
||||
********
|
||||
|
||||
This sample application demonstrates using the POSIX gettimeofday()
|
||||
function to display the absolute wall clock time every second. At
|
||||
system startup, the current time is queried using the SNTP networking
|
||||
protocol, enabled by setting the :option:`CONFIG_NET_CONFIG_CLOCK_SNTP_INIT`
|
||||
and :option:`CONFIG_NET_CONFIG_SNTP_INIT_SERVER` options.
|
||||
|
||||
Building and Running
|
||||
********************
|
||||
|
||||
This project outputs to the console. It can be built and executed
|
||||
on QEMU as follows:
|
||||
|
||||
.. zephyr-app-commands::
|
||||
:zephyr-app: samples/posix/gettimeofday
|
||||
:host-os: unix
|
||||
:board: qemu_x86
|
||||
:goals: run
|
||||
:compact:
|
||||
|
||||
For comparison, a version for native POSIX operating systems (e.g. Linux)
|
||||
can be built using:
|
||||
|
||||
.. code-block:: console
|
||||
|
||||
make -f Makefile.posix
|
30
samples/posix/gettimeofday/prj.conf
Normal file
30
samples/posix/gettimeofday/prj.conf
Normal file
|
@ -0,0 +1,30 @@
|
|||
# General config
|
||||
CONFIG_NEWLIB_LIBC=y
|
||||
CONFIG_POSIX_API=y
|
||||
CONFIG_SNTP=y
|
||||
CONFIG_NET_CONFIG_CLOCK_SNTP_INIT=y
|
||||
CONFIG_NET_CONFIG_SNTP_INIT_SERVER="time.nist.gov"
|
||||
|
||||
# Networking config
|
||||
CONFIG_NETWORKING=y
|
||||
CONFIG_NET_IPV4=y
|
||||
CONFIG_NET_IPV6=y
|
||||
CONFIG_NET_TCP=y
|
||||
CONFIG_NET_SOCKETS=y
|
||||
|
||||
CONFIG_DNS_RESOLVER=y
|
||||
CONFIG_DNS_SERVER_IP_ADDRESSES=y
|
||||
CONFIG_DNS_SERVER1="192.0.2.2"
|
||||
|
||||
# Network driver config
|
||||
CONFIG_TEST_RANDOM_GENERATOR=y
|
||||
|
||||
# Network address config
|
||||
CONFIG_NET_CONFIG_SETTINGS=y
|
||||
CONFIG_NET_CONFIG_NEED_IPV4=y
|
||||
CONFIG_NET_CONFIG_MY_IPV4_ADDR="192.0.2.1"
|
||||
CONFIG_NET_CONFIG_PEER_IPV4_ADDR="192.0.2.2"
|
||||
CONFIG_NET_CONFIG_MY_IPV4_GW="192.0.2.2"
|
||||
|
||||
# Network debug config
|
||||
CONFIG_NET_LOG=y
|
30
samples/posix/gettimeofday/src/main.c
Normal file
30
samples/posix/gettimeofday/src/main.c
Normal file
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
* Copyright (c) 2019 Linaro Limited
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <sys/time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
struct timeval tv;
|
||||
|
||||
while (1) {
|
||||
int res = gettimeofday(&tv, NULL);
|
||||
|
||||
if (res < 0) {
|
||||
printf("Error in gettimeofday(): %d\n", errno);
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("gettimeofday(): HI(tv_sec)=%d, LO(tv_sec)=%d, "
|
||||
"tv_usec=%d\n", (unsigned int)(tv.tv_sec >> 32),
|
||||
(unsigned int)tv.tv_sec, (unsigned int)tv.tv_usec);
|
||||
sleep(1);
|
||||
}
|
||||
}
|
10
samples/posix/posix.rst
Normal file
10
samples/posix/posix.rst
Normal file
|
@ -0,0 +1,10 @@
|
|||
.. _posix-samples:
|
||||
|
||||
POSIX Subsystem Samples
|
||||
#######################
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
:glob:
|
||||
|
||||
**/*
|
Loading…
Add table
Add a link
Reference in a new issue