Skip to content

Commit c694f64

Browse files
authored
Merge pull request #10549 from PaggieZ/ports/analog/add-urandom
Add os.urandom to ports/analog
2 parents f86bf4c + 06f1ac3 commit c694f64

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

ports/analog/Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#
33
# SPDX-FileCopyrightText: Copyright (c) 2020 Scott Shawcroft for Adafruit Industries
44
# SPDX-FileCopyrightText: Copyright (c) 2024 Brandon Hurst, Analog Devices, Inc.
5+
# SPDX-FileCopyrightText: Copyright (c) 2025 Peggy Zhu, Analog Devices, Inc.
56
#
67
# SPDX-License-Identifier: MIT
78

@@ -83,7 +84,8 @@ INC += \
8384
-I$(PERIPH_SRC)/ICC \
8485
-I$(PERIPH_SRC)/TMR \
8586
-I$(PERIPH_SRC)/RTC \
86-
-I$(PERIPH_SRC)/UART
87+
-I$(PERIPH_SRC)/UART \
88+
-I$(PERIPH_SRC)/TRNG
8789

8890
INC += -I$(CMSIS_ROOT)/Device/Maxim/$(MCU_VARIANT_UPPER)/Source/GCC
8991

@@ -116,7 +118,9 @@ SRC_MAX32 += \
116118
$(PERIPH_SRC)/TMR/tmr_$(DIE_TYPE).c \
117119
$(PERIPH_SRC)/UART/uart_common.c \
118120
$(PERIPH_SRC)/UART/uart_$(DIE_TYPE).c \
119-
$(PERIPH_SRC)/UART/uart_revb.c
121+
$(PERIPH_SRC)/UART/uart_revb.c \
122+
$(PERIPH_SRC)/TRNG/trng_revb.c \
123+
$(PERIPH_SRC)/TRNG/trng_$(DIE_TYPE).c
120124

121125
SRC_C += $(SRC_MAX32) \
122126
boards/$(BOARD)/board.c \

ports/analog/common-hal/os/__init__.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//
33
// SPDX-FileCopyrightText: Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
44
// SPDX-FileCopyrightText: Copyright (c) 2019 Lucian Copeland for Adafruit Industries
5+
// SPDX-FileCopyrightText: Copyright (c) 2025 Peggy Zhu, Analog Devices, Inc.
56
//
67
// SPDX-License-Identifier: MIT
78

@@ -15,9 +16,14 @@
1516

1617
// #include "peripherals/periph.h"
1718

19+
// true random number generator, TRNG
20+
#include "trng.h"
21+
1822
bool common_hal_os_urandom(uint8_t *buffer, uint32_t length) {
1923
#if (HAS_TRNG)
20-
// todo (low prior): implement
24+
// get a random number of "length" number of bytes
25+
MXC_TRNG_Random(buffer, length);
26+
return true;
2127
#else
2228
#endif
2329
return false;

ports/analog/mpconfigport.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#
33
# SPDX-FileCopyrightText: Copyright (c) 2020 Scott Shawcroft for Adafruit Industries
44
# SPDX-FileCopyrightText: Copyright (c) 2024 Brandon Hurst, Analog Devices, Inc.
5+
# SPDX-FileCopyrightText: Copyright (c) 2025 Peggy Zhu, Analog Devices, Inc.
56
#
67
# SPDX-License-Identifier: MIT
78

@@ -51,7 +52,7 @@ CIRCUITPY_BITBANGIO ?= 1
5152
# Requires Microcontroller
5253
CIRCUITPY_TOUCHIO ?= 1
5354
# Requires OS
54-
CIRCUITPY_RANDOM ?= 0
55+
CIRCUITPY_RANDOM ?= 1
5556
# Requires busio.UART
5657
CIRCUITPY_CONSOLE_UART ?= 0
5758
# Does nothing without I2C

ports/analog/supervisor/port.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
#include "mxc_delay.h"
4545
#include "rtc.h"
4646

47+
// true random number generator, TRNG
48+
#include "trng.h"
49+
4750
// msec to RTC subsec ticks (4 kHz)
4851
/* Converts a time in milleseconds to equivalent RSSA register value */
4952
#define MSEC_TO_SS_ALARM(x) (0 - ((x * 4096) / 1000))
@@ -112,9 +115,18 @@ safe_mode_t port_init(void) {
112115
}
113116
;
114117

118+
// enable TRNG (true random number generator)
119+
#ifdef CIRCUITPY_RANDOM
120+
MXC_TRNG_Init();
121+
#endif
122+
115123
return SAFE_MODE_NONE;
116124
}
117125

126+
void TRNG_IRQHandler(void) {
127+
MXC_TRNG_Handler();
128+
}
129+
118130
void RTC_IRQHandler(void) {
119131
// Read flags to clear
120132
int flags = MXC_RTC_GetFlags();

0 commit comments

Comments
 (0)