Page 1 of 1

ULP GPIO Control ESP32 DeepSleep

Posted: Fri Feb 21, 2020 7:58 am
by engrkhawaja
Hi,

I am to control GPIO33 in ULP while ESP32 in Deep Sleep.

Issue is GPIO value changes while I am not changing the value. May be its reset or anything else.

Please guide me where is issue or give me simple program that I can control GPIO inside ULP while main processor Deep Sleep.

Main Program.

Code: Select all

#include "esp32/ulp.h"
// include ulp header you will create
#include "ulp_main.h"
// must include ulptool helper functions also
#include "ulptool.h"
#include "driver/rtc_io.h"
#include "soc/rtc.h"
#include "soc/rtc_cntl_reg.h"
#include "soc/rtc_io_reg.h"
// Unlike the esp-idf always use these binary blob names
extern const uint8_t ulp_main_bin_start[] asm("_binary_ulp_main_bin_start");
extern const uint8_t ulp_main_bin_end[]   asm("_binary_ulp_main_bin_end");

static void init_run_ulp(uint32_t usec);


void setup() {

  Serial.begin(115200);
  delay(1000);
      esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause();
      switch(cause)
  {
    case ESP_SLEEP_WAKEUP_EXT0 : Serial.printf("Wakeup caused by external signal using RTC_IO"); break;
    case ESP_SLEEP_WAKEUP_EXT1 : Serial.printf("Wakeup caused by external signal using RTC_CNTL"); break;
    case ESP_SLEEP_WAKEUP_TIMER : Serial.printf("Wakeup caused by timer"); break;
    case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.printf("Wakeup caused by touchpad"); break;
    case ESP_SLEEP_WAKEUP_ULP : Serial.printf("Wakeup caused by ULP program"); break;
    default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",cause); break;
  }
    if (cause != ESP_SLEEP_WAKEUP_ULP) {
        Serial.printf("Not ULP wakeup, initializing ULP\n");
        init_run_ulp(5000*1000);
    } else {
  
   
    }

    Serial.printf("Entering deep sleep\n\n");
    start_ulp_program();
  // rtc_gpio_hold_en(GPIO_NUM_33);
    ESP_ERROR_CHECK( esp_sleep_enable_ulp_wakeup() );
    
    esp_deep_sleep_start();
}
int i;
void loop() {

}

static void init_run_ulp(uint32_t usec) {
   esp_err_t err = ulptool_load_binary(0, ulp_main_bin_start, (ulp_main_bin_end - ulp_main_bin_start) / sizeof(uint32_t));
    rtc_gpio_init(GPIO_NUM_33);
  rtc_gpio_set_direction(GPIO_NUM_33, RTC_GPIO_MODE_OUTPUT_ONLY);
   rtc_gpio_init(GPIO_NUM_32);
  rtc_gpio_set_direction(GPIO_NUM_32, RTC_GPIO_MODE_INPUT_ONLY);
  ulp_set_wakeup_period(0, usec);
 
 // err = ulp_run((&ulp_entry - RTC_SLOW_MEM) / sizeof(uint32_t));

  //if (err) Serial.println("Error Starting ULP Coprocessor");
  
}



/*
Method to print the reason by which ESP32
has been awaken from sleep
*/
void print_wakeup_reason(){
  esp_sleep_wakeup_cause_t wakeup_reason;

  wakeup_reason = esp_sleep_get_wakeup_cause();

  switch(wakeup_reason)
  {
    case ESP_SLEEP_WAKEUP_EXT0 : Serial.printf("Wakeup caused by external signal using RTC_IO"); break;
    case ESP_SLEEP_WAKEUP_EXT1 : Serial.printf("Wakeup caused by external signal using RTC_CNTL"); break;
    case ESP_SLEEP_WAKEUP_TIMER : Serial.printf("Wakeup caused by timer"); break;
    case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.printf("Wakeup caused by touchpad"); break;
    case ESP_SLEEP_WAKEUP_ULP : Serial.printf("Wakeup caused by ULP program"); break;
    default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break;
  }
}

static void start_ulp_program()
{
    /* Reset sample counter */
    

    /* Start the program */
    esp_err_t err = ulp_run((&ulp_entry - RTC_SLOW_MEM) / sizeof(uint32_t));
    ESP_ERROR_CHECK(err);
}
ULP Code

Code: Select all

#ifdef _ULPCC_
#include <ulp_c.h>
#include "soc/rtc_cntl_reg.h"
#include "soc/rtc_io_reg.h"
// global variable that the main processor can see

unsigned data1 = 0;
unsigned data2 = 0;
unsigned status1 = 0xFFFF;
unsigned i;

// all ulpcc programs have have this function
void entry() {

 wake ();
  halt();
}


#endif