From 0cdc5826fdd78130b333b90bd5ae9c6435a05651 Mon Sep 17 00:00:00 2001 From: Francisco Molina Date: Mon, 5 Jul 2021 14:30:19 +0200 Subject: [PATCH] pkg/mynewt-core: initial commit --- pkg/mynewt-core/Makefile | 46 ++++ pkg/mynewt-core/Makefile.dep | 17 ++ pkg/mynewt-core/Makefile.include | 16 ++ pkg/mynewt-core/contrib/Makefile | 12 + pkg/mynewt-core/contrib/callout.c | 58 +++++ pkg/mynewt-core/contrib/core.c | 34 +++ pkg/mynewt-core/contrib/cputime.c | 77 ++++++ pkg/mynewt-core/contrib/mutex.c | 57 +++++ pkg/mynewt-core/contrib/nrf5x_isr.c | 56 +++++ pkg/mynewt-core/contrib/sem.c | 41 +++ pkg/mynewt-core/contrib/task.c | 68 +++++ pkg/mynewt-core/doc.txt | 12 + pkg/mynewt-core/include/hal/hal_gpio.h | 197 +++++++++++++++ pkg/mynewt-core/include/hal/hal_spi.h | 169 +++++++++++++ pkg/mynewt-core/include/log/log.h | 48 ++++ pkg/mynewt-core/include/mcu/mcu.h | 61 +++++ pkg/mynewt-core/include/os/mynewt.h | 36 +++ pkg/mynewt-core/include/os/os.h | 146 +++++++++++ pkg/mynewt-core/include/os/os_dev.h | 44 ++++ pkg/mynewt-core/include/os/os_eventq.h | 236 ++++++++++++++++++ pkg/mynewt-core/include/os/os_time.h | 116 +++++++++ pkg/mynewt-core/include/os/os_types.h | 59 +++++ pkg/mynewt-core/include/syscfg/syscfg.h | 122 +++++++++ pkg/mynewt-core/include/sysinit/sysinit.h | 43 ++++ pkg/mynewt-core/mynewt-core_nrf5x_hal.mk | 5 + pkg/mynewt-core/mynewt-core_os.mk | 15 ++ pkg/mynewt-core/mynewt-core_util.mk | 5 + ...hw-mcu-nrf5x-adapt-NVIC-init-to-RIOT.patch | Bin 0 -> 2272 bytes .../0002-kernel-os-src-riot-patches.patch | Bin 0 -> 7985 bytes .../patches/0003-hw-hal-patch-hal_timer.patch | Bin 0 -> 1390 bytes sys/auto_init/auto_init.c | 5 + 31 files changed, 1801 insertions(+) create mode 100644 pkg/mynewt-core/Makefile create mode 100644 pkg/mynewt-core/Makefile.dep create mode 100644 pkg/mynewt-core/Makefile.include create mode 100644 pkg/mynewt-core/contrib/Makefile create mode 100644 pkg/mynewt-core/contrib/callout.c create mode 100644 pkg/mynewt-core/contrib/core.c create mode 100644 pkg/mynewt-core/contrib/cputime.c create mode 100644 pkg/mynewt-core/contrib/mutex.c create mode 100644 pkg/mynewt-core/contrib/nrf5x_isr.c create mode 100644 pkg/mynewt-core/contrib/sem.c create mode 100644 pkg/mynewt-core/contrib/task.c create mode 100644 pkg/mynewt-core/doc.txt create mode 100644 pkg/mynewt-core/include/hal/hal_gpio.h create mode 100644 pkg/mynewt-core/include/hal/hal_spi.h create mode 100644 pkg/mynewt-core/include/log/log.h create mode 100644 pkg/mynewt-core/include/mcu/mcu.h create mode 100644 pkg/mynewt-core/include/os/mynewt.h create mode 100644 pkg/mynewt-core/include/os/os.h create mode 100644 pkg/mynewt-core/include/os/os_dev.h create mode 100644 pkg/mynewt-core/include/os/os_eventq.h create mode 100644 pkg/mynewt-core/include/os/os_time.h create mode 100644 pkg/mynewt-core/include/os/os_types.h create mode 100644 pkg/mynewt-core/include/syscfg/syscfg.h create mode 100644 pkg/mynewt-core/include/sysinit/sysinit.h create mode 100644 pkg/mynewt-core/mynewt-core_nrf5x_hal.mk create mode 100644 pkg/mynewt-core/mynewt-core_os.mk create mode 100644 pkg/mynewt-core/mynewt-core_util.mk create mode 100644 pkg/mynewt-core/patches/0001-hw-mcu-nrf5x-adapt-NVIC-init-to-RIOT.patch create mode 100644 pkg/mynewt-core/patches/0002-kernel-os-src-riot-patches.patch create mode 100644 pkg/mynewt-core/patches/0003-hw-hal-patch-hal_timer.patch diff --git a/pkg/mynewt-core/Makefile b/pkg/mynewt-core/Makefile new file mode 100644 index 0000000000..f45bcb2425 --- /dev/null +++ b/pkg/mynewt-core/Makefile @@ -0,0 +1,46 @@ +PKG_NAME=mynewt-core +PKG_URL=https://github.com/apache/mynewt-core.git +PKG_VERSION=de203365f207dda658657a7525253e02f68503a1 +PKG_LICENSE=Apache-2.0 + +include $(RIOTBASE)/pkg/pkg.mk + +CFLAGS += -Wno-unused-parameter +CFLAGS += -Wno-unused-but-set-variable +CFLAGS += -Wno-sign-compare + +MYNEWT_CORE_MODULES := mynewt-core_os \ + mynewt-core_util \ + mynewt-core_nrf5x_hal \ + # + +MYNEWT_CORE_PATH_util = util/mem/src +MYNEWT_CORE_PATH_os = kernel/os/src + +ifneq (,$(filter nrf52,$(CPU))) + MYNEWT_CORE_PATH_nrf5x_hal = hw/mcu/nordic/nrf52xxx/src/ +endif +ifneq (,$(filter nrf51,$(CPU))) + MYNEWT_CORE_PATH_nrf5x_hal = hw/mcu/nordic/nrf51xxx/src/ +endif + +.PHONY: rm_riot_provided_headers + +all: $(filter $(MYNEWT_CORE_MODULES),$(USEMODULE)) + @true + +mynewt-core_%: rm_riot_provided_headers + "$(MAKE)" -C $(PKG_SOURCE_DIR)/$(MYNEWT_CORE_PATH_$*) -f $(RIOTPKG)/$(PKG_NAME)/$@.mk MODULE=$@ + +# The following mynewt-core headers are provided by RIOT, remove them from +# mynewt-core include paths to avoid header conflicts +MYNEWT_CORE_HAL_HEADERS = hal_gpio.h hal_spi.h +MYNEWT_CORE_OS_HEADERS = os.h mynewt.h os_dev.h os_eventq.h os_time.h +MYNEWT_CORE_HAL_HEADERS_PATH = hw/hal/include/hal +MYNEWT_CORE_OS_HEADERS_PATH = kernel/os/include/os/os_time + +rm_riot_provided_headers: + $(Q)for i in $(MYNEWT_CORE_OS_HEADERS); \ + do rm -f "$(PKG_SOURCE_DIR)/$(MYNEWT_CORE_OS_HEADERS_PATH)/$$i"; done + $(Q)for i in $(MYNEWT_CORE_HAL_HEADERS); \ + do rm -f "$(PKG_SOURCE_DIR)/$(MYNEWT_CORE_HAL_HEADERS_PATH)/$$i"; done diff --git a/pkg/mynewt-core/Makefile.dep b/pkg/mynewt-core/Makefile.dep new file mode 100644 index 0000000000..31480b59b3 --- /dev/null +++ b/pkg/mynewt-core/Makefile.dep @@ -0,0 +1,17 @@ +USEMODULE += event_callback +USEMODULE += sema +USEMODULE += ztimer +USEMODULE += ztimer_msec + +USEMODULE += mynewt-core + +DEFAULT_MODULE += auto_init_mynewt-core + +# MyNewt `os_hw_is_in_critical` function needs to know whether ISR are masked +# of if the function is being called in ISR context. There is no such function +# in RIOT except for arm (__get_PRIMASK), therefore unless a similar function +# is provided for other arch, this port is currently only enabled for those arch. +# Note: that this should not be a hindrance since nimble only works on nordic +# and uwb-core breakouts are all arm. +FEATURES_REQUIRED += arch_arm +FEATURES_BLACKLIST += arch_arm7 diff --git a/pkg/mynewt-core/Makefile.include b/pkg/mynewt-core/Makefile.include new file mode 100644 index 0000000000..3defd3ce4c --- /dev/null +++ b/pkg/mynewt-core/Makefile.include @@ -0,0 +1,16 @@ +INCLUDES += -I$(RIOTPKG)/mynewt-core/include \ + -I$(PKGDIRBASE)/mynewt-core/kernel/os/include \ + -I$(PKGDIRBASE)/mynewt-core/hw/hal/include \ + -I$(PKGDIRBASE)/mynewt-core/util/mem/include \ + -I$(PKGDIRBASE)/mynewt-core/sys/stats/stub/include \ + # + +DIRS += $(RIOTPKG)/mynewt-core/contrib \ + # + +ifneq (,$(filter nrf5%,$(CPU))) + # OS_CPUTIME is set to 32.767 Hz + CFLAGS += -DMYNEWT_VAL_OS_CPUTIME_FREQ=32768 +else + CFLAGS += -DMYNEWT_VAL_OS_CPUTIME_FREQ=CONFIG_ZTIMER_MSEC_BASE_FREQ +endif diff --git a/pkg/mynewt-core/contrib/Makefile b/pkg/mynewt-core/contrib/Makefile new file mode 100644 index 0000000000..0bb65a903f --- /dev/null +++ b/pkg/mynewt-core/contrib/Makefile @@ -0,0 +1,12 @@ +MODULE = mynewt-core + +# exclude submodule sources from *.c wildcard source selection +SRC := $(filter-out nrf5x_isr.c cputime.c,$(wildcard *.c)) + +ifneq (,$(filter nrf%,$(CPU))) + SRC += nrf5x_isr.c +else + SRC += cputime.c +endif + +include $(RIOTBASE)/Makefile.base diff --git a/pkg/mynewt-core/contrib/callout.c b/pkg/mynewt-core/contrib/callout.c new file mode 100644 index 0000000000..c6d9414edb --- /dev/null +++ b/pkg/mynewt-core/contrib/callout.c @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2020 Inria + * + * 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 pkg_mynewt_core + * @{ + * + * @file + * @brief mynewt-core callout + * + * @author Francisco Molina + * @} + */ + +#include + +#include "ztimer.h" +#include "os/os.h" +#include "os/os_callout.h" + +static void _os_callout_timer_cb(void* arg) +{ + struct os_callout *c = (struct os_callout *) arg; + assert(c); + + /* post the event if there is a queue, otherwise call the callback + here */ + if (c->c_evq) { + os_eventq_put(c->c_evq, &c->c_ev); + } else { + c->c_ev.e.callback(&c->c_ev); + } +} + +void os_callout_init(struct os_callout *c, struct os_eventq *q, + os_event_fn *e_cb, void *e_arg) +{ + os_event_init(&c->c_ev, e_cb, e_arg); + c->c_evq = q; + c->timer.callback = _os_callout_timer_cb; + c->timer.arg = (void*) c; +} + +int os_callout_reset(struct os_callout *c, os_time_t ticks) +{ + ztimer_set(ZTIMER_MSEC, &c->timer, ticks); + return OS_OK; +} + +void os_callout_stop(struct os_callout *c) +{ + ztimer_remove(ZTIMER_MSEC, &(c->timer)); +} diff --git a/pkg/mynewt-core/contrib/core.c b/pkg/mynewt-core/contrib/core.c new file mode 100644 index 0000000000..9dc9e11ada --- /dev/null +++ b/pkg/mynewt-core/contrib/core.c @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2021 Inria + * + * 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 pkg_mynewt_core + * @{ + * + * @file + * @brief mynewt-core bootstrapping functions + * + * @author Francisco Molina + * @} + */ + +#include + +#include "os/os_cputime.h" +#include "hal/hal_timer.h" + +void mynewt_core_init(void) +{ +#if (MYNEWT_VAL_OS_CPUTIME_TIMER_NUM >= 0) && (defined(CPU_NRF51) || defined(CPU_NRF52)) + int rc = hal_timer_init(5, NULL); + assert(rc == 0); + rc = os_cputime_init(MYNEWT_VAL_OS_CPUTIME_FREQ); + assert(rc == 0); + (void) rc; +#endif +} diff --git a/pkg/mynewt-core/contrib/cputime.c b/pkg/mynewt-core/contrib/cputime.c new file mode 100644 index 0000000000..52382936ba --- /dev/null +++ b/pkg/mynewt-core/contrib/cputime.c @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2020 Inria + * + * 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 pkg_mynewt_core + * @{ + * + * @file + * @brief cputime implementation for non nrf5x BOARDs + * + * @author Francisco Molina + * @} + */ + +#include "os/os_cputime.h" +#include "ztimer.h" +#include "hal/hal_timer.h" + +uint32_t os_cputime_get32(void) +{ + return ztimer_now(ZTIMER_MSEC_BASE); +} + +void os_cputime_delay_ticks(uint32_t ticks) +{ + ztimer_sleep(ZTIMER_MSEC_BASE, ticks); +} + +void os_cputime_delay_usecs(uint32_t usecs) +{ + ztimer_sleep(ZTIMER_MSEC_BASE, os_cputime_usecs_to_ticks(usecs)); +} + +int os_cputime_init(uint32_t clock_freq) +{ + (void)clock_freq; + return 0; +} + +void os_cputime_timer_init(struct hal_timer *timer, hal_timer_cb fp, + void *arg) +{ + timer->timer.callback = fp; + timer->timer.arg = arg; +} + +int os_cputime_timer_start(struct hal_timer *timer, uint32_t cputime) +{ + uint32_t now = ztimer_now(ZTIMER_MSEC_BASE); + + /* taken from mynewt-core 'hal_timer' implementations, this will + only work with timeouts at most 2**32-1 away, so it will have the same + limitations as their implementation does */ + if ((int32_t)(cputime - now) <= 0) { + ztimer_set(ZTIMER_MSEC_BASE, &timer->timer, 0); + } + else { + ztimer_set(ZTIMER_MSEC_BASE, &timer->timer, cputime - now); + } + return 0; +} + +int os_cputime_timer_relative(struct hal_timer *timer, uint32_t usecs) +{ + ztimer_set(ZTIMER_MSEC_BASE, &timer->timer, os_cputime_usecs_to_ticks(usecs)); + return 0; +} + +void os_cputime_timer_stop(struct hal_timer *timer) +{ + ztimer_remove(ZTIMER_MSEC_BASE, &timer->timer); +} diff --git a/pkg/mynewt-core/contrib/mutex.c b/pkg/mynewt-core/contrib/mutex.c new file mode 100644 index 0000000000..a8f8b684d1 --- /dev/null +++ b/pkg/mynewt-core/contrib/mutex.c @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2020 Inria + * + * 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 pkg_mynewt_core + * @{ + * + * @file + * @brief Decawave Porting Layer mutex RIOT wrapper + * + * @author Francisco Molina + * @} + */ + +#include "os/os_mutex.h" + +os_error_t os_mutex_init(struct os_mutex *mu) +{ + if (!mu) { + return OS_INVALID_PARM; + } + mutex_init(&mu->mutex); + return OS_OK; +} + +os_error_t os_mutex_release(struct os_mutex *mu) +{ + if (!mu) { + return OS_INVALID_PARM; + } + + mutex_unlock(&mu->mutex); + return OS_OK; +} + +os_error_t os_mutex_pend(struct os_mutex *mu, uint32_t timeout) +{ + int rc = OS_OK; + + if (!mu) { + return OS_INVALID_PARM; + } + + if (!timeout) { + rc = mutex_trylock(&mu->mutex); + } + else { + /* TODO: no timeout equivalent */ + mutex_lock(&mu->mutex); + } + return rc; +} diff --git a/pkg/mynewt-core/contrib/nrf5x_isr.c b/pkg/mynewt-core/contrib/nrf5x_isr.c new file mode 100644 index 0000000000..23260f19a5 --- /dev/null +++ b/pkg/mynewt-core/contrib/nrf5x_isr.c @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2021 Freie Universität Berlin + * + * 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 pkg_mynewt_core + * @{ + * + * @file + * @brief mynewt-core isr mapping + * + * @author Hauke Petersen + * @} + */ + + +#include "mcu/mcu.h" +#include "cpu.h" + +static void (*radio_isr_addr)(void); +static void (*rng_isr_addr)(void); +static void (*rtc0_isr_addr)(void); + +void isr_radio(void) +{ + radio_isr_addr(); +} + +void isr_rng(void) +{ + rng_isr_addr(); +} + +void isr_rtc0(void) +{ + rtc0_isr_addr(); +} + +void nrf5x_hw_set_isr(int irqn, void (*addr)(void)) +{ + switch (irqn) { + case RADIO_IRQn: + radio_isr_addr = addr; + break; + case RNG_IRQn: + rng_isr_addr = addr; + break; + case RTC0_IRQn: + rtc0_isr_addr = addr; + break; + } +} diff --git a/pkg/mynewt-core/contrib/sem.c b/pkg/mynewt-core/contrib/sem.c new file mode 100644 index 0000000000..c15fc2d019 --- /dev/null +++ b/pkg/mynewt-core/contrib/sem.c @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2020 Inria + * + * 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 pkg_mynewt_core + * @{ + * + * @file + * @brief Decawave Porting Layer semaphore RIOT wrapper + * + * @author Francisco Molina + * @} + */ + +#include + +#include "irq.h" +#include "os/os_sem.h" + +os_error_t os_sem_init(struct os_sem *sem, uint16_t tokens) +{ + sema_create(&sem->sema, tokens); + return OS_OK; +} + +os_error_t os_sem_release(struct os_sem *sem) +{ + int ret = sema_post(&sem->sema); + return (ret) ? OS_ERROR : OS_OK; +} + +os_error_t os_sem_pend(struct os_sem *sem, os_time_t timeout) +{ + int ret = sema_wait_timed_ztimer(&sem->sema, ZTIMER_MSEC, timeout); + return (ret) ? OS_ERROR : OS_OK; +} diff --git a/pkg/mynewt-core/contrib/task.c b/pkg/mynewt-core/contrib/task.c new file mode 100644 index 0000000000..f4d7bad184 --- /dev/null +++ b/pkg/mynewt-core/contrib/task.c @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2020 Inria + * + * 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 pkg_mynewt_core + * @{ + * + * @file + * @brief Decawave Porting Layer tasks RIOT wrapper + * + * @author Francisco Molina + * @} + */ + +#include "os/os_error.h" +#include "os/os_task.h" +#include "thread.h" + +#ifndef LOG_LEVEL +#define LOG_LEVEL LOG_INFO +#endif +#include "log.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int os_task_init(struct os_task *t, const char *name, os_task_func_t func, + void *arg, uint8_t prio, os_time_t sanity_itvl, + os_stack_t *stack_bottom, uint16_t stack_size) +{ + (void) sanity_itvl; + + LOG_INFO("[mynewt-core]: starting thread %s\n", name); + + kernel_pid_t pid = thread_create(stack_bottom, (int) stack_size, + prio, THREAD_CREATE_STACKTEST, + func, arg, name); + + t->pid = pid; + + return (pid) ? OS_ERROR : OS_OK;; +} + +int os_task_remove(struct os_task *t) +{ + thread_zombify(); + return thread_kill_zombie(t->pid); +} + +uint8_t os_task_count(void) +{ + return sched_num_threads; +} + +void os_task_yield(void) +{ + thread_yield(); +} + +#ifdef __cplusplus +} +#endif diff --git a/pkg/mynewt-core/doc.txt b/pkg/mynewt-core/doc.txt new file mode 100644 index 0000000000..f7c63bfd7b --- /dev/null +++ b/pkg/mynewt-core/doc.txt @@ -0,0 +1,12 @@ +/** + * @defgroup pkg_mynewt_core mynewt-core + * @ingroup pkg + * @brief Apache MyNewt package for MyNewt based packages: uwb-core, nimble + * @see https://github.com/apache/mynewt-core + */ + +# Apache MyNewt mynewt-core Package + +Packages like @ref pkg_uwb_core and @ref nimble where developed with MyNewt +as their default OS. Some of the features provided by that OS are not abstracted. +For those cases and to avoid header re-definitions mynewt-core is pulled in. diff --git a/pkg/mynewt-core/include/hal/hal_gpio.h b/pkg/mynewt-core/include/hal/hal_gpio.h new file mode 100644 index 0000000000..b09ba8beea --- /dev/null +++ b/pkg/mynewt-core/include/hal/hal_gpio.h @@ -0,0 +1,197 @@ +/* + * Copyright (C) 2020 Inria + * + * 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 pkg_uwb_dw1000 + * @{ + * + * @file + * @brief GPIO abstraction layer RIOT adaption + * + * @author Francisco Molina + * @} + */ + +#ifndef HAL_HAL_GPIO_H +#define HAL_HAL_GPIO_H + +#include "periph/gpio.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Map hal_gpio_pull_t enum types to gpio_mode_t enum types + */ +enum { + /** Pull-up/down not enabled */ + HAL_GPIO_PULL_NONE = GPIO_IN, + /** Pull-up enabled */ + HAL_GPIO_PULL_UP = GPIO_IN_PU, + /** Pull-down enabled */ + HAL_GPIO_PULL_DOWN = GPIO_IN_PD +}; +/** + * @brief hal_gpio_pull type + */ +typedef gpio_mode_t hal_gpio_pull_t; + +/** + * @brief Map hal_gpio_irq_trig_t enum types to gpio_flank_t enum types + */ +enum { +#ifdef GPIO_NONE + HAL_GPIO_TRIG_NONE = GPIO_NONE, +#endif + /** IRQ occurs on rising edge */ + HAL_GPIO_TRIG_RISING = GPIO_RISING, + /** IRQ occurs on falling edge */ + HAL_GPIO_TRIG_FALLING = GPIO_FALLING, + /** IRQ occurs on either edge */ + HAL_GPIO_TRIG_BOTH = GPIO_BOTH, + /** IRQ occurs when line is low */ +#ifdef GPIO_LOW + HAL_GPIO_TRIG_LOW = GPIO_LOW, +#endif + /** IRQ occurs when line is high */ +#ifdef GPIO_HIGH + HAL_GPIO_TRIG_HIGH = GPIO_HIGH +#endif +}; +/** + * @brief hal_gpio_irq_trig type + */ +typedef gpio_flank_t hal_gpio_irq_trig_t; + +/** + * @brief Function proto for GPIO irq handler functions + */ +typedef gpio_cb_t hal_gpio_irq_handler_t; + +/** + * Initializes the specified pin as an input + * + * @param pin Pin number to set as input + * @param pull pull type + * + * @return int 0: no error; -1 otherwise. + */ +static inline int hal_gpio_init_in(gpio_t pin, hal_gpio_pull_t pull) +{ + return gpio_init(pin, pull); +} + +/** + * Initialize the specified pin as an output, setting the pin to the specified + * value. + * + * @param pin Pin number to set as output + * @param val Value to set pin + * + * @return int 0: no error; -1 otherwise. + */ +static inline int hal_gpio_init_out(gpio_t pin, int val) +{ + int res = gpio_init(pin, GPIO_OUT); + gpio_write(pin, val); + return res; +} + +/** + * Write a value (either high or low) to the specified pin. + * + * @param pin Pin to set + * @param val Value to set pin (0:low 1:high) + */ +static inline void hal_gpio_write(gpio_t pin, int val) +{ + gpio_write(pin, val); +} + +/** + * Reads the specified pin. + * + * @param pin Pin number to read + * + * @return int 0: low, 1: high + */ +static inline int hal_gpio_read(gpio_t pin) +{ + return gpio_read(pin); +} + +/** + * Toggles the specified pin + * + * @param pin Pin number to toggle + * + * @return current gpio state int 0: low, 1: high + */ +static inline int hal_gpio_toggle(gpio_t pin) +{ + gpio_toggle(pin); + return gpio_read(pin); +} + +/** + * Initialize a given pin to trigger a GPIO IRQ callback. + * + * @param pin The pin to trigger GPIO interrupt on + * @param handler The handler function to call + * @param arg The argument to provide to the IRQ handler + * @param trig The trigger mode (e.g. rising, falling) + * @param pull The mode of the pin (e.g. pullup, pulldown) + * + * @return 0 on success, non-zero error code on failure. + */ +static inline int hal_gpio_irq_init(gpio_t pin, + hal_gpio_irq_handler_t handler, + void *arg, + hal_gpio_irq_trig_t trig, + hal_gpio_pull_t pull) +{ + return gpio_init_int(pin, pull, trig, handler, arg); +} + +/** + * Release a pin from being configured to trigger IRQ on state change. + * + * @param pin The pin to release + */ +static inline void hal_gpio_irq_release(gpio_t pin) +{ + /* can't release the interrupt so ar least disable it */ + gpio_irq_disable(pin); +} + +/** + * Enable IRQs on the passed pin + * + * @param pin The pin to enable IRQs on + */ +static inline void hal_gpio_irq_enable(gpio_t pin) +{ + gpio_irq_enable(pin); +} + +/** + * Disable IRQs on the passed pin + * + * @param pin The pin to disable IRQs on + */ +static inline void hal_gpio_irq_disable(gpio_t pin) +{ + gpio_irq_disable(pin); +} + +#ifdef __cplusplus +} +#endif + +#endif /* HAL_HAL_GPIO_H */ diff --git a/pkg/mynewt-core/include/hal/hal_spi.h b/pkg/mynewt-core/include/hal/hal_spi.h new file mode 100644 index 0000000000..da13747bb1 --- /dev/null +++ b/pkg/mynewt-core/include/hal/hal_spi.h @@ -0,0 +1,169 @@ +/* + * Copyright (C) 2020 Inria + * + * 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 pkg_uwb_dw1000 + * @{ + * + * @file + * @brief SPI abstraction layer RIOT adaption + * + * @author Francisco Molina + * @} + */ + +#ifndef HAL_HAL_SPI_H +#define HAL_HAL_SPI_H + +#include "periph/spi.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** SPI mode 0 */ +#define HAL_SPI_MODE0 (SPI_MODE_0) +/** SPI mode 1 */ +#define HAL_SPI_MODE1 (SPI_MODE_1) +/** SPI mode 2 */ +#define HAL_SPI_MODE2 (SPI_MODE_2) +/** SPI mode 3 */ +#define HAL_SPI_MODE3 (SPI_MODE_3) + +/** + * @brief Prototype for tx/rx callback + */ +typedef void (*hal_spi_txrx_cb)(void *arg, int len); + +/** + * @brief since one spi device can control multiple devices, some configuration + * can be changed on the fly from the hal + */ +struct hal_spi_settings { + /** Data mode of SPI driver, defined by HAL_SPI_MODEn */ + spi_mode_t data_mode; + /** Baudrate in kHz */ + spi_clk_t baudrate; +}; + +/** + * @brief Configure the spi. Must be called after the spi is initialized (after + * hal_spi_init is called) and when the spi is disabled (user must call + * hal_spi_disable if the spi has been enabled through hal_spi_enable prior + * to calling this function). Can also be used to reconfigure an initialized + * SPI (assuming it is disabled as described previously). + * + * @param spi_num The number of the SPI to configure. + * @param psettings The settings to configure this SPI with + * + * @return int 0 on success, non-zero error code on failure. + */ +int hal_spi_config(int spi_num, struct hal_spi_settings *psettings); + +/** + * @brief Sets the txrx callback (executed at interrupt context) when the + * buffer is transferred by the master or the slave using the non-blocking API. + * Cannot be called when the spi is enabled. This callback will also be called + * when chip select is de-asserted on the slave. + * + * NOTE: This callback is only used for the non-blocking interface and must + * be called prior to using the non-blocking API. + * + * @param spi_num SPI interface on which to set callback + * @param txrx_cb Callback function + * @param arg Argument to be passed to callback function + * + * @return int 0 on success, non-zero error code on failure. + */ +int hal_spi_set_txrx_cb(int spi_num, hal_spi_txrx_cb txrx_cb, void *arg); + +/** + * @brief Enables the SPI. This does not start a transmit or receive operation; + * it is used for power mgmt. Cannot be called when a SPI transfer is in + * progress. + * + * @param spi_num + * + * @return int 0 on success, non-zero error code on failure. + */ +int hal_spi_enable(int spi_num); + +/** + * @brief Disables the SPI. Used for power mgmt. It will halt any current SPI transfers + * in progress. + * + * @param spi_num + * + * @return int 0 on success, non-zero error code on failure. + */ +int hal_spi_disable(int spi_num); + +/** + * @brief Blocking interface to send a buffer and store the received values from the + * slave. The transmit and receive buffers are either arrays of 8-bit (uint8_t) + * values or 16-bit values depending on whether the spi is configured for 8 bit + * data or more than 8 bits per value. The 'cnt' parameter is the number of + * 8-bit or 16-bit values. Thus, if 'cnt' is 10, txbuf/rxbuf would point to an + * array of size 10 (in bytes) if the SPI is using 8-bit data; otherwise + * txbuf/rxbuf would point to an array of size 20 bytes (ten, uint16_t values). + * + * NOTE: these buffers are in the native endian-ness of the platform. + * + * MASTER: master sends all the values in the buffer and stores the + * stores the values in the receive buffer if rxbuf is not NULL. + * The txbuf parameter cannot be NULL. + * SLAVE: cannot be called for a slave; returns -1 + * + * @param spi_num SPI interface to use + * @param txbuf Pointer to buffer where values to transmit are stored. + * @param rxbuf Pointer to buffer to store values received from peer. + * @param cnt Number of 8-bit or 16-bit values to be transferred. + * + * @return int 0 on success, non-zero error code on failure. + */ +int hal_spi_txrx(int spi_num, void *txbuf, void *rxbuf, int cnt); + +/** + * @brief Non-blocking interface to send a buffer and store received values. Can be + * used for both master and slave SPI types. The user must configure the + * callback (using hal_spi_set_txrx_cb); the txrx callback is executed at + * interrupt context when the buffer is sent. + * + * The transmit and receive buffers are either arrays of 8-bit (uint8_t) + * values or 16-bit values depending on whether the spi is configured for 8 bit + * data or more than 8 bits per value. The 'cnt' parameter is the number of + * 8-bit or 16-bit values. Thus, if 'cnt' is 10, txbuf/rxbuf would point to an + * array of size 10 (in bytes) if the SPI is using 8-bit data; otherwise + * txbuf/rxbuf would point to an array of size 20 bytes (ten, uint16_t values). + * + * NOTE: these buffers are in the native endian-ness of the platform. + * + * MASTER: master sends all the values in the buffer and stores the + * stores the values in the receive buffer if rxbuf is not NULL. + * The txbuf parameter cannot be NULL + * SLAVE: Slave "preloads" the data to be sent to the master (values + * stored in txbuf) and places received data from master in rxbuf + * (if not NULL). The txrx callback occurs when len values are + * transferred or master de-asserts chip select. If txbuf is NULL, + * the slave transfers its default byte. Both rxbuf and txbuf cannot + * be NULL. + * + * @param spi_num SPI interface to use + * @param txbuf Pointer to buffer where values to transmit are stored. + * @param rxbuf Pointer to buffer to store values received from peer. + * @param cnt Number of 8-bit or 16-bit values to be transferred. + * + * @return int 0 on success, non-zero error code on failure. + */ +int hal_spi_txrx_noblock(int spi_num, void *txbuf, void *rxbuf, int cnt); + +#ifdef __cplusplus +} +#endif + +#endif /* HAL_HAL_SPI_H */ diff --git a/pkg/mynewt-core/include/log/log.h b/pkg/mynewt-core/include/log/log.h new file mode 100644 index 0000000000..7ee5fe776e --- /dev/null +++ b/pkg/mynewt-core/include/log/log.h @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2020 Inria + * + * 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 pkg_mynewt_core + * @{ + * + * @file + * @brief System logging header for mynewt-core + * + * @author Francisco Molina + * @} + */ + +#ifndef LOG_LOG_H +#define LOG_LOG_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include "log.h" + +/** + * @name Logging convenience defines wrappers + * @{ + */ +#define LOG_WARN(...) LOG(LOG_WARNING, __VA_ARGS__) +#define LOG_CRITICAL(...) LOG(LOG_ERROR, __VA_ARGS__) +#define log_register(__X, __Y, __Z, __A, __B) {} +/** @} */ + +/** + * @brief Empty log structure + */ +struct log { +}; + +#ifdef __cplusplus +} +#endif + +#endif /* LOG_LOG_H */ diff --git a/pkg/mynewt-core/include/mcu/mcu.h b/pkg/mynewt-core/include/mcu/mcu.h new file mode 100644 index 0000000000..2c3185a5c1 --- /dev/null +++ b/pkg/mynewt-core/include/mcu/mcu.h @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2020 Inria + * + * 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 pkg_mynewt_core + * @{ + * + * @file + * @brief Abstraction layer for RIOT adaption + * + * @author Francisco Molina + * @} + */ + +#ifndef MCU_MCU_H +#define MCU_MCU_H + +#include "cpu.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Set nrf5x radio ISR callback + * + * @param[in] irqn IRQ number + * @param[in] addr the ISR callback + */ +void nrf5x_hw_set_isr(int irqn, void (*addr)(void)); + +/** + * @name Entering and exiting critical section defines + * @{ + */ +#define __HAL_DISABLE_INTERRUPTS(x) \ + do { \ + x = irq_disable(); \ + } while (0); + +#define __HAL_ENABLE_INTERRUPTS(x) \ + do { \ + if (x) { \ + irq_restore(x); \ + } \ + else { \ + irq_enable(); \ + } \ + } while (0); +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* MCU_MCU_H */ diff --git a/pkg/mynewt-core/include/os/mynewt.h b/pkg/mynewt-core/include/os/mynewt.h new file mode 100644 index 0000000000..97bf9c986d --- /dev/null +++ b/pkg/mynewt-core/include/os/mynewt.h @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2020 Inria + * + * 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 pkg_mynewt_core + * @{ + * + * @file + * @brief mynewt-core header + * + * @author Francisco Molina + * @} + */ + +#ifndef OS_MYNEWT_H +#define OS_MYNEWT_H + +#include +#include "syscfg/syscfg.h" +#include "sysinit/sysinit.h" +#include "os/os.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* OS_MYNEWT_H */ diff --git a/pkg/mynewt-core/include/os/os.h b/pkg/mynewt-core/include/os/os.h new file mode 100644 index 0000000000..e39c1e81ac --- /dev/null +++ b/pkg/mynewt-core/include/os/os.h @@ -0,0 +1,146 @@ +/** + * Apache Mynewt + * Copyright 2015-2021 The Apache Software Foundation + * + * This product includes software developed at + * The Apache Software Foundation (http://www.apache.org/). + * + * Portions of this software were developed at + * Runtime Inc, copyright 2015. + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * @ingroup pkg_mynewt_core + * @{ + * + * @file + * @brief mynewt-core error types + * + * @} + */ + +#ifndef OS_OS_H +#define OS_OS_H + +#include +#include + +#include "irq.h" +#include "os/os_types.h" +#include "os/os_error.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @name MyNewt os/%.h required macros + * @{ + * + * PLEASE NOTE: Following macro definitions where copied directly from + * apache/mynewt-core and are under the copyright specified in + * the header. + * + */ +#ifndef min +#define min(a, b) ((a)<(b)?(a):(b)) +#endif + +#ifndef max +#define max(a, b) ((a)>(b)?(a):(b)) +#endif + +#define OS_ALIGN(__n, __a) ( \ + (((__n) & ((__a) - 1)) == 0) ? \ + (__n) : \ + ((__n) + ((__a) - ((__n) & ((__a) - 1)))) \ + ) +#define OS_ALIGNMENT (4) +/** @} */ + +/** + * @brief CPU status register + */ +typedef uint32_t os_sr_t; + +/** + * @name Entering and exiting critical section defines + * @{ + */ +#define OS_ENTER_CRITICAL(_sr) (_sr = os_hw_enter_critical()) +#define OS_EXIT_CRITICAL(_sr) (os_hw_exit_critical(_sr)) +#define OS_ASSERT_CRITICAL() assert(os_hw_is_in_critical()) +/** @} */ + +/** + * @brief Disable ISRs + * + * @return current isr context + */ +static inline uint32_t os_hw_enter_critical(void) +{ + uint32_t ctx = irq_disable(); + return ctx; +} + +/** + * @brief Restores ISR context + * + * @param[in] ctx ISR context to restore. + */ +static inline void os_hw_exit_critical(uint32_t ctx) +{ + irq_restore((unsigned)ctx); +} + +/** + * @brief Check if is in critical section + * + * @return true, if in critical section, false otherwise + */ +static inline bool os_hw_is_in_critical(void) +{ + return (irq_is_in() || __get_PRIMASK()); +} + +/* Mynewt components (not abstracted in NPL or DPL) */ +#include "os/endian.h" +#include "os/os_callout.h" +#include "os/os_cputime.h" +#include "os/os_dev.h" +#include "os/os_eventq.h" +#include "os/os_mbuf.h" +#include "os/os_mempool.h" +#include "os/os_mutex.h" +#include "os/os_sem.h" +#include "os/os_task.h" +#include "os/os_time.h" +#include "os/os_trace_api.h" +#include "os/queue.h" + +#if IS_USED(MODULE_NIMBLE) +#include "nimble/nimble_npl.h" +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* OS_OS_H */ diff --git a/pkg/mynewt-core/include/os/os_dev.h b/pkg/mynewt-core/include/os/os_dev.h new file mode 100644 index 0000000000..6998d0887c --- /dev/null +++ b/pkg/mynewt-core/include/os/os_dev.h @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2020 Inria + * + * 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 pkg_mynewt_core + * @{ + * + * @file + * @brief Abstraction layer for RIOT adaption + * + * @author Francisco Molina + * @} + */ +#ifndef OS_OS_DEV_H +#define OS_OS_DEV_H + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Device structure. + */ +struct os_dev { +}; + +/** + * @brief Unused define, void cast + */ +#define OS_DEV_SETHANDLERS(__dev, __open, __close) \ + (void) __dev; \ + (void) __open; \ + (void) __close; + +#ifdef __cplusplus +} +#endif + +#endif /* OS_OS_DEV_H */ diff --git a/pkg/mynewt-core/include/os/os_eventq.h b/pkg/mynewt-core/include/os/os_eventq.h new file mode 100644 index 0000000000..6a49d058f4 --- /dev/null +++ b/pkg/mynewt-core/include/os/os_eventq.h @@ -0,0 +1,236 @@ +/* + * Copyright (C) 2020 Inria + * + * 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 pkg_mynewt_core + * @{ + * + * @file + * @brief mynewt-core event and event queue abstraction + * + * @author Francisco Molina + * @} + */ + +#ifndef OS_OS_EVENTQ_H +#define OS_OS_EVENTQ_H + +#include + +#include "event/callback.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Event wrapper + */ +struct os_event +{ + event_callback_t e; /**< the event callback */ + void *arg; /**< the event argument */ +}; + +/** + * @brief Event queue wrapper + */ +struct os_eventq +{ + event_queue_t q; /**< the event queue */ +}; + +/** + * @brief Event callback function + */ +typedef void os_event_fn(struct os_event *ev); + +/** + * @brief Init a event + * + * @param[in] ev pointer to event to set + * @param[in] fn event callback function + * @param[in] arg event argument + */ +static inline void os_event_init(struct os_event *ev, os_event_fn * fn, + void *arg) +{ + /* + * Need to clear list_node manually since init function below does not do + * this. + */ + ev->e.super.list_node.next = NULL; + event_callback_init(&ev->e, (void(*)(void *))fn, ev); + ev->arg = arg; +} + +/** + * @brief Check if event is in queue + * + * @param[in] ev event to check + * + * @return true if event is queues, false otherwise + */ +static inline bool os_event_is_queued(struct os_event *ev) +{ + return (ev->e.super.list_node.next != NULL); +} + +/** + * @brief Returns event argument + * + * @param[in] ev event + */ +static inline void *os_event_get_arg(struct os_event *ev) +{ + return ev->arg; +} + +/** + * @brief Set the event argument + * + * @param[in] ev event + * @param[in] arg arg to set event + */ +static inline void os_event_set_arg(struct os_event *ev, void *arg) +{ + ev->arg = arg; +} + +/** + * @brief Runs an event + * + * @param[in] ev event to run + */ +static inline void os_event_run(struct os_event *ev) +{ + ev->e.super.handler(&ev->e.super); +} + +/** + * @brief Initialize the event queue + * + * @param[in] evq The event queue to initialize + */ +static inline void os_eventq_init(struct os_eventq *evq) +{ + event_queue_init_detached(&evq->q); +} + +/** + * @brief Check whether the event queue is initialized. + * + * @param[in] evq the event queue to check + */ +static inline int os_eventq_inited(struct os_eventq *evq) +{ + return evq->q.waiter != NULL; +} + +/** + * @brief Deinitialize an event queue + * + * @note Not supported in RIOT + * + * @param[in] evq the event queue to deinit + */ +static inline void os_eventq_deinit(struct os_eventq *evq) +{ + (void) evq; + /* Can't deinit an eventq in RIOT */ +} + +/** +* @brief Get next event from event queue + * + * @param[in] evq the event queue to pull an event from + * @param[in] tmo timeout, OS_WAIT_FOREVER to block, 0 to return immediately + * + * @return the event from the queue + */ +static inline struct os_event * os_eventq_get(struct os_eventq *evq, os_time_t tmo) +{ + if (evq->q.waiter == NULL) { + event_queue_claim(&evq->q); + } + + if (tmo == 0) { + return (struct os_event *)event_get(&evq->q); + } else if (tmo == OS_WAIT_FOREVER) { + return (struct os_event *)event_wait(&evq->q); + } else { + return (struct os_event *)event_wait_timeout_ztimer(&evq->q, + ZTIMER_MSEC, + (uint32_t)tmo); + } +} + +/** + * @brief Get next event from event queue, non-blocking + * + * @return event from the queue, or NULL if none available. + */ +static inline struct os_event * os_eventq_get_no_wait(struct os_eventq *evq) +{ + if (evq->q.waiter == NULL) { + event_queue_claim(&evq->q); + } + + return (struct os_event *) event_get(&evq->q); +} + +/** + * @brief Put an event on the event queue. + * + * @param[in] evq event queue + * @param[in] ev event to put in queue + */ +static inline void os_eventq_put(struct os_eventq *evq, struct os_event *ev) +{ + event_post(&evq->q, &ev->e.super); +} + +/** + * @brief Remove an event from the queue. + * + * @param[in] evq event queue to remove the event from + * @param[in] ev event to remove from the queue + */ +static inline void os_eventq_remove(struct os_eventq *evq, struct os_event *ev) +{ + event_cancel(&evq->q, &ev->e.super); +} + +/** + * @brief Gets and runs an event from the queue callback. + * + * @param[in] evq The event queue to pull the item off. + */ +static inline void os_eventq_run(struct os_eventq *evq) +{ + struct os_event *ev = os_eventq_get(evq, OS_WAIT_FOREVER); + os_event_run(ev); +} + +/** + * @brief Check if queue is empty + * + * @param[in] evq the event queue to check + * + * @return true if empty, false otherwise + */ +static inline bool os_eventq_is_empty(struct os_eventq *evq) +{ + return clist_count(&(evq->q.event_list)) == 0; +} + +#ifdef __cplusplus +} +#endif + +#endif /* OS_OS_EVENTQ_H */ diff --git a/pkg/mynewt-core/include/os/os_time.h b/pkg/mynewt-core/include/os/os_time.h new file mode 100644 index 0000000000..1ca7f50a59 --- /dev/null +++ b/pkg/mynewt-core/include/os/os_time.h @@ -0,0 +1,116 @@ +/* + * Copyright (C) 2020 Inria + * + * 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 pkg_mynewt_core + * @{ + * + * @file + * @brief mynewt-core time abstraction + * + * @author Francisco Molina + * @} + */ + +#ifndef OS_OS_TIME_H +#define OS_OS_TIME_H + +#include "os/os_error.h" +#include "ztimer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Ticks per seconds, ztimer_msec is used as system timer + */ +#define OS_TICKS_PER_SEC (MS_PER_SEC) + +/** + * @brief Returns the low 32 bits of cputime. + * + * @return uint32_t The lower 32 bits of cputime + */ +static inline os_time_t os_time_get(void) +{ + return ztimer_now(ZTIMER_MSEC); +} + +/** + * @brief Converts the given number of milliseconds into cputime ticks. + * + * @param[in] ms The number of milliseconds to convert to ticks + * @param[out] out_ticks The number of ticks corresponding to 'ms' + * + * @return os_error_t OS_OK - no error + */ +static inline os_error_t os_time_ms_to_ticks(uint32_t ms, os_time_t *out_ticks) +{ + *out_ticks = ms; + return OS_OK; +} + +/** + * @brief Convert the given number of ticks into milliseconds. + * + * @param[in] ticks The number of ticks to convert to milliseconds. + * @param[out] out_ms The converted milliseconds from 'ticks' + * + * @return os_error_t OS_OK - no error + */ +static inline os_error_t os_time_ticks_to_ms(os_time_t ticks, uint32_t *out_ms) +{ + *out_ms = ticks; + return OS_OK; +} + +/** + * @brief Converts the given number of milliseconds into cputime ticks. + * + * @param[in] ms The number of milliseconds to convert to ticks + * + * @return uint32_t The number of ticks corresponding to 'ms' + */ +static inline os_time_t os_time_ms_to_ticks32(uint32_t ms) +{ + return ms; +} + +/** + * @brief Convert the given number of ticks into milliseconds. + * + * @param[in] ticks The number of ticks to convert to milliseconds. + * + * @return uint32_t The number of milliseconds corresponding to 'ticks' + */ +static inline os_time_t os_time_ticks_to_ms32(os_time_t ticks) +{ + return ticks; +} + +/** + * @brief Wait until the number of ticks has elapsed, BLOICKING. + * + * @param[in] ticks The number of ticks to wait. + */ +static inline void os_time_delay(os_time_t ticks) +{ + if (irq_is_in()) { + ztimer_spin(ZTIMER_MSEC, ticks); + } + else { + ztimer_sleep(ZTIMER_MSEC, ticks); + } +} + +#ifdef __cplusplus +} +#endif + +#endif /* OS_OS_TIME_H */ diff --git a/pkg/mynewt-core/include/os/os_types.h b/pkg/mynewt-core/include/os/os_types.h new file mode 100644 index 0000000000..819f093e83 --- /dev/null +++ b/pkg/mynewt-core/include/os/os_types.h @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2020 Inria + * + * 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 pkg_mynewt_core + * @{ + * + * @file + * @brief mynewt-core types + * + * @author Francisco Molina + * @} + */ + +#ifndef OS_OS_TYPES_H +#define OS_OS_TYPES_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @name Error codes not abstracted in mynewt-core/kernel/os + * @{ + */ +#define SYS_EINVAL (-2) +#define SYS_ENOMEM (-1) +/** @} */ + +/** + * @name Macro to wait forever on events and mutexes + * @{ + */ +#define OS_TIMEOUT_NEVER (UINT32_MAX) +#define OS_WAIT_FOREVER (OS_TIMEOUT_NEVER) +/** @} */ + +/** + * @brief time type + */ +typedef uint32_t os_time_t; + +/** + * @brief stack buffer type + */ +typedef char os_stack_t; + +#ifdef __cplusplus +} +#endif + +#endif /* OS_OS_TYPES_H */ diff --git a/pkg/mynewt-core/include/syscfg/syscfg.h b/pkg/mynewt-core/include/syscfg/syscfg.h new file mode 100644 index 0000000000..b546f9c19f --- /dev/null +++ b/pkg/mynewt-core/include/syscfg/syscfg.h @@ -0,0 +1,122 @@ +/** + * Apache Mynewt + * Copyright 2015-2021 The Apache Software Foundation + * + * This product includes software developed at + * The Apache Software Foundation (http://www.apache.org/). + * + * Portions of this software were developed at + * Runtime Inc, copyright 2015. + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * @ingroup pkg_mynewt_core + * @{ + * + * @file + * @brief mynewt-core system configurations + * + * @} + */ + +#ifndef SYSCFG_SYSCFG_H +#define SYSCFG_SYSCFG_H + +#include "kernel_defines.h" + +/** + * @name MyNewt header inclusion macro definitions + * @{ + * + * PLEASE NOTE: Following macro definitions where copied directly from + * apache/mynewt-core and are under the copyright specified in + * the header. + * + * This macro exists to ensure code includes this header when needed. If code + * checks the existence of a setting directly via ifdef without including this + * header, the setting macro will silently evaluate to 0. In contrast, an + * attempt to use these macros without including this header will result in a + * compiler error. + */ +#define MYNEWT_VAL(_name) MYNEWT_VAL_ ## _name +#define MYNEWT_VAL_CHOICE(_name, _val) MYNEWT_VAL_ ## _name ## __ ## _val +/** @} */ + +/** + * @brief TIMER 5 (RTC_DEV0) will be mynewt-core OS_CPUTIME timer + */ +#ifndef MYNEWT_VAL_OS_CPUTIME_TIMER_NUM +#define MYNEWT_VAL_OS_CPUTIME_TIMER_NUM (5) +#endif + +/** + * @brief Enable TIMER 5 (RTC_DEV0) + */ +#ifndef MYNEWT_VAL_TIMER_5 +#define MYNEWT_VAL_TIMER_5 (1) +#endif + +#if IS_USED(MODULE_NIMBLE) +/*** @mynewt-nimble */ +#undef MYNEWT_VAL +#undef MYNEWT_VAL_CHOICE +#include "npl_sycfg.h" +#endif + +#if IS_USED(MODULE_UWB_CORE) +/*** @decawave-mynewt-core/hw/drivers/uwb */ +#include "dpl_syscfg/syscfg_uwb.h" + +/*** @decawave-mynewt-core/lib/twr_ds */ +#include "dpl_syscfg/syscfg_twr_ds.h" + +/*** @decawave-mynewt-core/lib/twr_ds_ext */ +#include "dpl_syscfg/syscfg_twr_ds_ext.h" + +/*** @decawave-mynewt-core/lib/twr_ss */ +#include "dpl_syscfg/syscfg_twr_ss.h" + +/*** @decawave-mynewt-core/lib/twr_ss_ack */ +#include "dpl_syscfg/syscfg_twr_ss_ack.h" + +/*** @decawave-mynewt-core/lib/twr_ss_ext */ +#include "dpl_syscfg/syscfg_twr_ss_ext.h" + +/*** @decawave-mynewt-core/lib/uwb_rng */ +#include "dpl_syscfg/syscfg_uwb_rng.h" + +/*** @decawave-mynewt-core/sys/uwbcfg */ +#include "dpl_syscfg/syscfg_uwbcfg.h" +#endif + +#if IS_USED(MODULE_UWB_DW1000) +/*** @decawave-uwb-dw1000/hw/drivers/uwb/uwb_dw1000 */ +#include "syscfg_uwb_dw1000.h" +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* SYSCFG_SYSCFG_H */ diff --git a/pkg/mynewt-core/include/sysinit/sysinit.h b/pkg/mynewt-core/include/sysinit/sysinit.h new file mode 100644 index 0000000000..7c65fd56f6 --- /dev/null +++ b/pkg/mynewt-core/include/sysinit/sysinit.h @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2020 Inria + * + * 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 pkg_mynewt_core + * @{ + * + * @file + * @brief sysinit abstraction layer for RIOT adaption + * + * @author Francisco Molina + * @} + */ + +#ifndef SYSINIT_SYSINIT_H +#define SYSINIT_SYSINIT_H + +#include "assert.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief assert macro + */ +#define SYSINIT_PANIC_ASSERT(rc) assert(rc); + +/** + * @brief empty definition + */ +#define SYSINIT_ASSERT_ACTIVE() + +#ifdef __cplusplus +} +#endif + +#endif /* SYSINIT_SYSINIT_H */ diff --git a/pkg/mynewt-core/mynewt-core_nrf5x_hal.mk b/pkg/mynewt-core/mynewt-core_nrf5x_hal.mk new file mode 100644 index 0000000000..b1f2145380 --- /dev/null +++ b/pkg/mynewt-core/mynewt-core_nrf5x_hal.mk @@ -0,0 +1,5 @@ +MODULE = mynewt-core_nrf5x_hal + +SRC := hal_timer.c + +include $(RIOTBASE)/Makefile.base diff --git a/pkg/mynewt-core/mynewt-core_os.mk b/pkg/mynewt-core/mynewt-core_os.mk new file mode 100644 index 0000000000..8077bdeabe --- /dev/null +++ b/pkg/mynewt-core/mynewt-core_os.mk @@ -0,0 +1,15 @@ +MODULE = mynewt-core_os + +SRC := \ + endian.c \ + os_mbuf.c \ + os_mempool.c \ + os_msys.c \ + os_cputime_pwr2.c \ + # + +ifneq (,$(filter nrf%,$(CPU))) + SRC += os_cputime.c +endif + +include $(RIOTBASE)/Makefile.base diff --git a/pkg/mynewt-core/mynewt-core_util.mk b/pkg/mynewt-core/mynewt-core_util.mk new file mode 100644 index 0000000000..eb4abdce05 --- /dev/null +++ b/pkg/mynewt-core/mynewt-core_util.mk @@ -0,0 +1,5 @@ +MODULE = mynewt-core_util + +SRC := mem.c + +include $(RIOTBASE)/Makefile.base diff --git a/pkg/mynewt-core/patches/0001-hw-mcu-nrf5x-adapt-NVIC-init-to-RIOT.patch b/pkg/mynewt-core/patches/0001-hw-mcu-nrf5x-adapt-NVIC-init-to-RIOT.patch new file mode 100644 index 0000000000000000000000000000000000000000..de1c0ba6c37afa0cf1799629ee6a2464a95a567a GIT binary patch literal 2272 zcmeHIT~FIE6n*!vxMD9YZ9eQYX(*-8hS~s7rexd@41J z#0+Q`!mwZefQ2C?CrHzW6O(DKDqdn@p>zfZQcUZo!JPZNI3&lJ6@5q32D%4FGlOtO zfyVt9`4VV_NJmEV@i)P3AHJL)UB3SaG>E>!9d=5W>hR|{gQQL9s85~{1VL=8`6`}$@ z$@I0z@pK{Ts)Vts)K#O^#0Z646yiSMH3`X;l;C;Ss6GoGPWOW1@hcOF(z#ebwzoGH zNzBK-9|_5ORAwPc3zJUAHR{!M3APAcU3)M9kFho$xP^avFc=Vcqckt(xqySLa&7S^ zhwDLMj4n50J7pD2@3gqZ+#TYr2zJ~Q@M)#0RNpA>Hd~vUnBbz1HFxl(uf6z9IyX@C zNr^g^j+K>46nXVz_hAfaio8$iBXUs}ClKty@ZvMfOleDdHxsZM5O5E*TU=v2?A?R(~3F0Y#e z2)Knpj#ZV73*iK{p%NBVsnRHW2UxNW+@Rq~gF_3p&U9kkau!RMa6~tpPOO94uvR!>x4B`EYCUKj*_|e8wJp_T&<9@0+E(DEarl;KHa|-)M0Z zw<-G*8;+}4ZBy^iPEBgWHN3?SRUN`HbZawX=`_q*KA}JU8~Xl|`>mnz!$z3TdVWaA EFF+Z^?f?J) literal 0 HcmV?d00001 diff --git a/pkg/mynewt-core/patches/0002-kernel-os-src-riot-patches.patch b/pkg/mynewt-core/patches/0002-kernel-os-src-riot-patches.patch new file mode 100644 index 0000000000000000000000000000000000000000..68ad996c3e6acef669071c82ebfb5480a1fcaa29 GIT binary patch literal 7985 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