Skip to content

Commit da5a1ad

Browse files
authored
Merge pull request #172 from tomoveu/add-nuvoton-gpio
Add GPIO example for NPCT7xx
2 parents 6d15c2b + aa71cfd commit da5a1ad

10 files changed

Lines changed: 481 additions & 56 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ examples/nvram/read
5555
examples/gpio/gpio_config
5656
examples/gpio/gpio_set
5757
examples/gpio/gpio_read
58+
examples/gpio/gpio_nuvoton
5859
examples/seal/seal
5960
examples/seal/unseal
6061
examples/attestation/make_credential

examples/README.md

Lines changed: 62 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ The PKCS #7 and TLS examples require generating CSR's and signing them using a t
88

99
To enable parameter encryption use `-aes` for AES-CFB mode or `-xor` for XOR mode. Only some TPM commands / responses support parameter encryption. If the TPM2_ API has .flags `CMD_FLAG_ENC2` or `CMD_FLAG_DEC2` set then the command will use parameter encryption / decryption.
1010

11-
There are some vendor specific examples, like the TPM 2.0 extra GPIO examples for ST33.
11+
There are some vendor specific examples, like the TPM 2.0 extra GPIO examples for ST33 and NPCT75x.
1212

1313
## Native API Test
1414

@@ -427,22 +427,27 @@ mySecretMessage
427427

428428
After a successful unsealing, the data is stored into a new file. If no filename is provided, the `unseal` tool stores the data in `unseal.bin`.
429429

430-
## GPIO control
430+
431+
## GPIO Control
431432

432433
Some TPM 2.0 modules have extra I/O functionalities and additional GPIO that the developer could use. This extra GPIO could be used to signal other subsystems about security events or system states.
433434

434-
Currently, the GPIO control examples support only ST33 TPM 2.0 modules.
435+
Currently, the GPIO control examples support ST33 and NPCT75x TPM 2.0 modules.
435436

436-
There are three examples available: `gpio/gpio_config`, `gpio/gpio_set`, `gpio/gpio_read`.
437+
There are four examples available: `gpio/gpio_config` for ST33 and `gpio/gpio_nuvoton` for NPCT75x.
437438

438439
Every example has a help option `-h`. Please consult with `gpio_config -h` about the various GPIO modes.
439440

440-
Demo usage is available, when no parameters are supplied. Then, GPIO 0 is used in output mode.
441+
Once configured, a GPIO can be controlled using `gpio/gpio_set` and `gpio/gpio_read`.
441442

443+
Demo usage is available, when no parameters are supplied. Recommended is to use carefully selected options, because GPIO interact with the physical world.
442444

443-
```
445+
### GPIO Config
446+
447+
ST33 supports 6 modes, information from `gpio/gpio_config` below:
444448

445-
examples/gpio/gpio_config -h
449+
```
450+
$ ./examples/gpio/gpio_config -h
446451
Expected usage:
447452
./examples/gpio/gpio_config [num] [mode]
448453
* num is a GPIO number between 0-3 (default 0)
@@ -455,45 +460,81 @@ Expected usage:
455460
5. pushpull - output in push pull configuration
456461
6. unconfigure - delete the NV index for the selected GPIO
457462
Example usage, without parameters, configures GPIO0 as input with a pull down.
458-
459463
```
460464

461465
Example usage for configuring a GPIO to output can be found below:
462466

463467
```
464-
465-
$ ./examples/gpio/gpio_config
468+
$ ./examples/gpio/gpio_config 0 5
466469
GPIO num is: 0
467470
GPIO mode is: 5
468471
Example how to use extra GPIO on a TPM 2.0 modules
469-
wolfTPM2_Init: success
470472
Trying to configure GPIO0...
471473
TPM2_GPIO_Config success
472474
NV Index for GPIO access created
473-
474-
$ ./examples/gpio/gpio_set
475-
GPIO0 set to high level
476-
477475
```
478476

479-
Switching a GPIO configuration is seamless, because gpio/config takes care of deleting existing NV Index, so a new GPIO configuration can be chosen.
480-
481-
Example usage for configuring a GPIO as input with a pulp-up can be found below:
477+
Example usage for configuring a GPIO as input with a pull-up on ST33 can be found below:
482478

483479
```
484-
485480
$ ./examples/gpio/gpio_config 0 3
486481
GPIO num is: 0
487482
GPIO mode is: 3
488483
Demo how to use extra GPIO on a TPM 2.0 modules
489-
wolfTPM2_Init: success
490484
Trying to configure GPIO0...
491485
TPM2_GPIO_Config success
492486
NV Index for GPIO access created
487+
```
488+
489+
### GPIO Config (NPCT75xx)
490+
491+
NPCT75x supports 3 output modes, information from `gpio/gpio_nuvoton` below:
492+
493+
```
494+
$ ./examples/gpio/gpio_nuvoton -h
495+
Expected usage:
496+
./examples/gpio/gpio_nuvoton [num] [mode]
497+
* num is a GPIO number between 3 and 4 (default 3)
498+
* mode is either push-pull, open-drain or open-drain with pull-up
499+
1. pushpull - output in push pull configuration
500+
2. opendrain - output in open drain configuration
501+
3. pullup - output in open drain with pull-up enabled
502+
4. unconfig - delete NV index for GPIO access
503+
Example usage, without parameters, configures GPIO3 as push-pull output.
504+
```
505+
506+
Please note that NPCT75x GPIO numbering starts from GPIO3, while ST33 starts from GPIO0.
507+
508+
```
509+
$ ./examples/gpio/gpio_nuvoton 4 1
510+
Example for GPIO configuration of a NPTC7xx TPM 2.0 module
511+
GPIO number: 4
512+
GPIO mode: 1
513+
Successfully read the current configuration
514+
Successfully wrote new configuration
515+
NV Index for GPIO access created
516+
```
517+
518+
### GPIO Usage
519+
520+
Switching a GPIO configuration is seamless.
521+
* For ST33 `gpio/gpio_config` takes care of deleting existing NV Index, so a new GPIO configuration can be chosen.
522+
* For NPCT75xx `gpio/gpio_nuvoton` can reconfigure any GPIO without deleting the created NV index.
493523

524+
```
525+
$ ./examples/gpio/gpio_set 0 -high
526+
GPIO0 set to high level
527+
528+
$ ./examples/gpio/gpio_set 0 -low
529+
GPIO0 set to low level
530+
```
531+
532+
```
494533
$ ./examples/gpio/gpio_read 0
495534
GPIO0 is Low
496-
497535
```
498536

537+
538+
## Support
539+
499540
If you need more information about using these examples please contact us at support@wolfssl.com

examples/gpio/gpio.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,24 @@
2626
extern "C" {
2727
#endif
2828

29-
#if defined(WOLFTPM_ST33) || defined(WOLFTPM_AUTODETECT)
29+
#if defined(WOLFTPM_ST33) || defined(WOLFTPM_NUVOTON) || defined(WOLFTPM_AUTODETECT)
3030
#define GPIO_NUM_MIN TPM_GPIO_A
31-
#define GPIO_NUM_MAX (TPM_GPIO_COUNT-1) /* see wolftpm/tpm2.h */
31+
#define GPIO_NUM_MAX TPM_GPIO_A+TPM_GPIO_COUNT-1 /* see wolftpm/tpm2.h */
32+
#endif
33+
34+
#ifdef WOLFTPM_NUVOTON
35+
/* Nuvoton GPIO Modes - only output */
36+
#define NUVOTON_GPIO_MODE_PUSHPULL 1
37+
#define NUVOTON_GPIO_MODE_OPENDRAIN 2
38+
#define NUVOTON_GPIO_MODE_PULLUP 3
39+
#define NUVOTON_GPIO_MODE_UNCONFIG 4 /* Not a real GPIO mode, deleting NV index */
40+
#define NUVOTON_GPIO_MODE_MAX 4
3241
#endif
3342

3443
int TPM2_GPIO_Config_Example(void* userCtx, int argc, char *argv[]);
3544
int TPM2_GPIO_Read_Example(void* userCtx, int argc, char *argv[]);
3645
int TPM2_GPIO_Set_Example(void* userCtx, int argc, char *argv[]);
46+
int TPM2_GPIO_Nuvoton_Example(void* userCtx, int argc, char *argv[]);
3747

3848
#ifdef __cplusplus
3949
} /* extern "C" */

examples/gpio/gpio_config.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ int TPM2_GPIO_Config_Example(void* userCtx, int argc, char *argv[])
150150

151151
/* Enable TPM2_GPIO_Config command */
152152
if (caps.mfg != TPM_MFG_STM) {
153-
printf("Extra GPIO is supported only on ST33 TPM 2.0 modules\n");
153+
printf("TPM model mismatch. GPIO support requires an ST33 TPM 2.0 module\n");
154154
goto exit;
155155
}
156156

@@ -255,7 +255,7 @@ int main(int argc, char *argv[])
255255
#if defined(WOLFTPM_ST33) || defined(WOLFTPM_AUTODETECT)
256256
rc = TPM2_GPIO_Config_Example(NULL, argc, argv);
257257
#else
258-
printf("Extra GPIO is supported only on ST33 TPM 2.0 modules.\n");
258+
printf("GPIO configuration requires an ST33 TPM 2.0 module built with WOLFTPM_ST33 or --enable-st33\n");
259259
(void)argc;
260260
(void)argv;
261261
#endif /* WOLFTPM_ST33 || WOLFTPM_AUTODETECT */

0 commit comments

Comments
 (0)