Contiki 3.x
adc-sensor.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, University of Bristol - http://www.bristol.ac.uk
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 copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28  * OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 /*---------------------------------------------------------------------------*/
31 /**
32  * \addtogroup cc26xx-adc-sensor
33  * @{
34  *
35  * \file
36  * Driver for the CC13xx/CC26xx ADC
37  */
38 /*---------------------------------------------------------------------------*/
39 #include "contiki.h"
40 #include "lib/sensors.h"
41 #include "dev/adc-sensor.h"
42 #include "gpio-interrupt.h"
43 #include "sys/timer.h"
44 #include "lpm.h"
45 
46 #include "ti-lib.h"
47 #include "driverlib/aux_adc.h"
48 #include "aux-ctrl.h"
49 
50 #include <stdio.h>
51 #include <stdbool.h>
52 /*---------------------------------------------------------------------------*/
53 static uint8_t channel = ADC_COMPB_IN_AUXIO0;
54 static bool is_active = false;
55 
56 static aux_consumer_module_t adc_aux = {
57  .clocks = AUX_WUC_ADI_CLOCK | AUX_WUC_ANAIF_CLOCK | AUX_WUC_SMPH_CLOCK
58 };
59 /*---------------------------------------------------------------------------*/
60 static int
61 config(int type, int c)
62 {
63  switch(type) {
64  case SENSORS_ACTIVE:
65  is_active = c;
66 
67  if(is_active) {
68  /* Request AUX access, with ADI and SMPH clocks */
70 
71  ti_lib_aux_adc_select_input(channel);
72  } else {
74  }
75  break;
76 
77  case ADC_SENSOR_SET_CHANNEL:
78  channel = c;
79  if(is_active) {
80  ti_lib_aux_adc_select_input(channel);
81  }
82  break;
83 
84  default:
85  break;
86  }
87 
88  return 1;
89 }
90 /*---------------------------------------------------------------------------*/
91 static int
92 status(int type)
93 {
94  switch(type) {
95  case SENSORS_ACTIVE:
96  if(is_active) {
97  return 1;
98  }
99  break;
100  default:
101  break;
102  }
103  return 0;
104 }
105 /*---------------------------------------------------------------------------*/
106 static int
107 value(int type)
108 {
109  if(type == ADC_SENSOR_VALUE) {
110  int val;
111 
112  if(!is_active) {
113  puts("ADC not active");
114  return 0;
115  }
116 
117  ti_lib_aux_adc_enable_sync(AUXADC_REF_FIXED, AUXADC_SAMPLE_TIME_2P7_US,
118  AUXADC_TRIGGER_MANUAL);
119 
120  ti_lib_aux_adc_gen_manual_trigger();
121  val = ti_lib_aux_adc_read_fifo();
122 
123  ti_lib_aux_adc_disable();
124 
125  return val;
126  }
127 
128  return 0;
129 }
130 /*---------------------------------------------------------------------------*/
131 SENSORS_SENSOR(adc_sensor, ADC_SENSOR, value, config, status);
132 /*---------------------------------------------------------------------------*/
133 /** @} */
void aux_ctrl_register_consumer(aux_consumer_module_t *consumer)
Register a module that no longer requires access to the AUX power domain.
Definition: aux-ctrl.c:43
Header file with macros which rename TI CC26xxware functions.
Header file for the management of the CC13xx/CC26xx AUX domain.
Timer library header file.
The data structure to be used for modules that require access to AUX.
Definition: aux-ctrl.h:62
Header file for the CC13xx/CC26xx GPIO interrupt management.
void aux_ctrl_unregister_consumer(aux_consumer_module_t *consumer)
Deregister a module that no longer requires access to the AUX power domain.
Definition: aux-ctrl.c:60