Contiki 2.5
adc.h
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 #ifndef ADC_H
37 #define ADC_H
38 
39 #include <stdint.h>
40 #include "utils.h"
41 
42 /* ADC registers are all 16-bit wide with 16-bit access only */
43 #define ADC_BASE (0x8000D000)
44 
45 /* Structure-based register definitions */
46 
47 struct ADC_struct {
48  union {
49  uint16_t COMP[8];
50  struct {
51  uint16_t COMP_0;
52  uint16_t COMP_1;
53  uint16_t COMP_2;
54  uint16_t COMP_3;
55  uint16_t COMP_4;
56  uint16_t COMP_5;
57  uint16_t COMP_6;
58  uint16_t COMP_7;
59  };
60  };
61  uint16_t BAT_COMP_OVER;
62  uint16_t BAT_COMP_UNDER;
63  union {
64  uint16_t SEQ_1;
65  struct ADC_SEQ_1 {
66  uint16_t CH0:1;
67  uint16_t CH1:1;
68  uint16_t CH2:1;
69  uint16_t CH3:1;
70  uint16_t CH4:1;
71  uint16_t CH5:1;
72  uint16_t CH6:1;
73  uint16_t CH7:1;
74  uint16_t BATT:1;
75  uint16_t :6;
76  uint16_t SEQ_MODE:1;
77  } SEQ_1bits;
78  };
79  union {
80  uint16_t SEQ_2;
81  struct ADC_SEQ_2 {
82  uint16_t CH0:1;
83  uint16_t CH1:1;
84  uint16_t CH2:1;
85  uint16_t CH3:1;
86  uint16_t CH4:1;
87  uint16_t CH5:1;
88  uint16_t CH6:1;
89  uint16_t CH7:1;
90  uint16_t :7;
91  uint16_t SEQ_MODE:1;
92  } SEQ_2bits;
93  };
94  union {
95  uint16_t CONTROL;
96  struct ADC_CONTROL {
97  uint16_t ON:1;
98  uint16_t TIMER1_ON:1;
99  uint16_t TIMER2_ON:1;
100  uint16_t SOFT_RESET:1;
101  uint16_t AD1_FREFHL_EN:1;
102  uint16_t AD2_VREFHL_EN:1;
103  uint16_t :6;
104  uint16_t COMPARE_IRQ_MASK:1;
105  uint16_t SEQ1_IRQ_MASK:1;
106  uint16_t SEQ2_IRQ_MASK:1;
107  uint16_t FIFO_IRQ_MASK:1;
108  } CONTROLbits;
109  };
110  uint16_t TRIGGERS;
111  uint16_t PRESCALE;
112  uint16_t reserved1;
113  uint16_t FIFO_READ;
114  uint16_t FIFO_CONTROL;
115  union {
116  uint16_t FIFO_STATUS;
117  struct ADC_FIFO_STATUS {
118  uint16_t LEVEL:4;
119  uint16_t FULL:1;
120  uint16_t EMPTY:1;
121  uint16_t :10;
122  } FIFO_STATUSbits;
123  };
124  uint16_t reserved2[5];
125  uint16_t SR_1_HIGH;
126  uint16_t SR_1_LOW;
127  uint16_t SR_2_HIGH;
128  uint16_t SR_2_LOW;
129  uint16_t ON_TIME;
130  uint16_t CONVERT_TIME;
131  uint16_t CLOCK_DIVIDER;
132  uint16_t reserved3;
133  union {
134  uint16_t OVERRIDE;
135  struct ADC_OVERRIDE {
136  uint16_t MUX1:4;
137  uint16_t MUX2:4;
138  uint16_t AD1_ON:1;
139  uint16_t AD2_ON:1;
140  uint16_t :6;
141  } OVERRIDEbits;
142  };
143  uint16_t IRQ;
144  uint16_t MODE;
145  uint16_t RESULT_1;
146  uint16_t RESULT_2;
147 };
148 
149 static volatile struct ADC_struct * const ADC = (void *) (ADC_BASE);
150 
151 #define NUM_ADC_CHAN 9
152 
153 #define adc_enable() (ADC->CONTROLbits.ON = 1)
154 #define adc_disable() (ADC->CONTROLbits.ON = 0)
155 #define adc_select_channels(chans) (ADC->SEQ_1 = (ADC->SEQ_1 & 0xFE00) | chans)
156 
157 extern uint16_t adc_reading[NUM_ADC_CHAN];
158 void ADC_flush(void);
159 uint16_t ADC_READ(void);
160 void read_scanners(void);
161 void adc_init(void);
162 void adc_service(void);
163 
164 #endif