summaryrefslogtreecommitdiff
path: root/bsp/acm32/acm32f0x0-nucleo/libraries/HAL_Driver/Src/HAL_TKEY.c
blob: c61c77718609201409a9c70f2ec4f76cc35a0adb (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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/*
  ******************************************************************************
  * @file    HAL_TKEY.c
  * @version V1.0.0
  * @date    2020
  * @brief   DMA HAL module driver.
  *          This file provides firmware functions to manage the following
  *          functionalities of the Direct Memory Access (DMA) peripheral:
  *           @ Initialization and de-initialization functions
  *           @ IO operation functions
  ******************************************************************************
*/
#include "ACM32Fxx_HAL.h"

__IO uint32_t u32Regbackup;

/************************************************************************
 * function   : TKEY_IRQHandler
 * Description: tkey interrupt service routine.
 * input :
 *         none
 * return: none
 ************************************************************************/
void TKEY_IRQHandler(void)
{
    TKEY->ISR = 0xFFF;
}

/************************************************************************
 * function   : HAL_TKEY_MspInit
 * Description: Init the hardware, GPIO and clock, etc.
 * input      : htkey : TKEY handle
 * return     : none
 ************************************************************************/
void HAL_TKEY_MspInit(TKEY_HandleTypeDef* htkey)
{
    /* For Example */
    GPIO_InitTypeDef GPIO_Handle;
    uint8_t ucI;

    for(ucI = 0; htkey->ChannelData[ucI].ChannelId != 0xFFFF; ucI++)
    {
        /* TKEY0 GPIO inition*/
        if(htkey->ChannelData[ucI].ChannelId < 4)             /*TKEY0-3 -> PA10-13*/
        {
            GPIO_Handle.Pin            = (uint16_t)(0x0001 << (htkey->ChannelData[ucI].ChannelId + 10));
            GPIO_Handle.Mode           = GPIO_MODE_ANALOG;
            GPIO_Handle.Pull           = GPIO_NOPULL;
            HAL_GPIO_Init(GPIOA, &GPIO_Handle);
        }

        if((htkey->ChannelData[ucI].ChannelId >= 4)&&(htkey->ChannelData[ucI].ChannelId <= 5))          /*TKEY4-5 -> PD6-7*/
        {
            GPIO_Handle.Pin            = (uint16_t)(0x0001 << (htkey->ChannelData[ucI].ChannelId + 2));
            GPIO_Handle.Mode           = GPIO_MODE_ANALOG;
            GPIO_Handle.Pull           = GPIO_NOPULL;
            HAL_GPIO_Init(GPIOD, &GPIO_Handle);
        }

        if((htkey->ChannelData[ucI].ChannelId >= 6)&&(htkey->ChannelData[ucI].ChannelId <= 7))          /*TKEY6-7 -> PA14-15*/
        {
            GPIO_Handle.Pin            = (uint16_t)(0x0001 << (htkey->ChannelData[ucI].ChannelId + 8));
            GPIO_Handle.Mode           = GPIO_MODE_ANALOG;
            GPIO_Handle.Pull           = GPIO_NOPULL;
            HAL_GPIO_Init(GPIOA, &GPIO_Handle);
        }

        if((htkey->ChannelData[ucI].ChannelId >= 8)&&(htkey->ChannelData[ucI].ChannelId <= 10))          /*TKEY8-10 -> PC10-12*/
        {
            GPIO_Handle.Pin            = (uint16_t)(0x0001 << (htkey->ChannelData[ucI].ChannelId + 2));
            GPIO_Handle.Mode           = GPIO_MODE_ANALOG;
            GPIO_Handle.Pull           = GPIO_NOPULL;
            HAL_GPIO_Init(GPIOC, &GPIO_Handle);
        }

        if(htkey->ChannelData[ucI].ChannelId == 11)          /*TKEY11 -> PD2*/
        {
            GPIO_Handle.Pin            = (uint16_t)(0x0001 << (htkey->ChannelData[ucI].ChannelId - 9));
            GPIO_Handle.Mode           = GPIO_MODE_ANALOG;
            GPIO_Handle.Pull           = GPIO_NOPULL;
            HAL_GPIO_Init(GPIOD, &GPIO_Handle);
        }

        if((htkey->ChannelData[ucI].ChannelId >= 12)&&(htkey->ChannelData[ucI].ChannelId <= 15))          /*TKEY12-15 -> PB3-6*/
        {
            GPIO_Handle.Pin            = (uint16_t)(0x0001 << (htkey->ChannelData[ucI].ChannelId - 9));
            GPIO_Handle.Mode           = GPIO_MODE_ANALOG;
            GPIO_Handle.Pull           = GPIO_NOPULL;
            HAL_GPIO_Init(GPIOB, &GPIO_Handle);
        }
    }

    if(htkey->Init.ShieldEn == TKEY_CR_SCAN_ENABLE)  /*TKEY_SHIELD -> PB7*/
    {
        GPIO_Handle.Pin            = GPIO_PIN_7;
        GPIO_Handle.Mode           = GPIO_MODE_ANALOG;
        GPIO_Handle.Pull           = GPIO_NOPULL;
        HAL_GPIO_Init(GPIOB, &GPIO_Handle);
    }

    /*Set the Cs(PB9) and Creg(PB8) pin to analog*/
    GPIO_Handle.Pin            = GPIO_PIN_8 | GPIO_PIN_9;
    GPIO_Handle.Mode           = GPIO_MODE_ANALOG;
    GPIO_Handle.Pull           = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOB, &GPIO_Handle);

    SCU->RCHCR |= ((15 << 17) | SCU_RCHCR_RC4M_EN); //RC4M TRIM and Enable.
    while((SCU->RCHCR & SCU_RCHCR_RC4MRDY) == 0x00);
    SCU->CCR2 |= SCU_CCR2_TKSCLK_SEL;  //TKEY use the RC4M as clock.

    System_Enable_RC32K(); //RC32K Enable.

    System_Module_Reset(RST_TKEY);

    /* Enable TKEY Clock */
    System_Module_Enable(EN_TKEY);

    /* Disable TKEY Interrupt */
    NVIC_ClearPendingIRQ(TKEY_IRQn);
    NVIC_DisableIRQ(TKEY_IRQn);
}

/************************************************************************
 * function   : HAL_TKEY_Start
 * Description: TKEY start to scan
 * input      : htkey : TKEY handle
 * return     : HAL_OK: success; HAL_ERROR: failed.
************************************************************************/
HAL_StatusTypeDef HAL_TKEY_Start(TKEY_HandleTypeDef* htkey)
{
    /* Check the parameters */
    if(!IS_TKEY_ALL_INSTANCE(htkey->Instance)) return HAL_ERROR;
    /*Enable the Tkey scan*/
    SET_BIT(htkey->Instance->CR , TKEY_CR_SCAN);

    /*Start the Tkey scan*/
    SET_BIT(htkey->Instance->CR, TKEY_CR_START);

    /* Return function status */
    return HAL_OK;
}

/************************************************************************
 * function   : HAL_TKEY_Stop
 * Description: TKEY stop the scan
 * input      : htkey : TKEY handle
 * return     : HAL_OK: success; HAL_ERROR: failed.
 ************************************************************************/
HAL_StatusTypeDef HAL_TKEY_Stop(TKEY_HandleTypeDef* htkey)
{
    /* Check the parameters */
    if(!IS_TKEY_ALL_INSTANCE(htkey->Instance)) return HAL_ERROR;

    /*Check if the Tkey scan is busy*/
    while(READ_BIT(htkey->Instance->ISR , TKEY_ISR_BUSY)){}

    /*disable the Tkey scan*/
    CLEAR_BIT(htkey->Instance->CR, TKEY_CR_SCAN);

    /* Return function status */
    return HAL_OK;
}

/************************************************************************
 * function   : HAL_TKEY_Suspend
 * Description: Set the sleep parameters.
 * input      : htkey : TKEY handle
 * return     : HAL_OK: success; HAL_ERROR: failed.
 ************************************************************************/
HAL_StatusTypeDef HAL_TKEY_Suspend(TKEY_HandleTypeDef* htkey)
{
    uint8_t ucI;
    __IO uint32_t *gu32RegTemp;

    /* Check the parameters */
    if(!IS_TKEY_ALL_INSTANCE(htkey->Instance)) return HAL_ERROR;

    /* Disable TKEY Interrupt */
    NVIC_ClearPendingIRQ(TKEY_IRQn);
    NVIC_DisableIRQ(TKEY_IRQn);

    u32Regbackup = htkey->Instance->SMPR;
    MODIFY_REG(htkey->Instance->SMPR, TKEY_SMPR_SWT_MASK, TKEY_SMPR_SWT(htkey->ScanPara.SleepScanWaitTime));   //Slow down the scan speed.

    SET_BIT(htkey->Instance->CR, TKEY_CR_SLEEP);      //Enable the wakeup.

    SET_BIT(htkey->Instance->IER, TKEY_IER_WAKEUPIE); //Enable the wakeup interrupt.

    gu32RegTemp = &htkey->Instance->CH0;
    for(ucI = 0; htkey->ChannelData[ucI].ChannelId != 0xFFFF; ucI++)
    {
        /* Write the base data and the wakeup threshold.*/
        *(gu32RegTemp + htkey->ChannelData[ucI].ChannelId) = htkey->ChannelData[ucI].Tkey_Data->Data;
        *(gu32RegTemp - 16 + htkey->ChannelData[ucI].ChannelId) = htkey->ChannelData[ucI].Tkey_RefPara->RefDelta*htkey->ScanPara.WakeUpThRatio;
    }

    /* Enable TKEY Interrupt */
    NVIC_ClearPendingIRQ(TKEY_IRQn);
    NVIC_EnableIRQ(TKEY_IRQn);

    /*Start the Tkey scan*/
    SET_BIT(htkey->Instance->CR, TKEY_CR_START);

    return HAL_OK;
}

/************************************************************************
 * function   : HAL_TKEY_Resume
 * Description: Resume the wakeup parameters.
 * input      : htkey : TKEY handle
 * return     : HAL_OK: success; HAL_ERROR: failed.
 ************************************************************************/
HAL_StatusTypeDef HAL_TKEY_Resume(TKEY_HandleTypeDef* htkey)
{
    /* Disable TKEY Interrupt */
    NVIC_ClearPendingIRQ(TKEY_IRQn);
    NVIC_DisableIRQ(TKEY_IRQn);

    CLEAR_BIT(htkey->Instance->IER, TKEY_IER_WAKEUPIE); //Disable the wakeup interrupt.

    htkey->Instance->SMPR = u32Regbackup;     //Use the backup scan value.
    CLEAR_BIT(htkey->Instance->CR, TKEY_CR_SLEEP);      //Disable the wakeup.

    return HAL_OK;
}

/************************************************************************
 * function   : HAL_TKEY_ReadNr
 * Description: Read the count number of the Cr.
 * input      : htkey : TKEY handle
 * return     : HAL_OK: success; HAL_ERROR: failed.
 ************************************************************************/
HAL_StatusTypeDef HAL_TKEY_ReadNr(TKEY_HandleTypeDef* htkey)
{
    HAL_StatusTypeDef Status = HAL_OK;
    /* Check the parameters */
    if(!IS_TKEY_ALL_INSTANCE(htkey->Instance)) return HAL_ERROR;

    /*Check if the Tkey scan is busy*/
    while(READ_BIT(htkey->Instance->ISR , TKEY_ISR_BUSY)){}

    /*Set the CREN, enabel the internal channel scan*/
    SET_BIT(htkey->Instance->CR, TKEY_CR_CREN);

    /*Clear the SLEEP, use normal scan mode*/
    CLEAR_BIT(htkey->Instance->CR, TKEY_CR_SLEEP);

    /* Clear all flag */
    htkey->Instance->ISR = 0x07;
    HAL_TKEY_Start(htkey);

    while(!READ_BIT(htkey->Instance->ISR, TKEY_ISR_EOC))
    {
        if(!READ_BIT(htkey->Instance->ISR, TKEY_ISR_BUSY)) //Some times will stop.restart.
            SET_BIT(htkey->Instance->CR, TKEY_CR_START);

        if(READ_BIT(htkey->Instance->ISR, TKEY_ISR_TIMEOUT))
        {
            SET_BIT(htkey->Instance->ISR, TKEY_ISR_TIMEOUT); //Clear the timeout flag
            Status = HAL_ERROR;
            break;
        }
    }

    htkey->Instance->ISR = TKEY_ISR_EOC;

    htkey->NrData = htkey->Instance->DR;

    return Status;
}

/************************************************************************
 * function   : HAL_TKEY_ReadChannelData
 * Description: Read the count number of the all channels.
 * input      : htkey : TKEY handle
 * return     : HAL_OK: success; HAL_ERROR: failed.
 ************************************************************************/
HAL_StatusTypeDef HAL_TKEY_ReadChannelData(TKEY_HandleTypeDef* htkey)
{
    uint8_t ucI;
    __IO uint32_t *gu32RegTemp;

    htkey->Instance->ISR = 0x07;
    if(!(htkey->Instance->CR & TKEY_CR_CONT))
    {
        /*Start the Tkey scan*/
        SET_BIT(htkey->Instance->CR, TKEY_CR_START);
    }

    while(!READ_BIT(htkey->Instance->ISR, TKEY_ISR_EOC))
    {
        if(!READ_BIT(htkey->Instance->ISR, TKEY_ISR_BUSY)) //Some times will stop.restart.
            SET_BIT(htkey->Instance->CR, TKEY_CR_START);

        if(READ_BIT(htkey->Instance->ISR, TKEY_ISR_TIMEOUT))
        {
            SET_BIT(htkey->Instance->ISR, TKEY_ISR_TIMEOUT); //Clear the timeout flag
            return HAL_ERROR;
        }
    }

    htkey->Instance->ISR = TKEY_ISR_EOC;

    gu32RegTemp = &htkey->Instance->CH0;
    for(ucI = 0; htkey->ChannelData[ucI].ChannelId != 0xFFFF; ucI++)
    {
        /* Read the data and calculate the delta.*/
        htkey->ChannelData[ucI].Tkey_Data->Data = *(gu32RegTemp + htkey->ChannelData[ucI].ChannelId);
        htkey->ChannelData[ucI].Tkey_Data->Delta = (INT32)htkey->ChannelData[ucI].Tkey_Data->RefData - (INT32)htkey->ChannelData[ucI].Tkey_Data->Data;
    }

    return HAL_OK;
}

/************************************************************************
 * function   : HAL_TKEY_ReadAllNx
 * Description: Read the count number of the all channels first time, and start the scan.
 * input      : htkey : TKEY handle
 * return     : HAL_OK: success; HAL_ERROR: failed.
 ************************************************************************/
HAL_StatusTypeDef HAL_TKEY_ReadAllNx(TKEY_HandleTypeDef* htkey)
{
    uint8_t ucI;

    /* Check the parameters */
    if(!IS_TKEY_ALL_INSTANCE(htkey->Instance)) return HAL_ERROR;

    /*Clear the CREN, disable the internal channel scan*/
    CLEAR_BIT(htkey->Instance->CR, TKEY_CR_CREN);

    for(ucI = 0; htkey->ChannelData[ucI].ChannelId != 0xFFFF; ucI++)
    {
        /*Enable the channels*/
        htkey->Instance->CXSELR |= (1<<htkey->ChannelData[ucI].ChannelId);
        /*If the channel need compensation*/
        if(htkey->ChannelData[ucI].Tkey_RefPara->CrSelect)
            htkey->Instance->CRSELR |= (1<<htkey->ChannelData[ucI].ChannelId);
    }
    /*Clear the SLEEP, use normal scan mode*/
    CLEAR_BIT(htkey->Instance->CR, TKEY_CR_SLEEP);

    HAL_TKEY_Start(htkey);

    HAL_TKEY_ReadChannelData(htkey);

    return HAL_OK;
}

/************************************************************************
 * function   : HAL_TKEY_StartUpStateProcess
 * Description: Init the TKEY channel data.
 * input      : ChannelData : TKEY channel data handle point to TKEY_ChannelDataDef.
 * return     : None
 ************************************************************************/
void HAL_TKEY_StartUpStateProcess(const TKEY_ChannelDataDef *ChannelData)
{
    ChannelData->Tkey_Data->DebIn = ChannelData->Tkey_RefPara->DebIn;
    ChannelData->Tkey_Data->DebOut = ChannelData->Tkey_RefPara->DebOut;
    ChannelData->Tkey_Data->StateId = TKEY_STATEID_RELEASE;
}

/************************************************************************
 * function   : HAL_TKEY_DebDetectStateProcess
 * Description: The TKEY detect action state process.
 * input      : ChannelData : TKEY channel data handle point to TKEY_ChannelDataDef.
 * return     : None
 ************************************************************************/
void HAL_TKEY_DebDetectStateProcess(const TKEY_ChannelDataDef *ChannelData)
{
    if (ChannelData->Tkey_Data->Delta >= ChannelData->Tkey_RefPara->DetectInTH)
    {
        if (ChannelData->Tkey_Data->DebIn > 0)
        {
            ChannelData->Tkey_Data->DebIn--;
        }
        if (ChannelData->Tkey_Data->DebIn == 0)
        {
            ChannelData->Tkey_Data->StateId = TKEY_STATEID_DETECT;
            ChannelData->Tkey_Data->DebOut = ChannelData->Tkey_RefPara->DebOut;
        }
      // else stay in Debounce Detect
    }
    else
    {
        ChannelData->Tkey_Data->StateId = TKEY_STATEID_RELEASE;
        ChannelData->Tkey_Data->DebIn = ChannelData->Tkey_RefPara->DebIn;
    }
}

/************************************************************************
 * function   : HAL_TKEY_DebReleaseDetectStateProcess
 * Description: The TKEY detect to release state process.
 * input      : ChannelData : TKEY channel data handle point to TKEY_ChannelDataDef.
 * return     : None
 ************************************************************************/
void HAL_TKEY_DebReleaseDetectStateProcess(const TKEY_ChannelDataDef *ChannelData)
{
    if (ChannelData->Tkey_Data->Delta >= ChannelData->Tkey_RefPara->DetectOutTH)
    {
        ChannelData->Tkey_Data->StateId = TKEY_STATEID_DETECT;
        ChannelData->Tkey_Data->DebOut = ChannelData->Tkey_RefPara->DebOut;
    }
    else
    {
        if (ChannelData->Tkey_Data->DebOut > 0)
        {
            ChannelData->Tkey_Data->DebOut--;
        }
        if (ChannelData->Tkey_Data->DebOut == 0)
        {
            ChannelData->Tkey_Data->StateId = TKEY_STATEID_RELEASE;
            ChannelData->Tkey_Data->DebIn = ChannelData->Tkey_RefPara->DebIn;
        }
    }
}

/************************************************************************
 * function   : HAL_TKEY_Init
 * Description: Init the TKEY.
 * input      : htkey : TKEY handle
 * return     : HAL_OK: success; HAL_ERROR: failed.
 ************************************************************************/
HAL_StatusTypeDef HAL_TKEY_Init(TKEY_HandleTypeDef* htkey)
{
    uint8_t ucI;
    uint32_t u32RegTemp;

    /* Check the TKEY handle allocation */
    if (htkey == NULL)
    {
        return HAL_ERROR;
    }

    /* Check the parameters */
    if(!IS_TKEY_ALL_INSTANCE(htkey->Instance)) return HAL_ERROR;
    if(!IS_TKEY_ALL_VKEYSEL(htkey->Init.VkeySel)) return HAL_ERROR;
    if(!IS_TKEY_ALL_VREFSEL(htkey->Init.VrefSel)) return HAL_ERROR;
    if(!IS_TKEY_ALL_SHIELDEN(htkey->Init.ShieldEn)) return HAL_ERROR;
    if(!IS_TKEY_ALL_SCANWAITTIME(htkey->Init.ScanWaitTime)) return HAL_ERROR;
    if(!IS_TKEY_ALL_CSDISCHARGETIME(htkey->Init.CsDisChargeTime)) return HAL_ERROR;
    if(!IS_TKEY_ALL_SW1(htkey->Init.Sw1H)) return HAL_ERROR;
    if(!IS_TKEY_ALL_SW1(htkey->Init.Sw1L)) return HAL_ERROR;

    /* Init the low level hardware : GPIO, CLOCK, NVIC, DMA */
    HAL_TKEY_MspInit(htkey);

    /*Check if the Tkey scan is busy*/
    while(READ_BIT(htkey->Instance->ISR , TKEY_ISR_BUSY)){}

    HAL_TKEY_Stop(htkey);

    /*Config the Tkey control register*/
    u32RegTemp =  ((TKEY_CR_CHARGESEL_LDO << 11)& TKEY_CR_CHARGESEL) | \
                  (TKEY_CR_VKEYSEL(htkey->Init.VkeySel) & TKEY_CR_VKEYSEL_MASK) | \
                  (TKEY_CR_VREFSEL(htkey->Init.VrefSel) & TKEY_CR_VREFSEL_MASK) | \
                  ((TKEY_CR_SPREAD_DISABLE << 5)& TKEY_CR_SPREAD) | \
                  ((TKEY_CR_CONT_ENABLE << 4)& TKEY_CR_CONT) | \
                  ((htkey->Init.ShieldEn << 3)& TKEY_CR_SHIELDEN);

    WRITE_REG(htkey->Instance->CR,u32RegTemp);

     /*Config the Tkey TKEY_SMPR register*/
    u32RegTemp = (TKEY_SMPR_SWT(htkey->Init.ScanWaitTime) & TKEY_SMPR_SWT_MASK) | \
                 (TKEY_SMPR_CST(htkey->Init.CsDisChargeTime) & TKEY_SMPR_CST_MASK);

    WRITE_REG(htkey->Instance->SMPR,u32RegTemp);

     /*Config the Tkey TKEY_SOFR register*/
    u32RegTemp = (TKEY_SOFR_SW1H(htkey->Init.Sw1H) & TKEY_SOFR_SW1H_MASK) | \
                 (TKEY_SOFR_SW1L(htkey->Init.Sw1L) & TKEY_SOFR_SW1L_MASK);

    WRITE_REG(htkey->Instance->SOFR,u32RegTemp);

    HAL_TKEY_ReadNr(htkey);

    for(ucI = 0; htkey->ChannelData[ucI].ChannelId != 0xFFFF; ucI++)
    {
        /* if need calibrate , read the data to the reference data.*/
        htkey->ChannelData[ucI].Tkey_Data->StateId = TKEY_STATEID_STARTUP;
        htkey->ChannelData[ucI].Tkey_Data->ReferenceFlag = 1;

        htkey->ChannelData[ucI].Tkey_RefPara->DetectInTH = htkey->ChannelData[ucI].Tkey_RefPara->RefDelta*htkey->ScanPara.DetectInThRatio;
        htkey->ChannelData[ucI].Tkey_RefPara->DetectOutTH =  htkey->ChannelData[ucI].Tkey_RefPara->RefDelta*htkey->ScanPara.DetectOutThRatio;
        htkey->ChannelData[ucI].Tkey_RefPara->CalibratTH =   htkey->ChannelData[ucI].Tkey_RefPara->RefDelta*htkey->ScanPara.CalibratThRatio;
    }

    HAL_TKEY_ReadAllNx(htkey);
    /* Clear all keys*/
    htkey->ChannelDetected = 0;
    htkey->ChannelDetectedNum  = 0;

    return  HAL_OK;
}

/************************************************************************
 * function   : HAL_TKEY_DetectProcess
 * Description: TKEY detect main process.
 * input      : htkey : TKEY handle
 * return     : None.
 ************************************************************************/
void HAL_TKEY_DetectProcess(TKEY_HandleTypeDef* htkey)
{
    uint8_t ucI;
    const TKEY_ChannelDataDef *ChannelData;

    HAL_TKEY_ReadChannelData(htkey);

    for(ucI = 0; htkey->ChannelData[ucI].ChannelId != 0xFFFF; ucI++)
    {
        ChannelData = &htkey->ChannelData[ucI];
        switch(ChannelData->Tkey_Data->StateId)
        {
            case TKEY_STATEID_STARTUP :
                HAL_TKEY_StartUpStateProcess(ChannelData);
                break;
            case TKEY_STATEID_RELEASE :
                HAL_TKEY_DebDetectStateProcess(ChannelData);
                break;
            case TKEY_STATEID_DETECT :
                if(htkey->ScanTimer >= htkey->ScanPara.DetectingTimeout)
                    htkey->ChannelDetecting |= (1 << ChannelData->ChannelId);
                HAL_TKEY_DebReleaseDetectStateProcess(ChannelData);
                if(ChannelData->Tkey_Data->StateId == TKEY_STATEID_RELEASE)
                {
                    htkey->ChannelDetected |= (1 << ChannelData->ChannelId);
                    htkey->ChannelValue = ChannelData->ChannelId;
                    htkey->ChannelDetectedNum++;
                    htkey->ChannelDetecting &= ~(1 << ChannelData->ChannelId);
                    htkey->ScanTimer = 0;    //Reset the timer when detect Key release.
                }
                break;
            default :
                break;
        }

        if((htkey->ChannelData[ucI].Tkey_Data->Delta > htkey->ChannelData[ucI].Tkey_RefPara->CalibratTH) \
            ||(htkey->ChannelData[ucI].Tkey_Data->Delta < (-htkey->ChannelData[ucI].Tkey_RefPara->CalibratTH)))
        {
            htkey->ScanTimer++;
            if(htkey->ScanTimer >= htkey->ScanPara.CalibratTimeout)
            {
                htkey->CalFlag = 1;     //Need calibrate.
                htkey->ScanTimer = 0;
                htkey->ChannelDetected = 0;
                htkey->ChannelDetecting = 0;
                break;
           }
        }
        if((htkey->ChannelData[ucI].Tkey_Data->Delta > 2*htkey->ChannelData[ucI].Tkey_RefPara->RefDelta) \
            ||(htkey->ChannelData[ucI].Tkey_Data->Delta < (-htkey->ChannelData[ucI].Tkey_RefPara->RefDelta)))
        {
            htkey->CalFlag = 1;     //Need calibrate.
            htkey->ScanTimer = 0;
            htkey->ChannelDetected = 0;
            htkey->ChannelDetecting = 0;
            break;
        }

        if(htkey->ChannelDetecting)  //If don't need detecting.
        {
            htkey->ChannelDetecting = 0;
            htkey->CalFlag = 1;     //Need calibrate.
        }
    }
}

/************************************************************************
 * function   : HAL_TKEY_Calibrate_RefData
 * Description: TKEY Calibrate the base Reference Data.
 * input      : htkey : TKEY handle
                CalTimes: The calibrat times.
 * return     : None
 ************************************************************************/
void HAL_TKEY_Calibrate_RefData(TKEY_HandleTypeDef* htkey, uint8_t CalTimes)
{
    uint8_t ucI,ucJ;
    uint32_t sum[16];

    memset(sum,0,sizeof(sum));
    for(ucJ=0; ucJ < CalTimes; ucJ++)
    {
        HAL_TKEY_ReadChannelData(htkey);
        for(ucI = 0; htkey->ChannelData[ucI].ChannelId != 0xFFFF; ucI++)
        {
            if(htkey->ChannelData[ucI].Tkey_Data->Data)
            {
                sum[ucI] +=  htkey->ChannelData[ucI].Tkey_Data->Data;
            }
            if(ucJ == (CalTimes-1))
            {
                htkey->ChannelData[ucI].Tkey_Data->RefData =  sum[ucI]/CalTimes;
            }
        }
    }
}