diff --git a/samples/index.rst b/samples/index.rst index f4cd4d20967..7492cf8b6b1 100644 --- a/samples/index.rst +++ b/samples/index.rst @@ -22,6 +22,7 @@ Samples and Demos display/* shields/* portability/* + posix/* gui/* video/* diff --git a/samples/posix/gettimeofday/CMakeLists.txt b/samples/posix/gettimeofday/CMakeLists.txt new file mode 100644 index 00000000000..751956e39fc --- /dev/null +++ b/samples/posix/gettimeofday/CMakeLists.txt @@ -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) diff --git a/samples/posix/gettimeofday/Makefile.posix b/samples/posix/gettimeofday/Makefile.posix new file mode 100644 index 00000000000..eb0529e1864 --- /dev/null +++ b/samples/posix/gettimeofday/Makefile.posix @@ -0,0 +1,4 @@ +# This makefile builds the sample for a POSIX system, like Linux + +gettimeofday: src/main.c + $(CC) $^ -o $@ diff --git a/samples/posix/gettimeofday/README.rst b/samples/posix/gettimeofday/README.rst new file mode 100644 index 00000000000..efa59c5059f --- /dev/null +++ b/samples/posix/gettimeofday/README.rst @@ -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 diff --git a/samples/posix/gettimeofday/prj.conf b/samples/posix/gettimeofday/prj.conf new file mode 100644 index 00000000000..aaa54d32869 --- /dev/null +++ b/samples/posix/gettimeofday/prj.conf @@ -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 diff --git a/samples/posix/gettimeofday/src/main.c b/samples/posix/gettimeofday/src/main.c new file mode 100644 index 00000000000..3b3655cb73b --- /dev/null +++ b/samples/posix/gettimeofday/src/main.c @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2019 Linaro Limited + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include +#include + +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); + } +} diff --git a/samples/posix/posix.rst b/samples/posix/posix.rst new file mode 100644 index 00000000000..c7b2e1d83a3 --- /dev/null +++ b/samples/posix/posix.rst @@ -0,0 +1,10 @@ +.. _posix-samples: + +POSIX Subsystem Samples +####################### + +.. toctree:: + :maxdepth: 1 + :glob: + + **/*