1 // Copyright 2010-2020 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 #pragma once 16 17 #include <stdint.h> 18 #include <stdbool.h> 19 #include "soc/gpio_reg.h" 20 21 #ifdef __cplusplus 22 extern "C" { 23 #endif 24 25 /** \defgroup gpio_apis, uart configuration and communication related apis 26 * @brief gpio apis 27 */ 28 29 /** @addtogroup gpio_apis 30 * @{ 31 */ 32 33 #define GPIO_REG_READ(reg) READ_PERI_REG(reg) 34 #define GPIO_REG_WRITE(reg, val) WRITE_PERI_REG(reg, val) 35 #define GPIO_ID_PIN0 0 36 #define GPIO_ID_PIN(n) (GPIO_ID_PIN0+(n)) 37 #define GPIO_PIN_ADDR(i) (GPIO_PIN0_REG + i*4) 38 39 #define GPIO_FUNC_IN_HIGH 0x38 40 #define GPIO_FUNC_IN_LOW 0x3C 41 42 #define GPIO_ID_IS_PIN_REGISTER(reg_id) \ 43 ((reg_id >= GPIO_ID_PIN0) && (reg_id <= GPIO_ID_PIN(GPIO_PIN_COUNT-1))) 44 45 #define GPIO_REGID_TO_PINIDX(reg_id) ((reg_id) - GPIO_ID_PIN0) 46 47 typedef enum { 48 GPIO_PIN_INTR_DISABLE = 0, 49 GPIO_PIN_INTR_POSEDGE = 1, 50 GPIO_PIN_INTR_NEGEDGE = 2, 51 GPIO_PIN_INTR_ANYEDGE = 3, 52 GPIO_PIN_INTR_LOLEVEL = 4, 53 GPIO_PIN_INTR_HILEVEL = 5 54 } GPIO_INT_TYPE; 55 56 #define GPIO_OUTPUT_SET(gpio_no, bit_value) \ 57 ((gpio_no < 32) ? gpio_output_set(bit_value<<gpio_no, (bit_value ? 0 : 1)<<gpio_no, 1<<gpio_no,0) : \ 58 gpio_output_set_high(bit_value<<(gpio_no - 32), (bit_value ? 0 : 1)<<(gpio_no - 32), 1<<(gpio_no -32),0)) 59 #define GPIO_DIS_OUTPUT(gpio_no) ((gpio_no < 32) ? gpio_output_set(0,0,0, 1<<gpio_no) : gpio_output_set_high(0,0,0, 1<<(gpio_no - 32))) 60 #define GPIO_INPUT_GET(gpio_no) ((gpio_no < 32) ? ((gpio_input_get()>>gpio_no)&BIT0) : ((gpio_input_get_high()>>(gpio_no - 32))&BIT0)) 61 62 /* GPIO interrupt handler, registered through gpio_intr_handler_register */ 63 typedef void (* gpio_intr_handler_fn_t)(uint32_t intr_mask, bool high, void *arg); 64 65 /** 66 * @brief Initialize GPIO. This includes reading the GPIO Configuration DataSet 67 * to initialize "output enables" and pin configurations for each gpio pin. 68 * Please do not call this function in SDK. 69 * 70 * @param None 71 * 72 * @return None 73 */ 74 void gpio_init(void); 75 76 /** 77 * @brief Change GPIO(0-31) pin output by setting, clearing, or disabling pins, GPIO0<->BIT(0). 78 * There is no particular ordering guaranteed; so if the order of writes is significant, 79 * calling code should divide a single call into multiple calls. 80 * 81 * @param uint32_t set_mask : the gpios that need high level. 82 * 83 * @param uint32_t clear_mask : the gpios that need low level. 84 * 85 * @param uint32_t enable_mask : the gpios that need be changed. 86 * 87 * @param uint32_t disable_mask : the gpios that need diable output. 88 * 89 * @return None 90 */ 91 void gpio_output_set(uint32_t set_mask, uint32_t clear_mask, uint32_t enable_mask, uint32_t disable_mask); 92 93 /** 94 * @brief Change GPIO(32-39) pin output by setting, clearing, or disabling pins, GPIO32<->BIT(0). 95 * There is no particular ordering guaranteed; so if the order of writes is significant, 96 * calling code should divide a single call into multiple calls. 97 * 98 * @param uint32_t set_mask : the gpios that need high level. 99 * 100 * @param uint32_t clear_mask : the gpios that need low level. 101 * 102 * @param uint32_t enable_mask : the gpios that need be changed. 103 * 104 * @param uint32_t disable_mask : the gpios that need diable output. 105 * 106 * @return None 107 */ 108 void gpio_output_set_high(uint32_t set_mask, uint32_t clear_mask, uint32_t enable_mask, uint32_t disable_mask); 109 110 /** 111 * @brief Sample the value of GPIO input pins(0-31) and returns a bitmask. 112 * 113 * @param None 114 * 115 * @return uint32_t : bitmask for GPIO input pins, BIT(0) for GPIO0. 116 */ 117 uint32_t gpio_input_get(void); 118 119 /** 120 * @brief Sample the value of GPIO input pins(32-39) and returns a bitmask. 121 * 122 * @param None 123 * 124 * @return uint32_t : bitmask for GPIO input pins, BIT(0) for GPIO32. 125 */ 126 uint32_t gpio_input_get_high(void); 127 128 /** 129 * @brief Register an application-specific interrupt handler for GPIO pin interrupts. 130 * Once the interrupt handler is called, it will not be called again until after a call to gpio_intr_ack. 131 * Please do not call this function in SDK. 132 * 133 * @param gpio_intr_handler_fn_t fn : gpio application-specific interrupt handler 134 * 135 * @param void *arg : gpio application-specific interrupt handler argument. 136 * 137 * @return None 138 */ 139 void gpio_intr_handler_register(gpio_intr_handler_fn_t fn, void *arg); 140 141 /** 142 * @brief Get gpio interrupts which happens but not processed. 143 * Please do not call this function in SDK. 144 * 145 * @param None 146 * 147 * @return uint32_t : bitmask for GPIO pending interrupts, BIT(0) for GPIO0. 148 */ 149 uint32_t gpio_intr_pending(void); 150 151 /** 152 * @brief Get gpio interrupts which happens but not processed. 153 * Please do not call this function in SDK. 154 * 155 * @param None 156 * 157 * @return uint32_t : bitmask for GPIO pending interrupts, BIT(0) for GPIO32. 158 */ 159 uint32_t gpio_intr_pending_high(void); 160 161 /** 162 * @brief Ack gpio interrupts to process pending interrupts. 163 * Please do not call this function in SDK. 164 * 165 * @param uint32_t ack_mask: bitmask for GPIO ack interrupts, BIT(0) for GPIO0. 166 * 167 * @return None 168 */ 169 void gpio_intr_ack(uint32_t ack_mask); 170 171 /** 172 * @brief Ack gpio interrupts to process pending interrupts. 173 * Please do not call this function in SDK. 174 * 175 * @param uint32_t ack_mask: bitmask for GPIO ack interrupts, BIT(0) for GPIO32. 176 * 177 * @return None 178 */ 179 void gpio_intr_ack_high(uint32_t ack_mask); 180 181 /** 182 * @brief Set GPIO to wakeup the ESP32. 183 * Please do not call this function in SDK. 184 * 185 * @param uint32_t i: gpio number. 186 * 187 * @param GPIO_INT_TYPE intr_state : only GPIO_PIN_INTR_LOLEVEL\GPIO_PIN_INTR_HILEVEL can be used 188 * 189 * @return None 190 */ 191 void gpio_pin_wakeup_enable(uint32_t i, GPIO_INT_TYPE intr_state); 192 193 /** 194 * @brief disable GPIOs to wakeup the ESP32. 195 * Please do not call this function in SDK. 196 * 197 * @param None 198 * 199 * @return None 200 */ 201 void gpio_pin_wakeup_disable(void); 202 203 /** 204 * @brief set gpio input to a signal, one gpio can input to several signals. 205 * 206 * @param uint32_t gpio : gpio number, 0~0x2f 207 * gpio == 0x3C, input 0 to signal 208 * gpio == 0x3A, input nothing to signal 209 * gpio == 0x38, input 1 to signal 210 * 211 * @param uint32_t signal_idx : signal index. 212 * 213 * @param bool inv : the signal is inv or not 214 * 215 * @return None 216 */ 217 void gpio_matrix_in(uint32_t gpio, uint32_t signal_idx, bool inv); 218 219 /** 220 * @brief set signal output to gpio, one signal can output to several gpios. 221 * 222 * @param uint32_t gpio : gpio number, 0~0x2f 223 * 224 * @param uint32_t signal_idx : signal index. 225 * signal_idx == 0x100, cancel output put to the gpio 226 * 227 * @param bool out_inv : the signal output is invert or not 228 * 229 * @param bool oen_inv : the signal output enable is invert or not 230 * 231 * @return None 232 */ 233 void gpio_matrix_out(uint32_t gpio, uint32_t signal_idx, bool out_inv, bool oen_inv); 234 235 /** 236 * @brief Select pad as a gpio function from IOMUX. 237 * 238 * @param uint32_t gpio_num : gpio number, 0~0x2f 239 * 240 * @return None 241 */ 242 void gpio_pad_select_gpio(uint32_t gpio_num); 243 244 /** 245 * @brief Set pad driver capability. 246 * 247 * @param uint32_t gpio_num : gpio number, 0~0x2f 248 * 249 * @param uint32_t drv : 0-3 250 * 251 * @return None 252 */ 253 void gpio_pad_set_drv(uint32_t gpio_num, uint32_t drv); 254 255 /** 256 * @brief Pull up the pad from gpio number. 257 * 258 * @param uint32_t gpio_num : gpio number, 0~0x2f 259 * 260 * @return None 261 */ 262 void gpio_pad_pullup(uint32_t gpio_num); 263 264 /** 265 * @brief Pull down the pad from gpio number. 266 * 267 * @param uint32_t gpio_num : gpio number, 0~0x2f 268 * 269 * @return None 270 */ 271 void gpio_pad_pulldown(uint32_t gpio_num); 272 273 /** 274 * @brief Unhold the pad from gpio number. 275 * 276 * @param uint32_t gpio_num : gpio number, 0~0x2f 277 * 278 * @return None 279 */ 280 void gpio_pad_unhold(uint32_t gpio_num); 281 282 /** 283 * @brief Hold the pad from gpio number. 284 * 285 * @param uint32_t gpio_num : gpio number, 0~0x2f 286 * 287 * @return None 288 */ 289 void gpio_pad_hold(uint32_t gpio_num); 290 291 /** 292 * @brief enable gpio pad input. 293 * 294 * @param uint32_t gpio_num : gpio number, 0~0x2f 295 * 296 * @return None 297 */ 298 void gpio_pad_input_enable(uint32_t gpio_num); 299 300 /** 301 * @} 302 */ 303 304 #ifdef __cplusplus 305 } 306 #endif 307