Sharing code base between ESP32 and arduino

zliudr
Posts: 204
Joined: Thu Oct 03, 2019 5:15 am

Sharing code base between ESP32 and arduino

Postby zliudr » Wed Jul 01, 2020 11:01 pm

Does the ESP-IDF #define any variables like ESP_IDF, ESP32 etc. to pass to the compiler?

I'm developing a project where I use ESP32 as the main processor and have ATMEGA MCUS such as 32u4 and 328p do peripherals. In developing the code, there are three firmware, one on each processor. It's a lot of work to keep them separate so I decide to share some classes among the three projects. I'm using Arduino IDE for the ATMEGA MCUs and ESP-IDF release 3.3 for ESP32. In Arduino IDE, it #define ARDUINO as an IDE version number so you can do conditional compilation depending on version. In order to share class code, things like heap_caps_malloc() needs to be changed into malloc() so I am using:

Code: Select all

#ifdef ARDUINO
	uint8_t *ddata=(uint8_t*)malloc(USB_DESCRIPTOR_MAX_LEN*4); // Allocate 1024 bytes of memory. Longest one is Honeywell 7580g 646 bytes
#else	
	uint8_t *ddata=(uint8_t*)heap_caps_malloc(USB_DESCRIPTOR_MAX_LEN*4,MALLOC_CAP_SPIRAM); // Allocate 1024 bytes of memory. Longest one is Honeywell 7580g 646 bytes
#endif
I wonder if there is a #define like ARDUINO in ESP-IDF to detect the code is compiled under ESP-IDF so instead of switching at the definition of ARDUINO, I can do it with something like ESP32or ESP_IDF. This makes my code more ESP32-centric and more readable like:

Code: Select all

#if defined(ARDUINO)
	uint8_t *ddata=(uint8_t*)malloc(1024);
#elif defined(ESP32)
	uint8_t *ddata=(uint8_t*)heap_caps_malloc(1024,MALLOC_CAP_SPIRAM);
#else
//blah maybe using online compilers to test functionality
#endif
Thanks.

nvannote
Posts: 51
Joined: Thu Nov 14, 2019 10:42 pm

Re: Sharing code base between ESP32 and arduino

Postby nvannote » Thu Jul 02, 2020 12:46 am

ESP_PLATFORM is defined.

In addition there is the ESP_IDF_VERSION series of macros defined in esp_idf_version.h. ESP_IDF_VERSION_MAJOR, ESP_IDF_VERSION_MINOR, etc.

https://docs.espressif.com/projects/esp ... _major#id4

So you can write conditionals like.

Code: Select all

#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 0, 0)
...
#endif
Best Regards

zliudr
Posts: 204
Joined: Thu Oct 03, 2019 5:15 am

Re: Sharing code base between ESP32 and arduino

Postby zliudr » Thu Jul 02, 2020 3:03 pm

Great! Thank you! I've been using:

Code: Select all

 'EXTRA_CPPFLAGS+=-DBLAHBLAH_IS_ESP32=1'
It's good to have the official #define

This is not very important but are these listed in the document somewhere I can read on my own?
Must be not waking up! You provided the link right there!

zliudr
Posts: 204
Joined: Thu Oct 03, 2019 5:15 am

Re: Sharing code base between ESP32 and arduino

Postby zliudr » Thu Jul 02, 2020 3:52 pm

OK, since I'm using release 3.3, I couldn't find the esp_idf_version.h file. I could do some digging and testing that macro, but ESP_PLATFORM already works. Thanks again!

Who is online

Users browsing this forum: Google [Bot] and 33 guests