Random Integers Crash Embedded Swift with ESP-IDF 5.2.2 and 5.3.1 for ESP32-C6
Update from Erhan Kurubas at Espressify:
@PaulSolt Please stay in the master (v5.4) branch. Don't checkout to the release v5.2 or v5.3
As a summary;
1. We have fixed theThe gap between ...
errors in this repo with the 2eb2975 . No change in the IDF releases.
2. We don't have getentropy error in IDF master (v5.4) branch. No need to apply a patch.
3. For the previous idf releases (v5.3.x and v5.2.x), apply the patch I prepared. https://github.com/user-attachments/files/15877608/0001-newlib-getentropy-implementation.patch
As of 9-12-2024, Random integers are not usable with Embedded Swift with a release version (5.2 - 5.3) of ESP-IDF.
There is an open issue on the Swift Embedded Examples github, which requires a workaround patch to get it build.
If you attempt to use random numbers, you'll see a panic and your ESP32 will keep restarting forever with the crashing code.
let z = Int.random(in: 0...25) // !!!!!!!!!!!!!!!!!!!! RANDOM Number CRASHES Embedded Swift!
Your crash stack frame may look like:
panic_abort (details=details@entry=0x4081195c "abort() was called at PC 0x42006459 on core 0") at /Users/paulsolt/esp/esp-idf/components/esp_system/panic.c:466
...
#8 arc4random_buf (buf=0x40811a38, n=8) at /builds/idf/crosstool-NG/.build/HOST-x86_64-apple-darwin21.1/riscv32-esp-elf/src/newlib/newlib/libc/stdlib/arc4random.c:214
#9 0x4200e45c in $sSGsE4next10upperBoundqd__qd___ts17FixedWidthIntegerRd__SURd__lFs27SystemRandomNumberGeneratorV_SuTg5 ()
If you are using ESP-IDF 5.2.2 or 5.3.1 or earlier, there's an issue with the ESP-IDF build tools that needs a patch.
Note: You may be able to use the master
branch without patching, but that may be less stable than a fixed version that is known to work with Embedded Swift.
Apply a Patch for ESP IDF 5.2.2
One of the developers from Espressify posted the patch files. I have only modified them so that they can work for version 5.2.2 and 5.3.1 which I had installed.
- Download the patch you need to fix random numbers
2. ESP-IDF 5.2.2 Random Number Patch
3. ESP-IDF 5.3.1 Random Number Patch
Apply the patch (5.2.2 or 5.3.1)
git apply ~/Dowloads/esp32c6_random_crash_fix_5.2.2.patch
Navigate to your ESP build folder
cd ~/esp/esp-idf/
Now you should be all set, but you can try using the latest from master
or a future release to fix this issue.
How to Update Your Install of ESP-IDF
If you want to try a different version or master
you can follow these steps to see if that will fix the problem without the need for a workaround patch.
This can take a minute or two depending on your connection speed.
cd ~/esp/esp-idf
git fetch --all --tags --prune
Checkout the latest version (5.3.1 as of 9/12/2024):
git checkout v5.3.1
git submodule update --init --recursive
Now you need to install the latest changes from ~/esp/esp-idf
:
./install.sh
After that you need to do a full clean in your project folder and build again.
Cross your fingers and hopefully it works, otherwise checkout the version like Apple's guide recommended.
# Navigate to your project folder
idf.py fullclean
idf.py build
idf.py flash monitor
Let me know if this helped you. You can find me on X: @PaulSolt