ESP8266_RTOS_SDK  v1.4.0
gpio.h
1 /*
2  * ESPRSSIF MIT License
3  *
4  * Copyright (c) 2015 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
5  *
6  * Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case,
7  * it is free of charge, to any person obtaining a copy of this software and associated
8  * documentation files (the "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the Software is furnished
11  * to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all copies or
14  * substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  *
23  */
24 
25 #ifndef __GPIO_H__
26 #define __GPIO_H__
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 #define GPIO_Pin_0 (BIT(0)) /* Pin 0 selected */
33 #define GPIO_Pin_1 (BIT(1)) /* Pin 1 selected */
34 #define GPIO_Pin_2 (BIT(2)) /* Pin 2 selected */
35 #define GPIO_Pin_3 (BIT(3)) /* Pin 3 selected */
36 #define GPIO_Pin_4 (BIT(4)) /* Pin 4 selected */
37 #define GPIO_Pin_5 (BIT(5)) /* Pin 5 selected */
38 #define GPIO_Pin_6 (BIT(6)) /* Pin 6 selected */
39 #define GPIO_Pin_7 (BIT(7)) /* Pin 7 selected */
40 #define GPIO_Pin_8 (BIT(8)) /* Pin 8 selected */
41 #define GPIO_Pin_9 (BIT(9)) /* Pin 9 selected */
42 #define GPIO_Pin_10 (BIT(10)) /* Pin 10 selected */
43 #define GPIO_Pin_11 (BIT(11)) /* Pin 11 selected */
44 #define GPIO_Pin_12 (BIT(12)) /* Pin 12 selected */
45 #define GPIO_Pin_13 (BIT(13)) /* Pin 13 selected */
46 #define GPIO_Pin_14 (BIT(14)) /* Pin 14 selected */
47 #define GPIO_Pin_15 (BIT(15)) /* Pin 15 selected */
48 #define GPIO_Pin_All (0xFFFF) /* All pins selected */
49 
50 #define GPIO_PIN_REG_0 PERIPHS_IO_MUX_GPIO0_U
51 #define GPIO_PIN_REG_1 PERIPHS_IO_MUX_U0TXD_U
52 #define GPIO_PIN_REG_2 PERIPHS_IO_MUX_GPIO2_U
53 #define GPIO_PIN_REG_3 PERIPHS_IO_MUX_U0RXD_U
54 #define GPIO_PIN_REG_4 PERIPHS_IO_MUX_GPIO4_U
55 #define GPIO_PIN_REG_5 PERIPHS_IO_MUX_GPIO5_U
56 #define GPIO_PIN_REG_6 PERIPHS_IO_MUX_SD_CLK_U
57 #define GPIO_PIN_REG_7 PERIPHS_IO_MUX_SD_DATA0_U
58 #define GPIO_PIN_REG_8 PERIPHS_IO_MUX_SD_DATA1_U
59 #define GPIO_PIN_REG_9 PERIPHS_IO_MUX_SD_DATA2_U
60 #define GPIO_PIN_REG_10 PERIPHS_IO_MUX_SD_DATA3_U
61 #define GPIO_PIN_REG_11 PERIPHS_IO_MUX_SD_CMD_U
62 #define GPIO_PIN_REG_12 PERIPHS_IO_MUX_MTDI_U
63 #define GPIO_PIN_REG_13 PERIPHS_IO_MUX_MTCK_U
64 #define GPIO_PIN_REG_14 PERIPHS_IO_MUX_MTMS_U
65 #define GPIO_PIN_REG_15 PERIPHS_IO_MUX_MTDO_U
66 
67 #define GPIO_PIN_REG(i) \
68  (i==0) ? GPIO_PIN_REG_0: \
69  (i==1) ? GPIO_PIN_REG_1: \
70  (i==2) ? GPIO_PIN_REG_2: \
71  (i==3) ? GPIO_PIN_REG_3: \
72  (i==4) ? GPIO_PIN_REG_4: \
73  (i==5) ? GPIO_PIN_REG_5: \
74  (i==6) ? GPIO_PIN_REG_6: \
75  (i==7) ? GPIO_PIN_REG_7: \
76  (i==8) ? GPIO_PIN_REG_8: \
77  (i==9) ? GPIO_PIN_REG_9: \
78  (i==10)? GPIO_PIN_REG_10: \
79  (i==11)? GPIO_PIN_REG_11: \
80  (i==12)? GPIO_PIN_REG_12: \
81  (i==13)? GPIO_PIN_REG_13: \
82  (i==14)? GPIO_PIN_REG_14: \
83  GPIO_PIN_REG_15
84 
85 #define GPIO_PIN_ADDR(i) (GPIO_PIN0_ADDRESS + i*4)
86 
87 #define GPIO_ID_IS_PIN_REGISTER(reg_id) \
88  ((reg_id >= GPIO_ID_PIN0) && (reg_id <= GPIO_ID_PIN(GPIO_PIN_COUNT-1)))
89 
90 #define GPIO_REGID_TO_PINIDX(reg_id) ((reg_id) - GPIO_ID_PIN0)
91 
92 typedef enum {
93  GPIO_PIN_INTR_DISABLE = 0,
94  GPIO_PIN_INTR_POSEDGE = 1,
95  GPIO_PIN_INTR_NEGEDGE = 2,
96  GPIO_PIN_INTR_ANYEDGE = 3,
97  GPIO_PIN_INTR_LOLEVEL = 4,
98  GPIO_PIN_INTR_HILEVEL = 5
99 } GPIO_INT_TYPE;
100 
101 typedef enum {
102  GPIO_Mode_Input = 0x0,
103  GPIO_Mode_Out_OD,
104  GPIO_Mode_Output ,
105  GPIO_Mode_Sigma_Delta ,
106 } GPIOMode_TypeDef;
107 
108 typedef enum {
109  GPIO_PullUp_DIS = 0x0,
110  GPIO_PullUp_EN = 0x1,
111 } GPIO_Pullup_IF;
112 
113 typedef struct {
114  uint16 GPIO_Pin;
115  GPIOMode_TypeDef GPIO_Mode;
116  GPIO_Pullup_IF GPIO_Pullup;
117  GPIO_INT_TYPE GPIO_IntrType;
119 
144 #define GPIO_OUTPUT_SET(gpio_no, bit_value) \
145  gpio_output_conf(bit_value<<gpio_no, ((~bit_value)&0x01)<<gpio_no, 1<<gpio_no, 0)
146 
155 #define GPIO_OUTPUT(gpio_bits, bit_value) \
156  if(bit_value) gpio_output_conf(gpio_bits, 0, gpio_bits, 0);\
157  else gpio_output_conf(0, gpio_bits, gpio_bits, 0)
158 
166 #define GPIO_DIS_OUTPUT(gpio_no) gpio_output_conf(0, 0, 0, 1<<gpio_no)
167 
175 #define GPIO_AS_INPUT(gpio_bits) gpio_output_conf(0, 0, 0, gpio_bits)
176 
184 #define GPIO_AS_OUTPUT(gpio_bits) gpio_output_conf(0, 0, gpio_bits, 0)
185 
193 #define GPIO_INPUT_GET(gpio_no) ((gpio_input_get()>>gpio_no)&BIT0)
194 
202 void gpio16_output_conf(void);
203 
211 void gpio16_output_set(uint8 value);
212 
220 void gpio16_input_conf(void);
221 
229 uint8 gpio16_input_get(void);
230 
245 void gpio_output_conf(uint32 set_mask, uint32 clear_mask, uint32 enable_mask, uint32 disable_mask);
246 
255 void gpio_intr_handler_register(void *fn, void *arg);
256 
265 void gpio_pin_wakeup_enable(uint32 i, GPIO_INT_TYPE intr_state);
266 
275 
284 void gpio_pin_intr_state_set(uint32 i, GPIO_INT_TYPE intr_state);
285 
293 uint32 gpio_input_get(void);
294 
303 #ifdef __cplusplus
304 }
305 #endif
306 
307 #endif
void gpio16_output_set(uint8 value)
Set GPIO16 output level.
uint32 gpio_input_get(void)
Sample the value of GPIO input pins and returns a bitmask.
uint16 GPIO_Pin
Definition: gpio.h:114
GPIO_Pullup_IF GPIO_Pullup
Definition: gpio.h:116
void gpio_pin_wakeup_enable(uint32 i, GPIO_INT_TYPE intr_state)
Configure GPIO wake up to light sleep,Only level way is effective.
void gpio16_output_conf(void)
Enable GPIO16 output.
uint8 gpio16_input_get(void)
Sample the value of GPIO16 input.
Definition: gpio.h:113
void gpio_pin_intr_state_set(uint32 i, GPIO_INT_TYPE intr_state)
Config interrupt types of GPIO pin.
GPIO_INT_TYPE GPIO_IntrType
Definition: gpio.h:117
GPIOMode_TypeDef GPIO_Mode
Definition: gpio.h:115
void gpio16_input_conf(void)
Enable GPIO pin intput.
void gpio_output_conf(uint32 set_mask, uint32 clear_mask, uint32 enable_mask, uint32 disable_mask)
Configure Gpio pins out or input.
void gpio_pin_wakeup_disable()
Disable GPIO wake up to light sleep.
void gpio_intr_handler_register(void *fn, void *arg)
Register an application-specific interrupt handler for GPIO pin interrupts.