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!
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;
47 #define PIOS_HCSR04_TRIG_GPIO_PORT GPIOD
48 #define PIOS_HCSR04_TRIG_PIN GPIO_Pin_2
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;
71 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
74 NVIC_InitTypeDef NVIC_InitStructure;
76 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
77 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
78 NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
79 NVIC_Init(&NVIC_InitStructure);
82 GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE);
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);
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);
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);
111 TIM_ITConfig(TIM3, TIM_IT_CC2, DISABLE);
114 TIM_Cmd(TIM3, ENABLE);
118 TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
119 TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
120 TIM_ICInitStructure.TIM_ICFilter = 0x0;
146 PIOS_HCSR04_TRIG_GPIO_PORT->BSRR = PIOS_HCSR04_TRIG_PIN;
148 PIOS_HCSR04_TRIG_GPIO_PORT->BRR = PIOS_HCSR04_TRIG_PIN;
149 TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE);
163 if (TIM_GetITStatus(TIM3, TIM_IT_CC2) == SET) {
165 if (CaptureState == 0) {
166 RiseValue = TIM_GetCapture2(TIM3);
168 FallValue = TIM_GetCapture2(TIM3);
173 TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
176 if (CaptureState == 0) {
181 TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
182 TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
183 TIM_ICInit(TIM3, &TIM_ICInitStructure);
187 if (FallValue > RiseValue) {
188 CaptureValue = (FallValue - RiseValue);
190 CaptureValue = ((0xFFFF - RiseValue) + FallValue);
198 TIM_ITConfig(TIM3, TIM_IT_CC2, DISABLE);
201 TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
202 TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
203 TIM_ICInit(TIM3, &TIM_ICInitStructure);
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
int32_t PIOS_HCSR04_Completed(void)
int32_t PIOS_HCSR04_Get(void)
int32_t PIOS_DELAY_WaituS(uint32_t uS)