LibDriver TM1637
Loading...
Searching...
No Matches
driver_tm1637.c
Go to the documentation of this file.
1
36
37#include "driver_tm1637.h"
38
42#define CHIP_NAME "Titan Micro Electronics TM1637"
43#define MANUFACTURER_NAME "Titan Micro Electronics"
44#define SUPPLY_VOLTAGE_MIN 3.3f
45#define SUPPLY_VOLTAGE_MAX 5.5f
46#define MAX_CURRENT 200.0f
47#define TEMPERATURE_MIN -40.0f
48#define TEMPERATURE_MAX 125.0f
49#define DRIVER_VERSION 1000
50
54#define TM1637_COMMAND_DATA (1 << 6)
55#define TM1637_COMMAND_DISPLAY (2 << 6)
56#define TM1637_COMMAND_ADDRESS (3 << 6)
57
64static uint8_t a_high_low_shift(uint8_t data)
65{
66 uint8_t output;
67
68 output = (data << 4) | (data >> 4); /* part 1 */
69 output = ((output << 2) & 0xCC) | ((output >> 2) & 0x33); /* part 2 */
70 output = ((output << 1) & 0xAA) | ((output >> 1) & 0x55); /* part 3 */
71
72 return output; /* return output */
73}
74
86static uint8_t a_tm1637_write(tm1637_handle_t *handle, uint8_t cmd, uint8_t *buf, uint16_t len)
87{
88 uint8_t inner_buffer[16];
89 uint16_t i;
90
91 if (len > 16) /* check len */
92 {
93 return 1; /* return error */
94 }
95 for (i = 0; i < len; i++) /* loop all */
96 {
97 inner_buffer[i] = a_high_low_shift(buf[i]); /* shift */
98 }
99 if (handle->iic_write_cmd_custom(a_high_low_shift(cmd), inner_buffer, len) != 0) /* write data */
100 {
101 return 1; /* return error */
102 }
103
104 return 0; /* success return 0 */
105}
106
118static uint8_t a_tm1637_read(tm1637_handle_t *handle, uint8_t cmd, uint8_t *buf, uint16_t len)
119{
120 uint16_t i;
121
122 if (handle->iic_read_cmd_custom(a_high_low_shift(cmd), buf, len) != 0) /* read data */
123 {
124 return 1; /* return error */
125 }
126 for (i = 0; i < len; i++) /* loop all */
127 {
128 buf[i] = a_high_low_shift(buf[i]); /* shift */
129 }
130
131 return 0; /* success return 0 */
132}
133
145{
146 if (handle == NULL) /* check handle */
147 {
148 return 2; /* return error */
149 }
150 if (handle->debug_print == NULL) /* check debug_print */
151 {
152 return 3; /* return error */
153 }
154 if (handle->iic_init == NULL) /* check iic_init */
155 {
156 handle->debug_print("tm1637: iic_init is null.\n"); /* iic_init is null */
157
158 return 3; /* return error */
159 }
160 if (handle->iic_deinit == NULL) /* check iic_deinit */
161 {
162 handle->debug_print("tm1637: iic_deinit is null.\n"); /* iic_deinit is null */
163
164 return 3; /* return error */
165 }
166 if (handle->iic_write_cmd_custom == NULL) /* check iic_write_cmd_custom */
167 {
168 handle->debug_print("tm1637: iic_write_cmd_custom is null.\n"); /* iic_write_cmd_custom is null */
169
170 return 3; /* return error */
171 }
172 if (handle->iic_read_cmd_custom == NULL) /* check iic_read_cmd_custom */
173 {
174 handle->debug_print("tm1637: iic_read_cmd_custom is null.\n"); /* iic_read_cmd_custom is null */
175
176 return 3; /* return error */
177 }
178 if (handle->delay_ms == NULL) /* check delay_ms */
179 {
180 handle->debug_print("tm1637: delay_ms is null.\n"); /* delay_ms is null */
181
182 return 3; /* return error */
183 }
184
185 if (handle->iic_init() != 0) /* iic init */
186 {
187 handle->debug_print("tm1637: iic init failed.\n"); /* iic init failed */
188
189 return 4; /* return error */
190 }
191 handle->data_conf = 0x00; /* init 0 */
192 handle->display_conf = 0x00; /* init 0 */
193 handle->inited = 1; /* flag inited */
194
195 return 0; /* success return 0 */
196}
197
210{
211 uint8_t res;
212 uint8_t cmd;
213
214 if (handle == NULL) /* check handle */
215 {
216 return 2; /* return error */
217 }
218 if (handle->inited != 1) /* check handle initialization */
219 {
220 return 3; /* return error */
221 }
222
223 handle->display_conf &= ~(1 << 3); /* clear settings */
224 cmd = TM1637_COMMAND_DISPLAY | handle->display_conf; /* set the command */
225 if (a_tm1637_write(handle, cmd, NULL, 0) != 0) /* write the command */
226 {
227 handle->debug_print("tm1637: power down failed.\n"); /* power down failed */
228
229 return 4; /* return error */
230 }
231
232 res = handle->iic_deinit(); /* close iic */
233 if (res != 0) /* check the result */
234 {
235 handle->debug_print("tm1637: iic deinit failed.\n"); /* iic deinit failed */
236
237 return 1; /* return error */
238 }
239 handle->inited = 0; /* flag close */
240
241 return 0; /* success return 0 */
242}
243
256{
257 uint8_t cmd;
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 handle->display_conf &= ~(7 << 0); /* clear settings */
269 handle->display_conf |= width; /* set display conf */
270 cmd = TM1637_COMMAND_DISPLAY | handle->display_conf; /* set the command */
271 if (a_tm1637_write(handle, cmd, NULL, 0) != 0) /* write the command */
272 {
273 handle->debug_print("tm1637: write failed.\n"); /* write failed */
274
275 return 1; /* return error */
276 }
277
278 return 0; /* success return 0 */
279}
280
292{
293 if (handle == NULL) /* check handle */
294 {
295 return 2; /* return error */
296 }
297 if (handle->inited != 1) /* check handle initialization */
298 {
299 return 3; /* return error */
300 }
301
302 *width = (tm1637_pulse_width_t)(handle->display_conf & 0x7); /* get width */
303
304 return 0; /* success return 0 */
305}
306
319{
320 uint8_t cmd;
321
322 if (handle == NULL) /* check handle */
323 {
324 return 2; /* return error */
325 }
326 if (handle->inited != 1) /* check handle initialization */
327 {
328 return 3; /* return error */
329 }
330
331 handle->display_conf &= ~(1 << 3); /* clear settings */
332 handle->display_conf |= enable << 3; /* set display conf */
333 cmd = TM1637_COMMAND_DISPLAY | handle->display_conf; /* set the command */
334 if (a_tm1637_write(handle, cmd, NULL, 0) != 0) /* write the command */
335 {
336 handle->debug_print("tm1637: write failed.\n"); /* write failed */
337
338 return 1; /* return error */
339 }
340
341 return 0; /* success return 0 */
342}
343
355{
356 if (handle == NULL) /* check handle */
357 {
358 return 2; /* return error */
359 }
360 if (handle->inited != 1) /* check handle initialization */
361 {
362 return 3; /* return error */
363 }
364
365 *enable = (tm1637_bool_t)((handle->display_conf >> 3) & 0x01); /* get bool */
366
367 return 0; /* success return 0 */
368}
369
382{
383 uint8_t cmd;
384
385 if (handle == NULL) /* check handle */
386 {
387 return 2; /* return error */
388 }
389 if (handle->inited != 1) /* check handle initialization */
390 {
391 return 3; /* return error */
392 }
393
394 handle->data_conf &= ~(1 << 2); /* clear settings */
395 handle->data_conf |= mode << 2; /* set address mode */
396 cmd = TM1637_COMMAND_DATA | handle->data_conf; /* set the command */
397 if (a_tm1637_write(handle, cmd, NULL, 0) != 0) /* write the command */
398 {
399 handle->debug_print("tm1637: write failed.\n"); /* write failed */
400
401 return 1; /* return error */
402 }
403
404 return 0; /* success return 0 */
405}
406
418{
419 if (handle == NULL) /* check handle */
420 {
421 return 2; /* return error */
422 }
423 if (handle->inited != 1) /* check handle initialization */
424 {
425 return 3; /* return error */
426 }
427
428 *mode = (tm1637_address_mode_t)((handle->data_conf >> 2) & 0x01); /* get address mode */
429
430 return 0; /* success return 0 */
431}
432
445{
446 uint8_t cmd;
447
448 if (handle == NULL) /* check handle */
449 {
450 return 2; /* return error */
451 }
452 if (handle->inited != 1) /* check handle initialization */
453 {
454 return 3; /* return error */
455 }
456
457 handle->data_conf &= ~(1 << 3); /* clear settings */
458 handle->data_conf |= enable << 3; /* set test mode */
459 cmd = TM1637_COMMAND_DATA | handle->data_conf; /* set the command */
460 if (a_tm1637_write(handle, cmd, NULL, 0) != 0) /* write the command */
461 {
462 handle->debug_print("tm1637: write failed.\n"); /* write failed */
463
464 return 1; /* return error */
465 }
466
467 return 0; /* success return 0 */
468}
469
481{
482 if (handle == NULL) /* check handle */
483 {
484 return 2; /* return error */
485 }
486 if (handle->inited != 1) /* check handle initialization */
487 {
488 return 3; /* return error */
489 }
490
491 *enable = (tm1637_bool_t)((handle->data_conf >> 3) & 0x01); /* get test mode */
492
493 return 0; /* success return 0 */
494}
495
510uint8_t tm1637_write_segment(tm1637_handle_t *handle, uint8_t addr, uint8_t *data, uint8_t len)
511{
512 uint8_t i;
513 uint8_t cmd;
514
515 if (handle == NULL) /* check handle */
516 {
517 return 2; /* return error */
518 }
519 if (handle->inited != 1) /* check handle initialization */
520 {
521 return 3; /* return error */
522 }
523 if (addr + len > 6) /* check range */
524 {
525 handle->debug_print("tm1637: addr + len > 6.\n"); /* addr + len > 6 */
526
527 return 4; /* return error */
528 }
529
530 cmd = TM1637_COMMAND_DATA | handle->data_conf; /* set the command */
531 if (a_tm1637_write(handle, cmd, NULL, 0) != 0) /* write the command */
532 {
533 handle->debug_print("tm1637: write failed.\n"); /* write failed */
534
535 return 1; /* return error */
536 }
537 if ((handle->data_conf & (1 << 2)) != 0) /* fixed address mode */
538 {
539 for (i = 0; i < len; i++) /* loop all */
540 {
541 cmd = TM1637_COMMAND_ADDRESS | (addr + i); /* set the command */
542 if (a_tm1637_write(handle, cmd, &data[i], 1) != 0) /* write the command */
543 {
544 handle->debug_print("tm1637: write failed.\n"); /* write failed */
545
546 return 1; /* return error */
547 }
548 }
549 }
550 else /* auto increment 1 mode */
551 {
552 cmd = TM1637_COMMAND_ADDRESS | addr; /* set the command */
553 if (a_tm1637_write(handle, cmd, data, len) != 0) /* write the command */
554 {
555 handle->debug_print("tm1637: write failed.\n"); /* write failed */
556
557 return 1; /* return error */
558 }
559 }
560
561 return 0; /* success return 0 */
562}
563
575{
576 uint8_t i;
577 uint8_t cmd;
578 uint8_t data[6] = {0};
579
580 if (handle == NULL) /* check handle */
581 {
582 return 2; /* return error */
583 }
584 if (handle->inited != 1) /* check handle initialization */
585 {
586 return 3; /* return error */
587 }
588
589 cmd = TM1637_COMMAND_DATA | handle->data_conf; /* set the command */
590 if (a_tm1637_write(handle, cmd, NULL, 0) != 0) /* write the command */
591 {
592 handle->debug_print("tm1637: write failed.\n"); /* write failed */
593
594 return 1; /* return error */
595 }
596 if ((handle->data_conf & (1 << 2)) != 0) /* fixed address mode */
597 {
598 for (i = 0; i < 6; i++) /* loop all */
599 {
600 cmd = TM1637_COMMAND_ADDRESS | i; /* set the command */
601 if (a_tm1637_write(handle, cmd, data + i, 1) != 0) /* write the command */
602 {
603 handle->debug_print("tm1637: write failed.\n"); /* write failed */
604
605 return 1; /* return error */
606 }
607 }
608 }
609 else /* auto increment 1 mode */
610 {
611 cmd = TM1637_COMMAND_ADDRESS; /* set the command */
612 if (a_tm1637_write(handle, cmd, data, 6) != 0) /* write the command */
613 {
614 handle->debug_print("tm1637: write failed.\n"); /* write failed */
615
616 return 1; /* return error */
617 }
618 }
619
620 return 0; /* success return 0 */
621}
622
635uint8_t tm1637_read_segment(tm1637_handle_t *handle, uint8_t *seg, uint8_t *k)
636{
637 uint8_t cmd;
638 uint8_t data;
639
640 if (handle == NULL) /* check handle */
641 {
642 return 2; /* return error */
643 }
644 if (handle->inited != 1) /* check handle initialization */
645 {
646 return 3; /* return error */
647 }
648
649 cmd = TM1637_COMMAND_DATA | handle->data_conf | (1 << 1); /* set the command */
650 if (a_tm1637_read(handle, cmd, &data, 1) != 0) /* read the command */
651 {
652 handle->debug_print("tm1637: read failed.\n"); /* read failed */
653
654 return 1; /* return error */
655 }
656 *seg = data & 0x7; /* get seg */
657 *k = (data >> 3) & 0x03; /* get k */
658
659 return 0; /* success return 0 */
660}
661
675uint8_t tm1637_set_reg(tm1637_handle_t *handle, uint8_t cmd, uint8_t *data, uint8_t len)
676{
677 if (handle == NULL) /* check handle */
678 {
679 return 2; /* return error */
680 }
681 if (handle->inited != 1) /* check handle initialization */
682 {
683 return 3; /* return error */
684 }
685
686 if (a_tm1637_write(handle, cmd, data, len) != 0) /* write the command */
687 {
688 handle->debug_print("tm1637: write failed.\n"); /* write failed */
689
690 return 1; /* return error */
691 }
692
693 return 0; /* success return 0 */
694}
695
709uint8_t tm1637_get_reg(tm1637_handle_t *handle, uint8_t cmd, uint8_t *data, uint8_t len)
710{
711 if (handle == NULL) /* check handle */
712 {
713 return 2; /* return error */
714 }
715 if (handle->inited != 1) /* check handle initialization */
716 {
717 return 3; /* return error */
718 }
719
720 if (a_tm1637_read(handle, cmd, data, len) != 0) /* read the command */
721 {
722 handle->debug_print("tm1637: read failed.\n"); /* read failed */
723
724 return 1; /* return error */
725 }
726
727 return 0; /* success return 0 */
728}
729
739{
740 if (info == NULL) /* check handle */
741 {
742 return 2; /* return error */
743 }
744
745 memset(info, 0, sizeof(tm1637_info_t)); /* initialize tm1637 info structure */
746 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
747 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
748 strncpy(info->interface, "IIC", 8); /* copy interface name */
749 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
750 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
751 info->max_current_ma = MAX_CURRENT; /* set maximum current */
752 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
753 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
754 info->driver_version = DRIVER_VERSION; /* set driver version */
755
756 return 0; /* success return 0 */
757}
#define TM1637_COMMAND_DATA
chip command definition
#define TM1637_COMMAND_DISPLAY
#define MAX_CURRENT
#define SUPPLY_VOLTAGE_MAX
#define TM1637_COMMAND_ADDRESS
#define TEMPERATURE_MAX
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
driver tm1637 header file
uint8_t tm1637_read_segment(tm1637_handle_t *handle, uint8_t *seg, uint8_t *k)
read segment
uint8_t tm1637_set_address_mode(tm1637_handle_t *handle, tm1637_address_mode_t mode)
set address mode
uint8_t tm1637_info(tm1637_info_t *info)
get chip's information
uint8_t tm1637_deinit(tm1637_handle_t *handle)
close the chip
tm1637_address_mode_t
tm1637 address mode enumeration definition
tm1637_pulse_width_t
tm1637 pulse width enumeration definition
uint8_t tm1637_get_display(tm1637_handle_t *handle, tm1637_bool_t *enable)
get display status
tm1637_bool_t
tm1637 bool enumeration definition
uint8_t tm1637_clear_segment(tm1637_handle_t *handle)
clear segment
uint8_t tm1637_get_test_mode(tm1637_handle_t *handle, tm1637_bool_t *enable)
get test mode status
uint8_t tm1637_get_pulse_width(tm1637_handle_t *handle, tm1637_pulse_width_t *width)
get pulse width
struct tm1637_info_s tm1637_info_t
tm1637 information structure definition
uint8_t tm1637_set_test_mode(tm1637_handle_t *handle, tm1637_bool_t enable)
enable or disable test mode
struct tm1637_handle_s tm1637_handle_t
tm1637 handle structure definition
uint8_t tm1637_write_segment(tm1637_handle_t *handle, uint8_t addr, uint8_t *data, uint8_t len)
write segment
uint8_t tm1637_get_address_mode(tm1637_handle_t *handle, tm1637_address_mode_t *mode)
get address mode
uint8_t tm1637_set_pulse_width(tm1637_handle_t *handle, tm1637_pulse_width_t width)
set pulse width
uint8_t tm1637_set_display(tm1637_handle_t *handle, tm1637_bool_t enable)
enable or disable display
uint8_t tm1637_init(tm1637_handle_t *handle)
initialize the chip
uint8_t tm1637_get_reg(tm1637_handle_t *handle, uint8_t cmd, uint8_t *data, uint8_t len)
get the chip register
uint8_t tm1637_set_reg(tm1637_handle_t *handle, uint8_t cmd, uint8_t *data, uint8_t len)
set the chip register
uint8_t(* iic_write_cmd_custom)(uint8_t addr, uint8_t *buf, uint16_t len)
uint8_t(* iic_read_cmd_custom)(uint8_t addr, uint8_t *buf, uint16_t len)
void(* delay_ms)(uint32_t ms)
void(* debug_print)(const char *const fmt,...)
uint8_t(* iic_init)(void)
uint8_t(* iic_deinit)(void)
float supply_voltage_max_v
uint32_t driver_version
char manufacturer_name[32]
float supply_voltage_min_v
char chip_name[32]