r/embedded • • Mar 04 '24

Development alternatives for C

I have been looking for languages to learn other than C which might be useful in the long run. So far I've come across Rust and Mojo but can't make a case for which is more suitable for embedded applications.

I'd like to know what you guys have experienced while using any of these languages.

2 Upvotes

62 comments sorted by

6

u/Shadow_Gabriel Mar 04 '24

You can go into the FPGA world and look at Verilog or look at some of the languages used in validation and automation: Python, CAPL, ACSL (frama-c).

Or model based design: Simulink, LabVIEW.

1

u/__Jay_sh Mar 04 '24

Thanks, I have been using Python and CAPL for my work so far, but I suppose that's limited to Validation. I don't see python for embedded development at least in my workplace, CAPL would be a different case as that is for automation restricted to CAN bus.

7

u/Sugardaddy_satan Mar 04 '24 edited Mar 17 '26

This post has been removed by its author. The deletion was carried out using Redact, possibly to protect personal information or limit exposure to data collection tools.

serious vase birds history bear important possessive memory steer cow

0

u/__Jay_sh Mar 04 '24

Can you emphasize why Zig is viable.Have you used it at your workplace or came across any article which has stated it's use at something significant ?

5

u/robodan65 Mar 04 '24

I've been watching Zig because it's the best new language for embedded work. It has an elegant syntax and very powerful "comptime" concept. Comptime is code that runs at compile time. This allows it to do the things that we used to do in the pre-processor but in a well defined way.

By design, it interoperates with C and (most) C++. So you should be able to drop it in existing software stacks without issues.

It doesn't do GC (garbage collection) and gives the programmer lots of control over memory allocation. This is more work than a GC language (TinyGo is also interesting), but is usually required for embedded.

The thing to watch is that it depends on LLVM for the processor support. If there is good LLVM for your CPU, then porting ZIG to it should be easy. It is a new language and getting support for the myriad of embedded processors and RTOS is an ongoing challenge.

People come up to speed on Zig in a couple days and are proficient in a week or two. This is in contrast to Rust which takes several week to understand how it approaches memory management.

1

u/PeterMortensenBlog Mar 04 '24

Re "Comptime is code that runs at compile time.": That is similar to Forth (defining 'words').

2

u/Sugardaddy_satan Mar 04 '24 edited Mar 17 '26

This specific post has been deleted. The author may have removed it to protect their privacy, maintain operational security, or prevent data scraping, using Redact.

subtract longing air nutty arrest hungry coherent childlike stocking bear

70

u/Well-WhatHadHappened 25+ Years Mar 04 '24

Embedded is absolutely dominated by C and C++

You simply have to know both of those, fluently, for embedded tasks.

Rust would get my vote for a language to learn for the future. It's going to be years.. maybe decades.. before C and C++ are the underdogs, but Rust is making small headways.

12

u/Brilliant_Armadillo9 Hardware Engineer Mar 04 '24

Is Rust ready for prime time? I haven't looked into it in a while, but as of a few years ago, it seemed immature.

14

u/tiajuanat Mar 04 '24

My company is using it for greenfield projects. There's still some rough edges, but it completely stops classes of bug sources, and it's easier to maintain than C.

7

u/LongUsername Mar 04 '24

There's a ISO 26262 (ASIL D) and IEC 61508 (SIL 4) certified toolchain version. How much more ready do you want?

https://ferrous-systems.com/ferrocene/

2

u/Brilliant_Armadillo9 Hardware Engineer Mar 05 '24

I don't care about 26262 or 61508, so not particularly interested in getting locked into that branch.

2

u/LongUsername Mar 05 '24

My point wasn't that you should use that branch, but that the existence of a safety certified version for embedded targets is a strong indicator that Rust for embedded is "Ready for Prime Time".

1

u/Brilliant_Armadillo9 Hardware Engineer Mar 05 '24

I disagree. A safety certified version of anything can be built. That doesn't mean the ecosystem as a whole is equally capable.

2

u/robodan65 Mar 04 '24

Some timely research on that:

Rust for Embedded Systems: Current State, Challenges and Open Problems https://arxiv.org/abs/2311.05063 https://news.ycombinator.com/item?id=39591694

1

u/squiggling-aviator Mar 05 '24 edited Mar 05 '24

I'd think it's still risky for long-term critical products and like to see more commercial products buy into using it first. Also, maybe an RTOS as comprehensive as Micrium implemented using Rust. I wouldn't want something else to overtake Rust within the next decade and vaporize its support.

The White House did come out and suggest the move to more secure programming environments like Rust [https://stackoverflow.blog/2024/03/04/in-rust-we-trust-white-house-office-urges-memory-safety/]. I've still yet to see funding and programs to get things moving along though.

My guess would be industries that would have the leeway to venture into using Rust right now would be automotive (infotainment), drones, industrial comms, payment systems, smart home systems, etc. but I haven't heard of anything of the sort yet.

8

u/Sushantnaik21 Mar 04 '24

SDK, HaL or Mcal everything is available in c why would micro controller manufacturers dump c and move on to some new language and burn money to develop new SDK or libraries

6

u/rpkarma Mar 04 '24

Because those SDKs and HALs are riddled with bugs and security errors, which really starts to matter with how many internet connected devices there are?

And writing complex config (at run time) driven systems in C is painful at scale?

There’s plenty of reasons companies are trying to. The problem is none of the solutions are ready yet.

2

u/squiggling-aviator Mar 05 '24

Because those SDKs and HALs are riddled with bugs and security errors,

You could argue maintenance costs as a reason to make the switch but it's probably not going to happen in the next decade if no one throws money at them.

1

u/Eplankton Mar 05 '24

These SDKs look like old monsters come from 1900s, which can never attract young developers who have been used to modern software environment. So that's why basically every SDK is now moving into VSCode.

1

u/Eplankton Mar 05 '24

And these HALs often written by someone comes from non-CS/SE world, which result in millions of bugs and hard-to-read code, filled with billions of macros or compiler-related tag. Some of these HALs even don't have a Github repo, you have to sign up or login in and then search the whole damn files on website or IDE(and I also doubt whether these HAL developers know what GitHub is)

0

u/Eplankton Mar 05 '24

Clearly Infineon and Espressif have put a lot of energy on it, we all have been familiar with esp-rs/esp-hal, and now infineon has moved on https://www.lauterbach.com/blog/rust-development-platform-debug-support-for-infineon-aurix

17

u/Izrakk Mar 04 '24

why are you allergic to c

8

u/kkert Mar 04 '24

For embedded, it's Rust and possibly Ada

When you filter languages by properties like it' can't have garbage collected memory management and it should be memory safe, there's not much left.

3

u/DownhillOneWheeler Mar 04 '24

You are going to need C in any case, at least for dealing with vendor code and existing projects, and for projects on older devices. C is used for about 80% of all projects. That's a great pity, but it's how things are.

C++ is a very solid option, used for about 15-20% of projects. It is often lumped together with C but they are really very different beasts. I used C++ almost exclusively for 15 years as I mostly work on Cortex-M devices, for which it has excellent support.

Another option is Rust, but this is as yet very much a wannabe with essentially zero market share. It may or may not become more relevant over time.

3

u/LongUsername Mar 04 '24

Rust is becoming more relevant all the time, with Microsoft rewriting core Windows libraries and Linux starting to use it in the Kernel.

Then there's Ferrocene, a TUV safety certified Rust Toolchain version.

Espressif is embracing Rust on the ESP processor line.

4

u/DownhillOneWheeler Mar 04 '24

I was referring to microcontrollers. I remain to be convinced of the value of Rust in this domain.

2

u/Eplankton Mar 05 '24

If we only consider those archs which have good llvm support(like cortex-m/a or risc-v) and ignore damn shit archs like pic8/16, then rust could be your friend.

3

u/DownhillOneWheeler Mar 05 '24

One of my concerns at the moment is that you end up importing a large number of crates of unknown quality and provencance for even simple projects. At least, this is my experience of working with Rust on a Linux application. It literally slurps in about 500 crates though transitive dependencies. This is not great for medical. We already had a massive headache with build reproducibility because a specific version of some crate we don't use directly was yanked from crates.io.

The Rust language itself seems an excellent replacement for C. I'm enjoying learning it a little better, but I am finding it rather frustrating coming decades of C++ experience.

The reason I question its value is that I rarely have any problems with memory safety in any case. My evaluation, at least for microcontrollers, is that Rust has little to offer a competent C++ developer. YMMV.

3

u/Eplankton Mar 05 '24

The rust guys have to figure out a way to get crates settled in local environment, like C++ Cmake/Ninja but with better toolchain, there can be a sweetpoint between Cmake/Ninja and Cargo/Npm.

1

u/Mac_Aravan Mar 06 '24

Quite frankly forget C++, I almost never saw it in 30+y of low level embedded.

First C, then fancy some Rust if you want, even if you have almost no chance to find a job outside crypto for the time being.

2

u/DownhillOneWheeler Mar 07 '24

And yet I have used C++ on most of my projects for many years (all bare metal or FreeRTOS). Almost all of my former colleagues transitioned to C++ during that time. I got my new job (mainly medical devices) largely based on my C++ experience, and my new team are all C++ developers. It really depends on the target devices.

A lot of older less capable devices (e.g. 8-bits) have poor or non-existent support for C++ (ditto Rust), so C (or, Heaven forfend, assembly) is your only option. But for more modern 32-bit devices such as Cortex-M, C++ is well-supported and a much better choice than C.

Rust is certainly interesting but very small potatoes at the moment. I know only one person who uses it professionally on microcontrollers. C++ has not replaced C although it could/should have done so decades ago. It will be interesting to see how Rust fares.

2

u/nila247 Mar 04 '24

I feel "embedded" for most people is just like "old-PCs" with 100 megabytes of flash and 2 megabytes of RAM. So they can slap linux or at least some RTOS and do not worry much about resources - just about OOP and Fengshui in general. Good for you.

If you take lower end of embedded we are talking 32-64K flash and 8-32K RAM. This is where your typical RTOS start to struggle and you can pretty much forget all the fancy languages and go back beg forgiveness from C.

2

u/lineofchimes Mar 04 '24

Zig is up and coming for low level applications. Has a conversion for C/C++ https://ziglang.org/

6

u/CaptainJack42 Mar 04 '24

I second rust, I've started using it for new projects for a while now and it's been great (ESP32, stm32) and I'd use rust over C pretty much anytime if I have the choice

3

u/DatBoi_BP Mar 04 '24

Any hurdles using Rust with those MCUs that one wouldn’t have using C instead?

1

u/CaptainJack42 Mar 04 '24

Not really, esp uses a fork of the rust tool chain, but it does so for C as well and installing the rust tool chain is way more straightforward than the C one. For stm32 it depends on the HAL, I've used the stm32XXxx-hal by stm32-rs which tend to miss some features for which I usually created a PR upon implementing it and I've recently tried embassy-rs for a bit which looks more complete plus the async runtime is pretty cool, haven't looked too deep into it yet though. For stm setting up debugging was also about the same effort than it would be when using C without STM32CubeIDE, for ESP I haven't bothered yet to properly set up debugging, but I have never bothered when writing C on it either since debugging ESP is a pita anyways

1

u/war-armadillo Mar 04 '24

I think you don't need a forked compiler if you use a RISC-V core.

1

u/Mac_Aravan Mar 06 '24

Only newer ESP are risc-v (I think they are fully transitioning to risc-v).

Old one still use Xtensa cores, which support is pretty low.

4

u/WereCatf Mar 04 '24

Embedded, as in devices running Linux, C, C++ and Python are the most popular ones followed by Rust, Java and JavaScript (via Node.js). There isn't really much point in learning anything else, unless actually required by a client or your workplace. Besides which, actually mastering 6 different languages is a really big task, not to mention keeping abreast of any new developments wrt. those languages, so you'll basically have your hands full for the rest of your life just with those.

For microcontroller-devices that cannot run a full-blown OS, it's basically just C and C++.

1

u/war-armadillo Mar 04 '24

Rust is definitely in the latter category too. It's well suited for bare-metal.

2

u/WereCatf Mar 04 '24

I didn't say anything about it not being suited for it, just that it's not yet that popular. It is gaining momentum, but it's still kind of niche from what I've seen.

2

u/war-armadillo Mar 04 '24

OP asked about suitability, not popularity, and your comment seems to imply that Rust cannot be run without a full-blown OS (although I understand that's not what you meant). But, fair enough :)

1

u/WereCatf Mar 04 '24

Well, I'll accept the criticism. I should've mentioned Rust, there.

2

u/rpkarma Mar 04 '24

Even if you use Nim, like we do at work, we have to interface with so much C/C++ that you simply must learn it first.

Rust is the real answer in this space; but again knowing C will be useful here for understanding.

1

u/Bitwise_Gamgee Mar 04 '24

If you won't use C, learn assembly (or a good grasp of Assembly) for the family of chips you're interested in working on.

1

u/geekguy Mar 04 '24

Apparently the White House no longer likes C and C++. Alternative for system level would be Rust, but I’ve never used it in any projects.

1

u/[deleted] Mar 04 '24

Holy C

1

u/bronco2p Mar 04 '24

rust, zig, cpp, odin (niche), maybe D (also niche)? But with ML Mojo could be good once it fully releases.

1

u/Sheepherder-Optimal Mar 04 '24

Python is extremely useful. Learning about how to build a website, like completely from scratch. I don't mean just designing some front end. Like implement an embedded web server.

1

u/PeterMortensenBlog Mar 04 '24

Embedded where?

1

u/Sheepherder-Optimal Mar 05 '24

Embedded web server meaning you implement the web APIs yourself, like get, post, etc. you use the absolute basics to create a server embedded in whatever MCU you want. Probably would need an ftp server as well.

0

u/Green_Concentrate427 Mar 04 '24 edited Mar 04 '24

Some companies that produce boards are actively creating Rust libraries. For example, Espressif.

6

u/Sushantnaik21 Mar 04 '24

Esp can't be held as an example for whole embedded market.

4

u/Green_Concentrate427 Mar 04 '24

I fixed my statements: "Some companies."

Better?

0

u/PowerOfTheShihTzu Mar 04 '24

Rust is the real deal nowadays it seems

0

u/Environmental_Two_68 Mar 04 '24

My suggestion would be to learn a functional language like racket. While it’s not practical useful for embedded, it will teach you a different perspective on programming that in the long term would be useful than a specific language.

1

u/PeterMortensenBlog Mar 04 '24

Racket. E.g., "The Racket language is a modern dialect of Lisp and a descendant of Scheme.".