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