dRonin  adbada4
dRonin firmware
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pios_hcsr04.c
Go to the documentation of this file.
1 
15 /*
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful, but
22  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24  * for more details.
25  *
26  * You should have received a copy of the GNU General Public License along
27  * with this program; if not, see <http://www.gnu.org/licenses/>
28  */
29 
30 /* Project Includes */
31 #include "pios.h"
32 
33 #if defined(PIOS_INCLUDE_HCSR04)
34 #if !(defined(PIOS_INCLUDE_DSM) || defined(PIOS_INCLUDE_SBUS))
35 #error Only supported with Spektrum/JR DSM or S.Bus interface!
36 #endif
37 
38 /* Local Variables */
39 
40 static TIM_ICInitTypeDef TIM_ICInitStructure;
41 static uint8_t CaptureState;
42 static uint16_t RiseValue;
43 static uint16_t FallValue;
44 static uint32_t CaptureValue;
45 static uint32_t CapCounter;
46 
47 #define PIOS_HCSR04_TRIG_GPIO_PORT GPIOD
48 #define PIOS_HCSR04_TRIG_PIN GPIO_Pin_2
49 
53 void PIOS_HCSR04_Init(void)
54 {
55  /* Init triggerpin */
56  GPIO_InitTypeDef GPIO_InitStructure;
57  GPIO_StructInit(&GPIO_InitStructure);
58  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
59  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
60  GPIO_InitStructure.GPIO_Pin = PIOS_HCSR04_TRIG_PIN;
61  GPIO_Init(PIOS_HCSR04_TRIG_GPIO_PORT, &GPIO_InitStructure);
62  PIOS_HCSR04_TRIG_GPIO_PORT->BSRR = PIOS_HCSR04_TRIG_PIN;
63 
64  /* Flush counter variables */
65  CaptureState = 0;
66  RiseValue = 0;
67  FallValue = 0;
68  CaptureValue = 0;
69 
70  /* Setup RCC */
71  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
72 
73  /* Enable timer interrupts */
74  NVIC_InitTypeDef NVIC_InitStructure;
75  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = PIOS_IRQ_PRIO_MID;
76  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
77  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
78  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
79  NVIC_Init(&NVIC_InitStructure);
80 
81  /* Partial pin remap for TIM3 (PB5) */
82  GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE);
83 
84  /* Configure input pins */
85  GPIO_StructInit(&GPIO_InitStructure);
86  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
87  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
88  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
89  GPIO_Init(GPIOB, &GPIO_InitStructure);
90 
91  /* Configure timer for input capture */
92  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
93  TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
94  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
95  TIM_ICInitStructure.TIM_ICFilter = 0x0;
96  TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
97  TIM_ICInit(TIM3, &TIM_ICInitStructure);
98 
99  /* Configure timer clocks */
100  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
101  TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
102  TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
103  TIM_TimeBaseStructure.TIM_Prescaler = (PIOS_SYSCLK / 500000) - 1;
104  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
105  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
106  TIM_InternalClockConfig(TIM3);
107  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
108 
109  /* Enable the Capture Compare Interrupt Request */
110  //TIM_ITConfig(PIOS_PWM_CH8_TIM_PORT, PIOS_PWM_CH8_CCR, ENABLE);
111  TIM_ITConfig(TIM3, TIM_IT_CC2, DISABLE);
112 
113  /* Enable timers */
114  TIM_Cmd(TIM3, ENABLE);
115 
116  /* Setup local variable which stays in this scope */
117  /* Doing this here and using a local variable saves doing it in the ISR */
118  TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
119  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
120  TIM_ICInitStructure.TIM_ICFilter = 0x0;
121 }
122 
127 int32_t PIOS_HCSR04_Get(void)
128 {
129  return CaptureValue;
130 }
131 
136 int32_t PIOS_HCSR04_Completed(void)
137 {
138  return CapCounter;
139 }
143 void PIOS_HCSR04_Trigger(void)
144 {
145  CapCounter=0;
146  PIOS_HCSR04_TRIG_GPIO_PORT->BSRR = PIOS_HCSR04_TRIG_PIN;
147  PIOS_DELAY_WaituS(15);
148  PIOS_HCSR04_TRIG_GPIO_PORT->BRR = PIOS_HCSR04_TRIG_PIN;
149  TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE);
150 }
151 
152 
156 //void PIOS_PWM_irq_handler(TIM_TypeDef * timer)
157 void TIM3_IRQHandler(void)
158 {
159  /* Zero value always will be changed but this prevents compiler warning */
160  int32_t i = 0;
161 
162  /* Do this as it's more efficient */
163  if (TIM_GetITStatus(TIM3, TIM_IT_CC2) == SET) {
164  i = 7;
165  if (CaptureState == 0) {
166  RiseValue = TIM_GetCapture2(TIM3);
167  } else {
168  FallValue = TIM_GetCapture2(TIM3);
169  }
170  }
171 
172  /* Clear TIM3 Capture compare interrupt pending bit */
173  TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
174 
175  /* Simple rise or fall state machine */
176  if (CaptureState == 0) {
177  /* Switch states */
178  CaptureState = 1;
179 
180  /* Switch polarity of input capture */
181  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
182  TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
183  TIM_ICInit(TIM3, &TIM_ICInitStructure);
184 
185  } else {
186  /* Capture computation */
187  if (FallValue > RiseValue) {
188  CaptureValue = (FallValue - RiseValue);
189  } else {
190  CaptureValue = ((0xFFFF - RiseValue) + FallValue);
191  }
192 
193  /* Switch states */
194  CaptureState = 0;
195 
196  /* Increase supervisor counter */
197  CapCounter++;
198  TIM_ITConfig(TIM3, TIM_IT_CC2, DISABLE);
199 
200  /* Switch polarity of input capture */
201  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
202  TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
203  TIM_ICInit(TIM3, &TIM_ICInitStructure);
204 
205  }
206 }
207 
208 
209 #endif
Main PiOS header to include all the compiled in PiOS options.
void PIOS_HCSR04_Init(void)
void PIOS_HCSR04_Trigger(void)
#define PIOS_IRQ_PRIO_MID
Definition: pios_board.h:170
#define TIM3_IRQHandler
uint8_t i
Definition: msp_messages.h:97
int32_t PIOS_HCSR04_Completed(void)
int32_t PIOS_HCSR04_Get(void)
#define PIOS_SYSCLK
Definition: pios_board.h:143
int32_t PIOS_DELAY_WaituS(uint32_t uS)
Definition: pios_delay.c:116