key.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /******************************************************************************
  2. * Copyright 2013-2014 Espressif Systems (Wuxi)
  3. *
  4. * FileName: key.c
  5. *
  6. * Description: key driver, now can use different gpio and install different function
  7. *
  8. * Modification history:
  9. * 2014/5/1, v1.0 create this file.
  10. *******************************************************************************/
  11. #ifdef __ESP8266__
  12. #include "ets_sys.h"
  13. #include "os_type.h"
  14. #include "osapi.h"
  15. #include "mem.h"
  16. #include "platform.h"
  17. #include "gpio.h"
  18. #include "user_interface.h"
  19. #include "driver/key.h"
  20. LOCAL void ICACHE_RAM_ATTR key_intr_handler(void *arg);
  21. /******************************************************************************
  22. * FunctionName : key_init_single
  23. * Description : init single key's gpio and register function
  24. * Parameters : uint8 gpio_id - which gpio to use
  25. * uint32 gpio_name - gpio mux name
  26. * uint32 gpio_func - gpio function
  27. * key_function long_press - long press function, needed to install
  28. * key_function short_press - short press function, needed to install
  29. * Returns : single_key_param - single key parameter, needed by key init
  30. *******************************************************************************/
  31. struct single_key_param *ICACHE_FLASH_ATTR
  32. key_init_single(uint8 gpio_id, uint32 gpio_name, uint8 gpio_func, key_function long_press, key_function short_press)
  33. {
  34. struct single_key_param *single_key = (struct single_key_param *)zalloc(sizeof(struct single_key_param));
  35. single_key->gpio_id = gpio_id;
  36. single_key->gpio_name = gpio_name;
  37. single_key->gpio_func = gpio_func;
  38. single_key->long_press = long_press;
  39. single_key->short_press = short_press;
  40. return single_key;
  41. }
  42. /******************************************************************************
  43. * FunctionName : key_init
  44. * Description : init keys
  45. * Parameters : key_param *keys - keys parameter, which inited by key_init_single
  46. * Returns : none
  47. *******************************************************************************/
  48. void ICACHE_FLASH_ATTR
  49. key_init(struct keys_param *keys)
  50. {
  51. uint8 i;
  52. ETS_GPIO_INTR_ATTACH(key_intr_handler, keys);
  53. ETS_GPIO_INTR_DISABLE();
  54. for (i = 0; i < keys->key_num; i++) {
  55. keys->single_key[i]->key_level = 1;
  56. PIN_FUNC_SELECT(keys->single_key[i]->gpio_name, keys->single_key[i]->gpio_func);
  57. gpio_output_set(0, 0, 0, GPIO_ID_PIN(keys->single_key[i]->gpio_id));
  58. gpio_register_set(GPIO_PIN_ADDR(keys->single_key[i]->gpio_id), GPIO_PIN_INT_TYPE_SET(GPIO_PIN_INTR_DISABLE)
  59. | GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_DISABLE)
  60. | GPIO_PIN_SOURCE_SET(GPIO_AS_PIN_SOURCE));
  61. //clear gpio14 status
  62. GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, BIT(keys->single_key[i]->gpio_id));
  63. //enable interrupt
  64. gpio_pin_intr_state_set(GPIO_ID_PIN(keys->single_key[i]->gpio_id), GPIO_PIN_INTR_NEGEDGE);
  65. }
  66. ETS_GPIO_INTR_ENABLE();
  67. }
  68. /******************************************************************************
  69. * FunctionName : key_5s_cb
  70. * Description : long press 5s timer callback
  71. * Parameters : single_key_param *single_key - single key parameter
  72. * Returns : none
  73. *******************************************************************************/
  74. LOCAL void ICACHE_FLASH_ATTR
  75. key_5s_cb(struct single_key_param *single_key)
  76. {
  77. os_timer_disarm(&single_key->key_5s);
  78. // low, then restart
  79. if (0 == GPIO_INPUT_GET(GPIO_ID_PIN(single_key->gpio_id))) {
  80. if (single_key->long_press) {
  81. single_key->long_press();
  82. }
  83. }
  84. }
  85. /******************************************************************************
  86. * FunctionName : key_50ms_cb
  87. * Description : 50ms timer callback to check it's a real key push
  88. * Parameters : single_key_param *single_key - single key parameter
  89. * Returns : none
  90. *******************************************************************************/
  91. LOCAL void ICACHE_FLASH_ATTR
  92. key_50ms_cb(struct single_key_param *single_key)
  93. {
  94. os_timer_disarm(&single_key->key_50ms);
  95. // high, then key is up
  96. if (1 == GPIO_INPUT_GET(GPIO_ID_PIN(single_key->gpio_id))) {
  97. os_timer_disarm(&single_key->key_5s);
  98. single_key->key_level = 1;
  99. gpio_pin_intr_state_set(GPIO_ID_PIN(single_key->gpio_id), GPIO_PIN_INTR_NEGEDGE);
  100. if (single_key->short_press) {
  101. single_key->short_press();
  102. }
  103. } else {
  104. gpio_pin_intr_state_set(GPIO_ID_PIN(single_key->gpio_id), GPIO_PIN_INTR_POSEDGE);
  105. }
  106. }
  107. /******************************************************************************
  108. * FunctionName : key_intr_handler
  109. * Description : key interrupt handler
  110. * Parameters : key_param *keys - keys parameter, which inited by key_init_single
  111. * Returns : none
  112. *******************************************************************************/
  113. LOCAL void
  114. key_intr_handler(void *arg)
  115. {
  116. struct keys_param *keys = arg;
  117. uint8 i;
  118. uint32 gpio_status = GPIO_REG_READ(GPIO_STATUS_ADDRESS);
  119. for (i = 0; i < keys->key_num; i++) {
  120. if (gpio_status & BIT(keys->single_key[i]->gpio_id)) {
  121. //disable interrupt
  122. gpio_pin_intr_state_set(GPIO_ID_PIN(keys->single_key[i]->gpio_id), GPIO_PIN_INTR_DISABLE);
  123. //clear interrupt status
  124. GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, gpio_status & BIT(keys->single_key[i]->gpio_id));
  125. if (keys->single_key[i]->key_level == 1) {
  126. // 5s, restart & enter softap mode
  127. os_timer_disarm(&keys->single_key[i]->key_5s);
  128. os_timer_setfn(&keys->single_key[i]->key_5s, (os_timer_func_t *)key_5s_cb, keys->single_key[i]);
  129. os_timer_arm(&keys->single_key[i]->key_5s, 5000, 0);
  130. keys->single_key[i]->key_level = 0;
  131. gpio_pin_intr_state_set(GPIO_ID_PIN(keys->single_key[i]->gpio_id), GPIO_PIN_INTR_POSEDGE);
  132. } else {
  133. // 50ms, check if this is a real key up
  134. os_timer_disarm(&keys->single_key[i]->key_50ms);
  135. os_timer_setfn(&keys->single_key[i]->key_50ms, (os_timer_func_t *)key_50ms_cb, keys->single_key[i]);
  136. os_timer_arm(&keys->single_key[i]->key_50ms, 50, 0);
  137. }
  138. }
  139. }
  140. }
  141. #endif