kscan.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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.c
  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. #include <string.h>
  40. #include "ap_cp.h"
  41. #include "OSAL.h"
  42. #include "kscan.h"
  43. #include "pwrmgr.h"
  44. #include "error.h"
  45. #include "gpio.h"
  46. #include "common.h"
  47. #include "uart.h"
  48. #include "log.h"
  49. typedef struct {
  50. bool enable;
  51. kscan_Cfg_t cfg;
  52. uint16_t key_state[18];
  53. uint8_t pin_state[NUM_KEY_ROWS];
  54. uint8_t kscan_task_id;
  55. uint16_t timeout_event;
  56. } kscan_Ctx_t;
  57. static kscan_Ctx_t m_kscanCtx;
  58. static kscan_Key_t m_keys[MAX_KEY_NUM];
  59. static uint8_t reScan_flag=0;
  60. //PRIVATE FUNCTIONS
  61. static void kscan_hw_config(void);
  62. static void hal_kscan_config_row(KSCAN_ROWS_e row);
  63. static void hal_kscan_config_col(KSCAN_COLS_e col);
  64. static void kscan_sleep_handler(void);
  65. static void kscan_wakeup_handler(void);
  66. static void get_key_matrix(uint16_t* key_matrix);
  67. static void rmv_ghost_key(uint16_t* key_matrix);
  68. static kscan_Evt_t kscan_compare_key(uint16_t* key_pre, uint16_t* key_nxt);
  69. static void hal_kscan_clear_config(void);
  70. #define TIMEOUT_DELTA 10
  71. /**************************************************************************************
  72. * @fn hal_kscan_init
  73. *
  74. * @brief This function process for key scan initial
  75. *
  76. * input parameters
  77. *
  78. * @param None.
  79. *
  80. * output parameters
  81. *
  82. * @param None.
  83. *
  84. * @return None.
  85. **************************************************************************************/
  86. int hal_kscan_init(kscan_Cfg_t cfg, uint8 task_id, uint16 event){
  87. if(m_kscanCtx.enable)
  88. return PPlus_ERR_INVALID_STATE;
  89. m_kscanCtx.cfg = cfg;
  90. m_kscanCtx.kscan_task_id = task_id;
  91. m_kscanCtx.timeout_event = event;
  92. m_kscanCtx.enable = TRUE;
  93. kscan_hw_config();
  94. hal_pwrmgr_register(MOD_KSCAN, kscan_sleep_handler, kscan_wakeup_handler);
  95. return PPlus_SUCCESS;
  96. }
  97. /**************************************************************************************
  98. * @fn hal_kscan_clear_config
  99. *
  100. * @brief This function process for key scan clear config
  101. *
  102. * input parameters
  103. *
  104. * @param None.
  105. *
  106. * output parameters
  107. *
  108. * @param None.
  109. *
  110. * @return None.
  111. **************************************************************************************/
  112. void hal_kscan_clear_config(){
  113. *(volatile unsigned int *) 0x4000384C &= 0x0;
  114. *(volatile unsigned int *) 0x40003850 &= 0x0;
  115. subWriteReg(0x400240F0, 15, 0, 0);
  116. subWriteReg(0x400240C0, 19, 2, 0);
  117. }
  118. void __attribute__((used)) hal_KSCAN_IRQHandler()
  119. {
  120. // LOG("key irq\n\r");
  121. if(reScan_flag==1)
  122. reScan_flag=0;
  123. osal_stop_timerEx(m_kscanCtx.kscan_task_id, m_kscanCtx.timeout_event);
  124. uint16_t key_nxt[18];
  125. get_key_matrix(key_nxt);
  126. if(m_kscanCtx.cfg.ghost_key_state == IGNORE_GHOST_KEY){
  127. rmv_ghost_key(key_nxt);
  128. }
  129. if(m_kscanCtx.cfg.evt_handler){
  130. kscan_Evt_t evt = kscan_compare_key(m_kscanCtx.key_state, key_nxt);
  131. if(evt.num>0)
  132. m_kscanCtx.cfg.evt_handler(&evt);
  133. }
  134. memcpy(m_kscanCtx.key_state, key_nxt, sizeof(uint16_t)*18);
  135. CLEAR_KEY_PRESSED;
  136. CLEAR_INTERUPT;
  137. osal_start_timerEx(m_kscanCtx.kscan_task_id, m_kscanCtx.timeout_event, (m_kscanCtx.cfg.interval+TIMEOUT_DELTA));
  138. }
  139. void hal_kscan_timeout_handler()
  140. {
  141. if(reScan_flag==0)
  142. {
  143. // LOG("kscan_reScan\n\r");
  144. hal_kscan_clear_config();
  145. reScan_flag=1;
  146. kscan_hw_config();
  147. osal_start_timerEx(m_kscanCtx.kscan_task_id, m_kscanCtx.timeout_event, (m_kscanCtx.cfg.interval+TIMEOUT_DELTA));
  148. }
  149. else if(reScan_flag==1)
  150. {
  151. //LOG("kscan_timeout_handler\n\r");
  152. osal_stop_timerEx(m_kscanCtx.kscan_task_id, m_kscanCtx.timeout_event);
  153. uint16_t key_nxt[18];
  154. memset(&key_nxt[0],0,sizeof(uint16_t)*18); //all register must be 0.teedy add 2019/01/23
  155. //get_key_matrix(key_nxt); //no need to read the register,because keyScan didn't update the register .teedy add 2019/01/23
  156. if(m_kscanCtx.cfg.ghost_key_state == IGNORE_GHOST_KEY){
  157. rmv_ghost_key(key_nxt);
  158. }
  159. if(m_kscanCtx.cfg.evt_handler){
  160. kscan_Evt_t evt = kscan_compare_key(m_kscanCtx.key_state, key_nxt);
  161. m_kscanCtx.cfg.evt_handler(&evt);
  162. }
  163. memcpy(m_kscanCtx.key_state, key_nxt, sizeof(uint16_t)*18);
  164. reScan_flag=0;
  165. hal_pwrmgr_unlock(MOD_KSCAN);
  166. }
  167. }
  168. static void kscan_hw_config(void)
  169. {
  170. hal_kscan_clear_config();
  171. kscan_Cfg_t* cfg = &(m_kscanCtx.cfg);
  172. for(uint8_t i=0;i<NUM_KEY_ROWS;i++)
  173. hal_kscan_config_row(cfg->key_rows[i]);
  174. for(uint8_t i=0;i<NUM_KEY_COLS;i++)
  175. hal_kscan_config_col(cfg->key_cols[i]);
  176. SET_MULTI_KEY_STATE(NOT_IGNORE_MULTI_KEY);
  177. SET_POLARITY(SENCE_HIGH);
  178. SET_INTERVAL(cfg->interval);
  179. ENABLE_AUTO_SCAN;
  180. NVIC_SetPriority((IRQn_Type)KSCAN_IRQ, IRQ_PRIO_HAL);
  181. KSCAN_IRQ_ENABLE;
  182. ENABLE_KSCAN;
  183. }
  184. /**************************************************************************************
  185. * @fn hal_kscan_config_row
  186. *
  187. * @brief This function process for setting key row pin
  188. *
  189. * input parameters
  190. *
  191. * @param KSCAN_ROWS_e row
  192. *
  193. * output parameters
  194. *
  195. * @param None.
  196. *
  197. * @return None.
  198. **************************************************************************************/
  199. static void hal_kscan_config_row(KSCAN_ROWS_e row){
  200. GPIO_Pin_e row_pin = (GPIO_Pin_e)KSCAN_ROW_GPIO[row];
  201. hal_gpio_fmux(row_pin, Bit_DISABLE);
  202. hal_gpio_pull_set(row_pin, PULL_DOWN);
  203. EN_MUX_KSCAN_ROW(KSCAN_ROW_MK[row]);
  204. CONFIG_KEY_ROW(KSCAN_ROW_MK[row]);
  205. }
  206. /**************************************************************************************
  207. * @fn hal_kscan_config_col
  208. *
  209. * @brief This function process for setting key scan col pin
  210. *
  211. * input parameters
  212. *
  213. * @param KSCAN_COLS_e col
  214. *
  215. * output parameters
  216. *
  217. * @param None.
  218. *
  219. * @return None.
  220. **************************************************************************************/
  221. static void hal_kscan_config_col(KSCAN_COLS_e col){
  222. GPIO_Pin_e col_pin = (GPIO_Pin_e)KSCAN_COL_GPIO[col];
  223. if(col == KEY_COL_P16 || col == KEY_COL_P17){
  224. hal_gpio_cfg_analog_io(col_pin, Bit_DISABLE);
  225. }
  226. hal_gpio_fmux(col_pin, Bit_DISABLE);
  227. hal_gpio_pull_set(col_pin, PULL_DOWN);
  228. EN_MUX_KSCAN_COL(KSCAN_COL_MK[col]);
  229. CONFIG_KEY_COL(KSCAN_COL_MK[col]);
  230. }
  231. static void kscan_sleep_handler(void)
  232. {
  233. hal_kscan_clear_config();
  234. IO_Wakeup_Pol_e pol;
  235. for(uint8_t i=0;i<NUM_KEY_COLS;i++){
  236. GPIO_Pin_e col_pin = (GPIO_Pin_e)KSCAN_COL_GPIO[m_kscanCtx.cfg.key_cols[i]];
  237. DIS_MUX_KSCAN_COL(KSCAN_COL_MK[m_kscanCtx.cfg.key_cols[i]]);
  238. hal_gpio_pin_init(col_pin, IE);
  239. hal_gpio_pull_set(col_pin, STRONG_PULL_UP);
  240. }
  241. for(uint8_t i=0;i<NUM_KEY_ROWS;i++){
  242. GPIO_Pin_e row_pin = (GPIO_Pin_e)KSCAN_ROW_GPIO[m_kscanCtx.cfg.key_rows[i]];
  243. DIS_MUX_KSCAN_ROW(KSCAN_ROW_MK[m_kscanCtx.cfg.key_rows[i]]);
  244. hal_gpio_pull_set(row_pin, PULL_DOWN);
  245. hal_gpio_pin_init(row_pin, IE);
  246. pol = hal_gpio_read(row_pin) ? NEGEDGE:POSEDGE;
  247. hal_gpio_wakeup_set(row_pin, pol);
  248. m_kscanCtx.pin_state[i] = pol;
  249. }
  250. }
  251. static void kscan_wakeup_handler(void)
  252. {
  253. for(uint8_t i=0;i<NUM_KEY_COLS;i++){
  254. GPIO_Pin_e col_pin = (GPIO_Pin_e)KSCAN_COL_GPIO[m_kscanCtx.cfg.key_cols[i]];
  255. hal_gpio_pin_init(col_pin, IE);
  256. hal_gpio_pull_set(col_pin, STRONG_PULL_UP);
  257. }
  258. for(uint8_t i=0;i<NUM_KEY_ROWS;i++){
  259. GPIO_Pin_e row_pin = (GPIO_Pin_e)KSCAN_ROW_GPIO[m_kscanCtx.cfg.key_rows[i]];
  260. hal_gpio_pin_init(row_pin, IE);
  261. hal_gpio_pull_set(row_pin, PULL_DOWN);//teddy add 20190122
  262. }
  263. for(uint8_t i=0;i<NUM_KEY_ROWS;i++){
  264. GPIO_Pin_e row_pin = (GPIO_Pin_e)KSCAN_ROW_GPIO[m_kscanCtx.cfg.key_rows[i]];
  265. hal_gpio_pin_init(row_pin, IE);
  266. IO_Wakeup_Pol_e pol = hal_gpio_read(row_pin) ? POSEDGE:NEGEDGE;
  267. if(pol == m_kscanCtx.pin_state[i]){
  268. break;
  269. }
  270. else if(i == NUM_KEY_ROWS-1){
  271. return;
  272. }
  273. }
  274. hal_pwrmgr_lock(MOD_KSCAN);
  275. for(uint8_t i=0;i<NUM_KEY_COLS;i++) //teddy add 20190122
  276. {
  277. GPIO_Pin_e col_pin = (GPIO_Pin_e)KSCAN_COL_GPIO[m_kscanCtx.cfg.key_cols[i]];
  278. hal_gpio_pin_init(col_pin, IE);
  279. hal_gpio_pull_set(col_pin, PULL_DOWN);
  280. }
  281. kscan_hw_config();
  282. reScan_flag=0;
  283. osal_start_timerEx(m_kscanCtx.kscan_task_id, m_kscanCtx.timeout_event, (m_kscanCtx.cfg.interval + TIMEOUT_DELTA));
  284. }
  285. /**************************************************************************************
  286. * @fn get_key_matrix
  287. *
  288. * @brief This function process for reading key row and col
  289. *
  290. * input parameters
  291. *
  292. * @param uint16_t* key_matrix
  293. *
  294. * output parameters
  295. *
  296. * @param None.
  297. *
  298. * @return None.
  299. **************************************************************************************/
  300. static void get_key_matrix(uint16_t* key_matrix){
  301. for(uint8_t i=0; i<9; i++){
  302. uint16_t low = (read_reg(MULTI_KEY_READ_ADDR + 4*i) & 0x0000FFFF);
  303. uint16_t high = (read_reg(MULTI_KEY_READ_ADDR + 4*i) & 0xFFFF0000) >> 16;
  304. key_matrix[i*2] = low;
  305. key_matrix[i*2+1] = high;
  306. }
  307. }
  308. /**************************************************************************************
  309. * @fn rmv_ghost_key
  310. *
  311. * @brief This function process for removing ghost key
  312. *
  313. * input parameters
  314. *
  315. * @param uint16_t* key_matrix
  316. *
  317. * output parameters
  318. *
  319. * @param None.
  320. *
  321. * @return None.
  322. **************************************************************************************/
  323. static void rmv_ghost_key(uint16_t* key_matrix){
  324. uint16_t mix_final = 0;
  325. for (uint8_t i=0; i<18; ++i){
  326. for (uint8_t j=i+1; j<18; ++j){
  327. uint16_t mix = key_matrix[i] & key_matrix[j];
  328. uint8_t bit_is_pow2 = (mix&(mix-1)) == 0;
  329. if (mix && !bit_is_pow2){
  330. mix_final |= mix;
  331. }
  332. }
  333. key_matrix[i] &= ~mix_final;
  334. }
  335. }
  336. static kscan_Evt_t kscan_compare_key(uint16_t* key_pre, uint16_t* key_nxt)
  337. {
  338. uint16_t multi_key_num = 0;
  339. for(uint8_t i=0; i<18; i++){
  340. uint16_t chg_key = key_pre[i] ^ key_nxt[i];
  341. uint16_t key_sta = chg_key & key_nxt[i];
  342. if(chg_key != 0){
  343. for(uint8_t j=0; j<16; j++){
  344. if((chg_key & BIT(j)) != 0){
  345. kscan_Key_t key_param;
  346. key_param.row = j;
  347. key_param.col = i;
  348. key_param.type = (key_sta & BIT(j)) ? KEY_PRESSED:KEY_RELEASED;
  349. m_keys[multi_key_num] = key_param;
  350. multi_key_num++;
  351. }
  352. if(multi_key_num == MAX_KEY_NUM){
  353. break;
  354. }
  355. }
  356. }
  357. }
  358. kscan_Evt_t evt;
  359. evt.keys = m_keys;
  360. evt.num = multi_key_num;
  361. return evt;
  362. }