Paparazzi UAS  v5.15_devel-111-g8fb4629
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
pwm_input_arch.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Gautier Hattenberger <gautier.hattenberger@enac.fr>
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 
28 #include "mcu_periph/pwm_input_arch.h"
29 #include "mcu_periph/pwm_input.h"
30 #include "mcu_periph/gpio.h"
31 #include <hal.h>
32 #include BOARD_CONFIG
33 
34 #define ONE_MHZ_CLK 1000000
35 
36 #ifdef USE_PWM_INPUT1
37 static void input1_period_cb(ICUDriver *icup) {
38  pwm_input_period_tics[PWM_INPUT1] = icuGetPeriodX(icup);
40 }
41 
42 static void input1_width_cb(ICUDriver *icup) {
43  pwm_input_duty_tics[PWM_INPUT1] = icuGetWidthX(icup);
45 }
46 
47 static ICUConfig pwm_input1_cfg = {
48 #if USE_PWM_INPUT1 == PWM_PULSE_TYPE_ACTIVE_LOW
49  ICU_INPUT_ACTIVE_LOW,
50 #elif USE_PWM_INPUT1 == PWM_PULSE_TYPE_ACTIVE_HIGH
51  ICU_INPUT_ACTIVE_HIGH,
52 #else
53 #error "Unknown PWM_INPUT1_PULSE_TYPE"
54 #endif
56  input1_width_cb,
57  input1_period_cb,
58  NULL,
60  0
61 };
62 #endif
63 
64 #ifdef USE_PWM_INPUT2
65 static void input2_period_cb(ICUDriver *icup) {
66  pwm_input_period_tics[PWM_INPUT2] = icuGetPeriodX(icup);
68 }
69 
70 static void input2_width_cb(ICUDriver *icup) {
71  pwm_input_duty_tics[PWM_INPUT2] = icuGetWidthX(icup);
73 }
74 
75 static ICUConfig pwm_input2_cfg = {
76 #if USE_PWM_INPUT2 == PWM_PULSE_TYPE_ACTIVE_LOW
77  ICU_INPUT_ACTIVE_LOW,
78 #elif USE_PWM_INPUT2 == PWM_PULSE_TYPE_ACTIVE_HIGH
79  ICU_INPUT_ACTIVE_HIGH,
80 #else
81 #error "Unknown PWM_INPUT2_PULSE_TYPE"
82 #endif
84  input2_width_cb,
85  input2_period_cb,
86  NULL,
88  0
89 };
90 #endif
91 
92 void pwm_input_init(void)
93 {
94  int i;
95  // initialize the arrays to 0
96  for (i = 0; i < PWM_INPUT_NB; i++) {
97  pwm_input_duty_tics[i] = 0;
98  pwm_input_duty_valid[i] = 0;
99  pwm_input_period_tics[i] = 0;
100  pwm_input_period_valid[i] = 0;
101  }
102 
103 #ifdef USE_PWM_INPUT1
104  icuStart(&PWM_INPUT1_ICU, &pwm_input1_cfg);
106  icuStartCapture(&PWM_INPUT1_ICU);
107  icuEnableNotifications(&PWM_INPUT1_ICU);
108 #endif
109 
110 #ifdef USE_PWM_INPUT2
111  icuStart(&PWM_INPUT2_ICU, &pwm_input2_cfg);
113  icuStartCapture(&PWM_INPUT2_ICU);
114  icuEnableNotifications(&PWM_INPUT2_ICU);
115 #endif
116 
117 }
118 
void gpio_setup_pin_af(ioportid_t port, uint16_t pin, uint8_t af, bool is_output)
Setup a gpio for input or output with alternate function.
Definition: gpio_arch.c:61
void pwm_input_init(void)
volatile uint32_t pwm_input_duty_tics[PWM_INPUT_NB]
Definition: pwm_input.c:34
#define PWM_INPUT1_GPIO_PIN
Definition: board.h:459
Some architecture independent helper functions for GPIOs.
arch independent PWM input capture API
#define PWM_INPUT2_GPIO_AF
Definition: board.h:470
volatile uint8_t pwm_input_period_valid[PWM_INPUT_NB]
Definition: pwm_input.c:37
#define PWM_INPUT2_ICU
Definition: board.h:466
volatile uint8_t pwm_input_duty_valid[PWM_INPUT_NB]
Definition: pwm_input.c:35
#define ONE_MHZ_CLK
#define PWM_INPUT1_GPIO_PORT
Definition: board.h:458
#define PWM_INPUT1_GPIO_AF
Definition: board.h:460
volatile uint32_t pwm_input_period_tics[PWM_INPUT_NB]
Definition: pwm_input.c:36
#define PWM_INPUT1_ICU
Definition: board.h:455
#define PWM_INPUT2_CHANNEL
Definition: board.h:467
#define PWM_INPUT2_GPIO_PORT
Definition: board.h:468
#define PWM_INPUT2_GPIO_PIN
Definition: board.h:469
#define PWM_INPUT1_CHANNEL
Definition: board.h:456
#define PWM_INPUT2_TICKS_PER_USEC
#define PWM_INPUT1_TICKS_PER_USEC
The default pwm counter is set-up to have 1/6 us resolution.