ESP8266 Non-OS SDK 和 RTOS SDK 实现GPIO的方法稍有不同:
对于 Non-OS SDK,比如需要把 MTDO 配置成输入,同时下降沿触发中断:
gpio_init(void){PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U,FUNC_GPIO15); GPIO_DIS_OUTPUT(GPIO_ID_PIN(15)); //Configure it in input mode. ETS_GPIO_INTR_DISABLE(); //Close the GPIO interrupt//Register the interrupt function ETS_GPIO_INTR_ATTACH(GPIO_INTERRUPT,NULL); gpio_pin_intr_state_set(GPIO_ID_PIN(15),GPIO_PIN_INTR_NEGEDGE); //Falling edge trigger ETS_GPIO_INTR_ENABLE() ; //Enable the GPIO interrupt}
对于 RTOS SDK,实现相同的功能:
{ GPIO_ConfigTypeDef gpio_in_cfg; //Define GPIO Init Structure gpio_in_cfg.GPIO_IntrType = GPIO_PIN_INTR_NEGEDGE; // gpio_in_cfg.GPIO_Mode = GPIO_Mode_Input; //Input mode gpio_in_cfg.GPIO_Pullup = GPIO_PullUp_EN; gpio_in_cfg.GPIO_Pin = keygpio; // Enable GPIO gpio_config(&gpio_in_cfg); //Initialization function GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, TRUE); gpio_intr_handler_register(keyinterrupt_cb, NULL); // Register the interrupt function _xt_isr_unmask(1 << ETS_GPIO_INUM); //Enable the GPIO interrupt}
特别留意:
在中断回调函数中,
{ _xt_isr_mask(1<
在disable interrupt之后,如果有中断信号触发中断引脚,即使还未使能中断,但interrupt mask仍然会被置为中断标志,因此,为避免Enable the GPIO interrupt之后,由于受之前中断位的影响而马上再次中断,故在_xt_isr_unmask(1 << ETS_GPIO_INUM); //Enable the GPIO interrupt之前务必加上GPIO_REG_WRITE( GPIO_STATUS_W1TC_ADDRESS, TRUE ); //clear interrupt mask