Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial Infineon I2C TPM support for Espressif ESP32 #351

Merged
merged 3 commits into from May 13, 2024

Conversation

gojimmypi
Copy link
Contributor

@gojimmypi gojimmypi commented May 9, 2024

This is the initial I2C TPM support for the Infineon Optiga 9673 TPM2 on the Espressif ESP32.

Note there's a newer I2C implementation noted in the latest Espressif documentation. The PR does not use that library yet, but instead utilizes the older library used in the Espressif v5.2 I2C examples which is compatible with a wider range of ESP-IDF versions.

The included example directly references the /examples/native/native_test.c to demonstrate using the TPM on the ESP32.

Support for the newer I2C library as well as SPI will be submitted in separate future pull requests.

This library is expected to work with all flavors of the ESP32-[C2/C3/C6/S2S6, etc.], but has only been tested on the basic Xtensa ESP32 at this time.

Edit

This PR as-is appears to be working reliably. For future reference:

After I submitted this PR, I bought a few LetsTrust TPM for Raspberry Pi devices. The folks at https://letstrust.de/ reached out to me today with an interesting question & comment:

Could you check that Clockstreching is activated in the I2C master?

Clock stretching is mandatory for the SLB9673, also for 100kHz. with 400kHz you will see some transfer issues.

I did not explicitly enable any clock stretching.

See the api-reference/peripherals/i2c docs and espressif/esp-idf#4173 - in particular the comment from costaud:

The timeout for i2c master is similar to clock stretching, but different for i2c slave side.
Seems esp32 slave doesn't support hardware clock stretching. (which will be supported on esp32s2 and later chips)

and this one from stickbreaker:

the only way to extend the clock stretching hardware limit of 13ms is to reduce the processor clock speed. the maximum timeout period is 2**20 APB clock cycles. APB clock is either 80MHz or CPU clock, whichever is slower. But, WiFi/Bluetooth does not function if APB clock is less than 80MHz.

The ESP32 for this PR is the master I2C device, talking to the Infineon 9673.

Although I did not implement any clock stretching in this PR, there are some adjustable delays between write-then-read, and after retries:

#define I2C_MASTER_FREQ_HZ      100000
#define I2C_MASTER_TX_BUF_DISABLE   0
#define I2C_MASTER_RX_BUF_DISABLE   0
#define I2C_MASTER_TIMEOUT_MS   25000
#define WRITE_TO_READ_GUARD_TIME    2
#define POST_READ_GUARD_TIME        2
#define POST_WRITE_GUARD_TIME       2
#define READ_RETRY_DELAY_TIME       2
#define WRITE_RETRY_DELAY_TIME      2

Adding some details on my sdkconfig.h... all the defaults resulting in these settings that may be of interest:

#define CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ_240 1
#define CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ 240
#define CONFIG_SOC_I2C_SUPPORTED 1
#define CONFIG_ESPTOOLPY_FLASHMODE "dio"
#define CONFIG_ESPTOOLPY_FLASHFREQ_40M 1
#define CONFIG_ESPTOOLPY_FLASHFREQ "40m"
#define CONFIG_ESPTOOLPY_FLASHSIZE_2MB 1
#define CONFIG_ESPTOOLPY_FLASHSIZE "2MB"
#define CONFIG_I2C_MASTER_SCL 19
#define CONFIG_I2C_MASTER_SDA 18
#define CONFIG_FREERTOS_HZ 1000

See also I2C clock is not generate reliably after clock stretch

EXTRA_DIST+= IDE\Espressif\main\main.c
EXTRA_DIST+= IDE\Espressif\main\wrap_test.c
EXTRA_DIST+= IDE\Espressif\main\include\main.h
EXTRA_DIST+= IDE\Espressif\main\include\wrap_test.h
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing from repo

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, but not intended to be included: wrap_test was an early demo that I abandoned in favor of native test.

I've removed those lines from include.am.

# The example application.
EXTRA_DIST+= IDE\Espressif\main\CMakeLists.txt
EXTRA_DIST+= IDE\Espressif\main\main.c
EXTRA_DIST+= IDE\Espressif\main\wrap_test.c
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing from repo

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as prior one. good catch, but not intended to be included: wrap_test was an early demo that I abandoned in favor of native_test.

I've removed those lines from include.am.

EXTRA_DIST+= IDE\Espressif\components\wolftpm\CMakeLists.txt
EXTRA_DIST+= IDE\Espressif\components\wolftpm\include\options.h

# The example application.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to add IDE/Espressif/main/Kconfig.projbuild here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, thanks for that one.


# wolfSSL source code is not included here and must be available in separate directory.
EXTRA_DIST+= IDE\Espressif\components\wolfssl\CMakeLists.txt
EXTRA_DIST+= IDE\Espressif\components\wolfssl\include\config.h
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing from repo

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, good catch. this one was a bit more tricky as config.h files are excluded in .gitignore

EXTRA_DIST+= IDE\Espressif\partitions_singleapp_large.csv
EXTRA_DIST+= IDE\Espressif\README.md

# wolfSSL source code is not included here and must be available in separate directory.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to add IDE/Espressif/components/wolftpm/CMakeLists.txt and IDE/Espressif/components/wolftpm/include/README.md?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup. also missing. thank you.

@@ -0,0 +1,23 @@
# vim:ft=automake
# included from Top Level Makefile.am
# All paths should be given relative to the root
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to add IDE/Espressif/VisualGDB/wolfssl_IDF_v5.2_ESP32.vgdbproj ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, that was missing. too.

root cause of the pesky git actions error was an off-by-one keypress. I inadvertently had a $ for a comment in the new include.am instead of the adjacent key #.


/* I2C master i2c port number,
* the number of i2c peripheral interfaces available will depend on the chip */
#define I2C_MASTER_NUM 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be overridable at build time?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's a nice feature to add. I also added some range checking.

I have plans for Kconfig in a future PR.

hal/tpm_io.c Show resolved Hide resolved
@gojimmypi gojimmypi requested a review from dgarske May 13, 2024 18:46
@@ -452,6 +452,8 @@ typedef int64_t INT64;
#ifdef WIN32
#include <windows.h>
#define XSLEEP_MS(ms) Sleep(ms)
#elif defined(WOLFSSL_ESPIDF)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would prefer #elif defined(FREERTOS).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed using FREERTOS here is better. Updated.

@gojimmypi gojimmypi requested a review from dgarske May 13, 2024 20:06
@dgarske dgarske merged commit 2d64093 into wolfSSL:master May 13, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants