Merge branch 'bugfix/i2c_timing_wrong_v5.1' into 'release/v5.1'

i2c: fix a bug in sda sample timing (backport v5.1)

See merge request espressif/esp-idf!23379
This commit is contained in:
morris 2023-05-15 10:47:35 +08:00
commit 78d88afbef
5 changed files with 31 additions and 6 deletions

View File

@ -10,6 +10,7 @@
#include <stdbool.h> #include <stdbool.h>
#include "hal/misc.h" #include "hal/misc.h"
#include "hal/assert.h"
#include "soc/i2c_periph.h" #include "soc/i2c_periph.h"
#include "soc/soc_caps.h" #include "soc/soc_caps.h"
#include "soc/i2c_struct.h" #include "soc/i2c_struct.h"
@ -92,12 +93,16 @@ static inline void i2c_ll_cal_bus_clk(uint32_t source_clk, uint32_t bus_freq, i2
clk_cal->scl_wait_high = (bus_freq >= 80*1000) ? (half_cycle / 2 - 2) : (half_cycle / 4); clk_cal->scl_wait_high = (bus_freq >= 80*1000) ? (half_cycle / 2 - 2) : (half_cycle / 4);
clk_cal->scl_high = half_cycle - clk_cal->scl_wait_high; clk_cal->scl_high = half_cycle - clk_cal->scl_wait_high;
clk_cal->sda_hold = half_cycle / 4; clk_cal->sda_hold = half_cycle / 4;
clk_cal->sda_sample = half_cycle / 2 + clk_cal->scl_wait_high; clk_cal->sda_sample = half_cycle / 2;
clk_cal->setup = half_cycle; clk_cal->setup = half_cycle;
clk_cal->hold = half_cycle; clk_cal->hold = half_cycle;
//default we set the timeout value to about 10 bus cycles //default we set the timeout value to about 10 bus cycles
// log(20*half_cycle)/log(2) = log(half_cycle)/log(2) + log(20)/log(2) // log(20*half_cycle)/log(2) = log(half_cycle)/log(2) + log(20)/log(2)
clk_cal->tout = (int)(sizeof(half_cycle) * 8 - __builtin_clz(5 * half_cycle)) + 2; clk_cal->tout = (int)(sizeof(half_cycle) * 8 - __builtin_clz(5 * half_cycle)) + 2;
/* Verify the assumptions made by the hardware */
HAL_ASSERT(clk_cal->scl_wait_high < clk_cal->sda_sample &&
clk_cal->sda_sample < clk_cal->scl_high);
} }
/** /**

View File

@ -10,6 +10,7 @@
#include "stdbool.h" #include "stdbool.h"
#include "hal/misc.h" #include "hal/misc.h"
#include "hal/assert.h"
#include "soc/i2c_periph.h" #include "soc/i2c_periph.h"
#include "soc/soc_caps.h" #include "soc/soc_caps.h"
#include "soc/i2c_struct.h" #include "soc/i2c_struct.h"
@ -93,12 +94,16 @@ static inline void i2c_ll_cal_bus_clk(uint32_t source_clk, uint32_t bus_freq, i2
clk_cal->scl_wait_high = (bus_freq >= 80*1000) ? (half_cycle / 2 - 2) : (half_cycle / 4); clk_cal->scl_wait_high = (bus_freq >= 80*1000) ? (half_cycle / 2 - 2) : (half_cycle / 4);
clk_cal->scl_high = half_cycle - clk_cal->scl_wait_high; clk_cal->scl_high = half_cycle - clk_cal->scl_wait_high;
clk_cal->sda_hold = half_cycle / 4; clk_cal->sda_hold = half_cycle / 4;
clk_cal->sda_sample = half_cycle / 2 + clk_cal->scl_wait_high; clk_cal->sda_sample = half_cycle / 2;
clk_cal->setup = half_cycle; clk_cal->setup = half_cycle;
clk_cal->hold = half_cycle; clk_cal->hold = half_cycle;
//default we set the timeout value to about 10 bus cycles //default we set the timeout value to about 10 bus cycles
// log(20*half_cycle)/log(2) = log(half_cycle)/log(2) + log(20)/log(2) // log(20*half_cycle)/log(2) = log(half_cycle)/log(2) + log(20)/log(2)
clk_cal->tout = (int)(sizeof(half_cycle) * 8 - __builtin_clz(5 * half_cycle)) + 2; clk_cal->tout = (int)(sizeof(half_cycle) * 8 - __builtin_clz(5 * half_cycle)) + 2;
/* Verify the assumptions made by the hardware */
HAL_ASSERT(clk_cal->scl_wait_high < clk_cal->sda_sample &&
clk_cal->sda_sample < clk_cal->scl_high);
} }
/** /**

View File

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
@ -10,6 +10,7 @@
#include <stdbool.h> #include <stdbool.h>
#include "hal/misc.h" #include "hal/misc.h"
#include "hal/assert.h"
#include "soc/i2c_periph.h" #include "soc/i2c_periph.h"
#include "soc/soc_caps.h" #include "soc/soc_caps.h"
#include "soc/i2c_struct.h" #include "soc/i2c_struct.h"
@ -91,12 +92,16 @@ static inline void i2c_ll_cal_bus_clk(uint32_t source_clk, uint32_t bus_freq, i2
clk_cal->scl_wait_high = (bus_freq >= 80*1000) ? (half_cycle / 2 - 2) : (half_cycle / 4); clk_cal->scl_wait_high = (bus_freq >= 80*1000) ? (half_cycle / 2 - 2) : (half_cycle / 4);
clk_cal->scl_high = half_cycle - clk_cal->scl_wait_high; clk_cal->scl_high = half_cycle - clk_cal->scl_wait_high;
clk_cal->sda_hold = half_cycle / 4; clk_cal->sda_hold = half_cycle / 4;
clk_cal->sda_sample = half_cycle / 2 + clk_cal->scl_wait_high; clk_cal->sda_sample = half_cycle / 2;
clk_cal->setup = half_cycle; clk_cal->setup = half_cycle;
clk_cal->hold = half_cycle; clk_cal->hold = half_cycle;
//default we set the timeout value to about 10 bus cycles //default we set the timeout value to about 10 bus cycles
// log(20*half_cycle)/log(2) = log(half_cycle)/log(2) + log(20)/log(2) // log(20*half_cycle)/log(2) = log(half_cycle)/log(2) + log(20)/log(2)
clk_cal->tout = (int)(sizeof(half_cycle) * 8 - __builtin_clz(5 * half_cycle)) + 2; clk_cal->tout = (int)(sizeof(half_cycle) * 8 - __builtin_clz(5 * half_cycle)) + 2;
/* Verify the assumptions made by the hardware */
HAL_ASSERT(clk_cal->scl_wait_high < clk_cal->sda_sample &&
clk_cal->sda_sample < clk_cal->scl_high);
} }
/** /**

View File

@ -10,6 +10,7 @@
#include <stdbool.h> #include <stdbool.h>
#include "hal/misc.h" #include "hal/misc.h"
#include "hal/assert.h"
#include "soc/i2c_periph.h" #include "soc/i2c_periph.h"
#include "soc/soc_caps.h" #include "soc/soc_caps.h"
#include "soc/i2c_struct.h" #include "soc/i2c_struct.h"
@ -92,12 +93,16 @@ static inline void i2c_ll_cal_bus_clk(uint32_t source_clk, uint32_t bus_freq, i2
clk_cal->scl_wait_high = (bus_freq >= 80*1000) ? (half_cycle / 2 - 2) : (half_cycle / 4); clk_cal->scl_wait_high = (bus_freq >= 80*1000) ? (half_cycle / 2 - 2) : (half_cycle / 4);
clk_cal->scl_high = half_cycle - clk_cal->scl_wait_high; clk_cal->scl_high = half_cycle - clk_cal->scl_wait_high;
clk_cal->sda_hold = half_cycle / 4; clk_cal->sda_hold = half_cycle / 4;
clk_cal->sda_sample = half_cycle / 2 + clk_cal->scl_wait_high; clk_cal->sda_sample = half_cycle / 2;
clk_cal->setup = half_cycle; clk_cal->setup = half_cycle;
clk_cal->hold = half_cycle; clk_cal->hold = half_cycle;
//default we set the timeout value to about 10 bus cycles //default we set the timeout value to about 10 bus cycles
// log(20*half_cycle)/log(2) = log(half_cycle)/log(2) + log(20)/log(2) // log(20*half_cycle)/log(2) = log(half_cycle)/log(2) + log(20)/log(2)
clk_cal->tout = (int)(sizeof(half_cycle) * 8 - __builtin_clz(5 * half_cycle)) + 2; clk_cal->tout = (int)(sizeof(half_cycle) * 8 - __builtin_clz(5 * half_cycle)) + 2;
/* Verify the assumptions made by the hardware */
HAL_ASSERT(clk_cal->scl_wait_high < clk_cal->sda_sample &&
clk_cal->sda_sample < clk_cal->scl_high);
} }
/** /**

View File

@ -10,6 +10,7 @@
#include "stdbool.h" #include "stdbool.h"
#include "hal/misc.h" #include "hal/misc.h"
#include "hal/assert.h"
#include "soc/i2c_periph.h" #include "soc/i2c_periph.h"
#include "soc/soc_caps.h" #include "soc/soc_caps.h"
#include "soc/i2c_struct.h" #include "soc/i2c_struct.h"
@ -92,12 +93,16 @@ static inline void i2c_ll_cal_bus_clk(uint32_t source_clk, uint32_t bus_freq, i2
clk_cal->scl_wait_high = (bus_freq >= 80*1000) ? (half_cycle / 2 - 2) : (half_cycle / 4); clk_cal->scl_wait_high = (bus_freq >= 80*1000) ? (half_cycle / 2 - 2) : (half_cycle / 4);
clk_cal->scl_high = half_cycle - clk_cal->scl_wait_high; clk_cal->scl_high = half_cycle - clk_cal->scl_wait_high;
clk_cal->sda_hold = half_cycle / 4; clk_cal->sda_hold = half_cycle / 4;
clk_cal->sda_sample = half_cycle / 2 + clk_cal->scl_wait_high; clk_cal->sda_sample = half_cycle / 2;
clk_cal->setup = half_cycle; clk_cal->setup = half_cycle;
clk_cal->hold = half_cycle; clk_cal->hold = half_cycle;
//default we set the timeout value to about 10 bus cycles //default we set the timeout value to about 10 bus cycles
// log(20*half_cycle)/log(2) = log(half_cycle)/log(2) + log(20)/log(2) // log(20*half_cycle)/log(2) = log(half_cycle)/log(2) + log(20)/log(2)
clk_cal->tout = (int)(sizeof(half_cycle) * 8 - __builtin_clz(5 * half_cycle)) + 2; clk_cal->tout = (int)(sizeof(half_cycle) * 8 - __builtin_clz(5 * half_cycle)) + 2;
/* Verify the assumptions made by the hardware */
HAL_ASSERT(clk_cal->scl_wait_high < clk_cal->sda_sample &&
clk_cal->sda_sample < clk_cal->scl_high);
} }
/** /**