Making rand() work on NRF52 + NRF53 with NRF Connect SDK

EDIT: since I accidentally marked this as "verified" and don't seem to be able to "unverify" it, I've opened a new call here:

devzone.nordicsemi.com/.../making-rand-work-on-nrf52-nrf53-with-nrf-connect-sdk-continued

We have an NRFConnect SDK/Zephyr project that must (a) compile both for NRF52840 and NRF5340, and, (b) needs a working rand(); nothing fancy, pseudo-random would be perfectly fine, preferable in fact since this is only used in test code and it would be good to have the sequences repeatable.  We have tried:

CONFIG_TEST_RANDOM_GENERATOR=y
CONFIG_ENTROPY_DEVICE_RANDOM_GENERATOR=y

...but, while this works for NRF53, for NRF52 we get a KConfig warning:

warning: TEST_RANDOM_GENERATOR (defined at subsys/random/Kconfig:8) was assigned the value 'y' but got the value 'n'. Check these unsatisfied dependencies: (!ENTROPY_HAS_DRIVER) (=n). See docs.zephyrproject.org/.../CONFIG_TEST_RANDOM_GENERATOR.html and/or look up TEST_RANDOM_GENERATOR in the menuconfig/guiconfig interface. The Application Development Primer, Setting Configuration Values, and Kconfig - Tips and Best Practices sections of the manual might be helpful too.

...and rand() crashes with a memory exception when it is called (pasted in at the bottom).

We have also tried CONFIG_TIMER_RANDOM_GENERATOR=y but with that, while the KConfig warning for NRF52 goes away, it STILL crashes in exactly the same way and NRF53 doesn't compile for us anymore:

/home/ubxlib/.ubxlibpkg/arm_embedded_gcc-10-2020-q4-major/bin/../lib/gcc/arm-none-eabi/10.2.1/../../../../arm-none-eabi/bin/ld: app/libapp.a(u_port_clib.c.obj): in function `sys_rand32_get':
/home/jenkinsslave-mlm/jenkins_workspace/workspace/iv_feature_gnss_transparent_rmea_4/_jenkins_work/17/build/zephyr/include/generated/syscalls/rand32.h:33: undefined reference to `z_impl_sys_rand32_get'`

What is the correct way to obtain an old-fashioned pseudo-random rand() library function for NRF52 and NRF53 on NRF Connect SDK (1.6.1) [with the normal default Zephyr minimal C lib]?

Here is the dump from the memory exception on NRF52:

2022-08-18 00:02:53 u_monitor_15.1 - U_GNSS_PRIVATE_TEST: test decoding NMEA message $GPGGA,092750.000,5321.6802,N,00630.3372,W,1,8,1.03,61.7,M,55.2,M,,*76
2022-08-18 00:03:10 u_monitor_15.1 - E: ***** MPU FAULT *****
2022-08-18 00:03:10 u_monitor_15.1 - E: Data Access Violation
2022-08-18 00:03:10 u_monitor_15.1 - E: MMFAR Address: 0xc
2022-08-18 00:03:10 u_monitor_15.1 - E: r0/a1: 0x200067d4 r1/a2: 0x00000000 r2/a3: 0x20002490
2022-08-18 00:03:10 u_monitor_15.1 - E: r3/a4: 0x20002490 r12/ip: 0x00000000 r14/lr: 0x000554c1
2022-08-18 00:03:10 u_monitor_15.1 - E: xpsr: 0x21000000
2022-08-18 00:03:10 u_monitor_15.1 - E: r4/v1: 0x00000000 r5/v2: 0xffffffff r6/v3: 0xffffffff
2022-08-18 00:03:10 u_monitor_15.1 - E: r7/v4: 0x00000000 r8/v5: 0x00000000 r9/v6: 0x2002bf1c
2022-08-18 00:03:10 u_monitor_15.1 - E: r10/v7: 0x20005730 r11/v8: 0x00000000 psp: 0x2002bdf0
2022-08-18 00:03:11 u_monitor_15.1 - E: EXC_RETURN: 0xfffffffd
2022-08-18 00:03:11 u_monitor_15.1 - E: Faulting instruction address (r15/pc): 0x000554c0
2022-08-18 00:03:11 u_monitor_15.1 - E: >>> ZEPHYR FATAL ERROR 0: CPU exception on CPU 0
2022-08-18 00:03:11 u_monitor_15.1 - E: Current thread: 0x20002490 (main)
2022-08-18 00:03:11 u_monitor_15.1 - ### Dumping current thread (main) ###
2022-08-18 00:03:11 u_monitor_15.1 - 0x000554c0: z_impl_k_mutex_lock /home/ubxlib/.ubxlibpkg/nrfconnectsdk-v1.6.1/zephyr/kernel/mutex.c:107
2022-08-18 00:03:11 u_monitor_15.1 - 0x000554bc: k_spin_lock /home/ubxlib/.ubxlibpkg/nrfconnectsdk-v1.6.1/zephyr/include/spinlock.h:141
2022-08-18 00:03:11 u_monitor_15.1 - (inlined by) z_impl_k_mutex_lock /home/ubxlib/.ubxlibpkg/nrfconnectsdk-v1.6.1/zephyr/kernel/mutex.c:105
2022-08-18 00:03:11 u_monitor_15.1 - 0x00045fa8: k_mutex_lock /var/jenkins/workspace/iv_feature_gnss_transparent_rmea_2/_jenkins_work/15.1/build/zephyr/include/generated/syscalls/kernel.h:674
2022-08-18 00:03:11 u_monitor_15.1 - (inlined by) mutex_lock_platform /home/ubxlib/.ubxlibpkg/nrfconnectsdk-v1.6.1/nrfxlib/crypto/nrf_cc310_platform/src/nrf_cc3xx_platform_mutex_zephyr.c:291
2022-08-18 00:03:11 u_monitor_15.1 - 0x0005d532: cc_mbedtls_ctr_drbg_random_with_add ??:?
2022-08-18 00:03:11 u_monitor_15.1 - 0x0005bf02: nrf_cc3xx_platform_ctr_drbg_get ??:?
2022-08-18 00:03:11 u_monitor_15.1 - 0x0004ec74: entropy_cc3xx_rng_get_entropy /home/ubxlib/.ubxlibpkg/nrfconnectsdk-v1.6.1/nrf/drivers/entropy/entropy_cc310.c:77
2022-08-18 00:03:11 u_monitor_15.1 - 0x0004bdf8: z_impl_entropy_get_entropy /home/ubxlib/.ubxlibpkg/nrfconnectsdk-v1.6.1/zephyr/include/drivers/entropy.h:77
2022-08-18 00:03:11 u_monitor_15.1 - (inlined by) entropy_get_entropy /var/jenkins/workspace/iv_feature_gnss_transparent_rmea_2/_jenkins_work/15.1/build/zephyr/include/generated/syscalls/entropy.h:33
2022-08-18 00:03:11 u_monitor_15.1 - (inlined by) z_impl_sys_rand32_get /home/ubxlib/.ubxlibpkg/nrfconnectsdk-v1.6.1/zephyr/subsys/random/rand32_entropy_device.c:33
2022-08-18 00:03:11 u_monitor_15.1 - 0x00065780: sys_rand32_get /var/jenkins/workspace/iv_feature_gnss_transparent_rmea_2/_jenkins_work/15.1/build/zephyr/include/generated/syscalls/rand32.h:33
2022-08-18 00:03:11 u_monitor_15.1 - (inlined by) rand port/platform/zephyr/src/u_port_clib.c:81
2022-08-18 00:03:11 u_monitor_15.1 - 0x0003c1f4: test209 gnss/test/u_gnss_private_test.c:243
2022-08-18 00:03:11 u_monitor_15.1 - 0x00042f22: UnityDefaultTestRun

Related