Contiki 3.x
adc-sensor.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011, George Oikonomou - <oikonomou@users.sourceforge.net>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  */
31 
32 /**
33  * \file
34  * ADC sensor module for TI SmartRF05EB devices.
35  *
36  * \author
37  * George Oikonomou - <oikonomou@users.sourceforge.net>
38  */
39 #include "sfr-bits.h"
40 #include "cc253x.h"
41 #include "adc-sensor.h"
42 
43 #if ADC_SENSOR_ON
44 /*---------------------------------------------------------------------------*/
45 static int
46 value(int type)
47 {
48  uint16_t reading;
49  /*
50  * For single-shot AD conversions, we may only write to ADCCON3[3:0] once
51  * (This write triggers the conversion). We thus use the variable 'command'
52  * to store intermediate steps (reference, decimation rate, input channel)
53  */
54  uint8_t command;
55 
56  /* 1.25V ref, max decimation rate */
57  command = ADCCON3_EDIV1 | ADCCON3_EDIV0;
58 
59  /* Clear the Interrupt Flag */
60  ADCIF = 0;
61 
62  /* Depending on the desired reading, append the input bits to 'command' and
63  * enable the corresponding input channel in ADCCFG if necessary */
64  switch(type) {
65 #if TEMP_SENSOR_ON
66  case ADC_SENSOR_TYPE_TEMP:
67  command |= ADCCON3_ECH3 | ADCCON3_ECH2 | ADCCON3_ECH1;
68  break;
69 #endif
70 #if VDD_SENSOR_ON
71  case ADC_SENSOR_TYPE_VDD:
72  command |= ADCCON3_ECH3 | ADCCON3_ECH2 | ADCCON3_ECH1 | ADCCON3_ECH0;
73  break;
74 #endif
75  default:
76  /* If the sensor is not present or disabled in conf, return -1 */
77  return -1;
78  }
79 
80  /* Writing in bits 3:0 of ADCCON3 will trigger a single conversion */
81  ADCCON3 = command;
82 
83  /*
84  * When the conversion is complete, the ADC interrupt flag is set. We don't
85  * use an ISR here, we just wait on the flag and clear it afterwards.
86  */
87  while(!ADCIF);
88 
89  /* Clear the Interrupt Flag */
90  ADCIF = 0;
91 
92  reading = ADCL;
93  reading |= (((uint8_t) ADCH) << 8);
94  /* 12-bit decimation rate: 4 LS bits are noise */
95  reading >>= 4;
96 
97  return reading;
98 }
99 /*---------------------------------------------------------------------------*/
100 static int
101 status(int type)
102 {
103  return 1;
104 }
105 /*---------------------------------------------------------------------------*/
106 static int
107 configure(int type, int value)
108 {
109  switch(type) {
110  case SENSORS_HW_INIT:
111 #if TEMP_SENSOR_ON
112  /* Connect temperature sensor to the SoC */
113  ATEST = 1;
114  TESTREG0 = 1;
115 #endif
116  APCFG = 0; /* Disables Input Channels */
117  break;
118  }
119  return 1;
120 }
121 /*---------------------------------------------------------------------------*/
122 SENSORS_SENSOR(adc_sensor, ADC_SENSOR, value, configure, status);
123 #endif /* ADC_SENSOR_ON */
Definitions for TI/Chipcon cc2530, cc2531 and cc2533 SFR registers.
Header file for ADC sensors on the SmartRF05EB.
Header file with definitions of bit masks for some cc2530 SFRs