LibDriver HCSR04
Loading...
Searching...
No Matches
driver_hcsr04.c
Go to the documentation of this file.
1
37
38#include "driver_hcsr04.h"
39
43#define CHIP_NAME "JieShenna HCSR04"
44#define MANUFACTURER_NAME "JieShenna"
45#define SUPPLY_VOLTAGE_MIN 4.8f
46#define SUPPLY_VOLTAGE_MAX 5.0f
47#define MAX_CURRENT 15.0f
48#define TEMPERATURE_MIN -40.0f
49#define TEMPERATURE_MAX 85.0f
50#define DRIVER_VERSION 2000
51
63{
64 if (handle == NULL) /* check handle */
65 {
66 return 2; /* return error */
67 }
68 if (handle->debug_print == NULL) /* check debug_print */
69 {
70 return 3; /* return error */
71 }
72 if (handle->trig_init == NULL) /* check trig_init */
73 {
74 handle->debug_print("hcsr04: trig_init is null.\n"); /* trig_init is null */
75
76 return 3; /* return error */
77 }
78 if (handle->trig_deinit == NULL) /* check trig_deinit */
79 {
80 handle->debug_print("hcsr04: trig_deinit is null.\n"); /* trig_deinit is null */
81
82 return 3; /* return error */
83 }
84 if (handle->trig_write == NULL) /* check trig_write */
85 {
86 handle->debug_print("hcsr04: trig_write is null.\n"); /* trig_write is null */
87
88 return 3; /* return error */
89 }
90 if (handle->echo_init == NULL) /* check echo_init */
91 {
92 handle->debug_print("hcsr04: echo_init is null.\n"); /* echo_init is null */
93
94 return 3; /* return error */
95 }
96 if (handle->echo_deinit == NULL) /* check echo_deinit */
97 {
98 handle->debug_print("hcsr04: echo_deinit is null.\n"); /* echo_deinit is null */
99
100 return 3; /* return error */
101 }
102 if (handle->echo_read == NULL) /* check echo_read */
103 {
104 handle->debug_print("hcsr04: echo_read is null.\n"); /* echo_read is null */
105
106 return 3; /* return error */
107 }
108 if (handle->delay_us == NULL) /* check delay_us */
109 {
110 handle->debug_print("hcsr04: delay_us is null.\n"); /* delay_us is null */
111
112 return 3; /* return error */
113 }
114 if (handle->delay_ms == NULL) /* check delay_ms */
115 {
116 handle->debug_print("hcsr04: delay_ms is null.\n"); /* delay_ms is null */
117
118 return 3; /* return error */
119 }
120 if (handle->timestamp_read == NULL) /* check timestamp_read */
121 {
122 handle->debug_print("hcsr04: timestamp_read is null.\n"); /* timestamp_read is null */
123
124 return 3; /* return error */
125 }
126
127 if (handle->trig_init() != 0) /* initialize trig */
128 {
129 handle->debug_print("hcsr04: trig init failed.\n"); /* trig init failed */
130
131 return 1; /* return error */
132 }
133 if (handle->echo_init() != 0) /* initialize echo */
134 {
135 handle->debug_print("hcsr04: echo failed.\n"); /* return error */
136 (void)handle->trig_deinit(); /* deinit trig */
137
138 return 1; /* return error */
139 }
140 handle->inited = 1; /* flag finish initialization */
141
142 return 0; /* success return 0 */
143}
144
156{
157 if (handle == NULL) /* check handle */
158 {
159 return 2; /* return error */
160 }
161 if (handle->inited != 1) /* check handle initialization */
162 {
163 return 3; /* return error */
164 }
165
166 if (handle->echo_deinit() != 0) /* echo deinit */
167 {
168 handle->debug_print("hcsr04: echo deinit failed.\n"); /* echo deinit failed */
169
170 return 1; /* return error */
171 }
172 if (handle->trig_deinit() != 0) /* trig deinit */
173 {
174 handle->debug_print("hcsr04: trig deinit failed.\n"); /* trig deinit failed */
175
176 return 1; /* return error */
177 }
178 handle->inited = 0; /* flag close */
179
180 return 0; /* success return 0 */
181}
182
195uint8_t hcsr04_read(hcsr04_handle_t *handle, uint32_t *time_us, float *m)
196{
197 uint8_t res, value;
198 hcsr04_time_t time_start;
199 hcsr04_time_t time_stop;
200 uint32_t timeout;
201 uint8_t retry = HCSR04_READ_RETRY_TIMES;
202
203 if (handle == NULL) /* check handle */
204 {
205 return 2; /* return error */
206 }
207 if (handle->inited != 1) /* check handle initialization */
208 {
209 return 3; /* return error */
210 }
211
212 while (1) /* loop */
213 {
214 res = handle->trig_write(1); /* write trig 1 */
215 if (res != 0) /* check result */
216 {
217 handle->debug_print("hcsr04: trig write failed.\n"); /* trig write failed */
218
219 return 1; /* return error */
220 }
221 handle->delay_us(20); /* delay 20 us */
222 res = handle->trig_write(0); /* write trig 0 */
223 if (res != 0) /* check result */
224 {
225 handle->debug_print("hcsr04: trig write failed.\n"); /* trig write failed */
226
227 return 1; /* return error */
228 }
229 value = 0; /* reset value */
230 timeout = 1000 * 5000; /* set timeout */
231 while (value == 0) /* wait 5 s */
232 {
233 res = handle->echo_read((uint8_t *)&value); /* read echo data */
234 if (res != 0) /* check result */
235 {
236 handle->debug_print("hcsr04: echo read failed.\n"); /* echo read failed */
237
238 return 1; /* return error */
239 }
240 handle->delay_us(1); /* delay 1 us */
241 timeout--; /* timeout-- */
242 if (timeout == 0) /* if timeout */
243 {
244 handle->debug_print("hcsr04: no response.\n"); /* no response */
245
246 return 1; /* return error */
247 }
248 }
249 res = handle->timestamp_read((hcsr04_time_t *)&time_start); /* read timestamp */
250 if (res != 0) /* check result */
251 {
252 handle->debug_print("hcsr04: read timestamp failed.\n"); /* read timestamp failed */
253
254 return 1; /* return error */
255 }
256 value = 1; /* reset value */
257 timeout = 1000 * 5000; /* wait 5 s */
258 while (value != 0) /* check value */
259 {
260 res = handle->echo_read((uint8_t *)&value); /* read echo data */
261 if (res != 0) /* check result */
262 {
263 handle->debug_print("hcsr04: echo read failed.\n"); /* echo read failed */
264
265 return 1; /* return error */
266 }
267 handle->delay_us(1); /* delay 1 us */
268 timeout--; /* timeout-- */
269 if (timeout == 0) /* if timeout */
270 {
271 handle->debug_print("hcsr04: no response.\n"); /* no response */
272
273 return 1; /* return error */
274 }
275 }
276 res = handle->timestamp_read((hcsr04_time_t *)&time_stop); /* read timestamp */
277 if (res != 0) /* check result */
278 {
279 handle->debug_print("hcsr04: read timestamp failed.\n"); /* read timestamp failed */
280
281 return 1; /* return error */
282 }
283 if (time_stop.millisecond < time_start.millisecond) /* check timestamp */
284 {
285 handle->debug_print("hcsr04: millisecond timestamp invalid.\n"); /* millisecond timestamp is invalid */
286
287 return 1; /* return error */
288 }
289 *time_us = (uint32_t)((int64_t)(((int64_t)time_stop.millisecond -
290 (int64_t)time_start.millisecond) * 1000 +
291 (int64_t)((int64_t)time_stop.microsecond -
292 (int64_t)time_start.microsecond))); /* get time */
293 if ((*time_us) > 150 * 1000) /* check time */
294 {
295 if (retry != 0) /* check remain retry times */
296 {
297 retry--; /* retry times-- */
298 handle->delay_ms(150 + rand() % 100); /* delay rand time */
299 }
300 else
301 {
302 handle->debug_print("hcsr04: no remain retry times.\n"); /* no remain retry times */
303
304 return 1; /* return error */
305 }
306 }
307 else
308 {
309 break; /* successful */
310 }
311 }
312 *m = 340.0f / 2.0f * (float)(*time_us) / 1000000.0f; /* calculate distance */
313
314 return 0; /* success return 0 */
315}
316
326{
327 if (info == NULL) /* check handle */
328 {
329 return 2; /* return error */
330 }
331
332 memset(info, 0, sizeof(hcsr04_info_t)); /* initialize hcsr04 info structure */
333 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
334 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
335 strncpy(info->interface, "GPIO", 8); /* copy interface name */
336 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
337 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
338 info->max_current_ma = MAX_CURRENT; /* set maximum current */
339 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
340 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
341 info->driver_version = DRIVER_VERSION; /* set driver version */
342
343 return 0; /* success return 0 */
344}
#define MAX_CURRENT
#define SUPPLY_VOLTAGE_MAX
#define TEMPERATURE_MAX
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
driver hcsr04 header file
uint8_t hcsr04_init(hcsr04_handle_t *handle)
initialize the chip
struct hcsr04_handle_s hcsr04_handle_t
hcsr04 handle structure definition
#define HCSR04_READ_RETRY_TIMES
hcsr04 read retry times definition
uint8_t hcsr04_read(hcsr04_handle_t *handle, uint32_t *time_us, float *m)
read the distance
uint8_t hcsr04_info(hcsr04_info_t *info)
get chip's information
uint8_t hcsr04_deinit(hcsr04_handle_t *handle)
close the chip
struct hcsr04_time_s hcsr04_time_t
hcsr04 time structure definition
struct hcsr04_info_s hcsr04_info_t
hcsr04 information structure definition
void(* delay_ms)(uint32_t ms)
uint8_t(* echo_read)(uint8_t *value)
void(* delay_us)(uint32_t ms)
uint8_t(* echo_deinit)(void)
void(* debug_print)(const char *const fmt,...)
uint8_t(* trig_write)(uint8_t value)
uint8_t(* trig_deinit)(void)
uint8_t(* trig_init)(void)
uint8_t(* timestamp_read)(hcsr04_time_t *time)
uint8_t(* echo_init)(void)
float supply_voltage_max_v
uint32_t driver_version
char manufacturer_name[32]
float supply_voltage_min_v
char chip_name[32]
uint32_t millisecond
uint64_t microsecond