pcnt.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /******************************************************************************
  2. * Copyright (C) 2021, Xiaohua Semiconductor Co., Ltd. All rights reserved.
  3. *
  4. * This software component is licensed by XHSC under BSD 3-Clause license
  5. * (the "License"); You may not use this file except in compliance with the
  6. * License. You may obtain a copy of the License at:
  7. * opensource.org/licenses/BSD-3-Clause
  8. *
  9. ******************************************************************************/
  10. /******************************************************************************
  11. * @file pcnt.h
  12. *
  13. * @brief Header file for PCNT functions
  14. *
  15. * @author MADS Team
  16. *
  17. ******************************************************************************/
  18. #ifndef __PCNT_H__
  19. #define __PCNT_H__
  20. /*******************************************************************************
  21. * Include files
  22. ******************************************************************************/
  23. #include "ddl.h"
  24. #ifdef __cplusplus
  25. extern "C"
  26. {
  27. #endif
  28. /**
  29. ******************************************************************************
  30. ** \defgroup PCNTGroup (PCNT)
  31. **
  32. ******************************************************************************/
  33. //@{
  34. /**
  35. *******************************************************************************
  36. ** function prototypes.
  37. ******************************************************************************/
  38. /**
  39. *******************************************************************************
  40. ** \brief PCNT S0通道极性选择
  41. ** \note PCNT_CTRL S0P
  42. ******************************************************************************/
  43. typedef enum
  44. {
  45. PcntS0PNoinvert = 0u, // S0通道极性不取反
  46. PcntS0PInvert = 1u // S0通道极性取反
  47. }en_pcnt_s0polar_t;
  48. /**
  49. *******************************************************************************
  50. ** \brief PCNT S1通道极性选择
  51. ** \note PCNT_CTRL S1P
  52. ******************************************************************************/
  53. typedef enum
  54. {
  55. PcntS1PNoinvert = 0u, // S1通道极性不取反
  56. PcntS1PInvert = 1u // S1通道极性取反
  57. }en_pcnt_s1polar_t;
  58. /**
  59. *******************************************************************************
  60. ** \brief PCNT 计数方向选择
  61. ** \note PCNT_CTRL DIR
  62. ******************************************************************************/
  63. typedef enum
  64. {
  65. PcntNDoubleDirAdd = 0u, // 加计数
  66. PcntNDoubleDirSub = 1u // 减计数
  67. }en_pcnt_dir_t;
  68. /**
  69. *******************************************************************************
  70. ** \brief PCNT 计数时钟选择
  71. ** \note PCNT_CTRL ClkSel
  72. ******************************************************************************/
  73. typedef enum
  74. {
  75. PcntCLKPclk = 1u, // PCLK
  76. PcntCLKXtl = 2u, // XTL
  77. PcntCLKRcl = 3u // RCL
  78. }en_pcnt_clksel_t;
  79. /**
  80. *******************************************************************************
  81. ** \brief PCNT 脉冲计数模式选择
  82. ** \note PCNT_CTRL Mode
  83. ******************************************************************************/
  84. typedef enum
  85. {
  86. PcntSingleMode = 1u, // 单通道脉冲计数模式
  87. PcntSpecialMode = 2u, // 双通道非正交脉冲计数模式
  88. PcntDoubleMode = 3u // 双通道正交脉冲计数模式
  89. }en_pcnt_mode_t;
  90. /**
  91. ******************************************************************************
  92. ** \brief PCNT 状态
  93. ** \note PCNT_IFR & PCNT_ICR & PCNT_IEN
  94. *****************************************************************************/
  95. typedef enum
  96. {
  97. PcntS1E = 7, // S1通道脉冲解码错误
  98. PcntS0E = 6, // S0通道脉冲解码错误
  99. PcntBB = 5, // 脉冲解码错误
  100. PcntFE = 4, // 采样周期脉冲解码错误
  101. PcntDIR = 3, // 反向改变中断
  102. PcntTO = 2, // 超时中断标识
  103. PcntOV = 1, // 上溢中断标识
  104. PcntUF = 0, // 下溢中断标识
  105. }en_pcnt_itfce_t;
  106. /**
  107. ******************************************************************************
  108. ** \brief PCNT 状态寄存器1 PCNT_SR1
  109. ** \note PCNT_SR1 : DIR
  110. *****************************************************************************/
  111. typedef enum
  112. {
  113. PcntDoubleDirAdd = 0, // 双通道正交脉冲加计数
  114. PcntDoubleDirSub = 1 // 双通道正交脉冲加计数
  115. }en_pcnt_sr1dir_t;
  116. /**
  117. ******************************************************************************
  118. ** \brief PCNT 观测输出选择寄存器 PCNT_DBG
  119. ** \note PCNT_DBG: DBG
  120. *****************************************************************************/
  121. typedef enum
  122. {
  123. PcntDBGZero = 0, // 固定为0
  124. PcntDBGSxA = 1, // 脉冲同步之后通过S0A/S1A输出
  125. PcntDBGSxP = 2, // 脉冲极性选择之后通过S0P/S1P输出
  126. PcntDBGSxPF = 3 // 脉冲滤波之后通过S0PF/S1PF输出
  127. }en_pcnt_dbg_t;
  128. typedef enum
  129. {
  130. PcntDirUp = 0, //加计数
  131. PcntDirDown = 1 //减计数
  132. }en_pent_dir_t;
  133. /**
  134. ******************************************************************************
  135. ** \brief PCNT 整体配置结构体
  136. *****************************************************************************/
  137. typedef struct stc_pcnt_cfg
  138. {
  139. en_pcnt_s0polar_t Pcnt_S0Sel; // S0通道极性选择
  140. en_pcnt_s1polar_t Pcnt_S1Sel; // S1通道极性选择
  141. en_pcnt_clksel_t Pcnt_Clk; // 计数时钟选择
  142. en_pcnt_mode_t Pcnt_Mode; // 脉冲计数模式选择
  143. boolean_t Pcnt_FltEn; // 脉冲宽度滤波器使能控制 PCNT_FLT: EN
  144. uint8_t Pcnt_DebTop; // 计数器阀值 PCNT_FLT: DebTop
  145. uint16_t Pcnt_ClkDiv; // 滤波时钟分频系数 PCNT_FLT: ClkDiv
  146. boolean_t Pcnt_TocrEn; // 超时功能使能控制位 PCNT_TOCR : EN
  147. uint16_t Pcnt_TocrTh; // 超时阈值 PCNT_TOCR : TH
  148. en_pcnt_dbg_t Pcnt_Dbg; // 观测输出选择寄存器 PCNT_DBG
  149. en_pent_dir_t Pcnt_Dir;
  150. }stc_pcnt_initstruct_t;
  151. /******************************************************************************
  152. * Global variable declarations ('extern', definition in C source)
  153. ******************************************************************************/
  154. /******************************************************************************
  155. * Global function prototypes (definition in C source)
  156. ******************************************************************************/
  157. extern boolean_t Pcnt_Cmd(boolean_t NewState);
  158. extern en_result_t Pcnt_SetB2T(uint16_t value);
  159. extern en_result_t Pcnt_SetB2C(uint16_t value);
  160. extern en_result_t Pcnt_SetT2C(void);
  161. extern void Pcnt_Init(stc_pcnt_initstruct_t* InitStruct);
  162. extern void Pcnt_ItCfg(en_pcnt_itfce_t IT_Src, boolean_t NewState);
  163. extern boolean_t Pcnt_GetItStatus(en_pcnt_itfce_t IT_Src);
  164. extern void Pcnt_ClrItStatus(en_pcnt_itfce_t IT_Src);
  165. extern uint16_t Pcnt_GetCnt(void);
  166. extern uint16_t Pcnt_GetTop(void);
  167. extern uint16_t Pcnt_GetBuf(void);
  168. extern void Pcnt_SetCnt(uint16_t value);
  169. extern void Pcnt_SetBuf(uint16_t value);
  170. extern void Pcnt_SetTop(uint16_t value);
  171. //@} // PCNT Group
  172. #ifdef __cplusplus
  173. #endif
  174. #endif /* __PCNT_H__ */
  175. /*******************************************************************************
  176. * EOF (not truncated)
  177. ******************************************************************************/