Subversion Repositories NaviCtrl

Rev

Rev 1 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 ingob 1
/******************** (C) COPYRIGHT 2006 STMicroelectronics ********************
2
* File Name          : 91x_adc.c
3
* Author             : MCD Application Team
4
* Date First Issued  : 05/18/2006 : Version 1.0
5
* Description        : This file provides all the ADC software functions.
6
********************************************************************************
7
* History:
8
* 05/22/2007 : Version 1.2
9
* 05/24/2006 : Version 1.1
10
* 05/18/2006 : Version 1.0
11
********************************************************************************
12
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH
13
* CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS
14
* A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT
15
* OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT
16
* OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION
17
* CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
18
*******************************************************************************/
19
 
20
 
21
/* Standard include ----------------------------------------------------------*/
22
#include "91x_adc.h"
23
#include "91x_scu.h"
24
/* Include of other module interface headers ---------------------------------*/
25
/* Local includes ------------------------------------------------------------*/
26
/* Private typedef -----------------------------------------------------------*/
27
/* Private define ------------------------------------------------------------*/
28
 
29
/* ADC mask */
30
#define   ADC_FLAG_MASK           0x001F     /* ADC Flag Mask           */
31
#define   ADC_RESULT_MASK         0x03FF     /* ADC Result Mask         */
32
#define   ADC_SCAN_MODE_MASK      0x0020     /* ADC Sacn Mode Mask      */
33
#define   ADC_STANDBY_MODE_MASK   0x0008     /* ADC Standby Mode Mask   */
34
#define   ADC_CMD_MASK            0x0002     /* ADC Command Mask        */
35
#define   ADC_CHANNEL_MASK        0xFE3F     /* ADC Channel Select Mask */
36
/* Private macro -------------------------------------------------------------*/
37
/* Private variables ---------------------------------------------------------*/
38
/* Private function prototypes -----------------------------------------------*/
39
/* Interface functions -------------------------------------------------------*/
40
/* Private functions ---------------------------------------------------------*/
41
 
42
/*******************************************************************************
43
* Function Name  : ADC_DeInit
44
* Description    : Deinitialize the ADC module registers to their default reset
45
*                  values
46
* Input          : None
47
* Output         : None
48
* Return         : None
49
*******************************************************************************/
50
void ADC_DeInit(void)
51
{
52
  /* Reset the ADC registers values */
53
  SCU_APBPeriphReset(__ADC,ENABLE);
54
  SCU_APBPeriphReset(__ADC,DISABLE);
55
}
56
 
57
/*******************************************************************************
58
* Function Name  : ADC_Init
59
* Description    : Initializes ADC  peripheral according to the specified
60
*                  parameters in the ADC_InitTypeDef structure.
61
* Input          : ADC_InitStruct: pointer to a ADC_InitTypeDef structure that
62
*                  contains the configuration information for the specified
63
*                  ADC peripheral.
64
* Output         : None
65
* Return         : None
66
*******************************************************************************/
67
void ADC_Init(ADC_InitTypeDef* ADC_InitStruct)
68
{
69
  /* Set the low threshold of the watchdog */
70
  ADC->LTR = ADC_InitStruct->ADC_WDG_Low_Threshold;
71
 
72
  /* Set the high threshold of the watchdog */
73
  ADC->HTR = ADC_InitStruct->ADC_WDG_High_Threshold;
74
 
75
 
76
  /* Channel 0 conversion mode */
77
  ADC->CCR &= 0xFFFC;
78
  ADC->CCR |= ADC_InitStruct->ADC_Channel_0_Mode;
79
 
80
  /* Channel 1 conversion mode */
81
  ADC->CCR &= 0xFFF3;
82
  ADC->CCR |= ADC_InitStruct->ADC_Channel_1_Mode << 0x2;
83
 
84
  /* Channel 2 conversion mode */
85
  ADC->CCR &= 0xFFCF;
86
  ADC->CCR |= ADC_InitStruct->ADC_Channel_2_Mode << 0x4;
87
 
88
  /* Channel 3 conversion mode */
89
  ADC->CCR &= 0xFF3F;
90
  ADC->CCR |= ADC_InitStruct->ADC_Channel_3_Mode << 0x6;
91
 
92
  /* Channel 4 conversion mode */
93
  ADC->CCR &= 0xFCFF;
94
  ADC->CCR |= ADC_InitStruct->ADC_Channel_4_Mode << 0x8;
95
 
96
  /* Channel 5 conversion mode */
97
  ADC->CCR &= 0xF3FF;
98
  ADC->CCR |= ADC_InitStruct->ADC_Channel_5_Mode << 0xA;
99
 
100
  /* Channel 6 conversion mode */
101
  ADC->CCR &= 0xCFFF;
102
  ADC->CCR |= ADC_InitStruct->ADC_Channel_6_Mode << 0xC;
103
 
104
  /* Channel 7 conversion mode */
105
  ADC->CCR &= 0x3FFF;
106
  ADC->CCR |= ADC_InitStruct->ADC_Channel_7_Mode << 0xE;
107
 
108
  /* Select the channel to be converted */
109
  ADC->CR &= ADC_CHANNEL_MASK;
110
  ADC->CR |= ADC_InitStruct->ADC_Select_Channel << 0x6;
111
 
112
  /* Enable/disable the scan mode */
113
  if (ADC_InitStruct->ADC_Scan_Mode == ENABLE)
114
  {
115
    /* Enable the scan mode */
116
    ADC->CR |= ADC_SCAN_MODE_MASK;
117
  }
118
  else
119
  {
120
    /* Disable the scan mode */
121
    ADC->CR &= ~ADC_SCAN_MODE_MASK;
122
  }
123
 
124
  /* Configure the conversion mode */
125
  if (ADC_InitStruct->ADC_Conversion_Mode == ADC_Continuous_Mode)
126
  {
127
    /* ADC continuous mode */
128
    ADC->CR |= ADC_Continuous_Mode;
129
  }
130
  else
131
  {
132
    /* ADC single mode */
133
    ADC->CR &= ADC_Single_Mode;
134
  }
135
}
136
 
137
/*******************************************************************************
138
* Function Name  : ADC_StructInit
139
* Description    : Fills each ADC_InitStruct member with its reset value.
140
* Input          : ADC_InitStruct : pointer to a ADC_InitTypeDef structure
141
*                   which will be initialized.
142
* Output         : None
143
* Return         : None.
144
*******************************************************************************/
145
void ADC_StructInit(ADC_InitTypeDef* ADC_InitStruct)
146
{
147
  ADC_InitStruct->ADC_WDG_High_Threshold = 0x0000;
148
  ADC_InitStruct->ADC_WDG_Low_Threshold  = 0x0000;
149
  ADC_InitStruct->ADC_Channel_0_Mode     = ADC_No_Conversion;
150
  ADC_InitStruct->ADC_Channel_1_Mode     = ADC_No_Conversion;
151
  ADC_InitStruct->ADC_Channel_2_Mode     = ADC_No_Conversion;
152
  ADC_InitStruct->ADC_Channel_3_Mode     = ADC_No_Conversion;
153
  ADC_InitStruct->ADC_Channel_4_Mode     = ADC_No_Conversion;
154
  ADC_InitStruct->ADC_Channel_5_Mode     = ADC_No_Conversion;
155
  ADC_InitStruct->ADC_Channel_6_Mode     = ADC_No_Conversion;
156
  ADC_InitStruct->ADC_Channel_7_Mode     = ADC_No_Conversion;
157
  ADC_InitStruct->ADC_Select_Channel     = ADC_Channel_0;
158
  ADC_InitStruct->ADC_Scan_Mode          = DISABLE;
159
  ADC_InitStruct->ADC_Conversion_Mode    = ADC_Single_Mode;
160
}
161
 
162
/*******************************************************************************
163
* Function Name  : ADC_PrescalerConfig
164
* Description    : This routine is used to configure the ADC prescaler value.
165
* Input          : ADC_Prescaler: specifies the prescaler value. This parameter
166
*                  can be a value from 0x0 to 0xFF.
167
* Output         : None
168
* Return         : None
169
*******************************************************************************/
170
void ADC_PrescalerConfig(u8 ADC_Prescaler)
171
{
172
  ADC->PRS &= 0xFF00;
173
  ADC->PRS |= ADC_Prescaler;
174
 
175
}
176
/*******************************************************************************
177
* Function Name  : ADC_GetPrescalerValue
178
* Description    : This routine is used to get the ADC prescaler value.
179
* Input          : None
180
* Output         : None
181
* Return         : The prescaler value.
182
*******************************************************************************/
183
u8 ADC_GetPrescalerValue(void)
184
{
185
  return ADC->PRS & 0x00FF;
186
}
187
/*******************************************************************************
188
* Function Name  : ADC_GetFlagStatus
189
* Description    : Checks whether the specified ADC flag is set or not.
190
* Input          : ADC_Flag: flag to check.
191
*                  This parameter can be one of the following values:
192
*                     - ADC_FLAG_OV_CH_0: Conversion overflow status for
193
*                                         channel 0.
194
*                     - ADC_FLAG_OV_CH_1: Conversion overflow status for
195
*                                         channel 1.
196
*                     - ADC_FLAG_OV_CH_2: Conversion overflow status for
197
*                                         channel 2.
198
*                     - ADC_FLAG_OV_CH_3: Conversion overflow status for
199
*                                         channel 3.
200
*                     - ADC_FLAG_OV_CH_4: Conversion overflow status for
201
*                                         channel 4.
202
*                     - ADC_FLAG_OV_CH_5: Conversion overflow status for
203
*                                         channel 5.
204
*                     - ADC_FLAG_OV_CH_6: Conversion overflow status for
205
*                                         channel 6.
206
*                     - ADC_FLAG_OV_CH_7: Conversion overflow status for
207
*                                         channel 7.
208
*                     - ADC_FLAG_ECV:     End of conversion status.
209
*                     - ADC_FLAG_AWD:     Analog watchdog status.
210
* Output         : None
211
* Return         : The NewState of the ADC_Flag (SET or RESET).
212
*******************************************************************************/
213
FlagStatus ADC_GetFlagStatus(u16 ADC_Flag)
214
{
215
  u8 AdcReg = 0, FlagPos = 0;
216
 
217
  /* Get the ADC register index */
218
  AdcReg = ADC_Flag >> 5;
219
 
220
  /* Get the flag position */
221
  FlagPos = ADC_Flag & ADC_FLAG_MASK;
222
 
223
  if(AdcReg == 1) /* The flag to check is in CR register */
224
  {
225
    if((ADC->CR & (1<<FlagPos))!= RESET)
226
    {
227
      return SET;
228
    }
229
    else
230
    {
231
      return RESET;
232
    }
233
  }
234
  else if(AdcReg == 6) /* The flag to check is in DR0 register */
235
  {
236
    if((ADC->DR0 & (1<<FlagPos))!= RESET)
237
    {
238
      return SET;
239
    }
240
    else
241
    {
242
      return RESET;
243
    }
244
  }
245
  else if(AdcReg == 7) /* The flag to check is in DR1 register */
246
  {
247
    if((ADC->DR1 & (1<<FlagPos))!= RESET)
248
    {
249
      return SET;
250
    }
251
    else
252
    {
253
      return RESET;
254
    }
255
  }
256
  else if(AdcReg == 8) /* The flag to check is in DR2 register */
257
  {
258
    if((ADC->DR2 & (1<<FlagPos))!= RESET)
259
    {
260
      return SET;
261
    }
262
    else
263
    {
264
      return RESET;
265
    }
266
  }
267
  else if(AdcReg == 9) /* The flag to check is in DR3 register */
268
  {
269
    if((ADC->DR3 & (1<<FlagPos))!= RESET)
270
    {
271
      return SET;
272
    }
273
    else
274
    {
275
      return RESET;
276
    }
277
  }
278
 
279
  else if(AdcReg == 0xA) /* The flag to check is in DR4 register */
280
  {
281
    if((ADC->DR4 & (1<<FlagPos))!= RESET)
282
    {
283
      return SET;
284
    }
285
    else
286
    {
287
      return RESET;
288
    }
289
  }
290
  else if(AdcReg == 0xB) /* The flag to check is in DR5 register */
291
  {
292
    if((ADC->DR5 & (1<<FlagPos))!= RESET)
293
    {
294
      return SET;
295
    }
296
    else
297
    {
298
      return RESET;
299
    }
300
  }
301
  else if(AdcReg == 0xC) /* The flag to check is in DR6 register */
302
  {
303
    if((ADC->DR6 & (1<<FlagPos))!= RESET)
304
    {
305
      return SET;
306
    }
307
    else
308
    {
309
      return RESET;
310
    }
311
  }
312
  else /* (AdcReg == 0xD), The flag to check is in DR7 register */
313
  {
314
    if((ADC->DR7 & (1<<FlagPos))!= RESET)
315
    {
316
      return SET;
317
    }
318
    else
319
    {
320
      return RESET;
321
    }
322
  }
323
}
324
 
325
/*******************************************************************************
326
* Function Name  : ADC_ClearFlag
327
* Description    : Clears the ADC Flag passed as a parameter.
328
* Input          : ADC_Flag: flag to clear.
329
*                  This parameter can be one of the following values:
330
*                     - ADC_FLAG_ECV: End of conversion status.
331
*                     - ADC_FLAG_AWD: Analog watchdog status.
332
* Output         : None
333
* Return         : None
334
*******************************************************************************/
335
void ADC_ClearFlag(u16 ADC_Flag)
336
{  
337
  /* Clear the correspondent flag */
338
  ADC->CR |= (1<<(ADC_Flag & ADC_FLAG_MASK));
339
}
340
 
341
/*******************************************************************************
342
* Function Name  : ADC_GetConversionValue
343
* Description    : Read the result of conversion from the appropriate data
344
*                  register.
345
* Input          : ADC_Channel: the correspondent channel of the ADC peripheral.
346
*                  This parameter can be one of the following values:
347
*                     - ADC_Channel_0: ADC channel 0.
348
*                     - ADC_Channel_1: ADC channel 1.
349
*                     - ADC_Channel_2: ADC channel 2.
350
*                     - ADC_Channel_3: ADC channel 3.
351
*                     - ADC_Channel_4: ADC channel 4.
352
*                     - ADC_Channel_5: ADC channel 5.
353
*                     - ADC_Channel_6: ADC channel 6.
354
*                     - ADC_Channel_7: ADC channel 7.
355
* Output         : None
356
* Return         : The result of the conversion for the specific channel.
357
*******************************************************************************/
358
u16 ADC_GetConversionValue(u16 ADC_Channel)
359
{
360
  u16 ADC_Conversion_Value = 0;
361
 
362
  switch (ADC_Channel)
363
  {
364
    case (ADC_Channel_0):
365
      /* Get the conversion value of the channel 0 */
366
      ADC_Conversion_Value = ADC->DR0 & ADC_RESULT_MASK;
367
      break;
368
 
369
    case (ADC_Channel_1):
370
      /* Get the conversion value of the channel 1 */
371
      ADC_Conversion_Value = ADC->DR1 & ADC_RESULT_MASK;
372
      break;
373
 
374
    case (ADC_Channel_2):
375
      /* Get the conversion value of the channel 2 */
376
      ADC_Conversion_Value = ADC->DR2 & ADC_RESULT_MASK;
377
      break;
378
 
379
    case (ADC_Channel_3):
380
      /* Get the conversion value of the channel 3 */
381
      ADC_Conversion_Value = ADC->DR3 & ADC_RESULT_MASK;
382
      break;
383
 
384
    case (ADC_Channel_4):
385
      /* Get the conversion value of the channel 4 */
386
      ADC_Conversion_Value = ADC->DR4 & ADC_RESULT_MASK;
387
      break;
388
 
389
    case (ADC_Channel_5):
390
      /* Get the conversion value of the channel 5 */
391
      ADC_Conversion_Value = ADC->DR5 & ADC_RESULT_MASK;
392
      break;
393
 
394
    case (ADC_Channel_6):
395
      /* Get the conversion value of the channel 6 */
396
      ADC_Conversion_Value = ADC->DR6 & ADC_RESULT_MASK;
397
      break;
398
 
399
    case (ADC_Channel_7):
400
      /* Get the conversion value of the channel 7 */
401
      ADC_Conversion_Value = ADC->DR7 & ADC_RESULT_MASK;
402
      break;
403
 
404
    default:
405
      break;
406
  }
407
 
408
  return(ADC_Conversion_Value);
409
}
410
 
411
/*******************************************************************************
412
* Function Name  : ADC_GetAnalogWatchdogResult
413
* Description    : Return the result of the comparaison on the selected Analog
414
*                  Watchdog.
415
* Input          : ADC_Channel: the correspondent channel of the ADC peripheral.
416
*                  This parameter can be one of the following values:
417
*                     - ADC_Channel_0: ADC channel 0.
418
*                     - ADC_Channel_1: ADC channel 1.
419
*                     - ADC_Channel_2: ADC channel 2.
420
*                     - ADC_Channel_3: ADC channel 3.
421
*                     - ADC_Channel_4: ADC channel 4.
422
*                     - ADC_Channel_5: ADC channel 5.
423
*                     - ADC_Channel_6: ADC channel 6.
424
*                     - ADC_Channel_7: ADC channel 7.
425
* Output         : None
426
* Return         : The state of the comparision (SET or RESET).
427
*******************************************************************************/
428
FlagStatus ADC_GetAnalogWatchdogResult(u16 ADC_Channel)
429
{
430
  if (ADC->CRR & (1<<ADC_Channel) != RESET)
431
  {
432
      return SET;
433
  }
434
  else
435
  {
436
    return RESET;
437
  }
438
}
439
 
440
/*******************************************************************************
441
* Function Name  : ADC_ClearAnalogWatchdogResult
442
* Description    : Clear the result of the comparaison on the selected Analog
443
*                  Watchdog.
444
* Input          : ADC_Channel: the correspondent channel of the ADC peripheral.
445
*                  This parameter can be one of the following values:
446
*                     - ADC_Channel_0: ADC channel 0.
447
*                     - ADC_Channel_1: ADC channel 1.
448
*                     - ADC_Channel_2: ADC channel 2.
449
*                     - ADC_Channel_3: ADC channel 3.
450
*                     - ADC_Channel_4: ADC channel 4.
451
*                     - ADC_Channel_5: ADC channel 5.
452
*                     - ADC_Channel_6: ADC channel 6.
453
*                     - ADC_Channel_7: ADC channel 7.
454
* Output         : None
455
* Return         : None
456
*******************************************************************************/
457
void ADC_ClearAnalogWatchdogResult(u16 ADC_Channel)
458
{
459
  /* Clear the correspondent watchdog result */
460
  ADC->CRR = 1<<ADC_Channel;
461
}
462
 
463
/*******************************************************************************
464
* Function Name  : ADC_GetWatchdogThreshold
465
* Description    : Get the higher/lower thresholds values of the watchdog.
466
* Input          : ADC_Threshold: the lower or the higher threshold.
467
*                  This parameter can be one of the following values:
468
*                     - ADC_HigherThreshold: The higher threshold.
469
*                     - ADC_LowerThreshold: The lower threshold.
470
* Output         : None
471
* Return         : The selected threshold value.
472
*******************************************************************************/
473
u16 ADC_GetWatchdogThreshold(ADC_ThresholdType ADC_Threshold)
474
{
475
  u16 ADC_Threshold_Value = 0;
476
 
477
  switch (ADC_Threshold)
478
  {
479
    case ADC_LowThreshold:
480
      /* Get the low threshold of the watchdog */
481
      ADC_Threshold_Value = ADC->LTR;
482
      break;
483
 
484
    case ADC_HighThreshold:
485
      /* Get the high threshol of the watchdog */
486
      ADC_Threshold_Value = ADC->HTR;
487
      break;
488
 
489
    default:
490
      break;
491
  }
492
 
493
  return(ADC_Threshold_Value);
494
}
495
 
496
/*******************************************************************************
497
* Function Name  : ADC_ITConfig
498
* Description    : Enables or disables the specified ADC interrupts.
499
* Input          : - ADC_IT: specifies the ADC interrupts sources to be enabled
500
*                    or disabled.
501
*                    This parameter can be one of the following values:
502
*                       - ADC_IT_EndOfConversion: End of conversion interrupt.
503
*                       - ADC_IT_AnalogWDG: Analog watchdog interrupt.
504
*                  - ADC_NewState: new state of the specified ADC interrupts.
505
*                    (ADC_Newstate can be ENABLE or DISABLE).
506
* Output         : None
507
* Return         : None
508
*******************************************************************************/
509
void ADC_ITConfig(u16 ADC_IT, FunctionalState ADC_NewState)
510
{
511
  if (ADC_NewState == ENABLE)
512
  {
513
    /* Enable the interrupt */
514
    ADC->CR |= ADC_IT;
515
  }
516
  else
517
  {
518
    /* Disable the interrupt */
519
    ADC->CR &= ~ADC_IT;
520
  }
521
}
522
 
523
/*******************************************************************************
524
* Function Name  : ADC_StandbyModeCmd
525
* Description    : Enable or disable the standby mode.
526
* Input          : ADC_NewState: new state of the ADC standby mode.
527
*                  (ADC_Newstate can be ENABLE or DISABLE).
528
* Output         : None
529
* Return         : None
530
*******************************************************************************/
531
void ADC_StandbyModeCmd(FunctionalState ADC_NewState)
532
{
533
  if (ADC_NewState == ENABLE)
534
  {
535
    /* Enable the standby mode */
536
    ADC->CR |= ADC_STANDBY_MODE_MASK;
537
  }
538
  else
539
  {
540
    /* Disable the standby mode */
541
    ADC->CR &= ~ADC_STANDBY_MODE_MASK;
542
  }
543
}
544
 
545
/*******************************************************************************
546
* Function Name  : ADC_Cmd
547
* Description    : Power on or put in reset mode the ADC peripheral.
548
* Input          : ADC_NewState: new state of the ADC peripheral.
549
*                  (ADC_Newstate can be ENABLE or DISABLE).
550
* Output         : None
551
* Return         : None
552
*******************************************************************************/
553
void ADC_Cmd(FunctionalState ADC_NewState)
554
{
555
  if (ADC_NewState == ENABLE)
556
  {
557
    /* Enable the ADC */
558
    ADC->CR |= ADC_CMD_MASK;
559
  }
560
  else
561
  {
562
    /* Disable the ADC */
563
    ADC->CR &= ~ADC_CMD_MASK;
564
  }
565
}
566
 
567
/*******************************************************************************
568
* Function Name  : ADC_ConversionCmd
569
* Description    : Start or stop the ADC conversion in the selected mode.
570
* Input          : ADC_Conversion: the conversion command.
571
*                  This parameter can be one of the following values:
572
*                     - ADC_Conversion_Start: Start the conversion.
573
*                     - ADC_Conversion_Stop: Stop the Conversion.
574
* Output         : None
575
* Return         : None
576
*******************************************************************************/
577
void ADC_ConversionCmd(u16 ADC_Conversion)
578
{
579
  if (ADC_Conversion == ADC_Conversion_Start)
580
  {
581
    /* Start the ADC conversion */
582
    ADC->CR |= ADC_Conversion_Start;
583
  }
584
  else
585
  {
586
    /* Stop the ADC conversion */
587
    ADC->CR &= ADC_Conversion_Stop;
588
  }
589
}
590
 
591
/******************* (C) COPYRIGHT 2006 STMicroelectronics *****END OF FILE****/