#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/timer.h"
#include "esp_log.h"
#include <string.h>
#include <unistd.h>
#include "esp_timer.h"
#include "esp_sleep.h"
#include "sdkconfig.h"
#include "driver/gpio.h"
static const char* TAG = "example";
#define TIMER_RESOLUTION_HZ 10000000 // resolution
#define TIMER_ALARM_PERIOD_S 1 // Alarm period sec
#define ZeroCross GPIO_NUM_4 // zerocross
#define ZeroCrossPIN_Select (1ULL << ZeroCross)
#define Indicator_LED1 GPIO_NUM_8
static xQueueHandle gpio_evt_queue;
esp_timer_handle_t oneshot_timer;
uint32_t counter1 = 0;
// static void periodic_timer_callback(void* arg);
static void oneshot_timer_callback(void* arg);
bool LED_Toggle(gpio_num_t gpioNum)
{
//
bool state = false;
gpio_num_t pin = (gpio_num_t)(gpioNum & 0x1F);
state = GPIO_REG_READ(GPIO_ENABLE_REG);
if(GPIO_REG_READ(GPIO_ENABLE_REG) & BIT(pin))
{
//pin is output - read the GPIO_OUT_REG register
state ^= (GPIO_REG_READ(GPIO_OUT_REG) >> pin) & 1U;
gpio_set_level(gpioNum, state);
}
else
{
//pin is input - read the GPIO_IN_REG register
state = (GPIO_REG_READ(GPIO_IN_REG) >> pin) & 1U;
}
return state;
}
//********************************************************
static void IRAM_ATTR GPIO_ISR_Handler(void* arg)
{
uint32_t gpio_num = (uint32_t) arg;
xQueueSendFromISR(gpio_evt_queue, &gpio_num, NULL);
}
//**********************************************************************
static void Interrupt_Task_Function(void* arg)
{
uint32_t ioNum;
printf("GPIO Interrupt Task Start... \r\n");
while(1)
{
if(xQueueReceive(gpio_evt_queue, &ioNum, portMAX_DELAY))
{
if(ioNum == ZeroCross)
{
printf("GPIO ZeroCross detected ... \r\n");
// reset timer value ????????????????????????
counter1 = counter1 + 100;
}
}
}
}
void app_main(void)
{
//
const esp_timer_create_args_t oneshot_timer_args = {
.callback = &oneshot_timer_callback,
/* argument specified here will be passed to timer callback function */
// .arg = (void*) periodic_timer,
.name = "one-shot"
};
ESP_ERROR_CHECK(esp_timer_create(&oneshot_timer_args, &oneshot_timer));
/* Start the timers */
ESP_ERROR_CHECK(esp_timer_start_once(oneshot_timer, 1000000));
ESP_LOGI(TAG, "Started timers, time since boot: %lld us", esp_timer_get_time());
ESP_LOGI(TAG, "Entering light sleep for 0.5s, time since boot: %lld us",
esp_timer_get_time());
gpio_pad_select_gpio(Indicator_LED1);
gpio_set_direction(Indicator_LED1, GPIO_MODE_OUTPUT);
gpio_set_level(Indicator_LED1, 0);
gpio_evt_queue = xQueueCreate(10, sizeof(uint32_t));
if(xTaskCreate(Interrupt_Task_Function, "Interrupt_Task_Function", 2048, NULL, 3, NULL) == pdPASS)
{
printf("Interrupt_Task_Function Created Success! \r\n");
}
// zero cross
gpio_set_direction(ZeroCross, GPIO_MODE_INPUT);
gpio_set_intr_type(ZeroCross, GPIO_INTR_POSEDGE);
gpio_set_pull_mode(ZeroCross, GPIO_PULLDOWN_ONLY);
gpio_install_isr_service(0);
gpio_isr_handler_add(ZeroCross, GPIO_ISR_Handler, (void*) ZeroCross);
ESP_LOGI(TAG, "while run");
while(1)
{
//
printf("timer value : %d \r\n", counter1);
vTaskDelay(pdMS_TO_TICKS(500));
// LED_Toggle(Indicator_LED1);
}
/* Clean up and finish the example */
ESP_ERROR_CHECK(esp_timer_stop(oneshot_timer));
// ESP_ERROR_CHECK(esp_timer_delete(periodic_timer));
ESP_ERROR_CHECK(esp_timer_delete(oneshot_timer));
ESP_LOGI(TAG, "Stopped and deleted timers");
}
// static void periodic_timer_callback(void* arg)
// {
// int64_t time_since_boot = esp_timer_get_time();
// ESP_LOGI(TAG, "Periodic timer called, time since boot: %lld us", time_since_boot);
// }
static void oneshot_timer_callback(void* arg)
{
int64_t time_since_boot = esp_timer_get_time();
ESP_LOGI(TAG, "One-shot timer called, time since boot: %lld us", time_since_boot);
}