17 Mart 2024 Pazar

C++ for Embedded Development

Over the last three years, I am trying to use C++ as an embedded software developer. During the past year, I have fully used C++. I have watched lots of video and read lots of article in this period. I want to share what I have learnedmy notes and some important topics according to me. 

If you continue to read this, you likely want to learn C++. There is an excellent lecture on YouTube. In today’s world, anyone can complain many issues but cannot complain about not being able to find resources and information. There is an excellent lecture from University of Bohm. In my opinion, these ten-video lecture series are outstanding starting point for learning C++. Thanks Igor Bogoslavskyi to make these videos public.

 

Lecture: Modern C++ (Summer 2018, Uni Bonn)

 

 

He also has his own YouTube channel if you wanna subscribe: 


https://m.youtube.com/c/Codeforyourself

 

C++ is a complex language. It is not necessary to learn every concept. For example, I avoid using any standart library. My advice is not to read reference book written by Bjarne Stroustrup or any other. This is not an ideal way for an embedded software developer. This is a language and it is enough to just focus on learning the parts that you will use. Otherwise, you will be overwhelm by complexity of C++. For embedded side, my suggestion is also not to use RTTI and exceptions. However, if you are interested in exception handling, there is an excellent video:

 

C++ exception handling

 

 

Most current SDKs only supports a subset of C++ functionality. Generally, wcan use C++ to wrap the SDK's C-API.

 

Now, you watched all these ten-video lecture series prepared by Igor Bogoslavskyi and learn lots of concepts. You might wonder how to apply all of these concepts on embedded sideThere is an excellent video on youtube as a starting projectC++ for the Embedded Programmer

 

The video is using C++ to set up a pin by using ST’s SDK. But, you can use any other SDK’s like Nodic, Texas so on. I used Zephyr RTOS. You will see that it is possible to use C and C++ in tandem. In embedded world, all SDKs will continue to use C as a main language. At the same time, all of these SDKs will increasingly supports C++. You can use these SDKs to create your own C++ classes. I don’t expect to switch completely from C to C++ in the near future. On the other hand, I am waiting that usage of the C++ on embedded environment will grow day by day due to its advantages like rich language features, type safety, easy TDD support and so on. You can check my sample on this link: https://github.com/srcnert/Zephyr_GPIO_Cpp. I used operator overloading and I wrap Zephyr's GPIO API by using C++.

 

Let’s consider another example to show advantage of C++. Let's think that we have two temperature sensor like AHT20 and HDC3020 and we want to select these two accordingly. This is a good example of polymorpishm. It is so easy to maintain code through object-oriented design via C++. Of course, you can do same thing with C but it will be a little bit complex.

 

Firstly, we will create a general drvTEMP class:

 

#ifndef drvTEMP_H__

#define drvTEMP_H__

#include <stdint.h>

class drvTEMP

{

  public:

    virtual bool begin() = 0;

    virtual void getTemperatureHumidity(float *tempfloat *hum) = 0;

};

#endif

 

After that, you can crete specific classes for each sensor.

 

class HDC302x : public drvTEMP 

{

    private:

        uint8_t address = 0;

        drvI2C *i2c;

 

    public:

        HDC302x(uint8_t addressdrvI2C *i2c);

 

        bool begin();

        void getTemperatureHumidity(float *tempfloat *hum);

};

 

 

class AHT20 : public drvTEMP

{

  private:

    uint8_t address 0;

    drvI2C *i2c;

 

  public:

    AHT20(uint8_t addressdrvI2C *i2c);

    

    bool begin();

    void getTemperatureHumidity(float *tempfloat *hum);

};

 

Finally, you can select your temprature sensor at the beginning of your main file and there is no need to change your code for different setup inside all of your different files. C++ offers lots of advantages.

 

drvTEMP *temp_sensor = new AHT20(eAHT20Address_Low, &i2c0);

drvTEMP *temp_sensor = new HDC302x(HDC302X_ADDRESS1, &i2c0);

 

Are you using C++ as an embedded software developer? Do you know other advantages or have suggestions for other good concepts? I am still new for C++, so if you have corrections or input, please share them in comments section below.

 

Sources:

 

C++ for Embedded Development

C++Now 2018: Michael Caisse “Modern C++ in Embedded Systems”

Modern C++: C++ Patterns to Make Embedded Programming More Productive - Steve Bush - CppCon 2022

C++ in the World of Embedded Systems - Vladimir Vishnevskii - CppCon 2022

Composition on Tiny Embedded Systems in C++ - Luke Valenty - CppNow 2023