#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_log.h"
#include "driver/uart.h"
#include "driver/gpio.h"
#include "driver/gpio.h"
// #include "app_ble.h"
#include "esp_pm.h"
#include "esp_sleep.h"
#include "soc/uart_periph.h"
#include "soc/rtc_cntl_reg.h"
#include "esp_timer.h"
SemaphoreHandle_t init_finish_semaphore;
QueueHandle_t event_Queue;
#define pdSECOND pdMS_TO_TICKS(1000)
#define GPIO_OUTPUT_IO_1 2
#define GPIO_OUTPUT_IO_3 4
#define GPIO_OUTPUT_IO_4 6
#define GPIO_OUTPUT_IO_5 10
#define GPIO_OUTPUT_PIN_SEL ((1ULL << GPIO_OUTPUT_IO_1 | 1ULL << GPIO_OUTPUT_IO_3 | 1ULL << GPIO_OUTPUT_IO_4))
#define GPIO_INPUT_PIN_SEL ((1ULL << GPIO_OUTPUT_IO_5))
#define ESP_INTR_FLAG_DEFAULT 0
#define event_REPORT (uint8_t)0
void timer_init();
void pin_Setup();
void lock_isr_handler(void *arg);
uint32_t counter_isr = 0;
void uart_thread(void *)
{
uint8_t *data = (uint8_t *)malloc(sizeof(1024));
uint8_t event;
int32_t result = 0;
xSemaphoreTake(init_finish_semaphore, portMAX_DELAY);
ESP_LOGI("DBG", "enter event loop");
while (1)
{
xQueueReceive(event_Queue, &event, portMAX_DELAY);
switch (event)
{
case event_REPORT:
ESP_LOGI("DBG", "INTR TRIGGERED");
vTaskDelay(pdMS_TO_TICKS(3000));
gpio_isr_handler_add(GPIO_NUM_10, lock_isr_handler, (void *)0);
}
}
}
void lock_isr_handler(void *arg)
{
// ESP_LOGI("LOCK_PIN","Interupt triggered");
// is_reporting++;
gpio_isr_handler_remove(GPIO_NUM_10);
counter_isr++;
uint8_t event = event_REPORT;
xQueueSend(event_Queue, &event, portMAX_DELAY);
}
void pin_Setup()
{
gpio_config_t io_conf;
//disable interrupt
io_conf.intr_type = GPIO_INTR_DISABLE;
//set as output mode
io_conf.mode = GPIO_MODE_INPUT_OUTPUT;
//bit mask of the pins that you want to set,e.g.GPIO18/19
io_conf.pin_bit_mask = GPIO_OUTPUT_PIN_SEL;
//disable pull-down mode
io_conf.pull_down_en = GPIO_PULLDOWN_ENABLE;
//disable pull-up mode
io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
//configure GPIO with the given settings
gpio_config(&io_conf);
io_conf.mode = GPIO_MODE_INPUT;
io_conf.intr_type = GPIO_INTR_ANYEDGE;
io_conf.pin_bit_mask = GPIO_INPUT_PIN_SEL;
io_conf.pull_down_en = GPIO_PULLDOWN_ENABLE;
io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
gpio_config(&io_conf);
gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);
gpio_isr_handler_add(GPIO_NUM_10, lock_isr_handler, (void *)GPIO_NUM_10);
// gpio_sleep_sel_dis(GPIO_NUM_10);
gpio_wakeup_enable(GPIO_NUM_4, GPIO_INTR_HIGH_LEVEL);
esp_sleep_enable_gpio_wakeup();
printf("Minimum free heap size: %d bytes\n", esp_get_minimum_free_heap_size());
}
static void periodic_timer_callback(void *arg)
{
int64_t time_since_boot = esp_timer_get_time();
ESP_LOGI("timer", "Periodic timer called, time since boot: %lld us", time_since_boot);
uint8_t event = event_REPORT;
xQueueSend(event_Queue, &event, portMAX_DELAY);
}
void timer_init()
{
const esp_timer_create_args_t periodic_timer_args = {
.callback = &periodic_timer_callback,
/* name is optional, but may help identify the timer when debugging */
.name = "periodic"};
esp_timer_handle_t periodic_timer;
ESP_ERROR_CHECK(esp_timer_create(&periodic_timer_args, &periodic_timer));
ESP_ERROR_CHECK(esp_timer_start_periodic(periodic_timer, (uint64_t)(86400000000)));
/* Print debugging information about timers to console every 2 seconds */
ESP_ERROR_CHECK(esp_timer_dump(stdout));
// usleep(1000000);
/* Timekeeping continues in light sleep, and timers are scheduled
* correctly after light sleep.
*/
// ESP_LOGI("timer", "Entering light sleep for 0.5s, time since boot: %lld us",
// esp_timer_get_time());
// ESP_ERROR_CHECK(esp_sleep_enable_timer_wakeup((uint64_t)(100 * 12 * 60 * 60 * 1000 * 1000)));
}
extern "C" void app_main(void)
{
esp_pm_config_esp32c3_t pm_config = {
.max_freq_mhz = 160,
.min_freq_mhz = 80,
.light_sleep_enable = true,
};
ESP_ERROR_CHECK(esp_pm_configure(&pm_config));
//initialize mutex
portMUX_TYPE my_mutex;
vPortCPUInitializeMutex(&my_mutex);
//initialize semaphores
init_finish_semaphore = xSemaphoreCreateBinary();
event_Queue = xQueueCreate(200, sizeof(uint8_t));
//set up all the pins
pin_Setup();
//init timer
timer_init();
//initialize queues and tasks
xTaskCreate(uart_thread, "uart_task", 30000, NULL, 6, NULL);
vTaskDelay(pdMS_TO_TICKS(1000));
xSemaphoreGive(init_finish_semaphore);
vTaskDelay(pdMS_TO_TICKS(1000));
uint8_t event = event_REPORT;
xQueueSend(event_Queue, &event, portMAX_DELAY);
while (1)
{
ESP_LOGI("DBG", "GPIO10:%d counter:%d" , gpio_get_level(GPIO_NUM_10),counter_isr);
vTaskDelay(pdMS_TO_TICKS(1000));
}
}