|
| 1 | +/* |
| 2 | + * GPIO.cpp |
| 3 | + * |
| 4 | + * Created on: Feb 28, 2017 |
| 5 | + * Author: kolban |
| 6 | + */ |
| 7 | + |
| 8 | +#include "GPIO.h" |
| 9 | +#include <driver/gpio.h> |
| 10 | +#include "sdkconfig.h" |
| 11 | +#include <esp_log.h> |
| 12 | +#include <esp_err.h> |
| 13 | +#include "GeneralUtils.h" |
| 14 | + |
| 15 | +static const char* LOG_TAG = "GPIO"; |
| 16 | + |
| 17 | +static bool g_isrServiceInstalled = false; |
| 18 | + |
| 19 | +/** |
| 20 | + * @brief Add an ISR handler to the pin. |
| 21 | + * @param [in] pin The pin to have the ISR associated with it. |
| 22 | + * @param [in] handler The function to be invoked when the interrupt is detected. |
| 23 | + * @param [in] pArgs Optional arguments to pass to the handler. |
| 24 | + */ |
| 25 | +void ESP32CPP::GPIO::addISRHandler(gpio_num_t pin, gpio_isr_t handler, void* pArgs) { |
| 26 | + ESP_LOGD(LOG_TAG, ">> addISRHandler: pin=%d", pin); |
| 27 | + |
| 28 | + // If we have not yet installed the ISR service handler, install it now. |
| 29 | + if (!g_isrServiceInstalled) { |
| 30 | + ESP_LOGD(LOG_TAG, "Installing the global ISR service"); |
| 31 | + esp_err_t errRc = ::gpio_install_isr_service(0); |
| 32 | + if (errRc != ESP_OK) { |
| 33 | + ESP_LOGE(LOG_TAG, "<< gpio_install_isr_service: rc=%d %s", errRc, GeneralUtils::errorToString(errRc)); |
| 34 | + return; |
| 35 | + } |
| 36 | + g_isrServiceInstalled = true; |
| 37 | + } |
| 38 | + |
| 39 | + esp_err_t errRc = ::gpio_isr_handler_add(pin, handler, pArgs); |
| 40 | + if (errRc != ESP_OK) { |
| 41 | + ESP_LOGE(LOG_TAG, "<< gpio_isr_handler_add: rc=%d %s", errRc, GeneralUtils::errorToString(errRc)); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + ESP_LOGD(LOG_TAG, "<< addISRHandler"); |
| 46 | +} // addISRHandler |
| 47 | + |
| 48 | + |
| 49 | +/** |
| 50 | + * @brief Set the pin high. |
| 51 | + * |
| 52 | + * Ensure that the pin is set to be output prior to calling this method. |
| 53 | + * |
| 54 | + * @param [in] pin The pin to be set high. |
| 55 | + * @return N/A. |
| 56 | + */ |
| 57 | +void ESP32CPP::GPIO::high(gpio_num_t pin) { |
| 58 | + write(pin, true); |
| 59 | +} // high |
| 60 | + |
| 61 | + |
| 62 | +/** |
| 63 | + * @brief Determine if the pin is a valid pin for an ESP32 (i.e. is it in range). |
| 64 | + * |
| 65 | + * @param [in] pin The pin number to validate. |
| 66 | + * @return The value of true if the pin is valid and false otherwise. |
| 67 | + */ |
| 68 | +bool ESP32CPP::GPIO::inRange(gpio_num_t pin) { |
| 69 | + return (pin >= 0 && pin <= 39); |
| 70 | +} // inRange |
| 71 | + |
| 72 | + |
| 73 | +/** |
| 74 | + * @brief Disable interrupts on the named pin. |
| 75 | + * @param [in] pin The pin to disable interrupts upon. |
| 76 | + * @return N/A. |
| 77 | + */ |
| 78 | +void ESP32CPP::GPIO::interruptDisable(gpio_num_t pin) { |
| 79 | + esp_err_t rc = ::gpio_intr_disable(pin); |
| 80 | + if (rc != ESP_OK) { |
| 81 | + ESP_LOGE(LOG_TAG, "interruptDisable: %d", rc); |
| 82 | + } |
| 83 | +} // interruptDisable |
| 84 | + |
| 85 | + |
| 86 | +/** |
| 87 | + * @brief Enable interrupts on the named pin. |
| 88 | + * @param [in] pin The pin to enable interrupts upon. |
| 89 | + * @return N/A. |
| 90 | + */ |
| 91 | +void ESP32CPP::GPIO::interruptEnable(gpio_num_t pin) { |
| 92 | + esp_err_t rc = ::gpio_intr_enable(pin); |
| 93 | + if (rc != ESP_OK) { |
| 94 | + ESP_LOGE(LOG_TAG, "interruptEnable: %d", rc); |
| 95 | + } |
| 96 | +} // interruptEnable |
| 97 | + |
| 98 | + |
| 99 | +/** |
| 100 | + * @brief Set the pin low. |
| 101 | + * |
| 102 | + * Ensure that the pin is set to be output prior to calling this method. |
| 103 | + * |
| 104 | + * @param [in] pin The pin to be set low. |
| 105 | + * @return N/A. |
| 106 | + */ |
| 107 | +void ESP32CPP::GPIO::low(gpio_num_t pin) { |
| 108 | + write(pin, false); |
| 109 | +} // low |
| 110 | + |
| 111 | + |
| 112 | +/** |
| 113 | + * @brief Read a value from the given pin. |
| 114 | + * |
| 115 | + * Ensure the pin is set as input before calling this method. |
| 116 | + * @param [in] pin The pin to read from. |
| 117 | + * @return True if the pin is high, false if the pin is low. |
| 118 | + */ |
| 119 | +bool ESP32CPP::GPIO::read(gpio_num_t pin) { |
| 120 | + return ::gpio_get_level(pin) == 1; |
| 121 | +} // read |
| 122 | + |
| 123 | + |
| 124 | +/** |
| 125 | + * @brief Set the pin as input. |
| 126 | + * |
| 127 | + * Set the direction of the pin as input. |
| 128 | + * @param [in] pin The pin to set as input. |
| 129 | + * @return N/A. |
| 130 | + */ |
| 131 | +void ESP32CPP::GPIO::setInput(gpio_num_t pin) { |
| 132 | + ::gpio_set_direction(pin, GPIO_MODE_INPUT); |
| 133 | +} // setInput |
| 134 | + |
| 135 | + |
| 136 | +/** |
| 137 | + * @brief Set the interrupt type. |
| 138 | + * The type of interrupt can be one of: |
| 139 | + * |
| 140 | + * * GPIO_INTR_ANYEDGE |
| 141 | + * * GPIO_INTR_DISABLE |
| 142 | + * * GPIO_INTR_NEGEDGE |
| 143 | + * * GPIO_INTR_POSEDGE |
| 144 | + * * GPIO_INTR_LOW_LEVEL |
| 145 | + * * GPIO_INTR_HIGH_LEVEL |
| 146 | + * |
| 147 | + * @param [in] pin The pin to set the interrupt upon. |
| 148 | + * @param [in] intrType The type of interrupt. |
| 149 | + * @return N/A. |
| 150 | + */ |
| 151 | +void ESP32CPP::GPIO::setInterruptType(gpio_num_t pin, gpio_int_type_t intrType) { |
| 152 | + esp_err_t rc = ::gpio_set_intr_type(pin, intrType); |
| 153 | + if (rc != ESP_OK) { |
| 154 | + ESP_LOGE(LOG_TAG, "setInterruptType: %d", rc); |
| 155 | + } |
| 156 | +} // setInterruptType |
| 157 | + |
| 158 | + |
| 159 | +/** |
| 160 | + * @brief Set the pin as output. |
| 161 | + * |
| 162 | + * Set the direction of the pin as output. Note that pins 34 through 39 are input only and can **not** be |
| 163 | + * set as output. |
| 164 | + * @param [in] pin The pin to set as output. |
| 165 | + * @return N/A. |
| 166 | + */ |
| 167 | +void ESP32CPP::GPIO::setOutput(gpio_num_t pin) { |
| 168 | + ::gpio_set_direction(pin, GPIO_MODE_OUTPUT); |
| 169 | +} // setOutput |
| 170 | + |
| 171 | + |
| 172 | +/** |
| 173 | + * @brief Write a value to the given pin. |
| 174 | + * |
| 175 | + * Ensure that the pin is set as output before calling this method. |
| 176 | + * @param [in] pin The gpio pin to change. |
| 177 | + * @param [out] value The value to be written to the pin. |
| 178 | + * @return N/A. |
| 179 | + */ |
| 180 | +void ESP32CPP::GPIO::write(gpio_num_t pin, bool value) { |
| 181 | + //ESP_LOGD(LOG_TAG, ">> write: pin: %d, value: %d", pin, value); |
| 182 | + esp_err_t errRc = ::gpio_set_level(pin, value ? 1 : 0); |
| 183 | + if (errRc != ESP_OK) { |
| 184 | + ESP_LOGE(LOG_TAG, "<< gpio_set_level: pin=%d, rc=%d %s", pin, errRc, GeneralUtils::errorToString(errRc)); |
| 185 | + } |
| 186 | +} // write |
| 187 | + |
| 188 | + |
| 189 | +/** |
| 190 | + * @brief Write up to 8 bits of data to a set of pins. |
| 191 | + * @param [in] pins An array of pins to set their values. |
| 192 | + * @param [in] value The data value to write. |
| 193 | + * @param [in] bits The number of bits to write. |
| 194 | + */ |
| 195 | +void ESP32CPP::GPIO::writeByte(gpio_num_t pins[], uint8_t value, int bits) { |
| 196 | + ESP_LOGD(LOG_TAG, ">> writeByte: value: %.2x, bits: %d", value, bits); |
| 197 | + for (int i = 0; i < bits; i++) { |
| 198 | + //ESP_LOGD(LOG_TAG, "i=%d, bits=%d", i, bits); |
| 199 | + write(pins[i], (value & (1 << i)) != 0); |
| 200 | + } |
| 201 | + ESP_LOGD(LOG_TAG, "<< writeByte"); |
| 202 | +} // writeByte |
0 commit comments