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.
 
 
 
 

64 lines
1.9 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"
// Button B
#define BUTTON_GPIO_NUM_DEFAULT GPIO_NUM_32
#define BUTTON_WAKEUP_LEVEL_DEFAULT 1
// 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 button_gpio_num = BUTTON_GPIO_NUM_DEFAULT;
const int wakeup_level = BUTTON_WAKEUP_LEVEL_DEFAULT;
gpio_config_t config = {.pin_bit_mask = BIT64(button_gpio_num), .mode = GPIO_MODE_INPUT};
ESP_ERROR_CHECK(gpio_config(&config));
gpio_wakeup_enable(button_gpio_num,
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_GPIO:
wakeup_reason = "pin";
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);
return cause == ESP_SLEEP_WAKEUP_GPIO;
}