@@ -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 */
462509static 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
0 commit comments