Skip to content
Permalink
Browse files

drivers: i2c_bitbang: Do not use CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC

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 <piotr.ziecik@nordicsemi.no>
  • Loading branch information...
pizi-nordic authored and carlescufi committed Apr 23, 2019
1 parent 7875707 commit aa363178d333dbe0fdf2f7752ff40fb79810e9dd
Showing with 6 additions and 14 deletions.
  1. +5 −13 drivers/i2c/i2c_bitbang.c
  2. +1 −1 drivers/i2c/i2c_bitbang.h
@@ -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;
@@ -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];
};

/**

0 comments on commit aa36317

Please sign in to comment.
You can’t perform that action at this time.