summaryrefslogtreecommitdiff
path: root/bsp/acm32/acm32f0x0-nucleo/libraries/HAL_Driver/Inc/HAL_TKEY.h
blob: e479f3099dad8aca81b3773c84ce0adf9f1aef8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/***********************************************************************
 * Filename    : HAL_TKEY.h
 * Description : HAL TKEY driver header file
 * Author(s)   : Xiao Han
 * version     : V1.0
 * Modify date : 2020-04-13
 ***********************************************************************/
#ifndef __HAL_TKEY_H__
#define __HAL_TKEY_H__

#include "ACM32Fxx_HAL.h"

/****************  Bit definition for TKEY_ISR register  **************************/
#define TKEY_ISR_CHNUM_POS                     (8U)
#define TKEY_ISR_CHNUM_MASK                    (BIT11|BIT10|BIT9|BIT8)
#define TKEY_ISR_CHNUM(x)                      (x << TKEY_ISR_CHNUM_POS)
#define TKEY_ISR_BUSY                          (BIT3)
#define TKEY_ISR_TIMEOUT                       (BIT2)
#define TKEY_ISR_WAKEUP                        (BIT1)
#define TKEY_ISR_EOC                           (BIT0)

/****************  Bit definition for TKEY_IER register  **************************/
#define TKEY_IER_TIMEOUTIE                     (BIT2)
#define TKEY_IER_WAKEUPIE                      (BIT1)
#define TKEY_IER_EOCIE                         (BIT0)

/****************  Bit definition for TKEY_CR register  **************************/
#define TKEY_CR_RANDM_POS                      (12U)
#define TKEY_CR_RANDM_MASK                     (BIT13|BIT12)
#define TKEY_CR_RANDM(x)                       (x << TKEY_CR_RANDM_POS)
#define TKEY_CR_CHARGESEL                      (BIT11)
#define TKEY_CR_VKEYSEL_POS                    (9U)
#define TKEY_CR_VKEYSEL_MASK                   (BIT10|BIT9)
#define TKEY_CR_VKEYSEL(x)                     (x << TKEY_CR_VKEYSEL_POS)
#define TKEY_CR_VREFSEL_POS                    (7U)
#define TKEY_CR_VREFSEL_MASK                   (BIT8|BIT7)
#define TKEY_CR_VREFSEL(x)                     (x << TKEY_CR_VREFSEL_POS)
#define TKEY_CR_SLEEP                          (BIT6)
#define TKEY_CR_SPREAD                         (BIT5)
#define TKEY_CR_CONT                           (BIT4)
#define TKEY_CR_SHIELDEN                       (BIT3)
#define TKEY_CR_CREN                           (BIT2)
#define TKEY_CR_START                          (BIT1)
#define TKEY_CR_SCAN                           (BIT0)

/****************  Bit definition for TKEY_SR register  **************************/
#define TKEY_SR_VCOUT2_ORG                      (BIT3)
#define TKEY_SR_VCOUT1_ORG                      (BIT2)
#define TKEY_SR_VCOUT2                          (BIT1)
#define TKEY_SR_VCOUT1                          (BIT0)

/****************  Bit definition for TKEY_SMPR register  **************************/
#define TKEY_SMPR_SWT_POS                       (4U)
#define TKEY_SMPR_SWT_MASK                      (0xFFF0U)
#define TKEY_SMPR_SWT(x)                        (x << TKEY_SMPR_SWT_POS)
#define TKEY_SMPR_CST_POS                       (0U)
#define TKEY_SMPR_CST_MASK                      (0x000FU)
#define TKEY_SMPR_CST(x)                        (x << TKEY_SMPR_CST_POS)

/****************  Bit definition for TKEY_SOFR register  **************************/
#define TKEY_SOFR_SW1H_POS                      (8U)
#define TKEY_SOFR_SW1H_MASK                     (0xFF00U)
#define TKEY_SOFR_SW1H(x)                       (x << TKEY_SOFR_SW1H_POS)
#define TKEY_SOFR_SW1L_POS                      (0U)
#define TKEY_SOFR_SW1L_MASK                     (0x00FFU)
#define TKEY_SOFR_SW1L(x)                       (x << TKEY_SOFR_SW1L_POS)

typedef enum
{
  // Calibration states
  TKEY_STATEID_CALIB              = 0,  /**<  0 - Object is in Calibration */
  // Release states
  TKEY_STATEID_RELEASE            = 1,  /**<  1 - Object is released */
  // Detect states
  TKEY_STATEID_DETECT             = 2, /**<   2 - Object is in Detect */
  //Startuo states
  TKEY_STATEID_STARTUP            = 3, /**<   3 - Object is in Startup */
} TKEY_StateId_enum_T;

typedef enum
{
  TKEY_CRSELECT_DISABLE           = 0,  /**<  0 - Object is in Calibration */
  TKEY_CRSELECT_ENABLE            = 1,  /**<  1 - Object is in Debounce Calibration */
} TKEY_CRSELECT_enum_T;

/**
  * @brief  TKEY Configuration Structure definition
  */
typedef struct
{
    uint8_t            VkeySel;
    uint8_t            VrefSel;
    uint8_t            ShieldEn;
    uint16_t           ScanWaitTime;
    uint8_t            CsDisChargeTime;
    uint8_t            Sw1H;
    uint8_t            Sw1L;
}TKEY_InitTypeDef;

/**
  * @brief  TKEY Configuration Structure definition
  */
typedef struct
{
    uint16_t             RefDelta;         /*!< Use a standard finger to get this value at the Development state*/
    uint16_t             DetectInTH;       /*!< The threshold when the state from release to detect*/
    uint16_t             DetectOutTH;      /*!< The threshold when the state from detect to release*/
    uint16_t             CalibratTH;       /*!< The threshold when need to calibration*/
    uint16_t             DebIn;            /*!< The filter counter for debounce when the state from release to detect*/
    uint16_t             DebOut;           /*!< The filter counter for debounce when the state from detect to release*/
    TKEY_CRSELECT_enum_T    CrSelect;   /*!< If the channel need compensation, set the CrSelect TKEY_CRSELECT_ENABLE*/
}TKEY_ParaDef;

typedef struct
{
    uint8_t              ReferenceFlag;    /*!< If need to get the reference data */
    uint32_t           RefData;          /*!< The reference data  */
    uint32_t           Data;             /*!< The current data  */
    int16_t            Delta;            /*!< The delta value  */
    uint16_t             DebIn;            /*!< The variate value for debounce*/
    uint16_t             DebOut;           /*!< The variate value for debounce*/
    TKEY_StateId_enum_T   StateId;       /*!< The state machine ID*/
}TKEY_DataDef;

typedef struct
{
    TKEY_ParaDef      *Tkey_RefPara;
    TKEY_DataDef      *Tkey_Data;        /*!<  */
    uint16_t            ChannelId;
}TKEY_ChannelDataDef;

typedef struct
{
    __IO uint32_t                          DetectingTimeout;    //Up to this times, will detecting keys.
    __IO uint32_t                          CalibratTimeout;     //Up to this times, means the environment be changed,need calibrate.
    __IO float                             DetectInThRatio;     //The ratio between detect in threshold and the Reference Delta RefDelta.
    __IO float                             DetectOutThRatio;    //The ratio between detect out threshold and the Reference Delta RefDelta.
    __IO float                             CalibratThRatio;     //The ratio between calibrate threshold and the Reference Delta RefDelta.
    __IO float                             WakeUpThRatio;       //The ratio between wakeup threshold and the Reference Delta RefDelta.
    __IO uint16_t                          SleepScanWaitTime;   //The Sleep mode scan wait time, Slow down the scan speed.
}TKEY_ScanParaDef;

/**
  * @brief  ADC handle Structure definition
  */
typedef struct
{
    TKEY_TypeDef                      *Instance;          /*!< Register base address    */
    const TKEY_ChannelDataDef         *ChannelData;       /*!< The channel data point*/
    TKEY_InitTypeDef                  Init;               /*!< TKEY init parameters */
    TKEY_ScanParaDef                  ScanPara;           /*!< TKEY Scan parameters */
    uint32_t                          NrData;             /*!< TKEY Cr counter data*/
    uint16_t                          ChannelDetecting;   /*!< TKEY detecting before release*/
    uint16_t                          ChannelDetected;    /*!< TKEY detected*/
    uint16_t                          ChannelValue;       /*!< TKEY Value*/
    uint16_t                          ChannelDetectedNum; /*!< TKEY detected channel number*/
    uint8_t                           TotalChannelNum;    /*!< TKEY toatal channel number*/
    uint32_t                          ScanTimer;          /*!< TKEY scan Timer*/
    uint8_t                           CalFlag;            /*!< TKEY If need to calibration */
} TKEY_HandleTypeDef;

#define TKEY_CHANNEL_0                  (0U)
#define TKEY_CHANNEL_1                  (1U)
#define TKEY_CHANNEL_2                  (2U)
#define TKEY_CHANNEL_3                  (3U)
#define TKEY_CHANNEL_4                  (4U)
#define TKEY_CHANNEL_5                  (5U)
#define TKEY_CHANNEL_6                  (6U)
#define TKEY_CHANNEL_7                  (7U)
#define TKEY_CHANNEL_8                  (8U)
#define TKEY_CHANNEL_9                  (9U)
#define TKEY_CHANNEL_10                 (10U)
#define TKEY_CHANNEL_11                 (11U)
#define TKEY_CHANNEL_12                 (12U)
#define TKEY_CHANNEL_13                 (13U)
#define TKEY_CHANNEL_14                 (14U)
#define TKEY_CHANNEL_15                 (15U)

#define TKEY_CR_CHARGESEL_OPA            (0U)
#define TKEY_CR_CHARGESEL_LDO            (1U)

#define TKEY_CR_VKEYSEL_1V2              (0U)
#define TKEY_CR_VKEYSEL_2V0              (1U)
#define TKEY_CR_VKEYSEL_2V5              (2U)
#define TKEY_CR_VKEYSEL_3V0              (3U)

#define TKEY_CR_VREFSEL_0V6              (0U)
#define TKEY_CR_VREFSEL_1V0              (1U)
#define TKEY_CR_VREFSEL_1V5              (2U)
#define TKEY_CR_VREFSEL_2V0              (3U)

#define TKEY_CR_SPREAD_DISABLE           (0U)
#define TKEY_CR_SPREAD_ENABLE            (1U)

#define TKEY_CR_CONT_DISABLE             (0U)
#define TKEY_CR_CONT_ENABLE              (1U)

#define TKEY_CR_SHIELDEN_DISABLE         (0U)
#define TKEY_CR_SHIELDEN_ENABLE          (1U)

#define TKEY_CR_CREN_CX                  (0U)
#define TKEY_CR_CREN_CR                  (1U)

#define TKEY_CR_SCAN_DISABLE             (0U)
#define TKEY_CR_SCAN_ENABLE              (1U)

#define TKEY_SOFR_RANDM_1                (0U)
#define TKEY_SOFR_RANDM_12               (1U)
#define TKEY_SOFR_RANDM_123              (2U)
#define TKEY_SOFR_RANDM_1234             (3U)


/******************************** ADC Instances *******************************/
#define IS_TKEY_ALL_INSTANCE(INSTANCE)     ((INSTANCE) == TKEY)

#define IS_TKEY_ALL_CHARGESEL(CHARGESEL)   (((CHARGESEL) == TKEY_CR_CHARGESEL_OPA) || \
                                           ((CHARGESEL) == TKEY_CR_CHARGESEL_LDO))

#define IS_TKEY_ALL_VKEYSEL(VKEYSEL)       (((VKEYSEL) == TKEY_CR_VKEYSEL_1V2) || \
                                           ((VKEYSEL) == TKEY_CR_VKEYSEL_2V0) || \
                                           ((VKEYSEL) == TKEY_CR_VKEYSEL_2V5) || \
                                           ((VKEYSEL) == TKEY_CR_VKEYSEL_3V0))

#define IS_TKEY_ALL_VREFSEL(VREFSEL)       (((VREFSEL) == TKEY_CR_VREFSEL_0V6) || \
                                           ((VREFSEL) == TKEY_CR_VREFSEL_1V0) || \
                                           ((VREFSEL) == TKEY_CR_VREFSEL_1V5) || \
                                           ((VREFSEL) == TKEY_CR_VREFSEL_2V0))

#define IS_TKEY_ALL_SPREAD(SPREAD)         (((SPREAD) == TKEY_CR_SPREAD_DISABLE) || \
                                           ((SPREAD) == TKEY_CR_SPREAD_ENABLE))

#define IS_TKEY_ALL_SCANCONT(SCANCONT)     (((SCANCONT) == TKEY_CR_CONT_DISABLE) || \
                                           ((SCANCONT) == TKEY_CR_CONT_ENABLE))

#define IS_TKEY_ALL_SHIELDEN(SHIELDEN)     (((SHIELDEN) == TKEY_CR_SHIELDEN_DISABLE) || \
                                           ((SHIELDEN) == TKEY_CR_SHIELDEN_ENABLE))

#define IS_TKEY_ALL_JITTERRANDM(JITTERRANDM)     (((JITTERRANDM) == TKEY_SOFR_RANDM_1) || \
                                           ((JITTERRANDM) == TKEY_SOFR_RANDM_12) || \
                                           ((JITTERRANDM) == TKEY_SOFR_RANDM_123) || \
                                           ((JITTERRANDM) == TKEY_SOFR_RANDM_1234))

#define IS_TKEY_ALL_SCANWAITTIME(SCANWAITTIME)     ((SCANWAITTIME) <= 0x0FFF)

#define IS_TKEY_ALL_CSDISCHARGETIME(CSDISCHARGETIME)    ((CSDISCHARGETIME) <= 0x0F)

#define IS_TKEY_ALL_SW1(_SW1)              ((_SW1) <= 0xFF)

/***************** TKEY Function definition *************************/
HAL_StatusTypeDef HAL_TKEY_Init(TKEY_HandleTypeDef* htkey);
HAL_StatusTypeDef HAL_TKEY_Start(TKEY_HandleTypeDef* htkey);
HAL_StatusTypeDef HAL_TKEY_Stop(TKEY_HandleTypeDef* htkey);
HAL_StatusTypeDef HAL_TKEY_ReadNr(TKEY_HandleTypeDef* htkey);
HAL_StatusTypeDef HAL_TKEY_ReadChannelData(TKEY_HandleTypeDef* htkey);
HAL_StatusTypeDef HAL_TKEY_ReadAllNx(TKEY_HandleTypeDef* htkey);
HAL_StatusTypeDef HAL_TKEY_Suspend(TKEY_HandleTypeDef* htkey);
HAL_StatusTypeDef HAL_TKEY_Resume(TKEY_HandleTypeDef* htkey);
void HAL_TKEY_DetectProcess(TKEY_HandleTypeDef* htkey);
void HAL_TKEY_Calibrate_RefData(TKEY_HandleTypeDef* htkey, uint8_t CalTimes);

#endif