Contiki-NG
adc-zoul.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, Zolertia - http://www.zolertia.com
3  * Copyright (c) 2015, University of Bristol - http://www.bristol.ac.uk
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 /**
34  * \addtogroup zoul-adc-interface
35  * @{
36  *
37  * \file
38  * Generic driver for the Zoul ADC interface
39  */
40 /*---------------------------------------------------------------------------*/
41 #include "contiki.h"
42 #include "sys/clock.h"
43 #include "dev/ioc.h"
44 #include "dev/gpio.h"
45 #include "dev/adc.h"
46 #include "adc-zoul.h"
47 #include "zoul-sensors.h"
48 #include <stdio.h>
49 #include <stdint.h>
50 /*---------------------------------------------------------------------------*/
51 #define DEBUG 0
52 #if DEBUG
53 #define PRINTF(...) printf(__VA_ARGS__)
54 #else
55 #define PRINTF(...)
56 #endif
57 /*---------------------------------------------------------------------------*/
58 static uint8_t decimation_rate;
59 static uint8_t enabled_channels;
60 /*---------------------------------------------------------------------------*/
61 static int
62 set_decimation_rate(uint8_t rate)
63 {
64  switch(rate) {
69  decimation_rate = rate;
70  break;
71  default:
72  return ZOUL_SENSORS_ERROR;
73  }
74 
75  return decimation_rate;
76 }
77 /*---------------------------------------------------------------------------*/
78 static int
79 get_channel_pin(int type)
80 {
81  if((ZOUL_SENSORS_ADC1) && (type == ZOUL_SENSORS_ADC1)) {
83  }
84  if((ZOUL_SENSORS_ADC2) && (type == ZOUL_SENSORS_ADC2)) {
86  }
87  if((ZOUL_SENSORS_ADC3) && (type == ZOUL_SENSORS_ADC3)) {
89  }
90  if((ZOUL_SENSORS_ADC4) && (type == ZOUL_SENSORS_ADC4)) {
92  }
93  if((ZOUL_SENSORS_ADC5) && (type == ZOUL_SENSORS_ADC5)) {
95  }
96  if((ZOUL_SENSORS_ADC6) && (type == ZOUL_SENSORS_ADC6)) {
98  }
99  return ZOUL_SENSORS_ERROR;
100 }
101 /*---------------------------------------------------------------------------*/
102 static int
103 value(int type)
104 {
105  int channel;
106  int16_t res;
107 
108  if(!(type & enabled_channels)) {
109  PRINTF("ADC: channel not enabled\n");
110  return ZOUL_SENSORS_ERROR;
111  }
112 
113  channel = get_channel_pin(type);
114 
115  if(channel == ZOUL_SENSORS_ERROR) {
116  PRINTF("ADC: pin not active\n");
117  return ZOUL_SENSORS_ERROR;
118  }
119 
120  res = adc_get(channel, ADC_SENSORS_REFERENCE, decimation_rate);
121 
122  /* Only allow negative values if using differential input */
123  if((ADC_SENSORS_REFERENCE != SOC_ADC_ADCCON_REF_EXT_DIFF) && (res < 0)) {
124  res = 0;
125  }
126 
127  return res;
128 }
129 /*---------------------------------------------------------------------------*/
130 static int
131 configure(int type, int value)
132 {
133  switch(type) {
134  case SENSORS_HW_INIT:
135 
136  /* This should filter out disabled sensors as its value should be zero */
137  if((value < ZOUL_SENSORS_ADC_MIN) || (value > ZOUL_SENSORS_ADC_ALL)) {
138  PRINTF("ADC: invalid adc pin mask (0x%02X)\n", value);
139  return ZOUL_SENSORS_ERROR;
140  }
141 
143  GPIO_SET_INPUT(GPIO_A_BASE, value);
144 
145  if(value & ZOUL_SENSORS_ADC1) {
147  }
148  if(value & ZOUL_SENSORS_ADC2) {
150  }
151  if(value & ZOUL_SENSORS_ADC3) {
153  }
154  if(value & ZOUL_SENSORS_ADC4) {
156  }
157  if(value & ZOUL_SENSORS_ADC5) {
159  }
160  if(value & ZOUL_SENSORS_ADC6) {
162  }
163  adc_init();
164  set_decimation_rate(SOC_ADC_ADCCON_DIV_512);
165  enabled_channels |= value;
166  PRINTF("ADC: enabled channels 0x%02X\n", enabled_channels);
167  break;
168 
169  case ZOUL_SENSORS_CONFIGURE_TYPE_DECIMATION_RATE:
170  return set_decimation_rate((uint8_t)value);
171 
172  default:
173  return ZOUL_SENSORS_ERROR;
174  }
175  return 0;
176 }
177 /*---------------------------------------------------------------------------*/
178 static int
179 status(int type)
180 {
181  return 1;
182 }
183 /*---------------------------------------------------------------------------*/
184 SENSORS_SENSOR(adc_zoul, ADC_ZOUL, value, configure, status);
185 /*---------------------------------------------------------------------------*/
186 /** @} */
187 
#define SOC_ADC_ADCCON_REF_EXT_DIFF
External reference on AIN6-AIN7 differential input.
Definition: soc-adc.h:92
#define ADC_SENSORS_ADC1_PIN
ADC1 to PA5.
Definition: board.h:185
#define SOC_ADC_ADCCON_DIV_512
512 decimation rate (12 bits ENOB)
Definition: soc-adc.h:96
#define ADC_SENSORS_ADC6_PIN
ADC6 not declared.
Definition: board.h:235
#define SOC_ADC_ADCCON_DIV_128
128 decimation rate (9 bits ENOB)
Definition: soc-adc.h:94
Header file with register and macro declarations for the cc2538 GPIO module.
#define ADC_SENSORS_ADC4_PIN
ADC4 to PA6.
Definition: board.h:215
Header file with declarations for the I/O Control module.
#define ADC_SENSORS_ADC3_PIN
ADC3 to PA2.
Definition: board.h:205
#define IOC_OVERRIDE_ANA
Analog Enable.
Definition: ioc.h:225
void adc_init(void)
Initializes the ADC controller.
Definition: adc.c:50
Header file for the cc2538 ADC driver.
Implementation of a generic module controlling Zoul sensors.
#define ZOUL_SENSORS_ADC_MIN
PA1 pin mask.
Definition: adc-zoul.h:102
#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 SOC_ADC_ADCCON_DIV_64
64 decimation rate (7 bits ENOB)
Definition: soc-adc.h:93
#define SOC_ADC_ADCCON_DIV_256
256 decimation rate (10 bits ENOB)
Definition: soc-adc.h:95
#define GPIO_A_NUM
GPIO_A: 0.
Definition: gpio.h:64
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_A_BASE
GPIO_A.
Definition: gpio.h:55
#define ADC_SENSORS_ADC2_PIN
ADC2 to PA4.
Definition: board.h:195
#define ADC_SENSORS_ADC5_PIN
ADC5 to PA7.
Definition: board.h:225
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
Header file for the Zoul ADC interface.