Paparazzi UAS  v5.0.5_stable-7-g4b8bbb7
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
adc_arch.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008 Pascal Brisset, Antoine Drouin
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  */
22 
30 #include "mcu_periph/adc.h"
31 
32 #include "LPC21xx.h"
33 #include "armVIC.h"
34 #include BOARD_CONFIG
35 
36 
37 #ifdef USE_AD0
38 #ifndef AD0_VIC_SLOT
39 #define AD0_VIC_SLOT 2
40 #endif
41 #endif
42 
43 #ifdef USE_AD1
44 #ifndef AD1_VIC_SLOT
45 #define AD1_VIC_SLOT 4
46 #endif
47 #endif
48 
49 
51 static struct adc_buf* buffers[NB_ADC*2];
52 
53 volatile uint16_t adc0_val[NB_ADC] = {1, 2, 3, 4, 5, 6, 7, 8};
54 volatile uint16_t adc1_val[NB_ADC] = {9, 10, 11, 12, 13, 14, 15, 16};
55 
56 void adcISR0 ( void ) __attribute__((naked));
57 void adcISR1 ( void ) __attribute__((naked));
58 
59 void adc_buf_channel(uint8_t adc_channel, struct adc_buf* s, uint8_t av_nb_sample) {
60  buffers[adc_channel] = s;
62 }
63 
64 /*
65 
66 pin11 AD0.0 P0.27 PINSEL1 1 << 22
67 pin13 AD0.1 P0.28 PINSEL1 1 << 24
68 pin14 AD0.2 P0.29 PINSEL1 1 << 26
69 pin15 AD0.3 P0.30 PINSEL1 1 << 28
70 pin9 AD0.4 P0.25 PINSEL1 1 << 18
71 pin10 AD0.5 P0.26 PINSEL1 1 << 20
72 pin27 AD0.6 P0.4 PINSEL0 3 << 8
73 pin29 AD0.7 P0.5 PINSEL0 3 << 10
74 
75 pin30 AD1.0 P0.6 PINSEL0 3 << 12
76 pin33 AD1.1 P0.8 PINSEL0 3 << 16
77 pin35 AD1.2 P0.10 PINSEL0 3 << 20
78 pin38 AD1.3 P0.12 PINSEL0 3 << 24
79 pin39 AD1.4 P0.13 PINSEL0 3 << 26
80 pin45 AD1.5 P0.15 PINSEL0 3 << 30
81 pin1 AD1.6 P0.21 PINSEL1 2 << 10
82 pin2 AD1.7 P0.22 PINSEL1 1 << 12
83 
84 */
85 
86 static const uint32_t ADC_PINSEL0_ONES = 0
87 #if defined USE_AD0_6
88  | 3 << 8
89 #endif
90 #if defined USE_AD0_7
91  | 3 << 10
92 #endif
93 #if defined USE_AD1_0
94  | 3 << 12
95 #endif
96 #if defined USE_AD1_1
97  | 3 << 16
98 #endif
99 #if defined USE_AD1_2
100  | 3 << 20
101 #endif
102 #if defined USE_AD1_3
103  | 3 << 24
104 #endif
105 #if defined USE_AD1_4
106  | 3 << 26
107 #endif
108 #if defined USE_AD1_5
109  | 3 << 30
110 #endif
111 ;
112 
113 static const uint32_t ADC_PINSEL1_ONES = 0
114 #if defined USE_AD0_0
115  | 1 << 22
116 #endif
117 #if defined USE_AD0_1
118  | 1 << 24
119 #endif
120 #if defined USE_AD0_2
121  | 1 << 26
122 #endif
123 #if defined USE_AD0_3
124  | 1 << 28
125 #endif
126 #if defined USE_AD0_4
127  | 1 << 18
128 #endif
129 #if defined USE_AD0_5
130  | 1 << 20
131 #endif
132 #if defined USE_AD1_6
133  | 2 << 10
134 #endif
135 #if defined USE_AD1_7
136  | 1 << 12
137 #endif
138 ;
139 
141 #if defined USE_AD0_0
142  | 1 << 0
143 #endif
144 #if defined USE_AD0_1
145  | 1 << 1
146 #endif
147 #if defined USE_AD0_2
148  | 1 << 2
149 #endif
150 #if defined USE_AD0_3
151  | 1 << 3
152 #endif
153 #if defined USE_AD0_4
154  | 1 << 4
155 #endif
156 #if defined USE_AD0_5
157  | 1 << 5
158 #endif
159 #if defined USE_AD0_6
160  | 1 << 6
161 #endif
162 #if defined USE_AD0_7
163  | 1 << 7
164 #endif
165 ;
166 
168 #if defined USE_AD1_0
169  | 1 << 0
170 #endif
171 #if defined USE_AD1_1
172  | 1 << 1
173 #endif
174 #if defined USE_AD1_2
175  | 1 << 2
176 #endif
177 #if defined USE_AD1_3
178  | 1 << 3
179 #endif
180 #if defined USE_AD1_4
181  | 1 << 4
182 #endif
183 #if defined USE_AD1_5
184  | 1 << 5
185 #endif
186 #if defined USE_AD1_6
187  | 1 << 6
188 #endif
189 #if defined USE_AD1_7
190  | 1 << 7
191 #endif
192 ;
193 
194 void adc_init( void ) {
195 
196  /* connect pins for selected ADCs */
199 
200 #ifdef USE_AD0
201  /* FIXME: this needs to be investigated, we should run just below 4.5MHz,
202  but we are a lot slower (e.g. 58.6kHz with PCLK = 15MHz), see
203  lpc_vor_convertions.c for right timing code */
204  /* setup hw scan - PCLK/256 ( 58.6kHz/117.2kHz/234.4kHz ) - BURST ON */
205  AD0CR = ADC_AD0CR_SEL_HW_SCAN | 0xFF << 8 | 1 << 16 | 0x01 << 21 ;
206  /* AD0 selected as IRQ */
208  /* AD0 interrupt enabled */
210  /* AD0 interrupt as VIC2 */
211  _VIC_CNTL(AD0_VIC_SLOT) = VIC_ENABLE | VIC_AD0;
212  _VIC_ADDR(AD0_VIC_SLOT) = (uint32_t)adcISR0;
213 #endif
214 
215 #ifdef USE_AD1
216  /* FIXME: this needs to be investigated, we should run just below 4.5MHz,
217  but we are a lot slower (e.g. 58.6kHz with PCLK = 15MHz), see
218  lpc_vor_convertions.c for right timing code */
219  /* setup hw scan - PCLK/256 ( 58.6kHz/117.2kHz/234.4kHz ) - BURST ON */
220  AD1CR = ADC_AD1CR_SEL_HW_SCAN | 0xFF << 8 | 1 << 16 | 0x01 << 21 ;
221  /* AD1 selected as IRQ */
223  /* AD1 interrupt enabled */
225  /* AD1 interrupt as VIC2 */
226  _VIC_CNTL(AD1_VIC_SLOT) = VIC_ENABLE | VIC_AD1;
227  _VIC_ADDR(AD1_VIC_SLOT) = (uint32_t)adcISR1;
228 #endif
229 
230 }
231 
232 
233 void adcISR0 ( void ) {
234  ISR_ENTRY();
235  uint32_t tmp = AD0GDR;
236  uint8_t channel = (uint8_t)(tmp >> 24) & 0x07;
237  uint16_t value = (uint16_t)(tmp >> 6) & 0x03FF;
239 
240  struct adc_buf* buf = buffers[channel];
241  if (buf) {
242  uint8_t new_head = buf->head + 1;
243  if (new_head >= buf->av_nb_sample) new_head = 0;
244  buf->sum -= buf->values[new_head];
245  buf->values[new_head] = value;
246  buf->sum += value;
247  buf->head = new_head;
248  }
249 
250  VICVectAddr = 0x00000000; // clear this interrupt from the VIC
251  ISR_EXIT(); // recover registers and return
252 }
253 
254 void adcISR1 ( void ) {
255  ISR_ENTRY();
256  uint32_t tmp = AD1GDR;
257  uint8_t channel = (uint8_t)(tmp >> 24) & 0x07;
258  uint16_t value = (uint16_t)(tmp >> 6) & 0x03FF;
260  struct adc_buf* buf = buffers[channel+NB_ADC];
261  if (buf) {
262  uint8_t new_head = buf->head + 1;
263  if (new_head >= buf->av_nb_sample) new_head = 0;
264  buf->sum -= buf->values[new_head];
265  buf->values[new_head] = value;
266  buf->sum += value;
267  buf->head = new_head;
268  }
269 
270  VICVectAddr = 0x00000000; // clear this interrupt from the VIC
271  ISR_EXIT(); // recover registers and return
272 }
#define VICIntSelect
Definition: LPC21xx.h:430
unsigned short uint16_t
Definition: types.h:16
#define VIC_AD0
Definition: lpcVIC.h:88
volatile uint16_t adc0_val[NB_ADC]
Definition: adc_arch.c:53
uint16_t value
ADC1+2 interrupt hander.
Definition: adc_arch.c:577
void adc_buf_channel(uint8_t adc_channel, struct adc_buf *s, uint8_t av_nb_sample)
Registers a buffer to be used to store the specified converted channel Usage:
Definition: adc_arch.c:59
#define _VIC_CNTL(idx)
Definition: armVIC.h:19
static const uint32_t ADC_PINSEL1_ONES
Definition: adc_arch.c:113
uint16_t values[MAX_AV_NB_SAMPLE]
Definition: adc.h:62
uint8_t av_nb_sample
Definition: adc.h:64
void adcISR1(void)
Definition: adc_arch.c:254
arch independent ADC (Analog to Digital Converter) API
#define _VIC_ADDR(idx)
Definition: armVIC.h:20
uint32_t sum
Definition: adc.h:61
volatile uint16_t adc1_val[NB_ADC]
Definition: adc_arch.c:54
#define AD0CR
Definition: LPC21xx.h:356
#define AD1CR
Definition: LPC21xx.h:373
Generic interface for all ADC hardware drivers, independent from microcontroller architecture.
Definition: adc.h:60
#define VICVectAddr
Definition: LPC21xx.h:436
unsigned long uint32_t
Definition: types.h:18
void adcISR0(void)
Definition: adc_arch.c:233
static const uint32_t ADC_PINSEL0_ONES
Definition: adc_arch.c:86
#define VIC_BIT(chan)
Definition: lpcVIC.h:105
#define PINSEL0
Definition: LPC21xx.h:347
struct adc_buf * buf
Definition: adc_arch.c:578
static struct adc_buf * buffers[NB_ADC *2]
First NB_ADC for bank 0, others for bank 2.
Definition: adc_arch.c:51
unsigned char uint8_t
Definition: types.h:14
#define ISR_EXIT()
Definition: armVIC.h:61
static const uint32_t ADC_AD0CR_SEL_HW_SCAN
Definition: adc_arch.c:140
#define VICIntEnable
Definition: LPC21xx.h:431
#define PINSEL1
Definition: LPC21xx.h:348
void adc_init(void)
Starts conversions.
Definition: adc_arch.c:194
#define VIC_AD1
Definition: lpcVIC.h:91
#define NB_ADC
Definition: adc_arch.h:72
static const uint32_t ADC_AD1CR_SEL_HW_SCAN
Definition: adc_arch.c:167
static uint8_t channel
Definition: ADS8344.c:80
uint8_t head
Definition: adc.h:63
#define ISR_ENTRY()
Definition: armVIC.h:40
#define AD1GDR
Definition: LPC21xx.h:374
#define VIC_ENABLE
Definition: lpcVIC.h:102
#define AD0GDR
Definition: LPC21xx.h:357