Contiki-NG
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 "sys/timer.h"
43 #include "lpm.h"
44 
45 #include "ti-lib.h"
46 #include "driverlib/aux_adc.h"
47 #include "aux-ctrl.h"
48 
49 #include <stdio.h>
50 #include <stdbool.h>
51 /*---------------------------------------------------------------------------*/
52 static uint8_t channel = ADC_COMPB_IN_AUXIO0;
53 static bool is_active = false;
54 
55 static aux_consumer_module_t adc_aux = {
56  .clocks = AUX_WUC_ADI_CLOCK | AUX_WUC_ANAIF_CLOCK | AUX_WUC_SMPH_CLOCK
57 };
58 /*---------------------------------------------------------------------------*/
59 static int
60 config(int type, int c)
61 {
62  switch(type) {
63  case SENSORS_ACTIVE:
64  is_active = c;
65 
66  if(is_active) {
67  /* Request AUX access, with ADI and SMPH clocks */
69 
70  ti_lib_aux_adc_select_input(channel);
71  } else {
73  }
74  break;
75 
76  case ADC_SENSOR_SET_CHANNEL:
77  channel = c;
78  if(is_active) {
79  ti_lib_aux_adc_select_input(channel);
80  }
81  break;
82 
83  default:
84  break;
85  }
86 
87  return 1;
88 }
89 /*---------------------------------------------------------------------------*/
90 static int
91 status(int type)
92 {
93  switch(type) {
94  case SENSORS_ACTIVE:
95  if(is_active) {
96  return 1;
97  }
98  break;
99  default:
100  break;
101  }
102  return 0;
103 }
104 /*---------------------------------------------------------------------------*/
105 static int
106 value(int type)
107 {
108  if(type == ADC_SENSOR_VALUE) {
109  int val, adj_val, adj_mv;
110 
111  if(!is_active) {
112  puts("ADC not active");
113  return 0;
114  }
115 
116  ti_lib_aux_adc_enable_sync(AUXADC_REF_FIXED, AUXADC_SAMPLE_TIME_2P7_US,
117  AUXADC_TRIGGER_MANUAL);
118 
119  ti_lib_aux_adc_gen_manual_trigger();
120  val = ti_lib_aux_adc_read_fifo();
121  adj_val = ti_lib_aux_adc_adjust_value_for_gain_and_offset(
122  val,
123  ti_lib_aux_adc_get_adjustment_gain(AUXADC_REF_FIXED),
124  ti_lib_aux_adc_get_adjustment_offset(AUXADC_REF_FIXED));
125  adj_mv = ti_lib_aux_adc_value_to_microvolts(AUXADC_FIXED_REF_VOLTAGE_NORMAL, adj_val);
126  ti_lib_aux_adc_disable();
127 
128  return adj_mv;
129  }
130 
131  return 0;
132 }
133 /*---------------------------------------------------------------------------*/
134 SENSORS_SENSOR(adc_sensor, ADC_SENSOR, value, config, status);
135 /*---------------------------------------------------------------------------*/
136 /** @} */
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:61
Header file with macros which rename TI CC26xxware functions.
Timer library header file.
Header file for the CC13xx/CC26xx ADC driver.
Header file for the management of the CC13xx/CC26xx AUX domain.
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:44
static int config(int type, int c, nrf_drv_gpiote_pin_t pin)
Configuration function for the button sensor for all buttons.
The data structure to be used for modules that require access to AUX.
Definition: aux-ctrl.h:62