I am looking for anyone to chime in on what programming language they use on ARM microcontrollers (STM32, LPC, Stellaris, etc): C or C++. I have always used C, but C++ has some great advantages. What do you think?
Great question. I know why I am interested in using C++ (stronger type checking, OOP model), but I want to hear more from the community about what they are using.
My 2c worth.
Most of the programming logic that would be running on a micro-controller is procedural in nature so C makes a good fit because of its smaller footprint as a bit-banging language. IMO pushing the OOP model into this class of programming problems seems to be driven by dogma instead of practical application and most of what's possible in C++ on the small uC level (Modular design ,Encapsulation,Polymorphism) is possible within C with proper coding.
I am in the middle of writing a GUI on an ARM chip.
It includes windows, icons, buttons, menus etc.
I could benefit by using OOP but I can manage just fine in C.
I should say the GUI part is 95% completed. Total code space is 30kB.
Cpp is certainly not going to add much benefit for small mcu projects (say less than 16kb). But mid size (64k to 1mb) could certainly benefit from cpp.
We now have 2 votes for c and perhaps a half vote for cpp.
When working with multi user, or graphical UI systems, Object Oriented makes some tasks simpler, at the price of a much higher footprint.
Even programs in "C++" for microcontrollers use the standard C library, rather than object methods and properties. It's only when doing that gets "hard" do people make an object and mix it in with the rest of the code.
Arduino is a great example. It was built to be programmed in C++, but nearly all applications use only the C functions unless dealing with a serial instance. In fact, the arduino forum recommends Against using the String object in favor of the standard C library string functions, especially when running out of program space.
So, C for embedded. When the user is messing with a GUI and changing apps, the application is no longer embedded, the user knows they are working with a computer.
C++ is just a way of doing things in C, not a new language. Early C++ compilers boiled down the C++ constructs to C.
For small programs, single task, single user, C++ does not make too much sense.
If you have more than 100 MHz, and some 100K memory + a large RAM, you can spawn stuff on the heap.
With a KByte of RAM, how can you do this?
All you can do is to use static C++ for class declarations. As I say, just a way of using C.
The descision what you use should be made by the type of application.
Unless you deal with a larger controller with >100 MHz, MCUs are often pretty much maxed out for performance, or the clocking is reduced to save power. C++ is working in the opposite direction.
It makes sense for a GUI like Windows, or a system which resembles it.
Talk about your software projects, and then about C/C++ on microcontrollers.
Or do you just want to learn C++? A microcontroller is not ideal for that.
I actually do a lot of c programming on micro controllers. There are few things about the language that I don't know. However, there is a lot about cpp that I don't know. Recently, I have experimented with squeezing the cpp footprint by telling the compiler to omit exceptions and run time type information. The footprint does get down to a manageable size. However any use of the standard template libraries can quickly make it balloon again.
I am working on with an arm cortex m3 with a 120mhz clock and 64k of ram. It is plenty to run cpp.
Arduino is an interesting example. The original arduino probably should not be running cpp, but the cortex m3 version should be able to.
I think cpp has some powerful features that allow for greater re-use ability of code as compared to c. I don't know enough about cpp to know if these features require a lot of runtime resources that would prohibit their use in embedded systems.
I recently used Cortex M4 for a project in C++ which was the first time for an embedded project.
I actually did a lot a testing because I was nervous about the move. I can say in my experience I only gained from using C++.
As someone mentioned about if you declare all objects statically, and used classes then its just a way of using C.
Since the project was complex and large, then C++ made sense. It might have had a slightly larger footprint I cant remember, but I can conclusively say that this being a performance intensive project C++ actually gave me some performance improvement over C, exactly the same code the only difference was that calls were being made within a class rather than globally as in C.
Can I also say that all C code compiles on the C++ compiler. So I see it as just an extention.
For me just to use it like C but with the advantages of classes makes for far better design and maintainability.
Yes you can do all of these things in plain C. If you use C++ I beleive these features of class, inheritance and encapsulation are less easily broken by other engineers and your intention to use these features is clearly defined.
For a very small project i'd probably stick with C. But for larger complicated designs I would now always use C++. It just becomes a lot more manageable as the project grows in requirements and complexity.
But for small microcontrollers, stick to statically declared objects.