4 ms·
I tried to use Nim in an embedded context (ESP32). Since one of the bullet points is that it "compiles to C", I thought it'd be a good option. In practice, eve
by rohansingh 8y ago
I tried to use Nim in an embedded context (ESP32). Since one of the bullet points is that it "compiles to C", I thought it'd be a good option.
In practice, even once I was able to compile basic Nim, there were a lot of glibc and other POSIX dependencies for any of Nim's standard libraries, some of which weren't available in ESP-IDF.
That's totally fair, but many of those dependencies aren't available when compiling to JS either, though, so I was surprised. In the end, delving into the code I found that a lot of stuff was gated behind the equivalent of "platform=JS" switches throughout the stdlib. It seemed like adding support for another platform would be a major pain.
This was all preliminary investigation and I didn't reach out to the Nim community, so perhaps I missed something. Maybe this kind of portability isn't even a goal for Nim. It would definitely be nice to have, though.
- mratsim 8y agoYou need to pass --gc:none when compiling so that the compiler warns you when you try to use a garbage collected type and instead of the stdlib, use the SDK facilities. Here are some libraries for embedded you can get inspiration from[1]: - msp430f5510 - Run Nim on MSP430f5510 micro-controller (6KB of RAM).[2] - stm32f3 - Run Nim on STM32F3 micro-controller (16KB of RAM).[3] - ardunimo - Nim wrapper for Arduino + LinkIt ONE SDK by Mediatek.[4] - ardunimesp - Nim wrapper for Arduino ESP8266 framework + A tool for flash, compile and make the nim project into an Arduino project.[5] [1] https://github.com/VPashkov/awesome-nim#embedded https://github.com/VPashkov/awesome-nim#embedded [2] https://gitlab.com/jalexander8717/msp430f5510-nim https://gitlab.com/jalexander8717/msp430f5510-nim [3] https://github.com/mwbrown/nim_stm32f3 https://github.com/mwbrown/nim_stm32f3 [4] https://github.com/gokr/ardunimo https://github.com/gokr/ardunimo [5] https://gitlab.com/NetaLabTek/Arduimesp https://gitlab.com/NetaLabTek/Arduimesp Also, yes abstracting a platform is a lot of pain but Nim accepts PR even for niche platforms like Haiku, Genode and Nintendo Switch. Also with the upcoming destructors, the standard library will be GC-free so much more will be usable on embedded.
- rohansingh 8y agoYeah, I tried --gc:none. After some playing around, I ended up with: --gc:stack --header:"nim.h" --os:standalone --noMain --genDeps In the end I got Nim code to run and was able to do a "hello world". As a hack I also used -d:android to trigger some conditionals that reduced the reliance on various dependencies. But it wasn't enough to be usable, and it was a bit unfortunate to have to do that. Looking at those projects you linked though, clearly I didn't try hard enough! Great to see that people have had success.
- dancek 8y agoThanks for sharing your experience. I read another comment about Nim being nice on microcontrollers because it compiles/transpiles to C, and my first thought was ESP32. I've written Rust for STM32 and MSP430 and it's awesome. ESP32, OTOH, seems like a nice platform but doesn't have LLVM support so C and MicroPython are pretty much the only options.
- rohansingh 8y agoRumor is that Espressif is actually working on LLVM support.
- dancek 8y agoAt least they were recruiting with that angle some months ago. I'm assuming it'll take a while even if all goes well, though.
- deleted 8y ago[deleted]