key.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**************************************************************************************************
  2. Phyplus Microelectronics Limited confidential and proprietary.
  3. All rights reserved.
  4. IMPORTANT: All rights of this software belong to Phyplus Microelectronics
  5. Limited ("Phyplus"). Your use of this Software is limited to those
  6. specific rights granted under the terms of the business contract, the
  7. confidential agreement, the non-disclosure agreement and any other forms
  8. of agreements as a customer or a partner of Phyplus. You may not use this
  9. Software unless you agree to abide by the terms of these agreements.
  10. You acknowledge that the Software may not be modified, copied,
  11. distributed or disclosed unless embedded on a Phyplus Bluetooth Low Energy
  12. (BLE) integrated circuit, either as a product or is integrated into your
  13. products. Other than for the aforementioned purposes, you may not use,
  14. reproduce, copy, prepare derivative works of, modify, distribute, perform,
  15. display or sell this Software and/or its documentation for any purposes.
  16. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  17. PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  18. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  19. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  20. PHYPLUS OR ITS SUBSIDIARIES BE LIABLE OR OBLIGATED UNDER CONTRACT,
  21. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  22. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  23. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  24. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  25. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  26. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  27. **************************************************************************************************/
  28. #include "key.h"
  29. #include "log.h"
  30. #include "OSAL.h"
  31. #include "pwrmgr.h"
  32. #include "common.h"
  33. #include "error.h"
  34. key_contex_t key_state;
  35. extern uint32 getMcuPrecisionCount(void);
  36. static int key_timer_start(uint32 intval_ms)
  37. {
  38. osal_start_timerEx(key_state.task_id, HAL_KEY_EVENT, intval_ms);
  39. return 0;
  40. }
  41. static void key_idle_handler(uint8 i,IO_Wakeup_Pol_e type)
  42. {
  43. if(((type == NEGEDGE) && (key_state.key[i].idle_level == HAL_HIGH_IDLE)) ||
  44. ((type == POSEDGE) && (key_state.key[i].idle_level == HAL_LOW_IDLE))){
  45. hal_pwrmgr_lock(MOD_USR1);
  46. key_state.key[i].state = HAL_STATE_KEY_PRESS_DEBOUNCE;
  47. key_state.temp[i].in_enable = TRUE;
  48. key_timer_start(HAL_KEY_DEBOUNCD);
  49. }
  50. }
  51. static void key_press_debonce_handler(uint8 i,IO_Wakeup_Pol_e type)
  52. {
  53. if(((type == NEGEDGE) && (key_state.key[i].idle_level == HAL_HIGH_IDLE)) ||
  54. ((type == POSEDGE) && (key_state.key[i].idle_level == HAL_LOW_IDLE))){
  55. key_timer_start(HAL_KEY_DEBOUNCD);
  56. }
  57. }
  58. static void key_press_handler(uint8 i,IO_Wakeup_Pol_e type)
  59. {
  60. if(((type == POSEDGE) && (key_state.key[i].idle_level == HAL_HIGH_IDLE)) ||
  61. ((type == NEGEDGE) && (key_state.key[i].idle_level == HAL_LOW_IDLE))){
  62. hal_pwrmgr_lock(MOD_USR1);
  63. key_state.key[i].state = HAL_STATE_KEY_RELEASE_DEBOUNCE;
  64. key_timer_start(HAL_KEY_DEBOUNCD);
  65. }
  66. }
  67. static void key_release_debonce_handler(uint8 i,IO_Wakeup_Pol_e type)
  68. {
  69. if(((type == POSEDGE) && (key_state.key[i].idle_level == HAL_HIGH_IDLE)) ||
  70. ((type == NEGEDGE) && (key_state.key[i].idle_level == HAL_LOW_IDLE))){
  71. key_timer_start(HAL_KEY_DEBOUNCD);
  72. }
  73. }
  74. static void pin_event_handler(GPIO_Pin_e pin,IO_Wakeup_Pol_e type)
  75. {
  76. uint8 i;
  77. for(i = 0;i < HAL_KEY_NUM;i++){
  78. if(pin == key_state.key[i].pin)
  79. break;
  80. }
  81. if(i < HAL_KEY_NUM){
  82. switch(key_state.key[i].state)
  83. {
  84. case HAL_STATE_KEY_IDLE:
  85. key_idle_handler(i,type);
  86. break;
  87. case HAL_STATE_KEY_PRESS_DEBOUNCE:
  88. key_press_debonce_handler(i,type);
  89. break;
  90. case HAL_STATE_KEY_PRESS:
  91. key_press_handler(i,type);
  92. break;
  93. case HAL_STATE_KEY_RELEASE_DEBOUNCE:
  94. key_release_debonce_handler(i,type);
  95. break;
  96. default:
  97. break;
  98. }
  99. }
  100. }
  101. void key_init(void)
  102. {
  103. uint8 i;
  104. for(i = 0; i < HAL_KEY_NUM; ++i){
  105. if(key_state.key[i].idle_level == HAL_LOW_IDLE){
  106. hal_gpio_pull_set(key_state.key[i].pin,PULL_DOWN);
  107. }
  108. else{
  109. hal_gpio_pull_set(key_state.key[i].pin,WEAK_PULL_UP);
  110. }
  111. key_state.temp[i].timer_tick = 0;
  112. (void)hal_gpioin_register(key_state.key[i].pin, pin_event_handler, pin_event_handler);
  113. }
  114. hal_pwrmgr_register(MOD_USR1, NULL, NULL);
  115. }
  116. static void key_press_debonce_timer_handler(uint8 i)
  117. {
  118. if(((hal_gpio_read(key_state.key[i].pin) == FALSE) && (key_state.key[i].idle_level == HAL_HIGH_IDLE)) ||
  119. ((hal_gpio_read(key_state.key[i].pin) == TRUE) && (key_state.key[i].idle_level == HAL_LOW_IDLE)))
  120. {
  121. #ifndef HAL_KEY_SUPPORT_LONG_PRESS
  122. hal_pwrmgr_unlock(MOD_USR1);
  123. #endif
  124. key_state.key[i].state = HAL_STATE_KEY_PRESS;
  125. key_state.temp[i].timer_tick = getMcuPrecisionCount();
  126. if(key_state.key_callbank != NULL)
  127. {
  128. key_state.key_callbank(i,HAL_KEY_EVT_PRESS);
  129. }
  130. }
  131. else
  132. {
  133. key_state.key[i].state = HAL_STATE_KEY_IDLE;
  134. key_state.temp[i].in_enable = FALSE;
  135. }
  136. }
  137. static void key_release_debonce_timer_handler(uint8 i)
  138. {
  139. if(key_state.key[i].idle_level == hal_gpio_read(key_state.key[i].pin))
  140. {
  141. osal_stop_timerEx(key_state.task_id,HAL_KEY_EVT_LONG_PRESS);
  142. uint32_t hold_tick = (getMcuPrecisionCount() - key_state.temp[i].timer_tick)*625;
  143. hal_pwrmgr_unlock(MOD_USR1);
  144. if(key_state.key_callbank != NULL)
  145. {
  146. #ifdef HAL_KEY_SUPPORT_LONG_PRESS
  147. if(hold_tick >= (HAL_KEY_LONG_PRESS_TIME * 1000))//2s
  148. {
  149. key_state.key_callbank(i,HAL_KEY_EVT_LONG_RELEASE);
  150. key_state.key[i].state = HAL_STATE_KEY_IDLE;
  151. }
  152. else
  153. #endif
  154. {
  155. key_state.key_callbank(i,HAL_KEY_EVT_RELEASE);
  156. key_state.key[i].state = HAL_STATE_KEY_IDLE;
  157. }
  158. }
  159. }
  160. else
  161. {
  162. key_state.key[i].state = HAL_STATE_KEY_PRESS;
  163. }
  164. }
  165. void gpio_key_timer_handler(uint8 i)
  166. {
  167. switch(key_state.key[i].state)
  168. {
  169. case HAL_STATE_KEY_PRESS_DEBOUNCE:
  170. key_press_debonce_timer_handler(i);
  171. break;
  172. case HAL_STATE_KEY_RELEASE_DEBOUNCE:
  173. key_release_debonce_timer_handler(i);
  174. break;
  175. default:
  176. break;
  177. }
  178. }