LibDriver BA111
Loading...
Searching...
No Matches
driver_ba111.c
Go to the documentation of this file.
1
36
37#include "driver_ba111.h"
38
42#define CHIP_NAME "AtomBit BA111"
43#define MANUFACTURER_NAME "AtomBit"
44#define SUPPLY_VOLTAGE_MIN 3.3f
45#define SUPPLY_VOLTAGE_MAX 5.0f
46#define MAX_CURRENT 3.0f
47#define TEMPERATURE_MIN -10.0f
48#define TEMPERATURE_MAX 75.0f
49#define DRIVER_VERSION 1000
50
54#define BA111_COMMAND_READ 0xA0
55#define BA111_COMMAND_BASELINE 0xA6
56#define BA111_COMMAND_NTC_RES 0xA3
57#define BA111_COMMAND_NTC_B 0xA5
58
69static uint8_t a_ba111_make_frame(uint8_t command, uint32_t data, uint8_t output[6])
70{
71 uint8_t i;
72 uint16_t sum;
73
74 output[0] = command; /* set command */
75 output[1] = (data >> 24) & 0xFF; /* set data */
76 output[2] = (data >> 16) & 0xFF; /* set data */
77 output[3] = (data >> 8) & 0xFF; /* set data */
78 output[4] = (data >> 0) & 0xFF; /* set data */
79
80 sum = 0; /* init 0 */
81 for (i = 0; i < 5; i++) /* add all */
82 {
83 sum += output[i]; /* sum */
84 }
85 output[5] = sum & 0xFF; /* set sum */
86
87 return 0; /* success return 0 */
88}
89
101static uint8_t a_ba111_parse_frame(ba111_handle_t *handle, uint8_t is_data, uint8_t input[6], uint32_t *data)
102{
103 uint8_t i;
104 uint16_t sum;
105
106 sum = 0; /* init 0 */
107 for (i = 0; i < 5; i++) /* add all */
108 {
109 sum += input[i]; /* sum */
110 }
111 if ((sum & 0xFF) != input[5]) /* check sum */
112 {
113 handle->debug_print("ba111: checksum error.\n"); /* checksum error */
114
115 return 1; /* return error */
116 }
117 if (is_data != 0) /* is data */
118 {
119 if (input[0] != 0xAA) /* check frame header */
120 {
121 handle->debug_print("ba111: frame header invalid.\n"); /* frame header invalid */
122
123 return 1; /* return error */
124 }
125 *data = ((uint32_t)input[1] << 24) | ((uint32_t)input[2] << 16) |
126 ((uint32_t)input[3] << 8) | ((uint32_t)input[4] << 0); /* get data */
127 }
128 else
129 {
130 if (input[0] != 0xAC) /* check frame header */
131 {
132 handle->debug_print("ba111: frame header invalid.\n"); /* frame header invalid */
133
134 return 1; /* return error */
135 }
136 *data = ((uint32_t)input[1] << 24) | ((uint32_t)input[2] << 26) |
137 ((uint32_t)input[3] << 8) | ((uint32_t)input[4] << 0); /* get data */
138 }
139
140 return 0; /* success return 0 */
141}
142
154{
155 if (handle == NULL) /* check handle */
156 {
157 return 2; /* return error */
158 }
159 if (handle->debug_print == NULL) /* check debug_print */
160 {
161 return 3; /* return error */
162 }
163 if (handle->uart_init == NULL) /* check uart_init */
164 {
165 handle->debug_print("ba111: uart_init is null.\n"); /* uart_init is null */
166
167 return 3; /* return error */
168 }
169 if (handle->uart_deinit == NULL) /* check uart_deinit */
170 {
171 handle->debug_print("ba111: uart_deinit is null.\n"); /* uart_deinit is null */
172
173 return 3; /* return error */
174 }
175 if (handle->uart_read == NULL) /* check uart_read */
176 {
177 handle->debug_print("ba111: uart_read is null.\n"); /* uart_read is null */
178
179 return 3; /* return error */
180 }
181 if (handle->uart_flush == NULL) /* check uart_flush */
182 {
183 handle->debug_print("ba111: uart_flush is null.\n"); /* uart_flush is null */
184
185 return 3; /* return error */
186 }
187 if (handle->uart_write == NULL) /* check uart_write */
188 {
189 handle->debug_print("ba111: uart_write is null.\n"); /* uart_write is null */
190
191 return 3; /* return error */
192 }
193 if (handle->delay_ms == NULL) /* check delay_ms */
194 {
195 handle->debug_print("ba111: delay_ms is null.\n"); /* delay_ms is null */
196
197 return 3; /* return error */
198 }
199
200 if (handle->uart_init() != 0) /* uart init */
201 {
202 handle->debug_print("ba111: uart init failed.\n"); /* uart init failed */
203
204 return 1; /* return error */
205 }
206
207 handle->inited = 1; /* flag finish initialization */
208
209 return 0; /* success return 0 */
210}
211
223{
224 if (handle == NULL) /* check handle */
225 {
226 return 2; /* return error */
227 }
228 if (handle->inited != 1) /* check handle initialization */
229 {
230 return 3; /* return error */
231 }
232
233 if (handle->uart_deinit() != 0) /* uart deinit */
234 {
235 handle->debug_print("ba111: uart deinit failed.\n"); /* uart deinit failed */
236
237 return 1; /* return error */
238 }
239 handle->inited = 0; /* flag close */
240
241 return 0; /* success return 0 */
242}
243
259uint8_t ba111_read(ba111_handle_t *handle, uint16_t *tds_raw, uint16_t *tds_ppm,
260 uint16_t *temperature_raw, float *temperature)
261{
262 uint8_t res;
263 uint8_t output[6];
264 uint8_t input[6];
265 uint16_t len;
266 uint32_t data;
267
268 if (handle == NULL) /* check handle */
269 {
270 return 2; /* return error */
271 }
272 if (handle->inited != 1) /* check handle initialization */
273 {
274 return 3; /* return error */
275 }
276
277 (void)a_ba111_make_frame(BA111_COMMAND_READ, 0x00000000, output); /* make frame */
278 res = handle->uart_flush(); /* uart flush */
279 if (res != 0) /* check result */
280 {
281 handle->debug_print("ba111: uart flush failed.\n"); /* uart flush failed */
282
283 return 1; /* return error */
284 }
285 res = handle->uart_write(output, 6); /* uart write */
286 if (res != 0) /* check result */
287 {
288 handle->debug_print("ba111: uart write failed.\n"); /* uart write failed */
289
290 return 1; /* return error */
291 }
292 handle->delay_ms(800); /* delay 800ms */
293 len = handle->uart_read(input, 6); /* uart read */
294 if (len != 6) /* check length */
295 {
296 handle->debug_print("ba111: uart read failed.\n"); /* uart read failed */
297
298 return 1; /* return error */
299 }
300 res = a_ba111_parse_frame(handle, 1, input, &data); /* parse data */
301 if (res != 0) /* check result */
302 {
303 handle->debug_print("ba111: frame error.\n"); /* frame error */
304
305 return 4; /* return error */
306 }
307 *tds_raw = (data >> 16) & 0xFFFFU; /* set tds raw */
308 *tds_ppm = *tds_raw ; /* set tds ppm */
309 *temperature_raw = (data >> 0) & 0xFFFFU; /* set temperature raw */
310 *temperature = (float)(*temperature_raw) / 100.0f; /* set temperature */
311
312 return 0; /* success return 0 */
313}
314
328{
329 uint8_t res;
330 uint8_t output[6];
331 uint8_t input[6];
332 uint16_t len;
333 uint32_t data;
334
335 if (handle == NULL) /* check handle */
336 {
337 return 2; /* return error */
338 }
339 if (handle->inited != 1) /* check handle initialization */
340 {
341 return 3; /* return error */
342 }
343
344 (void)a_ba111_make_frame(BA111_COMMAND_BASELINE, 0x00000000, output); /* make frame */
345 res = handle->uart_flush(); /* uart flush */
346 if (res != 0) /* check result */
347 {
348 handle->debug_print("ba111: uart flush failed.\n"); /* uart flush failed */
349
350 return 1; /* return error */
351 }
352 res = handle->uart_write(output, 6); /* uart write */
353 if (res != 0) /* check result */
354 {
355 handle->debug_print("ba111: uart write failed.\n"); /* uart write failed */
356
357 return 1; /* return error */
358 }
359 handle->delay_ms(500); /* delay 500ms */
360 len = handle->uart_read(input, 6); /* uart read */
361 if (len != 6) /* check length */
362 {
363 handle->debug_print("ba111: uart read failed.\n"); /* uart read failed */
364
365 return 1; /* return error */
366 }
367 res = a_ba111_parse_frame(handle, 0, input, &data); /* parse data */
368 if (res != 0) /* check result */
369 {
370 handle->debug_print("ba111: frame error.\n"); /* frame error */
371
372 return 4; /* return error */
373 }
374 handle->last_status = (data >> 24) & 0xFF; /* save last status */
375 if (handle->last_status != 0) /* check last status */
376 {
377 handle->debug_print("ba111: response error.\n"); /* response error */
378
379 return 5; /* return error */
380 }
381
382 return 0; /* success return 0 */
383}
384
398uint8_t ba111_set_ntc_resistance(ba111_handle_t *handle, uint32_t ohm)
399{
400 uint8_t res;
401 uint8_t output[6];
402 uint8_t input[6];
403 uint16_t len;
404 uint32_t data;
405
406 if (handle == NULL) /* check handle */
407 {
408 return 2; /* return error */
409 }
410 if (handle->inited != 1) /* check handle initialization */
411 {
412 return 3; /* return error */
413 }
414
415 (void)a_ba111_make_frame(BA111_COMMAND_NTC_RES, ohm, output); /* make frame */
416 res = handle->uart_flush(); /* uart flush */
417 if (res != 0) /* check result */
418 {
419 handle->debug_print("ba111: uart flush failed.\n"); /* uart flush failed */
420
421 return 1; /* return error */
422 }
423 res = handle->uart_write(output, 6); /* uart write */
424 if (res != 0) /* check result */
425 {
426 handle->debug_print("ba111: uart write failed.\n"); /* uart write failed */
427
428 return 1; /* return error */
429 }
430 handle->delay_ms(500); /* delay 500ms */
431 len = handle->uart_read(input, 6); /* uart read */
432 if (len != 6) /* check length */
433 {
434 handle->debug_print("ba111: uart read failed.\n"); /* uart read failed */
435
436 return 1; /* return error */
437 }
438 res = a_ba111_parse_frame(handle, 0, input, &data); /* parse data */
439 if (res != 0) /* check result */
440 {
441 handle->debug_print("ba111: frame error.\n"); /* frame error */
442
443 return 4; /* return error */
444 }
445 handle->last_status = (data >> 24) & 0xFF; /* save last status */
446 if (handle->last_status != 0) /* check last status */
447 {
448 handle->debug_print("ba111: response error.\n"); /* response error */
449
450 return 5; /* return error */
451 }
452
453 return 0; /* success return 0 */
454}
455
469uint8_t ba111_set_ntc_b(ba111_handle_t *handle, uint16_t value)
470{
471 uint8_t res;
472 uint8_t output[6];
473 uint8_t input[6];
474 uint16_t len;
475 uint32_t data;
476
477 if (handle == NULL) /* check handle */
478 {
479 return 2; /* return error */
480 }
481 if (handle->inited != 1) /* check handle initialization */
482 {
483 return 3; /* return error */
484 }
485
486 (void)a_ba111_make_frame(BA111_COMMAND_NTC_B, value << 16, output); /* make frame */
487 res = handle->uart_flush(); /* uart flush */
488 if (res != 0) /* check result */
489 {
490 handle->debug_print("ba111: uart flush failed.\n"); /* uart flush failed */
491
492 return 1; /* return error */
493 }
494 res = handle->uart_write(output, 6); /* uart write */
495 if (res != 0) /* check result */
496 {
497 handle->debug_print("ba111: uart write failed.\n"); /* uart write failed */
498
499 return 1; /* return error */
500 }
501 handle->delay_ms(500); /* delay 500ms */
502 len = handle->uart_read(input, 6); /* uart read */
503 if (len != 6) /* check length */
504 {
505 handle->debug_print("ba111: uart read failed.\n"); /* uart read failed */
506
507 return 1; /* return error */
508 }
509 res = a_ba111_parse_frame(handle, 0, input, &data); /* parse data */
510 if (res != 0) /* check result */
511 {
512 handle->debug_print("ba111: frame error.\n"); /* frame error */
513
514 return 4; /* return error */
515 }
516 handle->last_status = (data >> 24) & 0xFF; /* save last status */
517 if (handle->last_status != 0) /* check last status */
518 {
519 handle->debug_print("ba111: response error.\n"); /* response error */
520
521 return 5; /* return error */
522 }
523
524 return 0; /* success return 0 */
525}
526
538{
539 if (handle == NULL) /* check handle */
540 {
541 return 2; /* return error */
542 }
543 if (handle->inited != 1) /* check handle initialization */
544 {
545 return 3; /* return error */
546 }
547 *status = (ba111_status_t)(handle->last_status); /* set status */
548
549 return 0; /* success return 0 */
550}
551
564uint8_t ba111_set_buffer(ba111_handle_t *handle, uint8_t *buf, uint16_t len)
565{
566 uint8_t res;
567
568 if (handle == NULL) /* check handle */
569 {
570 return 2; /* return error */
571 }
572 if (handle->inited != 1) /* check handle initialization */
573 {
574 return 3; /* return error */
575 }
576
577 res = handle->uart_flush(); /* uart flush */
578 if (res != 0) /* check result */
579 {
580 handle->debug_print("ba111: uart flush failed.\n"); /* uart flush failed */
581
582 return 1; /* return error */
583 }
584 res = handle->uart_write(buf, len); /* uart write */
585 if (res != 0) /* check result */
586 {
587 handle->debug_print("ba111: uart write failed.\n"); /* uart write failed */
588
589 return 1; /* return error */
590 }
591
592 return 0; /* success return 0 */
593}
594
607uint8_t ba111_get_buffer(ba111_handle_t *handle, uint8_t *buf, uint16_t len)
608{
609 uint16_t l;
610
611 if (handle == NULL) /* check handle */
612 {
613 return 2; /* return error */
614 }
615 if (handle->inited != 1) /* check handle initialization */
616 {
617 return 3; /* return error */
618 }
619
620 l = handle->uart_read((uint8_t *)buf, len); /* uart read */
621 if (l != len) /* check result */
622 {
623 handle->debug_print("ba111: uart read failed.\n"); /* uart read failed */
624
625 return 1; /* return error */
626 }
627
628 return 0; /* success return 0 */
629}
630
640{
641 if (info == NULL) /* check handle */
642 {
643 return 2; /* return error */
644 }
645
646 memset(info, 0, sizeof(ba111_info_t)); /* initialize ba111 info structure */
647 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
648 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
649 strncpy(info->interface, "UART", 8); /* copy interface name */
650 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
651 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
652 info->max_current_ma = MAX_CURRENT; /* set maximum current */
653 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
654 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
655 info->driver_version = DRIVER_VERSION; /* set driver version */
656
657 return 0; /* success return 0 */
658}
#define BA111_COMMAND_BASELINE
#define BA111_COMMAND_READ
chip command definition
#define MAX_CURRENT
#define SUPPLY_VOLTAGE_MAX
#define TEMPERATURE_MAX
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define BA111_COMMAND_NTC_B
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
#define BA111_COMMAND_NTC_RES
driver ba111 header file
struct ba111_handle_s ba111_handle_t
ba111 handle structure definition
uint8_t ba111_init(ba111_handle_t *handle)
initialize the chip
struct ba111_info_s ba111_info_t
ba111 information structure definition
uint8_t ba111_set_ntc_resistance(ba111_handle_t *handle, uint32_t ohm)
set ntc resistance
uint8_t ba111_info(ba111_info_t *info)
get chip's information
ba111_status_t
ba111 status enumeration definition
uint8_t ba111_baseline_calibration(ba111_handle_t *handle)
baseline calibration
uint8_t ba111_read(ba111_handle_t *handle, uint16_t *tds_raw, uint16_t *tds_ppm, uint16_t *temperature_raw, float *temperature)
read the data
uint8_t ba111_deinit(ba111_handle_t *handle)
close the chip
uint8_t ba111_set_ntc_b(ba111_handle_t *handle, uint16_t value)
set ntc b
uint8_t ba111_get_last_status(ba111_handle_t *handle, ba111_status_t *status)
get last status
uint8_t ba111_set_buffer(ba111_handle_t *handle, uint8_t *buf, uint16_t len)
set buffer
uint8_t ba111_get_buffer(ba111_handle_t *handle, uint8_t *buf, uint16_t len)
get buffer
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]