qdec.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 qdec.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 "qdec.h"
  43. #include "gpio.h"
  44. #include "common.h"
  45. #include "uart.h"
  46. #include "log.h"
  47. #include "pwrmgr.h"
  48. #include "error.h"
  49. #include "clock.h"
  50. qdec_Ctx_t m_qdecCtx;
  51. void __attribute__((used)) hal_QDEC_IRQHandler()
  52. {
  53. hal_gpio_pin_init(P20, OEN);
  54. hal_gpio_write(P20,1);
  55. osal_stop_timerEx(m_qdecCtx.qdec_task_id, m_qdecCtx.timeout_event);
  56. WaitMs(1);
  57. int32_t delta = GET_CNT_QUAN(m_qdecCtx.cfg.qdec_chn);
  58. m_qdecCtx.count += delta;
  59. if(m_qdecCtx.cfg.evt_handler){
  60. qdec_Evt_t evt;
  61. evt.count = m_qdecCtx.count;
  62. m_qdecCtx.cfg.evt_handler(&evt);
  63. }
  64. CLR_INT_QUAN(m_qdecCtx.cfg.qdec_chn);
  65. hal_pwrmgr_unlock(MOD_QDEC);
  66. hal_gpio_pin_init(P20, OEN);
  67. hal_gpio_write(P20,0);
  68. }
  69. void hal_qdec_timeout_handler()
  70. {
  71. osal_stop_timerEx(m_qdecCtx.qdec_task_id, m_qdecCtx.timeout_event);
  72. hal_pwrmgr_unlock(MOD_QDEC);
  73. }
  74. /**************************************************************************************
  75. * @fn hal_qdec_set_cha
  76. *
  77. * @brief This function process for qdec initial
  78. *
  79. * input parameters
  80. *
  81. * @param None.
  82. *
  83. * output parameters
  84. *
  85. * @param None.
  86. *
  87. * @return None.
  88. **************************************************************************************/
  89. static void hal_qdec_set_cha(QDEC_CHN_e qdecCHN,GPIO_Pin_e pin){
  90. hal_gpio_pull_set(pin, PULL_DOWN);
  91. hal_gpio_fmux_set(pin, (Fmux_Type_e)(CHAX + (qdecCHN*3)));
  92. }
  93. /**************************************************************************************
  94. * @fn hal_qdec_set_chb
  95. *
  96. * @brief This function process for qdec initial
  97. *
  98. * input parameters
  99. *
  100. * @param None.
  101. *
  102. * output parameters
  103. *
  104. * @param None.
  105. *
  106. * @return None.
  107. **************************************************************************************/
  108. static void hal_qdec_set_chb(QDEC_CHN_e qdecCHN,GPIO_Pin_e pin){
  109. hal_gpio_pull_set(pin, PULL_DOWN);
  110. hal_gpio_fmux_set(pin, (Fmux_Type_e)(CHBX + (qdecCHN*3)));
  111. }
  112. /**************************************************************************************
  113. * @fn hal_qdec_set_chi
  114. *
  115. * @brief This function process for qdec initial
  116. *
  117. * input parameters
  118. *
  119. * @param None.
  120. *
  121. * output parameters
  122. *
  123. * @param None.
  124. *
  125. * @return None.
  126. **************************************************************************************/
  127. static void hal_qdec_set_chi(QDEC_CHN_e qdecCHN,GPIO_Pin_e pin){
  128. hal_gpio_pull_set(pin, PULL_DOWN);
  129. hal_gpio_fmux_set(pin, (Fmux_Type_e)(CHIX + (qdecCHN*3)));
  130. }
  131. /**************************************************************************************
  132. * @fn hal_qdec_init
  133. *
  134. * @brief This function process for qdec initial
  135. *
  136. * input parameters
  137. *
  138. * @param qdec_Cfg_t cfg
  139. *
  140. * output parameters
  141. *
  142. * @param None.
  143. *
  144. * @return None.
  145. **************************************************************************************/
  146. int hal_qdec_init(qdec_Cfg_t cfg, uint8 task_id, uint16 event){
  147. if(m_qdecCtx.enable)
  148. return PPlus_ERR_INVALID_STATE;
  149. uint8_t pins[3] = {cfg.cha_pin, cfg.chb_pin, cfg.chi_pin};
  150. memcpy(m_qdecCtx.pin_arr, pins, sizeof(uint8_t)*3);
  151. m_qdecCtx.cfg = cfg;
  152. m_qdecCtx.qdec_task_id = task_id;
  153. m_qdecCtx.timeout_event = event;
  154. m_qdecCtx.enable = TRUE;
  155. qdec_hw_config();
  156. hal_pwrmgr_register(MOD_QDEC, qdec_sleep_handler, qdec_wakeup_handler);
  157. return PPlus_SUCCESS;
  158. }
  159. void qdec_hw_config()
  160. {
  161. qdec_Cfg_t* cfg = &(m_qdecCtx.cfg);
  162. clk_gate_enable(MOD_QDEC);
  163. hal_qdec_set_cha(cfg->qdec_chn, cfg->cha_pin);
  164. hal_qdec_set_chb(cfg->qdec_chn, cfg->chb_pin);
  165. DIS_INT_INCN(cfg->qdec_chn);
  166. DIS_INT_QUAN(cfg->qdec_chn);
  167. DIS_INT_02F_QUAN(cfg->qdec_chn);
  168. DIS_INT_F20_QUAN(cfg->qdec_chn);
  169. SET_MODE_QUAN(cfg->qdec_chn, cfg->quaMode);
  170. hal_qdec_set_qua_irq(cfg->qdec_chn, cfg->intMode);
  171. if(cfg->use_inc){
  172. hal_qdec_set_chi(cfg->qdec_chn, cfg->chi_pin);
  173. if(cfg->use_inc_irq){
  174. hal_qdec_set_inc_irq(cfg->qdec_chn, cfg->incMode, cfg->intMode);
  175. }
  176. }
  177. QDEC_IRQ_ENABLE;
  178. ENABLE_CHN(cfg->qdec_chn);
  179. }
  180. static void qdec_sleep_handler(void)
  181. {
  182. uint8_t pin_num;
  183. pin_num = m_qdecCtx.cfg.use_inc ? 3:2;
  184. for(uint8_t i=0;i<pin_num;i++){
  185. IO_Wakeup_Pol_e pol;
  186. GPIO_Pin_e pin = m_qdecCtx.pin_arr[i];
  187. hal_gpio_pin_init(pin, IE);
  188. pol = hal_gpio_read(pin) ? NEGEDGE:POSEDGE;
  189. hal_gpio_wakeup_set(pin, pol);
  190. m_qdecCtx.pin_state[i] = pol;
  191. }
  192. DISABLE_CHN(m_qdecCtx.cfg.qdec_chn);
  193. hal_gpio_pin_init(P23, OEN);
  194. hal_gpio_write(P23,0);
  195. }
  196. static void qdec_wakeup_handler(void)
  197. {
  198. hal_gpio_pin_init(P23, OEN);
  199. hal_gpio_write(P23,1);
  200. uint8_t pin_num;
  201. pin_num = m_qdecCtx.cfg.use_inc ? 3:2;
  202. IO_Wakeup_Pol_e pol;
  203. for(uint8_t i=0;i<pin_num;i++){
  204. hal_gpio_pin_init(m_qdecCtx.pin_arr[i], IE);
  205. pol = hal_gpio_read(m_qdecCtx.pin_arr[i]) ? POSEDGE:NEGEDGE;
  206. if(pol == m_qdecCtx.pin_state[i]){
  207. break;
  208. }
  209. else if(i == pin_num-1){
  210. return;
  211. }
  212. }
  213. int32_t delta = GET_CNT_QUAN(m_qdecCtx.cfg.qdec_chn);
  214. hal_gpio_pin_init(P31, OEN);
  215. hal_gpio_write(P31,0);
  216. hal_pwrmgr_lock(MOD_QDEC);
  217. qdec_hw_config();
  218. hal_gpio_pin_init(P31, OEN);
  219. hal_gpio_write(P31,1);
  220. osal_start_timerEx(m_qdecCtx.qdec_task_id, m_qdecCtx.timeout_event, 150);
  221. }
  222. /**************************************************************************************
  223. * @fn hal_qdec_set_qua_irq
  224. *
  225. * @brief This function process for setting qdecode counter interupt mode
  226. *
  227. * input parameters
  228. *
  229. * @param QDEC_CHN_e chn
  230. * @param QDEC_INT_MODE_e intMode
  231. *
  232. * output parameters
  233. *
  234. * @param None.
  235. *
  236. * @return None.
  237. **************************************************************************************/
  238. static void hal_qdec_set_qua_irq(QDEC_CHN_e chn, QDEC_INT_MODE_e intMode){
  239. SET_INT_MODE_QUAN(chn, intMode);
  240. EN_INT_QUAN(chn);
  241. }
  242. /**************************************************************************************
  243. * @fn hal_qdec_set_inc_irq
  244. *
  245. * @brief This function process for setting qdecode index count mode and interupt mode
  246. *
  247. * input parameters
  248. *
  249. * @param QDEC_CHN_e chn
  250. * @param QDEC_INT_MODE_e intMode
  251. * @param QDEC_INC_MODE_e incMode
  252. *
  253. * output parameters
  254. *
  255. * @param None.
  256. *
  257. * @return None.
  258. **************************************************************************************/
  259. static void hal_qdec_set_inc_irq(QDEC_CHN_e chn, QDEC_INC_MODE_e incMode, QDEC_INT_MODE_e intMode){
  260. SET_MODE_INCN(chn, incMode);
  261. SET_INT_MODE_INCN(chn, intMode);
  262. EN_INT_INCN(chn);
  263. }