| BlueNRG-1 Peripherals drivers APIs

- Main Page
- Modules
- Classes
- Files

- File List
- File Members

BlueNRG1_adc.c
Go to the documentation of this file.
1
22 /* Includes ------------------------------------------------------------------*/
23 #include "BlueNRG1_adc.h"
24
25
47 #define FS_SIGMA_DELTA ((float)(2.4))
48
49
81 float ADC_ConvertDifferentialVoltage(int32_t raw_value, uint8_t attenuation)
82 {
83 float pga = 0;
84
85 switch(attenuation) {
86 case ADC_Attenuation_0dB:
87 pga = 0;
88 break;
89 case ADC_Attenuation_6dB02:
90 pga = 1;
91 break;
92 case ADC_Attenuation_9dB54:
93 pga = 2;
94 break;
95 }
96
97 float divider, kadc;
98 if(ADC->CONF_b.ROUND16 == SET) {
99 divider = 65536.0;
100 kadc = 1.12;
101 }
102 else {
103 divider = 2147483648.0;
104 kadc = 1.68;
105 }
106
107 return (kadc * ( (pga + 1) * FS_SIGMA_DELTA )*(raw_value / divider));
108 }
109
117 float ADC_ConvertSingleEndedVoltage(int32_t raw_value, uint8_t channel, uint8_t vRef, uint8_t attenuation)
118 {
119 float pga = 0, vref = 0.0;
120 float divider, kadc;
121
122 switch(vRef) {
123 case ADC_ReferenceVoltage_0V:
124 vref = 0.0;
125 break;
126 case ADC_ReferenceVoltage_0V4:
127 vref = 0.4;
128 break;
129 case ADC_ReferenceVoltage_0V6:
130 vref = 0.6;
131 break;
132 case ADC_ReferenceVoltage_1V2:
133 vref = 1.2;
134 break;
135 }
136
137 switch(attenuation) {
138 case ADC_Attenuation_0dB:
139 pga = 0;
140 break;
141 case ADC_Attenuation_6dB02:
142 pga = 1;
143 break;
144 case ADC_Attenuation_9dB54:
145 pga = 2;
146 break;
147 }
148
149 if(ADC->CONF_b.ROUND16 == SET) {
150 divider = 16384.0;
151 kadc = 1.12;
152 }
153 else {
154 divider = 536870912.0;
155 kadc = 1.68;
156 }
157
158 if(channel == ADC_Input_AdcPin1)
159 return ((((pga + 1) * FS_SIGMA_DELTA) / 4.0) * (1 + kadc * (raw_value/divider)) + (vref - 0.6) * (pga + 1));
160 else
161 return ((((pga + 1) * FS_SIGMA_DELTA) / 4.0) * (1 - kadc * (raw_value/divider)) + (vref - 0.6) * (pga + 1));
162 }
163
164
171 float ADC_ConvertBatterySensor(int32_t raw_value, uint8_t vRef)
172 {
173 return ((4.36 * (ADC_ConvertSingleEndedVoltage(raw_value, ADC_Input_AdcPin2, vRef, ADC_Attenuation_0dB) - 0.622)) + 2.7);
174 }
175
182 float ADC_ConvertTemperatureSensor(int32_t raw_value, uint8_t vRef, uint8_t attenuation)
183 {
184 return ((410 * (ADC_ConvertSingleEndedVoltage(raw_value, ADC_Input_AdcPin2, vRef, ADC_Attenuation_0dB) - 0.735)) + 27.5);
185 }
186
187
202 void ADC_DeInit(void)
203 {
204 /* Enable ADC reset state */
205 ADC->CTRL_b.RESET = SET;
206 }
207
208
214 void ADC_StructInit(ADC_InitType* ADC_InitStruct)
215 {
216 /* Set the decimation rate */
217 ADC_InitStruct->ADC_DecimationRate = ADC_DecimationRate_200;
218
219 /* Select the input source */
220 ADC_InitStruct->ADC_Input = ADC_Input_None;
221
222 /* Set the reference voltage */
223 ADC_InitStruct->ADC_ReferenceVoltage = ADC_ReferenceVoltage_1V2;
224
225 /* Set the conversion mode */
226 ADC_InitStruct->ADC_ConversionMode = ADC_ConversionMode_Single;
227
228 /* Set the attenuation */
229 ADC_InitStruct->ADC_Attenuation = ADC_Attenuation_0dB;
230 }
231
232
240 void ADC_Init(ADC_InitType* ADC_InitStruct)
241 {
242 /* Check the parameters */
243 assert_param(IS_ADC_DECIMATIONRATE(ADC_InitStruct->ADC_DecimationRate));
244 assert_param(IS_ADC_INPUT(ADC_InitStruct->ADC_Input));
245 assert_param(IS_ADC_CONVERSIONMODE(ADC_InitStruct->ADC_ConversionMode));
246 assert_param(IS_ADC_ATTENUATION(ADC_InitStruct->ADC_Attenuation));
247 assert_param(IS_ADC_REFERENCEVOLTAGE(ADC_InitStruct->ADC_ReferenceVoltage));
248
249 /* Set the decimation rate */
250 ADC->CONF_b.DECIM_RATE = ADC_InitStruct->ADC_DecimationRate;
251
252 /* Select the input source */
253 if(ADC_InitStruct->ADC_Input == ADC_Input_Microphone) {
254 ADC->CONF_b.MIC_SEL = SET;
255 }
256 else {
257 ADC->CONF_b.MIC_SEL = RESET;
258 ADC->CONF_b.CHSEL = ADC_InitStruct->ADC_Input;
259 }
260
261 /* Set the reference voltage */
262 ADC->CONF_b.REFSEL = ADC_InitStruct->ADC_ReferenceVoltage;
263
264 /* Set the conversion mode */
265 if(ADC_InitStruct->ADC_ConversionMode == ADC_ConversionMode_Continuous) {
266 ADC->CONF_b.CONT = SET;
267 }
268 else {
269 ADC->CONF_b.CONT = RESET;
270 }
271
272 /* Set the attenuation */
273 ADC->CONF_b.PGASEL = ADC_InitStruct->ADC_Attenuation;
274
275 }
276
277
284 void ADC_Cmd(FunctionalState NewState)
285 {
286 /* Check the parameters */
287 assert_param(IS_FUNCTIONAL_STATE(NewState));
288
289 if(NewState==ENABLE) {
290 if(ADC->CONF_b.MIC_SEL) {
291 ADC->CTRL_b.MIC_ON = SET;
292 }
293 ADC->CTRL_b.SWSTART = SET;
294 ADC->CTRL_b.ON = SET;
295 }
296 else {
297 ADC->CTRL_b.ON = RESET;
298 ADC->CTRL_b.STOP = SET;
299 }
300 }
301
302
309 void ADC_DmaCmd(FunctionalState NewState)
310 {
311 /* Check the parameters */
312 assert_param(IS_FUNCTIONAL_STATE(NewState));
313
314 if(NewState==ENABLE) {
315 ADC->CTRL_b.DMA_EN = SET;
316 }
317 else {
318 ADC->CTRL_b.DMA_EN = RESET;
319 }
320 }
321
322
323
330 void ADC_Calibration(FunctionalState NewState)
331 {
332 /* Check the parameters */
333 assert_param(IS_FUNCTIONAL_STATE(NewState));
334
335 if(NewState==ENABLE) {
336 ADC->CTRL_b.CALEN = SET;
337 }
338 else {
339 ADC->CTRL_b.RSTCALEN = SET;
340 ADC->CTRL_b.CALEN = RESET;
341 }
342 }
343
344
351 void ADC_AutoOffsetUpdate(FunctionalState NewState)
352 {
353 /* Check the parameters */
354 assert_param(IS_FUNCTIONAL_STATE(NewState));
355
356 if(NewState==ENABLE) {
357 ADC->CTRL_b.AUTO_OFFSET = SET;
358 }
359 else {
360 ADC->CTRL_b.AUTO_OFFSET = RESET;
361 }
362 }
363
364
371 void ADC_ThresholdCheck(FunctionalState NewState)
372 {
373 /* Check the parameters */
374 assert_param(IS_FUNCTIONAL_STATE(NewState));
375
376 if(NewState==ENABLE) {
377 ADC->CTRL_b.ENAB_COMP = SET;
378 }
379 else {
380 ADC->CTRL_b.ENAB_COMP = RESET;
381 }
382 }
383
384
391 void ADC_ThresholdConfig(uint32_t ThresholdLow, uint32_t ThresholdHigh)
392 {
393
394 ADC->THRESHOLD_HI = ThresholdHigh;
395 ADC->THRESHOLD_LO = ThresholdLow;
396 }
397
398
406 void ADC_ConversionMode(uint8_t ConvertionMode)
407 {
408 /* Check the parameters */
409 assert_param(IS_ADC_CONVERSIONMODE(ConvertionMode));
410
411 if(ConvertionMode==ADC_ConversionMode_Continuous) {
412 ADC->CONF_b.CONT = SET;
413 }
414 else {
415 ADC->CONF_b.CONT = RESET;
416 }
417 }
418
425 void ADC_RoundConvertedData(FunctionalState NewState)
426 {
427 /* Check the parameters */
428 assert_param(IS_FUNCTIONAL_STATE(NewState));
429
430 if(NewState==ENABLE) {
431 ADC->CONF_b.ROUND16 = SET;
432 }
433 else {
434 ADC->CONF_b.ROUND16 = RESET;
435 }
436 }
437
445 void ADC_SelectFrequencyMic(uint8_t Frequency)
446 {
447 /* Check the parameters */
448 assert_param(IS_ADC_MIC_FREQ_SEL(Frequency));
449
450 /* Check the value of Frequency */
451 if(Frequency == ADC_MIC_800KHZ) {
452 ADC->CONF_b.DIG_FILT_CLK = RESET;
453 }
454 else if(Frequency == ADC_MIC_1600KHZ) {
455 ADC->CONF_b.DIG_FILT_CLK = SET;
456 }
457
458 }
459
460
471 FlagStatus ADC_GetFlagStatus(uint8_t ADC_Flag)
472 {
473 /* Check the parameters */
474 assert_param(IS_ADC_GET_FLAG(ADC_Flag));
475
476 /* Check the status of the specified SPI flag */
477 if (READ_BIT(ADC->SR_REG, ADC_Flag) != (uint16_t)RESET) {
478 /* SPI_FLAG is set */
479 return SET;
480 }
481 else {
482 /* SPI_FLAG is reset */
483 return RESET;
484 }
485
486 }
487
499 ITStatus ADC_GetITStatus(uint8_t ADC_Flag)
500 {
501 /* Check the parameters */
502 assert_param(IS_ADC_GET_FLAG(ADC_Flag));
503
504 /* Check the status of the specified SPI interrupt */
505 if (READ_BIT(ADC->IRQSTAT, ADC_Flag) != (uint16_t)RESET) {
506 /* ADC_Flag is set */
507 return SET;
508 }
509 else {
510 /* SPI_IT is reset */
511 return RESET;
512 }
513
514 }
515
516
517
529 void ADC_ITConfig(uint8_t ADC_Flag, FunctionalState NewState)
530 {
531 /* Check the parameters */
532 assert_param(IS_FUNCTIONAL_STATE(NewState));
533 assert_param(IS_ADC_GET_FLAG(ADC_Flag));
534
535 if (NewState != DISABLE) {
536 /* Enable the selected SPI interrupts */
537 CLEAR_BIT(ADC->IRQMASK, ADC_Flag);
538 }
539 else {
540 /* Disable the selected SPI interrupts */
541 SET_BIT(ADC->IRQMASK, ADC_Flag);
542 }
543 }
544
562 float ADC_GetConvertedData(uint8_t DataType, uint8_t Vref)
563 {
564 int32_t raw_value;
565 uint8_t pga_reg;
566
567 /* Check the parameters */
568 assert_param(IS_ADC_INPUT(DataType));
569 assert_param(IS_ADC_REFERENCEVOLTAGE(Vref));
570
571 if(ADC->CONF_b.ROUND16 == SET) {
572 raw_value = (int16_t)(ADC_GetRawData());
573 }
574 else {
575 raw_value = (int32_t)ADC_GetRawData();
576 }
577 pga_reg = ADC->CONF_b.PGASEL;
578
579 if(DataType==ADC_Input_AdcPin1) {
580 return ADC_ConvertSingleEndedVoltage(raw_value, (uint8_t)ADC_Input_AdcPin1, Vref, pga_reg);
581 }
582 else if(DataType==ADC_Input_AdcPin2) {
583 return ADC_ConvertSingleEndedVoltage(raw_value, (uint8_t)ADC_Input_AdcPin2, Vref, pga_reg);
584 }
585 else if(DataType==ADC_Input_AdcPin12 || DataType==ADC_Input_Internal0V60V6 || DataType==ADC_Input_Internal1V20V0) {
586 return ADC_ConvertDifferentialVoltage(raw_value, pga_reg);
587 }
588 else if(DataType==ADC_Input_BattSensor) {
589 return ADC_ConvertBatterySensor(raw_value, Vref);
590 }
591 else if(DataType==ADC_Input_TempSensor) {
592 return ADC_ConvertTemperatureSensor(raw_value, Vref, pga_reg);
593 }
594 return raw_value;
595 }
596
597
598
611 /******************* (C) COPYRIGHT 2016 STMicroelectronics *****END OF FILE****/
ADC_Cmd
void ADC_Cmd(FunctionalState NewState)
Enable disable the ADC conversion.
Definition: BlueNRG1_adc.c:284
ADC_MIC_800KHZ
#define ADC_MIC_800KHZ
Definition: BlueNRG1_adc.h:172
IS_ADC_INPUT
#define IS_ADC_INPUT(INPUT)
Definition: BlueNRG1_adc.h:107
IS_ADC_REFERENCEVOLTAGE
#define IS_ADC_REFERENCEVOLTAGE(REFERENCEVOLTAGE)
Definition: BlueNRG1_adc.h:160
ADC_Input_AdcPin12
#define ADC_Input_AdcPin12
Definition: BlueNRG1_adc.h:100
ADC_Input_Internal0V60V6
#define ADC_Input_Internal0V60V6
Definition: BlueNRG1_adc.h:103
ADC_ConversionMode_Single
#define ADC_ConversionMode_Single
Definition: BlueNRG1_adc.h:125
ADC_InitType::ADC_ReferenceVoltage
uint8_t ADC_ReferenceVoltage
Definition: BlueNRG1_adc.h:62
ADC_ReferenceVoltage_0V4
#define ADC_ReferenceVoltage_0V4
Definition: BlueNRG1_adc.h:156
ADC_GetITStatus
ITStatus ADC_GetITStatus(uint8_t ADC_Flag)
Get the status of the masked IT flag. The read operation will clear the flags.
Definition: BlueNRG1_adc.c:499
BlueNRG1_adc.h
This file contains all the functions prototypes for the ADC firmware library.
ADC_Attenuation_6dB02
#define ADC_Attenuation_6dB02
Definition: BlueNRG1_adc.h:140
IS_ADC_GET_FLAG
#define IS_ADC_GET_FLAG(FLAG)
Definition: BlueNRG1_adc.h:190
ADC_Input_Microphone
#define ADC_Input_Microphone
Definition: BlueNRG1_adc.h:105
ADC_ReferenceVoltage_0V6
#define ADC_ReferenceVoltage_0V6
Definition: BlueNRG1_adc.h:157
ADC_Calibration
void ADC_Calibration(FunctionalState NewState)
Enable disable the ADC calibration procedure.
Definition: BlueNRG1_adc.c:330
IS_ADC_ATTENUATION
#define IS_ADC_ATTENUATION(ATTENUATION)
Definition: BlueNRG1_adc.h:143
ADC_Input_None
#define ADC_Input_None
Definition: BlueNRG1_adc.h:97
ADC_ConvertSingleEndedVoltage
float ADC_ConvertSingleEndedVoltage(int32_t raw_value, uint8_t channel, uint8_t vRef, uint8_t attenuation)
Convert raw ADC value in converted single ended voltage value.
Definition: BlueNRG1_adc.c:117
ADC_Input_BattSensor
#define ADC_Input_BattSensor
Definition: BlueNRG1_adc.h:102
ADC_GetConvertedData
float ADC_GetConvertedData(uint8_t DataType, uint8_t Vref)
Get the ADC converted value in Volt.
Definition: BlueNRG1_adc.c:562
IS_ADC_MIC_FREQ_SEL
#define IS_ADC_MIC_FREQ_SEL(FREQ)
Definition: BlueNRG1_adc.h:175
ADC_Input_Internal1V20V0
#define ADC_Input_Internal1V20V0
Definition: BlueNRG1_adc.h:104
ADC_GetRawData
#define ADC_GetRawData()
Get the ADC raw value.
Definition: BlueNRG1_adc.h:232
ADC_ReferenceVoltage_1V2
#define ADC_ReferenceVoltage_1V2
Definition: BlueNRG1_adc.h:158
ADC_InitType::ADC_DecimationRate
uint8_t ADC_DecimationRate
Definition: BlueNRG1_adc.h:50
ADC_InitType::ADC_Attenuation
uint8_t ADC_Attenuation
Definition: BlueNRG1_adc.h:59
ADC_InitType::ADC_Input
uint8_t ADC_Input
Definition: BlueNRG1_adc.h:53
ADC_Attenuation_9dB54
#define ADC_Attenuation_9dB54
Definition: BlueNRG1_adc.h:141
ADC_StructInit
void ADC_StructInit(ADC_InitType *ADC_InitStruct)
Fills the ADC_InitStruct with default values.
Definition: BlueNRG1_adc.c:214
ADC_DecimationRate_200
#define ADC_DecimationRate_200
Definition: BlueNRG1_adc.h:80
ADC_ThresholdCheck
void ADC_ThresholdCheck(FunctionalState NewState)
Enable disable the ADC threshold check.
Definition: BlueNRG1_adc.c:371
ADC_Input_AdcPin1
#define ADC_Input_AdcPin1
Definition: BlueNRG1_adc.h:99
ADC_ConvertTemperatureSensor
float ADC_ConvertTemperatureSensor(int32_t raw_value, uint8_t vRef, uint8_t attenuation)
Convert raw ADC value in temperature level.
Definition: BlueNRG1_adc.c:182
ADC_MIC_1600KHZ
#define ADC_MIC_1600KHZ
Definition: BlueNRG1_adc.h:173
ADC_Attenuation_0dB
#define ADC_Attenuation_0dB
Definition: BlueNRG1_adc.h:139
ADC_InitType
Structure definition of ADC initialization.
Definition: BlueNRG1_adc.h:49
ADC_ConversionMode
void ADC_ConversionMode(uint8_t ConvertionMode)
Configure the ADC conversion mode.
Definition: BlueNRG1_adc.c:406
ADC_ConvertDifferentialVoltage
float ADC_ConvertDifferentialVoltage(int32_t raw_value, uint8_t attenuation)
Convert raw ADC value in converted differential voltage value.
Definition: BlueNRG1_adc.c:81
ADC_Input_TempSensor
#define ADC_Input_TempSensor
Definition: BlueNRG1_adc.h:101
ADC_DmaCmd
void ADC_DmaCmd(FunctionalState NewState)
Enable disable the ADC DMA feature.
Definition: BlueNRG1_adc.c:309
ADC_ConversionMode_Continuous
#define ADC_ConversionMode_Continuous
Definition: BlueNRG1_adc.h:126
ADC_RoundConvertedData
void ADC_RoundConvertedData(FunctionalState NewState)
Enable disable the ADC auto round output data.
Definition: BlueNRG1_adc.c:425
ADC_GetFlagStatus
FlagStatus ADC_GetFlagStatus(uint8_t ADC_Flag)
Get the status flag of ADC.
Definition: BlueNRG1_adc.c:471
ADC_ITConfig
void ADC_ITConfig(uint8_t ADC_Flag, FunctionalState NewState)
Enable disable the ADC IT flag.
Definition: BlueNRG1_adc.c:529
ADC_InitType::ADC_ConversionMode
uint8_t ADC_ConversionMode
Definition: BlueNRG1_adc.h:56
IS_ADC_CONVERSIONMODE
#define IS_ADC_CONVERSIONMODE(CONVERSIONMODE)
Definition: BlueNRG1_adc.h:128
ADC_SelectFrequencyMic
void ADC_SelectFrequencyMic(uint8_t Frequency)
Select the frequency of the clock signal for an external MEMS microphone (through IO0) ...
Definition: BlueNRG1_adc.c:445
IS_ADC_DECIMATIONRATE
#define IS_ADC_DECIMATIONRATE(DECIMATIONRATE)
Definition: BlueNRG1_adc.h:85
ADC_Input_AdcPin2
#define ADC_Input_AdcPin2
Definition: BlueNRG1_adc.h:98
ADC_ThresholdConfig
void ADC_ThresholdConfig(uint32_t ThresholdLow, uint32_t ThresholdHigh)
Configure the ADC threshold.
Definition: BlueNRG1_adc.c:391
ADC_ReferenceVoltage_0V
#define ADC_ReferenceVoltage_0V
Definition: BlueNRG1_adc.h:155
ADC_AutoOffsetUpdate
void ADC_AutoOffsetUpdate(FunctionalState NewState)
Enable disable the ADC automatic update of the offset.
Definition: BlueNRG1_adc.c:351
FS_SIGMA_DELTA
#define FS_SIGMA_DELTA
Definition: BlueNRG1_adc.c:47
ADC_Init
void ADC_Init(ADC_InitType *ADC_InitStruct)
Initializes the ADC peripheral according to the specified parameters in the ADC_InitStruct.
Definition: BlueNRG1_adc.c:240
ADC_ConvertBatterySensor
float ADC_ConvertBatterySensor(int32_t raw_value, uint8_t vRef)
Convert raw ADC value in battery level.
Definition: BlueNRG1_adc.c:171
ADC_DeInit
void ADC_DeInit(void)
Deinitializes ADC peripheral registers to their default reset values.
Definition: BlueNRG1_adc.c:202