Contiki-NG
adc.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013, ADVANSEE - http://www.advansee.com/
3  * Benoît Thébaudeau <benoit.thebaudeau@advansee.com>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its
16  * contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30  * OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 /**
33  * \addtogroup cc2538-adc
34  * @{
35  *
36  * \file
37  * Implementation of the cc2538 ADC driver
38  */
39 #include "contiki.h"
40 #include "dev/soc-adc.h"
41 #include "dev/cctest.h"
42 #include "dev/rfcore-xreg.h"
43 #include "dev/adc.h"
44 #include "reg.h"
45 
46 #include <stdint.h>
47 
48 /*---------------------------------------------------------------------------*/
49 void
50 adc_init(void)
51 {
52  /* Start conversions only manually */
54 }
55 /*---------------------------------------------------------------------------*/
56 int16_t
57 adc_get(uint8_t channel, uint8_t ref, uint8_t div)
58 {
59  uint32_t cctest_tr0, rfcore_xreg_atest;
60  int16_t res;
61 
62  /* On-chip temperature sensor */
63  if(channel == SOC_ADC_ADCCON_CH_TEMP) {
64  /* Connect the temperature sensor to the ADC */
65  cctest_tr0 = REG(CCTEST_TR0);
66  REG(CCTEST_TR0) = cctest_tr0 | CCTEST_TR0_ADCTM;
67 
68  /* Enable the temperature sensor */
69  rfcore_xreg_atest = REG(RFCORE_XREG_ATEST);
70  REG(RFCORE_XREG_ATEST) = (rfcore_xreg_atest & ~RFCORE_XREG_ATEST_ATEST_CTRL) |
72  }
73 
74  /* Start a single extra conversion with the given parameters */
75  REG(SOC_ADC_ADCCON3) = (REG(SOC_ADC_ADCCON3) &
77  ref | div | channel;
78 
79  /* Poll until end of conversion */
80  while(!(REG(SOC_ADC_ADCCON1) & SOC_ADC_ADCCON1_EOC));
81 
82  /* Read conversion result, reading SOC_ADC_ADCH last to clear
83  * SOC_ADC_ADCCON1.EOC */
84  res = REG(SOC_ADC_ADCL) & 0xfc;
85  res |= REG(SOC_ADC_ADCH) << 8;
86 
87  /* On-chip temperature sensor */
88  if(channel == SOC_ADC_ADCCON_CH_TEMP) {
89  /* Restore the initial temperature sensor state and connection (better for
90  * power consumption) */
91  REG(RFCORE_XREG_ATEST) = rfcore_xreg_atest;
92  REG(CCTEST_TR0) = cctest_tr0;
93  }
94 
95  /* Return conversion result */
96  return res;
97 }
98 
99 /** @} */
#define SOC_ADC_ADCCON3
ADC Control 3.
Definition: soc-adc.h:51
#define CCTEST_TR0_ADCTM
Connect temperature sensor to ADC.
Definition: cctest.h:82
#define SOC_ADC_ADCCON3_ECH
Single channel select.
Definition: soc-adc.h:83
Header file with register manipulation macro definitions.
#define CCTEST_TR0
Test register 0.
Definition: cctest.h:61
void adc_init(void)
Initializes the ADC controller.
Definition: adc.c:50
Header file with register declarations for the cc2538 ADC and H/W RNG.
Header file for the cc2538 ADC driver.
#define SOC_ADC_ADCCON1_EOC
End of conversion.
Definition: soc-adc.h:62
Header with declarations of CCTEST module registers.
#define RFCORE_XREG_ATEST_ATEST_CTRL_TEMP
Analog test mode: enable temperature sensor.
Definition: rfcore-xreg.h:579
#define RFCORE_XREG_ATEST
Analog test control.
Definition: rfcore-xreg.h:104
#define SOC_ADC_ADCCON1
ADC Control 1.
Definition: soc-adc.h:49
#define SOC_ADC_ADCL
ADC Result, least significant part.
Definition: soc-adc.h:52
#define SOC_ADC_ADCCON3_EREF
Reference voltage for extra.
Definition: soc-adc.h:81
#define SOC_ADC_ADCCON_CH_TEMP
Temperature sensor.
Definition: soc-adc.h:110
int16_t adc_get(uint8_t channel, uint8_t ref, uint8_t div)
Performs a single conversion on a given ADC channel.
Definition: adc.c:57
#define SOC_ADC_ADCCON1_STSEL
Start select.
Definition: soc-adc.h:64
#define SOC_ADC_ADCCON3_EDIV
Decimation rate for extra.
Definition: soc-adc.h:82
#define RFCORE_XREG_ATEST_ATEST_CTRL
Controls the analog test mode.
Definition: rfcore-xreg.h:577
Header with declarations of the RF Core XREGs.
#define SOC_ADC_ADCH
ADC Result, most significant part.
Definition: soc-adc.h:53