Thread overview
Arduino and MCU Support
Jun 19, 2020
frasdoge
Jun 19, 2020
kinke
Jun 19, 2020
frasdoge
Jun 19, 2020
kinke
Jun 19, 2020
kinke
Jun 19, 2020
aberba
Jun 25, 2020
Dylan Graham
Jun 26, 2020
Dukc
Jun 27, 2020
Dylan Graham
June 19, 2020
I am looking to use D for microcontroller programming due to its benefits over C in workflow and general language features.

I was wondering what the current state of this is, especially with regards to AVR. An example of the MCUs I would like to develop with include anything from 8 bit ATmega328p to 32 bit ESP32.

The ESP32 can be programmed in C, C++, micropython and Lua, so I'm hoping there's at least some compatibility there.
June 19, 2020
On Friday, 19 June 2020 at 11:57:01 UTC, frasdoge wrote:
> I am looking to use D for microcontroller programming due to its benefits over C in workflow and general language features.
>
> I was wondering what the current state of this is, especially with regards to AVR. An example of the MCUs I would like to develop with include anything from 8 bit ATmega328p to 32 bit ESP32.
>
> The ESP32 can be programmed in C, C++, micropython and Lua, so I'm hoping there's at least some compatibility there.

AVR: https://wiki.dlang.org/D_on_AVR
With recent official LDC packages, you don't need to build LLVM and LDC yourself, AVR is supported out-of-the-box.

ESP32: https://wiki.dlang.org/D_on_esp32/esp8266(llvm-xtensa%2Bldc)_and_how_to_get_started
This arch hasn't been upstreamed to LLVM yet and so needs their LLVM fork, i.e., building it and LDC yourself.

For some more infos, use the search function of this forum.
June 19, 2020
On Friday, 19 June 2020 at 12:20:52 UTC, kinke wrote:
> AVR: https://wiki.dlang.org/D_on_AVR
> With recent official LDC packages, you don't need to build LLVM and LDC yourself, AVR is supported out-of-the-box.
>
> ESP32: https://wiki.dlang.org/D_on_esp32/esp8266(llvm-xtensa%2Bldc)_and_how_to_get_started
> This arch hasn't been upstreamed to LLVM yet and so needs their LLVM fork, i.e., building it and LDC yourself.
>
> For some more infos, use the search function of this forum.

Thanks for the reply.

I'm having a bit of trouble understanding how to actually get started even with those links.

I've installed the latest LDC and LLVM releases for Windows, though the -mcpu does not have any AVR options out-of-the-box as you mentioned. Perhaps I've missed something, or will I need to build the binary myself using the flags for AVR?

If you could explain in greater detail I would very much appreciate it, as there is *very* little documentation elsewhere or even on this forum.

The ESP32 process seems more involved. Due to my inexperience in this area, I'm wondering that, if I need to build the binaries myself anyway, how I would set it such that it'll work with AVR and ESP32 at the same time with one installation?

I'm hoping to have an analogous experience as with C, where I simply write my code, specify my cpu target, and then program over serial.
June 19, 2020
On Friday, 19 June 2020 at 14:14:07 UTC, frasdoge wrote:
> I'm having a bit of trouble understanding how to actually get started even with those links.
>
> I've installed the latest LDC and LLVM releases for Windows, though the -mcpu does not have any AVR options out-of-the-box as you mentioned. Perhaps I've missed something, or will I need to build the binary myself using the flags for AVR?

Compiling the sample from the AVR wiki works here on Win64 with the official LDC 1.22 package and:
ldc2 -betterC -Oz -mtriple=avr -mcpu=atmega328p -c test.d

For linking, you'll need an avr-gcc toolchain, and add `-gcc=<path\to\avr-gcc> -Xcc=-mmcu=atmega328p` as mentioned on the Wiki page. I don't have such a toolchain.

> The ESP32 process seems more involved. Due to my inexperience in this area, I'm wondering that, if I need to build the binaries myself anyway, how I would set it such that it'll work with AVR and ESP32 at the same time with one installation?

Simply using -mtriple and tweaking etc\ldc2.conf as explained here:
https://wiki.dlang.org/Cross-compiling_with_LDC

June 19, 2020
On Friday, 19 June 2020 at 14:14:07 UTC, frasdoge wrote:
> though the -mcpu does not have any AVR options out-of-the-box as you mentioned.

I guess you mean `ldc2 -mcpu=help` doesn't list any AVR CPUs. Use `ldc2 -mtriple=avr -mcpu=help`.
June 19, 2020
On Friday, 19 June 2020 at 11:57:01 UTC, frasdoge wrote:
> I am looking to use D for microcontroller programming due to its benefits over C in workflow and general language features.
>
> I was wondering what the current state of this is, especially with regards to AVR. An example of the MCUs I would like to develop with include anything from 8 bit ATmega328p to 32 bit ESP32.
>
> The ESP32 can be programmed in C, C++, micropython and Lua, so I'm hoping there's at least some compatibility there.

Once you get it to work, please document something for use by others. Its quite unfortunate this is not well documented.
June 25, 2020
On Friday, 19 June 2020 at 11:57:01 UTC, frasdoge wrote:
> I am looking to use D for microcontroller programming due to its benefits over C in workflow and general language features.
>
> I was wondering what the current state of this is, especially with regards to AVR. An example of the MCUs I would like to develop with include anything from 8 bit ATmega328p to 32 bit ESP32.
>
> The ESP32 can be programmed in C, C++, micropython and Lua, so I'm hoping there's at least some compatibility there.

I'd be pretty interested in this too. I'm currently making an automatic transmission controller with Arduino. C++ just has too many traps that I keep falling into. Since stability is critical (if the code screws up at 100km/h I'm dead), I'd rather use a sane language like D.

I'm going to look into getting it to run, but I can't promise much and I'm busy atm.
June 26, 2020
On Thursday, 25 June 2020 at 03:00:04 UTC, Dylan Graham wrote:
> I'm currently making an automatic transmission controller with Arduino. C++ just has too many traps that I keep falling into. Since stability is critical (if the code screws up at 100km/h I'm dead), I'd rather use a sane language like D.

No, don't! Regardless of the language, you should never trust your safety on one single program. See https://www.digitalmars.com/articles/b39.html

Realistically, doing such a controller cannot be one man/woman endeavour.
June 27, 2020
On Friday, 26 June 2020 at 09:30:15 UTC, Dukc wrote:
> On Thursday, 25 June 2020 at 03:00:04 UTC, Dylan Graham wrote:
>> I'm currently making an automatic transmission controller with Arduino. C++ just has too many traps that I keep falling into. Since stability is critical (if the code screws up at 100km/h I'm dead), I'd rather use a sane language like D.
>
> No, don't! Regardless of the language, you should never trust your safety on one single program. See https://www.digitalmars.com/articles/b39.html
>
> Realistically, doing such a controller cannot be one man/woman endeavour.

Thank you for the article. It was an intriguing read.

I don't really have any capacity to implement multiple concurrent programs given how limited the hardware is.

What I do have is a "pipeline" of modules that have their own checks to mitigate logical errors.

The transmission also has a mechanical backup should the electronics fail.

What I meant more so was creature comforts D provides that you begin missing when you revert to C++ -- something that can assist me in avoiding certain classes of bugs, which is what was referring to in my original message and I fear the most.

I have a running controller that even interacts with car's stock computers and is passing real world tests at current. Automotive engineering is more approachable than it first appears.