qdec.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /**
  2. * @file qdec.c
  3. * @author chipsea
  4. * @brief
  5. * @version 0.1
  6. * @date 2020-11-30
  7. * @copyright Copyright (c) 2020, CHIPSEA Co., Ltd.
  8. * @note
  9. */
  10. /*******************************************************************************
  11. * @file qdec.c
  12. * @brief Contains all functions support for key scan driver
  13. * @version 0.0
  14. * @date 13. Nov. 2017
  15. * @author Ding
  16. *
  17. *
  18. *******************************************************************************/
  19. #include "rom_sym_def.h"
  20. #include <string.h>
  21. #include "cst92f2x.h"
  22. #include "OSAL.h"
  23. #include "qdec.h"
  24. #include "gpio.h"
  25. #include "uart.h"
  26. #include "log.h"
  27. #include "pwrmgr.h"
  28. #include "error.h"
  29. #include "clock.h"
  30. qdec_Ctx_t m_qdecCtx;
  31. void __attribute__((used)) hal_QDEC_IRQHandler()
  32. {
  33. HalGpioPinInit(P20, OEN);
  34. HalGpioSet(P20,1);
  35. osal_stop_timerEx(m_qdecCtx.qdec_task_id, m_qdecCtx.timeout_event);
  36. WaitMs(1);
  37. int32_t delta = GET_CNT_QUAN(m_qdecCtx.cfg.qdec_chn);
  38. m_qdecCtx.count += delta;
  39. if(m_qdecCtx.cfg.evt_handler){
  40. qdec_Evt_t evt;
  41. evt.count = m_qdecCtx.count;
  42. m_qdecCtx.cfg.evt_handler(&evt);
  43. }
  44. CLR_INT_QUAN(m_qdecCtx.cfg.qdec_chn);
  45. hal_pwrmgr_unlock(MOD_QDEC);
  46. HalGpioPinInit(P20, OEN);
  47. HalGpioSet(P20,0);
  48. }
  49. void hal_qdec_timeout_handler()
  50. {
  51. osal_stop_timerEx(m_qdecCtx.qdec_task_id, m_qdecCtx.timeout_event);
  52. hal_pwrmgr_unlock(MOD_QDEC);
  53. }
  54. /**************************************************************************************
  55. * @fn hal_qdec_set_cha
  56. *
  57. * @brief This function process for qdec initial
  58. *
  59. * input parameters
  60. *
  61. * @param None.
  62. *
  63. * output parameters
  64. *
  65. * @param None.
  66. *
  67. * @return None.
  68. **************************************************************************************/
  69. static void hal_qdec_set_cha(QDEC_CHN_e qdecCHN,GpioPin_t pin){
  70. HalGpioPupdConfig(pin, PULL_DOWN);
  71. HalGpioFmuxConfig(pin, (Fmux_Type_e)(FMUX_CHAX + (qdecCHN*3)));
  72. }
  73. /**************************************************************************************
  74. * @fn hal_qdec_set_chb
  75. *
  76. * @brief This function process for qdec initial
  77. *
  78. * input parameters
  79. *
  80. * @param None.
  81. *
  82. * output parameters
  83. *
  84. * @param None.
  85. *
  86. * @return None.
  87. **************************************************************************************/
  88. static void hal_qdec_set_chb(QDEC_CHN_e qdecCHN,GpioPin_t pin){
  89. HalGpioPupdConfig(pin, PULL_DOWN);
  90. HalGpioFmuxConfig(pin, (Fmux_Type_e)(FMUX_CHBX + (qdecCHN*3)));
  91. }
  92. /**************************************************************************************
  93. * @fn hal_qdec_set_chi
  94. *
  95. * @brief This function process for qdec initial
  96. *
  97. * input parameters
  98. *
  99. * @param None.
  100. *
  101. * output parameters
  102. *
  103. * @param None.
  104. *
  105. * @return None.
  106. **************************************************************************************/
  107. static void hal_qdec_set_chi(QDEC_CHN_e qdecCHN,GpioPin_t pin){
  108. HalGpioPupdConfig(pin, PULL_DOWN);
  109. HalGpioFmuxConfig(pin, (Fmux_Type_e)(FMUX_CHIX + (qdecCHN*3)));
  110. }
  111. /**************************************************************************************
  112. * @fn hal_qdec_init
  113. *
  114. * @brief This function process for qdec initial
  115. *
  116. * input parameters
  117. *
  118. * @param qdec_Cfg_t cfg
  119. *
  120. * output parameters
  121. *
  122. * @param None.
  123. *
  124. * @return None.
  125. **************************************************************************************/
  126. int hal_qdec_init(qdec_Cfg_t cfg, uint8 task_id, uint16 event){
  127. if(m_qdecCtx.enable)
  128. return ERR_INVALID_STATE;
  129. uint8_t pins[3] = {cfg.cha_pin, cfg.chb_pin, cfg.chi_pin};
  130. memcpy(m_qdecCtx.pin_arr, pins, sizeof(uint8_t)*3);
  131. m_qdecCtx.cfg = cfg;
  132. m_qdecCtx.qdec_task_id = task_id;
  133. m_qdecCtx.timeout_event = event;
  134. m_qdecCtx.enable = TRUE;
  135. qdec_hw_config();
  136. hal_pwrmgr_register(MOD_QDEC, qdec_sleep_handler, qdec_wakeup_handler);
  137. return SUCCESS;
  138. }
  139. void qdec_hw_config()
  140. {
  141. qdec_Cfg_t* cfg = &(m_qdecCtx.cfg);
  142. hal_clk_gate_enable(MOD_QDEC);
  143. hal_qdec_set_cha(cfg->qdec_chn, cfg->cha_pin);
  144. hal_qdec_set_chb(cfg->qdec_chn, cfg->chb_pin);
  145. DIS_INT_INCN(cfg->qdec_chn);
  146. DIS_INT_QUAN(cfg->qdec_chn);
  147. DIS_INT_02F_QUAN(cfg->qdec_chn);
  148. DIS_INT_F20_QUAN(cfg->qdec_chn);
  149. SET_MODE_QUAN(cfg->qdec_chn, cfg->quaMode);
  150. hal_qdec_set_qua_irq(cfg->qdec_chn, cfg->intMode);
  151. if(cfg->use_inc){
  152. hal_qdec_set_chi(cfg->qdec_chn, cfg->chi_pin);
  153. if(cfg->use_inc_irq){
  154. hal_qdec_set_inc_irq(cfg->qdec_chn, cfg->incMode, cfg->intMode);
  155. }
  156. }
  157. QDEC_IRQ_ENABLE;
  158. ENABLE_CHN(cfg->qdec_chn);
  159. }
  160. static void qdec_sleep_handler(void)
  161. {
  162. uint8_t pin_num;
  163. pin_num = m_qdecCtx.cfg.use_inc ? 3:2;
  164. for(uint8_t i=0;i<pin_num;i++){
  165. GPIO_Wakeup_Pol_e pol;
  166. GpioPin_t pin = m_qdecCtx.pin_arr[i];
  167. HalGpioPinInit(pin, IE);
  168. pol = HalGpioGet(pin) ? NEGEDGE:POSEDGE;
  169. HalGpioWkupConfig(pin, pol);
  170. m_qdecCtx.pin_state[i] = pol;
  171. }
  172. DISABLE_CHN(m_qdecCtx.cfg.qdec_chn);
  173. HalGpioPinInit(P23, OEN);
  174. HalGpioSet(P23,0);
  175. }
  176. static void qdec_wakeup_handler(void)
  177. {
  178. HalGpioPinInit(P23, OEN);
  179. HalGpioSet(P23,1);
  180. uint8_t pin_num;
  181. pin_num = m_qdecCtx.cfg.use_inc ? 3:2;
  182. GPIO_Wakeup_Pol_e pol;
  183. for(uint8_t i=0;i<pin_num;i++){
  184. HalGpioPinInit(m_qdecCtx.pin_arr[i], IE);
  185. pol = HalGpioGet(m_qdecCtx.pin_arr[i]) ? POSEDGE:NEGEDGE;
  186. if(pol == m_qdecCtx.pin_state[i]){
  187. break;
  188. }
  189. else if(i == pin_num-1){
  190. return;
  191. }
  192. }
  193. int32_t delta = GET_CNT_QUAN(m_qdecCtx.cfg.qdec_chn);
  194. HalGpioPinInit(P31, OEN);
  195. HalGpioSet(P31,0);
  196. hal_pwrmgr_lock(MOD_QDEC);
  197. qdec_hw_config();
  198. HalGpioPinInit(P31, OEN);
  199. HalGpioSet(P31,1);
  200. osal_start_timerEx(m_qdecCtx.qdec_task_id, m_qdecCtx.timeout_event, 150);
  201. }
  202. /**************************************************************************************
  203. * @fn hal_qdec_set_qua_irq
  204. *
  205. * @brief This function process for setting qdecode counter interupt mode
  206. *
  207. * input parameters
  208. *
  209. * @param QDEC_CHN_e chn
  210. * @param QDEC_INT_MODE_e intMode
  211. *
  212. * output parameters
  213. *
  214. * @param None.
  215. *
  216. * @return None.
  217. **************************************************************************************/
  218. static void hal_qdec_set_qua_irq(QDEC_CHN_e chn, QDEC_INT_MODE_e intMode){
  219. SET_INT_MODE_QUAN(chn, intMode);
  220. EN_INT_QUAN(chn);
  221. }
  222. /**************************************************************************************
  223. * @fn hal_qdec_set_inc_irq
  224. *
  225. * @brief This function process for setting qdecode index count mode and interupt mode
  226. *
  227. * input parameters
  228. *
  229. * @param QDEC_CHN_e chn
  230. * @param QDEC_INT_MODE_e intMode
  231. * @param QDEC_INC_MODE_e incMode
  232. *
  233. * output parameters
  234. *
  235. * @param None.
  236. *
  237. * @return None.
  238. **************************************************************************************/
  239. static void hal_qdec_set_inc_irq(QDEC_CHN_e chn, QDEC_INC_MODE_e incMode, QDEC_INT_MODE_e intMode){
  240. SET_MODE_INCN(chn, incMode);
  241. SET_INT_MODE_INCN(chn, intMode);
  242. EN_INT_INCN(chn);
  243. }