driver_keyscan.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * @File name : driver_keyscan.c
  3. * @Author : Bluetrum IOT Team
  4. * @Date : 2023-03-15
  5. * @Description : This file provides functions to manage the most functionalities
  6. * of the KEYSCAN peripheral.
  7. *
  8. * Copyright (c) by Bluetrum, All Rights Reserved.
  9. */
  10. #include "driver_keyscan.h"
  11. /**
  12. * @brief Initializes the KEYSCAN peripheral according to the specified
  13. * parameters in the keyscan_init_struct.
  14. * @param keyscan_init_struct: pointer to a keyscan_init_typedef structure that
  15. * contains the configuration information for the specified KEYSCAN peripheral.
  16. * @retval
  17. */
  18. void keyscan_init(keyscan_init_typedef *keyscan_init_struct)
  19. {
  20. /* Configure Control0 Register */
  21. KEYSCAN->con1 &= ~(KEYSCAN_CON1_KEYPORT_STAB_TIME | KEYSCAN_CON1_KEY_DEBO_TIMES | KEYSCAN_CON1_KEY_WAIT_TIMES);
  22. KEYSCAN->con1 |= (keyscan_init_struct->stable_times * KEYSCAN_CON1_KEYPORT_STAB_TIME_0) & KEYSCAN_CON1_KEYPORT_STAB_TIME;
  23. KEYSCAN->con1 |= (keyscan_init_struct->debounce_times * KEYSCAN_CON1_KEY_DEBO_TIMES_0) & KEYSCAN_CON1_KEY_DEBO_TIMES;
  24. KEYSCAN->con1 |= (keyscan_init_struct->wait_times * KEYSCAN_CON1_KEY_WAIT_TIMES_0) & KEYSCAN_CON1_KEY_WAIT_TIMES;
  25. /* Configure Key Scan Mode for Row/Column Scan */
  26. KEYSCAN->con0 &= ~KEYSCAN_CON0_KEYSCAN_MODE;
  27. KEYSCAN->con0 |= keyscan_init_struct->key_mode & KEYSCAN_CON0_KEYSCAN_MODE;
  28. /* Configure Low Power Mode */
  29. KEYSCAN->con0 &= ~KEYSCAN_CON0_KEYSCAN_LP;
  30. KEYSCAN->con0 |= keyscan_init_struct->low_power_mode & KEYSCAN_CON0_KEYSCAN_LP;
  31. /* Configure Key Valid Wakeup System */
  32. KEYSCAN->con0 &= ~KEYSCAN_CON0_KEYVALID_WKUP_EN;
  33. KEYSCAN->con0 |= keyscan_init_struct->wakeup & KEYSCAN_CON0_KEYVALID_WKUP_EN;
  34. /* Configure Key Scan Software rstn */
  35. KEYSCAN->con0 &= ~KEYSCAN_CON0_KEYSCAN_SWRSTN;
  36. KEYSCAN->con0 |= keyscan_init_struct->sw_reset & KEYSCAN_CON0_KEYSCAN_SWRSTN;
  37. /* Configure Port Mask */
  38. KEYSCAN->con0 &= ~KEYSCAN_CON0_KEYPORT_EN;
  39. KEYSCAN->con0 |= (keyscan_init_struct->key_mask * KEYSCAN_CON0_KEYPORT_EN_0) & KEYSCAN_CON0_KEYPORT_EN;
  40. }
  41. /**
  42. * @brief De-initialize the specified KeyScan peripheral.
  43. * @retval None
  44. */
  45. void keyscan_deinit(void)
  46. {
  47. KEYSCAN->con0 = 0;
  48. KEYSCAN->con1 = 0;
  49. /* The clock is not unique to this module */
  50. //clk_gate0_cmd(CLK_GATE0_LP, CLK_DIS);
  51. }
  52. /**
  53. * @brief Enable or disable the KEYSCAN peripheral.
  54. * @param state: state of the KEYSCAN peripheral.
  55. * This parameter can be: ENABLE or DISABLE.
  56. * @retval None.
  57. */
  58. AT(.com_periph.keyscan.en_ctrl)
  59. void keyscan_cmd(FUNCTIONAL_STATE state)
  60. {
  61. if (state != DISABLE) {
  62. KEYSCAN->con0 |= KEYSCAN_CON0_KEYSCAN_EN;
  63. } else {
  64. KEYSCAN->con0 &= ~KEYSCAN_CON0_KEYSCAN_EN;
  65. }
  66. }
  67. /**
  68. * @brief Enable or disable the specified KEYSCAN interrupt.
  69. * @param isr: Function to be executed for service interruption.
  70. * @param pr: Priority of service interruption.
  71. * @param interrupt_type: specifies the KEYSCAN interrupt sources to be enable or disable.
  72. * this parameter can be one of the following values:
  73. * @arg KEYSCAN_IT_VALID: Any key has pressed interrupt.
  74. * @arg KEYSCAN_IT_INVALID: All key release interrupt.
  75. * @param state: the state of the KEYSCAN peripheral.
  76. * this parameter can be: ENABLE or DISABLE.
  77. * @retval None.
  78. */
  79. void keyscan_pic_config(isr_t isr, int pr, KEYSCAN_IT_TYPEDEF interrupt_type, FUNCTIONAL_STATE state)
  80. {
  81. uint32_t interrupt_pending_bit = 0;
  82. if (interrupt_type == 0) {
  83. return;
  84. }
  85. if (interrupt_type & KEYSCAN_IT_VALID) {
  86. interrupt_pending_bit |= KEYSCAN_CON0_KEYVALID_INT_EN;
  87. }
  88. if (interrupt_type & KEYSCAN_IT_INVALID) {
  89. interrupt_pending_bit |= KEYSCAN_CON0_KEYINVALID_INT_EN;
  90. }
  91. if (state != DISABLE) {
  92. KEYSCAN->con0 |= interrupt_pending_bit;
  93. sys_irq_init(IRQn_KEY_SCAN, pr, isr);
  94. } else {
  95. KEYSCAN->con0 &= ~interrupt_pending_bit;
  96. }
  97. }
  98. /**
  99. * @brief Get interrupt pendding function.
  100. * @param interrupt_type: specifies the interrupt to get.
  101. * this parameter can be one of the following values:
  102. * @arg KEYSCAN_IT_VALID: Any key has pressed interrupt.
  103. * @arg KEYSCAN_IT_INVALID: All key release interrupt.
  104. * @retval The state of interrupt_type (SET or RESET).
  105. */
  106. AT(.com_periph.keyscan.flag)
  107. FLAG_STATE keyscan_get_flag(KEYSCAN_IT_TYPEDEF interrupt_type)
  108. {
  109. uint32_t interrupt_pending_bit = 0;
  110. if (interrupt_type & KEYSCAN_IT_VALID) {
  111. interrupt_pending_bit |= KEYSCAN_CLR_KEYVALID_PND_CLR;
  112. }
  113. if (interrupt_type & KEYSCAN_IT_INVALID) {
  114. interrupt_pending_bit |= KEYSCAN_CLR_KEYINVALID_PND_CLR;
  115. }
  116. if (KEYSCAN->con0 & interrupt_pending_bit) {
  117. return SET;
  118. } else {
  119. return RESET;
  120. }
  121. }
  122. /**
  123. * @brief Clear interrupt pending function.
  124. * @param interrupt_type: specifies the interrupt to clear.
  125. * this parameter can be one of the following values:
  126. * @arg KEYSCAN_IT_VALID: Any key has pressed interrupt.
  127. * @arg KEYSCAN_IT_INVALID: All key release interrupt.
  128. * @retval None.
  129. */
  130. AT(.com_periph.keyscan.flag)
  131. void keyscan_clear_flag(KEYSCAN_IT_TYPEDEF interrupt_type)
  132. {
  133. uint32_t interrupt_pending_bit = 0;
  134. if (interrupt_type & KEYSCAN_IT_VALID) {
  135. interrupt_pending_bit |= KEYSCAN_CON0_KEYVALID_PND;
  136. }
  137. if (interrupt_type & KEYSCAN_IT_INVALID) {
  138. interrupt_pending_bit |= KEYSCAN_CON0_KEYINVALID_PND;
  139. }
  140. KEYSCAN->clr |= interrupt_pending_bit;
  141. }
  142. /**
  143. * @brief Get Group A Key ID that be triggered.
  144. * @retval Key ID. All Key ID please see enumeration "KEYSCAN_GROUP_A_KEY_ID_TYPEDEF".
  145. */
  146. AT(.com_periph.keyscan.query)
  147. uint32_t keyscan_get_groupA_key_id(void)
  148. {
  149. uint8_t idx;
  150. uint8_t *p;
  151. uint32_t info_reg[2];
  152. uint32_t key_val = 0;
  153. uint32_t key_val_img = 0;
  154. info_reg[0] = KEYSCAN->info0;
  155. info_reg[1] = KEYSCAN->info1;
  156. p = (u8 *)info_reg;
  157. key_val |= ((info_reg[0] & 0xfe) >> 1) * KA1;
  158. key_val |= ((info_reg[0] & 0xfc00) >> 10) * KB2;
  159. key_val |= ((info_reg[0] & 0xf80000) >> 19) * KC3;
  160. key_val |= ((info_reg[0] & 0xf0000000) >> 28) * KD4;
  161. key_val |= ((info_reg[1] & 0xe0) >> 5) * KE5;
  162. key_val |= ((info_reg[1] & 0xc000) >> 14) * KF6;
  163. key_val |= ((info_reg[1] & 0x800000) >> 23) * KG7;
  164. if (KEYSCAN->con0 & KEYSCAN_CON0_KEYSCAN_MODE) {
  165. //Resolve matrix keystroke conflicts
  166. for (u8 i = 0; i < 8; i++) {
  167. idx = i - 1;
  168. for (u8 j = 0; j < i; j++) {
  169. key_val_img |= (p[i] & (0x01 << j))? (0x01 << idx): 0;
  170. idx = idx + 6 - j;
  171. }
  172. }
  173. } else {
  174. key_val_img = 0xffffffff;
  175. }
  176. return key_val & key_val_img;
  177. }
  178. /**
  179. * @brief Get Group B Key ID that be triggered.
  180. * @retval Key ID. All Key ID please see enumeration "KEYSCAN_GROUP_B_KEY_ID_TYPEDEF".
  181. */
  182. AT(.com_periph.keyscan.query)
  183. uint32_t keyscan_get_groupB_key_id(void)
  184. {
  185. uint32_t info2_reg;
  186. uint32_t key_val = 0;
  187. info2_reg = KEYSCAN->info2;
  188. key_val |= (info2_reg & 0xff) * KJ0;
  189. key_val |= ((info2_reg & 0xff00) >> 8) * KI0;
  190. return key_val;
  191. }
  192. /**
  193. * @brief Get low power mode configuration.
  194. * @retval 1:low power mode en, 0:low power mode dis.
  195. */
  196. AT(.com_periph.keyscan.lowpwr_mode)
  197. bool keyscan_is_lowpwr_mode(void)
  198. {
  199. bool mode = (KEYSCAN->con0 & KEYSCAN_CON0_KEYSCAN_LP) >> 2;
  200. return mode;
  201. }