Skip to content

Commit

Permalink
sys: move WAIT_FOR into time_utils.h
Browse files Browse the repository at this point in the history
WAIT_FOR uses k_us_to_cyc_ceil32, which is defined in time_utils.h, so
right now time_utils.h and util.h include each others, potentially
causing issues due to inconsistent include ordering.

Fix this by moving WAIT_FOR into time_utils.h and dropping the
time_utils.h include from util.h.

Signed-off-by: Fabio Baltieri <[email protected]>
  • Loading branch information
fabiobaltieri committed Sep 13, 2023
1 parent da3c9d2 commit 023048b
Show file tree
Hide file tree
Showing 21 changed files with 49 additions and 39 deletions.
1 change: 1 addition & 0 deletions drivers/dai/intel/dmic/dmic.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ LOG_MODULE_REGISTER(LOG_DOMAIN);
#include <zephyr/devicetree.h>
#include <zephyr/pm/device.h>
#include <zephyr/pm/device_runtime.h>
#include <zephyr/sys/time_utils.h>

#include <zephyr/drivers/dai.h>
#include <zephyr/irq.h>
Expand Down
1 change: 1 addition & 0 deletions drivers/dai/intel/ssp/ssp.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <zephyr/devicetree.h>
#include <zephyr/pm/device.h>
#include <zephyr/pm/device_runtime.h>
#include <zephyr/sys/time_utils.h>
#define LOG_DOMAIN dai_intel_ssp
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(LOG_DOMAIN);
Expand Down
1 change: 1 addition & 0 deletions drivers/dma/dma_dw_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <zephyr/init.h>
#include <zephyr/drivers/dma.h>
#include <zephyr/pm/device_runtime.h>
#include <zephyr/sys/time_utils.h>
#include <soc.h>
#include "dma_dw_common.h"

Expand Down
1 change: 1 addition & 0 deletions drivers/dma/dma_intel_adsp_gpdma.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <adsp_interrupt.h>
#include <zephyr/drivers/dma.h>
#include <zephyr/sys/time_utils.h>
#include <zephyr/cache.h>

#define DT_DRV_COMPAT intel_adsp_gpdma
Expand Down
1 change: 1 addition & 0 deletions drivers/power_domain/power_domain_intel_adsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <zephyr/kernel.h>
#include <zephyr/pm/device.h>
#include <zephyr/pm/device_runtime.h>
#include <zephyr/sys/time_utils.h>
#include <adsp_shim.h>

#include <zephyr/logging/log.h>
Expand Down
26 changes: 26 additions & 0 deletions include/zephyr/sys/time_units.h
Original file line number Diff line number Diff line change
Expand Up @@ -1433,6 +1433,32 @@ static TIME_CONSTEXPR inline uint64_t k_ticks_to_cyc_ceil64(uint64_t t)

#undef TIME_CONSTEXPR

/**
* @brief Wait for an expression to return true with a timeout
*
* Spin on an expression with a timeout and optional delay between iterations
*
* Commonly needed when waiting on hardware to complete an asynchronous
* request to read/write/initialize/reset, but useful for any expression.
*
* @param expr Truth expression upon which to poll, e.g.: XYZREG & XYZREG_EN
* @param timeout Timeout to wait for in microseconds, e.g.: 1000 (1ms)
* @param delay_stmt Delay statement to perform each poll iteration
* e.g.: NULL, k_yield(), k_msleep(1) or k_busy_wait(1)
*
* @retval expr As a boolean return, if false then it has timed out.
*/
#define WAIT_FOR(expr, timeout, delay_stmt) \
({ \
uint32_t _wf_cycle_count = k_us_to_cyc_ceil32(timeout); \
uint32_t _wf_start = k_cycle_get_32(); \
while (!(expr) && (_wf_cycle_count > (k_cycle_get_32() - _wf_start))) { \
delay_stmt; \
Z_SPIN_DELAY(10); \
} \
(expr); \
})

/**
* @}
*/
Expand Down
31 changes: 0 additions & 31 deletions include/zephyr/sys/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -634,11 +634,6 @@ char *utf8_lcpy(char *dst, const char *src, size_t n);
}
#endif

/* This file must be included at the end of the !_ASMLANGUAGE guard.
* It depends on macros defined in this file above which cannot be forward declared.
*/
#include <zephyr/sys/time_units.h>

#endif /* !_ASMLANGUAGE */

/** @brief Number of bytes in @p x kibibytes */
Expand Down Expand Up @@ -676,32 +671,6 @@ char *utf8_lcpy(char *dst, const char *src, size_t n);
#define Z_SPIN_DELAY(t)
#endif

/**
* @brief Wait for an expression to return true with a timeout
*
* Spin on an expression with a timeout and optional delay between iterations
*
* Commonly needed when waiting on hardware to complete an asynchronous
* request to read/write/initialize/reset, but useful for any expression.
*
* @param expr Truth expression upon which to poll, e.g.: XYZREG & XYZREG_EN
* @param timeout Timeout to wait for in microseconds, e.g.: 1000 (1ms)
* @param delay_stmt Delay statement to perform each poll iteration
* e.g.: NULL, k_yield(), k_msleep(1) or k_busy_wait(1)
*
* @retval expr As a boolean return, if false then it has timed out.
*/
#define WAIT_FOR(expr, timeout, delay_stmt) \
({ \
uint32_t _wf_cycle_count = k_us_to_cyc_ceil32(timeout); \
uint32_t _wf_start = k_cycle_get_32(); \
while (!(expr) && (_wf_cycle_count > (k_cycle_get_32() - _wf_start))) { \
delay_stmt; \
Z_SPIN_DELAY(10); \
} \
(expr); \
})

/**
* @}
*/
Expand Down
1 change: 1 addition & 0 deletions soc/xtensa/intel_adsp/ace/comm_widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <zephyr/cache.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/time_utils.h>

#define CW_DT_NODE DT_NODELABEL(ace_comm_widget)
#define CW_BASE DT_REG_ADDR(CW_DT_NODE)
Expand Down
1 change: 1 addition & 0 deletions soc/xtensa/intel_adsp/ace/multiprocessing.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <zephyr/sys/check.h>
#include <zephyr/arch/cpu.h>
#include <zephyr/pm/pm.h>
#include <zephyr/sys/time_utils.h>

#include <soc.h>
#include <adsp_boot.h>
Expand Down
1 change: 1 addition & 0 deletions soc/xtensa/intel_adsp/ace/power.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <zephyr/pm/pm.h>
#include <zephyr/device.h>
#include <zephyr/debug/sparse.h>
#include <zephyr/sys/time_utils.h>
#include <zephyr/cache.h>
#include <cpu_init.h>
#include <soc_util.h>
Expand Down
1 change: 1 addition & 0 deletions soc/xtensa/intel_adsp/common/clk.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <zephyr/kernel.h>
#include <zephyr/spinlock.h>
#include <zephyr/sys/util.h>
#include <zephyr/sys/time_utils.h>

#include <adsp_clk.h>
#include <adsp_shim.h>
Expand Down
1 change: 1 addition & 0 deletions tests/boards/intel_adsp/hda/src/dma.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <zephyr/cache.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/time_utils.h>
#include <zephyr/ztest.h>
#include <intel_adsp_ipc.h>
#include <zephyr/drivers/dma.h>
Expand Down
1 change: 1 addition & 0 deletions tests/boards/intel_adsp/hda/src/smoke.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <zephyr/cache.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/time_utils.h>
#include <zephyr/ztest.h>
#include <intel_adsp_ipc.h>
#include <zephyr/devicetree.h>
Expand Down
1 change: 1 addition & 0 deletions tests/drivers/can/shell/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <zephyr/fff.h>
#include <zephyr/shell/shell.h>
#include <zephyr/shell/shell_dummy.h>
#include <zephyr/sys/time_utils.h>
#include <zephyr/ztest.h>

/**
Expand Down
1 change: 1 addition & 0 deletions tests/drivers/eeprom/shell/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <zephyr/shell/shell.h>
#include <zephyr/shell/shell_dummy.h>
#include <zephyr/sys/util.h>
#include <zephyr/sys/time_utils.h>
#include <zephyr/ztest.h>

#define FAKE_EEPROM_NAME DEVICE_DT_NAME(DT_NODELABEL(fake_eeprom))
Expand Down
1 change: 1 addition & 0 deletions tests/drivers/rtc/shell/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <zephyr/fff.h>
#include <zephyr/shell/shell.h>
#include <zephyr/shell/shell_dummy.h>
#include <zephyr/sys/time_utils.h>
#include <zephyr/ztest.h>

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(sys_util)
project(time_utils)

target_sources(app PRIVATE
src/main.c
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
*/

#include <zephyr/ztest.h>
#include <zephyr/sys/util.h>
#include <zephyr/sys/time_utils.h>
#include <zephyr/kernel.h>

/**
* @addtogroup sys_util_tests
* @addtogroup time_utils_tests
* @{
*/

Expand All @@ -19,7 +19,7 @@
* @see WAIT_FOR()
*/

ZTEST(sys_util, test_wait_for)
ZTEST(time_utils, test_wait_for)
{
uint32_t start, end, expected;

Expand All @@ -43,10 +43,10 @@ ZTEST(sys_util, test_wait_for)


/**
* @defgroup sys_util_tests Sys Util Tests
* @defgroup time_utils_tests Sys Util Tests
* @ingroup all_tests
* @{
* @}
*/

ZTEST_SUITE(sys_util, NULL, NULL, NULL, NULL, NULL);
ZTEST_SUITE(time_utils, NULL, NULL, NULL, NULL, NULL);
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
tests:
libraries.sys_util:
tags: sys_util
libraries.time_utils:
tags: time_utils
integration_platforms:
- native_posix
1 change: 1 addition & 0 deletions tests/subsys/shell/shell/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/

#include <zephyr/kernel.h>
#include <zephyr/sys/time_utils.h>
#include <zephyr/ztest.h>

#include <zephyr/shell/shell.h>
Expand Down

0 comments on commit 023048b

Please sign in to comment.