Skip to content

Commit 039187e

Browse files
committed
Fix TPM2_CreatePrimary: Endorsement timeout, polish
1 parent 8b9bb70 commit 039187e

File tree

3 files changed

+73
-22
lines changed

3 files changed

+73
-22
lines changed

IDE/Espressif/components/wolfssl/include/user_settings.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ Turn on timer debugging (used when CPU cycles not available)
647647

648648
/* Choices are I2C or SPI*/
649649
/* WOLFTPM_I2C or not; when not defined, assumes SPI. */
650-
#define WOLFTPM_I2C
650+
/* #define WOLFTPM_I2C */
651651

652652
/* Enable the wolfTPM example HAL in tpm_io.h */
653653
#define WOLFTPM_EXAMPLE_HAL
@@ -656,8 +656,10 @@ Turn on timer debugging (used when CPU cycles not available)
656656
#define WOLFTPM_INCLUDE_IO_FILE
657657

658658
/* The default TPM_TIMEOUT_TRIES is 1,000,000 but can be overridden.
659-
* A value of 10000 is much more appropriate for the ESP32: */
660-
#define TPM_TIMEOUT_TRIES 10000
659+
* A value of 10000 is much more appropriate for the ESP32.
660+
* The extra long delay is for TPM2_CreatePrimary: Endorsement operations,
661+
* which can take up to 60 seconds */
662+
#define TPM_TIMEOUT_TRIES 300000
661663

662664
/* If not defined here, TPM_I2C_TRIES is set to a default value of 10 */
663665
/* #define TPM_I2C_TRIES 10 */

hal/tpm_io_espressif.c

Lines changed: 66 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -440,23 +440,70 @@ int TPM2_IoCb_Espressif_I2C(TPM2_CTX* ctx, int isRead, word32 addr,
440440
/* end WOLFTPM_I2C */
441441

442442
#else /* If not I2C, it must be SPI */
443-
444-
/* FSPI (HOST_SPI2) on esp32-s3-wroom */
445-
#ifndef PIN_NUM_MISO
446-
#define PIN_NUM_MISO 13
447-
#endif
448-
#ifndef PIN_NUM_MOSI
449-
#define PIN_NUM_MOSI 11
450-
#endif
451-
#ifndef PIN_NUM_CLK
452-
#define PIN_NUM_CLK 12
453-
#endif
454-
#ifndef PIN_NUM_CS
455-
#define PIN_NUM_CS 10
443+
/*---------------------------------------------------------------------------*/
444+
445+
#define SPI_CLOCK_MHZ (10 * 1000 * 1000) /* 10MHz, but tested up to 22MHz */
446+
447+
/* If failures are encountered, try hardware reset of TPM device. */
448+
#if defined(CONFIG_IDF_TARGET_ESP32S3)
449+
/* NOTE: on esp, 64 byte limit includes data and header!!! */
450+
#define SPI_MAX_TRANSFER 64
451+
452+
/* FSPI (HOST_SPI2) on esp32-s3-wroom */
453+
#ifndef PIN_NUM_MISO
454+
#define PIN_NUM_MISO 13
455+
#endif
456+
#ifndef PIN_NUM_MOSI
457+
#define PIN_NUM_MOSI 11
458+
#endif
459+
#ifndef PIN_NUM_CLK
460+
#define PIN_NUM_CLK 12
461+
#endif
462+
#ifndef PIN_NUM_CS
463+
#define PIN_NUM_CS 10
464+
#endif
465+
#elif defined(CONFIG_IDF_TARGET_ESP32C6)
466+
#define SPI_MAX_TRANSFER 1024
467+
#ifndef PIN_NUM_MISO
468+
#define PIN_NUM_MISO 2 /* orange */
469+
#endif
470+
#ifndef PIN_NUM_MOSI
471+
#define PIN_NUM_MOSI 7 /* yellow */
472+
#endif
473+
#ifndef PIN_NUM_CLK
474+
#define PIN_NUM_CLK 6 /* gray */
475+
#endif
476+
#ifndef PIN_NUM_CS
477+
/* Set to 10 on ESP32-C6 despite documentation that states 16
478+
* Tested on ESP32-C6-DevKitC-1 */
479+
#define PIN_NUM_CS 10
480+
/* Allow software control */
481+
#define PIN_NUM_SPICS_IO -1
482+
#endif
483+
#else
484+
#define SPI_MAX_TRANSFER 64
485+
#ifndef PIN_NUM_MISO
486+
#error PIN_NUM_MISO undefined
487+
#endif
488+
#ifndef PIN_NUM_MOSI
489+
#error PIN_NUM_MOSI undefined
490+
#endif
491+
#ifndef PIN_NUM_CLK
492+
#error PIN_NUM_CLK undefined
493+
#endif
494+
#ifndef PIN_NUM_CS
495+
#error PIN_NUM_CS undefined
496+
#endif
456497
#endif
457498

458-
/* NOTE: on esp, 64 byte limit includes data and header!!! */
459-
#define SPI_MAX_TRANSFER 64
499+
/* Set spi_device_interface_config_t.spics_io_num = PIN_NUM_SPICS_IO
500+
* to -1 to let software control.
501+
* to CS Pin = PIN_NUM_CS for hardware control.
502+
* See tpm_spi_acquire() and tpm_spi_release()
503+
* Software control still requires CS pin */
504+
#ifndef PIN_NUM_SPICS_IO
505+
#define PIN_NUM_SPICS_IO PIN_NUM_CS
506+
#endif
460507

461508
/* TPM data storing SPI handles & timeouts */
462509
static struct TPM_DATA {
@@ -475,12 +522,12 @@ static int esp_spi_master_init() {
475522
.sclk_io_num = PIN_NUM_CLK,
476523
.quadwp_io_num = -1,
477524
.quadhd_io_num = -1,
478-
.max_transfer_sz = 64
525+
.max_transfer_sz = SPI_MAX_TRANSFER
479526
};
480527
const spi_device_interface_config_t dev_cfg = {
481-
.clock_speed_hz = 10*1000*1000, /* 10MHz, but tested up to 22MHz */
528+
.clock_speed_hz = SPI_CLOCK_MHZ,
482529
.mode = 0,
483-
.spics_io_num = PIN_NUM_CS,
530+
.spics_io_num = PIN_NUM_SPICS_IO, /* -1 for SW or PIN_NUM_CS */
484531
.queue_size = 1,
485532
.pre_cb = NULL,
486533
.post_cb = NULL,
@@ -531,7 +578,7 @@ int tpm_spi_raw_transfer (const byte *data_out, byte *data_in, size_t cnt) {
531578

532579
/* Maximum transfer size is 64 byte because we don't use DMA. */
533580
if (cnt > SPI_MAX_TRANSFER) {
534-
printf("tpm_io_espressif: cnt %d\n", cnt);
581+
ESP_LOGE(TAG, "tpm_io_espressif: cnt %d\n", cnt);
535582
return -1;
536583
}
537584

wolftpm/tpm2_types.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,8 @@ typedef int64_t INT64;
493493
#endif
494494
#include <unistd.h>
495495
#define XTPM_WAIT() usleep(XTPM_WAIT_POLLING_US);
496+
#elif defined(WOLFSSL_ESPIDF)
497+
#define XTPM_WAIT() vTaskDelay(pdMS_TO_TICKS(1))
496498
#endif
497499
#ifndef XTPM_WAIT
498500
#define XTPM_WAIT() /* just poll without delay by default */

0 commit comments

Comments
 (0)