Skip to content

Add os.urandom to ports/analog #10549

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 6, 2025

Conversation

PaggieZ
Copy link

@PaggieZ PaggieZ commented Aug 6, 2025

Summary

Add os.urandom() support to the analog port. Users can now generate random numbers utilizing the TRNG (true random number generator) hardware on the analog port.

The following code generates multiple random numbers and prints them in hex:

import os
print(os.urandom(4).hex())
print(os.urandom(3).hex())
print(os.urandom(2).hex())
print(os.urandom(1).hex())

An example of the expected output is:

bb4f0ebb
8b2dd8
918b
ac

Testing

After I finished implementing the module, I built and flashed the project in the ports/analog directory using OpenOCD with a J-Link device. After flashing firmware.elf to an AD-APARD32690-SL, I ran the code snippet in both code.py and REPL and got different numbers every time I executed it.

PaggieZ added 2 commits August 4, 2025 16:19
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
Copy link
Collaborator

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@dhalbert dhalbert merged commit c694f64 into adafruit:main Aug 6, 2025
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants