LibDriver BA234
Loading...
Searching...
No Matches
driver_ba234.c
Go to the documentation of this file.
1
36
37#include "driver_ba234.h"
38
42#define CHIP_NAME "AtomBit BA234"
43#define MANUFACTURER_NAME "AtomBit"
44#define SUPPLY_VOLTAGE_MIN 3.0f
45#define SUPPLY_VOLTAGE_MAX 3.6f
46#define MAX_CURRENT 1.0f
47#define TEMPERATURE_MIN -40.0f
48#define TEMPERATURE_MAX 85.0f
49#define DRIVER_VERSION 1000
50
54#define BA234_COMMAND_READ 0xA0
55#define BA234_COMMAND_BASELINE 0xA1
56#define BA234_COMMAND_CALIBRATION 0xA7
57
68static uint8_t a_ba234_make_frame(uint8_t command, uint32_t data, uint8_t output[6])
69{
70 uint8_t i;
71 uint16_t sum;
72
73 output[0] = command; /* set command */
74 output[1] = (data >> 24) & 0xFF; /* set data */
75 output[2] = (data >> 16) & 0xFF; /* set data */
76 output[3] = (data >> 8) & 0xFF; /* set data */
77 output[4] = (data >> 0) & 0xFF; /* set data */
78
79 sum = 0; /* init 0 */
80 for (i = 0; i < 5; i++) /* add all */
81 {
82 sum += output[i]; /* sum */
83 }
84 output[5] = sum & 0xFF; /* set sum */
85
86 return 0; /* success return 0 */
87}
88
99static uint8_t a_ba234_parse_frame(ba234_handle_t *handle, uint8_t input[6], uint32_t *data)
100{
101 uint8_t i;
102 uint16_t sum;
103
104 sum = 0; /* init 0 */
105 for (i = 0; i < 5; i++) /* add all */
106 {
107 sum += input[i]; /* sum */
108 }
109 if ((sum & 0xFF) != input[5]) /* check sum */
110 {
111 handle->debug_print("ba234: checksum error.\n"); /* checksum error */
112
113 return 1; /* return error */
114 }
115 if (input[0] != 0xAC) /* check frame header */
116 {
117 handle->debug_print("ba234: frame header invalid.\n"); /* frame header invalid */
118
119 return 1; /* return error */
120 }
121 *data = ((uint32_t)input[1] << 24) | ((uint32_t)input[2] << 16) |
122 ((uint32_t)input[3] << 8) | ((uint32_t)input[4] << 0); /* get data */
123
124 return 0; /* success return 0 */
125}
126
137static uint8_t a_ba234_parse_data(ba234_handle_t *handle, uint8_t input[16], ba234_data_t *data)
138{
139 uint8_t i;
140 uint16_t sum;
141
142 sum = 0; /* init 0 */
143 for (i = 0; i < 15; i++) /* add all */
144 {
145 sum += input[i]; /* sum */
146 }
147 if ((sum & 0xFF) != input[15]) /* check sum */
148 {
149 handle->debug_print("ba234: checksum error.\n"); /* checksum error */
150
151 return 1; /* return error */
152 }
153 if (input[0] != 0xAA) /* check frame header */
154 {
155 handle->debug_print("ba234: frame header invalid.\n"); /* frame header invalid */
156
157 return 1; /* return error */
158 }
159 data->tds_raw = (((uint16_t)(input[1])) << 8) | (uint16_t)(input[2]); /* set tds raw */
160 data->electrical_conductivity_raw = (((uint32_t)(input[3])) << 24) |
161 (((uint32_t)(input[4])) << 16) |
162 (((uint32_t)(input[5])) << 8) |
163 (((uint32_t)(input[6])) << 0);
164 data->salinity_raw = (((uint16_t)(input[7])) << 8) | (uint16_t)(input[8]); /* set electrical conductivity raw */
165 data->specific_gravity_raw = (((uint16_t)(input[9])) << 8) | (uint16_t)(input[10]); /* set specific gravity raw */
166 data->temperature_raw = (((uint16_t)(input[11])) << 8) | (uint16_t)(input[12]); /* set temperature raw */
167 data->water_hardness_raw = (((uint16_t)(input[13])) << 8) | (uint16_t)(input[14]); /* set water hardness raw */
168 data->tds_ppm = (float)(data->tds_raw); /* convert tds */
169 data->electrical_conductivity_us_cm = (float)(data->electrical_conductivity_raw); /* convert electrical conductivity */
170 data->salinity_percentage = (float)(data->salinity_raw) / 100.0f; /* convert salinity */
171 data->specific_gravity = (float)(data->specific_gravity_raw) / 10000.0f; /* convert specific gravity */
172 data->temperature_degrees_celsius = (float)(data->temperature_raw) / 10.0f; /* convert temperature */
173 data->water_hardness_ppm = (float)(data->water_hardness_raw); /* convert water hardness */
174
175 return 0; /* success return 0 */
176}
177
189{
190 if (handle == NULL) /* check handle */
191 {
192 return 2; /* return error */
193 }
194 if (handle->debug_print == NULL) /* check debug_print */
195 {
196 return 3; /* return error */
197 }
198 if (handle->uart_init == NULL) /* check uart_init */
199 {
200 handle->debug_print("ba234: uart_init is null.\n"); /* uart_init is null */
201
202 return 3; /* return error */
203 }
204 if (handle->uart_deinit == NULL) /* check uart_deinit */
205 {
206 handle->debug_print("ba234: uart_deinit is null.\n"); /* uart_deinit is null */
207
208 return 3; /* return error */
209 }
210 if (handle->uart_read == NULL) /* check uart_read */
211 {
212 handle->debug_print("ba234: uart_read is null.\n"); /* uart_read is null */
213
214 return 3; /* return error */
215 }
216 if (handle->uart_flush == NULL) /* check uart_flush */
217 {
218 handle->debug_print("ba234: uart_flush is null.\n"); /* uart_flush is null */
219
220 return 3; /* return error */
221 }
222 if (handle->uart_write == NULL) /* check uart_write */
223 {
224 handle->debug_print("ba234: uart_write is null.\n"); /* uart_write is null */
225
226 return 3; /* return error */
227 }
228 if (handle->delay_ms == NULL) /* check delay_ms */
229 {
230 handle->debug_print("ba234: delay_ms is null.\n"); /* delay_ms is null */
231
232 return 3; /* return error */
233 }
234
235 if (handle->uart_init() != 0) /* uart init */
236 {
237 handle->debug_print("ba234: uart init failed.\n"); /* uart init failed */
238
239 return 1; /* return error */
240 }
241
242 handle->inited = 1; /* flag finish initialization */
243
244 return 0; /* success return 0 */
245}
246
258{
259 if (handle == NULL) /* check handle */
260 {
261 return 2; /* return error */
262 }
263 if (handle->inited != 1) /* check handle initialization */
264 {
265 return 3; /* return error */
266 }
267
268 if (handle->uart_deinit() != 0) /* uart deinit */
269 {
270 handle->debug_print("ba234: uart deinit failed.\n"); /* uart deinit failed */
271
272 return 1; /* return error */
273 }
274 handle->inited = 0; /* flag close */
275
276 return 0; /* success return 0 */
277}
278
292{
293 uint8_t res;
294 uint8_t output[6];
295 uint8_t input[16];
296 uint16_t len;
297
298 if (handle == NULL) /* check handle */
299 {
300 return 2; /* return error */
301 }
302 if (handle->inited != 1) /* check handle initialization */
303 {
304 return 3; /* return error */
305 }
306
307 (void)a_ba234_make_frame(BA234_COMMAND_READ, 0x00000000, output); /* make frame */
308 res = handle->uart_flush(); /* uart flush */
309 if (res != 0) /* check result */
310 {
311 handle->debug_print("ba234: uart flush failed.\n"); /* uart flush failed */
312
313 return 1; /* return error */
314 }
315 res = handle->uart_write(output, 6); /* uart write */
316 if (res != 0) /* check result */
317 {
318 handle->debug_print("ba234: uart write failed.\n"); /* uart write failed */
319
320 return 1; /* return error */
321 }
322 handle->delay_ms(BA234_READ_TIMEOUT); /* delay */
323 len = handle->uart_read(input, 16); /* uart read */
324 if (len != 16) /* check length */
325 {
326 handle->debug_print("ba234: uart read failed.\n"); /* uart read failed */
327
328 return 1; /* return error */
329 }
330 res = a_ba234_parse_data(handle, input, data); /* parse data */
331 if (res != 0) /* check result */
332 {
333 handle->debug_print("ba234: frame error.\n"); /* frame error */
334
335 return 4; /* return error */
336 }
337
338 return 0; /* success return 0 */
339}
340
354{
355 uint8_t res;
356 uint8_t output[6];
357 uint8_t input[6];
358 uint16_t len;
359 uint32_t data;
360
361 if (handle == NULL) /* check handle */
362 {
363 return 2; /* return error */
364 }
365 if (handle->inited != 1) /* check handle initialization */
366 {
367 return 3; /* return error */
368 }
369
370 (void)a_ba234_make_frame(BA234_COMMAND_BASELINE, 0x00000000, output); /* make frame */
371 res = handle->uart_flush(); /* uart flush */
372 if (res != 0) /* check result */
373 {
374 handle->debug_print("ba234: uart flush failed.\n"); /* uart flush failed */
375
376 return 1; /* return error */
377 }
378 res = handle->uart_write(output, 6); /* uart write */
379 if (res != 0) /* check result */
380 {
381 handle->debug_print("ba234: uart write failed.\n"); /* uart write failed */
382
383 return 1; /* return error */
384 }
385 handle->delay_ms(BA234_READ_TIMEOUT); /* delay */
386 len = handle->uart_read(input, 6); /* uart read */
387 if (len != 6) /* check length */
388 {
389 handle->debug_print("ba234: uart read failed.\n"); /* uart read failed */
390
391 return 1; /* return error */
392 }
393 res = a_ba234_parse_frame(handle, input, &data); /* parse data */
394 if (res != 0) /* check result */
395 {
396 handle->debug_print("ba234: frame error.\n"); /* frame error */
397
398 return 4; /* return error */
399 }
400 handle->last_status = (data >> 24) & 0xFF; /* save last status */
401 if (handle->last_status != 0) /* check last status */
402 {
403 handle->debug_print("ba234: response error.\n"); /* response error */
404
405 return 5; /* return error */
406 }
407
408 return 0; /* success return 0 */
409}
410
424uint8_t ba234_salinity_calibration(ba234_handle_t *handle, uint16_t value)
425{
426 uint8_t res;
427 uint8_t output[6];
428 uint8_t input[6];
429 uint16_t len;
430 uint32_t data;
431
432 if (handle == NULL) /* check handle */
433 {
434 return 2; /* return error */
435 }
436 if (handle->inited != 1) /* check handle initialization */
437 {
438 return 3; /* return error */
439 }
440
441 (void)a_ba234_make_frame(BA234_COMMAND_CALIBRATION,
442 (uint32_t)(value) << 16, output); /* make frame */
443 res = handle->uart_flush(); /* uart flush */
444 if (res != 0) /* check result */
445 {
446 handle->debug_print("ba234: uart flush failed.\n"); /* uart flush failed */
447
448 return 1; /* return error */
449 }
450 res = handle->uart_write(output, 6); /* uart write */
451 if (res != 0) /* check result */
452 {
453 handle->debug_print("ba234: uart write failed.\n"); /* uart write failed */
454
455 return 1; /* return error */
456 }
457 handle->delay_ms(BA234_READ_TIMEOUT); /* delay */
458 len = handle->uart_read(input, 6); /* uart read */
459 if (len != 6) /* check length */
460 {
461 handle->debug_print("ba234: uart read failed.\n"); /* uart read failed */
462
463 return 1; /* return error */
464 }
465 res = a_ba234_parse_frame(handle, input, &data); /* parse data */
466 if (res != 0) /* check result */
467 {
468 handle->debug_print("ba234: frame error.\n"); /* frame error */
469
470 return 4; /* return error */
471 }
472 handle->last_status = (data >> 24) & 0xFF; /* save last status */
473 if (handle->last_status != 0) /* check last status */
474 {
475 handle->debug_print("ba234: response error.\n"); /* response error */
476
477 return 5; /* return error */
478 }
479
480 return 0; /* success return 0 */
481}
482
494{
495 if (handle == NULL) /* check handle */
496 {
497 return 2; /* return error */
498 }
499 if (handle->inited != 1) /* check handle initialization */
500 {
501 return 3; /* return error */
502 }
503 *status = (ba234_status_t)(handle->last_status); /* set status */
504
505 return 0; /* success return 0 */
506}
507
519uint8_t ba234_salinity_convert_to_register(ba234_handle_t *handle, float percentage, uint16_t *reg)
520{
521 if (handle == NULL) /* check handle */
522 {
523 return 2; /* return error */
524 }
525 if (handle->inited != 1) /* check handle initialization */
526 {
527 return 3; /* return error */
528 }
529
530 *reg = (uint16_t)(percentage * 100.0f); /* convert real data to register data */
531
532 return 0; /* success return 0 */
533}
534
546uint8_t ba234_salinity_convert_to_data(ba234_handle_t *handle, uint16_t reg, float *percentage)
547{
548 if (handle == NULL) /* check handle */
549 {
550 return 2; /* return error */
551 }
552 if (handle->inited != 1) /* check handle initialization */
553 {
554 return 3; /* return error */
555 }
556
557 *percentage = (float)(reg) / 100.0f; /* convert raw data to real data */
558
559 return 0; /* success return 0 */
560}
561
574uint8_t ba234_set_buffer(ba234_handle_t *handle, uint8_t *buf, uint16_t len)
575{
576 uint8_t res;
577
578 if (handle == NULL) /* check handle */
579 {
580 return 2; /* return error */
581 }
582 if (handle->inited != 1) /* check handle initialization */
583 {
584 return 3; /* return error */
585 }
586
587 res = handle->uart_flush(); /* uart flush */
588 if (res != 0) /* check result */
589 {
590 handle->debug_print("ba234: uart flush failed.\n"); /* uart flush failed */
591
592 return 1; /* return error */
593 }
594 res = handle->uart_write(buf, len); /* uart write */
595 if (res != 0) /* check result */
596 {
597 handle->debug_print("ba234: uart write failed.\n"); /* uart write failed */
598
599 return 1; /* return error */
600 }
601
602 return 0; /* success return 0 */
603}
604
617uint8_t ba234_get_buffer(ba234_handle_t *handle, uint8_t *buf, uint16_t len)
618{
619 uint16_t l;
620
621 if (handle == NULL) /* check handle */
622 {
623 return 2; /* return error */
624 }
625 if (handle->inited != 1) /* check handle initialization */
626 {
627 return 3; /* return error */
628 }
629
630 l = handle->uart_read((uint8_t *)buf, len); /* uart read */
631 if (l != len) /* check result */
632 {
633 handle->debug_print("ba234: uart read failed.\n"); /* uart read failed */
634
635 return 1; /* return error */
636 }
637
638 return 0; /* success return 0 */
639}
640
650{
651 if (info == NULL) /* check handle */
652 {
653 return 2; /* return error */
654 }
655
656 memset(info, 0, sizeof(ba234_info_t)); /* initialize ba234 info structure */
657 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
658 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
659 strncpy(info->interface, "UART", 8); /* copy interface name */
660 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
661 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
662 info->max_current_ma = MAX_CURRENT; /* set maximum current */
663 info->temperature_max = TEMPERATURE_MAX; /* set maximum temperature */
664 info->temperature_min = TEMPERATURE_MIN; /* set minimal temperature */
665 info->driver_version = DRIVER_VERSION; /* set driver version */
666
667 return 0; /* success return 0 */
668}
#define MAX_CURRENT
#define BA234_COMMAND_BASELINE
#define SUPPLY_VOLTAGE_MAX
#define BA234_COMMAND_READ
chip command definition
#define TEMPERATURE_MAX
#define BA234_COMMAND_CALIBRATION
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
driver ba234 header file
struct ba234_info_s ba234_info_t
ba234 information structure definition
uint8_t ba234_info(ba234_info_t *info)
get chip's information
uint8_t ba234_get_last_status(ba234_handle_t *handle, ba234_status_t *status)
get last status
#define BA234_READ_TIMEOUT
ba234 read timeout definition
uint8_t ba234_init(ba234_handle_t *handle)
initialize the chip
uint8_t ba234_deinit(ba234_handle_t *handle)
close the chip
struct ba234_handle_s ba234_handle_t
ba234 handle structure definition
uint8_t ba234_salinity_calibration(ba234_handle_t *handle, uint16_t value)
salinity calibration
uint8_t ba234_baseline_calibration(ba234_handle_t *handle)
baseline calibration
uint8_t ba234_read(ba234_handle_t *handle, ba234_data_t *data)
read the data
struct ba234_data_s ba234_data_t
ba234 data structure definition
uint8_t ba234_salinity_convert_to_data(ba234_handle_t *handle, uint16_t reg, float *percentage)
convert the register raw data to the salinity
uint8_t ba234_salinity_convert_to_register(ba234_handle_t *handle, float percentage, uint16_t *reg)
convert the salinity to the register raw data
ba234_status_t
ba234 status enumeration definition
uint8_t ba234_get_buffer(ba234_handle_t *handle, uint8_t *buf, uint16_t len)
get buffer
uint8_t ba234_set_buffer(ba234_handle_t *handle, uint8_t *buf, uint16_t len)
set buffer
float temperature_degrees_celsius
uint16_t temperature_raw
float specific_gravity
float electrical_conductivity_us_cm
float salinity_percentage
uint16_t specific_gravity_raw
float water_hardness_ppm
uint16_t salinity_raw
uint16_t water_hardness_raw
uint16_t tds_raw
uint32_t electrical_conductivity_raw
uint8_t(* uart_flush)(void)
uint8_t(* uart_write)(uint8_t *buf, uint16_t len)
uint8_t last_status
void(* delay_ms)(uint32_t ms)
uint8_t(* uart_deinit)(void)
void(* debug_print)(const char *const fmt,...)
uint16_t(* uart_read)(uint8_t *buf, uint16_t len)
uint8_t(* uart_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]