diff --git a/sys/include/ztimer/periph_ptp.h b/sys/include/ztimer/periph_ptp.h new file mode 100644 index 0000000000..b968f1f8a4 --- /dev/null +++ b/sys/include/ztimer/periph_ptp.h @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2018 Kaspar Schleiser + 2021 Otto-von-Guericke-Universität Magdeburg + * + * This file is subject to the terms and conditions of the GNU Lesser General + * Public License v2.1. See the file LICENSE in the top level directory for more + * details. + */ + +/** + * @defgroup sys_ztimer_periph_ptp ztimer periph/ptp backend + * @ingroup sys_ztimer + * @brief ztimer periph/ptp backend + * + * This ztimer module implements a ztimer virtual clock on top of periph/ptp. + * + * @{ + * + * @file + * @brief ztimer periph/ptp backend API + * + * @author Jana Eisoldt + */ + +#ifndef ZTIMER_PERIPH_PTP_H +#define ZTIMER_PERIPH_PTP_H + +#include "ztimer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief ztimer_periph_ptp structure definition + * + * The periph/ptp backend has no private fields, thus this is just a typedef + * to ztimer_clock_t. + */ +typedef ztimer_clock_t ztimer_periph_ptp_t; + +/** + * @brief ztimer periph/ptp backend initialization function + * + * @param[in, out] clock ztimer_periph_ptp object to initialize + */ +void ztimer_periph_ptp_init(ztimer_periph_ptp_t *clock); + +#ifdef __cplusplus +} +#endif + +#endif /* ZTIMER_PERIPH_PTP_H */ +/** @} */ diff --git a/sys/ztimer/Kconfig b/sys/ztimer/Kconfig index 39d1e81467..4d7f045246 100644 --- a/sys/ztimer/Kconfig +++ b/sys/ztimer/Kconfig @@ -28,6 +28,11 @@ config MODULE_ZTIMER_PERIPH_RTT select MODULE_PERIPH_RTT default y if !MODULE_ZTIMER_PERIPH_TIMER +config MODULE_ZTIMER_PERIPH_PTP + bool "PTP peripheral" + depends on HAS_PERIPH_PTP_TIMER + select MODULE_PERIPH_PTP_TIMER + config MODULE_ZTIMER_PERIPH_TIMER bool "Timer peripheral" depends on HAS_PERIPH_TIMER diff --git a/sys/ztimer/Makefile.dep b/sys/ztimer/Makefile.dep index c72b3c3828..b8dfdebd19 100644 --- a/sys/ztimer/Makefile.dep +++ b/sys/ztimer/Makefile.dep @@ -70,6 +70,10 @@ ifneq (,$(filter ztimer_periph_rtt,$(USEMODULE))) FEATURES_REQUIRED += periph_rtt endif +ifneq (,$(filter ztimer_periph_ptp,$(USEMODULE))) + FEATURES_REQUIRED += periph_ptp_timer +endif + ifneq (,$(filter ztimer_convert_frac,$(USEMODULE))) USEMODULE += frac endif diff --git a/sys/ztimer/periph_ptp.c b/sys/ztimer/periph_ptp.c new file mode 100644 index 0000000000..d1d324e3f1 --- /dev/null +++ b/sys/ztimer/periph_ptp.c @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2018 Kaspar Schleiser + 2021 Otto-von-Guericke-Universität Magdeburg + * + * This file is subject to the terms and conditions of the GNU Lesser General + * Public License v2.1. See the file LICENSE in the top level directory for more + * details. + */ + +/** + * @ingroup sys_ztimer_periph_ptp + * @{ + * + * @file + * @brief ztimer periph/ptp implementation + * + * @author Jana Eisoldt + * + * @} + */ +#include "assert.h" + +#include "irq.h" +#include "periph/ptp.h" +#include "ztimer/periph_ptp.h" + +#define ENABLE_DEBUG 0 +#include "debug.h" + +static ztimer_clock_t *clock_timer; + +void ptp_timer_cb(void) +{ + ztimer_handler(clock_timer); +} + +static void _ztimer_periph_ptp_set(ztimer_clock_t *clock, uint32_t val) +{ + (void)clock; + ptp_timer_set_u64(val); +} + +static uint32_t _ztimer_periph_ptp_now(ztimer_clock_t *clock) +{ + (void)clock; + return (uint32_t)ptp_clock_read_u64(); +} + +static void _ztimer_periph_ptp_cancel(ztimer_clock_t *clock) +{ + (void)clock; + ptp_timer_clear(); +} + +static const ztimer_ops_t _ztimer_periph_ptp_ops = { + .set = _ztimer_periph_ptp_set, + .now = _ztimer_periph_ptp_now, + .cancel = _ztimer_periph_ptp_cancel, +}; + +void ztimer_periph_ptp_init(ztimer_periph_ptp_t *clock) +{ + clock->ops = &_ztimer_periph_ptp_ops; + clock->max_value = UINT32_MAX; + clock_timer = clock; + + ztimer_init_extend(clock); +}