91 #include <libopencm3/stm32/rcc.h>
92 #include <libopencm3/stm32/gpio.h>
93 #include <libopencm3/stm32/adc.h>
94 #include <libopencm3/cm3/nvic.h>
95 #include <libopencm3/stm32/timer.h>
100 #include BOARD_CONFIG
103 #ifndef NVIC_ADC_IRQ_PRIO
104 #define NVIC_ADC_IRQ_PRIO 0
108 #define ADC_SAMPLE_TIME ADC_SMPR_SMP_41DOT5CYC
109 #elif defined(STM32F4)
110 #define ADC_SAMPLE_TIME ADC_SMPR_SMP_56CYC
115 #if defined(AD1_1_CHANNEL) || defined(AD1_2_CHANNEL) || defined(AD1_3_CHANNEL) || defined(AD1_4_CHANNEL)
121 #if defined(AD2_1_CHANNEL) || defined(AD2_2_CHANNEL) || defined(AD2_3_CHANNEL) || defined(AD2_4_CHANNEL)
129 #if defined(AD3_1_CHANNEL) || defined(AD3_2_CHANNEL) || defined(AD3_3_CHANNEL) || defined(AD3_4_CHANNEL)
135 #define RST_ADC1 RST_ADC
136 #define RST_ADC2 RST_ADC
137 #define RST_ADC3 RST_ADC
154 #if !USE_AD1 && !USE_AD2 && !USE_AD3 && !defined FBW
155 #warning ALL ADC CONVERTERS INACTIVE
158 #ifndef ADC_TIMER_PERIOD
159 #define ADC_TIMER_PERIOD 10000
167 #ifndef ADC_TIMER_FREQUENCY
168 #define ADC_TIMER_FREQUENCY 2000000
200 #if USE_AD1 || USE_AD2 || USE_AD3
201 #define ADC_NUM_CHANNELS 4
223 adc_watchdog_callback cb;
233 #if USE_AD1 || USE_AD2 || USE_AD3
342 for (x = 0; x < 4; x++) { adc2_buffers[x] = NULL; }
368 for (x = 0; x < 4; x++) { adc3_buffers[x] = NULL; }
373 adc_watchdog.cb = NULL;
374 adc_watchdog.timeStamp = 0;
395 s->av_nb_sample = av_nb_sample;
402 adc_watchdog.adc = adc;
403 adc_watchdog.cb = cb;
406 adc_set_watchdog_low_threshold(adc, low);
407 adc_set_watchdog_high_threshold(adc, high);
408 adc_enable_analog_watchdog_injected(adc);
409 adc_enable_analog_watchdog_on_selected_channel(adc, chan);
410 adc_enable_awd_interrupt(adc);
418 #if defined(USE_AD_TIM4)
420 #define RCC_TIM_ADC RCC_TIM4
421 #elif defined(USE_AD_TIM1)
423 #define RCC_TIM_ADC RCC_TIM1
426 #define RCC_TIM_ADC RCC_TIM2
432 #if USE_AD1 || USE_AD2 || USE_AD3
436 adc_set_clk_prescale(ADC_CCR_ADCPRE_BY2);
441 rcc_periph_clock_enable(RCC_ADC1);
442 rcc_periph_reset_pulse(RST_ADC1);
445 rcc_periph_clock_enable(RCC_ADC2);
446 rcc_periph_reset_pulse(RST_ADC2);
449 rcc_periph_clock_enable(RCC_ADC3);
450 rcc_periph_reset_pulse(RST_ADC3);
454 timer_set_mode(
TIM_ADC, TIM_CR1_CKD_CK_INT,
455 TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
462 timer_set_master_mode(
TIM_ADC, TIM_CR2_MMS_UPDATE);
473 nvic_enable_irq(NVIC_ADC1_2_IRQ);
474 #elif defined(STM32F4)
476 nvic_enable_irq(NVIC_ADC_IRQ);
490 adc_disable_analog_watchdog_regular(adc);
492 adc_disable_analog_watchdog_injected(adc);
494 adc_disable_discontinuous_mode_regular(adc);
496 adc_disable_discontinuous_mode_injected(adc);
498 adc_disable_automatic_injected_group_conversion(adc);
500 adc_enable_scan_mode(adc);
502 adc_enable_eoc_interrupt_injected(adc);
504 adc_disable_awd_interrupt(adc);
506 adc_disable_eoc_interrupt(adc);
510 adc_disable_temperature_sensor();
512 adc_disable_external_trigger_regular(adc);
514 adc_set_right_aligned(adc);
516 adc_disable_dma(adc);
518 adc_set_single_conversion_mode(adc);
524 adc_set_sample_time_on_all_channels(adc, ADC_SAMPLE_TIME);
526 adc_set_injected_sequence(adc, nb_channels, channel_map);
531 adc_enable_external_trigger_injected(adc, ADC_CR2_JEXTSEL_TIM4_TRGO);
532 #elif defined(STM32F4)
533 adc_enable_external_trigger_injected(adc, ADC_CR2_JEXTSEL_TIM4_TRGO, ADC_CR2_JEXTEN_BOTH_EDGES);
538 adc_enable_external_trigger_injected(adc, ADC_CR2_JEXTSEL_TIM1_TRGO);
539 #elif defined(STM32F4)
540 adc_enable_external_trigger_injected(adc, ADC_CR2_JEXTSEL_TIM1_TRGO, ADC_CR2_JEXTEN_BOTH_EDGES);
545 adc_enable_external_trigger_injected(adc, ADC_CR2_JEXTSEL_TIM2_TRGO);
546 #elif defined(STM32F4)
547 adc_enable_external_trigger_injected(adc, ADC_CR2_JEXTSEL_TIM2_TRGO, ADC_CR2_JEXTEN_BOTH_EDGES);
555 adc_reset_calibration(adc);
572 buf->
values[new_head] = value;
574 buf->
head = new_head;
582 void adc1_2_isr(
void)
583 #elif defined(STM32F4)
587 #if USE_AD1 || USE_AD2 || USE_AD3
600 const bool shouldAccumulateValue = timeStampDiff > 20;
601 if (shouldAccumulateValue) {
605 if (adc_watchdog.cb != NULL) {
606 if (adc_awd(adc_watchdog.adc)) {
607 ADC_SR(adc_watchdog.adc) &= ~ADC_SR_AWD;
615 if (adc_eoc_injected(ADC1)) {
616 ADC_SR(ADC1) &= ~ADC_SR_JEOC;
618 if (shouldAccumulateValue) {
623 value = adc_read_injected(ADC1, channel + 1);
634 if (adc_eoc_injected(ADC2)) {
635 ADC_SR(ADC2) &= ~ADC_SR_JEOC;
637 if (shouldAccumulateValue) {
640 buf = adc2_buffers[channel];
642 value = adc_read_injected(ADC2, channel + 1);
653 if (adc_eoc_injected(ADC3)) {
654 ADC_SR(ADC3) &= ~ADC_SR_JEOC;
656 if (shouldAccumulateValue) {
659 buf = adc3_buffers[channel];
661 value = adc_read_injected(ADC3, channel + 1);
arch independent ADC (Analog to Digital Converter) API
uint16_t values[MAX_AV_NB_SAMPLE]
Generic interface for all ADC hardware drivers, independent from microcontroller architecture.
static struct adc_buf * adc1_buffers[ADC_NUM_CHANNELS]
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.
void adc_init(void)
Adc init.
static const uint8_t adc_channel_map[ADC_NUM_CHANNELS]
void gpio_setup_pin_analog(ioportid_t port, uint16_t pin)
Setup a gpio for analog use.
uint32_t get_sys_time_msec(void)
Get the time in milliseconds since startup.
Some architecture independent helper functions for GPIOs.
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
uint32_t timer_get_frequency(uint32_t timer_peripheral)
Get Timer clock frequency (before prescaling) Only valid if using the internal clock for the timer.
#define NVIC_ADC_IRQ_PRIO
static uint8_t nb_adc3_channels
#define ADC_TIMER_FREQUENCY
Timer frequency for ADC Timer will trigger an update event after reaching the period reload value.
static void adc_init_irq(void)
Configure and enable ADC interrupt.
static void adc_init_rcc(void)
Configure and enable RCC for peripherals (ADC1, ADC2, Timer)
static uint8_t nb_adc1_channels
static void adc_push_sample(struct adc_buf *buf, uint16_t sample)
static uint8_t nb_adc2_channels
static void adc_init_single(uint32_t adc, uint8_t nb_channels, uint8_t *channel_map)
Architecture independent timing functions.
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.