Contiki 3.x
adc-sensor.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 cc2538dk-adc-sensor
34  * @{
35  *
36  * \file
37  * Driver for the SmartRF06EB ADC
38  */
39 #include "contiki.h"
40 #include "sys/clock.h"
41 #include "dev/ioc.h"
42 #include "dev/gpio.h"
43 #include "dev/adc.h"
44 #include "dev/adc-sensor.h"
45 
46 #include <stdint.h>
47 
48 #define ADC_ALS_PWR_PORT_BASE GPIO_PORT_TO_BASE(ADC_ALS_PWR_PORT)
49 #define ADC_ALS_PWR_PIN_MASK GPIO_PIN_MASK(ADC_ALS_PWR_PIN)
50 #define ADC_ALS_OUT_PIN_MASK GPIO_PIN_MASK(ADC_ALS_OUT_PIN)
51 /*---------------------------------------------------------------------------*/
52 static int
53 value(int type)
54 {
55  uint8_t channel;
56  int16_t res;
57 
58  switch(type) {
59  case ADC_SENSOR_VDD_3:
60  channel = SOC_ADC_ADCCON_CH_VDD_3;
61  break;
62  case ADC_SENSOR_TEMP:
63  channel = SOC_ADC_ADCCON_CH_TEMP;
64  break;
65  case ADC_SENSOR_ALS:
67  GPIO_SET_PIN(ADC_ALS_PWR_PORT_BASE, ADC_ALS_PWR_PIN_MASK);
68  clock_delay_usec(2000);
69  break;
70  default:
71  return 0;
72  }
73 
75 
76  if(type == ADC_SENSOR_ALS) {
77  GPIO_CLR_PIN(ADC_ALS_PWR_PORT_BASE, ADC_ALS_PWR_PIN_MASK);
78  }
79 
80  return res;
81 }
82 /*---------------------------------------------------------------------------*/
83 static int
84 configure(int type, int value)
85 {
86  switch(type) {
87  case SENSORS_HW_INIT:
88  GPIO_SOFTWARE_CONTROL(ADC_ALS_PWR_PORT_BASE, ADC_ALS_PWR_PIN_MASK);
89  GPIO_SET_OUTPUT(ADC_ALS_PWR_PORT_BASE, ADC_ALS_PWR_PIN_MASK);
90  GPIO_CLR_PIN(ADC_ALS_PWR_PORT_BASE, ADC_ALS_PWR_PIN_MASK);
92 
93  GPIO_SOFTWARE_CONTROL(GPIO_A_BASE, ADC_ALS_OUT_PIN_MASK);
94  GPIO_SET_INPUT(GPIO_A_BASE, ADC_ALS_OUT_PIN_MASK);
96 
97  adc_init();
98  break;
99  }
100  return 0;
101 }
102 /*---------------------------------------------------------------------------*/
103 static int
104 status(int type)
105 {
106  return 1;
107 }
108 /*---------------------------------------------------------------------------*/
109 SENSORS_SENSOR(adc_sensor, ADC_SENSOR, value, configure, status);
110 
111 /** @} */
#define GPIO_A_NUM
GPIO_A: 0.
Definition: gpio.h:79
#define IOC_OVERRIDE_ANA
Analog Enable.
Definition: ioc.h:225
#define ADC_ALS_PWR_PIN
ALS power GPIO control pin.
Definition: board.h:179
#define GPIO_A_BASE
GPIO_A.
Definition: gpio.h:70
void clock_delay_usec(uint16_t dt)
Delay a given number of microseconds.
Definition: clock.c:94
#define ADC_ALS_OUT_PIN
ALS output ADC input pin on port A.
Definition: board.h:180
Header file with register and macro declarations for the cc2538 GPIO module.
#define ADC_SENSOR_VDD_3
On-chip VDD / 3.
Definition: adc-sensor.h:55
#define SOC_ADC_ADCCON_REF_INT
Internal reference.
Definition: soc-adc.h:89
#define GPIO_CLR_PIN(PORT_BASE, PIN_MASK)
Set pins with PIN_MASK of port with PORT_BASE low.
Definition: gpio.h:114
void ioc_set_over(uint8_t port, uint8_t pin, uint8_t over)
Set Port:Pin override function.
Definition: ioc.c:54
#define GPIO_SET_INPUT(PORT_BASE, PIN_MASK)
Set pins with PIN_MASK of port with PORT_BASE to input.
Definition: gpio.h:93
Header file with declarations for the I/O Control module.
#define SOC_ADC_ADCCON_DIV_512
512 decimation rate (12 bits ENOB)
Definition: soc-adc.h:96
#define SOC_ADC_ADCCON_CH_VDD_3
VDD/3.
Definition: soc-adc.h:111
#define IOC_OVERRIDE_DIS
Override Disabled.
Definition: ioc.h:226
#define GPIO_SOFTWARE_CONTROL(PORT_BASE, PIN_MASK)
Configure the pin to be software controlled with PIN_MASK of port with PORT_BASE. ...
Definition: gpio.h:214
#define GPIO_SET_OUTPUT(PORT_BASE, PIN_MASK)
Set pins with PIN_MASK of port with PORT_BASE to output.
Definition: gpio.h:100
#define ADC_SENSOR_TEMP
On-chip temperature.
Definition: adc-sensor.h:56
#define ADC_ALS_PWR_PORT
ALS power GPIO control port.
Definition: board.h:178
#define SOC_ADC_ADCCON_CH_AIN0
AIN0.
Definition: soc-adc.h:97
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 GPIO_SET_PIN(PORT_BASE, PIN_MASK)
Set pins with PIN_MASK of port with PORT_BASE high.
Definition: gpio.h:107
void adc_init(void)
Initializes the ADC controller.
Definition: adc.c:50
#define ADC_SENSOR_ALS
Ambient light sensor.
Definition: adc-sensor.h:57
#define SOC_ADC_ADCCON_CH_TEMP
Temperature sensor.
Definition: soc-adc.h:110