1 /**
2   ******************************************************************************
3   * @file    lib_comp.c
4   * @author  Application Team
5   * @version V4.4.0
6   * @date    2018-09-27
7   * @brief   COMP library.
8   ******************************************************************************
9   * @attention
10   *
11   ******************************************************************************
12   */
13 #include "lib_comp.h"
14 
15 extern __IO uint32_t ana_reg3_tmp;
16 /**
17   * @brief  Comparator debounce configure.
18   * @param  COMPx:
19                 COMP_1
20                 COMP_2
21             Debounce:
22                 COMP_DEB_0
23                 COMP_DEB_1
24                 COMP_DEB_2
25                 COMP_DEB_3
26   * @retval None
27   */
COMP_DEBConfig(uint32_t COMPx,uint32_t Debounce)28 void COMP_DEBConfig(uint32_t COMPx, uint32_t Debounce)
29 {
30   uint32_t tmp;
31 
32   /* Check parameters */
33   assert_parameters(IS_COMP(COMPx));
34   assert_parameters(IS_COMP_DEB(Debounce));
35 
36   tmp = ANA->CTRL;
37   tmp &= ~(ANA_CTRL_CMP1DEB << COMPx);
38   tmp |= Debounce << COMPx;
39   ANA->CTRL = tmp;
40 }
41 
42 /**
43   * @brief  Comparator mode configure.
44   * @param  COMPx:
45                 COMP_1
46                 COMP_2
47             Mode:
48                 COMP_MODE_OFF
49                 COMP_MODE_RISING
50                 COMP_MODE_FALLING
51                 COMP_MODE_BOTH
52   * @retval None
53   */
COMP_ModeConfig(uint32_t COMPx,uint32_t Mode)54 void COMP_ModeConfig(uint32_t COMPx, uint32_t Mode)
55 {
56   uint32_t tmp;
57 
58   /* Check parameters */
59   assert_parameters(IS_COMP(COMPx));
60   assert_parameters(IS_COMP_MODE(Mode));
61 
62   tmp = ANA->CTRL;
63   tmp &= ~(ANA_CTRL_COMP1_SEL << COMPx);
64   tmp |= Mode << COMPx;
65   ANA->CTRL = tmp;
66 }
67 
68 /**
69   * @brief  Configure signal source.
70   * @param  COMPx:
71   *             COMP_1
72   *             COMP_2
73   *         SourceSelect:
74   *             COMP_SIGNALSRC_P_TO_REF
75   *             COMP_SIGNALSRC_N_TO_REF
76   *             COMP_SIGNALSRC_P_TO_N
77   * @retval None
78   */
COMP_SignalSourceConfig(uint32_t COMPx,uint32_t SourceSelect)79 void COMP_SignalSourceConfig(uint32_t COMPx, uint32_t SourceSelect)
80 {
81   uint32_t tmp;
82 
83   /* Check parameters */
84   assert_parameters(IS_COMP(COMPx));
85   assert_parameters(IS_COMP_SIGNALSRC(SourceSelect));
86 
87   tmp = ANA->REG2;
88   tmp &= ~(ANA_REG2_CMP1_SEL << COMPx);
89   tmp |= SourceSelect << COMPx;
90 
91   ANA->REG2 = tmp;
92 }
93 
94 /**
95   * @brief  Comparator configure REF selection.
96   * @param  COMPx:
97   *             COMP_1
98   *             COMP_2
99   *         REFSelect:
100   *             COMP_REF_VREF
101   *             COMP_REF_BGPREF
102   * @retval None
103   */
COMP_REFConfig(uint32_t COMPx,uint32_t REFSelect)104 void COMP_REFConfig(uint32_t COMPx, uint32_t REFSelect)
105 {
106   uint32_t tmp;
107 
108   /* Check parameters */
109   assert_parameters(IS_COMP(COMPx));
110   assert_parameters(IS_COMP_REF(REFSelect));
111 
112   tmp = ANA->REG2;
113   tmp &= ~(ANA_REG2_REFSEL_CMP1 << (COMPx / 2));
114   tmp |= REFSelect << (COMPx / 2);
115 
116   ANA->REG2 = tmp;
117 }
118 
119 /**
120   * @brief  Comparator configure Bias current selection.
121   * @param  COMPx:
122   *             COMP_1
123   *             COMP_2
124   *         BiasSel:
125   *             COMP_BIAS_20nA
126   *             COMP_BIAS_100nA
127   *             COMP_BIAS_500nA
128   * @retval None
129   */
COMP_BiasConfig(uint32_t COMPx,uint32_t BiasSel)130 void COMP_BiasConfig(uint32_t COMPx, uint32_t BiasSel)
131 {
132   uint32_t tmp;
133 
134   /* Check parameters */
135   assert_parameters(IS_COMP(COMPx));
136   assert_parameters(IS_COMP_BIAS(BiasSel));
137 
138   tmp = ANA->REG5;
139   tmp &= ~(ANA_REG5_IT_CMP1 << COMPx);
140   tmp |= BiasSel << COMPx;
141 
142   ANA->REG5 = tmp;
143 }
144 
145 /**
146   * @brief  Get comparator count value.
147   * @param  COMPx:
148                  COMP_1
149                  COMP_2
150   * @retval Comparator count value.
151   */
COMP_GetCNTValue(uint32_t COMPx)152 uint32_t COMP_GetCNTValue(uint32_t COMPx)
153 {
154   __IO uint32_t *addr;
155 
156   /* Check parameters */
157   assert_parameters(IS_COMP(COMPx));
158 
159   addr = &ANA->CMPCNT1 + (COMPx / 2);
160 
161   return (*addr);
162 }
163 
164 /**
165   * @brief  Clear comparator counter value.
166   * @param  COMPx:
167                  COMP_1
168                  COMP_2
169   * @retval None
170   */
COMP_ClearCNTValue(uint32_t COMPx)171 void COMP_ClearCNTValue(uint32_t COMPx)
172 {
173   __IO uint32_t *addr;
174 
175   /* Check parameters */
176   assert_parameters(IS_COMP(COMPx));
177 
178   addr = &ANA->CMPCNT1 + (COMPx / 2);
179   *addr = 0;
180 }
181 
182 /**
183   * @brief  comparator output enable control.
184   * @param  COMPx:
185                  COMP_1
186                  COMP_2
187             NewState:
188                 ENABLE
189                 DISABLE
190   * @retval None
191   */
COMP_Output_Cmd(uint32_t COMPx,uint32_t NewState)192 void COMP_Output_Cmd(uint32_t COMPx, uint32_t NewState)
193 {
194   /* Check parameters */
195   assert_parameters(IS_COMP(COMPx));
196   assert_parameters(IS_FUNCTIONAL_STATE(NewState));
197 
198   if (NewState == ENABLE)
199   {
200     if (COMPx == COMP_1)
201       GPIOAF->SELE |= IOE_SEL_SEL7;
202     else
203       PMU->IOASEL |= PMU_IOASEL_SEL6;
204   }
205   else
206   {
207     if (COMPx == COMP_1)
208       GPIOAF->SELE &= ~IOE_SEL_SEL7;
209     else
210       PMU->IOASEL &= ~PMU_IOASEL_SEL6;
211   }
212 }
213 
214 /**
215   * @brief  Comparator enable control.
216   * @param  COMPx:
217                  COMP_1
218                  COMP_2
219             NewState:
220                 ENABLE
221                 DISABLE
222   * @retval None
223   */
COMP_Cmd(uint32_t COMPx,uint32_t NewState)224 void COMP_Cmd(uint32_t COMPx, uint32_t NewState)
225 {
226   /* Check parameters */
227   assert_parameters(IS_COMP(COMPx));
228   assert_parameters(IS_FUNCTIONAL_STATE(NewState));
229 
230   if (COMPx == COMP_1)
231   {
232     if (NewState == ENABLE)
233       ana_reg3_tmp |= ANA_REG3_CMP1PDN;
234     else
235       ana_reg3_tmp &= ~ANA_REG3_CMP1PDN;
236   }
237   else
238   {
239     if (NewState == ENABLE)
240       ana_reg3_tmp |= ANA_REG3_CMP2PDN;
241     else
242       ana_reg3_tmp &= ~ANA_REG3_CMP2PDN;
243   }
244   ANA->REG3 = ana_reg3_tmp;
245 }
246 
247 /**
248   * @brief  Get comparator 1 output level
249   * @param  None
250   * @retval None
251   */
COMP1_GetOutputLevel(void)252 uint8_t COMP1_GetOutputLevel(void)
253 {
254   if (ANA->COMPOUT & ANA_COMPOUT_COMP1)
255     return 1;
256   else
257     return 0;
258 }
259 
260 /**
261   * @brief  Get comparator 2 output level
262   * @param  None
263   * @retval None
264   */
COMP2_GetOutputLevel(void)265 uint8_t COMP2_GetOutputLevel(void)
266 {
267   if (ANA->COMPOUT & ANA_COMPOUT_COMP2)
268     return 1;
269   else
270     return 0;
271 }
272 
273 /**
274   * @brief  Comparator interrupt enable control.
275   * @param  COMPx:
276   *              COMP_1
277   *              COMP_2
278   *         NewState:
279   *             ENABLE
280   *             DISABLE
281   * @retval None
282   */
COMP_INTConfig(uint32_t COMPx,uint32_t NewState)283 void COMP_INTConfig(uint32_t COMPx, uint32_t NewState)
284 {
285   /* Check parameters */
286   assert_parameters(IS_COMP(COMPx));
287 
288   if (NewState == ENABLE)
289   {
290     ANA->INTEN |= ANA_INTEN_INTEN2 << (COMPx/2);
291   }
292   else
293   {
294     ANA->INTEN &= ~(ANA_INTEN_INTEN2 << (COMPx/2));
295   }
296 }
297 
298 /**
299   * @brief  Get comparator interrupt flag status.
300   * @param  COMPx:
301   *              COMP_1
302   *              COMP_2
303   * @retval flag status
304   *              0: status not set
305   *              1: status set
306   */
COMP_GetINTStatus(uint32_t COMPx)307 uint8_t COMP_GetINTStatus(uint32_t COMPx)
308 {
309   /* Check parameters */
310   assert_parameters(IS_COMP(COMPx));
311 
312   if (ANA->INTSTS & (ANA_INTSTS_INTSTS2 << (COMPx/2)))
313   {
314     return 1;
315   }
316   else
317   {
318     return 0;
319   }
320 }
321 
322 /**
323   * @brief  Clear comparator interrupt flag.
324   * @param  COMPx:
325   *              COMP_1
326   *              COMP_2
327   * @retval None
328   */
COMP_ClearINTStatus(uint32_t COMPx)329 void COMP_ClearINTStatus(uint32_t COMPx)
330 {
331   /* Check parameters */
332   assert_parameters(IS_COMP(COMPx));
333 
334   ANA->INTSTS = ANA_INTSTS_INTSTS2 << (COMPx/2);
335 }
336 
337 /*********************************** END OF FILE ******************************/
338