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