1 #ifndef __N32L43X_LPRCNT_H__
2 #define __N32L43X_LPRCNT_H__
3 #include "n32l43x.h"
4 #ifdef __cplusplus
5 extern "C" {
6 #endif
7 
8 /* LPRCNT CH_TH mask */
9 #define DACREF_default         ((uint32_t)0xFFC0FFFF)           //比较器内部参考电压设置
10 
11 /* LPRCNT Chanal define */
12 typedef enum
13 {
14     CHANNEL_0 = 0,
15     CHANNEL_1 = 1,
16     CHANNEL_2 = 2,
17 } CHANNELX;
18 
19 #define CHANNEL_ERROR   0xff
20 /* LPRCNT VibrationPowerSelect mask */
21 #define POWERSELECT1V5        0     //1.5V
22 #define POWERSELECT1V65       1     //1.65V
23 #define POWERSELECT1V8        2     //1.8V
24 #define POWERSELECT2V0        3     //2.0V
25 
26 
27 /* LPRCNT the alarm sensor scan frequence  mask */
28 #define FRETIME4        0
29 #define FRETIME8        1
30 #define FRETIME16       2
31 #define FRETIME32       3
32 /* LPRCNT MSI division factor mask */
33 #define LPRCNT_PRESCALER_DIV1               0x00000000U
34 #define LPRCNT_PRESCALER_DIV2               0x00010000U
35 #define LPRCNT_PRESCALER_DIV4               0x00020000U
36 #define LPRCNT_PRESCALER_DIV8               0x00030000U
37 
38 #define LPRCNT_ALMFRE_DIV4                  0x00000000U
39 #define LPRCNT_ALMFRE_DIV8                  0x04000000U
40 #define LPRCNT_ALMFRE_DIV16                 0x08000000U
41 #define LPRCNT_ALMFRE_DIV32                 0x0c000000U
42 /* LPRCNT interrupt mask */
43 #define CALIBRATION_INT                 LPRCNT_CTRL_CALIE
44 #define REPORT_INT                      LPRCNT_CTRL_RPTIE
45 #define ALARM_INT                       LPRCNT_CTRL_ALMIE
46 /* LPRCNT interrupt flag mask */
47 #define CALIBRATION_INT_FLAG                 LPRCNT_INTSTS_CALIF
48 #define REPORT_INT_FLAG                      LPRCNT_INTSTS_RPTIF
49 #define ALARM_INT_FLAG                       LPRCNT_INTSTS_ALMIF
50 //CMD register
51 #define START                                 1
52 #define STOP                                  2
53 #define CLEAR                                 4
54 #define CMD_REG_CLR                           0x03
55 //work mode
56 #define LPRCNT_MODE                           1
57 #define CAL_MODE                              0
58 
59 
60 //Time register
61 #define CLEAR_TIME_VALE          0xFF00C0C0
62 //TH register
63 #define CLEAR_TH_VALE            0xFFFF0000
64 
65 /**
66  * @brief  LPRCNT COMP definition
67  */
68 //COMP bits Clear mask
69 //#define CMP_HYSEL_CLEAR      ((uint32_t)0xFFD8F300)
70 //#define CMP_INMSEL_CLEAR     ((uint32_t)0xFFD88F00)
71 //#define CMP_FILTH_CLEAR      ((uint32_t)0xFD68FF00)
72  //Filter threshold control
73 #define CMP_FILTH_MODE0                       ((uint32_t)0x00000000)
74 #define CMP_FILTH_MODE1                       ((uint32_t)0x00800000)
75 #define CMP_FILTH_MODE2                       ((uint32_t)0x01000000)
76 
77 /**
78  * @brief  define LPRCNT some funtion
79  */
80 
81 //#define  STARTSTS(void)   ((bool)(LPRCNT->CMD & 0x01) //read start status
82 #define  LPRCNTModeEnable(mode) do{LPRCNT->CMD &= (~CMD_REG_CLR); LPRCNT->CMD |= (uint32_t)mode;}while (0)//set LPRCNT module CMD .
83 #define  SetLPRCNTWorkMode(mode) do{LPRCNT->CTRL &= (~LPRCNT_CTRL_RCNTM); LPRCNT->CTRL |= (uint32_t)(mode << 24);}while (0)//calibration mode or  LPRCNT mode
84 /**
85  * @brief  define Auto detection
86  */
87 #define AUTODETPERIOD4 0  //count overflow 4*pulse period
88 #define AUTODETPERIOD8 1  //count overflow 8*pulse period
89 /**************************************************************************************************************************************/
90 /*                           function structure variable                                                                              */
91 /**************************************************************************************************************************************/
92 typedef enum
93 {
94     HYST_NO   = (0x0L << 10),
95     HYST_LOW  = (0x1L << 10),
96     HYST_MID  = (0x2L << 10),
97     HYST_HIGH = (0x3L << 10),
98 } LPRCNT_COMP_CTRL_HYST;
99 
100 typedef enum {
101   //comp1 inm sel
102   INMSEL_NC          = ((uint32_t)0x00000000),
103   INMSEL_DAC1        = ((uint32_t)0x00001000),
104   INMSEL_PA0         = ((uint32_t)0x00002000),
105   INMSEL_PA5         = ((uint32_t)0x00003000),
106   INMSEL_PB5         = ((uint32_t)0x00004000),
107   INMSEL_PD4         = ((uint32_t)0x00005000),
108   INMSEL_VREF_VC1    = ((uint32_t)0x00006000),
109   INMSEL_VREF_VC2    = ((uint32_t)0x00007000),
110 }LPRCNT_COMP_CTRL_INMSEL;
111 /**
112  * @brief  LPRCNT Init structure definition
113  */
114 typedef struct
115 {
116       uint8_t vibrationtime;
117       uint8_t dischargetime;
118       uint8_t chargetime;
119 } LPRCNT_InitTime;
120 typedef struct
121 {
122       uint8_t dacreference;
123       uint8_t undampedTh;
124       uint8_t dampedTh;
125 } LPRCNT_InitThreshold;
126 typedef struct
127 {
128       uint16_t low_speed;
129       uint8_t hight_speed;
130       uint8_t swtich_time;
131 } LPRCNT_Initfrequence;
132 typedef struct
133 {
134       uint8_t Channel[3];
135       uint8_t ChargeVol;
136       uint8_t WorkMode;
137       LPRCNT_InitTime ChTime[3];
138       LPRCNT_InitThreshold ChTH[3];
139       LPRCNT_Initfrequence NormalFreq;
140       uint8_t AlarmFreq;
141       uint32_t PrescaleDiv;
142       uint16_t Circle;
143       FunctionalState AutoDetEn;
144       bool AutoWaitPer;
145       FunctionalState IntEn;
146       uint32_t Int;
147 
148 } LPRCNT_InitType;
149 
150 /**
151  * @brief  COMP Init structure definition
152  */
153 
154 typedef struct
155 {
156     bool LowPoweMode;          //low power mode
157     LPRCNT_COMP_CTRL_HYST Hyst;  //COMP hysteresis
158     LPRCNT_COMP_CTRL_INMSEL InmSel; //COMP input minus selection
159     uint16_t ClkPsc;
160 } LPRCNT_COMP_InitType;
161 
162 
163 /**************************************************************************************************************************************/
164 /*                           specail  registers                                                                                       */
165 /**************************************************************************************************************************************/
166 //LPRCNT module setup time
167 #define DacSetupTime(time)      do{LPRCNT->CAL2 &= (~LPRCNT_CAL2_DACSET); LPRCNT->CAL2 |= (uint32_t)(time);}while (0)//time < 64 ,6bit
168 #define CompSetupTime(time)     do{LPRCNT->CAL2 &= (~LPRCNT_CAL2_CMPSET); LPRCNT->CAL2 |= (uint32_t)(time << 8);}while (0)//time < 64 ,6bit
169 
170 #define DacSetupTimeConfig()   DacSetupTime(20)
171 #define CompSetupTimeConfig()  CompSetupTime(31)
172 
173 #define RcntAdjustCircleNum(time)  do{LPRCNT->CAL2 &= (~LPRCNT_CAL2_RCNTADJ); LPRCNT->CAL2 |= (uint32_t)(time << 20);}while (0)//time < 16 ,4bit
174 #define ChargeAndDischargeGap(time) do{LPRCNT->CAL2 &= (~LPRCNT_CAL2_GAP); LPRCNT->CAL2 |= (uint32_t)(time << 16);}while (0)//time < 16 ,4bit
175 //Analog filter
176 #define POPH 0
177 #define NEPH 1
178 #define ANGFILT_TH   (__IO unsigned*)(0x40001800 + 0x1c)
179 #define ANGFILT_CTRL (__IO unsigned*)(0x40001800 + 0x28)
180 #define CLERR_ANGTH  0xffffff87 //bit3~6
181 #define CLERR_ANGPHA 0xffffefff //bit12
182 #define SetAnalogFilterTh(vale) do{(*ANGFILT_TH) &= CLERR_ANGTH;(*ANGFILT_TH) |= (uint32_t)(vale <<3);}while (0) //vale only equal to 0,1,3,8,12
183 #define CompAnalogFilterPhase(dir) do{(*ANGFILT_CTRL) &= CLERR_ANGPHA;(*ANGFILT_CTRL) |= (uint32_t)(dir << 12);}while (0)//Analog filtering  phase selection
184 //auto control set funtion
185 #define PwrAutoChargeEnable(cmd) do{LPRCNT->CAL3 & = (~LPRCNT_CAL3_PWR_DUR_EN);(LPRCNT->CAL3)|= (uint32_t)(cmd <<7);}while (0)//0 :enable 1:disable
186 
187 /**************************************************************************************************************************************/
188 /*                           function declaration                                                                                     */
189 /**************************************************************************************************************************************/
190 void ClearITPendingBit(uint32_t intflag);
191 void COMP_1_2_IRQHandler(void);
192 void LPRCNT_IE(uint32_t MODE_IE ,FunctionalState NewState);
193 void CfgChannelTime(uint8_t Ch,uint8_t VibrationTime ,uint8_t DischargeTime,uint8_t  ChargeTime);
194 void CfgChannelDacRefVol(uint8_t Ch,uint8_t DacRef);
195 void CfgChannelThr(uint8_t Ch, uint8_t UndampedTh, uint8_t DampedTh);
196 void SetMsiClkPrescale(uint32_t Div);
197 void SetAutoReportCircle(uint16_t Circle);
198 void SetScanAverageValue(uint8_t N);
199 void SetVibrationPower(uint8_t Value);
200 void SetNormalSensorScanfrequence(uint16_t low_speed,uint8_t hight_speed,uint8_t swtich_time);
201 void SetAlarmSensorScanfrequence(uint8_t Period);
202 void SetAutoDetect(FunctionalState NewState );
203 void SetAutoDetectEnale(FunctionalState NewState );
204 void SetAutoDetectPeriod(bool per);
205 uint8_t GetSampleMode(void);
206 uint8_t GetChannelSensorWavesNum(uint8_t Ch);
207 uint8_t GetChannelSensorState(uint8_t Ch);
208 uint16_t GetRotationCircle(void);
209 uint16_t GetSetRcnt(void);
210 void ClrRcntCircle(void);
211 void SetPwrAutoCharge(bool En);
212 bool ReadStartState(void);
213 void LPRCNTInit(LPRCNT_InitType* LPRCNT_InitStruct);
214 void DAC_CMP_ALWSONCmd(FunctionalState NewState);
215 
216 void LPRCNT_CompInit(LPRCNT_COMP_InitType* COMP_InitStruct);
217 void CompDigitalFilterCfg(bool cmd, uint32_t filterTh);
218 void CompAnalogFilterEn(bool cmd);
219 void LPRCNTAnalogFilterConfig(void);
220 //Interruprt mask
221 void LPRCNT_ClrIntBit(uint32_t intflag);
222 
223 INTStatus LPRCNT_GetIntSts(uint32_t Int);
224 void LPRCNT_IntEn(uint32_t Mode ,FunctionalState NewState);
225 void CompDigitalFilterPhase(bool dir);
226 
227 //for prinft some set values
228 uint16_t GetNormalSensorLowSpeed(void);
229 uint8_t GetNormalSensorHightSpeed(void);
230 uint8_t GetNormalSensorSwtichTime(void);
231 uint8_t GetDacRefVol(uint8_t Ch);
232 uint8_t GetUndampedTh(uint8_t Ch);
233 uint8_t GetDampedTh(uint8_t Ch);
234 uint8_t GetVibrationTime(uint8_t Ch);
235 uint8_t GetDischargeTime(uint8_t Ch);
236 uint8_t GetChargeTime(uint8_t Ch);
237 #endif /* __LPRCNT_H__ */
238 /**
239  * @}
240  */
241