Skip to content

Commit

Permalink
Merge pull request wolfSSL#302 from dgarske/stm32_i2c
Browse files Browse the repository at this point in the history
Fixes for STM32 I2C
  • Loading branch information
jpbland1 authored Oct 6, 2023
2 parents 29634c2 + 5226df3 commit 241f554
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 29 deletions.
5 changes: 4 additions & 1 deletion IDE/STM32CUBE/wolftpm_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
* along with wolfTPM. If not, see <http://www.gnu.org/licenses/>.
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "wolftpm_test.h"

#ifndef SINGLE_THREADED
Expand Down Expand Up @@ -45,7 +48,7 @@ void wolfTPMTest(const void* argument)
printf("Running wolfTPM Wrap Test...\n");

/* Run wolfTPM wrap test */
ret = TPM2_Wrapper_Test(NULL);
ret = TPM2_Wrapper_Test(argument);

printf("wolfTPM wrap test: Return code %d\n", ret);

Expand Down
4 changes: 1 addition & 3 deletions IDE/STM32CUBE/wolftpm_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
#ifndef WOLFTPM_TEST_H_
#define WOLFTPM_TEST_H_

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <wolftpm/tpm2_types.h>

#ifndef SINGLE_THREADED
#include <cmsis_os.h>
Expand Down
2 changes: 1 addition & 1 deletion hal/tpm_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
#ifdef WOLFTPM_EXAMPLE_HAL

#ifdef WOLFTPM_ADV_IO
WOLFTPM_API int TPM2_IoCb(TPM2_CTX*, int isRead, word32 addr, byte* buf, word16 size,
WOLFTPM_API int TPM2_IoCb(TPM2_CTX* ctx, int isRead, word32 addr, byte* buf, word16 size,
void* userCtx);
#else
WOLFTPM_API int TPM2_IoCb(TPM2_CTX* ctx, const byte* txBuf, byte* rxBuf,
Expand Down
2 changes: 1 addition & 1 deletion hal/tpm_io_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
int rc;
struct i2c_rdwr_ioctl_data rdwr;
struct i2c_msg msgs[2];
unsigned char buf[2];
unsigned char buf[1];
int timeout = TPM_I2C_TRIES;

rdwr.msgs = msgs;
Expand Down
85 changes: 62 additions & 23 deletions hal/tpm_io_st.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,54 +38,93 @@
defined(WOLFTPM_WINAPI) )

#if defined(WOLFSSL_STM32_CUBEMX)
#ifdef WOLFTPM_I2C
#ifdef WOLFTPM_I2C
#ifndef TPM_I2C_TRIES
#define TPM_I2C_TRIES 10
#endif
#ifndef TPM2_I2C_ADDR
#define TPM2_I2C_ADDR 0x2e
#endif
#ifndef STM32_CUBEMX_I2C_TIMEOUT
#define STM32_CUBEMX_I2C_TIMEOUT 250 /* ticks/ms */
#endif

/* STM32 CubeMX HAL I2C */
#define STM32_CUBEMX_I2C_TIMEOUT 250
static int i2c_read(void* userCtx, word32 reg, byte* data, int len)
{
int rc;
int i2cAddr = (TPM2_I2C_ADDR << 1) | 0x01; /* For I2C read LSB is 1 */
byte buf[MAX_SPI_FRAMESIZE+1];
int ret = TPM_RC_FAILURE;
HAL_StatusTypeDef status;
I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)userCtx;
int i2cAddr = (TPM2_I2C_ADDR << 1) | 0x01; /* For I2C read LSB is 1 */
int timeout = TPM_I2C_TRIES;
byte buf[1];

/* TIS layer should never provide a buffer larger than this,
but double check for good coding practice */
if (len > MAX_SPI_FRAMESIZE)
return BAD_FUNC_ARG;

buf[0] = (reg & 0xFF);
rc = HAL_I2C_Master_Receive(&hi2c, i2cAddr, data, len, STM32_CUBEMX_I2C_TIMEOUT);

if (rc != -1) {
XMEMCPY(data, buf+1, len);
return TPM_RC_SUCCESS;
buf[0] = (reg & 0xFF); /* convert to simple 8-bit address for I2C */

/* The I2C takes about 80us to wake up and will NAK until it is ready */
do {
/* Write address to read from - retry until ack */
status = HAL_I2C_Master_Transmit(hi2c, i2cAddr, buf, sizeof(buf),
STM32_CUBEMX_I2C_TIMEOUT);
HAL_Delay(1); /* guard time - should be 250us */
} while (status != HAL_OK && --timeout > 0);
if (status == HAL_OK) {
timeout = TPM_I2C_TRIES;
/* Perform read with retry */
do {
status = HAL_I2C_Master_Receive(hi2c, i2cAddr, data, len,
STM32_CUBEMX_I2C_TIMEOUT);
if (status != HAL_OK)
HAL_Delay(1); /* guard time - should be 250us */
} while (status != HAL_OK && --timeout > 0);
}

return TPM_RC_FAILURE;
if (status == HAL_OK) {
ret = TPM_RC_SUCCESS;
}
else {
printf("I2C Read failure %d (tries %d)\n",
status, TPM_I2C_TRIES - timeout);
}
return ret;
}

static int i2c_write(void* userCtx, word32 reg, byte* data, int len)
{
int rc;
int ret = TPM_RC_FAILURE;
HAL_StatusTypeDef status;
I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)userCtx;
int i2cAddr = (TPM2_I2C_ADDR << 1); /* I2C write operation, LSB is 0 */
int timeout = TPM_I2C_TRIES;
byte buf[MAX_SPI_FRAMESIZE+1];
I2C_HandleTypeDef* hi2c = (I2C_HandleTypeDef*)userCtx;

/* TIS layer should never provide a buffer larger than this,
but double check for good coding practice */
if (len > MAX_SPI_FRAMESIZE)
return BAD_FUNC_ARG;

buf[0] = (reg & 0xFF); /* TPM register address */
/* Build packet with TPM register and data */
buf[0] = (reg & 0xFF); /* convert to simple 8-bit address for I2C */
XMEMCPY(buf + 1, data, len);
rc = HAL_I2C_Master_Transmit(&hi2c, TPM2_I2C_ADDR << 1, buf, len);

if (rc != -1) {
return TPM_RC_SUCCESS;
/* The I2C takes about 80us to wake up and will NAK until it is ready */
do {
status = HAL_I2C_Master_Transmit(hi2c, i2cAddr, buf, len+1,
STM32_CUBEMX_I2C_TIMEOUT);
if (status != HAL_OK)
HAL_Delay(1); /* guard time - should be 250us */
} while (status != HAL_OK && --timeout > 0);
if (status == HAL_OK) {
ret = TPM_RC_SUCCESS;
}

return TPM_RC_FAILURE;
else {
printf("I2C Write failure %d\n", status);
}
return ret;
}

int TPM2_IoCb_STCubeMX_I2C(TPM2_CTX* ctx, int isRead, word32 addr,
Expand All @@ -105,7 +144,7 @@
return ret;
}

#else /* STM32 CubeMX Hal SPI */
#else /* STM32 CubeMX Hal SPI */
#define STM32_CUBEMX_SPI_TIMEOUT 250
int TPM2_IoCb_STCubeMX_SPI(TPM2_CTX* ctx, const byte* txBuf, byte* rxBuf,
word16 xferSz, void* userCtx)
Expand Down Expand Up @@ -178,7 +217,7 @@

return ret;
}
#endif /* WOLFTPM_I2C */
#endif /* WOLFTPM_I2C */
#endif
#endif /* !(WOLFTPM_LINUX_DEV || WOLFTPM_SWTPM || WOLFTPM_WINAPI) */
#endif /* WOLFTPM_INCLUDE_IO_FILE */
Expand Down

0 comments on commit 241f554

Please sign in to comment.