Contiki-NG
als-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-als-sensor
34  * @{
35  *
36  * \file
37  * Driver for the SmartRF06EB ALS
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/als-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 = SOC_ADC_ADCCON_CH_AIN0 + ADC_ALS_OUT_PIN;
56  int16_t res;
57 
58  GPIO_SET_PIN(ADC_ALS_PWR_PORT_BASE, ADC_ALS_PWR_PIN_MASK);
59  clock_delay_usec(2000);
60 
62 
63  GPIO_CLR_PIN(ADC_ALS_PWR_PORT_BASE, ADC_ALS_PWR_PIN_MASK);
64 
65  return res;
66 }
67 /*---------------------------------------------------------------------------*/
68 static int
69 configure(int type, int value)
70 {
71  switch(type) {
72  case SENSORS_HW_INIT:
73  GPIO_SOFTWARE_CONTROL(ADC_ALS_PWR_PORT_BASE, ADC_ALS_PWR_PIN_MASK);
74  GPIO_SET_OUTPUT(ADC_ALS_PWR_PORT_BASE, ADC_ALS_PWR_PIN_MASK);
75  GPIO_CLR_PIN(ADC_ALS_PWR_PORT_BASE, ADC_ALS_PWR_PIN_MASK);
77 
78  GPIO_SOFTWARE_CONTROL(GPIO_A_BASE, ADC_ALS_OUT_PIN_MASK);
79  GPIO_SET_INPUT(GPIO_A_BASE, ADC_ALS_OUT_PIN_MASK);
81 
82  break;
83  }
84  return 0;
85 }
86 /*---------------------------------------------------------------------------*/
87 static int
88 status(int type)
89 {
90  return 1;
91 }
92 /*---------------------------------------------------------------------------*/
93 SENSORS_SENSOR(als_sensor, ALS_SENSOR, value, configure, status);
94 
95 /** @} */
#define GPIO_SET_PIN(PORT_BASE, PIN_MASK)
Set pins with PIN_MASK of port with PORT_BASE high.
Definition: gpio.h:106
#define SOC_ADC_ADCCON_DIV_512
512 decimation rate (12 bits ENOB)
Definition: soc-adc.h:96
Header file with register and macro declarations for the cc2538 GPIO module.
#define GPIO_CLR_PIN(PORT_BASE, PIN_MASK)
Set pins with PIN_MASK of port with PORT_BASE low.
Definition: gpio.h:113
Header file with declarations for the I/O Control module.
void clock_delay_usec(uint16_t dt)
Delay a given number of microseconds.
Definition: clock.c:150
#define IOC_OVERRIDE_ANA
Analog Enable.
Definition: ioc.h:225
#define IOC_OVERRIDE_DIS
Override Disabled.
Definition: ioc.h:226
Header file for the cc2538 ADC driver.
#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:258
#define ADC_ALS_PWR_PIN
ALS power GPIO control pin.
Definition: board.h:179
#define GPIO_A_NUM
GPIO_A: 0.
Definition: gpio.h:64
#define SOC_ADC_ADCCON_REF_INT
Internal reference.
Definition: soc-adc.h:89
Header file for the cc2538dk ALS Driver.
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:78
#define SOC_ADC_ADCCON_CH_AIN0
AIN0.
Definition: soc-adc.h:97
#define GPIO_SET_OUTPUT(PORT_BASE, PIN_MASK)
Set pins with PIN_MASK of port with PORT_BASE to output.
Definition: gpio.h:85
#define GPIO_A_BASE
GPIO_A.
Definition: gpio.h:55
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 ADC_ALS_PWR_PORT
ALS power GPIO control port.
Definition: board.h:178
#define ADC_ALS_OUT_PIN
ALS output ADC input pin on port A.
Definition: board.h:180
const struct sensors_sensor als_sensor
Exports a global symbol to be used by the sensor API.