A timing app designed to run on my Odroid-GO
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.3 KiB

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include "driver/gpio.h"
#include "driver/uart.h"
#include "esp_log.h"
#include "esp_sleep.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "input.h"
#define BUTTON_WAKEUP_LEVEL_DEFAULT 0
// Returns whether button was pressed to wake
// See Dependencies/esp-idf/examples/system/light_sleep/main/light_sleep_example_main.c
bool Odroid_EnterLightSleep(int64_t timerAutoWakeupMicroSecs)
{
esp_sleep_enable_timer_wakeup(timerAutoWakeupMicroSecs);
const int wakeup_level = BUTTON_WAKEUP_LEVEL_DEFAULT;
gpio_config_t config = {.pin_bit_mask = (1ULL << BUTTON_PIN_A) | (1ULL << BUTTON_PIN_B) |
(1ULL << BUTTON_PIN_START) |
(1ULL << BUTTON_PIN_SELECT) |
(1ULL << BUTTON_PIN_VOLUME) | (1ULL << BUTTON_PIN_MENU),
.mode = GPIO_MODE_INPUT,
.pull_up_en = GPIO_PULLUP_ENABLE};
ESP_ERROR_CHECK(gpio_config(&config));
gpio_num_t buttonsToWatch[] = {BUTTON_PIN_A, BUTTON_PIN_B, BUTTON_PIN_START,
BUTTON_PIN_SELECT, BUTTON_PIN_VOLUME, BUTTON_PIN_MENU};
for (int i = 0; i < sizeof(buttonsToWatch) / sizeof(buttonsToWatch[0]); ++i)
{
gpio_wakeup_enable(buttonsToWatch[i],
wakeup_level == 0 ? GPIO_INTR_LOW_LEVEL : GPIO_INTR_HIGH_LEVEL);
}
esp_sleep_enable_gpio_wakeup();
printf("Entering light sleep\n");
/* To make sure the complete line is printed before entering sleep mode, */
/* need to wait until UART TX FIFO is empty: */
uart_wait_tx_idle_polling(CONFIG_ESP_CONSOLE_UART_NUM);
/* Get timestamp before entering sleep */
int64_t t_before_us = esp_timer_get_time();
esp_light_sleep_start();
/* Get timestamp after waking up from sleep */
int64_t t_after_us = esp_timer_get_time();
/* Determine wake up reason */
const char* wakeup_reason;
esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause();
switch (cause)
{
case ESP_SLEEP_WAKEUP_TIMER:
wakeup_reason = "timer";
break;
case ESP_SLEEP_WAKEUP_UNDEFINED:
wakeup_reason = "undefined";
break;
case ESP_SLEEP_WAKEUP_ALL:
wakeup_reason =
"Not a wakeup cause, used to disable all wakeup sources with "
"esp_sleep_disable_wakeup_source.";
break;
case ESP_SLEEP_WAKEUP_EXT0:
wakeup_reason = "external signal using RTC_IO.";
break;
case ESP_SLEEP_WAKEUP_EXT1:
wakeup_reason = "external signal using RTC_CNTL.";
break;
case ESP_SLEEP_WAKEUP_TOUCHPAD:
wakeup_reason = "touchpad";
break;
case ESP_SLEEP_WAKEUP_ULP:
wakeup_reason = "ULP program";
break;
case ESP_SLEEP_WAKEUP_GPIO:
wakeup_reason = "GPIO (light sleep only)";
break;
case ESP_SLEEP_WAKEUP_UART:
wakeup_reason = "UART (light sleep only)";
break;
default:
wakeup_reason = "other";
break;
}
printf("Returned from light sleep, reason: %s, t=%lld ms, slept for %lld ms\n", wakeup_reason,
t_after_us / 1000, (t_after_us - t_before_us) / 1000);
for (int i = 0; i < sizeof(buttonsToWatch) / sizeof(buttonsToWatch[0]); ++i)
{
gpio_wakeup_disable(buttonsToWatch[i]);
}
/// Reconfigure GPIO to our desired settings
Odroid_InitializeInput();
return cause == ESP_SLEEP_WAKEUP_GPIO;
}