kscan.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. /*******************************************************************************
  29. * @file kscan.h
  30. * @brief Contains all functions support for key scan driver
  31. * @version 0.0
  32. * @date 13. Nov. 2017
  33. * @author Ding
  34. *
  35. * Copyright(C) 2016, PhyPlus Semiconductor
  36. * All rights reserved.
  37. *
  38. *******************************************************************************/
  39. #ifndef __KSCAN__H__
  40. #define __KSCAN__H__
  41. #include "types.h"
  42. #include "gpio.h"
  43. #define MULTI_KEY_READ_ADDR 0x400240CCUL
  44. #define KSCAN_IRQ_ENABLE do{\
  45. *(volatile unsigned int *) 0x400240C0 |= BIT(1);\
  46. *(volatile unsigned int *) 0xe000e100 |= BIT(5);\
  47. }while(0)
  48. #define KSCAN_IRQ_DISABLE do{\
  49. *(volatile unsigned int *) 0x400240C0 &= ~BIT(1);\
  50. *(volatile unsigned int *) 0xe000e100 &= ~BIT(5);\
  51. }while(0)
  52. #define ENABLE_KSCAN *(volatile unsigned int *) 0x400240C0 |= BIT(0)
  53. #define DISABLE_KSCAN *(volatile unsigned int *) 0x400240C0 &= ~BIT(0)
  54. #define CONFIG_KEY_ROW(v) *(volatile unsigned int *) 0x400240F0 |= BIT(v)
  55. #define CONFIG_KEY_COL(v) *(volatile unsigned int *) 0x400240C0 |= (BIT(v) << 2)
  56. #define ENABLE_AUTO_SCAN *(volatile unsigned int *) 0x400240C0 |= BIT(21)
  57. #define DISABLE_AUTO_SCAN *(volatile unsigned int *) 0x400240C0 &= ~BIT(21)
  58. #define CLEAR_INTERUPT *(volatile unsigned int *) 0x400240C4 |= BIT(0)
  59. #define CLEAR_KEY_PRESSED *(volatile unsigned int *) 0x400240C4 |= BIT(17)
  60. #define EN_MUX_KSCAN_ROW(v) *(volatile unsigned int *) 0x4000384C |= BIT(v)
  61. #define EN_MUX_KSCAN_COL(v) *(volatile unsigned int *) 0x40003850 |= BIT(v)
  62. #define DIS_MUX_KSCAN_ROW(v) *(volatile unsigned int *) 0x4000384C &= ~BIT(v)
  63. #define DIS_MUX_KSCAN_COL(v) *(volatile unsigned int *) 0x40003850 &= ~BIT(v)
  64. #define SET_MULTI_KEY_STATE(v) subWriteReg(0x400240C0,20,20,v)
  65. #define SET_POLARITY(v) subWriteReg(0x400240C0,23,23,v)
  66. #define SET_INTERVAL(v) subWriteReg(0x400240C0,31,24,v)
  67. #define IS_KEY_PRESSED (read_reg(0x400240C4) & 0x00020000) >> 17
  68. #define KEY_PRESS_STATE (read_reg(0x400240C8) & 0x00000C00) >> 10
  69. #define ROW_OF_ONE_KEY (read_reg(0x400240C8) & 0x000003E0) >> 5
  70. #define COL_OF_ONE_KEY (read_reg(0x400240C8) & 0x0000001F)
  71. #define POWER_OFF_32K_XOSC do{\
  72. *(volatile unsigned int *) 0x4000F01C |= BIT(6);\
  73. *(volatile unsigned int *) 0x4000F01C &= ~BIT(9);\
  74. }while(0)
  75. #define HW_CRTL_MODE do{\
  76. *(volatile unsigned int *) 0x4000F01C &= ~BIT(6);\
  77. }while(0)
  78. #define IS_ENABLE_KSCAN (read_reg(0x400240C0) & BIT(0))
  79. #define IS_ENABEL_KSCANIRQ ((read_reg(0x400240C0) & BIT(1))>>1)
  80. #define IS_ENABEL_AUTOKSCAN ((read_reg(0x400240C0) & BIT(21))>>21)
  81. const static uint8_t KSCAN_ROW_GPIO[16] = {0,2,5,7,10,12,15,18,19,22,23,25,27,29,32,34};
  82. const static uint8_t KSCAN_ROW_MK[16] = {0,1,10,11,4,12,2,5,13,14,6,3,9,15,7,8};
  83. const static uint8_t KSCAN_COL_GPIO[18] = {1,3,4,6,9,11,13,14,16,17,20,21,24,26,28,30,31,33};
  84. const static uint8_t KSCAN_COL_MK[18] = {0,1,9,10,4,11,12,2,16,17,5,13,3,14,8,15,7,6};
  85. #define NUM_KEY_ROWS 4
  86. #define NUM_KEY_COLS 4
  87. #define MAX_KEY_NUM 10
  88. /*************************************************************
  89. * @brief enum variable used for setting rows
  90. *
  91. */
  92. typedef enum{
  93. KEY_ROW_P00 = 0,
  94. KEY_ROW_P02 = 1,
  95. KEY_ROW_P05 = 2,
  96. KEY_ROW_P07 = 3,
  97. KEY_ROW_P10 = 4,
  98. KEY_ROW_P12 = 5,
  99. KEY_ROW_P15 = 6,
  100. KEY_ROW_P18 = 7,
  101. KEY_ROW_P19 = 8,
  102. KEY_ROW_P22 = 9,
  103. KEY_ROW_P23 = 10,
  104. KEY_ROW_P25 = 11,
  105. KEY_ROW_P27 = 12,
  106. KEY_ROW_P29 = 13,
  107. KEY_ROW_P32 = 14,
  108. KEY_ROW_P34 = 15
  109. }KSCAN_ROWS_e;
  110. /*************************************************************
  111. * @brief enum variable used for setting cols
  112. *
  113. */
  114. typedef enum{
  115. KEY_COL_P01 = 0,
  116. KEY_COL_P03 = 1,
  117. KEY_COL_P04 = 2,
  118. KEY_COL_P06 = 3,
  119. KEY_COL_P09 = 4,
  120. KEY_COL_P11 = 5,
  121. KEY_COL_P13 = 6,
  122. KEY_COL_P14 = 7,
  123. KEY_COL_P16 = 8,
  124. KEY_COL_P17 = 9,
  125. KEY_COL_P20 = 10,
  126. KEY_COL_P21 = 11,
  127. KEY_COL_P24 = 12,
  128. KEY_COL_P26 = 13,
  129. KEY_COL_P28 = 14,
  130. KEY_COL_P30 = 15,
  131. KEY_COL_P31 = 16,
  132. KEY_COL_P33 = 17
  133. }KSCAN_COLS_e;
  134. /*************************************************************
  135. * @brief enum variable used for setting multiple key press
  136. *
  137. */
  138. typedef enum{
  139. NOT_IGNORE_MULTI_KEY = 0,
  140. IGNORE_MULTI_KEY = 1
  141. }KSCAN_MULTI_KEY_STATE_e;
  142. /*************************************************************
  143. * @brief enum variable used for setting whether ignore ghost key
  144. *
  145. */
  146. typedef enum{
  147. NOT_IGNORE_GHOST_KEY = 0,
  148. IGNORE_GHOST_KEY = 1
  149. }KSCAN_GHOST_KEY_STATE_e;
  150. /*************************************************************
  151. * @brief enum variable used for setting key press sense type
  152. *
  153. */
  154. typedef enum{
  155. SENCE_HIGH = 0,
  156. SENCE_LOW = 1
  157. }KSCAN_POLARITY_e;
  158. /*************************************************************
  159. * @brief enum variable used for setting key press sense type
  160. *
  161. */
  162. typedef enum{
  163. NO_KEY_PRESS = 0x00,
  164. ONE_KEY_PRESS = 0x01,
  165. MULTI_KEY_PRESS = 0x02
  166. }KSCAN_KEY_PRESS_STATE_e;
  167. typedef enum{
  168. KEY_RELEASED = 0,
  169. KEY_PRESSED,
  170. } kscan_Evt_Type_t;
  171. typedef struct {
  172. uint8_t row;
  173. uint8_t col;
  174. kscan_Evt_Type_t type;
  175. } kscan_Key_t;
  176. typedef struct kscan_Evt_t_{
  177. uint8_t num;
  178. kscan_Key_t* keys;
  179. } kscan_Evt_t;
  180. typedef void (*kscan_Hdl_t)(kscan_Evt_t* pev);
  181. typedef struct {
  182. KSCAN_GHOST_KEY_STATE_e ghost_key_state;
  183. KSCAN_ROWS_e* key_rows;
  184. KSCAN_COLS_e* key_cols;
  185. kscan_Hdl_t evt_handler;
  186. uint8_t interval;
  187. } kscan_Cfg_t;
  188. //PUBLIC FUNCTIONS
  189. int hal_kscan_init(kscan_Cfg_t cfg, uint8 task_id, uint16 event);
  190. void hal_kscan_timeout_handler(void);
  191. void __attribute__((weak)) hal_KSCAN_IRQHandler(void);
  192. #endif