diff --git a/gettimeofday/CMakeLists.txt b/gettimeofday/CMakeLists.txt new file mode 100644 index 0000000..93fbe00 --- /dev/null +++ b/gettimeofday/CMakeLists.txt @@ -0,0 +1,47 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(ldc_gettimeofday) + +target_sources(app PRIVATE src/main.c) + +if(${ARCH} STREQUAL "posix" OR ${ARCH} STREQUAL "x86") + set(ldc_target i686-unknown-none-newlibeabi) +elseif(CONFIG_CPU_CORTEX_M) + if(CONFIG_CPU_CORTEX_M3) + set(ldc_target thumbv7em-unknown-none-newlibeabi) + else() + message(FATAL_ERROR "Unknown Cortex-M target") + endif() +else() + message(FATAL_ERROR "ARCH ${ARCH} not supported") +endif() + +include(ExternalProject) +set(ldc_src_dir ${CMAKE_CURRENT_SOURCE_DIR}) +ExternalProject_Add( + ldc_project + PREFIX ${CMAKE_CURRENT_BINARY_DIR} + SOURCE_DIR ${ldc_src_dir} + BUILD_IN_SOURCE 1 + BUILD_ALWAYS 1 + CONFIGURE_COMMAND "" + BUILD_COMMAND + dub + build + -f + -b debug + --compiler=ldc2 + --arch=${ldc_target} + INSTALL_COMMAND "" + BUILD_BYPRODUCTS ${ldc_src_dir}/lib/libgettimeofday_d.a +) + +add_library(ldc_lib STATIC IMPORTED GLOBAL) +add_dependencies( + ldc_lib + ldc_project +) +set_target_properties(ldc_lib PROPERTIES IMPORTED_LOCATION ${ldc_src_dir}/lib/libgettimeofday_d.a) +target_link_libraries(app PUBLIC ldc_lib) diff --git a/gettimeofday/README.md b/gettimeofday/README.md new file mode 100644 index 0000000..d37cf48 --- /dev/null +++ b/gettimeofday/README.md @@ -0,0 +1,14 @@ +# gettimeofday example + +```console +west build -b qemu_cortex_m3 gettimeofday +west build -t run +``` + +See [networking with qemu section](https://docs.zephyrproject.org/latest/connectivity/networking/qemu_setup.html#networking-with-qemu) too. + +creating helper socket. + +```console +./loop-socat.sh +``` diff --git a/gettimeofday/dub.sdl b/gettimeofday/dub.sdl new file mode 100644 index 0000000..a3631b8 --- /dev/null +++ b/gettimeofday/dub.sdl @@ -0,0 +1,7 @@ +name "gettimeofday" +authors "Hiroki Noda" +license "BSL-1.0" +targetType "staticLibrary" +targetPath "lib" +targetName "gettimeofday_d" +dependency "zephyr-core" path="../zephyr-core" diff --git a/gettimeofday/ldc2.conf b/gettimeofday/ldc2.conf new file mode 100644 index 0000000..b3b5e6a --- /dev/null +++ b/gettimeofday/ldc2.conf @@ -0,0 +1,13 @@ +default: +{ + switches = [ + "--betterC", + "--float-abi=soft", + "--relocation-model=static", + "--defaultlib=" + ]; + post-switches = [ + "-I%%ldcbinarypath%%/../import", + ]; + lib-dirs = []; +} diff --git a/gettimeofday/prj.conf b/gettimeofday/prj.conf new file mode 100644 index 0000000..5da1e3b --- /dev/null +++ b/gettimeofday/prj.conf @@ -0,0 +1,33 @@ +# General config +CONFIG_MAIN_STACK_SIZE=2048 +CONFIG_NEWLIB_LIBC=y +CONFIG_POSIX_API=y +CONFIG_LINKER_ORPHAN_SECTION_PLACE=y +CONFIG_SNTP=y +CONFIG_NET_CONFIG_CLOCK_SNTP_INIT=y +CONFIG_NET_CONFIG_SNTP_INIT_SERVER="time.nist.gov" +CONFIG_NET_CONFIG_SNTP_INIT_RESYNC=y + +# 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/gettimeofday/src/app.d b/gettimeofday/src/app.d new file mode 100644 index 0000000..eb92450 --- /dev/null +++ b/gettimeofday/src/app.d @@ -0,0 +1,35 @@ +import zephyr.core.stdc.stdio; +import zephyr.core.stdc.errno; +import zephyr.core.sys.posix.time; +import zephyr.core.sys.posix.sys.time; +import zephyr.core.sys.posix.sys.types; +import zephyr.core.sys.posix.unistd; + +extern(C): +@nogc: +nothrow: + +int d_main() + { + timeval tv = void; + + while (true) + { + int res = gettimeofday(&tv, null); + time_t now = time(null); + tm tm = void; + localtime_r(&now, &tm); + + 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\t%s\n", cast(uint)(tv.tv_sec >> 32), + cast(uint)tv.tv_sec, cast(uint)tv.tv_usec, + asctime(&tm)); + sleep(1); + } +} diff --git a/gettimeofday/src/main.c b/gettimeofday/src/main.c new file mode 100644 index 0000000..b7e29e8 --- /dev/null +++ b/gettimeofday/src/main.c @@ -0,0 +1,5 @@ +extern int d_main(); + +int main() { + return d_main(); +}