Paparazzi UAS  v5.10_stable-5-g83a0da5-dirty
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces 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 {
61  buffers[adc_channel] = s;
63 }
64 
65 /*
66 
67 pin11 AD0.0 P0.27 PINSEL1 1 << 22
68 pin13 AD0.1 P0.28 PINSEL1 1 << 24
69 pin14 AD0.2 P0.29 PINSEL1 1 << 26
70 pin15 AD0.3 P0.30 PINSEL1 1 << 28
71 pin9 AD0.4 P0.25 PINSEL1 1 << 18
72 pin10 AD0.5 P0.26 PINSEL1 1 << 20
73 pin27 AD0.6 P0.4 PINSEL0 3 << 8
74 pin29 AD0.7 P0.5 PINSEL0 3 << 10
75 
76 pin30 AD1.0 P0.6 PINSEL0 3 << 12
77 pin33 AD1.1 P0.8 PINSEL0 3 << 16
78 pin35 AD1.2 P0.10 PINSEL0 3 << 20
79 pin38 AD1.3 P0.12 PINSEL0 3 << 24
80 pin39 AD1.4 P0.13 PINSEL0 3 << 26
81 pin45 AD1.5 P0.15 PINSEL0 3 << 30
82 pin1 AD1.6 P0.21 PINSEL1 2 << 10
83 pin2 AD1.7 P0.22 PINSEL1 1 << 12
84 
85 */
86 
87 static const uint32_t ADC_PINSEL0_ONES = 0
88 #if defined USE_AD0_6
89  | 3 << 8
90 #endif
91 #if defined USE_AD0_7
92  | 3 << 10
93 #endif
94 #if defined USE_AD1_0
95  | 3 << 12
96 #endif
97 #if defined USE_AD1_1
98  | 3 << 16
99 #endif
100 #if defined USE_AD1_2
101  | 3 << 20
102 #endif
103 #if defined USE_AD1_3
104  | 3 << 24
105 #endif
106 #if defined USE_AD1_4
107  | 3 << 26
108 #endif
109 #if defined USE_AD1_5
110  | 3 << 30
111 #endif
112  ;
113 
114 static const uint32_t ADC_PINSEL1_ONES = 0
115 #if defined USE_AD0_0
116  | 1 << 22
117 #endif
118 #if defined USE_AD0_1
119  | 1 << 24
120 #endif
121 #if defined USE_AD0_2
122  | 1 << 26
123 #endif
124 #if defined USE_AD0_3
125  | 1 << 28
126 #endif
127 #if defined USE_AD0_4
128  | 1 << 18
129 #endif
130 #if defined USE_AD0_5
131  | 1 << 20
132 #endif
133 #if defined USE_AD1_6
134  | 2 << 10
135 #endif
136 #if defined USE_AD1_7
137  | 1 << 12
138 #endif
139  ;
140 
142 #if defined USE_AD0_0
143  | 1 << 0
144 #endif
145 #if defined USE_AD0_1
146  | 1 << 1
147 #endif
148 #if defined USE_AD0_2
149  | 1 << 2
150 #endif
151 #if defined USE_AD0_3
152  | 1 << 3
153 #endif
154 #if defined USE_AD0_4
155  | 1 << 4
156 #endif
157 #if defined USE_AD0_5
158  | 1 << 5
159 #endif
160 #if defined USE_AD0_6
161  | 1 << 6
162 #endif
163 #if defined USE_AD0_7
164  | 1 << 7
165 #endif
166  ;
167 
169 #if defined USE_AD1_0
170  | 1 << 0
171 #endif
172 #if defined USE_AD1_1
173  | 1 << 1
174 #endif
175 #if defined USE_AD1_2
176  | 1 << 2
177 #endif
178 #if defined USE_AD1_3
179  | 1 << 3
180 #endif
181 #if defined USE_AD1_4
182  | 1 << 4
183 #endif
184 #if defined USE_AD1_5
185  | 1 << 5
186 #endif
187 #if defined USE_AD1_6
188  | 1 << 6
189 #endif
190 #if defined USE_AD1_7
191  | 1 << 7
192 #endif
193  ;
194 
195 void adc_init(void)
196 {
197 
198  /* connect pins for selected ADCs */
201 
202 #ifdef USE_AD0
203  /* FIXME: this needs to be investigated, we should run just below 4.5MHz,
204  but we are a lot slower (e.g. 58.6kHz with PCLK = 15MHz), see
205  lpc_vor_convertions.c for right timing code */
206  /* setup hw scan - PCLK/256 ( 58.6kHz/117.2kHz/234.4kHz ) - BURST ON */
207  AD0CR = ADC_AD0CR_SEL_HW_SCAN | 0xFF << 8 | 1 << 16 | 0x01 << 21 ;
208  /* AD0 selected as IRQ */
210  /* AD0 interrupt enabled */
212  /* AD0 interrupt as VIC2 */
213  _VIC_CNTL(AD0_VIC_SLOT) = VIC_ENABLE | VIC_AD0;
214  _VIC_ADDR(AD0_VIC_SLOT) = (uint32_t)adcISR0;
215 #endif
216 
217 #ifdef USE_AD1
218  /* FIXME: this needs to be investigated, we should run just below 4.5MHz,
219  but we are a lot slower (e.g. 58.6kHz with PCLK = 15MHz), see
220  lpc_vor_convertions.c for right timing code */
221  /* setup hw scan - PCLK/256 ( 58.6kHz/117.2kHz/234.4kHz ) - BURST ON */
222  AD1CR = ADC_AD1CR_SEL_HW_SCAN | 0xFF << 8 | 1 << 16 | 0x01 << 21 ;
223  /* AD1 selected as IRQ */
225  /* AD1 interrupt enabled */
227  /* AD1 interrupt as VIC2 */
228  _VIC_CNTL(AD1_VIC_SLOT) = VIC_ENABLE | VIC_AD1;
229  _VIC_ADDR(AD1_VIC_SLOT) = (uint32_t)adcISR1;
230 #endif
231 
232 }
233 
234 
235 void adcISR0(void)
236 {
237  ISR_ENTRY();
238  uint32_t tmp = AD0GDR;
239  uint8_t channel = (uint8_t)(tmp >> 24) & 0x07;
240  uint16_t value = (uint16_t)(tmp >> 6) & 0x03FF;
241  adc0_val[channel] = value;
242 
243  struct adc_buf *buf = buffers[channel];
244  if (buf) {
245  uint8_t new_head = buf->head + 1;
246  if (new_head >= buf->av_nb_sample) { new_head = 0; }
247  buf->sum -= buf->values[new_head];
248  buf->values[new_head] = value;
249  buf->sum += value;
250  buf->head = new_head;
251  }
252 
253  VICVectAddr = 0x00000000; // clear this interrupt from the VIC
254  ISR_EXIT(); // recover registers and return
255 }
256 
257 void adcISR1(void)
258 {
259  ISR_ENTRY();
260  uint32_t tmp = AD1GDR;
261  uint8_t channel = (uint8_t)(tmp >> 24) & 0x07;
262  uint16_t value = (uint16_t)(tmp >> 6) & 0x03FF;
263  adc1_val[channel] = value;
264  struct adc_buf *buf = buffers[channel + NB_ADC];
265  if (buf) {
266  uint8_t new_head = buf->head + 1;
267  if (new_head >= buf->av_nb_sample) { new_head = 0; }
268  buf->sum -= buf->values[new_head];
269  buf->values[new_head] = value;
270  buf->sum += value;
271  buf->head = new_head;
272  }
273 
274  VICVectAddr = 0x00000000; // clear this interrupt from the VIC
275  ISR_EXIT(); // recover registers and return
276 }
uint8_t av_nb_sample
Definition: adc.h:57
#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
#define _VIC_CNTL(idx)
Definition: armVIC.h:19
static const uint32_t ADC_PINSEL1_ONES
Definition: adc_arch.c:114
void adc_buf_channel(uint8_t adc_channel, struct adc_buf *s, uint8_t av_nb_sample)
Link between ChibiOS ADC drivers and Paparazzi adc_buffers.
Definition: adc_arch.c:265
uint32_t sum
Definition: adc.h:54
#define NB_ADC
8 ADCs for bank 0, others for bank 2
Definition: adc_arch.h:41
uint8_t head
Definition: adc.h:56
void adcISR1(void)
Definition: adc_arch.c:257
arch independent ADC (Analog to Digital Converter) API
#define _VIC_ADDR(idx)
Definition: armVIC.h:20
Generic interface for all ADC hardware drivers, independent from microcontroller architecture.
Definition: adc.h:53
volatile uint16_t adc1_val[NB_ADC]
Definition: adc_arch.c:54
#define AD0CR
Definition: LPC21xx.h:356
#define AD1CR
Definition: LPC21xx.h:373
#define VICVectAddr
Definition: LPC21xx.h:436
unsigned long uint32_t
Definition: types.h:18
void adcISR0(void)
Definition: adc_arch.c:235
static const uint32_t ADC_PINSEL0_ONES
Definition: adc_arch.c:87
#define VIC_BIT(chan)
Definition: lpcVIC.h:105
uint16_t values[MAX_AV_NB_SAMPLE]
Definition: adc.h:55
#define PINSEL0
Definition: LPC21xx.h:347
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:141
#define VICIntEnable
Definition: LPC21xx.h:431
#define PINSEL1
Definition: LPC21xx.h:348
#define VIC_AD1
Definition: lpcVIC.h:91
void adc_init(void)
Adc init.
Definition: adc_arch.c:286
static const uint32_t ADC_AD1CR_SEL_HW_SCAN
Definition: adc_arch.c:168
static uint8_t channel
Definition: ADS8344.c:80
#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