1 /*
2   ******************************************************************************
3   * @file    HAL_TKEY.c
4   * @version V1.0.0
5   * @date    2020
6   * @brief   DMA HAL module driver.
7   *          This file provides firmware functions to manage the following
8   *          functionalities of the Direct Memory Access (DMA) peripheral:
9   *           @ Initialization and de-initialization functions
10   *           @ IO operation functions
11   ******************************************************************************
12 */
13 #include "ACM32Fxx_HAL.h"
14 
15 __IO uint32_t u32Regbackup;
16 
17 /************************************************************************
18  * function   : TKEY_IRQHandler
19  * Description: tkey interrupt service routine.
20  * input :
21  *         none
22  * return: none
23  ************************************************************************/
TKEY_IRQHandler(void)24 void TKEY_IRQHandler(void)
25 {
26     TKEY->ISR = 0xFFF;
27 }
28 
29 /************************************************************************
30  * function   : HAL_TKEY_MspInit
31  * Description: Init the hardware, GPIO and clock, etc.
32  * input      : htkey : TKEY handle
33  * return     : none
34  ************************************************************************/
HAL_TKEY_MspInit(TKEY_HandleTypeDef * htkey)35 void HAL_TKEY_MspInit(TKEY_HandleTypeDef* htkey)
36 {
37     /* For Example */
38     GPIO_InitTypeDef GPIO_Handle;
39     uint8_t ucI;
40 
41     for(ucI = 0; htkey->ChannelData[ucI].ChannelId != 0xFFFF; ucI++)
42     {
43         /* TKEY0 GPIO inition*/
44         if(htkey->ChannelData[ucI].ChannelId < 4)             /*TKEY0-3 -> PA10-13*/
45         {
46             GPIO_Handle.Pin            = (uint16_t)(0x0001 << (htkey->ChannelData[ucI].ChannelId + 10));
47             GPIO_Handle.Mode           = GPIO_MODE_ANALOG;
48             GPIO_Handle.Pull           = GPIO_NOPULL;
49             HAL_GPIO_Init(GPIOA, &GPIO_Handle);
50         }
51 
52         if((htkey->ChannelData[ucI].ChannelId >= 4)&&(htkey->ChannelData[ucI].ChannelId <= 5))          /*TKEY4-5 -> PD6-7*/
53         {
54             GPIO_Handle.Pin            = (uint16_t)(0x0001 << (htkey->ChannelData[ucI].ChannelId + 2));
55             GPIO_Handle.Mode           = GPIO_MODE_ANALOG;
56             GPIO_Handle.Pull           = GPIO_NOPULL;
57             HAL_GPIO_Init(GPIOD, &GPIO_Handle);
58         }
59 
60         if((htkey->ChannelData[ucI].ChannelId >= 6)&&(htkey->ChannelData[ucI].ChannelId <= 7))          /*TKEY6-7 -> PA14-15*/
61         {
62             GPIO_Handle.Pin            = (uint16_t)(0x0001 << (htkey->ChannelData[ucI].ChannelId + 8));
63             GPIO_Handle.Mode           = GPIO_MODE_ANALOG;
64             GPIO_Handle.Pull           = GPIO_NOPULL;
65             HAL_GPIO_Init(GPIOA, &GPIO_Handle);
66         }
67 
68         if((htkey->ChannelData[ucI].ChannelId >= 8)&&(htkey->ChannelData[ucI].ChannelId <= 10))          /*TKEY8-10 -> PC10-12*/
69         {
70             GPIO_Handle.Pin            = (uint16_t)(0x0001 << (htkey->ChannelData[ucI].ChannelId + 2));
71             GPIO_Handle.Mode           = GPIO_MODE_ANALOG;
72             GPIO_Handle.Pull           = GPIO_NOPULL;
73             HAL_GPIO_Init(GPIOC, &GPIO_Handle);
74         }
75 
76         if(htkey->ChannelData[ucI].ChannelId == 11)          /*TKEY11 -> PD2*/
77         {
78             GPIO_Handle.Pin            = (uint16_t)(0x0001 << (htkey->ChannelData[ucI].ChannelId - 9));
79             GPIO_Handle.Mode           = GPIO_MODE_ANALOG;
80             GPIO_Handle.Pull           = GPIO_NOPULL;
81             HAL_GPIO_Init(GPIOD, &GPIO_Handle);
82         }
83 
84         if((htkey->ChannelData[ucI].ChannelId >= 12)&&(htkey->ChannelData[ucI].ChannelId <= 15))          /*TKEY12-15 -> PB3-6*/
85         {
86             GPIO_Handle.Pin            = (uint16_t)(0x0001 << (htkey->ChannelData[ucI].ChannelId - 9));
87             GPIO_Handle.Mode           = GPIO_MODE_ANALOG;
88             GPIO_Handle.Pull           = GPIO_NOPULL;
89             HAL_GPIO_Init(GPIOB, &GPIO_Handle);
90         }
91     }
92 
93     if(htkey->Init.ShieldEn == TKEY_CR_SCAN_ENABLE)  /*TKEY_SHIELD -> PB7*/
94     {
95         GPIO_Handle.Pin            = GPIO_PIN_7;
96         GPIO_Handle.Mode           = GPIO_MODE_ANALOG;
97         GPIO_Handle.Pull           = GPIO_NOPULL;
98         HAL_GPIO_Init(GPIOB, &GPIO_Handle);
99     }
100 
101     /*Set the Cs(PB9) and Creg(PB8) pin to analog*/
102     GPIO_Handle.Pin            = GPIO_PIN_8 | GPIO_PIN_9;
103     GPIO_Handle.Mode           = GPIO_MODE_ANALOG;
104     GPIO_Handle.Pull           = GPIO_NOPULL;
105     HAL_GPIO_Init(GPIOB, &GPIO_Handle);
106 
107     SCU->RCHCR |= ((15 << 17) | SCU_RCHCR_RC4M_EN); //RC4M TRIM and Enable.
108     while((SCU->RCHCR & SCU_RCHCR_RC4MRDY) == 0x00);
109     SCU->CCR2 |= SCU_CCR2_TKSCLK_SEL;  //TKEY use the RC4M as clock.
110 
111     System_Enable_RC32K(); //RC32K Enable.
112 
113     System_Module_Reset(RST_TKEY);
114 
115     /* Enable TKEY Clock */
116     System_Module_Enable(EN_TKEY);
117 
118     /* Disable TKEY Interrupt */
119     NVIC_ClearPendingIRQ(TKEY_IRQn);
120     NVIC_DisableIRQ(TKEY_IRQn);
121 }
122 
123 /************************************************************************
124  * function   : HAL_TKEY_Start
125  * Description: TKEY start to scan
126  * input      : htkey : TKEY handle
127  * return     : HAL_OK: success; HAL_ERROR: failed.
128 ************************************************************************/
HAL_TKEY_Start(TKEY_HandleTypeDef * htkey)129 HAL_StatusTypeDef HAL_TKEY_Start(TKEY_HandleTypeDef* htkey)
130 {
131     /* Check the parameters */
132     if(!IS_TKEY_ALL_INSTANCE(htkey->Instance)) return HAL_ERROR;
133     /*Enable the Tkey scan*/
134     SET_BIT(htkey->Instance->CR , TKEY_CR_SCAN);
135 
136     /*Start the Tkey scan*/
137     SET_BIT(htkey->Instance->CR, TKEY_CR_START);
138 
139     /* Return function status */
140     return HAL_OK;
141 }
142 
143 /************************************************************************
144  * function   : HAL_TKEY_Stop
145  * Description: TKEY stop the scan
146  * input      : htkey : TKEY handle
147  * return     : HAL_OK: success; HAL_ERROR: failed.
148  ************************************************************************/
HAL_TKEY_Stop(TKEY_HandleTypeDef * htkey)149 HAL_StatusTypeDef HAL_TKEY_Stop(TKEY_HandleTypeDef* htkey)
150 {
151     /* Check the parameters */
152     if(!IS_TKEY_ALL_INSTANCE(htkey->Instance)) return HAL_ERROR;
153 
154     /*Check if the Tkey scan is busy*/
155     while(READ_BIT(htkey->Instance->ISR , TKEY_ISR_BUSY)){}
156 
157     /*disable the Tkey scan*/
158     CLEAR_BIT(htkey->Instance->CR, TKEY_CR_SCAN);
159 
160     /* Return function status */
161     return HAL_OK;
162 }
163 
164 /************************************************************************
165  * function   : HAL_TKEY_Suspend
166  * Description: Set the sleep parameters.
167  * input      : htkey : TKEY handle
168  * return     : HAL_OK: success; HAL_ERROR: failed.
169  ************************************************************************/
HAL_TKEY_Suspend(TKEY_HandleTypeDef * htkey)170 HAL_StatusTypeDef HAL_TKEY_Suspend(TKEY_HandleTypeDef* htkey)
171 {
172     uint8_t ucI;
173     __IO uint32_t *gu32RegTemp;
174 
175     /* Check the parameters */
176     if(!IS_TKEY_ALL_INSTANCE(htkey->Instance)) return HAL_ERROR;
177 
178     /* Disable TKEY Interrupt */
179     NVIC_ClearPendingIRQ(TKEY_IRQn);
180     NVIC_DisableIRQ(TKEY_IRQn);
181 
182     u32Regbackup = htkey->Instance->SMPR;
183     MODIFY_REG(htkey->Instance->SMPR, TKEY_SMPR_SWT_MASK, TKEY_SMPR_SWT(htkey->ScanPara.SleepScanWaitTime));   //Slow down the scan speed.
184 
185     SET_BIT(htkey->Instance->CR, TKEY_CR_SLEEP);      //Enable the wakeup.
186 
187     SET_BIT(htkey->Instance->IER, TKEY_IER_WAKEUPIE); //Enable the wakeup interrupt.
188 
189     gu32RegTemp = &htkey->Instance->CH0;
190     for(ucI = 0; htkey->ChannelData[ucI].ChannelId != 0xFFFF; ucI++)
191     {
192         /* Write the base data and the wakeup threshold.*/
193         *(gu32RegTemp + htkey->ChannelData[ucI].ChannelId) = htkey->ChannelData[ucI].Tkey_Data->Data;
194         *(gu32RegTemp - 16 + htkey->ChannelData[ucI].ChannelId) = htkey->ChannelData[ucI].Tkey_RefPara->RefDelta*htkey->ScanPara.WakeUpThRatio;
195     }
196 
197     /* Enable TKEY Interrupt */
198     NVIC_ClearPendingIRQ(TKEY_IRQn);
199     NVIC_EnableIRQ(TKEY_IRQn);
200 
201     /*Start the Tkey scan*/
202     SET_BIT(htkey->Instance->CR, TKEY_CR_START);
203 
204     return HAL_OK;
205 }
206 
207 /************************************************************************
208  * function   : HAL_TKEY_Resume
209  * Description: Resume the wakeup parameters.
210  * input      : htkey : TKEY handle
211  * return     : HAL_OK: success; HAL_ERROR: failed.
212  ************************************************************************/
HAL_TKEY_Resume(TKEY_HandleTypeDef * htkey)213 HAL_StatusTypeDef HAL_TKEY_Resume(TKEY_HandleTypeDef* htkey)
214 {
215     /* Disable TKEY Interrupt */
216     NVIC_ClearPendingIRQ(TKEY_IRQn);
217     NVIC_DisableIRQ(TKEY_IRQn);
218 
219     CLEAR_BIT(htkey->Instance->IER, TKEY_IER_WAKEUPIE); //Disable the wakeup interrupt.
220 
221     htkey->Instance->SMPR = u32Regbackup;     //Use the backup scan value.
222     CLEAR_BIT(htkey->Instance->CR, TKEY_CR_SLEEP);      //Disable the wakeup.
223 
224     return HAL_OK;
225 }
226 
227 /************************************************************************
228  * function   : HAL_TKEY_ReadNr
229  * Description: Read the count number of the Cr.
230  * input      : htkey : TKEY handle
231  * return     : HAL_OK: success; HAL_ERROR: failed.
232  ************************************************************************/
HAL_TKEY_ReadNr(TKEY_HandleTypeDef * htkey)233 HAL_StatusTypeDef HAL_TKEY_ReadNr(TKEY_HandleTypeDef* htkey)
234 {
235     HAL_StatusTypeDef Status = HAL_OK;
236     /* Check the parameters */
237     if(!IS_TKEY_ALL_INSTANCE(htkey->Instance)) return HAL_ERROR;
238 
239     /*Check if the Tkey scan is busy*/
240     while(READ_BIT(htkey->Instance->ISR , TKEY_ISR_BUSY)){}
241 
242     /*Set the CREN, enabel the internal channel scan*/
243     SET_BIT(htkey->Instance->CR, TKEY_CR_CREN);
244 
245     /*Clear the SLEEP, use normal scan mode*/
246     CLEAR_BIT(htkey->Instance->CR, TKEY_CR_SLEEP);
247 
248     /* Clear all flag */
249     htkey->Instance->ISR = 0x07;
250     HAL_TKEY_Start(htkey);
251 
252     while(!READ_BIT(htkey->Instance->ISR, TKEY_ISR_EOC))
253     {
254         if(!READ_BIT(htkey->Instance->ISR, TKEY_ISR_BUSY)) //Some times will stop.restart.
255             SET_BIT(htkey->Instance->CR, TKEY_CR_START);
256 
257         if(READ_BIT(htkey->Instance->ISR, TKEY_ISR_TIMEOUT))
258         {
259             SET_BIT(htkey->Instance->ISR, TKEY_ISR_TIMEOUT); //Clear the timeout flag
260             Status = HAL_ERROR;
261             break;
262         }
263     }
264 
265     htkey->Instance->ISR = TKEY_ISR_EOC;
266 
267     htkey->NrData = htkey->Instance->DR;
268 
269     return Status;
270 }
271 
272 /************************************************************************
273  * function   : HAL_TKEY_ReadChannelData
274  * Description: Read the count number of the all channels.
275  * input      : htkey : TKEY handle
276  * return     : HAL_OK: success; HAL_ERROR: failed.
277  ************************************************************************/
HAL_TKEY_ReadChannelData(TKEY_HandleTypeDef * htkey)278 HAL_StatusTypeDef HAL_TKEY_ReadChannelData(TKEY_HandleTypeDef* htkey)
279 {
280     uint8_t ucI;
281     __IO uint32_t *gu32RegTemp;
282 
283     htkey->Instance->ISR = 0x07;
284     if(!(htkey->Instance->CR & TKEY_CR_CONT))
285     {
286         /*Start the Tkey scan*/
287         SET_BIT(htkey->Instance->CR, TKEY_CR_START);
288     }
289 
290     while(!READ_BIT(htkey->Instance->ISR, TKEY_ISR_EOC))
291     {
292         if(!READ_BIT(htkey->Instance->ISR, TKEY_ISR_BUSY)) //Some times will stop.restart.
293             SET_BIT(htkey->Instance->CR, TKEY_CR_START);
294 
295         if(READ_BIT(htkey->Instance->ISR, TKEY_ISR_TIMEOUT))
296         {
297             SET_BIT(htkey->Instance->ISR, TKEY_ISR_TIMEOUT); //Clear the timeout flag
298             return HAL_ERROR;
299         }
300     }
301 
302     htkey->Instance->ISR = TKEY_ISR_EOC;
303 
304     gu32RegTemp = &htkey->Instance->CH0;
305     for(ucI = 0; htkey->ChannelData[ucI].ChannelId != 0xFFFF; ucI++)
306     {
307         /* Read the data and calculate the delta.*/
308         htkey->ChannelData[ucI].Tkey_Data->Data = *(gu32RegTemp + htkey->ChannelData[ucI].ChannelId);
309         htkey->ChannelData[ucI].Tkey_Data->Delta = (INT32)htkey->ChannelData[ucI].Tkey_Data->RefData - (INT32)htkey->ChannelData[ucI].Tkey_Data->Data;
310     }
311 
312     return HAL_OK;
313 }
314 
315 /************************************************************************
316  * function   : HAL_TKEY_ReadAllNx
317  * Description: Read the count number of the all channels first time, and start the scan.
318  * input      : htkey : TKEY handle
319  * return     : HAL_OK: success; HAL_ERROR: failed.
320  ************************************************************************/
HAL_TKEY_ReadAllNx(TKEY_HandleTypeDef * htkey)321 HAL_StatusTypeDef HAL_TKEY_ReadAllNx(TKEY_HandleTypeDef* htkey)
322 {
323     uint8_t ucI;
324 
325     /* Check the parameters */
326     if(!IS_TKEY_ALL_INSTANCE(htkey->Instance)) return HAL_ERROR;
327 
328     /*Clear the CREN, disable the internal channel scan*/
329     CLEAR_BIT(htkey->Instance->CR, TKEY_CR_CREN);
330 
331     for(ucI = 0; htkey->ChannelData[ucI].ChannelId != 0xFFFF; ucI++)
332     {
333         /*Enable the channels*/
334         htkey->Instance->CXSELR |= (1<<htkey->ChannelData[ucI].ChannelId);
335         /*If the channel need compensation*/
336         if(htkey->ChannelData[ucI].Tkey_RefPara->CrSelect)
337             htkey->Instance->CRSELR |= (1<<htkey->ChannelData[ucI].ChannelId);
338     }
339     /*Clear the SLEEP, use normal scan mode*/
340     CLEAR_BIT(htkey->Instance->CR, TKEY_CR_SLEEP);
341 
342     HAL_TKEY_Start(htkey);
343 
344     HAL_TKEY_ReadChannelData(htkey);
345 
346     return HAL_OK;
347 }
348 
349 /************************************************************************
350  * function   : HAL_TKEY_StartUpStateProcess
351  * Description: Init the TKEY channel data.
352  * input      : ChannelData : TKEY channel data handle point to TKEY_ChannelDataDef.
353  * return     : None
354  ************************************************************************/
HAL_TKEY_StartUpStateProcess(const TKEY_ChannelDataDef * ChannelData)355 void HAL_TKEY_StartUpStateProcess(const TKEY_ChannelDataDef *ChannelData)
356 {
357     ChannelData->Tkey_Data->DebIn = ChannelData->Tkey_RefPara->DebIn;
358     ChannelData->Tkey_Data->DebOut = ChannelData->Tkey_RefPara->DebOut;
359     ChannelData->Tkey_Data->StateId = TKEY_STATEID_RELEASE;
360 }
361 
362 /************************************************************************
363  * function   : HAL_TKEY_DebDetectStateProcess
364  * Description: The TKEY detect action state process.
365  * input      : ChannelData : TKEY channel data handle point to TKEY_ChannelDataDef.
366  * return     : None
367  ************************************************************************/
HAL_TKEY_DebDetectStateProcess(const TKEY_ChannelDataDef * ChannelData)368 void HAL_TKEY_DebDetectStateProcess(const TKEY_ChannelDataDef *ChannelData)
369 {
370     if (ChannelData->Tkey_Data->Delta >= ChannelData->Tkey_RefPara->DetectInTH)
371     {
372         if (ChannelData->Tkey_Data->DebIn > 0)
373         {
374             ChannelData->Tkey_Data->DebIn--;
375         }
376         if (ChannelData->Tkey_Data->DebIn == 0)
377         {
378             ChannelData->Tkey_Data->StateId = TKEY_STATEID_DETECT;
379             ChannelData->Tkey_Data->DebOut = ChannelData->Tkey_RefPara->DebOut;
380         }
381       // else stay in Debounce Detect
382     }
383     else
384     {
385         ChannelData->Tkey_Data->StateId = TKEY_STATEID_RELEASE;
386         ChannelData->Tkey_Data->DebIn = ChannelData->Tkey_RefPara->DebIn;
387     }
388 }
389 
390 /************************************************************************
391  * function   : HAL_TKEY_DebReleaseDetectStateProcess
392  * Description: The TKEY detect to release state process.
393  * input      : ChannelData : TKEY channel data handle point to TKEY_ChannelDataDef.
394  * return     : None
395  ************************************************************************/
HAL_TKEY_DebReleaseDetectStateProcess(const TKEY_ChannelDataDef * ChannelData)396 void HAL_TKEY_DebReleaseDetectStateProcess(const TKEY_ChannelDataDef *ChannelData)
397 {
398     if (ChannelData->Tkey_Data->Delta >= ChannelData->Tkey_RefPara->DetectOutTH)
399     {
400         ChannelData->Tkey_Data->StateId = TKEY_STATEID_DETECT;
401         ChannelData->Tkey_Data->DebOut = ChannelData->Tkey_RefPara->DebOut;
402     }
403     else
404     {
405         if (ChannelData->Tkey_Data->DebOut > 0)
406         {
407             ChannelData->Tkey_Data->DebOut--;
408         }
409         if (ChannelData->Tkey_Data->DebOut == 0)
410         {
411             ChannelData->Tkey_Data->StateId = TKEY_STATEID_RELEASE;
412             ChannelData->Tkey_Data->DebIn = ChannelData->Tkey_RefPara->DebIn;
413         }
414     }
415 }
416 
417 /************************************************************************
418  * function   : HAL_TKEY_Init
419  * Description: Init the TKEY.
420  * input      : htkey : TKEY handle
421  * return     : HAL_OK: success; HAL_ERROR: failed.
422  ************************************************************************/
HAL_TKEY_Init(TKEY_HandleTypeDef * htkey)423 HAL_StatusTypeDef HAL_TKEY_Init(TKEY_HandleTypeDef* htkey)
424 {
425     uint8_t ucI;
426     uint32_t u32RegTemp;
427 
428     /* Check the TKEY handle allocation */
429     if (htkey == NULL)
430     {
431         return HAL_ERROR;
432     }
433 
434     /* Check the parameters */
435     if(!IS_TKEY_ALL_INSTANCE(htkey->Instance)) return HAL_ERROR;
436     if(!IS_TKEY_ALL_VKEYSEL(htkey->Init.VkeySel)) return HAL_ERROR;
437     if(!IS_TKEY_ALL_VREFSEL(htkey->Init.VrefSel)) return HAL_ERROR;
438     if(!IS_TKEY_ALL_SHIELDEN(htkey->Init.ShieldEn)) return HAL_ERROR;
439     if(!IS_TKEY_ALL_SCANWAITTIME(htkey->Init.ScanWaitTime)) return HAL_ERROR;
440     if(!IS_TKEY_ALL_CSDISCHARGETIME(htkey->Init.CsDisChargeTime)) return HAL_ERROR;
441     if(!IS_TKEY_ALL_SW1(htkey->Init.Sw1H)) return HAL_ERROR;
442     if(!IS_TKEY_ALL_SW1(htkey->Init.Sw1L)) return HAL_ERROR;
443 
444     /* Init the low level hardware : GPIO, CLOCK, NVIC, DMA */
445     HAL_TKEY_MspInit(htkey);
446 
447     /*Check if the Tkey scan is busy*/
448     while(READ_BIT(htkey->Instance->ISR , TKEY_ISR_BUSY)){}
449 
450     HAL_TKEY_Stop(htkey);
451 
452     /*Config the Tkey control register*/
453     u32RegTemp =  ((TKEY_CR_CHARGESEL_LDO << 11)& TKEY_CR_CHARGESEL) | \
454                   (TKEY_CR_VKEYSEL(htkey->Init.VkeySel) & TKEY_CR_VKEYSEL_MASK) | \
455                   (TKEY_CR_VREFSEL(htkey->Init.VrefSel) & TKEY_CR_VREFSEL_MASK) | \
456                   ((TKEY_CR_SPREAD_DISABLE << 5)& TKEY_CR_SPREAD) | \
457                   ((TKEY_CR_CONT_ENABLE << 4)& TKEY_CR_CONT) | \
458                   ((htkey->Init.ShieldEn << 3)& TKEY_CR_SHIELDEN);
459 
460     WRITE_REG(htkey->Instance->CR,u32RegTemp);
461 
462      /*Config the Tkey TKEY_SMPR register*/
463     u32RegTemp = (TKEY_SMPR_SWT(htkey->Init.ScanWaitTime) & TKEY_SMPR_SWT_MASK) | \
464                  (TKEY_SMPR_CST(htkey->Init.CsDisChargeTime) & TKEY_SMPR_CST_MASK);
465 
466     WRITE_REG(htkey->Instance->SMPR,u32RegTemp);
467 
468      /*Config the Tkey TKEY_SOFR register*/
469     u32RegTemp = (TKEY_SOFR_SW1H(htkey->Init.Sw1H) & TKEY_SOFR_SW1H_MASK) | \
470                  (TKEY_SOFR_SW1L(htkey->Init.Sw1L) & TKEY_SOFR_SW1L_MASK);
471 
472     WRITE_REG(htkey->Instance->SOFR,u32RegTemp);
473 
474     HAL_TKEY_ReadNr(htkey);
475 
476     for(ucI = 0; htkey->ChannelData[ucI].ChannelId != 0xFFFF; ucI++)
477     {
478         /* if need calibrate , read the data to the reference data.*/
479         htkey->ChannelData[ucI].Tkey_Data->StateId = TKEY_STATEID_STARTUP;
480         htkey->ChannelData[ucI].Tkey_Data->ReferenceFlag = 1;
481 
482         htkey->ChannelData[ucI].Tkey_RefPara->DetectInTH = htkey->ChannelData[ucI].Tkey_RefPara->RefDelta*htkey->ScanPara.DetectInThRatio;
483         htkey->ChannelData[ucI].Tkey_RefPara->DetectOutTH =  htkey->ChannelData[ucI].Tkey_RefPara->RefDelta*htkey->ScanPara.DetectOutThRatio;
484         htkey->ChannelData[ucI].Tkey_RefPara->CalibratTH =   htkey->ChannelData[ucI].Tkey_RefPara->RefDelta*htkey->ScanPara.CalibratThRatio;
485     }
486 
487     HAL_TKEY_ReadAllNx(htkey);
488     /* Clear all keys*/
489     htkey->ChannelDetected = 0;
490     htkey->ChannelDetectedNum  = 0;
491 
492     return  HAL_OK;
493 }
494 
495 /************************************************************************
496  * function   : HAL_TKEY_DetectProcess
497  * Description: TKEY detect main process.
498  * input      : htkey : TKEY handle
499  * return     : None.
500  ************************************************************************/
HAL_TKEY_DetectProcess(TKEY_HandleTypeDef * htkey)501 void HAL_TKEY_DetectProcess(TKEY_HandleTypeDef* htkey)
502 {
503     uint8_t ucI;
504     const TKEY_ChannelDataDef *ChannelData;
505 
506     HAL_TKEY_ReadChannelData(htkey);
507 
508     for(ucI = 0; htkey->ChannelData[ucI].ChannelId != 0xFFFF; ucI++)
509     {
510         ChannelData = &htkey->ChannelData[ucI];
511         switch(ChannelData->Tkey_Data->StateId)
512         {
513             case TKEY_STATEID_STARTUP :
514                 HAL_TKEY_StartUpStateProcess(ChannelData);
515                 break;
516             case TKEY_STATEID_RELEASE :
517                 HAL_TKEY_DebDetectStateProcess(ChannelData);
518                 break;
519             case TKEY_STATEID_DETECT :
520                 if(htkey->ScanTimer >= htkey->ScanPara.DetectingTimeout)
521                     htkey->ChannelDetecting |= (1 << ChannelData->ChannelId);
522                 HAL_TKEY_DebReleaseDetectStateProcess(ChannelData);
523                 if(ChannelData->Tkey_Data->StateId == TKEY_STATEID_RELEASE)
524                 {
525                     htkey->ChannelDetected |= (1 << ChannelData->ChannelId);
526                     htkey->ChannelValue = ChannelData->ChannelId;
527                     htkey->ChannelDetectedNum++;
528                     htkey->ChannelDetecting &= ~(1 << ChannelData->ChannelId);
529                     htkey->ScanTimer = 0;    //Reset the timer when detect Key release.
530                 }
531                 break;
532             default :
533                 break;
534         }
535 
536         if((htkey->ChannelData[ucI].Tkey_Data->Delta > htkey->ChannelData[ucI].Tkey_RefPara->CalibratTH) \
537             ||(htkey->ChannelData[ucI].Tkey_Data->Delta < (-htkey->ChannelData[ucI].Tkey_RefPara->CalibratTH)))
538         {
539             htkey->ScanTimer++;
540             if(htkey->ScanTimer >= htkey->ScanPara.CalibratTimeout)
541             {
542                 htkey->CalFlag = 1;     //Need calibrate.
543                 htkey->ScanTimer = 0;
544                 htkey->ChannelDetected = 0;
545                 htkey->ChannelDetecting = 0;
546                 break;
547            }
548         }
549         if((htkey->ChannelData[ucI].Tkey_Data->Delta > 2*htkey->ChannelData[ucI].Tkey_RefPara->RefDelta) \
550             ||(htkey->ChannelData[ucI].Tkey_Data->Delta < (-htkey->ChannelData[ucI].Tkey_RefPara->RefDelta)))
551         {
552             htkey->CalFlag = 1;     //Need calibrate.
553             htkey->ScanTimer = 0;
554             htkey->ChannelDetected = 0;
555             htkey->ChannelDetecting = 0;
556             break;
557         }
558 
559         if(htkey->ChannelDetecting)  //If don't need detecting.
560         {
561             htkey->ChannelDetecting = 0;
562             htkey->CalFlag = 1;     //Need calibrate.
563         }
564     }
565 }
566 
567 /************************************************************************
568  * function   : HAL_TKEY_Calibrate_RefData
569  * Description: TKEY Calibrate the base Reference Data.
570  * input      : htkey : TKEY handle
571                 CalTimes: The calibrat times.
572  * return     : None
573  ************************************************************************/
HAL_TKEY_Calibrate_RefData(TKEY_HandleTypeDef * htkey,uint8_t CalTimes)574 void HAL_TKEY_Calibrate_RefData(TKEY_HandleTypeDef* htkey, uint8_t CalTimes)
575 {
576     uint8_t ucI,ucJ;
577     uint32_t sum[16];
578 
579     memset(sum,0,sizeof(sum));
580     for(ucJ=0; ucJ < CalTimes; ucJ++)
581     {
582         HAL_TKEY_ReadChannelData(htkey);
583         for(ucI = 0; htkey->ChannelData[ucI].ChannelId != 0xFFFF; ucI++)
584         {
585             if(htkey->ChannelData[ucI].Tkey_Data->Data)
586             {
587                 sum[ucI] +=  htkey->ChannelData[ucI].Tkey_Data->Data;
588             }
589             if(ucJ == (CalTimes-1))
590             {
591                 htkey->ChannelData[ucI].Tkey_Data->RefData =  sum[ucI]/CalTimes;
592             }
593         }
594     }
595 }
596