Paparazzi UAS  v4.0.4_stable-3-gf39211a
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ppm_arch.c
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Copyright (C) 2010 The Paparazzi Team
5  *
6  * This file is part of paparazzi.
7  *
8  * paparazzi is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2, or (at your option)
11  * any later version.
12  *
13  * paparazzi is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with paparazzi; see the file COPYING. If not, write to
20  * the Free Software Foundation, 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 
26 
27 #include <stm32/rcc.h>
28 #include <stm32/gpio.h>
29 #include <stm32/tim.h>
30 #include <stm32/misc.h>
31 
32 #include "mcu_periph/sys_time.h"
33 
34 /*
35  *
36  * This a radio control ppm driver for stm32
37  * signal on PA1 TIM2/CH2 (uart1 trig on lisa/L)
38  *
39  */
44 
45 void tim2_irq_handler(void);
46 
47 void ppm_arch_init ( void ) {
48 
49  /* TIM2 channel 2 pin (PA.01) configuration */
50  GPIO_InitTypeDef GPIO_InitStructure;
51  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
52  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
53  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
54  GPIO_Init(GPIOA, &GPIO_InitStructure);
55 
56  /* TIM2 clock enable */
57  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
58 
59  /* GPIOA clock enable */
60  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
61 
62  /* Time Base configuration */
63  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
64  TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
65  TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
66  TIM_TimeBaseStructure.TIM_Prescaler = 0x8;
67  TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
68  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
69  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
70 
71  /* TIM2 configuration: Input Capture mode ---------------------
72  The external signal is connected to TIM2 CH2 pin (PA.01)
73  The Rising edge is used as active edge,
74  ------------------------------------------------------------ */
75  TIM_ICInitTypeDef TIM_ICInitStructure;
76  TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
77  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
78  TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
79  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
80  TIM_ICInitStructure.TIM_ICFilter = 0x00;
81  TIM_ICInit(TIM2, &TIM_ICInitStructure);
82 
83  /* Enable the TIM2 global Interrupt */
84  NVIC_InitTypeDef NVIC_InitStructure;
85  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
86  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
87  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
88  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
89  NVIC_Init(&NVIC_InitStructure);
90 
91  /* TIM2 enable counter */
92  TIM_Cmd(TIM2, ENABLE);
93 
94  /* Enable the CC2 Interrupt Request */
95  TIM_ITConfig(TIM2, TIM_IT_CC2|TIM_IT_Update, ENABLE);
96 
100 
101 }
102 
103 
104 void tim2_irq_handler(void) {
105 
106  if(TIM_GetITStatus(TIM2, TIM_IT_CC2) == SET) {
107  TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);
108 
109  uint32_t now = TIM_GetCapture2(TIM2) + timer_rollover_cnt;
110  DecodePpmFrame(now);
111  }
112  else if(TIM_GetITStatus(TIM2, TIM_IT_Update) == SET) {
113  timer_rollover_cnt+=(1<<16);
114  TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
115  }
116 
117 }
uint32_t ppm_last_pulse_time
Definition: ppm_arch.c:28
bool_t ppm_data_valid
Definition: ppm_arch.c:29
void ppm_arch_init(void)
Architecture dependant code.
Definition: ppm_arch.c:31
uint8_t ppm_cur_pulse
State machine for decoding ppm frames.
Definition: ppm_arch.c:27
Architecture independent timing functions.
void tim2_irq_handler(void)
Definition: ppm_arch.c:104
unsigned long uint32_t
Definition: types.h:18
#define DecodePpmFrame(_ppm_time)
A valid ppm frame:
Definition: ppm.h:89
#define RADIO_CONTROL_NB_CHANNEL
Definition: spektrum_arch.h:36
unsigned char uint8_t
Definition: types.h:14
static uint32_t timer_rollover_cnt
Definition: ppm_arch.c:43