|
| 1 | +// ----------------------------------------------------------------------------- |
| 2 | +// Copyright (C) 2024 David Hansel |
| 3 | +// |
| 4 | +// This program is free software; you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation; either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | +// |
| 14 | +// You should have receikved a copy of the GNU General Public License |
| 15 | +// along with this program; if not, write to the Free Software Foundation, |
| 16 | +// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | +// ----------------------------------------------------------------------------- |
| 18 | + |
| 19 | +#ifndef IECESPIDF_H |
| 20 | +#define IECESPIDF_H |
| 21 | + |
| 22 | +#include <stdint.h> |
| 23 | +#include <memory.h> |
| 24 | +#include <esp_timer.h> |
| 25 | +#include <driver/gpio.h> |
| 26 | +#include <soc/gpio_reg.h> |
| 27 | +#include "hal/gpio_hal.h" |
| 28 | +#include <freertos/FreeRTOS.h> |
| 29 | + |
| 30 | +#define ARDUINO 2024 |
| 31 | +#define ESP32 |
| 32 | + |
| 33 | +#define PROGMEM |
| 34 | + |
| 35 | +#pragma GCC diagnostic ignored "-Wunused-function" |
| 36 | +#pragma GCC diagnostic ignored "-Wunused-but-set-variable" |
| 37 | +#pragma GCC diagnostic ignored "-Wvolatile" |
| 38 | + |
| 39 | +typedef void (*interruptFcn)(void *); |
| 40 | + |
| 41 | +#define INPUT 0x0 |
| 42 | +#define OUTPUT 0x1 |
| 43 | +#define INPUT_PULLUP 0x2 |
| 44 | +#define LOW 0x0 |
| 45 | +#define HIGH 0x1 |
| 46 | +#define FALLING GPIO_INTR_NEGEDGE |
| 47 | +#define RISING GPIO_INTR_POSEDGE |
| 48 | +#define bit(n) (1<<(n)) |
| 49 | +#define digitalWrite(pin, v) gpio_set_level((gpio_num_t) pin, v); |
| 50 | +#define pinMode(pin, mode) { gpio_reset_pin((gpio_num_t) pin); gpio_set_direction((gpio_num_t)pin, mode==OUTPUT ? GPIO_MODE_OUTPUT : GPIO_MODE_INPUT); if( mode==INPUT_PULLUP ) gpio_pullup_en((gpio_num_t) pin); } |
| 51 | +#define digitalPinToGPIONumber(digitalPin) (digitalPin) |
| 52 | +#define digitalPinToBitMask(pin) (1UL << (digitalPinToGPIONumber(pin)&31)) |
| 53 | +#define portInputRegister(port) ((volatile uint32_t*)((port)?GPIO_IN1_REG:GPIO_IN_REG)) |
| 54 | +#define portOutputRegister(port) ((volatile uint32_t*)((port)?GPIO_OUT1_REG:GPIO_OUT_REG)) |
| 55 | +#define portModeRegister(port) ((volatile uint32_t*)((port)?GPIO_ENABLE1_REG:GPIO_ENABLE_REG)) |
| 56 | +#define digitalPinToPort(pin) ((digitalPinToGPIONumber(pin)>31)?1:0) |
| 57 | +#define digitalPinToInterrupt(p) ((((uint8_t)digitalPinToGPIONumber(p))<SOC_GPIO_PIN_COUNT)?digitalPinToGPIONumber(p):NOT_AN_INTERRUPT) |
| 58 | + |
| 59 | +#define noInterrupts() portDISABLE_INTERRUPTS() |
| 60 | +#define interrupts() portENABLE_INTERRUPTS() |
| 61 | +#define micros() ((uint32_t) esp_timer_get_time()) |
| 62 | +#define PSTR(x) x |
| 63 | +#define strncmp_P strncmp |
| 64 | +#define strcmp_P strcmp |
| 65 | +#define min(x, y) ((x)<(y) ? (x) : (y)) |
| 66 | +#define max(x, y) ((x)>(y) ? (x) : (y)) |
| 67 | + |
| 68 | +static void delayMicroseconds(uint32_t n) |
| 69 | +{ |
| 70 | + uint32_t s = micros(); |
| 71 | + while((micros()-s)<n); |
| 72 | +} |
| 73 | + |
| 74 | +static void attachInterrupt(uint8_t pin, interruptFcn userFunc, gpio_int_type_t intr_type) |
| 75 | +{ |
| 76 | + static bool interrupt_initialized = false; |
| 77 | + |
| 78 | + if (pin >= SOC_GPIO_PIN_COUNT) return; |
| 79 | + |
| 80 | + if (!interrupt_initialized) { |
| 81 | + esp_err_t err = gpio_install_isr_service(0 /* ESP_INTR_FLAG_IRAM */); |
| 82 | + interrupt_initialized = (err == ESP_OK) || (err == ESP_ERR_INVALID_STATE); |
| 83 | + } |
| 84 | + |
| 85 | + gpio_set_intr_type((gpio_num_t)pin, intr_type); |
| 86 | + gpio_isr_handler_add((gpio_num_t)pin, userFunc, NULL); |
| 87 | + |
| 88 | + gpio_hal_context_t gpiohal; |
| 89 | + gpiohal.dev = GPIO_LL_GET_HW(GPIO_PORT_0); |
| 90 | + gpio_hal_input_enable(&gpiohal, pin); |
| 91 | +} |
| 92 | + |
| 93 | +static void detachInterrupt(uint8_t pin) |
| 94 | +{ |
| 95 | + gpio_isr_handler_remove((gpio_num_t)pin); |
| 96 | + gpio_set_intr_type((gpio_num_t)pin, GPIO_INTR_DISABLE); |
| 97 | +} |
| 98 | + |
| 99 | +#endif |
0 commit comments