HAL_TKEY.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /***********************************************************************
  2. * Filename : HAL_TKEY.h
  3. * Description : HAL TKEY driver header file
  4. * Author(s) : Xiao Han
  5. * version : V1.0
  6. * Modify date : 2020-04-13
  7. ***********************************************************************/
  8. #ifndef __HAL_TKEY_H__
  9. #define __HAL_TKEY_H__
  10. #include "ACM32Fxx_HAL.h"
  11. /**************** Bit definition for TKEY_ISR register **************************/
  12. #define TKEY_ISR_CHNUM_POS (8U)
  13. #define TKEY_ISR_CHNUM_MASK (BIT11|BIT10|BIT9|BIT8)
  14. #define TKEY_ISR_CHNUM(x) (x << TKEY_ISR_CHNUM_POS)
  15. #define TKEY_ISR_BUSY (BIT3)
  16. #define TKEY_ISR_TIMEOUT (BIT2)
  17. #define TKEY_ISR_WAKEUP (BIT1)
  18. #define TKEY_ISR_EOC (BIT0)
  19. /**************** Bit definition for TKEY_IER register **************************/
  20. #define TKEY_IER_TIMEOUTIE (BIT2)
  21. #define TKEY_IER_WAKEUPIE (BIT1)
  22. #define TKEY_IER_EOCIE (BIT0)
  23. /**************** Bit definition for TKEY_CR register **************************/
  24. #define TKEY_CR_RANDM_POS (12U)
  25. #define TKEY_CR_RANDM_MASK (BIT13|BIT12)
  26. #define TKEY_CR_RANDM(x) (x << TKEY_CR_RANDM_POS)
  27. #define TKEY_CR_CHARGESEL (BIT11)
  28. #define TKEY_CR_VKEYSEL_POS (9U)
  29. #define TKEY_CR_VKEYSEL_MASK (BIT10|BIT9)
  30. #define TKEY_CR_VKEYSEL(x) (x << TKEY_CR_VKEYSEL_POS)
  31. #define TKEY_CR_VREFSEL_POS (7U)
  32. #define TKEY_CR_VREFSEL_MASK (BIT8|BIT7)
  33. #define TKEY_CR_VREFSEL(x) (x << TKEY_CR_VREFSEL_POS)
  34. #define TKEY_CR_SLEEP (BIT6)
  35. #define TKEY_CR_SPREAD (BIT5)
  36. #define TKEY_CR_CONT (BIT4)
  37. #define TKEY_CR_SHIELDEN (BIT3)
  38. #define TKEY_CR_CREN (BIT2)
  39. #define TKEY_CR_START (BIT1)
  40. #define TKEY_CR_SCAN (BIT0)
  41. /**************** Bit definition for TKEY_SR register **************************/
  42. #define TKEY_SR_VCOUT2_ORG (BIT3)
  43. #define TKEY_SR_VCOUT1_ORG (BIT2)
  44. #define TKEY_SR_VCOUT2 (BIT1)
  45. #define TKEY_SR_VCOUT1 (BIT0)
  46. /**************** Bit definition for TKEY_SMPR register **************************/
  47. #define TKEY_SMPR_SWT_POS (4U)
  48. #define TKEY_SMPR_SWT_MASK (0xFFF0U)
  49. #define TKEY_SMPR_SWT(x) (x << TKEY_SMPR_SWT_POS)
  50. #define TKEY_SMPR_CST_POS (0U)
  51. #define TKEY_SMPR_CST_MASK (0x000FU)
  52. #define TKEY_SMPR_CST(x) (x << TKEY_SMPR_CST_POS)
  53. /**************** Bit definition for TKEY_SOFR register **************************/
  54. #define TKEY_SOFR_SW1H_POS (8U)
  55. #define TKEY_SOFR_SW1H_MASK (0xFF00U)
  56. #define TKEY_SOFR_SW1H(x) (x << TKEY_SOFR_SW1H_POS)
  57. #define TKEY_SOFR_SW1L_POS (0U)
  58. #define TKEY_SOFR_SW1L_MASK (0x00FFU)
  59. #define TKEY_SOFR_SW1L(x) (x << TKEY_SOFR_SW1L_POS)
  60. typedef enum
  61. {
  62. // Calibration states
  63. TKEY_STATEID_CALIB = 0, /**< 0 - Object is in Calibration */
  64. // Release states
  65. TKEY_STATEID_RELEASE = 1, /**< 1 - Object is released */
  66. // Detect states
  67. TKEY_STATEID_DETECT = 2, /**< 2 - Object is in Detect */
  68. //Startuo states
  69. TKEY_STATEID_STARTUP = 3, /**< 3 - Object is in Startup */
  70. } TKEY_StateId_enum_T;
  71. typedef enum
  72. {
  73. TKEY_CRSELECT_DISABLE = 0, /**< 0 - Object is in Calibration */
  74. TKEY_CRSELECT_ENABLE = 1, /**< 1 - Object is in Debounce Calibration */
  75. } TKEY_CRSELECT_enum_T;
  76. /**
  77. * @brief TKEY Configuration Structure definition
  78. */
  79. typedef struct
  80. {
  81. uint8_t VkeySel;
  82. uint8_t VrefSel;
  83. uint8_t ShieldEn;
  84. uint16_t ScanWaitTime;
  85. uint8_t CsDisChargeTime;
  86. uint8_t Sw1H;
  87. uint8_t Sw1L;
  88. }TKEY_InitTypeDef;
  89. /**
  90. * @brief TKEY Configuration Structure definition
  91. */
  92. typedef struct
  93. {
  94. uint16_t RefDelta; /*!< Use a standard finger to get this value at the Development state*/
  95. uint16_t DetectInTH; /*!< The threshold when the state from release to detect*/
  96. uint16_t DetectOutTH; /*!< The threshold when the state from detect to release*/
  97. uint16_t CalibratTH; /*!< The threshold when need to calibration*/
  98. uint16_t DebIn; /*!< The filter counter for debounce when the state from release to detect*/
  99. uint16_t DebOut; /*!< The filter counter for debounce when the state from detect to release*/
  100. TKEY_CRSELECT_enum_T CrSelect; /*!< If the channel need compensation, set the CrSelect TKEY_CRSELECT_ENABLE*/
  101. }TKEY_ParaDef;
  102. typedef struct
  103. {
  104. uint8_t ReferenceFlag; /*!< If need to get the reference data */
  105. uint32_t RefData; /*!< The reference data */
  106. uint32_t Data; /*!< The current data */
  107. int16_t Delta; /*!< The delta value */
  108. uint16_t DebIn; /*!< The variate value for debounce*/
  109. uint16_t DebOut; /*!< The variate value for debounce*/
  110. TKEY_StateId_enum_T StateId; /*!< The state machine ID*/
  111. }TKEY_DataDef;
  112. typedef struct
  113. {
  114. TKEY_ParaDef *Tkey_RefPara;
  115. TKEY_DataDef *Tkey_Data; /*!< */
  116. uint16_t ChannelId;
  117. }TKEY_ChannelDataDef;
  118. typedef struct
  119. {
  120. __IO uint32_t DetectingTimeout; //Up to this times, will detecting keys.
  121. __IO uint32_t CalibratTimeout; //Up to this times, means the environment be changed,need calibrate.
  122. __IO float DetectInThRatio; //The ratio between detect in threshold and the Reference Delta RefDelta.
  123. __IO float DetectOutThRatio; //The ratio between detect out threshold and the Reference Delta RefDelta.
  124. __IO float CalibratThRatio; //The ratio between calibrate threshold and the Reference Delta RefDelta.
  125. __IO float WakeUpThRatio; //The ratio between wakeup threshold and the Reference Delta RefDelta.
  126. __IO uint16_t SleepScanWaitTime; //The Sleep mode scan wait time, Slow down the scan speed.
  127. }TKEY_ScanParaDef;
  128. /**
  129. * @brief ADC handle Structure definition
  130. */
  131. typedef struct
  132. {
  133. TKEY_TypeDef *Instance; /*!< Register base address */
  134. const TKEY_ChannelDataDef *ChannelData; /*!< The channel data point*/
  135. TKEY_InitTypeDef Init; /*!< TKEY init parameters */
  136. TKEY_ScanParaDef ScanPara; /*!< TKEY Scan parameters */
  137. uint32_t NrData; /*!< TKEY Cr counter data*/
  138. uint16_t ChannelDetecting; /*!< TKEY detecting before release*/
  139. uint16_t ChannelDetected; /*!< TKEY detected*/
  140. uint16_t ChannelValue; /*!< TKEY Value*/
  141. uint16_t ChannelDetectedNum; /*!< TKEY detected channel number*/
  142. uint8_t TotalChannelNum; /*!< TKEY toatal channel number*/
  143. uint32_t ScanTimer; /*!< TKEY scan Timer*/
  144. uint8_t CalFlag; /*!< TKEY If need to calibration */
  145. } TKEY_HandleTypeDef;
  146. #define TKEY_CHANNEL_0 (0U)
  147. #define TKEY_CHANNEL_1 (1U)
  148. #define TKEY_CHANNEL_2 (2U)
  149. #define TKEY_CHANNEL_3 (3U)
  150. #define TKEY_CHANNEL_4 (4U)
  151. #define TKEY_CHANNEL_5 (5U)
  152. #define TKEY_CHANNEL_6 (6U)
  153. #define TKEY_CHANNEL_7 (7U)
  154. #define TKEY_CHANNEL_8 (8U)
  155. #define TKEY_CHANNEL_9 (9U)
  156. #define TKEY_CHANNEL_10 (10U)
  157. #define TKEY_CHANNEL_11 (11U)
  158. #define TKEY_CHANNEL_12 (12U)
  159. #define TKEY_CHANNEL_13 (13U)
  160. #define TKEY_CHANNEL_14 (14U)
  161. #define TKEY_CHANNEL_15 (15U)
  162. #define TKEY_CR_CHARGESEL_OPA (0U)
  163. #define TKEY_CR_CHARGESEL_LDO (1U)
  164. #define TKEY_CR_VKEYSEL_1V2 (0U)
  165. #define TKEY_CR_VKEYSEL_2V0 (1U)
  166. #define TKEY_CR_VKEYSEL_2V5 (2U)
  167. #define TKEY_CR_VKEYSEL_3V0 (3U)
  168. #define TKEY_CR_VREFSEL_0V6 (0U)
  169. #define TKEY_CR_VREFSEL_1V0 (1U)
  170. #define TKEY_CR_VREFSEL_1V5 (2U)
  171. #define TKEY_CR_VREFSEL_2V0 (3U)
  172. #define TKEY_CR_SPREAD_DISABLE (0U)
  173. #define TKEY_CR_SPREAD_ENABLE (1U)
  174. #define TKEY_CR_CONT_DISABLE (0U)
  175. #define TKEY_CR_CONT_ENABLE (1U)
  176. #define TKEY_CR_SHIELDEN_DISABLE (0U)
  177. #define TKEY_CR_SHIELDEN_ENABLE (1U)
  178. #define TKEY_CR_CREN_CX (0U)
  179. #define TKEY_CR_CREN_CR (1U)
  180. #define TKEY_CR_SCAN_DISABLE (0U)
  181. #define TKEY_CR_SCAN_ENABLE (1U)
  182. #define TKEY_SOFR_RANDM_1 (0U)
  183. #define TKEY_SOFR_RANDM_12 (1U)
  184. #define TKEY_SOFR_RANDM_123 (2U)
  185. #define TKEY_SOFR_RANDM_1234 (3U)
  186. /******************************** ADC Instances *******************************/
  187. #define IS_TKEY_ALL_INSTANCE(INSTANCE) ((INSTANCE) == TKEY)
  188. #define IS_TKEY_ALL_CHARGESEL(CHARGESEL) (((CHARGESEL) == TKEY_CR_CHARGESEL_OPA) || \
  189. ((CHARGESEL) == TKEY_CR_CHARGESEL_LDO))
  190. #define IS_TKEY_ALL_VKEYSEL(VKEYSEL) (((VKEYSEL) == TKEY_CR_VKEYSEL_1V2) || \
  191. ((VKEYSEL) == TKEY_CR_VKEYSEL_2V0) || \
  192. ((VKEYSEL) == TKEY_CR_VKEYSEL_2V5) || \
  193. ((VKEYSEL) == TKEY_CR_VKEYSEL_3V0))
  194. #define IS_TKEY_ALL_VREFSEL(VREFSEL) (((VREFSEL) == TKEY_CR_VREFSEL_0V6) || \
  195. ((VREFSEL) == TKEY_CR_VREFSEL_1V0) || \
  196. ((VREFSEL) == TKEY_CR_VREFSEL_1V5) || \
  197. ((VREFSEL) == TKEY_CR_VREFSEL_2V0))
  198. #define IS_TKEY_ALL_SPREAD(SPREAD) (((SPREAD) == TKEY_CR_SPREAD_DISABLE) || \
  199. ((SPREAD) == TKEY_CR_SPREAD_ENABLE))
  200. #define IS_TKEY_ALL_SCANCONT(SCANCONT) (((SCANCONT) == TKEY_CR_CONT_DISABLE) || \
  201. ((SCANCONT) == TKEY_CR_CONT_ENABLE))
  202. #define IS_TKEY_ALL_SHIELDEN(SHIELDEN) (((SHIELDEN) == TKEY_CR_SHIELDEN_DISABLE) || \
  203. ((SHIELDEN) == TKEY_CR_SHIELDEN_ENABLE))
  204. #define IS_TKEY_ALL_JITTERRANDM(JITTERRANDM) (((JITTERRANDM) == TKEY_SOFR_RANDM_1) || \
  205. ((JITTERRANDM) == TKEY_SOFR_RANDM_12) || \
  206. ((JITTERRANDM) == TKEY_SOFR_RANDM_123) || \
  207. ((JITTERRANDM) == TKEY_SOFR_RANDM_1234))
  208. #define IS_TKEY_ALL_SCANWAITTIME(SCANWAITTIME) ((SCANWAITTIME) <= 0x0FFF)
  209. #define IS_TKEY_ALL_CSDISCHARGETIME(CSDISCHARGETIME) ((CSDISCHARGETIME) <= 0x0F)
  210. #define IS_TKEY_ALL_SW1(_SW1) ((_SW1) <= 0xFF)
  211. /***************** TKEY Function definition *************************/
  212. HAL_StatusTypeDef HAL_TKEY_Init(TKEY_HandleTypeDef* htkey);
  213. HAL_StatusTypeDef HAL_TKEY_Start(TKEY_HandleTypeDef* htkey);
  214. HAL_StatusTypeDef HAL_TKEY_Stop(TKEY_HandleTypeDef* htkey);
  215. HAL_StatusTypeDef HAL_TKEY_ReadNr(TKEY_HandleTypeDef* htkey);
  216. HAL_StatusTypeDef HAL_TKEY_ReadChannelData(TKEY_HandleTypeDef* htkey);
  217. HAL_StatusTypeDef HAL_TKEY_ReadAllNx(TKEY_HandleTypeDef* htkey);
  218. HAL_StatusTypeDef HAL_TKEY_Suspend(TKEY_HandleTypeDef* htkey);
  219. HAL_StatusTypeDef HAL_TKEY_Resume(TKEY_HandleTypeDef* htkey);
  220. void HAL_TKEY_DetectProcess(TKEY_HandleTypeDef* htkey);
  221. void HAL_TKEY_Calibrate_RefData(TKEY_HandleTypeDef* htkey, uint8_t CalTimes);
  222. #endif