Contiki 3.x
adc.c
1 /*
2  * Copyright (c) 2010, Mariano Alvira <mar@devl.org> and other contributors
3  * to the MC1322x project (http://mc1322x.devl.org)
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  * 3. Neither the name of the Institute nor the names of its contributors
15  * may be used to endorse or promote products derived from this software
16  * without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * This file is part of libmc1322x: see http://mc1322x.devl.org
31  * for details.
32  *
33  *
34  */
35 
36 #include <stdint.h>
37 #include "mc1322x.h"
38 
39 //#define ADC_CHANS_ENABLED 0x3F // channels 0-5 enabled
40 //#define ADC_CHANS_ENABLED 0x7E // channels 1-6 enabled
41 //#define ADC_CHANS_ENABLED (1 << 6) // only channel 6 enabled
42 #define ADC_CHANS_ENABLED 0x1FF // all channels, including battery reference voltage
43 
44 //#define ADC_PRESCALE_VALUE 24 // divide by 24 for 1MHz.
45 
46 #define ADC_SAMPLE_FREQ 400 // Hz (minimum of ~366.21 Hz)
47 #define ADC_PRESCALE_VALUE (REF_OSC / ADC_SAMPLE_FREQ)
48 
49 #define ADC_USE_TIMER 0
50 #define ADC_USE_INTERRUPTS 0 // incomplete support
51 
52 uint16_t adc_reading[NUM_ADC_CHAN];
53 
54 void ADC_flush(void) {
55  while(ADC->FIFO_STATUSbits.EMPTY == 0) ADC->FIFO_READ;
56 }
57 
58 uint16_t ADC_READ(void) {
59  while(ADC->FIFO_STATUSbits.EMPTY); // loop while empty
60  return ADC->FIFO_READ; // upper 4 bits are channel number
61 }
62 
63 
64 void adc_service(void) {
65  uint16_t value;
66  uint8_t channel;
67  while (ADC->FIFO_STATUSbits.EMPTY == 0) {
68  value = ADC->FIFO_READ;
69  channel = value >> 12;
70  if (channel < NUM_ADC_CHAN) adc_reading[channel] = value & 0xFFF;
71  }
72 }
73 
74 #define _adc_setup_chan(x) do { \
75  if ( channel == x ) { \
76  ADC->SEQ_1bits.CH##x = 1; \
77  GPIO->FUNC_SEL.ADC##x = 1; \
78  GPIO->PAD_DIR.ADC##x = 0; \
79  GPIO->PAD_KEEP.ADC##x = 0; \
80  GPIO->PAD_PU_EN.ADC##x = 0; \
81 }} while (0)
82 
83 void adc_setup_chan(uint8_t channel) {
84  _adc_setup_chan(0);
85  _adc_setup_chan(1);
86  _adc_setup_chan(2);
87  _adc_setup_chan(3);
88  _adc_setup_chan(4);
89  _adc_setup_chan(5);
90  _adc_setup_chan(6);
91  _adc_setup_chan(7);
92 }
93 
94 void adc_init(void) {
95  ADC->CLOCK_DIVIDER = 80; // 300 KHz
96  ADC->PRESCALE = ADC_PRESCALE_VALUE - 1; // divide by 24 for 1MHz.
97 
98  ADC->CONTROL = 1;
99 
100  // The ON-TIME must always be 10µs or greater - typical On-Time value = 0x000A (10dec)
101  ADC->ON_TIME = 10;
102 
103  /*
104  NOTE
105  The ADC analog block requires 6 ADC_Clocks per conversion, and the
106  ADC_Clock must 300kHz or less. With 6 clocks/conversion and a 33.33µs
107  clock period:
108  * The ADC convert time must always be 20µs or greater
109  * If the ADC_Clock is a frequency lower than 300kHz, the convert time
110  must always be 6 ADC_Clock periods or greater
111  * For override mode, extend convert time to 40µs minimum or greater
112  For the convert time:
113  * This delay is a function of the Prescale Clock (typically 1 MHz)
114  * The register must be initialized for proper operation
115  * For a 20µs convert time with 1MHz, value = 0x0014 (20dec)
116  * If convert time is insufficient, inaccurate sample data will result
117  */
118  ADC->CONVERT_TIME = 1000000 / (115200 / 8) - 1;
119 
120  ADC->MODE = 0; // Automatic
121 
122 #if ADC_USE_INTERRUPTS
123  ADC->FIFO_CONTROL = 7;
124 #else
125  ADC->FIFO_CONTROL = 0;
126 #endif
127 
128 #if ADC_USE_TIMER
129  ADC->SR_1_HIGH = 0x0000;
130  ADC->SR_1_LOW = (REF_OSC / ADC_PRESCALE_VALUE) / (115200 / 8) + 1;
131 #endif
132 
133  ADC->SEQ_1 = 0;
134 #if ADC_USE_TIMER
135  ADC->SEQ_1bits.SEQ_MODE = 1; // sequence based on Timer 1
136 #else
137  ADC->SEQ_1bits.SEQ_MODE = 0; // sequence based on convert time.
138 #endif
139  ADC->SEQ_1bits.BATT = 1;
140 
141  ADC->CONTROL = 0xF001
142 //#if ADC_USE_TIMER
143  | (1 << 1) // Timer 1 enable
144 //#endif
145  ;
146  ADC->OVERRIDE = (1 << 8);
147 
148 }
void adc_init(void)
Initializes the ADC controller.
Definition: adc.c:50