|
| 1 | +// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | + |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "esp32-hal-gpio.h" |
| 16 | +#include "esp32-hal-periman.h" |
| 17 | +#include "hal/gpio_hal.h" |
| 18 | +#include "soc/soc_caps.h" |
| 19 | + |
| 20 | +// It fixes lack of pin definition for S3 and for any future SoC |
| 21 | +// this function works for ESP32, ESP32-S2 and ESP32-S3 - including the C3, it will return -1 for any pin |
| 22 | +#if SOC_TOUCH_SENSOR_NUM > 0 |
| 23 | +#include "soc/touch_sensor_periph.h" |
| 24 | + |
| 25 | +int8_t digitalPinToTouchChannel(uint8_t pin) |
| 26 | +{ |
| 27 | + int8_t ret = -1; |
| 28 | + if (pin < SOC_GPIO_PIN_COUNT) { |
| 29 | + for (uint8_t i = 0; i < SOC_TOUCH_SENSOR_NUM; i++) { |
| 30 | + if (touch_sensor_channel_io_map[i] == pin) { |
| 31 | + ret = i; |
| 32 | + break; |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + return ret; |
| 37 | +} |
| 38 | +#else |
| 39 | +// No Touch Sensor available |
| 40 | +int8_t digitalPinToTouchChannel(uint8_t pin) |
| 41 | +{ |
| 42 | + return -1; |
| 43 | +} |
| 44 | +#endif |
| 45 | + |
| 46 | +#ifdef SOC_ADC_SUPPORTED |
| 47 | +#include "soc/adc_periph.h" |
| 48 | + |
| 49 | +int8_t digitalPinToAnalogChannel(uint8_t pin) |
| 50 | +{ |
| 51 | + uint8_t channel = 0; |
| 52 | + if (pin < SOC_GPIO_PIN_COUNT) { |
| 53 | + for (uint8_t i = 0; i < SOC_ADC_PERIPH_NUM; i++) { |
| 54 | + for (uint8_t j = 0; j < SOC_ADC_MAX_CHANNEL_NUM; j++) { |
| 55 | + if (adc_channel_io_map[i][j] == pin) { |
| 56 | + return channel; |
| 57 | + } |
| 58 | + channel++; |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + return -1; |
| 63 | +} |
| 64 | + |
| 65 | +int8_t analogChannelToDigitalPin(uint8_t channel) |
| 66 | +{ |
| 67 | + if (channel >= (SOC_ADC_PERIPH_NUM * SOC_ADC_MAX_CHANNEL_NUM)) { |
| 68 | + return -1; |
| 69 | + } |
| 70 | + uint8_t adc_unit = (channel / SOC_ADC_MAX_CHANNEL_NUM); |
| 71 | + uint8_t adc_chan = (channel % SOC_ADC_MAX_CHANNEL_NUM); |
| 72 | + return adc_channel_io_map[adc_unit][adc_chan]; |
| 73 | +} |
| 74 | +#else |
| 75 | +// No Analog channels availible |
| 76 | +int8_t analogChannelToDigitalPin(uint8_t channel) |
| 77 | +{ |
| 78 | + return -1; |
| 79 | +} |
| 80 | +#endif |
| 81 | + |
| 82 | +typedef void (*voidFuncPtr)(void); |
| 83 | +typedef void (*voidFuncPtrArg)(void*); |
| 84 | +typedef struct { |
| 85 | + voidFuncPtr fn; |
| 86 | + void* arg; |
| 87 | + bool functional; |
| 88 | +} InterruptHandle_t; |
| 89 | +static InterruptHandle_t __pinInterruptHandlers[SOC_GPIO_PIN_COUNT] = {0,}; |
| 90 | + |
| 91 | +#include "driver/rtc_io.h" |
| 92 | + |
| 93 | +static bool gpioDetachBus(void * bus){ |
| 94 | + return true; |
| 95 | +} |
| 96 | + |
| 97 | +extern void ARDUINO_ISR_ATTR __pinMode(uint8_t pin, uint8_t mode) |
| 98 | +{ |
| 99 | +#ifdef RGB_BUILTIN |
| 100 | + if (pin == RGB_BUILTIN){ |
| 101 | + __pinMode(RGB_BUILTIN-SOC_GPIO_PIN_COUNT, mode); |
| 102 | + return; |
| 103 | + } |
| 104 | +#endif |
| 105 | + |
| 106 | + if (pin >= SOC_GPIO_PIN_COUNT) { |
| 107 | + log_e("Invalid IO %i selected", pin); |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + if(perimanGetPinBus(pin, ESP32_BUS_TYPE_GPIO) == NULL){ |
| 112 | + perimanSetBusDeinit(ESP32_BUS_TYPE_GPIO, gpioDetachBus); |
| 113 | + if(!perimanClearPinBus(pin)){ |
| 114 | + log_e("Deinit of previous bus from IO %i failed", pin); |
| 115 | + return; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + gpio_hal_context_t gpiohal; |
| 120 | + gpiohal.dev = GPIO_LL_GET_HW(GPIO_PORT_0); |
| 121 | + |
| 122 | + gpio_config_t conf = { |
| 123 | + .pin_bit_mask = (1ULL<<pin), /*!< GPIO pin: set with bit mask, each bit maps to a GPIO */ |
| 124 | + .mode = GPIO_MODE_DISABLE, /*!< GPIO mode: set input/output mode */ |
| 125 | + .pull_up_en = GPIO_PULLUP_DISABLE, /*!< GPIO pull-up */ |
| 126 | + .pull_down_en = GPIO_PULLDOWN_DISABLE, /*!< GPIO pull-down */ |
| 127 | + .intr_type = gpiohal.dev->pin[pin].int_type /*!< GPIO interrupt type - previously set */ |
| 128 | + }; |
| 129 | + if (mode < 0x20) {//io |
| 130 | + conf.mode = mode & (INPUT | OUTPUT); |
| 131 | + if (mode & OPEN_DRAIN) { |
| 132 | + conf.mode |= GPIO_MODE_DEF_OD; |
| 133 | + } |
| 134 | + if (mode & PULLUP) { |
| 135 | + conf.pull_up_en = GPIO_PULLUP_ENABLE; |
| 136 | + } |
| 137 | + if (mode & PULLDOWN) { |
| 138 | + conf.pull_down_en = GPIO_PULLDOWN_ENABLE; |
| 139 | + } |
| 140 | + } |
| 141 | + if(gpio_config(&conf) != ESP_OK) |
| 142 | + { |
| 143 | + log_e("IO %i config failed", pin); |
| 144 | + return; |
| 145 | + } |
| 146 | + if(perimanGetPinBus(pin, ESP32_BUS_TYPE_GPIO) == NULL){ |
| 147 | + if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_GPIO, (void *)(pin+1), -1, -1)){ |
| 148 | + //gpioDetachBus((void *)(pin+1)); |
| 149 | + return; |
| 150 | + } |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +extern void ARDUINO_ISR_ATTR __digitalWrite(uint8_t pin, uint8_t val) |
| 155 | +{ |
| 156 | + #ifdef RGB_BUILTIN |
| 157 | + if(pin == RGB_BUILTIN){ |
| 158 | + //use RMT to set all channels on/off |
| 159 | + const uint8_t comm_val = val != 0 ? RGB_BRIGHTNESS : 0; |
| 160 | + neopixelWrite(RGB_BUILTIN, comm_val, comm_val, comm_val); |
| 161 | + return; |
| 162 | + } |
| 163 | + #endif |
| 164 | + if(perimanGetPinBus(pin, ESP32_BUS_TYPE_GPIO) != NULL){ |
| 165 | + gpio_set_level((gpio_num_t)pin, val); |
| 166 | + } else { |
| 167 | + log_e("IO %i is not set as GPIO.", pin); |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +extern int ARDUINO_ISR_ATTR __digitalRead(uint8_t pin) |
| 172 | +{ |
| 173 | + if(perimanGetPinBus(pin, ESP32_BUS_TYPE_GPIO) != NULL){ |
| 174 | + return gpio_get_level((gpio_num_t)pin); |
| 175 | + } |
| 176 | + else { |
| 177 | + log_e("IO %i is not set as GPIO.", pin); |
| 178 | + return 0; |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +static void ARDUINO_ISR_ATTR __onPinInterrupt(void * arg) { |
| 183 | + InterruptHandle_t * isr = (InterruptHandle_t*)arg; |
| 184 | + if(isr->fn) { |
| 185 | + if(isr->arg){ |
| 186 | + ((voidFuncPtrArg)isr->fn)(isr->arg); |
| 187 | + } else { |
| 188 | + isr->fn(); |
| 189 | + } |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | +extern void cleanupFunctional(void* arg); |
| 194 | + |
| 195 | +extern void __attachInterruptFunctionalArg(uint8_t pin, voidFuncPtrArg userFunc, void * arg, int intr_type, bool functional) |
| 196 | +{ |
| 197 | + static bool interrupt_initialized = false; |
| 198 | + |
| 199 | + // makes sure that pin -1 (255) will never work -- this follows Arduino standard |
| 200 | + if (pin >= SOC_GPIO_PIN_COUNT) return; |
| 201 | + |
| 202 | + if(!interrupt_initialized) { |
| 203 | + esp_err_t err = gpio_install_isr_service((int)ARDUINO_ISR_FLAG); |
| 204 | + interrupt_initialized = (err == ESP_OK) || (err == ESP_ERR_INVALID_STATE); |
| 205 | + } |
| 206 | + if(!interrupt_initialized) { |
| 207 | + log_e("IO %i ISR Service Failed To Start", pin); |
| 208 | + return; |
| 209 | + } |
| 210 | + |
| 211 | + // if new attach without detach remove old info |
| 212 | + if (__pinInterruptHandlers[pin].functional && __pinInterruptHandlers[pin].arg) |
| 213 | + { |
| 214 | + cleanupFunctional(__pinInterruptHandlers[pin].arg); |
| 215 | + } |
| 216 | + __pinInterruptHandlers[pin].fn = (voidFuncPtr)userFunc; |
| 217 | + __pinInterruptHandlers[pin].arg = arg; |
| 218 | + __pinInterruptHandlers[pin].functional = functional; |
| 219 | + |
| 220 | + gpio_set_intr_type((gpio_num_t)pin, (gpio_int_type_t)(intr_type & 0x7)); |
| 221 | + if(intr_type & 0x8){ |
| 222 | + gpio_wakeup_enable((gpio_num_t)pin, (gpio_int_type_t)(intr_type & 0x7)); |
| 223 | + } |
| 224 | + gpio_isr_handler_add((gpio_num_t)pin, __onPinInterrupt, &__pinInterruptHandlers[pin]); |
| 225 | + |
| 226 | + |
| 227 | + //FIX interrupts on peripherals outputs (eg. LEDC,...) |
| 228 | + //Enable input in GPIO register |
| 229 | + gpio_hal_context_t gpiohal; |
| 230 | + gpiohal.dev = GPIO_LL_GET_HW(GPIO_PORT_0); |
| 231 | + gpio_hal_input_enable(&gpiohal, pin); |
| 232 | +} |
| 233 | + |
| 234 | +extern void __attachInterruptArg(uint8_t pin, voidFuncPtrArg userFunc, void * arg, int intr_type) |
| 235 | +{ |
| 236 | + __attachInterruptFunctionalArg(pin, userFunc, arg, intr_type, false); |
| 237 | +} |
| 238 | + |
| 239 | +extern void __attachInterrupt(uint8_t pin, voidFuncPtr userFunc, int intr_type) { |
| 240 | + __attachInterruptFunctionalArg(pin, (voidFuncPtrArg)userFunc, NULL, intr_type, false); |
| 241 | +} |
| 242 | + |
| 243 | +extern void __detachInterrupt(uint8_t pin) |
| 244 | +{ |
| 245 | + gpio_isr_handler_remove((gpio_num_t)pin); //remove handle and disable isr for pin |
| 246 | + gpio_wakeup_disable((gpio_num_t)pin); |
| 247 | + |
| 248 | + if (__pinInterruptHandlers[pin].functional && __pinInterruptHandlers[pin].arg) |
| 249 | + { |
| 250 | + cleanupFunctional(__pinInterruptHandlers[pin].arg); |
| 251 | + } |
| 252 | + __pinInterruptHandlers[pin].fn = NULL; |
| 253 | + __pinInterruptHandlers[pin].arg = NULL; |
| 254 | + __pinInterruptHandlers[pin].functional = false; |
| 255 | + |
| 256 | + gpio_set_intr_type((gpio_num_t)pin, GPIO_INTR_DISABLE); |
| 257 | +} |
| 258 | + |
| 259 | +extern void enableInterrupt(uint8_t pin) { |
| 260 | + gpio_intr_enable((gpio_num_t)pin); |
| 261 | +} |
| 262 | + |
| 263 | +extern void disableInterrupt(uint8_t pin) { |
| 264 | + gpio_intr_disable((gpio_num_t)pin); |
| 265 | +} |
| 266 | + |
| 267 | + |
| 268 | +extern void pinMode(uint8_t pin, uint8_t mode) __attribute__ ((weak, alias("__pinMode"))); |
| 269 | +extern void digitalWrite(uint8_t pin, uint8_t val) __attribute__ ((weak, alias("__digitalWrite"))); |
| 270 | +extern int digitalRead(uint8_t pin) __attribute__ ((weak, alias("__digitalRead"))); |
| 271 | +extern void attachInterrupt(uint8_t pin, voidFuncPtr handler, int mode) __attribute__ ((weak, alias("__attachInterrupt"))); |
| 272 | +extern void attachInterruptArg(uint8_t pin, voidFuncPtrArg handler, void * arg, int mode) __attribute__ ((weak, alias("__attachInterruptArg"))); |
| 273 | +extern void detachInterrupt(uint8_t pin) __attribute__ ((weak, alias("__detachInterrupt"))); |
0 commit comments