From 26361be59c08917c47718bcd4114b8028aaf7810 Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Tue, 29 Oct 2024 21:03:04 -0600 Subject: libhw: Tidy --- libhw/CMakeLists.txt | 8 +++++++- libhw/alarmclock.c | 21 --------------------- libhw/common_alarmclock.c | 21 +++++++++++++++++++++ libhw/host_alarmclock.c | 2 +- libhw/host_net.c | 2 +- libhw/host_sigrt.c | 21 --------------------- libhw/host_sigrt.h | 47 ----------------------------------------------- libhw/host_util.c | 21 +++++++++++++++++++++ libhw/host_util.h | 47 +++++++++++++++++++++++++++++++++++++++++++++++ libhw/w5500.c | 10 ++++++---- 10 files changed, 104 insertions(+), 96 deletions(-) delete mode 100644 libhw/alarmclock.c create mode 100644 libhw/common_alarmclock.c delete mode 100644 libhw/host_sigrt.c delete mode 100644 libhw/host_sigrt.h create mode 100644 libhw/host_util.c create mode 100644 libhw/host_util.h (limited to 'libhw') diff --git a/libhw/CMakeLists.txt b/libhw/CMakeLists.txt index 1baeee8..3627641 100644 --- a/libhw/CMakeLists.txt +++ b/libhw/CMakeLists.txt @@ -6,9 +6,15 @@ add_library(libhw INTERFACE) target_include_directories(libhw SYSTEM INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/common_include) target_link_libraries(libhw INTERFACE + libcr + libcr_ipc libmisc ) +target_sources(libhw INTERFACE + common_alarmclock.c +) + if (PICO_PLATFORM STREQUAL "rp2040") target_include_directories(libhw SYSTEM INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/rp2040_include) target_sources(libhw INTERFACE @@ -27,7 +33,7 @@ endif() if (PICO_PLATFORM STREQUAL "host") target_include_directories(libhw SYSTEM INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/host_include) target_sources(libhw INTERFACE - host_sigrt.c + host_util.c host_alarmclock.c host_net.c ) diff --git a/libhw/alarmclock.c b/libhw/alarmclock.c deleted file mode 100644 index 7b311c0..0000000 --- a/libhw/alarmclock.c +++ /dev/null @@ -1,21 +0,0 @@ -/* libhw/alarmclock.c - Device-independent utilities - * - * Copyright (C) 2024 Luke T. Shumaker - * SPDX-License-Identifier: AGPL-3.0-or-later - */ - -#include -#include - -#include - -static void alarmclock_sleep_intrhandler(void *_arg) { - cid_t *cid = _arg; - cr_unpause_from_intrhandler(*cid); -} - -void alarmclock_sleep_until_ns(implements_alarmclock *clock, uint64_t abstime_ns) { - cid_t cid = cr_getcid(); - struct alarmclock_trigger trigger; - VCALL(clock, add_trigger, &trigger, abstime_ns, alarmclock_sleep_intrhandler, &cid); -} diff --git a/libhw/common_alarmclock.c b/libhw/common_alarmclock.c new file mode 100644 index 0000000..9812d44 --- /dev/null +++ b/libhw/common_alarmclock.c @@ -0,0 +1,21 @@ +/* libhw/common_alarmclock.c - Device-independent utilities + * + * Copyright (C) 2024 Luke T. Shumaker + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +#include +#include + +#include + +static void alarmclock_sleep_intrhandler(void *_arg) { + cid_t *cid = _arg; + cr_unpause_from_intrhandler(*cid); +} + +void alarmclock_sleep_until_ns(implements_alarmclock *clock, uint64_t abstime_ns) { + cid_t cid = cr_getcid(); + struct alarmclock_trigger trigger; + VCALL(clock, add_trigger, &trigger, abstime_ns, alarmclock_sleep_intrhandler, &cid); +} diff --git a/libhw/host_alarmclock.c b/libhw/host_alarmclock.c index 26aca94..141927c 100644 --- a/libhw/host_alarmclock.c +++ b/libhw/host_alarmclock.c @@ -16,7 +16,7 @@ #define IMPLEMENTATION_FOR_LIBHW_GENERIC_ALARMCLOCK_H YES #include -#include "host_sigrt.h" +#include "host_util.h" /* for host_sigrt_alloc(), ns_to_host_ns_time() */ /* Types **********************************************************************/ diff --git a/libhw/host_net.c b/libhw/host_net.c index 9d1ffcf..20b72f1 100644 --- a/libhw/host_net.c +++ b/libhw/host_net.c @@ -27,7 +27,7 @@ #define IMPLEMENTATION_FOR_LIBHW_HOST_NET_H YES #include -#include "host_sigrt.h" /* for host_sigrt_alloc(), ns_to_host_us_time() */ +#include "host_util.h" /* for host_sigrt_alloc(), ns_to_host_us_time() */ /* common *********************************************************************/ diff --git a/libhw/host_sigrt.c b/libhw/host_sigrt.c deleted file mode 100644 index 6dcb963..0000000 --- a/libhw/host_sigrt.c +++ /dev/null @@ -1,21 +0,0 @@ -/* libhw/host_sigrt.c - Manage glibc realtime signals - * - * Copyright (C) 2024 Luke T. Shumaker - * SPDX-License-Identifier: AGPL-3.0-or-later - */ - -#include /* for error(3gnu) */ -#include /* for SIGRTMIN, SIGRTMAX */ - -#include "host_sigrt.h" - -int host_sigrt_alloc(void) { - static int next = 0; - - if (!next) - next = SIGRTMIN; - int ret = next++; - if (ret > SIGRTMAX) - error(1, 0, "SIGRTMAX exceeded"); - return ret; -} diff --git a/libhw/host_sigrt.h b/libhw/host_sigrt.h deleted file mode 100644 index e8be26a..0000000 --- a/libhw/host_sigrt.h +++ /dev/null @@ -1,47 +0,0 @@ -/* libhw/host_sigrt.h - Manage glibc realtime signals - * - * Copyright (C) 2024 Luke T. Shumaker - * SPDX-License-Identifier: AGPL-3.0-or-later - */ - -#ifndef _LIBHW_HOST_SIGRT_H_ -#define _LIBHW_HOST_SIGRT_H_ - -#include /* for struct timespec */ -#include /* for struct timeval */ - -#include /* for {X}S_PER_S */ - -int host_sigrt_alloc(void); - -typedef struct timeval host_us_time_t; -typedef struct timespec host_ns_time_t; - -static inline host_us_time_t ns_to_host_us_time(uint64_t time_ns) { - host_us_time_t ret; - ret.tv_sec = time_ns - /NS_PER_S; - ret.tv_usec = (time_ns - ((uint64_t)ret.tv_sec)*NS_PER_S) - /(NS_PER_S/US_PER_S); - return ret; -} - -static inline host_ns_time_t ns_to_host_ns_time(uint64_t time_ns) { - host_ns_time_t ret; - ret.tv_sec = time_ns - /NS_PER_S; - ret.tv_nsec = time_ns - ((uint64_t)ret.tv_sec)*NS_PER_S; - return ret; -} - -static inline uint64_t ns_from_host_us_time(host_us_time_t host_time) { - return (((uint64_t)host_time.tv_sec) * NS_PER_S) + - ((uint64_t)host_time.tv_usec * (NS_PER_S/US_PER_S)); -} - -static inline uint64_t ns_from_host_ns_time(host_ns_time_t host_time) { - return (((uint64_t)host_time.tv_sec) * NS_PER_S) + - ((uint64_t)host_time.tv_nsec); -} - -#endif /* _LIBHW_HOST_SIGRT_H_ */ diff --git a/libhw/host_util.c b/libhw/host_util.c new file mode 100644 index 0000000..b862e39 --- /dev/null +++ b/libhw/host_util.c @@ -0,0 +1,21 @@ +/* libhw/host_util.c - Utilities for GNU/Linux hosts + * + * Copyright (C) 2024 Luke T. Shumaker + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +#include /* for error(3gnu) */ +#include /* for SIGRTMIN, SIGRTMAX */ + +#include "host_util.h" + +int host_sigrt_alloc(void) { + static int next = 0; + + if (!next) + next = SIGRTMIN; + int ret = next++; + if (ret > SIGRTMAX) + error(1, 0, "SIGRTMAX exceeded"); + return ret; +} diff --git a/libhw/host_util.h b/libhw/host_util.h new file mode 100644 index 0000000..edb6da4 --- /dev/null +++ b/libhw/host_util.h @@ -0,0 +1,47 @@ +/* libhw/host_util.h - Utilities for GNU/Linux hosts + * + * Copyright (C) 2024 Luke T. Shumaker + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +#ifndef _LIBHW_HOST_SIGRT_H_ +#define _LIBHW_HOST_SIGRT_H_ + +#include /* for struct timespec */ +#include /* for struct timeval */ + +#include /* for {X}S_PER_S */ + +int host_sigrt_alloc(void); + +typedef struct timeval host_us_time_t; +typedef struct timespec host_ns_time_t; + +static inline host_us_time_t ns_to_host_us_time(uint64_t time_ns) { + host_us_time_t ret; + ret.tv_sec = time_ns + /NS_PER_S; + ret.tv_usec = (time_ns - ((uint64_t)ret.tv_sec)*NS_PER_S) + /(NS_PER_S/US_PER_S); + return ret; +} + +static inline host_ns_time_t ns_to_host_ns_time(uint64_t time_ns) { + host_ns_time_t ret; + ret.tv_sec = time_ns + /NS_PER_S; + ret.tv_nsec = time_ns - ((uint64_t)ret.tv_sec)*NS_PER_S; + return ret; +} + +static inline uint64_t ns_from_host_us_time(host_us_time_t host_time) { + return (((uint64_t)host_time.tv_sec) * NS_PER_S) + + ((uint64_t)host_time.tv_usec * (NS_PER_S/US_PER_S)); +} + +static inline uint64_t ns_from_host_ns_time(host_ns_time_t host_time) { + return (((uint64_t)host_time.tv_sec) * NS_PER_S) + + ((uint64_t)host_time.tv_nsec); +} + +#endif /* _LIBHW_HOST_SIGRT_H_ */ diff --git a/libhw/w5500.c b/libhw/w5500.c index 0d9cd01..603863a 100644 --- a/libhw/w5500.c +++ b/libhw/w5500.c @@ -67,12 +67,15 @@ * SPDX-License-Identifier: MIT */ -#include /* for sleep_ms() */ +/* TODO: Write a to avoid w5500.c being + * pico-sdk-specific. */ #include /* pico-sdk:hardware_gpio */ #include /* for cr_yield() */ #include /* for VCALL_SELF() */ +#include /* for sleep_*() */ + #define IMPLEMENTATION_FOR_LIBHW_W5500_H YES #include @@ -265,11 +268,10 @@ static inline void w5500_post_reset(struct w5500 *chip) { } void w5500_hard_reset(struct w5500 *chip) { - /* TODO: Replace blocking sleep_ms() with something libcr-friendly. */ gpio_put(chip->pin_reset, 0); - sleep_ms(1); /* minimum of 500us */ + sleep_for_ms(1); /* minimum of 500us */ gpio_put(chip->pin_reset, 1); - sleep_ms(2); /* minimum of 1ms */ + sleep_for_ms(2); /* minimum of 1ms */ w5500_post_reset(chip); } -- cgit v1.2.3-2-g168b