From aa363178d333dbe0fdf2f7752ff40fb79810e9dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Zi=C4=99cik?= Date: Tue, 23 Apr 2019 15:08:00 +0200 Subject: [PATCH] drivers: i2c_bitbang: Do not use CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On some SoCs the frequency of the system clock is obtained at run time as the exact configuration of the hardware is not known at compile time. On such platforms using CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC define directly introduces timing errors. This commit replaces CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC by the call to inline function sys_clock_hw_cycles_per_sec() which always returns correct frequency of the system clock. Signed-off-by: Piotr Zięcik --- drivers/i2c/i2c_bitbang.c | 18 +++++------------- drivers/i2c/i2c_bitbang.h | 2 +- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/drivers/i2c/i2c_bitbang.c b/drivers/i2c/i2c_bitbang.c index 5c074498b4b..002b6f1c230 100644 --- a/drivers/i2c/i2c_bitbang.c +++ b/drivers/i2c/i2c_bitbang.c @@ -37,17 +37,7 @@ #define T_BUF T_LOW #define NS_TO_SYS_CLOCK_HW_CYCLES(ns) \ - ((u64_t)CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC * (ns) / NSEC_PER_SEC + 1) - -static const u32_t delays_fast[] = { - [T_LOW] = NS_TO_SYS_CLOCK_HW_CYCLES(1300), - [T_HIGH] = NS_TO_SYS_CLOCK_HW_CYCLES(600), -}; - -static const u32_t delays_standard[] = { - [T_LOW] = NS_TO_SYS_CLOCK_HW_CYCLES(4700), - [T_HIGH] = NS_TO_SYS_CLOCK_HW_CYCLES(4000), -}; + ((u64_t)sys_clock_hw_cycles_per_sec() * (ns) / NSEC_PER_SEC + 1) int i2c_bitbang_configure(struct i2c_bitbang *context, u32_t dev_config) { @@ -59,10 +49,12 @@ int i2c_bitbang_configure(struct i2c_bitbang *context, u32_t dev_config) /* Setup speed to use */ switch (I2C_SPEED_GET(dev_config)) { case I2C_SPEED_STANDARD: - context->delays = delays_standard; + context->delays[T_LOW] = NS_TO_SYS_CLOCK_HW_CYCLES(4700); + context->delays[T_HIGH] = NS_TO_SYS_CLOCK_HW_CYCLES(4000); break; case I2C_SPEED_FAST: - context->delays = delays_fast; + context->delays[T_LOW] = NS_TO_SYS_CLOCK_HW_CYCLES(1300); + context->delays[T_HIGH] = NS_TO_SYS_CLOCK_HW_CYCLES(600); break; default: return -ENOTSUP; diff --git a/drivers/i2c/i2c_bitbang.h b/drivers/i2c/i2c_bitbang.h index 9be690eead3..796b4978647 100644 --- a/drivers/i2c/i2c_bitbang.h +++ b/drivers/i2c/i2c_bitbang.h @@ -28,7 +28,7 @@ struct i2c_bitbang_io { struct i2c_bitbang { const struct i2c_bitbang_io *io; void *io_context; - const u32_t *delays; + u32_t delays[2]; }; /**