Skip to content

Commit bfbbd2e

Browse files
authored
Merge pull request #368 from gojimmypi/espressif-spi
Add Espressif SPI support
2 parents 0a3b8de + 1e785d4 commit bfbbd2e

File tree

8 files changed

+264
-18
lines changed

8 files changed

+264
-18
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# wolfSSL as a Component in wolfTPM
2+
3+
This directory for wolfSSL is built-in to this wolfTPM example.
4+
5+
## Update
6+
7+
To update this component to the latest wolfSSL component, see the template reference example:
8+
9+
https://github.com/wolfSSL/wolfssl/tree/master/IDE/Espressif/ESP-IDF/examples/template
10+
11+
Copy the entire [components/wolfssl](https://github.com/wolfSSL/wolfssl/tree/master/IDE/Espressif/ESP-IDF/examples/template/components/wolfssl) here.
12+
13+
## Managed Component
14+
15+
To use a managed component, delete this `[project]/components/wolfssl` directory and use
16+
the Espressif Managed Component install.
17+
18+
See blog for more information:
19+
20+
https://www.wolfssl.com/wolfssl-now-available-in-espressif-component-registry/

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555
#define WOLFSSL_SHA3
5656
#endif
5757

58+
#define WOLFTPM_ADV_IO
59+
#define WOLFTPM_EXAMPLE_HAL
60+
#define WOLFTPM_INCLUDE_IO_FILE
61+
5862
/*
5963
* ONE of these Espressif chip families will be detected from sdkconfig:
6064
*
@@ -146,7 +150,7 @@
146150
#else
147151
#define HAVE_ECC
148152
#define HAVE_CURVE25519
149-
#define CURVE25519_SMALL
153+
/* #define CURVE25519_SMALL */
150154
#endif
151155

152156
#define HAVE_ED25519
@@ -643,7 +647,7 @@ Turn on timer debugging (used when CPU cycles not available)
643647

644648
/* Choices are I2C or SPI*/
645649
/* WOLFTPM_I2C or not; when not defined, assumes SPI. */
646-
#define WOLFTPM_I2C
650+
/* #define WOLFTPM_I2C */
647651

648652
/* Enable the wolfTPM example HAL in tpm_io.h */
649653
#define WOLFTPM_EXAMPLE_HAL
@@ -652,8 +656,10 @@ Turn on timer debugging (used when CPU cycles not available)
652656
#define WOLFTPM_INCLUDE_IO_FILE
653657

654658
/* The default TPM_TIMEOUT_TRIES is 1,000,000 but can be overridden.
655-
* A value of 10000 is much more appropriate for the ESP32: */
656-
#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
657663

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

IDE/Espressif/main/main.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,30 @@
5555
#define WOLFTPM_MAIN_TEST_ITERATIONS 1
5656
#endif
5757

58+
/* I2C tested with 35840 stack, SPI with 45840 on ESP32-C6 */
59+
#define MIN_TPM_TEST_STACK_SIZE 45840
60+
5861
static const char* const TAG = "wolfTPM main";
5962

6063
void app_main(void)
6164
{
6265
char mydata[1024];
6366
int tests = WOLFTPM_MAIN_TEST_ITERATIONS;
6467
esp_err_t ret = 0;
68+
#ifdef CONFIG_MAIN_TASK_STACK_SIZE
69+
if (CONFIG_MAIN_TASK_STACK_SIZE < MIN_TPM_TEST_STACK_SIZE) {
70+
ESP_LOGE(TAG, "Stack may be too small!");
71+
}
72+
#else
73+
ESP_LOGE(TAG, "No CONFIG_MAIN_TASK_STACK_SIZE defined?");
74+
#endif
75+
#ifdef CONFIG_ESP_MAIN_TASK_STACK_SIZE
76+
if (CONFIG_ESP_MAIN_TASK_STACK_SIZE < MIN_TPM_TEST_STACK_SIZE) {
77+
ESP_LOGE(TAG, "Stack may be too small!");
78+
}
79+
#else
80+
ESP_LOGE(TAG, "No CONFIG_ESP_MAIN_TASK_STACK_SIZE defined?");
81+
#endif
6582

6683
#ifdef LIBWOLFTPM_VERSION_STRING
6784
ESP_LOGI(TAG, "Hello wolfTPM version %s!", LIBWOLFTPM_VERSION_STRING);

IDE/Espressif/sdkconfig.defaults

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ CONFIG_ESP32_DEFAULT_CPU_FREQ_240=y
66
#
77
# This is typically way bigger than needed for stack size. See user_settings.h
88
#
9-
CONFIG_ESP_MAIN_TASK_STACK_SIZE=35840
9+
CONFIG_ESP_MAIN_TASK_STACK_SIZE=45840
1010

1111
# Legacy stack size for older ESP-IDF versions
12-
CONFIG_MAIN_TASK_STACK_SIZE=35840
12+
CONFIG_MAIN_TASK_STACK_SIZE=45840
1313

1414
#
1515
# Compiler options

hal/tpm_io.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ static int TPM2_IoCb_SPI(TPM2_CTX* ctx, const byte* txBuf, byte* rxBuf,
103103
ret = TPM2_IoCb_Infineon_TriCore_SPI(ctx, txBuf, rxBuf, xferSz, userCtx);
104104
#elif defined(WOLFTPM_MICROCHIP_HARMONY)
105105
ret = TPM2_IoCb_Microchip_SPI(ctx, txBuf, rxBuf, xferSz, userCtx);
106+
#elif defined(WOLFSSL_ESPIDF)
107+
ret = TPM2_IoCb_Espressif_SPI(ctx, txBuf, rxBuf, xferSz, userCtx);
106108
#else
107109

108110
/* TODO: Add your platform here for HW SPI interface */

hal/tpm_io_espressif.c

Lines changed: 201 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242

4343
/* Espressif */
4444
#include "sdkconfig.h"
45+
#include <driver/gpio.h>
46+
#include <driver/spi_master.h>
4547

4648
#define TAG "TPM_IO"
4749

@@ -165,7 +167,7 @@
165167
#define TPM_I2C_TRIES 10
166168
#endif
167169

168-
static int _is_initialized_i2c = FALSE;
170+
static int _is_initialized_i2c = 0;
169171

170172
#ifdef DEBUG_WOLFSSL_VERBOSE
171173
static esp_err_t show_binary(byte* theVar, size_t dataSz) {
@@ -238,7 +240,7 @@ static esp_err_t i2c_master_delete(void)
238240
{
239241
ESP_LOGI(TAG, "i2c_master_delete");
240242
ESP_ERROR_CHECK(i2c_driver_delete(I2C_MASTER_NUM));
241-
_is_initialized_i2c = FALSE;
243+
_is_initialized_i2c = 0;
242244
return ESP_OK;
243245
}
244246

@@ -438,20 +440,209 @@ int TPM2_IoCb_Espressif_I2C(TPM2_CTX* ctx, int isRead, word32 addr,
438440
/* end WOLFTPM_I2C */
439441

440442
#else /* If not I2C, it must be SPI */
441-
/* TODO implement SPI */
443+
/*---------------------------------------------------------------------------*/
442444

443-
#ifndef TPM2_SPI_HZ
444-
/* Use the max speed by default
445-
* See tpm2_types.h for chip specific max values */
446-
#define TPM2_SPI_HZ TPM2_SPI_MAX_HZ
445+
/* Note: If an error is encountered during SPI transfer:
446+
* TPM2_StartAuthSession failed 0x903: TPM_RC_SESSION_MEMORY
447+
* Try hardware reset of the TPM device. */
448+
449+
#define SPI_CLOCK_MHZ (10 * 1000 * 1000) /* 10MHz, but tested up to 22MHz */
450+
451+
/* If failures are encountered, try hardware reset of TPM device. */
452+
#if defined(CONFIG_IDF_TARGET_ESP32S3)
453+
#ifndef ESP_SPI_MAX_TRANSFER
454+
#define ESP_SPI_MAX_TRANSFER_SIZE MAX_SPI_FRAMESIZE + 4
455+
#endif
456+
/* FSPI (HOST_SPI2) on esp32-s3-wroom */
457+
#ifndef PIN_NUM_MISO
458+
#define PIN_NUM_MISO 13
459+
#endif
460+
#ifndef PIN_NUM_MOSI
461+
#define PIN_NUM_MOSI 11
462+
#endif
463+
#ifndef PIN_NUM_CLK
464+
#define PIN_NUM_CLK 12
465+
#endif
466+
#ifndef PIN_NUM_CS
467+
#define PIN_NUM_CS 10
468+
#endif
469+
#elif defined(CONFIG_IDF_TARGET_ESP32C6)
470+
#ifndef ESP_SPI_MAX_TRANSFER
471+
#define ESP_SPI_MAX_TRANSFER_SIZE 1024
472+
#endif
473+
#ifndef PIN_NUM_MISO
474+
#define PIN_NUM_MISO 2
475+
#endif
476+
#ifndef PIN_NUM_MOSI
477+
#define PIN_NUM_MOSI 7
478+
#endif
479+
#ifndef PIN_NUM_CLK
480+
#define PIN_NUM_CLK 6
481+
#endif
482+
#ifndef PIN_NUM_CS
483+
/* Set to 10 on ESP32-C6 despite documentation that states 16
484+
* Tested on ESP32-C6-DevKitC-1 */
485+
#define PIN_NUM_CS 10
486+
/* Allow software control */
487+
#define PIN_NUM_SPICS_IO -1
488+
#endif
489+
#else
490+
#ifndef ESP_SPI_MAX_TRANSFER
491+
#define ESP_SPI_MAX_TRANSFER_SIZE MAX_SPI_FRAMESIZE
492+
#endif
493+
#ifndef PIN_NUM_MISO
494+
#error PIN_NUM_MISO undefined
447495
#endif
448-
#ifdef WOLFTPM_CHECK_WAIT_STATE
449-
#error SPI check wait state logic not supported
496+
#ifndef PIN_NUM_MOSI
497+
#error PIN_NUM_MOSI undefined
450498
#endif
499+
#ifndef PIN_NUM_CLK
500+
#error PIN_NUM_CLK undefined
501+
#endif
502+
#ifndef PIN_NUM_CS
503+
#error PIN_NUM_CS undefined
504+
#endif
505+
#endif
451506

452-
#error TPM2 SPI support on this platform not supported yet
507+
/* Set spi_device_interface_config_t.spics_io_num = PIN_NUM_SPICS_IO
508+
* to -1 to let software control.
509+
* to CS Pin = PIN_NUM_CS for hardware control.
510+
* See tpm_spi_acquire() and tpm_spi_release()
511+
* Software control still requires CS pin */
512+
#ifndef PIN_NUM_SPICS_IO
513+
#define PIN_NUM_SPICS_IO PIN_NUM_CS
453514
#endif
454515

516+
/* TPM data storing SPI handles & timeouts */
517+
static struct TPM_DATA {
518+
spi_device_handle_t spi;
519+
gpio_num_t cs_pin;
520+
int64_t timeout_expiry;
521+
} *tpm_data;
522+
523+
static int _is_initialized_spi = 0;
524+
525+
static int esp_spi_master_init(void)
526+
{
527+
/* SPI bus & device configuration */
528+
const spi_bus_config_t bus_cfg = {
529+
.miso_io_num = PIN_NUM_MISO,
530+
.mosi_io_num = PIN_NUM_MOSI,
531+
.sclk_io_num = PIN_NUM_CLK,
532+
.quadwp_io_num = -1,
533+
.quadhd_io_num = -1,
534+
.max_transfer_sz = ESP_SPI_MAX_TRANSFER_SIZE
535+
};
536+
const spi_device_interface_config_t dev_cfg = {
537+
.clock_speed_hz = SPI_CLOCK_MHZ,
538+
.mode = 0,
539+
.spics_io_num = PIN_NUM_SPICS_IO, /* -1 for SW or PIN_NUM_CS */
540+
.queue_size = 1,
541+
.pre_cb = NULL,
542+
.post_cb = NULL,
543+
};
544+
esp_err_t ret;
545+
546+
/* Initializing CS pin */
547+
esp_rom_gpio_pad_select_gpio(PIN_NUM_CS);
548+
gpio_set_direction(PIN_NUM_CS, GPIO_MODE_OUTPUT);
549+
gpio_set_level(PIN_NUM_CS, 1);
550+
551+
/* Initialize the SPI bus and device */
552+
ret = spi_bus_initialize(SPI2_HOST, &bus_cfg, 0);
553+
ESP_ERROR_CHECK(ret);
554+
555+
/* Attach the device to the SPI bus */
556+
spi_device_handle_t spi;
557+
ret = spi_bus_add_device(SPI2_HOST, &dev_cfg, &spi);
558+
ESP_ERROR_CHECK(ret);
559+
560+
tpm_data = malloc(sizeof(struct TPM_DATA));
561+
tpm_data->spi = spi;
562+
tpm_data->cs_pin = PIN_NUM_CS;
563+
tpm_data->timeout_expiry = 0;
564+
565+
_is_initialized_spi = TRUE;
566+
return 0;
567+
}
568+
569+
/* Aquire SPI bus and keep pulling CS */
570+
static int tpm_spi_acquire(void)
571+
{
572+
int ret;
573+
gpio_set_level(tpm_data->cs_pin, 0);
574+
ret = spi_device_acquire_bus(tpm_data->spi, portMAX_DELAY);
575+
return ret;
576+
}
577+
578+
/* Release SPI bus and CS */
579+
int tpm_spi_release (void)
580+
{
581+
gpio_set_level(tpm_data->cs_pin, 1);
582+
spi_device_release_bus(tpm_data->spi);
583+
return 0;
584+
}
585+
586+
int tpm_spi_raw_transfer (const byte *data_out, byte *data_in, size_t cnt)
587+
{
588+
spi_transaction_t t;
589+
esp_err_t ret;
590+
591+
/* Maximum transfer size is 64 byte because we don't use DMA. */
592+
if (cnt > MAX_SPI_FRAMESIZE + ESP_SPI_ADDITIONAL_FRAME_BUFFER) {
593+
ESP_LOGE(TAG, "tpm_io_espressif: cnt %d larger than framesize\n", cnt);
594+
return -1;
595+
}
596+
597+
/* At least one of the buffers has to be set. */
598+
if (data_out == NULL && data_in == NULL) {
599+
return -1;
600+
}
601+
602+
/* Setup transaction */
603+
memset(&t, 0, sizeof(t));
604+
t.length = cnt*8;
605+
t.tx_buffer = data_out;
606+
t.rx_buffer = data_in;
607+
608+
/* Transmit */
609+
ret = spi_device_polling_transmit(tpm_data->spi, &t);
610+
if (ret != ESP_OK) {
611+
ESP_LOGE(TAG, "spi_transmit returned error %d\n", ret);
612+
return -1;
613+
}
614+
615+
return 0;
616+
} /* tpm_spi_raw_transfer */
617+
618+
int TPM2_IoCb_Espressif_SPI(TPM2_CTX* ctx, const byte* txBuf, byte* rxBuf,
619+
word16 xferSz, void* userCtx)
620+
{
621+
(void)ctx;
622+
int ret = TPM_RC_FAILURE;
623+
624+
if (_is_initialized_spi) {
625+
ret = ESP_OK;
626+
}
627+
else {
628+
ret = esp_spi_master_init(); /* ESP return code, not TPM */
629+
ESP_LOGV(TAG, "HAL: Initializing SPI %d", ret);
630+
}
631+
632+
if (ret == ESP_OK) {
633+
tpm_spi_acquire();
634+
ret = tpm_spi_raw_transfer(txBuf, rxBuf, xferSz);
635+
tpm_spi_release();
636+
}
637+
else {
638+
ESP_LOGE(TAG, "SPI Failed to initialize. Error: %d", ret);
639+
ret = TPM_RC_FAILURE;
640+
}
641+
642+
return ret;
643+
} /* TPM2_IoCb_Espressif_SPI */
644+
645+
#endif /* Espressif SPI */
455646
#endif /* WOLFSSL_ESPIDF */
456647
#endif /* WOLFTPM_INCLUDE_IO_FILE */
457648

src/tpm2_tis.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
#include <wolftpm/tpm2_tis.h>
2727

28-
2928
/******************************************************************************/
3029
/* --- BEGIN TPM Interface Specification (TIS) Layer */
3130
/******************************************************************************/
@@ -405,6 +404,7 @@ int TPM2_TIS_GetBurstCount(TPM2_CTX* ctx, word16* burstCount)
405404
}
406405
else
407406
#endif
407+
408408
{
409409
int timeout = TPM_TIMEOUT_TRIES;
410410
*burstCount = 0;

0 commit comments

Comments
 (0)