Page 5 of 16
Page 113
#include <stdio.h>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
void ISR(void *arg)
{
gpio_intr_disable(4);
gpio_set_level(2, !gpio_get_level(2));
gpio_intr_enable(4);
}
void app_main(void)
{
gpio_isr_register(ISR, NULL, ESP_INTR_FLAG_LOWMED, NULL);
gpio_reset_pin(2);
gpio_set_direction(2, GPIO_MODE_INPUT_OUTPUT);
gpio_reset_pin(4);
gpio_set_direction(4, GPIO_MODE_INPUT);
gpio_intr_disable(4);
gpio_set_intr_type(4, GPIO_INTR_ANYEDGE);
gpio_intr_enable(4);
}
Page 114
#include <stdio.h>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
void ISR(void *arg)
{
gpio_intr_disable(16);
gpio_intr_disable(4);
gpio_set_level(2, !gpio_get_level(2));
gpio_intr_enable(16);
gpio_intr_enable(4);
}
void app_main(void)
{
gpio_isr_register(ISR, NULL, ESP_INTR_FLAG_LOWMED | ESP_INTR_FLAG_EDGE, NULL);
gpio_reset_pin(2);
gpio_set_direction(2, GPIO_MODE_INPUT_OUTPUT);
gpio_reset_pin(4);
gpio_set_direction(4, GPIO_MODE_INPUT);
gpio_intr_disable(4);
gpio_set_intr_type(4, GPIO_INTR_ANYEDGE);
gpio_reset_pin(16);
gpio_set_direction(16, GPIO_MODE_INPUT);
gpio_intr_disable(16);
gpio_set_intr_type(16, GPIO_INTR_ANYEDGE);
gpio_intr_enable(4);
gpio_intr_enable(16);
}
Page 115
#include <stdio.h>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "soc/gpio_reg.h"
int32_t getIRQStatus()
{
return *(int *)GPIO_STATUS_REG;
}
void ISR(void *arg)
{
int32_t intStatus = getIRQStatus();
gpio_intr_disable(16);
gpio_intr_disable(4);
int32_t mask = 1 << 16;
if (intStatus & mask)
gpio_set_level(2, !gpio_get_level(2));
gpio_intr_enable(16);
gpio_intr_enable(4);
}
void app_main(void)
{
gpio_isr_register(ISR, NULL, ESP_INTR_FLAG_LOWMED | ESP_INTR_FLAG_EDGE, NULL);
gpio_reset_pin(2);
gpio_set_direction(2, GPIO_MODE_INPUT_OUTPUT);
gpio_reset_pin(4);
gpio_set_direction(4, GPIO_MODE_INPUT);
gpio_intr_disable(4);
gpio_set_intr_type(4, GPIO_INTR_ANYEDGE);
gpio_reset_pin(16);
gpio_set_direction(16, GPIO_MODE_INPUT);
gpio_intr_disable(16);
gpio_set_intr_type(16, GPIO_INTR_ANYEDGE);
gpio_intr_enable(4);
gpio_intr_enable(16);
}
Page 118
#include <stdio.h>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include <sys/time.h>
int64_t tick_us(int64_t offset)
{
static struct timeval tv_now;
gettimeofday(&tv_now, NULL);
return (int64_t)tv_now.tv_sec * 1000000L +
(int64_t)tv_now.tv_usec + offset;
}
void delay_ms(int t) {
vTaskDelay(t / portTICK_PERIOD_MS);
}
int64_t t = 0;
int state = 0;
void ISR(void *arg)
{
gpio_intr_disable(4);
t = tick_us(-t);
state++;
gpio_set_level(2, !gpio_get_level(2));
gpio_intr_enable(4);
}
void app_main(void)
{
gpio_isr_register(ISR, NULL, ESP_INTR_FLAG_LOWMED, NULL);
gpio_reset_pin(2);
gpio_set_direction(2, GPIO_MODE_INPUT_OUTPUT);
gpio_reset_pin(4);
gpio_set_direction(4, GPIO_MODE_INPUT);
gpio_intr_disable(4);
gpio_set_intr_type(4, GPIO_INTR_ANYEDGE);
gpio_intr_enable(4);
while (true)
{
while (state != 2)
{
};
gpio_intr_disable(4);
printf("time = %lld, %d\n", t, state);
fflush(stdout);
state = 0;
t = 0;
delay_ms(1000);
gpio_intr_enable(4);
}
}
Page 120
#include <stdio.h>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
char data[3];
void ISR(void *arg)
{
gpio_intr_disable(4);
for (int i = 0; i < 3; i++)
data[i] = 0;
gpio_intr_enable(4);
}
void app_main(void)
{
gpio_isr_register(ISR, NULL, ESP_INTR_FLAG_LOWMED, NULL);
gpio_reset_pin(4);
gpio_set_direction(4, GPIO_MODE_INPUT);
gpio_intr_disable(4);
gpio_set_intr_type(4, GPIO_INTR_ANYEDGE);
gpio_intr_enable(4);
while (true)
{
for (int i = 0; i < 3; i++)
data[i] = 0xFF;
if (data[0] != data[1] || data[1] != data[2] || data[2] != data[0])
break;
}
printf("%d %d %d\n", data[0], data[1], data[2]);
}
Page 122
#include <stdio.h>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include <sys/time.h>
int64_t tick_us(int64_t offset)
{
static struct timeval tv_now;
gettimeofday(&tv_now, NULL);
return (int64_t)tv_now.tv_sec * 1000000L +
(int64_t)tv_now.tv_usec + offset;
}
int64_t t = 0;
static QueueHandle_t gpio_evt_queue = NULL;
void ISR(void *arg)
{
gpio_intr_disable(4);
t = tick_us(0);
xQueueSendToBackFromISR(gpio_evt_queue, &t, NULL);
gpio_intr_enable(4);
}
void app_main(void)
{
gpio_evt_queue = xQueueCreate(10, sizeof(int32_t));
gpio_isr_register(ISR, NULL, ESP_INTR_FLAG_LOWMED, NULL);
gpio_reset_pin(4);
gpio_set_direction(4, GPIO_MODE_INPUT);
gpio_intr_disable(4);
gpio_set_intr_type(4, GPIO_INTR_ANYEDGE);
gpio_intr_enable(4);
int64_t time;
while (true)
{
if (xQueueReceive(gpio_evt_queue, &time, portMAX_DELAY))
{
printf("time = %lld\n", time);
fflush(stdout);
}
}
}
Page 125
#include <stdio.h>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "driver/gptimer.h"
void app_main(void)
{
gptimer_config_t timer_config = {
.clk_src = GPTIMER_CLK_SRC_DEFAULT,
.direction = GPTIMER_COUNT_UP,
.resolution_hz = 1221,
};
gptimer_handle_t gptimer = NULL;
gptimer_new_timer(&timer_config, &gptimer);
gptimer_enable(gptimer);
gptimer_start(gptimer);
uint64_t count = 0;
do
{
gptimer_get_raw_count(gptimer, &count);
} while (count < 1000);
printf("Time up %lld\n", count);
gptimer_stop(gptimer);
gptimer_disable(gptimer);
gptimer_del_timer(gptimer);
}
Page 126
#include <stdio.h>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "driver/gptimer.h"
gptimer_handle_t tick_us_start(void)
{
gptimer_config_t timer_config = {
.clk_src = GPTIMER_CLK_SRC_DEFAULT,
.direction = GPTIMER_COUNT_UP,
.resolution_hz = 1000000,
};
gptimer_handle_t gptimer_us = NULL;
gptimer_new_timer(&timer_config, &gptimer_us);
gptimer_enable(gptimer_us);
gptimer_start(gptimer_us);
return gptimer_us;
}
int64_t tick_us(gptimer_handle_t gptimer_us, int64_t offset)
{
uint64_t count;
gptimer_get_raw_count(gptimer_us, &count);
return count + offset;
}
void app_main(void)
{
gptimer_handle_t gptimer_us = tick_us_start();
int64_t tick = tick_us(gptimer_us, 1000000);
while (tick_us(gptimer_us, 0) < tick)
{
};
printf("time up %lld\n", tick_us(gptimer_us, 0));
}
Page 127
#include <stdio.h>
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "driver/gptimer.h"
bool ISR(gptimer_handle_t gptimer,
const gptimer_alarm_event_data_t *edata,
void *user_data)
{
gpio_set_level(2, !gpio_get_level(2));
return true;
}
void app_main(void)
{
gpio_set_direction(2, GPIO_MODE_INPUT_OUTPUT);
gpio_reset_pin(2);
gpio_set_direction(2, GPIO_MODE_INPUT_OUTPUT);
gptimer_handle_t gptimer = NULL;
gptimer_config_t timer_config = {
.clk_src = GPTIMER_CLK_SRC_DEFAULT,
.direction = GPTIMER_COUNT_UP,
.resolution_hz = 1221,
};
gptimer_new_timer(&timer_config, &gptimer);
gptimer_alarm_config_t alarm_config = {
.flags.auto_reload_on_alarm = true,
.alarm_count = 1000,
.reload_count = 0};
gptimer_set_alarm_action(gptimer, &alarm_config);
gptimer_event_callbacks_t cbs = {
.on_alarm = ISR,
};
gptimer_register_event_callbacks(gptimer, &cbs, NULL);
gptimer_enable(gptimer);
gptimer_start(gptimer);
while (true)
{
};
}