-
Notifications
You must be signed in to change notification settings - Fork 0
/
ultrasonicranging.c
262 lines (226 loc) · 7.16 KB
/
ultrasonicranging.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
* Water Tower Monitor
*
* Copyright (c) 2015, longfeng.xiao <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <cmsis_os.h> // CMSIS RTOS header file
#include <stdio.h>
#include <stdlib.h>
#include "stm32f10x_exti.h"
#define TIM_CAPTURE_INVALID 0xffff
#define TIM_DELTA_USEC_INVALID 0xffffffff
#define TIM_PERIOD 0x7fff
#define TIM_ACCURACY_USEC_SHIFT 3
#define TIM_ACCURACY_USEC (1 << TIM_ACCURACY_USEC_SHIFT)
typedef struct
{
uint8_t id;
GPIO_TypeDef* triggerGPIO;
uint16_t trigger;
GPIO_TypeDef* echoGPIO;
uint16_t echo;
TIM_TypeDef* tim;
uint16_t channel;
uint16_t irq;
uint16_t capture;
uint32_t delta_usec;
uint8_t renewed;
} UltrasonicRanging;
static UltrasonicRanging sensors[2] = {
{
0,
GPIOA,
GPIO_Pin_7,
GPIOA,
GPIO_Pin_2,
TIM5,
TIM_Channel_3,
TIM_IT_CC3,
TIM_CAPTURE_INVALID,
TIM_DELTA_USEC_INVALID,
0,
},
{
1,
GPIOA,
GPIO_Pin_5,
GPIOA,
GPIO_Pin_3,
TIM5,
TIM_Channel_4,
TIM_IT_CC4,
TIM_CAPTURE_INVALID,
TIM_DELTA_USEC_INVALID,
0,
},
};
#define NUM_OF_SENSOR (sizeof (sensors) / sizeof (sensors[0]))
static uint16_t ultrasonicRangingsampleInterval = 0;
/*----------------------------------------------------------------------------
* Thread 1: ultrasonic ranging thread
*---------------------------------------------------------------------------*/
static void ultrasonicRanging(void const *argument); // thread function
static osThreadId tidultrasonicRanging; // thread id
static osThreadDef (ultrasonicRanging, osPriorityNormal, 1, 0); // thread object
static uint16_t TIM_GetCapture(TIM_TypeDef* TIMx, uint16_t TIM_IT)
{
uint16_t ccr;
switch (TIM_IT) {
case TIM_IT_CC1:
ccr = TIM_GetCapture1(TIMx);
break;
case TIM_IT_CC2:
ccr = TIM_GetCapture2(TIMx);
break;
case TIM_IT_CC3:
ccr = TIM_GetCapture3(TIMx);
break;
case TIM_IT_CC4:
ccr = TIM_GetCapture4(TIMx);
break;
default:
ccr = 0;
break;
}
return ccr;
}
static uint16_t captureAccuracy(uint16_t usec)
{
uint32_t prescale;
usec = usec < 900 ? usec : 900;
prescale = SystemCoreClock / 1000000 * usec - 1;
return prescale < 0xffff ? prescale : 0xffff;
}
static void timICInit(UltrasonicRanging *sensor, uint16_t polarity)
{
TIM_ICInitTypeDef timICInitStruct;
timICInitStruct.TIM_Channel = sensor->channel;
timICInitStruct.TIM_ICPolarity = polarity;
timICInitStruct.TIM_ICSelection = TIM_ICSelection_DirectTI;
timICInitStruct.TIM_ICPrescaler = TIM_ICPSC_DIV1;
timICInitStruct.TIM_ICFilter = 0x00;
TIM_ICInit(sensor->tim, &timICInitStruct);
}
static void timICStart(UltrasonicRanging *sensor)
{
sensor->capture = TIM_CAPTURE_INVALID;
timICInit(sensor, TIM_ICPolarity_Rising);
TIM_Cmd(sensor->tim, ENABLE);
TIM_ITConfig(sensor->tim, sensor->irq, ENABLE);
}
static void timICStop(UltrasonicRanging *sensor)
{
TIM_Cmd(sensor->tim, DISABLE);
TIM_ITConfig(sensor->tim, sensor->irq, DISABLE);
}
void TIM5_IRQHandler(void)
{
UltrasonicRanging *sensor;
uint32_t delta_usec;
uint16_t capture;
int i;
for (i = 0; i < NUM_OF_SENSOR; i++) {
sensor = &sensors[i];
if (TIM_GetITStatus(sensor->tim, sensor->irq) == SET) {
TIM_ClearITPendingBit(sensor->tim, sensor->irq);
if (sensor->capture != TIM_CAPTURE_INVALID) {
sensor->renewed = 0;
capture = TIM_GetCapture(sensor->tim, sensor->irq);
if (capture > sensor->capture) {
delta_usec = (capture - sensor->capture) << TIM_ACCURACY_USEC_SHIFT;
} else {
delta_usec = ((TIM_PERIOD - sensor->capture + 1) + capture) << TIM_ACCURACY_USEC_SHIFT;
}
sensor->delta_usec = delta_usec > 5 ? delta_usec : TIM_DELTA_USEC_INVALID;
timICStop(sensor);
} else {
sensor->capture = TIM_GetCapture(sensor->tim, sensor->irq);
timICInit(sensor, TIM_ICPolarity_Falling);
}
}
}
}
static void sensorInit(void)
{
GPIO_InitTypeDef gpioInitStructure;
TIM_TimeBaseInitTypeDef timTimeBaseInitStruct;
UltrasonicRanging *sensor;
int i;
for (i = 0; i < NUM_OF_SENSOR; i++) {
sensor = &sensors[i];
sensor->capture = TIM_CAPTURE_INVALID;
gpioInitStructure.GPIO_Pin = sensor->trigger;
gpioInitStructure.GPIO_Speed = GPIO_Speed_2MHz;
gpioInitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(sensor->triggerGPIO, &gpioInitStructure);
GPIO_WriteBit(sensor->triggerGPIO, sensor->trigger, Bit_RESET);
gpioInitStructure.GPIO_Pin = sensor->echo;
gpioInitStructure.GPIO_Speed = GPIO_Speed_2MHz;
gpioInitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_Init(sensor->echoGPIO, &gpioInitStructure);
timTimeBaseInitStruct.TIM_Prescaler = captureAccuracy(TIM_ACCURACY_USEC);
timTimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
timTimeBaseInitStruct.TIM_Period = TIM_PERIOD;
timTimeBaseInitStruct.TIM_ClockDivision = 0;
timTimeBaseInitStruct.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(sensor->tim, &timTimeBaseInitStruct);
}
}
int ultrasonicRangingInit(void)
{
tidultrasonicRanging = osThreadCreate (osThread(ultrasonicRanging), NULL);
if(!tidultrasonicRanging) return(-1);
sensorInit();
return(0);
}
void setUltrasonicRangingSampleInterval(uint16_t sampleInterval)
{
ultrasonicRangingsampleInterval = sampleInterval;
}
uint32_t getUltrasonicRangingSample(uint16_t index)
{
if (index > NUM_OF_SENSOR)
return 0;
if (sensors[index].renewed < 3)
return sensors[index].delta_usec;
else
return 0xffffffff;
}
void ultrasonicRanging(void const *argument)
{
UltrasonicRanging *sensor;
int i;
osDelay(3000);
while (1) {
if (ultrasonicRangingsampleInterval > 0) {
for (i = 0; i < NUM_OF_SENSOR; i++) {
sensor = &sensors[i];
sensor->renewed++;
if (sensor->renewed > 128)
sensor->renewed = 128;
GPIO_WriteBit(sensor->triggerGPIO, sensor->trigger, Bit_SET);
osDelay(2);
timICStart(sensor);
GPIO_WriteBit(sensor->triggerGPIO, sensor->trigger, Bit_RESET);
osDelay(ultrasonicRangingsampleInterval * 500);
}
} else {
osDelay(1000);
}
}
}