From 3aa680cf9c47f3399fa62a902b678a747c575fd2 Mon Sep 17 00:00:00 2001 From: Peggy Date: Mon, 4 Aug 2025 16:19:06 -0700 Subject: [PATCH 1/2] Add os.urandom module to ports/analog for MAX32690_apard I added a port-specific module, os.urandom() to ports/analog. Now users can generate random numbers from the TRNG hardware. The following code generates 3 random numbers and prints them in hex: import os print(os.urandom(3).hex()) Files I modiied: * ports/analog/Makefile: added INC and SRC for TRNG * ports/mpconfigport.mk: enabled CIRCUITPY_RANDOM * ports/analog/common-hal/os/__init__.c: implemented common_hal_os_urandom() * ports/analog/supervisor/port.c: initialized the TRNG object --- ports/analog/Makefile | 7 +++++-- ports/analog/common-hal/os/__init__.c | 7 ++++++- ports/analog/mpconfigport.mk | 2 +- ports/analog/supervisor/port.c | 12 ++++++++++++ 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/ports/analog/Makefile b/ports/analog/Makefile index 08b245c5897e3..0d4d05f136804 100644 --- a/ports/analog/Makefile +++ b/ports/analog/Makefile @@ -83,7 +83,8 @@ INC += \ -I$(PERIPH_SRC)/ICC \ -I$(PERIPH_SRC)/TMR \ -I$(PERIPH_SRC)/RTC \ - -I$(PERIPH_SRC)/UART + -I$(PERIPH_SRC)/UART \ + -I$(PERIPH_SRC)/TRNG INC += -I$(CMSIS_ROOT)/Device/Maxim/$(MCU_VARIANT_UPPER)/Source/GCC @@ -116,7 +117,9 @@ SRC_MAX32 += \ $(PERIPH_SRC)/TMR/tmr_$(DIE_TYPE).c \ $(PERIPH_SRC)/UART/uart_common.c \ $(PERIPH_SRC)/UART/uart_$(DIE_TYPE).c \ - $(PERIPH_SRC)/UART/uart_revb.c + $(PERIPH_SRC)/UART/uart_revb.c \ + $(PERIPH_SRC)/TRNG/trng_revb.c \ + $(PERIPH_SRC)/TRNG/trng_$(DIE_TYPE).c SRC_C += $(SRC_MAX32) \ boards/$(BOARD)/board.c \ diff --git a/ports/analog/common-hal/os/__init__.c b/ports/analog/common-hal/os/__init__.c index 7b607cf6b3c4b..04667db8b5896 100644 --- a/ports/analog/common-hal/os/__init__.c +++ b/ports/analog/common-hal/os/__init__.c @@ -15,9 +15,14 @@ // #include "peripherals/periph.h" +// true random number generator, TRNG +#include "trng.h" + bool common_hal_os_urandom(uint8_t *buffer, uint32_t length) { #if (HAS_TRNG) - // todo (low prior): implement + // get a random number of "length" number of bytes + MXC_TRNG_Random(buffer, length); + return true; #else #endif return false; diff --git a/ports/analog/mpconfigport.mk b/ports/analog/mpconfigport.mk index 1729a284a3f47..221730106d269 100644 --- a/ports/analog/mpconfigport.mk +++ b/ports/analog/mpconfigport.mk @@ -51,7 +51,7 @@ CIRCUITPY_BITBANGIO ?= 1 # Requires Microcontroller CIRCUITPY_TOUCHIO ?= 1 # Requires OS -CIRCUITPY_RANDOM ?= 0 +CIRCUITPY_RANDOM ?= 1 # Requires busio.UART CIRCUITPY_CONSOLE_UART ?= 0 # Does nothing without I2C diff --git a/ports/analog/supervisor/port.c b/ports/analog/supervisor/port.c index ad003c12ed9e0..ff05ff2d28709 100644 --- a/ports/analog/supervisor/port.c +++ b/ports/analog/supervisor/port.c @@ -44,6 +44,9 @@ #include "mxc_delay.h" #include "rtc.h" +// true random number generator, TRNG +#include "trng.h" + // msec to RTC subsec ticks (4 kHz) /* Converts a time in milleseconds to equivalent RSSA register value */ #define MSEC_TO_SS_ALARM(x) (0 - ((x * 4096) / 1000)) @@ -112,9 +115,18 @@ safe_mode_t port_init(void) { } ; + // enable TRNG (true random number generator) + #ifdef CIRCUITPY_RANDOM + MXC_TRNG_Init(); + #endif + return SAFE_MODE_NONE; } +void TRNG_IRQHandler(void) { + MXC_TRNG_Handler(); +} + void RTC_IRQHandler(void) { // Read flags to clear int flags = MXC_RTC_GetFlags(); From 06f1ac33e02657a9fd33ad1b18a47b7be06615fc Mon Sep 17 00:00:00 2001 From: Peggy Date: Tue, 5 Aug 2025 11:34:20 -0700 Subject: [PATCH 2/2] update licenses --- ports/analog/Makefile | 1 + ports/analog/common-hal/os/__init__.c | 1 + ports/analog/mpconfigport.mk | 1 + 3 files changed, 3 insertions(+) diff --git a/ports/analog/Makefile b/ports/analog/Makefile index 0d4d05f136804..ce9082c5e255a 100644 --- a/ports/analog/Makefile +++ b/ports/analog/Makefile @@ -2,6 +2,7 @@ # # SPDX-FileCopyrightText: Copyright (c) 2020 Scott Shawcroft for Adafruit Industries # SPDX-FileCopyrightText: Copyright (c) 2024 Brandon Hurst, Analog Devices, Inc. +# SPDX-FileCopyrightText: Copyright (c) 2025 Peggy Zhu, Analog Devices, Inc. # # SPDX-License-Identifier: MIT diff --git a/ports/analog/common-hal/os/__init__.c b/ports/analog/common-hal/os/__init__.c index 04667db8b5896..508b40798e22b 100644 --- a/ports/analog/common-hal/os/__init__.c +++ b/ports/analog/common-hal/os/__init__.c @@ -2,6 +2,7 @@ // // SPDX-FileCopyrightText: Copyright (c) 2017 Scott Shawcroft for Adafruit Industries // SPDX-FileCopyrightText: Copyright (c) 2019 Lucian Copeland for Adafruit Industries +// SPDX-FileCopyrightText: Copyright (c) 2025 Peggy Zhu, Analog Devices, Inc. // // SPDX-License-Identifier: MIT diff --git a/ports/analog/mpconfigport.mk b/ports/analog/mpconfigport.mk index 221730106d269..67f1f79fb8121 100644 --- a/ports/analog/mpconfigport.mk +++ b/ports/analog/mpconfigport.mk @@ -2,6 +2,7 @@ # # SPDX-FileCopyrightText: Copyright (c) 2020 Scott Shawcroft for Adafruit Industries # SPDX-FileCopyrightText: Copyright (c) 2024 Brandon Hurst, Analog Devices, Inc. +# SPDX-FileCopyrightText: Copyright (c) 2025 Peggy Zhu, Analog Devices, Inc. # # SPDX-License-Identifier: MIT