Sharing code base between ESP32 and arduino
Posted: 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:
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:
Thanks.
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
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