cpp template using freertos and external cpp libraries

DL88AI88
Posts: 29
Joined: Mon Aug 29, 2016 8:26 am

cpp template using freertos and external cpp libraries

Postby DL88AI88 » Sun Mar 26, 2017 3:01 pm

Hi there,

I noticed that there are some cpp skeletons out there like these:
e.g.
https://github.com/nkolban/esp32-snippe ... letons/cpp
in conjunction with these
  • snippets
(works fine for me)
e.g.
https://github.com/pcbreflux/espressif/ ... _cpp_hello
(haven't tested yet by myself)

I want to include already existing cpp libraries from arduino in my cpp project running on freertos.
Now I have 2 questions:
How do I run freertos in a cpp environment?
Can I add cpp libraries from arduino (written for an esp32 basis) as simple as I can with c-files?
I know these questions are a bit shady, but above all I am looking for an esp32 cpp/freertos template in an eclipse environment.

Any kind of hint is welcome.

best regards

DL88AI88

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: cpp template using freertos and external cpp libraries

Postby kolban » Sun Mar 26, 2017 7:32 pm

Howdy,
Think of any application you write as running "within" a FreeRTOS environment by default. When the ESP32 loads (with the standard bootloader), FreeRTOS is started including the scheduler. All the FreeRTOS libraries are available to your application.

By default, user code (i.e. code you write) is given control through the function you export called "app_main()". Within your code you can use all the FreeRTOS functions you like. The same story is true for both C and C++.

When you compile your application, you are statically linking all the libraries that the code needs to run including FreeRTOS ... but specially, any libraries that you may get from elsewhere also have to be statically linked with your application. When you flash your ESP32 with the app, this also pulls in the libraries that were link-edited with your code.

The ESP32 provides an Arduino environment which is no more and no less than C code that implements the Arduino framework. Instead of YOU having to code "app_main()", that has been coded into the Arduino libraries. Those libraries expect to find two function called "loop" and "setup" (if memory serves me) and it is your responsibility to code those. If you wish to use additional Arduino libraries, you simply link them into your application.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

rsimpsonbusa
Posts: 131
Joined: Tue May 17, 2016 8:12 pm

Re: cpp template using freertos and external cpp libraries

Postby rsimpsonbusa » Tue Mar 28, 2017 4:53 am

I struggled with this myself.

Your main.c should now be called main.cpp.

For C libraries you now need a extern C envelope (look at the beginning of the file).

All esp-libraries are "extern C " protected so no need to put them inside this section BUT must put app_main(), ei your start routine, inside the extern c envelope.

When adding pure C code, like ds18b20.h, put it inside the Extern C includes. ALl other cpp (c++) libraries OUTSIDE of the extern C envelope or you´ll get a lot of compiler errors.

Attach a template I used for testing several features of the ESP32.

There are some Arduino C libraries and some C++ libraries. I put the ESP32 components inside the extern c envelope even though its not required.

Hope it gives you some guidance.

RSN
Attachments
main.cpp.zip
(9.49 KiB) Downloaded 759 times

DL88AI88
Posts: 29
Joined: Mon Aug 29, 2016 8:26 am

Re: cpp template using freertos and external cpp libraries

Postby DL88AI88 » Tue Mar 28, 2017 8:42 am

Hi RSN,

wow, that is really helpful.
Thumbs up!
Thank you.

Best regards

DL88AI88

Who is online

Users browsing this forum: hamzatamer76 and 70 guests