Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
adc_generic.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010 Martin Muller
3  * Copyright (C) 2016 Gautier Hattenberger
4  *
5  * This file is part of paparazzi.
6  *
7  * paparazzi is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * paparazzi is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with paparazzi; see the file COPYING. If not, see
19  * <http://www.gnu.org/licenses/>.
20  */
21 
32 #include "adc_generic.h"
33 #include "mcu_periph/adc.h"
34 #include "mcu_periph/uart.h"
35 #include "pprzlink/messages.h"
37 
40 
41 #ifndef ADC_CHANNEL_GENERIC1
42 #ifndef ADC_CHANNEL_GENERIC2
43 #error "at least one ADC_CHANNEL_GENERIC1/2 needs to be defined to use the generic_adc module"
44 #endif
45 #endif
46 
47 #ifndef ADC_CHANNEL_GENERIC_NB_SAMPLES
48 #define ADC_CHANNEL_GENERIC_NB_SAMPLES DEFAULT_AV_NB_SAMPLE
49 #endif
50 
51 #if PERIODIC_TELEMETRY
53 #endif
54 
55 #ifndef ADC_GENERIC_PERIODIC_SEND
56 #define ADC_GENERIC_PERIODIC_SEND TRUE
57 #endif
58 
59 #ifdef ADC_CHANNEL_GENERIC1
60 static struct adc_buf buf_generic1;
61 #endif
62 
63 #ifdef ADC_CHANNEL_GENERIC2
64 static struct adc_buf buf_generic2;
65 #endif
66 
67 static void adc_msg_send(struct transport_tx *trans, struct link_device *dev) {
68 #ifdef ADC_CHANNEL_GENERIC1
69  adc_generic_val1 = buf_generic1.sum / buf_generic1.av_nb_sample;
70 #endif
71 #ifdef ADC_CHANNEL_GENERIC2
72  adc_generic_val2 = buf_generic2.sum / buf_generic2.av_nb_sample;
73 #endif
74  pprz_msg_send_ADC_GENERIC(trans, dev, AC_ID, &adc_generic_val1, &adc_generic_val2);
75 }
76 
77 void adc_generic_init(void)
78 {
79  adc_generic_val1 = 0;
80  adc_generic_val2 = 0;
81 
82 #ifdef ADC_CHANNEL_GENERIC1
83  adc_buf_channel(ADC_CHANNEL_GENERIC1, &buf_generic1, ADC_CHANNEL_GENERIC_NB_SAMPLES);
84 #endif
85 #ifdef ADC_CHANNEL_GENERIC2
86  adc_buf_channel(ADC_CHANNEL_GENERIC2, &buf_generic2, ADC_CHANNEL_GENERIC_NB_SAMPLES);
87 #endif
88 #if PERIODIC_TELEMETRY
89  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_ADC_GENERIC, adc_msg_send);
90 #endif
91 
92 }
93 
95 {
96 #if ADC_GENERIC_PERIODIC_SEND
97  adc_msg_send(&(DefaultChannel).trans_tx, &(DefaultDevice).device);
98 #endif
99 }
100 
uint16_t
unsigned short uint16_t
Definition: types.h:16
adc_buf_channel
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:289
adc.h
arch independent ADC (Analog to Digital Converter) API
adc_generic_val2
uint16_t adc_generic_val2
Definition: adc_generic.c:39
telemetry.h
uart.h
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
dev
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:74
adc_generic_init
void adc_generic_init(void)
Definition: adc_generic.c:77
register_periodic_telemetry
int8_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint8_t _id, telemetry_cb _cb)
Register a telemetry callback function.
Definition: telemetry.c:46
adc_generic_periodic
void adc_generic_periodic(void)
Definition: adc_generic.c:94
ADC_CHANNEL_GENERIC_NB_SAMPLES
#define ADC_CHANNEL_GENERIC_NB_SAMPLES
Definition: adc_generic.c:48
adc_generic_val1
uint16_t adc_generic_val1
Definition: adc_generic.c:38
DefaultPeriodic
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
adc_generic.h
adc_msg_send
static void adc_msg_send(struct transport_tx *trans, struct link_device *dev)
Definition: adc_generic.c:67
adc_buf
Generic interface for all ADC hardware drivers, independent from microcontroller architecture.
Definition: adc.h:53