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