1 /** ----------------------------------------------------------------------------
2 * Nationz Technology Software Support - NATIONZ -
3 * -----------------------------------------------------------------------------
4 * Copyright (c) 2022, Nationz Corporation All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * - Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the disclaiimer below.
11 *
12 * - Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the disclaimer below in the documentation and/or
14 * other materials provided with the distribution.
15 *
16 * Nationz's name may not be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONZ "AS IS" AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
22 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
25 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * -----------------------------------------------------------------------------
31 */
32
33 /** ****************************************************************************
34 * @copyright Nationz Co.,Ltd
35 * Copyright (c) 2019 All Rights Reserved
36 *******************************************************************************
37 * @file n32l43x_LPRCNT.c
38 * @author
39 * @date
40 * @version v1.2.0
41 * @brief
42 ******************************************************************************/
43
44 /* Includes ------------------------------------------------------------------*/
45 #include "n32l43x_LPRCNT.h"
46 //nclude "n32l43x_rcc.h"
47 //#include "n32l43x.h"
48 #include "n32l43x_exti.h"
49 #include "misc.h"
50 /** @addtogroup n32l43x_StdPeriph_Driver
51 * @{
52 */
53
54 /** @defgroup LPRCNT
55 * @brief LPRCNT driver modules
56 * @{
57 */
58
59 /** @defgroup LPRCNT_Private_TypesDefinitions
60 * @{
61 */
62 /**
63 * @}
64 */
65
66
67
68
69 /* Private types -------------------------------------------------------------*/
70 /* Private variables ---------------------------------------------------------*/
71 /* Private constants ---------------------------------------------------------*/
72 /* Private macros ------------------------------------------------------------*/
73
74 /**
75 * @brief clear LPRCNT interrupt flag funtion.
76 * @param intflag: include LPRCNT interrupt flag
77 * This parameter can be one of the following values:
78 * @arg CALIBRATION_INT_FLAG
79 * @arg REPORT_INT_FLAG
80 * @arg ALARM_INT_FLAG
81 * @retval None
82 */
LPRCNT_ClrIntBit(uint32_t IntFlag)83 void LPRCNT_ClrIntBit(uint32_t IntFlag)
84 {
85 LPRCNT->INTSTS |= IntFlag;
86 }
87
88 /**
89 * @brief Checks whether the specified LPRCNT interrupt has occurred or not.
90 * @param LPRCNT_INT specifies the LPRCNT interrupt source to check.
91 * This parameter can be one of the following values:
92 * @arg CALIBRATION_INT_FLAG Calibration mode damped oscillation interrupt.
93 * @arg REPORT_INT_FLAG Report interrupt.
94 * @arg ALARM_INT_FLAG Alarm interrupt .
95 * @return The new state of LPRCNT_INT (SET or RESET).
96 */
LPRCNT_GetIntSts(uint32_t Int)97 INTStatus LPRCNT_GetIntSts(uint32_t Int)
98 {
99 INTStatus bitstatus = RESET;
100 uint32_t Temp = 0;
101
102 Temp = LPRCNT->INTSTS ;
103 Temp &= 0x00070000;
104 if (Temp == Int)
105 {
106 bitstatus = SET;
107 }
108 else
109 {
110 bitstatus = RESET;
111 }
112 return bitstatus;
113
114 }
115 /**
116 * @brief LPRCNT interrupt funtion.
117 * @param MODE_IE: include LPRCNT interrupt
118 * This parameter can be one of the following values:
119 * @arg CALIBRATION_INT
120 * @arg REPORT_INT
121 * @arg ALARM_INT
122 * @param NewState: open or disable
123 * @arg DISABLE
124 * @arg ENABLE
125 * @retval None
126 */
LPRCNT_IntEn(uint32_t Mode,FunctionalState NewState)127 void LPRCNT_IntEn(uint32_t Mode ,FunctionalState NewState)
128 {
129 if (NewState == ENABLE)
130 {
131 LPRCNT->CTRL |= Mode;
132 }
133 else
134 {
135 LPRCNT->CTRL &= ~Mode;
136 }
137 }
138 /**
139 * @brief configure per channel sensor detection time.
140 * @param ch: sensor channel .
141 * @param vibrationtime: comparator processing duration time.
142 * @param dischargetime: discharge duration time.
143 * @param chargetime: power charge duration time.
144 * @retval None
145 */
CfgChannelTime(uint8_t Ch,uint8_t VibrationTime,uint8_t DischargeTime,uint8_t ChargeTime)146 void CfgChannelTime(uint8_t Ch,uint8_t VibrationTime ,uint8_t DischargeTime,uint8_t ChargeTime )
147 {
148 uint32_t Temp = 0;
149 switch (Ch)
150 {
151 case CHANNEL_0 :
152 Temp = LPRCNT->CH0CFG1;
153 Temp &= CLEAR_TIME_VALE;
154 Temp |= (uint32_t)((VibrationTime << 16) | (DischargeTime << 8) | (ChargeTime));
155 LPRCNT->CH0CFG1 = Temp;
156 break;
157 case CHANNEL_1 :
158 Temp = LPRCNT->CH1CFG1;
159 Temp &= CLEAR_TIME_VALE;
160 Temp |= (uint32_t)((VibrationTime << 16) | (DischargeTime << 8) | (ChargeTime));
161 LPRCNT->CH1CFG1 = Temp;
162 break;
163 case CHANNEL_2 :
164 Temp = LPRCNT->CH2CFG1;
165 Temp &= CLEAR_TIME_VALE;
166 Temp |= (uint32_t)((VibrationTime << 16) | (DischargeTime << 8) | (ChargeTime));
167 LPRCNT->CH2CFG1 = Temp;
168 break;
169 default: break ;
170 }
171 }
172
173 /**
174 * @brief get the comparator processing duration time.
175 * @param ch: sensor channel .
176 * @retval duration time
177 */
GetVibrationTime(uint8_t Ch)178 uint8_t GetVibrationTime(uint8_t Ch)
179 {
180 uint8_t Temp = 0;
181 switch (Ch)
182 {
183 case CHANNEL_0 :
184 Temp = (LPRCNT->CH0CFG1 >> 16);
185 break;
186 case CHANNEL_1 :
187 Temp = (LPRCNT->CH1CFG1 >> 16);
188 break;
189 case CHANNEL_2 :
190 Temp = (LPRCNT->CH2CFG1 >> 16);
191 break;
192 }
193 return Temp;
194 }
195 /**
196 * @brief get discharge duration time.
197 * @param ch: sensor channel .
198 * @retval discharge duration time
199 */
GetDischargeTime(uint8_t Ch)200 uint8_t GetDischargeTime(uint8_t Ch)
201 {
202 uint8_t Temp = 0;
203 switch (Ch)
204 {
205 case CHANNEL_0 :
206 Temp = ((LPRCNT->CH0CFG1 >> 8) & 0x3f);
207 break;
208 case CHANNEL_1 :
209 Temp = ((LPRCNT->CH1CFG1 >> 8) & 0x3f);
210 break;
211 case CHANNEL_2 :
212 Temp = ((LPRCNT->CH2CFG1 >> 8) & 0x3f);
213 break;
214 }
215 return Temp;
216 }
217 /**
218 * @brief get charge duration time.
219 * @param ch: sensor channel .
220 * @retval charge duration time
221 */
GetChargeTime(uint8_t Ch)222 uint8_t GetChargeTime(uint8_t Ch)
223 {
224 uint8_t Temp = 0;
225 switch (Ch)
226 {
227 case CHANNEL_0 :
228 Temp = (LPRCNT->CH0CFG1 & 0x3f);
229 break;
230 case CHANNEL_1 :
231 Temp = (LPRCNT->CH1CFG1 & 0x3f);
232 break;
233 case CHANNEL_2 :
234 Temp = (LPRCNT->CH2CFG1 & 0x3f);
235 break;
236 }
237 return Temp;
238 }
239 /**
240 * @brief configure per channel sensor detection threshold.
241 * @param ch: sensor channel .
242 * @param dacreference: DAC reference value for comparator.
243 * @retval None
244 */
CfgChannelDacRefVol(uint8_t Ch,uint8_t DacRef)245 void CfgChannelDacRefVol(uint8_t Ch,uint8_t DacRef)
246 {
247 uint32_t Temp = 0;
248 switch (Ch)
249 {
250 case CHANNEL_0 :
251 Temp = LPRCNT->CH0CFG0;
252 Temp &= (~LPRCNT_CH0CFG0_DACREF);
253 Temp |= (uint32_t)(DacRef <<16);
254 LPRCNT->CH0CFG0 = Temp;
255 break;
256 case CHANNEL_1 :
257 Temp = LPRCNT->CH1CFG0;
258 Temp &= (~LPRCNT_CH1CFG0_DACREF);
259 Temp |= (uint32_t)(DacRef <<16);
260 LPRCNT->CH1CFG0 = Temp;
261 break;
262 case CHANNEL_2 :
263 Temp = LPRCNT->CH2CFG0;
264 Temp &= (~LPRCNT_CH2CFG0_DACREF);
265 Temp |= (uint32_t)(DacRef <<16);
266 LPRCNT->CH2CFG0 = Temp;
267 break;
268 default:
269 break ;
270 }
271
272 }
273 /**
274 * @brief get the DAC reference voltage.
275 * @param ch: sensor channel .
276 * @retval DAC reference voltage values,Temp <= 64
277 */
GetDacRefVol(uint8_t Ch)278 uint8_t GetDacRefVol(uint8_t Ch)
279 {
280 uint8_t Temp = 0;
281 switch (Ch)
282 {
283 case CHANNEL_0 :
284 Temp = ((LPRCNT->CH0CFG0 >> 16) & 0x3f);
285 break;
286 case CHANNEL_1 :
287 Temp = ((LPRCNT->CH1CFG0 >> 16) & 0x3f);
288 break;
289 case CHANNEL_2 :
290 Temp = ((LPRCNT->CH2CFG0 >> 16) & 0x3f);
291 break;
292 }
293 return Temp;
294 }
295
296 /**
297 * @brief configure per channel sensor detection threshold.
298 * @param ch: sensor channel .
299 * @param undampedTh: undamped threshold.
300 * @param dampedTh: damped threshold.
301 * @retval None
302 */
CfgChannelThr(uint8_t Ch,uint8_t UndampedTh,uint8_t DampedTh)303 void CfgChannelThr(uint8_t Ch, uint8_t UndampedTh, uint8_t DampedTh)
304 {
305 uint32_t Temp = 0;
306 switch (Ch)
307 {
308 case CHANNEL_0 :
309 Temp = LPRCNT->CH0CFG0;
310 Temp &= CLEAR_TH_VALE;
311 Temp |= (uint32_t)((UndampedTh <<8) | DampedTh);
312 LPRCNT->CH0CFG0 = Temp;
313 break;
314 case CHANNEL_1 :
315 Temp = LPRCNT->CH1CFG0;
316 Temp &= CLEAR_TH_VALE;
317 Temp |= (uint32_t)((UndampedTh <<8) | DampedTh);
318 LPRCNT->CH1CFG0 = Temp;
319 break;
320 case CHANNEL_2 :
321 Temp = LPRCNT->CH2CFG0;
322 Temp &= CLEAR_TH_VALE;
323 Temp |= (uint32_t)((UndampedTh <<8) | DampedTh);
324 LPRCNT->CH2CFG0 = Temp;
325 break;
326 default: break ;
327 }
328 }
329
330 /**
331 * @brief get undamped threshold.
332 * @param ch: sensor channel .
333 * @retval undamped threshold.
334 */
GetUndampedTh(uint8_t Ch)335 uint8_t GetUndampedTh(uint8_t Ch)
336 {
337 uint8_t Temp = 0;
338 switch (Ch)
339 {
340 case CHANNEL_0 :
341 Temp = (LPRCNT->CH0CFG0 >> 8);
342 break;
343 case CHANNEL_1 :
344 Temp = (LPRCNT->CH1CFG0 >> 8);
345 break;
346 case CHANNEL_2 :
347 Temp = (LPRCNT->CH2CFG0 >> 8);
348 break;
349 }
350 return Temp;
351 }
352 /**
353 * @brief get damped threshold.
354 * @param ch: sensor channel .
355 * @retval damped threshold.
356 */
GetDampedTh(uint8_t Ch)357 uint8_t GetDampedTh(uint8_t Ch)
358 {
359 uint8_t Temp = 0;
360 switch (Ch)
361 {
362 case CHANNEL_0 :
363 Temp = (LPRCNT->CH0CFG0);
364 break;
365 case CHANNEL_1 :
366 Temp = (LPRCNT->CH1CFG0);
367 break;
368 case CHANNEL_2 :
369 Temp = (LPRCNT->CH2CFG0);
370 break;
371 }
372 return Temp;
373 }
374 /**
375 * @brief MSI clock prescale.
376 * @param div: division factor
377 * This parameter can be one of the following values:
378 * @arg LPRCNT_PRESCALER_DIV1
379 * @arg LPRCNT_PRESCALER_DIV2
380 * @arg LPRCNT_PRESCALER_DIV4
381 * @arg LPRCNT_PRESCALER_DIV8
382 * @retval None
383 */
SetMsiClkPrescale(uint32_t Div)384 void SetMsiClkPrescale(uint32_t Div)
385 {
386 uint32_t Temp = 0;
387 Temp = LPRCNT->CTRL;
388 //clear the bit that need add new vale
389 Temp &= (~LPRCNT_CTRL_CLKDIV);
390 Temp |= Div;
391 LPRCNT->CTRL = Temp;
392 }
393
394 /**
395 * @brief get the Circle value .
396 * @param ch: there are three state mode
397 * This parameter can be one of the following values:
398 * @retval the ratation Circle value
399 */
400
GetRotationCircle(void)401 uint16_t GetRotationCircle(void)
402 {
403 uint16_t Temp;
404 Temp = (uint16_t)LPRCNT->INTSTS;
405 return Temp ;
406
407 }
408 /**
409 * @brief get the user set circle value .
410 * This parameter can be one of the following values:
411 * @retval the set ratation Circle value
412 */
413
GetSetRcnt(void)414 uint16_t GetSetRcnt(void)
415 {
416 uint16_t Temp;
417 Temp = (uint16_t)LPRCNT->CTRL;
418 return Temp ;
419 }
420 /**
421 * @brief clear the RCNT circle.
422 * @param None
423 * @retval None
424 */
ClrRcntCircle(void)425 void ClrRcntCircle(void)
426 {
427 uint32_t Temp;
428 Temp = LPRCNT->CMD;
429 //clear the bit that need add new vale
430 Temp &= (~LPRCNT_CMD_CLRCNT);
431 Temp |= LPRCNT_CMD_CLRCNT;
432 LPRCNT->CMD = Temp;
433 }
434 /**
435 * @brief when the setting number is reach ,it is will creat a overflow interrupt.
436 * @param Circle: the rotating Circle number
437 * @retval None
438 */
SetAutoReportCircle(uint16_t Circle)439 void SetAutoReportCircle(uint16_t Circle)
440 {
441 uint32_t Temp = 0;
442 uint16_t Cnt = 0;
443 //when set the circle numbers,need add the lase numbers
444 Cnt = GetRotationCircle();
445 Temp = LPRCNT->CTRL;
446 //clear the bit that need add new vale
447 Temp &= (~LPRCNT_CTRL_RPTTH);
448 Temp |= (uint32_t)(Circle + Cnt);
449 LPRCNT->CTRL = Temp;
450
451 }
452 #if 0
453 /**
454 * @brief when the setting number is reach ,it is will creat a overflow interrupt.
455 * @param Circle: the rotating Circle number
456 * @retval None
457 */
458 void SetAutoReportCircle(uint16_t Circle)
459 {
460 uint32_t Temp = 0;
461 Temp = LPRCNT->CTRL;
462 //clear the bit that need add new vale
463 Temp &= (~LPRCNT_CTRL_RPTTH);
464 Temp |= (uint32_t)Circle;
465 LPRCNT->CTRL = Temp;
466 }
467 #endif
468 /**
469 * @brief enable to auto detect comparator stop.
470 * @param CMD: ENABLE or DISABLE
471 * @retval None
472 */
SetAutoDetectEnale(FunctionalState NewState)473 void SetAutoDetectEnale(FunctionalState NewState )
474 {
475 uint32_t Temp = 0;
476 Temp = LPRCNT->CTRL;
477 //clear the bit that need add new vale
478 Temp &= (~LPRCNT_CTRL_CMPAUT);
479 Temp |= (uint32_t)(NewState <<23);
480 LPRCNT->CTRL = Temp;
481 }
482 /**
483 * @brief set auto detect comparator stop need wait period.
484 * @param per: AUTODETPERIOD4 or AUTODETPERIOD8
485 * @retval None
486 */
SetAutoDetectPeriod(bool per)487 void SetAutoDetectPeriod(bool per)
488 {
489 uint32_t Temp = 0;
490 Temp = LPRCNT->CAL3;
491 //clear the bit that need add new vale
492 Temp &= (~LPRCNT_CAL3_CMP_AUTO_MODE);
493 Temp |= (uint32_t)(per <<6);
494 LPRCNT->CAL3 = Temp;
495 }
496 /**
497 * @brief set auto to charge by DAC.
498 * @param En: ENABLE or DISABLE
499 * @retval None
500 */
SetPwrAutoCharge(bool En)501 void SetPwrAutoCharge(bool En)
502 {
503 uint32_t Temp = 0;
504 Temp = LPRCNT->CAL3;
505 //clear the bit that need add new vale
506 Temp &= (~LPRCNT_CAL3_PWR_DUR_EN);
507 Temp |= (uint32_t)(En <<7);
508 LPRCNT->CAL3 = Temp;
509 }
510 /**
511 * @brief After seveval rounds of scanning , it is necessary to take the average .
512 * @param n: the scanning times , times = 2^n
513 * @retval None
514 */
SetScanAverageValue(uint8_t N)515 void SetScanAverageValue(uint8_t N)
516 {
517 uint32_t Temp = 0;
518 if (N <= 3)
519 {
520 Temp = LPRCNT->CTRL;
521 //clear the bit that need add new vale
522 Temp &= (~LPRCNT_CTRL_AVGSEL);
523 Temp |= (uint32_t)(N << 18);
524 LPRCNT->CTRL = Temp;
525 }
526 }
527 /**
528 * @brief SetVibrationPower.
529 * @param value: the damped vibration power select.
530 * This parameter can be one of the following values:
531 * @arg POWERSELECT1V5
532 * @arg POWERSELECT1V65
533 * @arg POWERSELECT1V8
534 * @arg POWERSELECT2V0
535 * @retval None
536 */
SetVibrationPower(uint8_t Value)537 void SetVibrationPower(uint8_t Value)
538 {
539 uint32_t Temp = 0;
540 Temp = LPRCNT->CTRL;
541 //clear the bit that need add new vale
542 Temp &= (~LPRCNT_CTRL_PWRLVL);
543 Temp |= (uint32_t)(Value << 21);
544 LPRCNT->CTRL = Temp;
545 }
546
547 /**
548 * @brief configure normal sensor scan frequence.
549 * @param low_speed: if the rotating object does not move for a long time ,it will enter into low speed .
550 * @param hight_speed: In other cases, MCU keep in hight speed to detect rotation.
551 * @param swtich_time: the time interval about hight speed swtich to low speed.
552 * @retval None
553 */
SetNormalSensorScanfrequence(uint16_t Low_speed,uint8_t Hight_speed,uint8_t Swtich_time)554 void SetNormalSensorScanfrequence(uint16_t Low_speed,uint8_t Hight_speed,uint8_t Swtich_time)
555 {
556 uint32_t Temp = 0;
557 Temp = LPRCNT->SCTRL;
558 //clear the bit that need add new vale
559 Temp &= 0xfc000000;
560 Temp |= (uint32_t)((Low_speed <<16) | (Swtich_time <<8) | (Hight_speed));
561 LPRCNT->SCTRL = Temp;
562 }
563 /**
564 * @brief get normal sensor low speed values.
565 * @retval low speed values
566 */
GetNormalSensorLowSpeed(void)567 uint16_t GetNormalSensorLowSpeed(void)
568 {
569 uint16_t Temp = 0;
570 Temp = (uint16_t)((LPRCNT->SCTRL >> 16) & 0x03ff);
571 return Temp;
572 }
573 /**
574 * @brief get normal sensor hight speed values.
575 * @retval hight speed values
576 */
GetNormalSensorHightSpeed(void)577 uint8_t GetNormalSensorHightSpeed(void)
578 {
579 uint8_t Temp = 0;
580 Temp = (uint8_t)(LPRCNT->SCTRL);
581 return Temp;
582 }
583 /**
584 * @brief get normal sensor swtich time.
585 * @retval swtich time values
586 */
GetNormalSensorSwtichTime(void)587 uint8_t GetNormalSensorSwtichTime(void)
588 {
589 uint8_t Temp = 0;
590 Temp = (uint8_t)(LPRCNT->SCTRL >> 8);
591 return Temp;
592 }
593 /**
594 * @brief SetAlarmSensorScanfrequence.
595 * @param the frequence is several times than the normal sensor .
596 * This parameter can be one of the following values:
597 * @arg FRETIME4
598 * @arg FRETIME8
599 * @arg FRETIME16
600 * @arg FRETIME32
601 * @retval None
602 */
SetAlarmSensorScanfrequence(uint8_t Period)603 void SetAlarmSensorScanfrequence(uint8_t Period)
604 {
605 uint32_t Temp = 0;
606 Temp = LPRCNT->CTRL;
607 //clear the bit that need add new vale
608 Temp &= (~LPRCNT_CTRL_ALMPRD);
609 Temp |= (uint32_t)(Period <<26);
610 LPRCNT->CTRL = Temp;
611 }
612 /**
613 * @brief set LPRCNT module CMD .
614 * @param command .
615 * This parameter can be one of the following values:
616 * @retval None
617 */
618 //void LPRCNTModeEnable(uint8_t Cmd)
619 //{
620 // LPRCNT->CMD |= (uint32_t)Cmd;
621 //
622 //}
623 /**
624 * @brief Read lprcnt Start state .
625 * @param void .
626 * @retval 0 or 1
627 */
ReadStartState(void)628 bool ReadStartState(void)
629 {
630 bool temp;
631 temp = LPRCNT->CMD;
632 return temp ;
633 }
634
635 /**
636 * @brief After the comparator , the square waves can be got for each channel .
637 * @param ch: there are in total three sensor.The frequence of the three sensors is staggered,
638 * so the number of square waves will be different.
639 * @retval the waves number
640 */
GetChannelSensorWavesNum(uint8_t Ch)641 uint8_t GetChannelSensorWavesNum(uint8_t Ch)
642 {
643 uint8_t Temp =0;
644 if (Ch == CHANNEL_0)
645 {
646 Temp = (LPRCNT->CAL0 & 0x000000ff);
647 return Temp;
648 }
649 else if (Ch == CHANNEL_1)
650 {
651 Temp =((LPRCNT->CAL0 & 0x00ff0000)>> 16);
652 return Temp;
653 }
654 else
655 {
656 Temp = (LPRCNT->CAL1 & 0x000000ff);
657 return Temp;
658 }
659 }
660
661 /**
662 * @brief Through the threshold value judgment,per channel can redefine as the state mode .
663 * @param ch: there are three state mode
664 * This parameter can be one of the following values:
665 * @arg 0: undamped
666 * @arg 1: middle state
667 * @arg 2: damped
668 * @retval the state value
669 */
GetChannelSensorState(uint8_t Ch)670 uint8_t GetChannelSensorState(uint8_t Ch)
671 {
672 if (Ch == CHANNEL_0)
673 {
674 return ((uint8_t)((LPRCNT->CAL0 >> 8) & 0x3));
675 }
676 else if (Ch == CHANNEL_1)
677 {
678 return ((uint8_t)((LPRCNT->CAL0 >> 24) & 0x3));
679 }
680 else if (Ch == CHANNEL_2)
681 {
682 return ((uint8_t)((LPRCNT->CAL1 >> 8) & 0x3));
683 }
684 else
685 {
686 return CHANNEL_ERROR;//channel error
687 }
688 }
689
690 /**
691 * @brief get the sample mode .
692 * @param ch: there are three state mode
693 * This parameter can be one of the following values:
694 * @retval the ratation Circle value
695 */
GetSampleMode(void)696 uint8_t GetSampleMode(void)
697 {
698 uint8_t Temp;
699 Temp = (uint8_t)((LPRCNT->CAL2 >> 25) & 0x01);
700 return Temp ;
701 }
702 /**
703 * @brief LPRCNT module can work in two mode.this is LPRCNT mode and calibration mode .
704 * @param mode: working mode .
705 * This parameter can be one of the following values:
706 * @arg 0: calibration mode , for calibration rotation object parameters
707 * @arg 1: LPRCNT mode, the mode can detect the Circle of rotation
708 * @retval None
709 */
710 //void SetLPRCNTWorkMode(uint8_t mode)//封装成一个宏
711 //{
712 // LPRCNT->CTRL |= (uint32_t)(mode << 24);
713 //}
714
715 /**
716 * @brief LPRCNT COMP Init.
717 * @param COMP_InitStruct.
718 * This parameter can be one of the following values:
719 * LPRCNT_COMP_InitType
720 * @retval None
721 */
LPRCNT_CompInit(LPRCNT_COMP_InitType * COMP_InitStruct)722 void LPRCNT_CompInit(LPRCNT_COMP_InitType* COMP_InitStruct)
723 {
724 uint32_t Temp ;
725 Temp = LPRCNT->CAL3;
726 //clear the bits about COMP hysteresis
727 Temp &= (~LPRCNT_CAL3_CMP_HYSEL);
728 Temp |= COMP_InitStruct->Hyst;
729 //clear input minus selection bits
730 Temp &= (~LPRCNT_CAL3_CMP_INMSEL);
731 Temp |= COMP_InitStruct->InmSel;
732 //COMP low power enable
733 Temp |= COMP_InitStruct->LowPoweMode;
734 LPRCNT->CAL3 = Temp;
735 }
736
737 /**
738 * @brief DigitalfilterConfig.
739 * @param cmd:enable or disable.
740 * @param filterTh:Filter threshold control.
741 * This parameter can be one of the following values:
742 * CMP_FILTH_MODE0 : T/2~T
743 * CMP_FILTH_MODE1 : T~3T/2
744 * CMP_FILTH_MODE2 : 3T/2~2T
745 * @retval None
746 */
CompDigitalFilterCfg(bool Cmd,uint32_t FilterThr)747 void CompDigitalFilterCfg(bool Cmd, uint32_t FilterThr)
748 {
749 uint32_t Temp ;
750 Temp = LPRCNT->CAL3;
751 //clear FILTH bit
752 Temp &= (~LPRCNT_CAL3_FILTH);
753 Temp |= FilterThr;
754 LPRCNT->CAL3 = Temp;
755 if (Cmd == ENABLE)
756 {
757 LPRCNT->CAL3 |= LPRCNT_CAL3_DIGFILEN;
758 }
759 else
760 {
761 LPRCNT->CAL3 &= (~LPRCNT_CAL3_DIGFILEN);
762 }
763 }
764 /**
765 * @brief COMPDigitalfilterEnable.
766 * @param cmd:ENABLE or DISABLE.
767 * @retval None
768 */
CompAnalogFilterEn(bool Cmd)769 void CompAnalogFilterEn(bool Cmd)
770 {
771 if (Cmd == ENABLE)
772 {
773 LPRCNT->CAL3 |= LPRCNT_CAL3_ANGFILEN;
774 }
775 else
776 {
777 LPRCNT->CAL3 &= (~LPRCNT_CAL3_ANGFILEN);
778 }
779 }
780
781 /**
782 * @brief Digital filter phase control.
783 * @param direction:P or N.
784 * @retval None
785 */
CompDigitalFilterPhase(bool dir)786 void CompDigitalFilterPhase(bool dir)
787 {
788 uint32_t Temp ;
789 Temp = LPRCNT->CAL3;
790 Temp &= (~LPRCNT_CAL3_DIGFILPH);
791 if (dir == POPH)
792 {
793 Temp |= LPRCNT_CAL3_DIGFILPH_P;
794 LPRCNT->CAL3 = Temp;
795 }
796 else
797 {
798 Temp |= LPRCNT_CAL3_DIGFILPH_N;
799 LPRCNT->CAL3 = Temp;
800 }
801 }
802
803
804 /**
805 * @brief DAC & CMP always on enable while 1 round sampling.
806 * @param NewState:ENABLE or DISABLE.
807 * @retval None
808 */
DAC_CMP_ALWSONCmd(FunctionalState NewState)809 void DAC_CMP_ALWSONCmd(FunctionalState NewState)
810 {
811 if (NewState != DISABLE)
812 {
813 /* Enable the selected LPRCNT AlwaysON,hardware auto turn on MSI clock */
814 LPRCNT->CAL3 |= LPRCNT_CAL3_DAC_CMP_ALWSON;
815 RCC_EnableMsi(DISABLE);
816 }
817 else
818 {
819 /* Disable the selected LPRCNT stop */
820 LPRCNT->CAL3 &= ~LPRCNT_CAL3_DAC_CMP_ALWSON;
821 }
822 }
823 /**
824 * @brief LPRCNT analog filter,include gated values and phases.
825 * @param None
826 * @retval None
827 */
LPRCNTAnalogFilterConfig(void)828 void LPRCNTAnalogFilterConfig(void)
829 {
830 SetAnalogFilterTh(1);
831 CompAnalogFilterPhase(POPH);
832 CompAnalogFilterEn(ENABLE);
833 }
834 /**
835 * @brief LPRCNT module Init.
836 * @param LPRCNT are ready to run .
837 * This parameter can be one of the following values:
838 * @arg 0: LPRCNT_InitStruct , for init rotation object parameters
839 * @retval None
840 */
LPRCNTInit(LPRCNT_InitType * LPRCNT_InitStruct)841 void LPRCNTInit(LPRCNT_InitType* LPRCNT_InitStruct)
842 {
843 //clock and charge voltage
844 SetMsiClkPrescale(LPRCNT_InitStruct->PrescaleDiv);
845 SetVibrationPower(LPRCNT_InitStruct->ChargeVol);
846 //SetPwrAutoCharge(ENABLE);
847 //DAC_CMP_ALWSONCmd(ENABLE);
848 //sensor time paragram
849 CfgChannelTime(CHANNEL_0,LPRCNT_InitStruct->ChTime[0].vibrationtime,LPRCNT_InitStruct->ChTime[0].dischargetime,LPRCNT_InitStruct->ChTime[0].chargetime);
850 CfgChannelTime(CHANNEL_1,LPRCNT_InitStruct->ChTime[1].vibrationtime,LPRCNT_InitStruct->ChTime[1].dischargetime,LPRCNT_InitStruct->ChTime[1].chargetime);
851 CfgChannelTime(CHANNEL_2,LPRCNT_InitStruct->ChTime[2].vibrationtime,LPRCNT_InitStruct->ChTime[2].dischargetime,LPRCNT_InitStruct->ChTime[2].chargetime);
852 //sensor state paragram
853 CfgChannelThr(CHANNEL_0,LPRCNT_InitStruct->ChTH[0].undampedTh,LPRCNT_InitStruct->ChTH[0].dampedTh);
854 CfgChannelDacRefVol(CHANNEL_0,LPRCNT_InitStruct->ChTH[0].dacreference);
855 CfgChannelThr(CHANNEL_1,LPRCNT_InitStruct->ChTH[1].undampedTh,LPRCNT_InitStruct->ChTH[1].dampedTh);
856 CfgChannelDacRefVol(CHANNEL_1,LPRCNT_InitStruct->ChTH[1].dacreference);
857 CfgChannelThr(CHANNEL_2,LPRCNT_InitStruct->ChTH[2].undampedTh,LPRCNT_InitStruct->ChTH[2].dampedTh);
858 CfgChannelDacRefVol(CHANNEL_2,LPRCNT_InitStruct->ChTH[2].dacreference);
859 SetNormalSensorScanfrequence(LPRCNT_InitStruct->NormalFreq.low_speed,LPRCNT_InitStruct->NormalFreq.hight_speed,LPRCNT_InitStruct->NormalFreq.swtich_time);
860 SetScanAverageValue(0);//default 0
861 //scan period
862 SetNormalSensorScanfrequence(LPRCNT_InitStruct->NormalFreq.low_speed,LPRCNT_InitStruct->NormalFreq.hight_speed,LPRCNT_InitStruct->NormalFreq.swtich_time);
863 SetAlarmSensorScanfrequence(LPRCNT_InitStruct->AlarmFreq);
864 SetAutoDetectPeriod(LPRCNT_InitStruct->AutoWaitPer);
865 SetAutoDetectEnale(LPRCNT_InitStruct->AutoDetEn);
866 //setup time funtion
867 DacSetupTimeConfig();
868 CompSetupTimeConfig();
869 //ChargeAndDischargeGap(5);
870 ClrRcntCircle();
871 SetAutoReportCircle(LPRCNT_InitStruct->Circle);
872 LPRCNTAnalogFilterConfig();//default to select the analog filter
873 SetLPRCNTWorkMode(LPRCNT_InitStruct->WorkMode);
874 //interruput
875 LPRCNT_ClrIntBit(LPRCNT_INTSTS_RPTIF);
876 LPRCNT_IntEn(LPRCNT_InitStruct->Int,LPRCNT_InitStruct->IntEn);
877 }
878
879
880
881 /**
882 * @}
883 */
884
885 /**
886 * @}
887 */
888
889
890 /******************* (C) COPYRIGHT 2019 NATIONZ *****END OF FILE****/
891
892