Contiki 3.x
voltage.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, Eistec AB.
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 contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  * This file is part of the Mulle platform port of the Contiki operating system.
30  *
31  */
32 
33 /**
34  * \file
35  * Helper functions for reading Mulle platform board voltages.
36  * \author
37  * Joakim Gebart <joakim.gebart@eistec.se>
38  */
39 
40 #include "voltage.h"
41 #include "adc.h"
42 #include "K60.h"
43 #include "config-board.h"
44 
45 void
46 voltage_init(void)
47 {
48  SIM->SCGC3 |= SIM_SCGC3_ADC1_MASK; /* Enable ADC1 clock gate */
49 
50  /* ADC clock = Bus clock / 16 ( == 3MHz when F_BUS = 48 MHz) */
51  /* For the calibration it is important that the ADC clock is <= 4 MHz */
52  ADC1->CFG1 = ADC_CFG1_ADICLK(1) | ADC_CFG1_MODE(0b11) | ADC_CFG1_ADIV(0b11) |
53  ADC_CFG1_ADLPC_MASK | ADC_CFG1_ADLSMP_MASK;
54 
55  ADC1->CFG2 = ADC_CFG2_MUXSEL_MASK | ADC_CFG2_ADLSTS(0); /* Add 20 extra cycles per sample */
56  ADC1->SC2 = 0; /* Software trigger */
57  ADC1->SC3 = ADC_SC3_AVGE_MASK | ADC_SC3_AVGS(0b11); /* Hardware average 32 samples */
58 
59  /* Run a calibration for the new settings. */
60  adc_calibrate(MULLE_ADC_VBAT_ADC_NUM);
61 }
62 /**
63  * Scale a raw ADC reading from 0..65535 to millivolts depending on the board's
64  * VREFH, VREFL reference voltages.
65  */
66 uint16_t
67 voltage_from_raw_adc(uint16_t adc_raw)
68 {
69  uint32_t millivolts;
70  millivolts = adc_raw;
71  /* convert to millivolts */
73  millivolts /= 65536; /** \todo Nitpick: Should we divide by 65535 or 65536 in the ADC conversion? */
74  millivolts += MULLE_ADC_VREFL_MILLIVOLTS;
75 
76  return millivolts;
77 }
78 /** \todo Use interrupts to handle AD conversions of Vbat/Vchr */
79 uint16_t
81 {
82  uint16_t raw;
83  uint16_t millivolts;
84 
85  /* read the raw value (0..65535) */
86  raw = adc_read_raw(MULLE_ADC_VBAT_ADC_NUM, MULLE_ADC_VBAT_CHANNEL);
87  millivolts = voltage_from_raw_adc(raw);
88  /* The ADC inputs on Vbat and Vchr are connected to a voltage divider in order
89  * to be able to measure voltages greater than 3.3V */
90  millivolts *= 2;
91  return millivolts;
92 }
93 uint16_t
94 voltage_read_vchr()
95 {
96  uint16_t raw;
97  uint32_t millivolts;
98 
99  /* read the raw value (0..65535) */
100  raw = adc_read_raw(MULLE_ADC_VCHR_ADC_NUM, MULLE_ADC_VCHR_CHANNEL);
101  millivolts = voltage_from_raw_adc(raw);
102  /* The ADC inputs on Vbat and Vchr are connected to a voltage divider in order
103  * to be able to measure voltages greater than 3.3V */
104  millivolts *= 2;
105  return millivolts;
106 }
#define MULLE_ADC_VREFL_MILLIVOLTS
Voltage reference low for ADC computations (millivolts).
Definition: config-board.h:77
uint16_t voltage_read_vbat()
Definition: voltage.c:80
#define SIM
Peripheral SIM base pointer.
Definition: MK60D10.h:7650
Board configuration defines for Mulle platform.
K60 hardware register header wrapper.
#define ADC1
Peripheral ADC1 base pointer.
Definition: MK60D10.h:468
#define MULLE_ADC_VBAT_ADC_NUM
Which channel should perform Vbat measurements.
Definition: config-board.h:87
uint16_t voltage_from_raw_adc(uint16_t adc_raw)
Scale a raw ADC reading from 0..65535 to millivolts depending on the board&#39;s VREFH, VREFL reference voltages.
Definition: voltage.c:67
#define MULLE_ADC_VREFHL_SCALE_MILLIVOLTS
Total span of ADC measurement (millivolts).
Definition: config-board.h:82
Helper functions for reading Mulle platform board voltages.