From 4f3b57a6e2505da69449514d2ce3bc69076b2550 Mon Sep 17 00:00:00 2001 From: gojimmypi Date: Mon, 13 May 2024 11:43:33 -0700 Subject: [PATCH] flexibility for I2C_MASTER_NUM, add settings comments --- .../wolfssl/include/user_settings.h | 15 +++++++++++++- hal/tpm_io.c | 2 ++ hal/tpm_io_espressif.c | 20 ++++++++++++++++--- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/IDE/Espressif/components/wolfssl/include/user_settings.h b/IDE/Espressif/components/wolfssl/include/user_settings.h index e45536f..a885299 100644 --- a/IDE/Espressif/components/wolfssl/include/user_settings.h +++ b/IDE/Espressif/components/wolfssl/include/user_settings.h @@ -656,11 +656,24 @@ Turn on timer debugging (used when CPU cycles not available) #define TPM_TIMEOUT_TRIES 10000 /* If not defined here, TPM_I2C_TRIES is set to a default value of 10 */ -/* TPM_I2C_TRIES 10 */ +/* #define TPM_I2C_TRIES 10 */ + +/* If not defined here, I2C_MASTER_FREQ_HZ is 100000 + * Do not exceed a value of 400000 */ +/* #define I2C_MASTER_FREQ_HZ 100000 */ /* Examples may have a main() function, we'll have oour own: */ #define NO_MAIN_DRIVER +/* I2C GPIO settings are defined in idf.py menuconfig + * + * CONFIG_I2C_MASTER_SCL (default SCL GPIO pin is 19) + * CONFIG_I2C_MASTER_SDA (default SDA GPIO pin is 18) + */ + +/* The default I2C_MASTER_NUM is 0 but can be overridden: */ +/* #define I2C_MASTER_NUM 0 */ + /* I2C_MASTER_FREQ_HZ notes: * * Although the Infineon supports higher speeds, the ESP32 does not. diff --git a/hal/tpm_io.c b/hal/tpm_io.c index 2f0b3e2..8944805 100644 --- a/hal/tpm_io.c +++ b/hal/tpm_io.c @@ -69,6 +69,8 @@ #include "hal/tpm_io_infineon.c" #elif defined(WOLFTPM_MICROCHIP_HARMONY) #include "hal/tpm_io_microchip.c" +#elif defined(WOLFSSL_ESPIDF) +#include "hal/tpm_io_espressif.c" #endif #if !defined(WOLFTPM_I2C) && !defined(WOLFTPM_MMIO) diff --git a/hal/tpm_io_espressif.c b/hal/tpm_io_espressif.c index 697f76f..8a003a4 100644 --- a/hal/tpm_io_espressif.c +++ b/hal/tpm_io_espressif.c @@ -74,10 +74,14 @@ #include #endif +#ifndef CONFIG_SOC_I2C_SUPPORTED + #error "It appears I2C is not supported. Please check sdkconfig." +#endif + /* GPIO number used for I2C master clock */ #ifdef CONFIG_I2C_MASTER_SCL /* Yellow wire Clock */ - #define I2C_MASTER_SCL_IO CONFIG_I2C_MASTER_SCL + #define I2C_MASTER_SCL_IO CONFIG_I2C_MASTER_SCL #else /* There should have been a Kconfig.projbuild file in the ./main * directory to set I2C parameters in the sdkconfig project file. */ @@ -87,7 +91,7 @@ /* GPIO number used for I2C master data */ #ifdef CONFIG_I2C_MASTER_SDA /* Orange wire */ - #define I2C_MASTER_SDA_IO CONFIG_I2C_MASTER_SDA + #define I2C_MASTER_SDA_IO CONFIG_I2C_MASTER_SDA #else /* There should have been a Kconfig.projbuild file in the ./main * directory to set I2C parameters in the sdkconfig project file. */ @@ -96,7 +100,9 @@ /* I2C master i2c port number, * the number of i2c peripheral interfaces available will depend on the chip */ -#define I2C_MASTER_NUM 0 +#ifndef I2C_MASTER_NUM + #define I2C_MASTER_NUM 0 +#endif /* I2C master clock frequency * Typically, an I2C slave device has a 7-bit address or 10-bit address. @@ -184,11 +190,19 @@ static esp_err_t esp_i2c_master_init(void) int i2c_master_port = I2C_MASTER_NUM; esp_err_t ret = ESP_OK; + /* I2C port number, can be I2C_NUM_0 ~ (I2C_NUM_MAX-1). */ + if (I2C_MASTER_NUM >= I2C_NUM_MAX) { + ESP_LOGW(TAG, "Warning: I2C_MASTER_NUM value %d exceeds (I2C_NUM_MAX-1)" + " %d ", I2C_MASTER_NUM, I2C_NUM_MAX); + } ESP_LOGI(TAG, "esp_i2c_master_init"); ESP_LOGI(TAG, "I2C_MASTER_FREQ_HZ = %d", (int)I2C_MASTER_FREQ_HZ); ESP_LOGI(TAG, "I2C_READ_WAIT_TICKS = %d", (int)I2C_READ_WAIT_TICKS); ESP_LOGI(TAG, "I2C_WRITE_WAIT_TICKS = %d", (int)I2C_WRITE_WAIT_TICKS); ESP_LOGI(TAG, "I2C_MASTER_TIMEOUT_MS = %d", (int)I2C_MASTER_TIMEOUT_MS); + ESP_LOGI(TAG, "I2C_MASTER_NUM = %d", (int)I2C_MASTER_NUM); + ESP_LOGI(TAG, "I2C_MASTER_SCL_IO = %d", (int)I2C_MASTER_SCL_IO); + ESP_LOGI(TAG, "I2C_MASTER_SDA_IO = %d", (int)I2C_MASTER_SDA_IO); conf.mode = I2C_MODE_MASTER; conf.sda_io_num = I2C_MASTER_SDA_IO;