Paparazzi UAS  v5.15_devel-61-g9c6bcf9
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) 2013 AggieAir, A Remote Sensing Unmanned Aerial System for Scientific Applications
3  * Utah State University, http://aggieair.usu.edu/
4  *
5  * Michal Podhradsky (michal.podhradsky@aggiemail.usu.edu)
6  * Calvin Coopmans (c.r.coopmans@ieee.org)
7  *
8  * Copyright (C) 2015 Gautier Hattenberger, Alexandre Bustico
9  *
10  * This file is part of paparazzi.
11  *
12  * paparazzi is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2, or (at your option)
15  * any later version.
16  *
17  * paparazzi is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with paparazzi; see the file COPYING. If not, write to
24  * the Free Software Foundation, 59 Temple Place - Suite 330,
25  * Boston, MA 02111-1307, USA.
26  */
53 #include "mcu_periph/adc.h"
54 #include "mcu_periph/gpio.h"
55 #include "hal.h"
56 #include "std.h"
57 #include "mcu_periph/ram_arch.h"
58 
59 
60 // Macros to automatically enable the correct ADC
61 // FIXME we can't use NB_ADC1_CHANNELS it is not a macro
62 //#if NB_ADC1_CHANNELS != 0
63 #ifndef USE_AD1
64 #define USE_AD1 1
65 #endif
66 
68 
69 // architecture dependent settings
70 #if defined(__STM32F10x_H) || defined(__STM32F105xC_H) || defined (__STM32F107xC_H)
71 // STM32F1xx
72 #define ADC_SAMPLE_RATE ADC_SAMPLE_41P5
73 #define ADC_CR2_CFG ADC_CR2_TSVREFE
74 #elif defined(__STM32F4xx_H) || defined(__STM32F7xx_H)
75 // STM32F4xx | STM32F7xx
76 #define ADC_SAMPLE_RATE ADC_SAMPLE_480
77 #define ADC_CR2_CFG ADC_CR2_SWSTART
78 #elif defined(__STM32F373xC_H)
79 #define ADC_SAMPLE_RATE ADC_SAMPLE_239P5
80 #define ADC_CR2_CFG ADC_CR2_SWSTART
81 #elif defined(__STM32F3xx_H)
82 #define ADC_SAMPLE_RATE ADC_SMPR_SMP_601P5
83 #endif
84 
85 
86 // Create channel map
88 #ifdef AD1_1_CHANNEL
90 #endif
91 #ifdef AD1_2_CHANNEL
93 #endif
94 #ifdef AD1_3_CHANNEL
96 #endif
97 #ifdef AD1_4_CHANNEL
99 #endif
100 #ifdef AD1_5_CHANNEL
102 #endif
103 #ifdef AD1_6_CHANNEL
104  AD1_6_CHANNEL,
105 #endif
106 #ifdef AD1_7_CHANNEL
108 #endif
109 #ifdef AD1_8_CHANNEL
110  AD1_8_CHANNEL,
111 #endif
112 #ifdef AD1_9_CHANNEL
114 #endif
115 #ifdef AD1_10_CHANNEL
116  AD1_10_CHANNEL,
117 #endif
118 #ifdef AD1_11_CHANNEL
119  AD1_11_CHANNEL,
120 #endif
121 #ifdef AD1_12_CHANNEL
122  AD1_12_CHANNEL,
123 #endif
124 #ifdef AD1_13_CHANNEL
125  AD1_13_CHANNEL,
126 #endif
127 #ifdef AD1_14_CHANNEL
128  AD1_14_CHANNEL,
129 #endif
130 #ifdef AD1_15_CHANNEL
131  AD1_15_CHANNEL,
132 #endif
133 #ifdef AD1_16_CHANNEL
134  AD1_16_CHANNEL,
135 #endif
136 };
137 
139 ADCDriver *adcp_err = NULL;
140 
141 // adc_samples buffer
142 // FIXME compute size to have to correct number of samples ?
143 #ifndef ADC_BUF_DEPTH
144 #define ADC_BUF_DEPTH (MAX_AV_NB_SAMPLE/2)
145 #endif
146 static IN_DMA_SECTION(adcsample_t adc_samples[ADC_NUM_CHANNELS * ADC_BUF_DEPTH]);
147 
148 #if USE_AD1
152 #endif
153 #if USE_AD2
154 #error ADC2_not implemented in ChibiOS
155 #endif
156 #if USE_AD3
157 #error ADC3_not implemented in ChibiOS
158 #endif
159 
160 #if USE_ADC_WATCHDOG
161 // watchdog structure with adc bank and callback
162 static struct {
163  ADCDriver *adc;
164  adc_channels_num_t channel;
165  adcsample_t vmin;
166  adc_watchdog_callback cb;
167 } adc_watchdog;
168 #endif
169 
170 // From libopencm3
171 static void adc_regular_sequence(uint32_t *sqr1, uint32_t *sqr2, uint32_t *sqr3, uint8_t length, const uint8_t channel[])
172 {
173  uint32_t first6 = 0;
174  uint32_t second6 = 0;
175  uint32_t third6 = ADC_SQR1_NUM_CH(length);
176  uint8_t i = 0;
177 
178  for (i = 1; i <= length; i++) {
179  if (i <= 6) {
180  first6 |= (channel[i - 1] << ((i - 1) * 5));
181  }
182  if ((i > 6) & (i <= 12)) {
183  second6 |= (channel[i - 1] << ((i - 6 - 1) * 5));
184  }
185  if ((i > 12) & (i <= 18)) {
186  third6 |= (channel[i - 1] << ((i - 12 - 1) * 5));
187  }
188  }
189  *sqr3 = first6;
190  *sqr2 = second6;
191  *sqr1 = third6;
192 }
193 
194 // From libopencm3
195 static void adc_sample_time_on_all_channels(uint32_t *smpr1, uint32_t *smpr2, uint8_t time)
196 {
197  uint8_t i;
198  uint32_t reg32 = 0;
199 
200  for (i = 0; i <= 9; i++) {
201  reg32 |= (time << (i * 3));
202  }
203  *smpr2 = reg32;
204 
205  reg32 = 0;
206  for (i = 10; i <= 17; i++) {
207  reg32 |= (time << ((i - 10) * 3));
208  }
209  *smpr1 = reg32;
210 }
211 
224 void adc1callback(ADCDriver *adcp, adcsample_t *buffer, size_t n)
225 {
226  if (adcp->state != ADC_STOP) {
227 #if USE_AD1
228  for (int channel = 0; channel < ADC_NUM_CHANNELS; channel++) {
229  if (adc1_buffers[channel] != NULL) {
230  adc1_sum_tmp[channel] = 0;
231  if (n > 0) {
233  } else {
235  }
236  for (unsigned int sample = 0; sample < n; sample++) {
237  adc1_sum_tmp[channel] += buffer[channel + sample * ADC_NUM_CHANNELS];
238  }
239  }
240  }
241  chSysLockFromISR();
242  for (int channel = 0; channel < ADC_NUM_CHANNELS; channel++) {
243  if (adc1_buffers[channel] != NULL) {
244  adc1_buffers[channel]->sum = adc1_sum_tmp[channel];
245  adc1_buffers[channel]->av_nb_sample = adc1_samples_tmp[channel];
246  }
247  }
248 #if USE_ADC_WATCHDOG
249  if ((adc_watchdog.adc == adcp) &&
250  (adc_watchdog.channel < ADC_NUM_CHANNELS) &&
251  (adc_watchdog.cb != NULL)) {
252  if (adc1_buffers[adc_watchdog.channel]->sum <
253  (adc1_buffers[adc_watchdog.channel]->av_nb_sample * adc_watchdog.vmin)) {
254  adc_watchdog.cb();
255  }
256  }
257 #endif // USE_ADC_WATCHDOG
258 
259  chSysUnlockFromISR();
260 #endif
261  }
262 }
263 
264 
270 static void adcerrorcallback(ADCDriver *adcp, adcerror_t err)
271 {
272  chSysLockFromISR();
273  adcp_err = adcp;
274  adc_error_flag = err;
275  chSysUnlockFromISR();
276 }
277 
281 void adc_buf_channel(uint8_t adc_channel, struct adc_buf *s, uint8_t av_nb_sample)
282 {
283  // check for out-of-bounds access
284  if (adc_channel >= ADC_NUM_CHANNELS) { return; }
285  adc1_buffers[adc_channel] = s;
286  if (av_nb_sample <= MAX_AV_NB_SAMPLE) {
287  s->av_nb_sample = av_nb_sample;
288  } else {
290  }
291 }
292 
297 static ADCConversionGroup adcgrpcfg;
298 
304 void adc_init(void)
305 {
306  /* Init GPIO ports for ADC operation
307  */
308 #if USE_ADC_1
309  PRINT_CONFIG_MSG("Info: Using ADC_1");
311 #endif
312 #if USE_ADC_2
313  PRINT_CONFIG_MSG("Info: Using ADC_2");
315 #endif
316 #if USE_ADC_3
317  PRINT_CONFIG_MSG("Info: Using ADC_3");
319 #endif
320 #if USE_ADC_4
321  PRINT_CONFIG_MSG("Info: Using ADC_4");
323 #endif
324 #if USE_ADC_5
325  PRINT_CONFIG_MSG("Info: Using ADC_5");
327 #endif
328 #if USE_ADC_6
329  PRINT_CONFIG_MSG("Info: Using ADC_6");
331 #endif
332 #if USE_ADC_7
333  PRINT_CONFIG_MSG("Info: Using ADC_7");
335 #endif
336 #if USE_ADC_8
337  PRINT_CONFIG_MSG("Info: Using ADC_8");
338  gpio_setup_pin_analog(ADC_8_GPIO_PORT, ADC_8_GPIO_PIN);
339 #endif
340 #if USE_ADC_9
341  PRINT_CONFIG_MSG("Info: Using ADC_9");
343 #endif
344 
345  // Configuration register
346  uint32_t sqr1, sqr2, sqr3;
348 
349  uint32_t smpr1, smpr2;
350  adc_sample_time_on_all_channels(&smpr1, &smpr2, ADC_SAMPLE_RATE);
351 
352 #if USE_ADC_WATCHDOG
353  adc_watchdog.adc = NULL;
354  adc_watchdog.cb = NULL;
355  adc_watchdog.channel = 0;
356  adc_watchdog.vmin = (1 << 12) - 1; // max adc
357 #endif
358 
359  adcgrpcfg.circular = TRUE;
360  adcgrpcfg.num_channels = ADC_NUM_CHANNELS;
361  adcgrpcfg.end_cb = adc1callback;
362  adcgrpcfg.error_cb = adcerrorcallback;
363 #if defined(__STM32F373xC_H)
364  adcgrpcfg.u.adc.smpr[0] = smpr1;
365  adcgrpcfg.u.adc.smpr[1] = smpr2;
366  adcgrpcfg.u.adc.sqr[0] = sqr1;
367  adcgrpcfg.u.adc.sqr[1] = sqr2;
368  adcgrpcfg.u.adc.sqr[2] = sqr3;
369  adcgrpcfg.u.adc.cr1 = 0;
370  adcgrpcfg.u.adc.cr2 = ADC_CR2_CFG;
371 #elif defined(__STM32F3xx_H)
372  //TODO: check if something needs to be done with the other regs (can be found in ~/paparazzi/sw/ext/chibios/os/hal/ports/STM32/LLD/ADCv3)
373 #warning ADCs not tested with stm32f30
374  // cfgr
375  // tr1
376 
377  adcgrpcfg.smpr[0] = smpr1; // is this even correct?
378  adcgrpcfg.smpr[1] = smpr2;
379  adcgrpcfg.sqr[0] = sqr1;
380  adcgrpcfg.sqr[1] = sqr2;
381  adcgrpcfg.sqr[2] = sqr3;
382 #else
383  adcgrpcfg.cr2 = ADC_CR2_CFG;
384  adcgrpcfg.cr1 = 0;
385  adcgrpcfg.smpr1 = smpr1;
386  adcgrpcfg.smpr2 = smpr2;
387  adcgrpcfg.sqr1 = sqr1;
388  adcgrpcfg.sqr2 = sqr2;
389  adcgrpcfg.sqr3 = sqr3;
390 #endif
391 
392  // Start ADC in continious conversion mode
393  adcStart(&ADCD1, NULL);
394  adcStartConversion(&ADCD1, &adcgrpcfg, adc_samples, ADC_BUF_DEPTH);
395 }
396 
397 #if USE_ADC_WATCHDOG
398 void register_adc_watchdog(ADCDriver *adc, adc_channels_num_t channel, adcsample_t vmin,
399  adc_watchdog_callback cb)
400 {
401  for (int i = 0; i < NB_ADC1_CHANNELS; i++) { // FIXME when more than ADC1 will be in use
402  if (adc_channel_map[i] == channel) {
403  adc_watchdog.adc = adc;
404  adc_watchdog.channel = i;
405  adc_watchdog.vmin = vmin;
406  adc_watchdog.cb = cb;
407  break;
408  }
409  }
410 }
411 #endif
uint8_t av_nb_sample
Definition: adc.h:57
Specific RAM section for DMA usage on F7.
static uint32_t s
#define ADC_7_GPIO_PIN
Definition: chimera.h:191
#define ADC_4_GPIO_PIN
Definition: board.h:192
#define AD1_1_CHANNEL
Definition: cjmcu.h:62
ADCDriver * adcp_err
Definition: adc_arch.c:139
Some architecture independent helper functions for GPIOs.
#define AD1_7_CHANNEL
Definition: chimera.h:188
static IN_DMA_SECTION(adcsample_t adc_samples[ADC_NUM_CHANNELS *ADC_BUF_DEPTH])
static uint32_t adc1_sum_tmp[ADC_NUM_CHANNELS]
Definition: adc_arch.c:150
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:281
uint32_t sum
Definition: adc.h:54
#define ADC_6_GPIO_PIN
#define ADC_7_GPIO_PORT
Definition: chimera.h:190
#define ADC_9_GPIO_PIN
Definition: tawaki.h:135
static ADCConversionGroup adcgrpcfg
Configuration structure must be global.
Definition: adc_arch.c:297
arch independent ADC (Analog to Digital Converter) API
#define ADC_1_GPIO_PORT
Definition: cjmcu.h:64
#define ADC_3_GPIO_PIN
Definition: lisa_l_1.0.h:151
#define AD1_9_CHANNEL
Definition: tawaki.h:132
static struct adc_buf * adc1_buffers[ADC_NUM_CHANNELS]
Definition: adc_arch.c:149
Generic interface for all ADC hardware drivers, independent from microcontroller architecture.
Definition: adc.h:53
#define TRUE
Definition: std.h:4
static const uint8_t adc_channel_map[ADC_NUM_CHANNELS]
#endif
Definition: adc_arch.c:87
#define ADC_5_GPIO_PIN
Definition: board.h:284
unsigned long uint32_t
Definition: types.h:18
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
void gpio_setup_pin_analog(ioportid_t port, uint16_t pin)
Setup a gpio for analog use.
Definition: gpio_arch.c:86
#define ADC_1_GPIO_PIN
Definition: cjmcu.h:65
#define AD1_5_CHANNEL
Definition: board.h:281
#define ADC_5_GPIO_PORT
Definition: board.h:283
uint8_t adc_error_flag
Definition: adc_arch.c:138
#define AD1_4_CHANNEL
Definition: board.h:189
static void adc_sample_time_on_all_channels(uint32_t *smpr1, uint32_t *smpr2, uint8_t time)
Definition: adc_arch.c:195
float vmin
#define ADC_9_GPIO_PORT
Definition: tawaki.h:134
#define MAX_AV_NB_SAMPLE
Definition: adc.h:40
#define ADC_4_GPIO_PORT
Definition: board.h:191
#define ADC_3_GPIO_PORT
Definition: lisa_l_1.0.h:150
unsigned char uint8_t
Definition: types.h:14
static void adcerrorcallback(ADCDriver *adcp, adcerror_t err)
Adc error callback.
Definition: adc_arch.c:270
void adc1callback(ADCDriver *adcp, adcsample_t *buffer, size_t n)
Adc1 callback.
Definition: adc_arch.c:224
static uint8_t adc1_samples_tmp[ADC_NUM_CHANNELS]
Definition: adc_arch.c:151
void adc_init(void)
Adc init.
Definition: adc_arch.c:304
#define ADC_6_GPIO_PORT
static uint8_t channel
Definition: ADS8344.c:80
#define AD1_3_CHANNEL
Definition: lisa_l_1.0.h:148
static void adc_regular_sequence(uint32_t *sqr1, uint32_t *sqr2, uint32_t *sqr3, uint8_t length, const uint8_t channel[])
Definition: adc_arch.c:171
#define ADC_NUM_CHANNELS
Definition: adc_arch.h:93
#define ADC_BUF_DEPTH
Definition: adc_arch.c:144