B4R Question Inline-C definition syntax

peacemaker

Expert
Licensed User
Longtime User
Hi, All

Recently i published the code https://www.b4x.com/android/forum/threads/esp32-deepsleep-until-wake-up-by-timer-or-button.167482 that worked for classical ESP32.
But now i'm trying to make esp32-c3 to wake up by the GPIO0 button.

And this does not work. And more over - "Serial.println" from inline-C - does not work at all, and i'm not sure if #ifdef runs...
Any help with the code below ?

B4X:
'if zero - just pin wake up is activated
Public Sub Go_Sleep(ms As ULong)
    Log("Go deep sleep for ", ms, " msecs")
    Delay(10)
    esp32_DeepSleep(ms)
End Sub

Private Sub esp32_DeepSleep(ms As ULong)
    RunNative("deepSleep", ms)
End Sub

#if C
#include <esp_sleep.h>
#include <driver/rtc_io.h>

#define BUTTON_PIN GPIO_NUM_0

void deepSleep(B4R::Object* o) {
  // wake up by timer (msec)
  uint64_t sleep_time_us = o->toULong() * 1000;
  if(sleep_time_us > 0) {
    esp_sleep_enable_timer_wakeup(sleep_time_us);
  }

  #ifdef CONFIG_IDF_TARGET_ESP32
      // ESP32: use EXT0 (just single pin)
      Serial.println("CONFIG_IDF_TARGET_ESP32");
      delay(100);
      esp_sleep_enable_ext0_wakeup(BUTTON_PIN, LOW);
  #elifdef defined(CONFIG_IDF_TARGET_ESP32S2) || defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32S3)
          Serial.println("CONFIG_IDF_TARGET_ESP32++++");
        delay(100);
      // ESP32-S2 / C3 / S3: use EXT1 (pins mask)
      uint64_t ext1_mask = 1ULL << BUTTON_PIN;
      esp_sleep_enable_ext1_wakeup(ext1_mask, ESP_SLEEP_EXT1_WAKEUP_ALL_LOW); //ESP_SLEEP_EXT1_WAKEUP_ANY_HIGH
  #endif
    delay(100);
  // going To Deep Sleep
  esp_deep_sleep_start();
 }
#end if

Usage is just

B4X:
    Log("going to sleep")
    others.Go_Sleep(0)

New upload port: COM14 (serial)
---------------------
AppStart
ESP.ResetReasonMessage = Restarted 11: Unknown reset reason (11)
Device_ID = F0F5BDFDD470_v0.20
MAGIC_EEPROM = 213
This device name = aiFonar_F0F5BDFDD470_v0.20
RTC read BatCapacity = 0
From RTC BatCapacity = 0, try from NVS
From NVS BatCapacity = 59.8000
RTC saved BatCapacity = 59.8000
Configuring WDT...
Last Reset : ESP_OK
WDT is configured
going to sleep
Go deep sleep for 0 msecs
 

peacemaker

Expert
Licensed User
Longtime User
It was always enabled "USB mode: CDC & JTAG" and CDC on boot.
BTW, "CDC on boot" option is very curious on the screenshot (two ENABLED options !), for the board esp32c3 super mini (i really have):
1751882903385.png



But if to use "ESP32C3 Dev Module" - options are different, separated CDC on boot and JTAG.
1751883627504.png


And no any difference what board to choose (and any JTAG option): flashing is OK, log from B4R is OK, but no log from inline-C and no wake up by the button.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
So...this one inline-C - does not work, #ifdef does not work:
bad code:
#if C
#include <esp_sleep.h>
#include <driver/rtc_io.h>

#define BUTTON_PIN GPIO_NUM_0

void deepSleep(B4R::Object* o) {
  // wake up by timer (msec)
  uint64_t sleep_time_us = o->toULong() * 1000;
  if(sleep_time_us > 0) {
    esp_sleep_enable_timer_wakeup(sleep_time_us);
  }

  #ifdef CONFIG_IDF_TARGET_ESP32
      // ESP32: use EXT0 (just single pin)
      Serial.println("CONFIG_IDF_TARGET_ESP32");
      delay(100);
      esp_sleep_enable_ext0_wakeup(BUTTON_PIN, LOW);
  #elifdef defined(CONFIG_IDF_TARGET_ESP32S2) || defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32S3)
          Serial.println("CONFIG_IDF_TARGET_ESP32++++");
        delay(100);
      // ESP32-S2 / C3 / S3: use EXT1 (pins mask)
      uint64_t ext1_mask = 1ULL << BUTTON_PIN;
      //esp_sleep_enable_ext1_wakeup(ext1_mask, ESP_GPIO_WAKEUP_GPIO_LOW); //these two: ESP_SLEEP_EXT1_WAKEUP_ANY_LOW, ESP_EXT1_WAKEUP_ALL_LOW = do not work
      esp_deep_sleep_enable_gpio_wakeup(ext1_mask, ESP_GPIO_WAKEUP_GPIO_LOW);
  #endif
  // going To Deep Sleep
  esp_deep_sleep_start();
 }
#end if

But without #ifdef - it works ! even "Serial.println" also works:

Works without #ifdef:
#if C
#include <esp_sleep.h>
#include <driver/rtc_io.h>

#define BUTTON_PIN GPIO_NUM_0

void deepSleep(B4R::Object* o) {
  // wake up by timer (msec)
  uint64_t sleep_time_us = o->toULong() * 1000;
  if(sleep_time_us > 0) {
    esp_sleep_enable_timer_wakeup(sleep_time_us);
  }

//  #ifdef CONFIG_IDF_TARGET_ESP32
//      // ESP32: use EXT0 (just single pin)
//      Serial.println("CONFIG_IDF_TARGET_ESP32");
//      delay(100);
//      esp_sleep_enable_ext0_wakeup(BUTTON_PIN, LOW);
//  #elifdef defined(CONFIG_IDF_TARGET_ESP32S2) || defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32S3)
          Serial.println("CONFIG_IDF_TARGET_ESP32++++");
        delay(100);
      // ESP32-S2 / C3 / S3: use EXT1 (pins mask)
      uint64_t ext1_mask = 1ULL << BUTTON_PIN;
      esp_deep_sleep_enable_gpio_wakeup(ext1_mask, ESP_GPIO_WAKEUP_GPIO_LOW);
//  #endif
  // going To Deep Sleep
  esp_deep_sleep_start();
 }
#end if

New upload port: COM14 (serial)
---------------------
AppStart
ESP.ResetReasonMessage = Restarted 11: Unknown reset reason (11)
Device_ID = F0F5BDFDD470_v0.21
MAGIC_EEPROM = 213
This device name = aiFonar_F0F5BDFDD470_v0.21
RTC read BatCapacity = 0
From RTC BatCapacity = 0, try from NVS
From NVS BatCapacity = 59.8000
RTC saved BatCapacity = 59.8000
Configuring WDT...
Last Reset : ESP_OK
WDT is configured
going to sleep
Go deep sleep for 0 msecs
CONFIG_IDF_TARGET_ESP32++++
And waking up from deepsleep by GPIO0 button - is ok !
But it's interesting to have the universal code for various MCU models, with #ifdef.

@Erel , could you comment it ? What's wrong with #ifdef inside the Inline-C? Always compiled OK, but does not work.
 
Upvote 0
Top