|
| 1 | +#include "ets_sys.h" |
| 2 | +#include "osapi.h" |
| 3 | +#include "gpio.h" |
| 4 | +#include "os_type.h" |
| 5 | +#include "user_interface.h" |
| 6 | + |
| 7 | +#define user_procTaskPrio 0 |
| 8 | +#define user_procTaskQueueLen 1 |
| 9 | +os_event_t user_procTaskQueue[user_procTaskQueueLen]; |
| 10 | +static void loop(os_event_t *events); |
| 11 | + |
| 12 | + |
| 13 | +// variable modified indirectly by interrupt handler |
| 14 | +volatile int whatyouwant; |
| 15 | + |
| 16 | +// gpio interrupt handler. See below |
| 17 | +LOCAL void gpio_intr_handler(int * dummy); |
| 18 | + |
| 19 | +//------------------------------------------------------------------------------------------------- |
| 20 | +// loop function will be execute by "os" periodically |
| 21 | +static void ICACHE_FLASH_ATTR loop(os_event_t *events) |
| 22 | +{ |
| 23 | + static int level; |
| 24 | + |
| 25 | + os_printf("What you want %d \r\n",whatyouwant); |
| 26 | +// change GPIO 2 level |
| 27 | + level = !level; |
| 28 | + GPIO_OUTPUT_SET(GPIO_ID_PIN(2), ((level) ? 1 : 0)); |
| 29 | +// Delay |
| 30 | + os_delay_us(100000); |
| 31 | +// turn again |
| 32 | + system_os_post(user_procTaskPrio, 0, 0 ); |
| 33 | +} |
| 34 | + |
| 35 | +//------------------------------------------------------------------------------------------------- |
| 36 | +//Init function |
| 37 | +void ICACHE_FLASH_ATTR user_init() |
| 38 | +{ |
| 39 | + |
| 40 | +// Initialize UART0 to use as debug |
| 41 | + uart_div_modify(0, UART_CLK_FREQ / 9600); |
| 42 | + |
| 43 | + os_printf( |
| 44 | + "GPIO Interrupt ESP8266 test\r\n" |
| 45 | + "---------------------------\r\n" |
| 46 | + " This program out a square wave on gpio2 and count edges for gpio0\r\n" |
| 47 | + " The program report count edges gpio0 periodically.\r\n" |
| 48 | + " You Can\r\n" |
| 49 | + " 1.- Connect GPIO0 to GPIO2 so you can see increment count edge periodically\r\n" |
| 50 | + " 2.- Unconnect GPIO0 from GPIO2 so you can see stop count edge\r\n" |
| 51 | + " 3.- Connect GPIO0 to ground to increment count edge\r\n" |
| 52 | +); |
| 53 | + |
| 54 | +// Initialize the GPIO subsystem. |
| 55 | + gpio_init(); |
| 56 | + |
| 57 | +// ================================================= |
| 58 | +// Initialize GPIO2 and GPIO0 as GPIO |
| 59 | +// ================================================= |
| 60 | +// |
| 61 | +// |
| 62 | +// PIN_FUNC_SELECT() defined in eagle_soc.h |
| 63 | +// PERIPHS_IO_MUX_... for each pin defined in eagle_soc.h |
| 64 | +// FUNC_... for each PERIPHS macros defined in eagle_soc.h |
| 65 | +// |
| 66 | +// From eagle_soc.h: |
| 67 | +// GPIO0 only can be GPIO |
| 68 | +// GPIO2 can be GPIO, TXD from uart0, or TXD from uart1 |
| 69 | +// |
| 70 | +// #define PERIPHS_IO_MUX_GPIO0_U (PERIPHS_IO_MUX + 0x34) |
| 71 | +// #define FUNC_GPIO0 0 |
| 72 | +// #define PERIPHS_IO_MUX_GPIO2_U (PERIPHS_IO_MUX + 0x38) |
| 73 | +// #define FUNC_GPIO2 0 |
| 74 | +// #define FUNC_U1TXD_BK 2 |
| 75 | +// #define FUNC_U0TXD_BK 4 |
| 76 | +// |
| 77 | + |
| 78 | + PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0); |
| 79 | + PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_GPIO2); |
| 80 | + |
| 81 | +// You can select enable/disable internal pullup/pulldown for each pin |
| 82 | +// |
| 83 | +// PIN_PULLUP_DIS(PIN_NAME) |
| 84 | +// PIN_PULLUP_EN(PIN_NAME) |
| 85 | +// PIN_PULLDWN_DIS(PIN_NAME) |
| 86 | +// PIN_PULLDWN_EN(PIN_NAME) |
| 87 | + |
| 88 | + PIN_PULLUP_DIS(PERIPHS_IO_MUX_GPIO0_U); |
| 89 | + PIN_PULLDWN_DIS(PERIPHS_IO_MUX_GPIO0_U); |
| 90 | + PIN_PULLUP_EN(PERIPHS_IO_MUX_GPIO2_U); |
| 91 | + |
| 92 | +// |
| 93 | +// void gpio_output_set(uint32 set_mask, uint32 clear_mask, uint32 enable_mask, uint32 disable_mask) |
| 94 | +// |
| 95 | +// set status and direction pin by mask gpio id pin. |
| 96 | + |
| 97 | + gpio_output_set(0, GPIO_ID_PIN(2), GPIO_ID_PIN(2), GPIO_ID_PIN(0)); // set gpio 2 as output and low. set gpio 0 as input |
| 98 | + |
| 99 | +// ================================================= |
| 100 | +// Activate gpio interrupt for gpio2 |
| 101 | +// ================================================= |
| 102 | + |
| 103 | + // Disable interrupts by GPIO |
| 104 | + ETS_GPIO_INTR_DISABLE(); |
| 105 | + |
| 106 | +// Attach interrupt handle to gpio interrupts. |
| 107 | +// You can set a void pointer that will be passed to interrupt handler each interrupt |
| 108 | + |
| 109 | + ETS_GPIO_INTR_ATTACH(gpio_intr_handler, &whatyouwant); |
| 110 | + |
| 111 | +// void gpio_register_set(uint32 reg_id, uint32 value); |
| 112 | +// |
| 113 | +// From include file |
| 114 | +// Set the specified GPIO register to the specified value. |
| 115 | +// This is a very general and powerful interface that is not |
| 116 | +// expected to be used during normal operation. It is intended |
| 117 | +// mainly for debug, or for unusual requirements. |
| 118 | +// |
| 119 | +// All people repeat this mantra but I don't know what it means |
| 120 | +// |
| 121 | + gpio_register_set(GPIO_PIN_ADDR(0), |
| 122 | + GPIO_PIN_INT_TYPE_SET(GPIO_PIN_INTR_DISABLE) | |
| 123 | + GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_DISABLE) | |
| 124 | + GPIO_PIN_SOURCE_SET(GPIO_AS_PIN_SOURCE)); |
| 125 | + |
| 126 | +// clear gpio status. Say ESP8266EX SDK Programming Guide in 5.1.6. GPIO interrupt handler |
| 127 | + |
| 128 | + GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, BIT(0)); |
| 129 | + |
| 130 | +// enable interrupt for his GPIO |
| 131 | +// GPIO_PIN_INTR_... defined in gpio.h |
| 132 | + |
| 133 | + gpio_pin_intr_state_set(GPIO_ID_PIN(0), GPIO_PIN_INTR_ANYEGDE); |
| 134 | + |
| 135 | + ETS_GPIO_INTR_ENABLE(); |
| 136 | + |
| 137 | + //Start os task |
| 138 | + system_os_task(loop, user_procTaskPrio,user_procTaskQueue, user_procTaskQueueLen); |
| 139 | + |
| 140 | + system_os_post(user_procTaskPrio, 0, 0 ); |
| 141 | +} |
| 142 | + |
| 143 | +//------------------------------------------------------------------------------------------------- |
| 144 | +// interrupt handler |
| 145 | +// this function will be executed on any edge of GPIO0 |
| 146 | +LOCAL void gpio_intr_handler(int * dummy) |
| 147 | +{ |
| 148 | +// clear gpio status. Say ESP8266EX SDK Programming Guide in 5.1.6. GPIO interrupt handler |
| 149 | + |
| 150 | + uint32 gpio_status = GPIO_REG_READ(GPIO_STATUS_ADDRESS); |
| 151 | + |
| 152 | +// if the interrupt was by GPIO0 |
| 153 | + if (gpio_status & BIT(0)) |
| 154 | + { |
| 155 | +// disable interrupt for GPIO0 |
| 156 | + gpio_pin_intr_state_set(GPIO_ID_PIN(0), GPIO_PIN_INTR_DISABLE); |
| 157 | + |
| 158 | +// Do something, for example, increment whatyouwant indirectly |
| 159 | + (*dummy)++; |
| 160 | + |
| 161 | +//clear interrupt status for GPIO0 |
| 162 | + GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, gpio_status & BIT(0)); |
| 163 | + |
| 164 | +// Reactivate interrupts for GPIO0 |
| 165 | + gpio_pin_intr_state_set(GPIO_ID_PIN(0), GPIO_PIN_INTR_ANYEGDE); |
| 166 | + } |
| 167 | +} |
| 168 | + |
0 commit comments