key.c 6.2 KB

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