Contiki 2.5
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 "crm.h"
38 #include "adc.h"
39 #include "gpio-util.h"
40 
41 //#define ADC_CHANS_ENABLED 0x3F // channels 0-5 enabled
42 //#define ADC_CHANS_ENABLED 0x7E // channels 1-6 enabled
43 //#define ADC_CHANS_ENABLED (1 << 6) // only channel 6 enabled
44 #define ADC_CHANS_ENABLED 0x1FF // all channels, including battery reference voltage
45 
46 //#define ADC_PRESCALE_VALUE 24 // divide by 24 for 1MHz.
47 
48 #define ADC_SAMPLE_FREQ 400 // Hz (minimum of ~366.21 Hz)
49 #define ADC_PRESCALE_VALUE (REF_OSC / ADC_SAMPLE_FREQ)
50 
51 #define ADC_USE_TIMER 0
52 #define ADC_USE_INTERRUPTS 0 // incomplete support
53 
54 uint16_t adc_reading[NUM_ADC_CHAN];
55 
56 void ADC_flush(void) {
57  while(ADC->FIFO_STATUSbits.EMPTY == 0) ADC->FIFO_READ;
58 }
59 
60 uint16_t ADC_READ(void) {
61  while(ADC->FIFO_STATUSbits.EMPTY); // loop while empty
62  return ADC->FIFO_READ; // upper 4 bits are channel number
63 }
64 
65 
66 void adc_service(void) {
67  uint16_t value;
68  uint8_t channel;
69  while (ADC->FIFO_STATUSbits.EMPTY == 0) {
70  value = ADC->FIFO_READ;
71  channel = value >> 12;
72  if (channel < NUM_ADC_CHAN) adc_reading[channel] = value & 0xFFF;
73  }
74 }
75 
76 void adc_init(void) {
77  uint8_t n;
78 
79  ADC->CLOCK_DIVIDER = 80; // 300 KHz
80  ADC->PRESCALE = ADC_PRESCALE_VALUE - 1; // divide by 24 for 1MHz.
81 
82  ADC->CONTROL = 1;
83 
84  // The ON-TIME must always be 10µs or greater - typical On-Time value = 0x000A (10dec)
85  ADC->ON_TIME = 10;
86 
87  /*
88  NOTE
89  The ADC analog block requires 6 ADC_Clocks per conversion, and the
90  ADC_Clock must 300kHz or less. With 6 clocks/conversion and a 33.33µs
91  clock period:
92  * The ADC convert time must always be 20µs or greater
93  * If the ADC_Clock is a frequency lower than 300kHz, the convert time
94  must always be 6 ADC_Clock periods or greater
95  * For override mode, extend convert time to 40µs minimum or greater
96  For the convert time:
97  * This delay is a function of the Prescale Clock (typically 1 MHz)
98  * The register must be initialized for proper operation
99  * For a 20µs convert time with 1MHz, value = 0x0014 (20dec)
100  * If convert time is insufficient, inaccurate sample data will result
101  */
102  ADC->CONVERT_TIME = 1000000 / (115200 / 8) - 1;
103 
104  ADC->MODE = 0; // Automatic
105 
106 #if ADC_USE_INTERRUPTS
107  ADC->FIFO_CONTROL = 7;
108 #else
109  ADC->FIFO_CONTROL = 0;
110 #endif
111 
112 #if ADC_USE_TIMER
113  ADC->SR_1_HIGH = 0x0000;
114  ADC->SR_1_LOW = (REF_OSC / ADC_PRESCALE_VALUE) / (115200 / 8) + 1;
115 #endif
116 
117  ADC->SEQ_1 = 0
118 #if ADC_USE_TIMER
119  | (1 << 15) // sequence based on Timer 1.
120 #else
121  | (0 << 15) // sequence based on convert time.
122 #endif
123  | ADC_CHANS_ENABLED;
124 
125  ADC->CONTROL = 0xF001
126 //#if ADC_USE_TIMER
127  | (1 << 1) // Timer 1 enable
128 //#endif
129  ;
130  ADC->OVERRIDE = (1 << 8);
131 
132  for (n=0; n<=8; n++) {
133  if ((ADC_CHANS_ENABLED >> n) & 1) {
134  gpio_select_function(30 + n, 1); // Function 1 = ADC
135  gpio_set_pad_dir(30 + n, PAD_DIR_INPUT);
136  }
137  }
138 }