Skip to content

Commit 3aa680c

Browse files
committed
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
1 parent 234ebf4 commit 3aa680c

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

ports/analog/Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ INC += \
8383
-I$(PERIPH_SRC)/ICC \
8484
-I$(PERIPH_SRC)/TMR \
8585
-I$(PERIPH_SRC)/RTC \
86-
-I$(PERIPH_SRC)/UART
86+
-I$(PERIPH_SRC)/UART \
87+
-I$(PERIPH_SRC)/TRNG
8788

8889
INC += -I$(CMSIS_ROOT)/Device/Maxim/$(MCU_VARIANT_UPPER)/Source/GCC
8990

@@ -116,7 +117,9 @@ SRC_MAX32 += \
116117
$(PERIPH_SRC)/TMR/tmr_$(DIE_TYPE).c \
117118
$(PERIPH_SRC)/UART/uart_common.c \
118119
$(PERIPH_SRC)/UART/uart_$(DIE_TYPE).c \
119-
$(PERIPH_SRC)/UART/uart_revb.c
120+
$(PERIPH_SRC)/UART/uart_revb.c \
121+
$(PERIPH_SRC)/TRNG/trng_revb.c \
122+
$(PERIPH_SRC)/TRNG/trng_$(DIE_TYPE).c
120123

121124
SRC_C += $(SRC_MAX32) \
122125
boards/$(BOARD)/board.c \

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@
1515

1616
// #include "peripherals/periph.h"
1717

18+
// true random number generator, TRNG
19+
#include "trng.h"
20+
1821
bool common_hal_os_urandom(uint8_t *buffer, uint32_t length) {
1922
#if (HAS_TRNG)
20-
// todo (low prior): implement
23+
// get a random number of "length" number of bytes
24+
MXC_TRNG_Random(buffer, length);
25+
return true;
2126
#else
2227
#endif
2328
return false;

ports/analog/mpconfigport.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ CIRCUITPY_BITBANGIO ?= 1
5151
# Requires Microcontroller
5252
CIRCUITPY_TOUCHIO ?= 1
5353
# Requires OS
54-
CIRCUITPY_RANDOM ?= 0
54+
CIRCUITPY_RANDOM ?= 1
5555
# Requires busio.UART
5656
CIRCUITPY_CONSOLE_UART ?= 0
5757
# 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)