From 2070147fe68b6ec0a6c624653e2e49f857e82c08 Mon Sep 17 00:00:00 2001 From: Giovanni Di Sirio Date: Wed, 13 Oct 2021 14:47:22 +0000 Subject: [PATCH] Experimental code, to be removed. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@14908 27425a3e-05d8-49a3-a47f-9c15f0e5edd8 --- os/hal/osal/sb/osal.c | 147 +++++++++ os/hal/osal/sb/osal.h | 492 +++++++++++++++++++++++++++++++ os/hal/osal/sb/osal.mk | 9 + os/hal/ports/sandbox/hal_lld.c | 60 ++++ os/hal/ports/sandbox/hal_lld.h | 92 ++++++ os/hal/ports/sandbox/platform.mk | 28 ++ 6 files changed, 828 insertions(+) create mode 100644 os/hal/osal/sb/osal.c create mode 100644 os/hal/osal/sb/osal.h create mode 100644 os/hal/osal/sb/osal.mk create mode 100644 os/hal/ports/sandbox/hal_lld.c create mode 100644 os/hal/ports/sandbox/hal_lld.h create mode 100644 os/hal/ports/sandbox/platform.mk diff --git a/os/hal/osal/sb/osal.c b/os/hal/osal/sb/osal.c new file mode 100644 index 000000000..261ae4028 --- /dev/null +++ b/os/hal/osal/sb/osal.c @@ -0,0 +1,147 @@ +/* + ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio + + Licensed 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. +*/ + +/** + * @file osal.c + * @brief OSAL module code. + * + * @addtogroup OSAL + * @{ + */ + +#include "osal.h" +#include "osal_vt.h" + +/*===========================================================================*/ +/* Module local definitions. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module exported variables. */ +/*===========================================================================*/ + +/** + * @brief Pointer to a halt error message. + * @note The message is meant to be retrieved by the debugger after the + * system halt caused by an unexpected error. + */ +const char *osal_halt_msg; + +/*===========================================================================*/ +/* Module local types. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module local variables. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module local functions. */ +/*===========================================================================*/ + +static void callback_timeout(void *p) { + osalSysLockFromISR(); + osalThreadResumeI((thread_reference_t *)p, MSG_TIMEOUT); + osalSysUnlockFromISR(); +} + +/*===========================================================================*/ +/* Module exported functions. */ +/*===========================================================================*/ + +/** + * @brief System halt with error message. + * + * @param[in] reason the halt message pointer + * + * @api + */ +#if !defined(__DOXYGEN__) +__attribute__((weak, noreturn)) +#endif +void osalSysHalt(const char *reason) { + + osalSysDisable(); + osal_halt_msg = reason; + while (true) { + } +} + +/** + * @brief Polled delay. + * @note The real delay is always few cycles in excess of the specified + * value. + * + * @param[in] cycles number of cycles + * + * @xclass + */ +void osalSysPolledDelayX(rtcnt_t cycles) { + + (void)cycles; +} + +/** + * @brief Current system time. + * @details Returns the number of system ticks since the @p osalInit() + * invocation. + * @note The counter can reach its maximum and then restart from zero. + * @note This function can be called from any context but its atomicity + * is not guaranteed on architectures whose word size is less than + * @p systime_t size. + * + * @return The system time in ticks. + * + * @xclass + */ +systime_t osalOsGetSystemTimeX(void) { + + return 0; +} + +/** + * @brief Suspends the invoking thread for the specified time. + * + * @param[in] time the delay in system ticks, the special values are + * handled as follow: + * - @a TIME_INFINITE is allowed but interpreted as a + * normal time specification. + * - @a TIME_IMMEDIATE this value is not allowed. + * . + * + * @sclass + */ +void osalThreadSleepS(sysinterval_t time) { + +} + +/** + * @brief Suspends the invoking thread for the specified time. + * + * @param[in] time the delay in system ticks, the special values are + * handled as follow: + * - @a TIME_INFINITE is allowed but interpreted as a + * normal time specification. + * - @a TIME_IMMEDIATE this value is not allowed. + * . + * + * @api + */ +void osalThreadSleep(sysinterval_t time) { + +} + +/** @} */ diff --git a/os/hal/osal/sb/osal.h b/os/hal/osal/sb/osal.h new file mode 100644 index 000000000..ca3256ac4 --- /dev/null +++ b/os/hal/osal/sb/osal.h @@ -0,0 +1,492 @@ +/* + ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio + + Licensed 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. +*/ + +/** + * @file osal.h + * @brief OSAL module header. + * + * @addtogroup OSAL + * @{ + */ + +#ifndef OSAL_H +#define OSAL_H + +#include +#include +#include + +#include "sbuser.h" + +/*===========================================================================*/ +/* Module constants. */ +/*===========================================================================*/ + +/** + * @name Common constants + * @{ + */ +#if !defined(FALSE) || defined(__DOXYGEN__) +#define FALSE 0 +#endif + +#if !defined(TRUE) || defined(__DOXYGEN__) +#define TRUE 1 +#endif + +#define OSAL_SUCCESS false +#define OSAL_FAILED true +/** @} */ + +/** + * @name Systick modes. + * @{ + */ +#define OSAL_ST_MODE_NONE 0 +#define OSAL_ST_MODE_PERIODIC 1 +#define OSAL_ST_MODE_FREERUNNING 2 +/** @} */ + +/** + * @name Systick parameters. + * @{ + */ +/** + * @brief Systick mode required by the underlying OS. + */ +#define OSAL_ST_MODE OSAL_ST_MODE_NONE +/** @} */ + +/*===========================================================================*/ +/* Module pre-compile time settings. */ +/*===========================================================================*/ + +/** + * @brief Enables OSAL assertions. + */ +#if !defined(OSAL_DBG_ENABLE_ASSERTS) || defined(__DOXYGEN__) +#define OSAL_DBG_ENABLE_ASSERTS FALSE +#endif + +/** + * @brief Enables OSAL functions parameters checks. + */ +#if !defined(OSAL_DBG_ENABLE_CHECKS) || defined(__DOXYGEN__) +#define OSAL_DBG_ENABLE_CHECKS FALSE +#endif + +/** + * @brief OSAL initialization hook. + */ +#if !defined(OSAL_INIT_HOOK) || defined(__DOXYGEN__) +#define OSAL_INIT_HOOK() +#endif + +/** + * @brief Idle loop hook macro. + */ +#if !defined(OSAL_IDLE_HOOK) || defined(__DOXYGEN__) +#define OSAL_IDLE_HOOK() +#endif + +/*===========================================================================*/ +/* Derived constants and error checks. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module data structures and types. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Module macros. */ +/*===========================================================================*/ + +/** + * @name Debug related macros + * @{ + */ +/** + * @brief Condition assertion. + * @details If the condition check fails then the OSAL panics with a + * message and halts. + * @note The condition is tested only if the @p OSAL_ENABLE_ASSERTIONS + * switch is enabled. + * @note The remark string is not currently used except for putting a + * comment in the code about the assertion. + * + * @param[in] c the condition to be verified to be true + * @param[in] remark a remark string + * + * @api + */ +#define osalDbgAssert(c, remark) do { \ + /*lint -save -e506 -e774 [2.1, 14.3] Can be a constant by design.*/ \ + if (OSAL_DBG_ENABLE_ASSERTS != FALSE) { \ + if (!(c)) { \ + /*lint -restore*/ \ + osalSysHalt(__func__); \ + } \ + } \ +} while (false) + +/** + * @brief Function parameters check. + * @details If the condition check fails then the OSAL panics and halts. + * @note The condition is tested only if the @p OSAL_ENABLE_CHECKS switch + * is enabled. + * + * @param[in] c the condition to be verified to be true + * + * @api + */ +#define osalDbgCheck(c) do { \ + /*lint -save -e506 -e774 [2.1, 14.3] Can be a constant by design.*/ \ + if (OSAL_DBG_ENABLE_CHECKS != FALSE) { \ + if (!(c)) { \ + /*lint -restore*/ \ + osalSysHalt(__func__); \ + } \ + } \ +} while (false) + +/** + * @brief I-Class state check. + * @note Implementation is optional. + */ +#define osalDbgCheckClassI() + +/** + * @brief S-Class state check. + * @note Implementation is optional. + */ +#define osalDbgCheckClassS() +/** @} */ + +/** + * @name Time conversion utilities + * @{ + */ +/** + * @brief Seconds to time interval. + * @details Converts from seconds to system ticks number. + * @note The result is rounded upward to the next tick boundary. + * @note Use of this macro for large values is not secure because + * integer overflows, make sure your value can be correctly + * converted. + * + * @param[in] secs number of seconds + * @return The number of ticks. + * + * @api + */ +#define OSAL_S2I(secs) \ + ((sysinterval_t)((time_conv_t)(secs) * (time_conv_t)OSAL_ST_FREQUENCY)) + +/** + * @brief Milliseconds to time interval. + * @details Converts from milliseconds to system ticks number. + * @note The result is rounded upward to the next tick boundary. + * @note Use of this macro for large values is not secure because + * integer overflows, make sure your value can be correctly + * converted. + * + * @param[in] msecs number of milliseconds + * @return The number of ticks. + * + * @api + */ +#define OSAL_MS2I(msecs) \ + ((sysinterval_t)((((time_conv_t)(msecs) * \ + (time_conv_t)OSAL_ST_FREQUENCY) + \ + (time_conv_t)999) / (time_conv_t)1000)) + +/** + * @brief Microseconds to time interval. + * @details Converts from microseconds to system ticks number. + * @note The result is rounded upward to the next tick boundary. + * @note Use of this macro for large values is not secure because + * integer overflows, make sure your value can be correctly + * converted. + * + * @param[in] usecs number of microseconds + * @return The number of ticks. + * + * @api + */ +#define OSAL_US2I(usecs) \ + ((sysinterval_t)((((time_conv_t)(usecs) * \ + (time_conv_t)OSAL_ST_FREQUENCY) + \ + (time_conv_t)999999) / (time_conv_t)1000000)) + +/** + * @brief Time interval to seconds. + * @details Converts from system ticks number to seconds. + * @note The result is rounded up to the next second boundary. + * @note Use of this macro for large values is not secure because + * integer overflows, make sure your value can be correctly + * converted. + * + * @param[in] interval interval in ticks + * @return The number of seconds. + * + * @api + */ +#define OSAL_I2S(interval) \ + (time_secs_t)(((time_conv_t)(interval) + \ + (time_conv_t)OSAL_ST_FREQUENCY - \ + (time_conv_t)1) / (time_conv_t)OSAL_ST_FREQUENCY) + +/** + * @brief Time interval to milliseconds. + * @details Converts from system ticks number to milliseconds. + * @note The result is rounded up to the next millisecond boundary. + * @note Use of this macro for large values is not secure because + * integer overflows, make sure your value can be correctly + * converted. + * + * @param[in] interval interval in ticks + * @return The number of milliseconds. + * + * @api + */ +#define OSAL_I2MS(interval) \ + (time_msecs_t)((((time_conv_t)(interval) * (time_conv_t)1000) + \ + (time_conv_t)OSAL_ST_FREQUENCY - (time_conv_t)1) / \ + (time_conv_t)OSAL_ST_FREQUENCY) + +/** + * @brief Time interval to microseconds. + * @details Converts from system ticks number to microseconds. + * @note The result is rounded up to the next microsecond boundary. + * @note Use of this macro for large values is not secure because + * integer overflows, make sure your value can be correctly + * converted. + * + * @param[in] interval interval in ticks + * @return The number of microseconds. + * + * @api + */ +#define OSAL_I2US(interval) \ + (time_msecs_t)((((time_conv_t)(interval) * (time_conv_t)1000000) + \ + (time_conv_t)OSAL_ST_FREQUENCY - (time_conv_t)1) / \ + (time_conv_t)OSAL_ST_FREQUENCY) +/** @} */ + +/** + * @name Time conversion utilities for the realtime counter + * @{ + */ +/** + * @brief Seconds to realtime counter. + * @details Converts from seconds to realtime counter cycles. + * @note The macro assumes that @p freq >= @p 1. + * + * @param[in] freq clock frequency, in Hz, of the realtime counter + * @param[in] sec number of seconds + * @return The number of cycles. + * + * @api + */ +#define OSAL_S2RTC(freq, sec) ((freq) * (sec)) + +/** + * @brief Milliseconds to realtime counter. + * @details Converts from milliseconds to realtime counter cycles. + * @note The result is rounded upward to the next millisecond boundary. + * @note The macro assumes that @p freq >= @p 1000. + * + * @param[in] freq clock frequency, in Hz, of the realtime counter + * @param[in] msec number of milliseconds + * @return The number of cycles. + * + * @api + */ +#define OSAL_MS2RTC(freq, msec) (rtcnt_t)((((freq) + 999UL) / 1000UL) * (msec)) + +/** + * @brief Microseconds to realtime counter. + * @details Converts from microseconds to realtime counter cycles. + * @note The result is rounded upward to the next microsecond boundary. + * @note The macro assumes that @p freq >= @p 1000000. + * + * @param[in] freq clock frequency, in Hz, of the realtime counter + * @param[in] usec number of microseconds + * @return The number of cycles. + * + * @api + */ +#define OSAL_US2RTC(freq, usec) (rtcnt_t)((((freq) + 999999UL) / 1000000UL) * (usec)) +/** @} */ + +/** + * @name Sleep macros using absolute time + * @{ + */ +/** + * @brief Delays the invoking thread for the specified number of seconds. + * @note The specified time is rounded up to a value allowed by the real + * system tick clock. + * @note The maximum specifiable value is implementation dependent. + * + * @param[in] secs time in seconds, must be different from zero + * + * @api + */ +#define osalThreadSleepSeconds(secs) osalThreadSleep(OSAL_S2I(secs)) + +/** + * @brief Delays the invoking thread for the specified number of + * milliseconds. + * @note The specified time is rounded up to a value allowed by the real + * system tick clock. + * @note The maximum specifiable value is implementation dependent. + * + * @param[in] msecs time in milliseconds, must be different from zero + * + * @api + */ +#define osalThreadSleepMilliseconds(msecs) osalThreadSleep(OSAL_MS2I(msecs)) + +/** + * @brief Delays the invoking thread for the specified number of + * microseconds. + * @note The specified time is rounded up to a value allowed by the real + * system tick clock. + * @note The maximum specifiable value is implementation dependent. + * + * @param[in] usecs time in microseconds, must be different from zero + * + * @api + */ +#define osalThreadSleepMicroseconds(usecs) osalThreadSleep(OSAL_US2I(usecs)) +/** @} */ + +/*===========================================================================*/ +/* External declarations. */ +/*===========================================================================*/ + +extern const char *osal_halt_msg; + +#ifdef __cplusplus +extern "C" { +#endif + void osalInit(void); + void osalSysHalt(const char *reason); + void osalSysPolledDelayX(rtcnt_t cycles); + systime_t osalOsGetSystemTimeX(void); + void osalThreadSleepS(sysinterval_t time); + void osalThreadSleep(sysinterval_t time); +#ifdef __cplusplus +} +#endif + +/*===========================================================================*/ +/* Module inline functions. */ +/*===========================================================================*/ + +/** + * @brief Disables interrupts globally. + * + * @special + */ +static inline void osalSysDisable(void) { + +} + +/** + * @brief Enables interrupts globally. + * + * @special + */ +static inline void osalSysEnable(void) { + +} + +/** + * @brief Enters a critical zone from thread context. + * @note This function cannot be used for reentrant critical zones. + * + * @special + */ +static inline void osalSysLock(void) { + +} + +/** + * @brief Leaves a critical zone from thread context. + * @note This function cannot be used for reentrant critical zones. + * + * @special + */ +static inline void osalSysUnlock(void) { + +} + +/** + * @brief Adds an interval to a system time returning a system time. + * + * @param[in] systime base system time + * @param[in] interval interval to be added + * @return The new system time. + * + * @xclass + */ +static inline systime_t osalTimeAddX(systime_t systime, + sysinterval_t interval) { + + return systime + (systime_t)interval; +} + +/** + * @brief Subtracts two system times returning an interval. + * + * @param[in] start first system time + * @param[in] end second system time + * @return The interval representing the time difference. + * + * @xclass + */ +static inline sysinterval_t osalTimeDiffX(systime_t start, systime_t end) { + + return (sysinterval_t)((systime_t)(end - start)); +} + +/** + * @brief Checks if the specified time is within the specified time window. + * @note When start==end then the function returns always false because the + * time window has zero size. + * @note This function can be called from any context. + * + * @param[in] time the time to be verified + * @param[in] start the start of the time window (inclusive) + * @param[in] end the end of the time window (non inclusive) + * @retval true current time within the specified time window. + * @retval false current time not within the specified time window. + * + * @xclass + */ +static inline bool osalTimeIsInRangeX(systime_t time, + systime_t start, + systime_t end) { + + return (bool)((systime_t)((systime_t)time - (systime_t)start) < + (systime_t)((systime_t)end - (systime_t)start)); +} + +#endif /* OSAL_H */ + +/** @} */ diff --git a/os/hal/osal/sb/osal.mk b/os/hal/osal/sb/osal.mk new file mode 100644 index 000000000..59c3400aa --- /dev/null +++ b/os/hal/osal/sb/osal.mk @@ -0,0 +1,9 @@ +# OSAL files. +OSALSRC += ${CHIBIOS}/os/hal/osal/sb/osal.c + +# Required include directories +OSALINC += ${CHIBIOS}/os/hal/osal/sb + +# Shared variables +ALLCSRC += $(OSALSRC) +ALLINC += $(OSALINC) diff --git a/os/hal/ports/sandbox/hal_lld.c b/os/hal/ports/sandbox/hal_lld.c new file mode 100644 index 000000000..eb98e1bcf --- /dev/null +++ b/os/hal/ports/sandbox/hal_lld.c @@ -0,0 +1,60 @@ +/* + ChibiOS - Copyright (C) 2006..2021 Giovanni Di Sirio + + Licensed 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. +*/ + +/** + * @file sandbox/hal_lld.c + * @brief SandBox HAL subsystem low level driver source. + * + * @addtogroup HAL + * @{ + */ + +#include "hal.h" + +/*===========================================================================*/ +/* Driver local definitions. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver exported variables. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver local variables and types. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver local functions. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver interrupt handlers. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver exported functions. */ +/*===========================================================================*/ + +/** + * @brief Low level HAL driver initialization. + * + * @notapi + */ +void hal_lld_init(void) { + +} + +/** @} */ diff --git a/os/hal/ports/sandbox/hal_lld.h b/os/hal/ports/sandbox/hal_lld.h new file mode 100644 index 000000000..216106166 --- /dev/null +++ b/os/hal/ports/sandbox/hal_lld.h @@ -0,0 +1,92 @@ +/* + ChibiOS - Copyright (C) 2006..2021 Giovanni Di Sirio + + Licensed 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. +*/ + +/** + * @file sandbox/hal_lld.h + * @brief SandBox HAL subsystem low level driver header. + * + * @addtogroup HAL + * @{ + */ + +#ifndef HAL_LLD_H +#define HAL_LLD_H + +/*===========================================================================*/ +/* Driver constants. */ +/*===========================================================================*/ + +/** + * @brief Specifies implementation of dynamic clock management. + */ +/*#define HAL_LLD_USE_CLOCK_MANAGEMENT*/ + +/** + * @name Platform identification macros + * @{ + */ +#define PLATFORM_NAME "SandBox" +/** @} */ + +/*===========================================================================*/ +/* Driver pre-compile time settings. */ +/*===========================================================================*/ + +/** + * @name PLATFORM configuration options + * @{ + */ +/** @} */ + +/*===========================================================================*/ +/* Derived constants and error checks. */ +/*===========================================================================*/ + +/* + * Configuration-related checks. + */ +#if !defined(SANDBOX_MCUCONF) +#error "Using a wrong mcuconf.h file, SANDBOX_MCUCONF not defined" +#endif + +/*===========================================================================*/ +/* Driver data structures and types. */ +/*===========================================================================*/ + +/** + * @brief Type of a clock point identifier. + */ +typedef unsigned halclkpt_t; + +/*===========================================================================*/ +/* Driver macros. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* External declarations. */ +/*===========================================================================*/ + +#ifdef __cplusplus +extern "C" { +#endif + void hal_lld_init(void); +#ifdef __cplusplus +} +#endif + +#endif /* HAL_LLD_H */ + +/** @} */ diff --git a/os/hal/ports/sandbox/platform.mk b/os/hal/ports/sandbox/platform.mk new file mode 100644 index 000000000..99b906808 --- /dev/null +++ b/os/hal/ports/sandbox/platform.mk @@ -0,0 +1,28 @@ +# Required platform files. +PLATFORMSRC := $(CHIBIOS)/os/hal/ports/sandbox/hal_lld.c + +# Required include directories. +PLATFORMINC := $(CHIBIOS)/os/hal/ports/sandbox + +# Optional platform files. +ifeq ($(USE_SMART_BUILD),yes) + +# Configuration files directory +ifeq ($(HALCONFDIR),) + ifeq ($(CONFDIR),) + HALCONFDIR = . + else + HALCONFDIR := $(CONFDIR) + endif +endif + +HALCONF := $(strip $(shell cat $(HALCONFDIR)/halconf.h | egrep -e "\#define")) + +else +endif + +# Drivers compatible with the platform. + +# Shared variables +ALLCSRC += $(PLATFORMSRC) +ALLINC += $(PLATFORMINC)