From 487547d8afb93826f12b6aa2d9a18d16a3d7d0ee Mon Sep 17 00:00:00 2001 From: Atis Elsts Date: Thu, 21 Apr 2016 16:55:01 +0300 Subject: [PATCH] Add CC13xx/CC26xx ADC driver --- cpu/cc26xx-cc13xx/Makefile.cc26xx-cc13xx | 2 +- cpu/cc26xx-cc13xx/dev/adc-sensor.c | 133 +++++++++++++++++++++++ cpu/cc26xx-cc13xx/dev/adc-sensor.h | 61 +++++++++++ 3 files changed, 195 insertions(+), 1 deletion(-) create mode 100644 cpu/cc26xx-cc13xx/dev/adc-sensor.c create mode 100644 cpu/cc26xx-cc13xx/dev/adc-sensor.h diff --git a/cpu/cc26xx-cc13xx/Makefile.cc26xx-cc13xx b/cpu/cc26xx-cc13xx/Makefile.cc26xx-cc13xx index 40638e80e..b9b755057 100644 --- a/cpu/cc26xx-cc13xx/Makefile.cc26xx-cc13xx +++ b/cpu/cc26xx-cc13xx/Makefile.cc26xx-cc13xx @@ -69,7 +69,7 @@ CONTIKI_CPU_DIRS += ../arm/common/dbg-io ### CPU-dependent source files CONTIKI_CPU_SOURCEFILES += clock.c rtimer-arch.c soc-rtc.c uart.c CONTIKI_CPU_SOURCEFILES += contiki-watchdog.c aux-ctrl.c -CONTIKI_CPU_SOURCEFILES += putchar.c ieee-addr.c batmon-sensor.c +CONTIKI_CPU_SOURCEFILES += putchar.c ieee-addr.c batmon-sensor.c adc-sensor.c CONTIKI_CPU_SOURCEFILES += slip-arch.c slip.c cc26xx-uart.c lpm.c CONTIKI_CPU_SOURCEFILES += gpio-interrupt.c oscillators.c CONTIKI_CPU_SOURCEFILES += rf-core.c rf-ble.c ieee-mode.c diff --git a/cpu/cc26xx-cc13xx/dev/adc-sensor.c b/cpu/cc26xx-cc13xx/dev/adc-sensor.c new file mode 100644 index 000000000..3ccf90e6d --- /dev/null +++ b/cpu/cc26xx-cc13xx/dev/adc-sensor.c @@ -0,0 +1,133 @@ +/* + * Copyright (c) 2016, University of Bristol - http://www.bristol.ac.uk + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ +/*---------------------------------------------------------------------------*/ +/** + * \addtogroup cc26xx-adc-sensor + * @{ + * + * \file + * Driver for the CC13xx/CC26xx ADC + */ +/*---------------------------------------------------------------------------*/ +#include "contiki.h" +#include "lib/sensors.h" +#include "dev/adc-sensor.h" +#include "gpio-interrupt.h" +#include "sys/timer.h" +#include "lpm.h" + +#include "ti-lib.h" +#include "driverlib/aux_adc.h" +#include "aux-ctrl.h" + +#include +#include +/*---------------------------------------------------------------------------*/ +static uint8_t channel = ADC_COMPB_IN_AUXIO0; +static bool is_active = false; + +static aux_consumer_module_t adc_aux = { + .clocks = AUX_WUC_ADI_CLOCK | AUX_WUC_ANAIF_CLOCK | AUX_WUC_SMPH_CLOCK +}; +/*---------------------------------------------------------------------------*/ +static int +config(int type, int c) +{ + switch(type) { + case SENSORS_ACTIVE: + is_active = c; + + if(is_active) { + /* Request AUX access, with ADI and SMPH clocks */ + aux_ctrl_register_consumer(&adc_aux); + + ti_lib_aux_adc_select_input(channel); + } else { + aux_ctrl_unregister_consumer(&adc_aux); + } + break; + + case ADC_SENSOR_SET_CHANNEL: + channel = c; + if(is_active) { + ti_lib_aux_adc_select_input(channel); + } + break; + + default: + break; + } + + return 1; +} +/*---------------------------------------------------------------------------*/ +static int +status(int type) +{ + switch(type) { + case SENSORS_ACTIVE: + if(is_active) { + return 1; + } + break; + default: + break; + } + return 0; +} +/*---------------------------------------------------------------------------*/ +static int +value(int type) +{ + if(type == ADC_SENSOR_VALUE) { + int val; + + if(!is_active) { + puts("ADC not active"); + return 0; + } + + ti_lib_aux_adc_enable_sync(AUXADC_REF_FIXED, AUXADC_SAMPLE_TIME_2P7_US, + AUXADC_TRIGGER_MANUAL); + + ti_lib_aux_adc_gen_manual_trigger(); + val = ti_lib_aux_adc_read_fifo(); + + ti_lib_aux_adc_disable(); + + return val; + } + + return 0; +} +/*---------------------------------------------------------------------------*/ +SENSORS_SENSOR(adc_sensor, ADC_SENSOR, value, config, status); +/*---------------------------------------------------------------------------*/ +/** @} */ diff --git a/cpu/cc26xx-cc13xx/dev/adc-sensor.h b/cpu/cc26xx-cc13xx/dev/adc-sensor.h new file mode 100644 index 000000000..fd8f71537 --- /dev/null +++ b/cpu/cc26xx-cc13xx/dev/adc-sensor.h @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2016, University of Bristol - http://www.bristol.ac.uk + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ +/*---------------------------------------------------------------------------*/ +/** + * \addtogroup cc26xx + * @{ + * + * \defgroup cc26xx-adc-sensor CC13xx/CC26xx ADC Sensor + * @{ + * + * \file + * Header file for the CC13xx/CC26xx ADC driver + */ +/*---------------------------------------------------------------------------*/ +#ifndef ADC_SENSOR_H_ +#define ADC_SENSOR_H_ +/*---------------------------------------------------------------------------*/ +#include "lib/sensors.h" +/*---------------------------------------------------------------------------*/ +#define ADC_SENSOR "ADC" +/*---------------------------------------------------------------------------*/ +#define ADC_SENSOR_VALUE 0 +/*---------------------------------------------------------------------------*/ +/* configuration commands */ +#define ADC_SENSOR_SET_CHANNEL 1 /* takes ADC_COMPB_IN_AUXIxx as parameter */ +/*---------------------------------------------------------------------------*/ +extern const struct sensors_sensor adc_sensor; +/*---------------------------------------------------------------------------*/ +#endif /* ADC_SENSOR_H_ */ +/*---------------------------------------------------------------------------*/ +/** + * @} + * @} + */