summaryrefslogtreecommitdiff
path: root/bsp/Vango/v85xx/libraries/VangoV85xx_standard_peripheral/Source/lib_comp.c
blob: c0f899cfc394faef2ec85bc10260198a5ca758d1 (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
/**
  ******************************************************************************
  * @file    lib_comp.c 
  * @author  Application Team
  * @version V4.4.0
  * @date    2018-09-27
  * @brief   COMP library.
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */
#include "lib_comp.h"

extern __IO uint32_t ana_reg3_tmp;
/**
  * @brief  Comparator debounce configure.
  * @param  COMPx:
                COMP_1
                COMP_2
            Debounce:
                COMP_DEB_0
                COMP_DEB_1
                COMP_DEB_2
                COMP_DEB_3
  * @retval None
  */
void COMP_DEBConfig(uint32_t COMPx, uint32_t Debounce)
{
  uint32_t tmp;

  /* Check parameters */
  assert_parameters(IS_COMP(COMPx));
  assert_parameters(IS_COMP_DEB(Debounce));  
  
  tmp = ANA->CTRL;
  tmp &= ~(ANA_CTRL_CMP1DEB << COMPx);
  tmp |= Debounce << COMPx;
  ANA->CTRL = tmp;
}

/**
  * @brief  Comparator mode configure.
  * @param  COMPx:
                COMP_1
                COMP_2
            Mode:
                COMP_MODE_OFF    
                COMP_MODE_RISING 
                COMP_MODE_FALLING
                COMP_MODE_BOTH   
  * @retval None
  */
void COMP_ModeConfig(uint32_t COMPx, uint32_t Mode)
{
  uint32_t tmp;

  /* Check parameters */
  assert_parameters(IS_COMP(COMPx));
  assert_parameters(IS_COMP_MODE(Mode));
  
  tmp = ANA->CTRL;
  tmp &= ~(ANA_CTRL_COMP1_SEL << COMPx);
  tmp |= Mode << COMPx;
  ANA->CTRL = tmp; 
}

/**
  * @brief  Configure signal source.
  * @param  COMPx: 
  *             COMP_1
  *             COMP_2
  *         SourceSelect:
  *             COMP_SIGNALSRC_P_TO_REF
  *             COMP_SIGNALSRC_N_TO_REF
  *             COMP_SIGNALSRC_P_TO_N
  * @retval None
  */
void COMP_SignalSourceConfig(uint32_t COMPx, uint32_t SourceSelect)
{
  uint32_t tmp;

  /* Check parameters */
  assert_parameters(IS_COMP(COMPx));
  assert_parameters(IS_COMP_SIGNALSRC(SourceSelect));
  
  tmp = ANA->REG2;
  tmp &= ~(ANA_REG2_CMP1_SEL << COMPx);
  tmp |= SourceSelect << COMPx;
  
  ANA->REG2 = tmp;
}

/**
  * @brief  Comparator configure REF selection.
  * @param  COMPx: 
  *             COMP_1
  *             COMP_2
  *         REFSelect:
  *             COMP_REF_VREF
  *             COMP_REF_BGPREF
  * @retval None
  */
void COMP_REFConfig(uint32_t COMPx, uint32_t REFSelect)
{
  uint32_t tmp;

  /* Check parameters */
  assert_parameters(IS_COMP(COMPx));
  assert_parameters(IS_COMP_REF(REFSelect));
  
  tmp = ANA->REG2;
  tmp &= ~(ANA_REG2_REFSEL_CMP1 << (COMPx / 2));
  tmp |= REFSelect << (COMPx / 2);
  
  ANA->REG2 = tmp;
}

/**
  * @brief  Comparator configure Bias current selection.
  * @param  COMPx: 
  *             COMP_1
  *             COMP_2
  *         BiasSel:
  *             COMP_BIAS_20nA
  *             COMP_BIAS_100nA
  *             COMP_BIAS_500nA
  * @retval None
  */
void COMP_BiasConfig(uint32_t COMPx, uint32_t BiasSel)
{
  uint32_t tmp;

  /* Check parameters */
  assert_parameters(IS_COMP(COMPx));
  assert_parameters(IS_COMP_BIAS(BiasSel));  
  
  tmp = ANA->REG5;
  tmp &= ~(ANA_REG5_IT_CMP1 << COMPx);
  tmp |= BiasSel << COMPx;
  
  ANA->REG5 = tmp;
}

/**
  * @brief  Get comparator count value.
  * @param  COMPx:
                 COMP_1
                 COMP_2
  * @retval Comparator count value.
  */
uint32_t COMP_GetCNTValue(uint32_t COMPx)
{
  __IO uint32_t *addr;

  /* Check parameters */
  assert_parameters(IS_COMP(COMPx));
  
  addr = &ANA->CMPCNT1 + (COMPx / 2);
    
  return (*addr);
}

/**
  * @brief  Clear comparator counter value.
  * @param  COMPx:
                 COMP_1
                 COMP_2
  * @retval None
  */
void COMP_ClearCNTValue(uint32_t COMPx)
{
  __IO uint32_t *addr;

  /* Check parameters */
  assert_parameters(IS_COMP(COMPx));
  
  addr = &ANA->CMPCNT1 + (COMPx / 2);
  *addr = 0;
}

/**
  * @brief  comparator output enable control.
  * @param  COMPx:
                 COMP_1
                 COMP_2
            NewState: 
                ENABLE
                DISABLE
  * @retval None
  */
void COMP_Output_Cmd(uint32_t COMPx, uint32_t NewState)
{
  /* Check parameters */
  assert_parameters(IS_COMP(COMPx));
  assert_parameters(IS_FUNCTIONAL_STATE(NewState)); 
  
  if (NewState == ENABLE)
  {
    if (COMPx == COMP_1)
      GPIOAF->SELE |= IOE_SEL_SEL7;
    else
      PMU->IOASEL |= PMU_IOASEL_SEL6;
  }
  else
  {
    if (COMPx == COMP_1)
      GPIOAF->SELE &= ~IOE_SEL_SEL7;
    else
      PMU->IOASEL &= ~PMU_IOASEL_SEL6;  
  }
}

/**
  * @brief  Comparator enable control.
  * @param  COMPx:
                 COMP_1
                 COMP_2
            NewState: 
                ENABLE
                DISABLE
  * @retval None
  */
void COMP_Cmd(uint32_t COMPx, uint32_t NewState)
{
  /* Check parameters */
  assert_parameters(IS_COMP(COMPx));
  assert_parameters(IS_FUNCTIONAL_STATE(NewState)); 

  if (COMPx == COMP_1)
  {
    if (NewState == ENABLE)
      ana_reg3_tmp |= ANA_REG3_CMP1PDN;
    else
      ana_reg3_tmp &= ~ANA_REG3_CMP1PDN;
  }
  else
  {
    if (NewState == ENABLE)
      ana_reg3_tmp |= ANA_REG3_CMP2PDN;
    else
      ana_reg3_tmp &= ~ANA_REG3_CMP2PDN;
  }
  ANA->REG3 = ana_reg3_tmp;
}

/**
  * @brief  Get comparator 1 output level
  * @param  None
  * @retval None
  */
uint8_t COMP1_GetOutputLevel(void)
{
  if (ANA->COMPOUT & ANA_COMPOUT_COMP1)
    return 1;
  else
    return 0;
}

/**
  * @brief  Get comparator 2 output level
  * @param  None
  * @retval None
  */
uint8_t COMP2_GetOutputLevel(void)
{
  if (ANA->COMPOUT & ANA_COMPOUT_COMP2)
    return 1;
  else
    return 0;
}

/**
  * @brief  Comparator interrupt enable control.
  * @param  COMPx:
  *              COMP_1
  *              COMP_2
  *         NewState: 
  *             ENABLE
  *             DISABLE
  * @retval None
  */
void COMP_INTConfig(uint32_t COMPx, uint32_t NewState)
{
  /* Check parameters */
  assert_parameters(IS_COMP(COMPx));
  
  if (NewState == ENABLE)
  {
    ANA->INTEN |= ANA_INTEN_INTEN2 << (COMPx/2);
  }
  else
  {
    ANA->INTEN &= ~(ANA_INTEN_INTEN2 << (COMPx/2));
  }
}

/**
  * @brief  Get comparator interrupt flag status.
  * @param  COMPx:
  *              COMP_1
  *              COMP_2
  * @retval flag status
  *              0: status not set
  *              1: status set
  */
uint8_t COMP_GetINTStatus(uint32_t COMPx)
{
  /* Check parameters */
  assert_parameters(IS_COMP(COMPx));
  
  if (ANA->INTSTS & (ANA_INTSTS_INTSTS2 << (COMPx/2)))
  {
    return 1;
  }
  else
  {
    return 0;
  }
}

/**
  * @brief  Clear comparator interrupt flag.
  * @param  COMPx:
  *              COMP_1
  *              COMP_2
  * @retval None
  */
void COMP_ClearINTStatus(uint32_t COMPx)
{
  /* Check parameters */
  assert_parameters(IS_COMP(COMPx));
  
  ANA->INTSTS = ANA_INTSTS_INTSTS2 << (COMPx/2);
}

/*********************************** END OF FILE ******************************/