ESP8266 GPIO
来自Jack's Lab
(版本间的差异)
(→概述) |
|||
第14行: | 第14行: | ||
* PIN15 --- GPIO0 / SPICS2 | * PIN15 --- GPIO0 / SPICS2 | ||
* PIN16 --- GPIO4 | * PIN16 --- GPIO4 | ||
+ | |||
+ | * PIN18 --- SPIHD; HSPIHD; GPIO9 | ||
+ | * PIN19 --- SPIWP; HSPIWP; GPIO10 | ||
+ | * PIN20 --- SPI_CS0; GPIO11 | ||
+ | * PIN21 --- SPI_CLK; GPIO6 | ||
+ | * PIN22 --- SPI_MSIO; GPIO7 | ||
+ | * PIN23 --- SPI_MOSI; GPIO8 | ||
+ | |||
* PIN24 --- GPIO5 | * PIN24 --- GPIO5 | ||
* PIN25 --- GPIO3 / UART RX during flash programming / URXD | * PIN25 --- GPIO3 / UART RX during flash programming / URXD |
2015年10月15日 (四) 12:34的版本
目录 |
1 概述
- PIN8 --- GPIO16 / Deep-Sleep Wakeup
- PIN9 --- GPIO14 / HSPICLK
- PIN10 --- GPIO12 / HSPIQ
- PIN12 --- GPIO13 / HSPID
- PIN13 --- GPIO15 / HSPICS
- PIN14 --- GPIO2 / UART TX during flash programming
- PIN15 --- GPIO0 / SPICS2
- PIN16 --- GPIO4
- PIN18 --- SPIHD; HSPIHD; GPIO9
- PIN19 --- SPIWP; HSPIWP; GPIO10
- PIN20 --- SPI_CS0; GPIO11
- PIN21 --- SPI_CLK; GPIO6
- PIN22 --- SPI_MSIO; GPIO7
- PIN23 --- SPI_MOSI; GPIO8
- PIN24 --- GPIO5
- PIN25 --- GPIO3 / UART RX during flash programming / URXD
- PIN26 --- GPIO1 / SPICS1 / UTXD
2 注意事项
GPIO2, GPIO0 和 GPIO3 (U0RXD) 用作 GPIO 要留意,外围电路,有可能影响其状态,造成正常启动失败
应避免使用 GPIO0, GPIO2, GPIO15
3 SDK API
3.1 PIN Macro
PIN_PULLUP_DIS(PERIPHS_IO_MUX_GPIO2_U);
PIN_PULLUP_EN(PERIPHS_IO_MUX_GPIO4_U);
PIN_PULLDWN_DIS(PERIPHS_IO_MUX_U0TXD_U);
PIN_PULLDWN_EN(PERIPHS_IO_MUX_GPIO5_U);
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12); // 选择 MTDI 脚复用为 GPIO12,大量管脚有多个功能,需用此宏选择具体的管脚功能
参考 SDK_DIR/include/eagle_soc.h
3.2 gpio_output_set
gpio_output_set(u32 set_mask, u32 clear_mask, u32 enable_mask, u32 disable_mask)
- set_mask: 设置输出为高的位,对应位为 1 输出高,对应位为 0 不改变状态
- clear_mask: 设置输出为低的位,对应位为 1 输出低,对应位为 0 不改变状态
- enable_mask: 设置使能输出的位
- disable_mask: 设置使能输入的位
#include <gpio.h> // Initialize the GPIO subsystem. gpio_init(); GPIO_REG_READ(GPIO_OUT_ADDRESS) & BIT2 == 1 //Set GPIO2 to output mode PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_GPIO2); //Set GPIO2 low gpio_output_set(0, BIT2, BIT2, 0); //Set GPIO0 to HIGH gpio_output_set(BIT2, 0, BIT2, 0); //Set GPIO12 to HIGH, GPIO13 to LOW gpio_output_set(BIT12, BIT13, BIT12|BIT13, 0);
有个简化的宏:GPIO_OUTPUT_SET(gpio_no, bit_value);
还有一个: GPIO_DIS_OUTPUT(gpio_no);
GPIO_OUTPUT_SET(0, 1); // set gpio0 to 1 GPIO_DIS_OUTPUT(0); // disable the GPIO0 output (change to input)
- 注意:批量输出时(同时操作四个 GPIO 口),使用 GPIO_OUTPUT_SET 要留意,宏堆叠有些奇怪现象:
GPIO14, 12, 13, 15 同时操作时,只有第一个 GPIO(14) 能改变状态,貌似被优化了还是咋的
GPIO5, 4, 14, 12 同时操作时,现象同上
GPIO1, 2, 0, 3 同时操作时,最后一个 GPIO (3) 无法改变状态
3.3 GPIO_INPUT_GET
GPIO_INPUT_GET(gpio_no);
GPIO_INPUT_GET(2);
等效于:
GPIO_REG_READ(GPIO_OUT_ADDRESS) & BIT2
3.4 GPIO 中断相关
ETS_GPIO_INTR_ATTACH(func, arg); ETS_GPIO_INTR_DISABLE(); EST_GPIO_INTR_ENABLE(); void gpio_pin_intr_state_set(u32 gpio_no, GPIO_INT_TYPE stat); typedef enum { GPIO_PIN_INTR_DISABLE = 0, GPIO_PIN_INTR_POSEDGE = 1, GPIO_PIN_INTR_NEGEDGE = 2, GPIO_PIN_INTR_ANYEDGE = 3, GPIO_PIN_INTR_LOLEVEL = 4, GPIO_PIN_INTR_HILEVEL =5 } GPIO_INT_TYPE; /* * Register an application-specific interrupt handler for GPIO pin * interrupts. Once the interrupt handler is called, it will not * be called again until after a call to gpio_intr_ack. Any GPIO * interrupts that occur during the interim are masked. * * The application-specific handler is called with a mask of * pending GPIO interrupts. After processing pin interrupts, the * application-specific handler may wish to use gpio_intr_pending * to check for any additional pending interrupts before it returns. */ void gpio_intr_handler_register(gpio_intr_handler_fn_t fn, void *arg); typedef void (* gpio_intr_handler_fn_t)(uint32 intr_mask, void *arg); /* Determine which GPIO interrupts are pending. */ uint32 gpio_intr_pending(void); /* * Acknowledge GPIO interrupts. * Intended to be called from the gpio_intr_handler_fn. */ void gpio_intr_ack(uint32 ack_mask); void gpio_pin_wakeup_enable(uint32 i, GPIO_INT_TYPE intr_state); void gpio_pin_wakeup_disable();
GPIO 中断处理:
uint32 gpio_status; gpio_status = GPIO_REG_READ(GPIO_STATUS_ADDRESS); //clear interrupt status GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, gpio_status);