From 0f2e8f0b01edd1a0389426b316f491acf49af596 Mon Sep 17 00:00:00 2001 From: Giovanni Di Sirio Date: Sun, 7 Oct 2018 08:56:43 +0000 Subject: TRNG driver working. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@12343 110e8d01-0319-4d1e-a829-52ad28d1bb01 --- demos/STM32/RT-STM32L476-DISCOVERY/cfg/mcuconf.h | 5 +++ demos/STM32/RT-STM32L476RG-NUCLEO64/mcuconf.h | 5 +++ os/hal/ports/STM32/LLD/RNGv1/hal_trng_lld.c | 2 +- readme.txt | 3 +- testex/STM32/STM32L4xx/SPI-L3GD20/mcuconf.h | 5 +++ testhal/STM32/STM32L4xx/ADC/mcuconf.h | 5 +++ testhal/STM32/STM32L4xx/CAN/mcuconf.h | 5 +++ testhal/STM32/STM32L4xx/GPT-ADC/mcuconf.h | 5 +++ testhal/STM32/STM32L4xx/IRQ_STORM/mcuconf.h | 5 +++ testhal/STM32/STM32L4xx/WSPI-N25Q128/mcuconf.h | 5 +++ .../multi/PAL/cfg/stm32l476_discovery/mcuconf.h | 5 +++ .../multi/SPI/cfg/stm32l476_discovery/mcuconf.h | 5 +++ ...Select ELF file)(OpenOCD, Flash and Run).launch | 2 +- testhal/STM32/multi/TRNG/main.c | 48 ++++++++++++++++++++++ .../STM32/multi/TRNG/make/stm32l476_discovery.make | 3 +- .../USB_CDC/cfg/stm32l476_discovery/mcuconf.h | 5 +++ .../WSPI-MFS/cfg/stm32l476_discovery/mcuconf.h | 5 +++ .../conf/mcuconf_stm32l476xx/mcuconf.h.ftl | 5 +++ 18 files changed, 119 insertions(+), 4 deletions(-) diff --git a/demos/STM32/RT-STM32L476-DISCOVERY/cfg/mcuconf.h b/demos/STM32/RT-STM32L476-DISCOVERY/cfg/mcuconf.h index c61614ea7..1a60ff9a7 100644 --- a/demos/STM32/RT-STM32L476-DISCOVERY/cfg/mcuconf.h +++ b/demos/STM32/RT-STM32L476-DISCOVERY/cfg/mcuconf.h @@ -277,6 +277,11 @@ #define STM32_ST_IRQ_PRIORITY 8 #define STM32_ST_USE_TIMER 2 +/* + * TRNG driver system settings. + */ +#define STM32_TRNG_USE_RNG1 FALSE + /* * UART driver system settings. */ diff --git a/demos/STM32/RT-STM32L476RG-NUCLEO64/mcuconf.h b/demos/STM32/RT-STM32L476RG-NUCLEO64/mcuconf.h index 9d1884888..baba1fb9d 100644 --- a/demos/STM32/RT-STM32L476RG-NUCLEO64/mcuconf.h +++ b/demos/STM32/RT-STM32L476RG-NUCLEO64/mcuconf.h @@ -277,6 +277,11 @@ #define STM32_ST_IRQ_PRIORITY 8 #define STM32_ST_USE_TIMER 2 +/* + * TRNG driver system settings. + */ +#define STM32_TRNG_USE_RNG1 FALSE + /* * UART driver system settings. */ diff --git a/os/hal/ports/STM32/LLD/RNGv1/hal_trng_lld.c b/os/hal/ports/STM32/LLD/RNGv1/hal_trng_lld.c index ac6d39838..b4a780d90 100644 --- a/os/hal/ports/STM32/LLD/RNGv1/hal_trng_lld.c +++ b/os/hal/ports/STM32/LLD/RNGv1/hal_trng_lld.c @@ -152,7 +152,7 @@ bool trng_lld_generate(TRNGDriver *trngp, size_t size, uint8_t *out) { /* Waiting for a random number in data register.*/ tmo = STM32_DATA_FETCH_ATTEMPTS; - while ((tmo > 0) && ((trngp->rng->SR & RNG_SR_DRDY) != 0)) { + while ((tmo > 0) && ((trngp->rng->SR & RNG_SR_DRDY) == 0)) { tmo--; if (tmo == 0) { return true; diff --git a/readme.txt b/readme.txt index 7bdef6b3f..516325898 100644 --- a/readme.txt +++ b/readme.txt @@ -85,7 +85,8 @@ to eight data lines so "quad" was no more appropriate. - NEW: Added a new SIO driver model to HAL, it is a low level abstraction of an UART. -- NEW: Independent TRNG driver model added to HAL. +- NEW: Independent TRNG driver model added to HAL. Implemented a RNGv1 driver + for STM32, added test application. - NEW: Added a new "pipes" subsystem to the OS library. - NEW: Added mcuconf.h generators for STM32L432xx, STM32L476xx, STM32L496xx, STM32L4R5xx, STM32F72x/73x, STM32F746/756 and STM32F76x/77x devices. diff --git a/testex/STM32/STM32L4xx/SPI-L3GD20/mcuconf.h b/testex/STM32/STM32L4xx/SPI-L3GD20/mcuconf.h index 302bbdb1d..61bc898a6 100644 --- a/testex/STM32/STM32L4xx/SPI-L3GD20/mcuconf.h +++ b/testex/STM32/STM32L4xx/SPI-L3GD20/mcuconf.h @@ -277,6 +277,11 @@ #define STM32_ST_IRQ_PRIORITY 8 #define STM32_ST_USE_TIMER 2 +/* + * TRNG driver system settings. + */ +#define STM32_TRNG_USE_RNG1 FALSE + /* * UART driver system settings. */ diff --git a/testhal/STM32/STM32L4xx/ADC/mcuconf.h b/testhal/STM32/STM32L4xx/ADC/mcuconf.h index 794d15778..dd2ad2869 100644 --- a/testhal/STM32/STM32L4xx/ADC/mcuconf.h +++ b/testhal/STM32/STM32L4xx/ADC/mcuconf.h @@ -277,6 +277,11 @@ #define STM32_ST_IRQ_PRIORITY 8 #define STM32_ST_USE_TIMER 2 +/* + * TRNG driver system settings. + */ +#define STM32_TRNG_USE_RNG1 FALSE + /* * UART driver system settings. */ diff --git a/testhal/STM32/STM32L4xx/CAN/mcuconf.h b/testhal/STM32/STM32L4xx/CAN/mcuconf.h index f1f97e475..dacd19d6c 100644 --- a/testhal/STM32/STM32L4xx/CAN/mcuconf.h +++ b/testhal/STM32/STM32L4xx/CAN/mcuconf.h @@ -277,6 +277,11 @@ #define STM32_ST_IRQ_PRIORITY 8 #define STM32_ST_USE_TIMER 2 +/* + * TRNG driver system settings. + */ +#define STM32_TRNG_USE_RNG1 FALSE + /* * UART driver system settings. */ diff --git a/testhal/STM32/STM32L4xx/GPT-ADC/mcuconf.h b/testhal/STM32/STM32L4xx/GPT-ADC/mcuconf.h index 5775b917e..4bb6a40e6 100644 --- a/testhal/STM32/STM32L4xx/GPT-ADC/mcuconf.h +++ b/testhal/STM32/STM32L4xx/GPT-ADC/mcuconf.h @@ -277,6 +277,11 @@ #define STM32_ST_IRQ_PRIORITY 8 #define STM32_ST_USE_TIMER 2 +/* + * TRNG driver system settings. + */ +#define STM32_TRNG_USE_RNG1 FALSE + /* * UART driver system settings. */ diff --git a/testhal/STM32/STM32L4xx/IRQ_STORM/mcuconf.h b/testhal/STM32/STM32L4xx/IRQ_STORM/mcuconf.h index 5359f99d3..94f048bd8 100644 --- a/testhal/STM32/STM32L4xx/IRQ_STORM/mcuconf.h +++ b/testhal/STM32/STM32L4xx/IRQ_STORM/mcuconf.h @@ -277,6 +277,11 @@ #define STM32_ST_IRQ_PRIORITY 8 #define STM32_ST_USE_TIMER 2 +/* + * TRNG driver system settings. + */ +#define STM32_TRNG_USE_RNG1 FALSE + /* * UART driver system settings. */ diff --git a/testhal/STM32/STM32L4xx/WSPI-N25Q128/mcuconf.h b/testhal/STM32/STM32L4xx/WSPI-N25Q128/mcuconf.h index d5acc2985..6bf47d820 100644 --- a/testhal/STM32/STM32L4xx/WSPI-N25Q128/mcuconf.h +++ b/testhal/STM32/STM32L4xx/WSPI-N25Q128/mcuconf.h @@ -277,6 +277,11 @@ #define STM32_ST_IRQ_PRIORITY 8 #define STM32_ST_USE_TIMER 2 +/* + * TRNG driver system settings. + */ +#define STM32_TRNG_USE_RNG1 FALSE + /* * UART driver system settings. */ diff --git a/testhal/STM32/multi/PAL/cfg/stm32l476_discovery/mcuconf.h b/testhal/STM32/multi/PAL/cfg/stm32l476_discovery/mcuconf.h index ca04e21c0..b60398347 100644 --- a/testhal/STM32/multi/PAL/cfg/stm32l476_discovery/mcuconf.h +++ b/testhal/STM32/multi/PAL/cfg/stm32l476_discovery/mcuconf.h @@ -277,6 +277,11 @@ #define STM32_ST_IRQ_PRIORITY 8 #define STM32_ST_USE_TIMER 2 +/* + * TRNG driver system settings. + */ +#define STM32_TRNG_USE_RNG1 FALSE + /* * UART driver system settings. */ diff --git a/testhal/STM32/multi/SPI/cfg/stm32l476_discovery/mcuconf.h b/testhal/STM32/multi/SPI/cfg/stm32l476_discovery/mcuconf.h index 80c9adf1b..d5a4b4865 100644 --- a/testhal/STM32/multi/SPI/cfg/stm32l476_discovery/mcuconf.h +++ b/testhal/STM32/multi/SPI/cfg/stm32l476_discovery/mcuconf.h @@ -277,6 +277,11 @@ #define STM32_ST_IRQ_PRIORITY 8 #define STM32_ST_USE_TIMER 2 +/* + * TRNG driver system settings. + */ +#define STM32_TRNG_USE_RNG1 FALSE + /* * UART driver system settings. */ diff --git a/testhal/STM32/multi/TRNG/debug/STM32-TRNG (Select ELF file)(OpenOCD, Flash and Run).launch b/testhal/STM32/multi/TRNG/debug/STM32-TRNG (Select ELF file)(OpenOCD, Flash and Run).launch index 5b1ae6f78..c5b8ef7e9 100644 --- a/testhal/STM32/multi/TRNG/debug/STM32-TRNG (Select ELF file)(OpenOCD, Flash and Run).launch +++ b/testhal/STM32/multi/TRNG/debug/STM32-TRNG (Select ELF file)(OpenOCD, Flash and Run).launch @@ -33,7 +33,7 @@ - + diff --git a/testhal/STM32/multi/TRNG/main.c b/testhal/STM32/multi/TRNG/main.c index 83edb3e63..51d03718d 100644 --- a/testhal/STM32/multi/TRNG/main.c +++ b/testhal/STM32/multi/TRNG/main.c @@ -16,6 +16,7 @@ #include "ch.h" #include "hal.h" +#include "chprintf.h" #include "portab.h" @@ -61,6 +62,53 @@ int main(void) { /* Normal main() thread activity, in this demo it does nothing.*/ while (true) { + if (palReadLine(PORTAB_LINE_BUTTON) == PORTAB_BUTTON_PRESSED) { + bool err; + uint8_t rand[32]; + + trngStart(&TRNGD1, NULL); + + err = trngGenerate(&TRNGD1, 32, rand); + if (err) { + chprintf((BaseSequentialStream *)&PORTAB_SD1, "Error!\r\n"); + } + else { + unsigned i; + + for (i = 0; i < 32; i++) { + chprintf((BaseSequentialStream *)&PORTAB_SD1, "%02x", rand[i]); + } + chprintf((BaseSequentialStream *)&PORTAB_SD1, "\r\n"); + } + + err = trngGenerate(&TRNGD1, 15, rand); + if (err) { + chprintf((BaseSequentialStream *)&PORTAB_SD1, "Error!\r\n"); + } + else { + unsigned i; + + for (i = 0; i < 15; i++) { + chprintf((BaseSequentialStream *)&PORTAB_SD1, "%02x", rand[i]); + } + chprintf((BaseSequentialStream *)&PORTAB_SD1, "\r\n"); + } + + err = trngGenerate(&TRNGD1, 2, rand); + if (err) { + chprintf((BaseSequentialStream *)&PORTAB_SD1, "Error!\r\n"); + } + else { + unsigned i; + + for (i = 0; i < 2; i++) { + chprintf((BaseSequentialStream *)&PORTAB_SD1, "%02x", rand[i]); + } + chprintf((BaseSequentialStream *)&PORTAB_SD1, "\r\n"); + } + + trngStop(&TRNGD1); + } chThdSleepMilliseconds(500); } return 0; diff --git a/testhal/STM32/multi/TRNG/make/stm32l476_discovery.make b/testhal/STM32/multi/TRNG/make/stm32l476_discovery.make index a8d43f714..21875522e 100644 --- a/testhal/STM32/multi/TRNG/make/stm32l476_discovery.make +++ b/testhal/STM32/multi/TRNG/make/stm32l476_discovery.make @@ -5,7 +5,7 @@ # Compiler options here. ifeq ($(USE_OPT),) - USE_OPT = -O2 -ggdb -fomit-frame-pointer -falign-functions=16 + USE_OPT = -O0 -ggdb -fomit-frame-pointer -falign-functions=16 endif # C specific options here (added to USE_OPT). @@ -112,6 +112,7 @@ include $(CHIBIOS)/tools/mk/autobuild.mk include $(CHIBIOS)/test/lib/test.mk include $(CHIBIOS)/test/rt/rt_test.mk include $(CHIBIOS)/test/oslib/oslib_test.mk +include $(CHIBIOS)/os/hal/lib/streams/streams.mk # Define linker script file here. LDSCRIPT= $(STARTUPLD)/STM32L476xG.ld diff --git a/testhal/STM32/multi/USB_CDC/cfg/stm32l476_discovery/mcuconf.h b/testhal/STM32/multi/USB_CDC/cfg/stm32l476_discovery/mcuconf.h index 669ff94cb..bf0da7566 100644 --- a/testhal/STM32/multi/USB_CDC/cfg/stm32l476_discovery/mcuconf.h +++ b/testhal/STM32/multi/USB_CDC/cfg/stm32l476_discovery/mcuconf.h @@ -277,6 +277,11 @@ #define STM32_ST_IRQ_PRIORITY 8 #define STM32_ST_USE_TIMER 2 +/* + * TRNG driver system settings. + */ +#define STM32_TRNG_USE_RNG1 FALSE + /* * UART driver system settings. */ diff --git a/testhal/STM32/multi/WSPI-MFS/cfg/stm32l476_discovery/mcuconf.h b/testhal/STM32/multi/WSPI-MFS/cfg/stm32l476_discovery/mcuconf.h index 46e69ff34..c4e6a0754 100644 --- a/testhal/STM32/multi/WSPI-MFS/cfg/stm32l476_discovery/mcuconf.h +++ b/testhal/STM32/multi/WSPI-MFS/cfg/stm32l476_discovery/mcuconf.h @@ -277,6 +277,11 @@ #define STM32_ST_IRQ_PRIORITY 8 #define STM32_ST_USE_TIMER 2 +/* + * TRNG driver system settings. + */ +#define STM32_TRNG_USE_RNG1 FALSE + /* * UART driver system settings. */ diff --git a/tools/ftl/processors/conf/mcuconf_stm32l476xx/mcuconf.h.ftl b/tools/ftl/processors/conf/mcuconf_stm32l476xx/mcuconf.h.ftl index 7b1973f07..1efa589c0 100644 --- a/tools/ftl/processors/conf/mcuconf_stm32l476xx/mcuconf.h.ftl +++ b/tools/ftl/processors/conf/mcuconf_stm32l476xx/mcuconf.h.ftl @@ -288,6 +288,11 @@ #define STM32_ST_IRQ_PRIORITY ${doc.STM32_ST_IRQ_PRIORITY!"8"} #define STM32_ST_USE_TIMER ${doc.STM32_ST_USE_TIMER!"2"} +/* + * TRNG driver system settings. + */ +#define STM32_TRNG_USE_RNG1 ${doc.STM32_TRNG_USE_RNG1!"FALSE"} + /* * UART driver system settings. */ -- cgit v1.2.3