qdec.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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.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 __QDEC__H__
  40. #define __QDEC__H__
  41. #include "types.h"
  42. #include "gpio.h"
  43. #define QDEC_IRQ_ENABLE *(volatile unsigned int *) 0xe000e100 |= BIT(30)
  44. #define ENABLE_CHN(n) *(volatile unsigned int *) 0x4000B000 |= BIT(4*n)
  45. #define DISABLE_CHN(n) *(volatile unsigned int *) 0x4000B000 &= ~BIT(4*n)
  46. #define EN_INT_QUAN(n) *(volatile unsigned int *) 0x4000B004 |= BIT(8*n)
  47. #define EN_INT_INCN(n) *(volatile unsigned int *) 0x4000B004 |= BIT(4+8*n)
  48. #define DIS_INT_QUAN(n) *(volatile unsigned int *) 0x4000B004 &= ~BIT(8*n)
  49. #define DIS_INT_INCN(n) *(volatile unsigned int *) 0x4000B004 &= ~BIT(4+8*n)
  50. #define SET_INT_MODE_QUAN(chn,mod) subWriteReg(0x4000B004,2+8*chn,2+8*chn,mod)
  51. #define SET_INT_MODE_INCN(chn,mod) subWriteReg(0x4000B004,6+8*chn,6+8*chn,mod)
  52. #define EN_INT_F20_QUAN(n) *(volatile unsigned int *) 0x4000B004 |= BIT(24+2*n)
  53. #define EN_INT_02F_QUAN(n) *(volatile unsigned int *) 0x4000B004 |= BIT(25+2*n)
  54. #define DIS_INT_F20_QUAN(n) *(volatile unsigned int *) 0x4000B004 &= ~BIT(24+2*n)
  55. #define DIS_INT_02F_QUAN(n) *(volatile unsigned int *) 0x4000B004 &= ~BIT(25+2*n)
  56. #define CLR_INT_F20_QUAN(n) *(volatile unsigned int *) 0x4000B008 |= BIT(24+2*n)
  57. #define CLR_INT_02F_QUAN(n) *(volatile unsigned int *) 0x4000B008 |= BIT(25+2*n)
  58. #define CLR_INT_QUAN(n) *(volatile unsigned int *) 0x4000B008 |= BIT(8*n)
  59. #define CLR_INT_INCN(n) *(volatile unsigned int *) 0x4000B008 |= BIT(4+8*n)
  60. #define STATUS_INT_QUAN(n) read_reg(0x4000B00C) & BIT(8*n)
  61. #define STATUS_INT_INCN(n) read_reg(0x4000B00C) & BIT(4+8*n)
  62. #define STATUS_INT_F20_QUAN(n) read_reg(0x4000B00C) & BIT(24+2*n)
  63. #define STATUS_INT_02F_QUAN(n) read_reg(0x4000B00C) & BIT(25+2*n)
  64. #define SET_MODE_QUAN(chn,mod) subWriteReg(0x4000B010 + 0x14*chn,1,0, mod)
  65. #define SET_MODE_INCN(chn,mod) subWriteReg(0x4000B010 + 0x14*chn,17,16,mod)
  66. #define SET_HIT_QUAN(chn,mod) write_reg(0x4000B014 + 0x14*chn,mod)
  67. #define SET_HIT_INCN(chn,mod) write_reg(0x4000B018 + 0x14*chn,mod)
  68. #define GET_CNT_QUAN(n) read_reg(0x4000B01C + 0x14*n)
  69. #define GET_CNT_INCN(n) read_reg(0x4000B020 + 0x14*n)
  70. /*************************************************************
  71. * @brief enum variable used for setting channel
  72. *
  73. */
  74. typedef enum{
  75. QDEC_CHX = 0,
  76. QDEC_CHY = 1,
  77. QDEC_CHZ = 2
  78. }QDEC_CHN_e;
  79. /*************************************************************
  80. * @brief enum variable used for setting quadrature count mode
  81. *
  82. */
  83. typedef enum{
  84. QDEC_MODE_1X = 1,
  85. QDEC_MODE_2X = 2,
  86. QDEC_MODE_4X = 3
  87. }QDEC_QUA_MODE_e;
  88. /*************************************************************
  89. * @brief enum variable used for setting index count mode
  90. *
  91. */
  92. typedef enum{
  93. HIGH_LEVEL = 0,
  94. POS_EDGE = 1,
  95. NEG_EDGE = 2,
  96. POS_OR_NEG_EDGE = 3,
  97. }QDEC_INC_MODE_e;
  98. /*************************************************************
  99. * @brief enum variable used for setting interupt mode
  100. *
  101. */
  102. typedef enum{
  103. INT_BY_CHANGE = 0,
  104. INT_BY_HIT = 1
  105. }QDEC_INT_MODE_e;
  106. typedef struct {
  107. int32_t count;
  108. } qdec_Evt_t;
  109. typedef void (*qdec_Hdl_t)(qdec_Evt_t* pev);
  110. typedef struct {
  111. GPIO_Pin_e cha_pin;
  112. GPIO_Pin_e chb_pin;
  113. QDEC_CHN_e qdec_chn;
  114. QDEC_QUA_MODE_e quaMode;
  115. QDEC_INT_MODE_e intMode;
  116. qdec_Hdl_t evt_handler;
  117. bool use_inc;
  118. bool use_inc_irq;
  119. GPIO_Pin_e chi_pin;
  120. QDEC_INC_MODE_e incMode;
  121. } qdec_Cfg_t;
  122. typedef struct {
  123. bool enable;
  124. qdec_Cfg_t cfg;
  125. int32_t count;
  126. uint8_t pin_state[3];
  127. GPIO_Pin_e pin_arr[3];
  128. uint8_t qdec_task_id;
  129. uint16_t timeout_event;
  130. } qdec_Ctx_t;
  131. static void qdec_hw_config(void);
  132. static void qdec_sleep_handler(void);
  133. static void qdec_wakeup_handler(void);
  134. static void hal_qdec_set_cha(QDEC_CHN_e qdecCHN,GPIO_Pin_e pin);
  135. static void hal_qdec_set_chb(QDEC_CHN_e qdecCHN,GPIO_Pin_e pin);
  136. static void hal_qdec_set_chi(QDEC_CHN_e qdecCHN,GPIO_Pin_e pin);
  137. static void hal_qdec_set_qua_irq(QDEC_CHN_e chn, QDEC_INT_MODE_e intMode);
  138. static void hal_qdec_set_inc_irq(QDEC_CHN_e chn, QDEC_INC_MODE_e incMode, QDEC_INT_MODE_e intMode);
  139. int hal_qdec_init(qdec_Cfg_t cfg, uint8 task_id, uint16 event);
  140. void hal_qdec_timeout_handler(void);
  141. void hal_qdec_IRQHandler(void);
  142. #endif