LibDriver TM1640
Loading...
Searching...
No Matches
driver_tm1640.c
Go to the documentation of this file.
1
36
37#include "driver_tm1640.h"
38
42#define CHIP_NAME "Titan Micro Electronics TM1640"
43#define MANUFACTURER_NAME "Titan Micro Electronics"
44#define SUPPLY_VOLTAGE_MIN 3.0f
45#define SUPPLY_VOLTAGE_MAX 5.5f
46#define MAX_CURRENT 200.0f
47#define TEMPERATURE_MIN -40.0f
48#define TEMPERATURE_MAX 85.0f
49#define DRIVER_VERSION 1000
50
54#define TM1640_COMMAND_DATA (1 << 6)
55#define TM1640_COMMAND_DISPLAY (2 << 6)
56#define TM1640_COMMAND_ADDRESS (3 << 6)
57
66static uint8_t a_tm1640_start(tm1640_handle_t *handle)
67{
68 if (handle->din_gpio_write(1) != 0) /* din set high */
69 {
70 return 1; /* return error */
71 }
72 if (handle->sclk_gpio_write(1) != 0) /* sclk set high */
73 {
74 return 1; /* return error */
75 }
76 handle->delay_us(TM1640_COMMAND_DATA_DELAY); /* delay */
77 if (handle->din_gpio_write(0) != 0) /* din set low */
78 {
79 return 1; /* return error */
80 }
81 handle->delay_us(TM1640_COMMAND_DATA_DELAY); /* delay */
82 if (handle->sclk_gpio_write(0) != 0) /* sclk set low */
83 {
84 return 1; /* return error */
85 }
86 handle->delay_us(TM1640_COMMAND_DATA_DELAY); /* delay */
87
88 return 0; /* success return 0 */
89}
90
99static uint8_t a_tm1640_stop(tm1640_handle_t *handle)
100{
101 if (handle->din_gpio_write(0) != 0) /* din set low */
102 {
103 return 1; /* return error */
104 }
105 if (handle->sclk_gpio_write(1) != 0) /* sclk set high */
106 {
107 return 1; /* return error */
108 }
109 handle->delay_us(TM1640_COMMAND_DATA_DELAY); /* delay */
110 if (handle->din_gpio_write(1) != 0) /* din set high */
111 {
112 return 1; /* return error */
113 }
114 handle->delay_us(TM1640_COMMAND_DATA_DELAY); /* delay */
115
116 return 0; /* success return 0 */
117}
118
128static uint8_t a_tm1640_byte(tm1640_handle_t *handle, uint8_t data)
129{
130 uint8_t i;
131 uint8_t temp;
132
133 temp = data; /* set data */
134 if (handle->din_gpio_write(0) != 0) /* din set low */
135 {
136 return 1; /* return error */
137 }
138 if (handle->sclk_gpio_write(0) != 0) /* sclk set low */
139 {
140 return 1; /* return error */
141 }
142
143 for (i = 0; i < 8; i++) /* write 8 bits */
144 {
145 if (handle->sclk_gpio_write(0) != 0) /* sclk set low */
146 {
147 return 1; /* return error */
148 }
149 handle->delay_us(TM1640_COMMAND_DATA_DELAY); /* delay */
150 if ((temp & 0x1) != 0) /* set lsb */
151 {
152 if (handle->din_gpio_write(1) != 0) /* din set high */
153 {
154 return 1; /* return error */
155 }
156 handle->delay_us(TM1640_COMMAND_DATA_DELAY); /* delay */
157 }
158 else
159 {
160 if (handle->din_gpio_write(0) != 0) /* din set low */
161 {
162 return 1; /* return error */
163 }
164 handle->delay_us(TM1640_COMMAND_DATA_DELAY); /* delay */
165 }
166 if (handle->sclk_gpio_write(1) != 0) /* sclk set high */
167 {
168 return 1; /* return error */
169 }
170 handle->delay_us(TM1640_COMMAND_DATA_DELAY); /* delay */
171 temp >>= 1; /* right shift 1 */
172 }
173
174 if (handle->din_gpio_write(0) != 0) /* din set low */
175 {
176 return 1; /* return error */
177 }
178 if (handle->sclk_gpio_write(0) != 0) /* sclk set low */
179 {
180 return 1; /* return error */
181 }
182
183 return 0; /* success return 0 */
184}
185
197static uint8_t a_tm1640_write(tm1640_handle_t *handle, uint8_t cmd, uint8_t *buf, uint16_t len)
198{
199 uint8_t res;
200 uint16_t i;
201
202 res = a_tm1640_start(handle); /* start */
203 if (res != 0) /* check result */
204 {
205 return 1; /* return error */
206 }
207
208 res = a_tm1640_byte(handle, cmd); /* write command */
209 if (res != 0) /* check result */
210 {
211 return 1; /* return error */
212 }
213
214 for (i = 0; i < len; i++) /* write all */
215 {
216 res = a_tm1640_byte(handle, buf[i]); /* write data */
217 if (res != 0) /* check result */
218 {
219 return 1; /* return error */
220 }
221 }
222
223 res = a_tm1640_stop(handle); /* stop */
224 if (res != 0) /* check result */
225 {
226 return 1; /* return error */
227 }
228
229 return 0; /* success return 0 */
230}
231
243{
244 uint8_t res;
245 uint8_t cmd;
246
247 if (handle == NULL) /* check handle */
248 {
249 return 2; /* return error */
250 }
251 if (handle->debug_print == NULL) /* check debug_print */
252 {
253 return 3; /* return error */
254 }
255 if (handle->sclk_gpio_init == NULL) /* check sclk_gpio_init */
256 {
257 handle->debug_print("tm1640: sclk_gpio_init is null.\n"); /* sclk_gpio_init is null */
258
259 return 3; /* return error */
260 }
261 if (handle->sclk_gpio_deinit == NULL) /* check sclk_gpio_deinit */
262 {
263 handle->debug_print("tm1640: sclk_gpio_deinit is null.\n"); /* sclk_gpio_deinit is null */
264
265 return 3; /* return error */
266 }
267 if (handle->sclk_gpio_write == NULL) /* check sclk_gpio_write */
268 {
269 handle->debug_print("tm1640: sclk_gpio_write is null.\n"); /* sclk_gpio_write is null */
270
271 return 3; /* return error */
272 }
273 if (handle->din_gpio_init == NULL) /* check din_gpio_init */
274 {
275 handle->debug_print("tm1640: din_gpio_init is null.\n"); /* din_gpio_init is null */
276
277 return 3; /* return error */
278 }
279 if (handle->din_gpio_deinit == NULL) /* check din_gpio_deinit */
280 {
281 handle->debug_print("tm1640: din_gpio_deinit is null.\n"); /* din_gpio_deinit is null */
282
283 return 3; /* return error */
284 }
285 if (handle->din_gpio_write == NULL) /* check din_gpio_write */
286 {
287 handle->debug_print("tm1640: din_gpio_write is null.\n"); /* din_gpio_write is null */
288
289 return 3; /* return error */
290 }
291 if (handle->delay_us == NULL) /* check delay_us */
292 {
293 handle->debug_print("tm1640: delay_us is null.\n"); /* delay_us is null */
294
295 return 3; /* return error */
296 }
297 if (handle->delay_ms == NULL) /* check delay_ms */
298 {
299 handle->debug_print("tm1640: delay_ms is null.\n"); /* delay_ms is null */
300
301 return 3; /* return error */
302 }
303
304 res = handle->sclk_gpio_init(); /* sclk gpio init */
305 if (res != 0) /* check result */
306 {
307 handle->debug_print("tm1640: sclk gpio init failed.\n"); /* sclk gpio init failed */
308
309 return 1; /* return error */
310 }
311 res = handle->din_gpio_init(); /* din gpio init */
312 if (res != 0) /* check result */
313 {
314 handle->debug_print("tm1640: din gpio init failed.\n"); /* din gpio init failed */
315 (void)handle->sclk_gpio_deinit(); /* sclk gpio deinit */
316
317 return 1; /* return error */
318 }
319 res = handle->sclk_gpio_write(1); /* set high */
320 if (res != 0) /* check result */
321 {
322 handle->debug_print("tm1640: sclk gpio write failed.\n"); /* sclk gpio write failed */
323 (void)handle->sclk_gpio_deinit(); /* sclk gpio deinit */
324 (void)handle->din_gpio_deinit(); /* din gpio deinit */
325
326 return 1; /* return error */
327 }
328 res = handle->din_gpio_write(1); /* set high */
329 if (res != 0) /* check result */
330 {
331 handle->debug_print("tm1640: din gpio write failed.\n"); /* din gpio write failed */
332 (void)handle->sclk_gpio_deinit(); /* sclk gpio deinit */
333 (void)handle->din_gpio_deinit(); /* din gpio deinit */
334
335 return 1; /* return error */
336 }
337 handle->data_conf = 0x00; /* init 0 */
338 handle->display_conf = 0x00; /* init 0 */
339 cmd = TM1640_COMMAND_DISPLAY | handle->display_conf; /* set the command */
340 res = a_tm1640_write(handle, cmd, NULL, 0); /* write */
341 if (res != 0) /* check error */
342 {
343 handle->debug_print("tm1640: write failed.\n"); /* write failed */
344 (void)handle->sclk_gpio_deinit(); /* sclk gpio deinit */
345 (void)handle->din_gpio_deinit(); /* din gpio deinit */
346
347 return 1; /* return error */
348 }
349 handle->inited = 1; /* flag inited */
350
351 return 0; /* success return 0 */
352}
353
366{
367 uint8_t res;
368 uint8_t cmd;
369
370 if (handle == NULL) /* check handle */
371 {
372 return 2; /* return error */
373 }
374 if (handle->inited != 1) /* check handle initialization */
375 {
376 return 3; /* return error */
377 }
378
379 handle->display_conf &= ~(1 << 3); /* clear settings */
380 cmd = TM1640_COMMAND_DISPLAY | handle->display_conf; /* set the command */
381 res = a_tm1640_write(handle, cmd, NULL, 0); /* write */
382 if (res != 0) /* check error */
383 {
384 handle->debug_print("tm1640: write failed.\n"); /* write failed */
385
386 return 4; /* return error */
387 }
388
389 res = handle->sclk_gpio_deinit(); /* sclk gpio deinit */
390 if (res != 0) /* check the result */
391 {
392 handle->debug_print("tm1640: sclk gpio deinit failed.\n"); /* sclk gpio deinit failed */
393
394 return 1; /* return error */
395 }
396 res = handle->din_gpio_deinit(); /* din gpio deinit */
397 if (res != 0) /* check the result */
398 {
399 handle->debug_print("tm1640: din gpio deinit failed.\n"); /* din gpio deinit failed */
400
401 return 1; /* return error */
402 }
403 handle->inited = 0; /* flag close */
404
405 return 0; /* success return 0 */
406}
407
420{
421 uint8_t res;
422 uint8_t cmd;
423
424 if (handle == NULL) /* check handle */
425 {
426 return 2; /* return error */
427 }
428 if (handle->inited != 1) /* check handle initialization */
429 {
430 return 3; /* return error */
431 }
432
433 handle->display_conf &= ~(7 << 0); /* clear settings */
434 handle->display_conf |= width; /* set display conf */
435 cmd = TM1640_COMMAND_DISPLAY | handle->display_conf; /* set the command */
436 res = a_tm1640_write(handle, cmd, NULL, 0); /* write */
437 if (res != 0) /* check error */
438 {
439 handle->debug_print("tm1640: write failed.\n"); /* write failed */
440
441 return 1; /* return error */
442 }
443
444 return 0; /* success return 0 */
445}
446
458{
459 if (handle == NULL) /* check handle */
460 {
461 return 2; /* return error */
462 }
463 if (handle->inited != 1) /* check handle initialization */
464 {
465 return 3; /* return error */
466 }
467
468 *width = (tm1640_pulse_width_t)(handle->display_conf & 0x7); /* get width */
469
470 return 0; /* success return 0 */
471}
472
485{
486 uint8_t res;
487 uint8_t cmd;
488
489 if (handle == NULL) /* check handle */
490 {
491 return 2; /* return error */
492 }
493 if (handle->inited != 1) /* check handle initialization */
494 {
495 return 3; /* return error */
496 }
497
498 handle->display_conf &= ~(1 << 3); /* clear settings */
499 handle->display_conf |= enable << 3; /* set display conf */
500 cmd = TM1640_COMMAND_DISPLAY | handle->display_conf; /* set the command */
501 res = a_tm1640_write(handle, cmd, NULL, 0); /* write */
502 if (res != 0) /* check error */
503 {
504 handle->debug_print("tm1640: write failed.\n"); /* write failed */
505
506 return 1; /* return error */
507 }
508
509 return 0; /* success return 0 */
510}
511
523{
524 if (handle == NULL) /* check handle */
525 {
526 return 2; /* return error */
527 }
528 if (handle->inited != 1) /* check handle initialization */
529 {
530 return 3; /* return error */
531 }
532
533 *enable = (tm1640_bool_t)((handle->display_conf >> 3) & 0x01); /* get bool */
534
535 return 0; /* success return 0 */
536}
537
550{
551 uint8_t res;
552 uint8_t cmd;
553
554 if (handle == NULL) /* check handle */
555 {
556 return 2; /* return error */
557 }
558 if (handle->inited != 1) /* check handle initialization */
559 {
560 return 3; /* return error */
561 }
562
563 handle->data_conf &= ~(1 << 2); /* clear settings */
564 handle->data_conf |= mode << 2; /* set address mode */
565 cmd = TM1640_COMMAND_DATA | handle->data_conf; /* set the command */
566 res = a_tm1640_write(handle, cmd, NULL, 0); /* write */
567 if (res != 0) /* check error */
568 {
569 handle->debug_print("tm1640: write failed.\n"); /* write failed */
570
571 return 1; /* return error */
572 }
573
574 return 0; /* success return 0 */
575}
576
588{
589 if (handle == NULL) /* check handle */
590 {
591 return 2; /* return error */
592 }
593 if (handle->inited != 1) /* check handle initialization */
594 {
595 return 3; /* return error */
596 }
597
598 *mode = (tm1640_address_mode_t)((handle->data_conf >> 2) & 0x01); /* get address mode */
599
600 return 0; /* success return 0 */
601}
602
615{
616 uint8_t res;
617 uint8_t cmd;
618
619 if (handle == NULL) /* check handle */
620 {
621 return 2; /* return error */
622 }
623 if (handle->inited != 1) /* check handle initialization */
624 {
625 return 3; /* return error */
626 }
627
628 handle->data_conf &= ~(1 << 3); /* clear settings */
629 handle->data_conf |= enable << 3; /* set test mode */
630 cmd = TM1640_COMMAND_DATA | handle->data_conf; /* set the command */
631 res = a_tm1640_write(handle, cmd, NULL, 0); /* write */
632 if (res != 0) /* check error */
633 {
634 handle->debug_print("tm1640: write failed.\n"); /* write failed */
635
636 return 1; /* return error */
637 }
638
639 return 0; /* success return 0 */
640}
641
653{
654 if (handle == NULL) /* check handle */
655 {
656 return 2; /* return error */
657 }
658 if (handle->inited != 1) /* check handle initialization */
659 {
660 return 3; /* return error */
661 }
662
663 *enable = (tm1640_bool_t)((handle->data_conf >> 3) & 0x01); /* get test mode */
664
665 return 0; /* success return 0 */
666}
667
679{
680 uint8_t res;
681 uint8_t i;
682 uint8_t cmd;
683 uint8_t data[16] = {0};
684
685 if (handle == NULL) /* check handle */
686 {
687 return 2; /* return error */
688 }
689 if (handle->inited != 1) /* check handle initialization */
690 {
691 return 3; /* return error */
692 }
693
694 cmd = TM1640_COMMAND_DATA | handle->data_conf; /* set the command */
695 res = a_tm1640_write(handle, cmd, NULL, 0); /* write */
696 if (res != 0) /* check error */
697 {
698 handle->debug_print("tm1640: write failed.\n"); /* write failed */
699
700 return 1; /* return error */
701 }
702
703 if ((handle->data_conf & (1 << 2)) != 0) /* fixed address mode */
704 {
705 for (i = 0; i < 16; i++) /* loop all */
706 {
707 uint8_t temp;
708
709 cmd = TM1640_COMMAND_ADDRESS | i; /* set the command */
710 temp = data[i]; /* set lsb */
711 res = a_tm1640_write(handle, cmd, &temp, 1); /* write */
712 if (res != 0) /* check error */
713 {
714 handle->debug_print("tm1640: write failed.\n"); /* write failed */
715
716 return 1; /* return error */
717 }
718 }
719 }
720 else /* auto increment 1 mode */
721 {
722 cmd = TM1640_COMMAND_ADDRESS; /* set the command */
723 res = a_tm1640_write(handle, cmd, data, 16); /* write */
724 if (res != 0) /* check error */
725 {
726 handle->debug_print("tm1640: write failed.\n"); /* write failed */
727
728 return 1; /* return error */
729 }
730 }
731
732 return 0; /* success return 0 */
733}
734
749uint8_t tm1640_write_segment(tm1640_handle_t *handle, uint8_t addr, uint8_t *data, uint8_t len)
750{
751 uint8_t res;
752 uint8_t i;
753 uint8_t cmd;
754
755 if (handle == NULL) /* check handle */
756 {
757 return 2; /* return error */
758 }
759 if (handle->inited != 1) /* check handle initialization */
760 {
761 return 3; /* return error */
762 }
763 if (addr + len > 16) /* check range */
764 {
765 handle->debug_print("tm1640: addr + len > 16.\n"); /* addr + len > 16 */
766
767 return 4; /* return error */
768 }
769
770 cmd = TM1640_COMMAND_DATA | handle->data_conf; /* set the command */
771 res = a_tm1640_write(handle, cmd, NULL, 0); /* write */
772 if (res != 0) /* check error */
773 {
774 handle->debug_print("tm1640: write failed.\n"); /* write failed */
775
776 return 1; /* return error */
777 }
778
779 if ((handle->data_conf & (1 << 2)) != 0) /* fixed address mode */
780 {
781 for (i = 0; i < len; i++) /* loop all */
782 {
783 uint8_t temp;
784
785 cmd = TM1640_COMMAND_ADDRESS | (addr + i); /* set the command */
786 temp = data[i]; /* set data */
787 res = a_tm1640_write(handle, cmd, &temp, 1); /* write */
788 if (res != 0) /* check error */
789 {
790 handle->debug_print("tm1640: write failed.\n"); /* write failed */
791
792 return 1; /* return error */
793 }
794 }
795 }
796 else /* auto increment 1 mode */
797 {
798 cmd = TM1640_COMMAND_ADDRESS | addr; /* set the command */
799 res = a_tm1640_write(handle, cmd, data, len); /* write */
800 if (res != 0) /* check error */
801 {
802 handle->debug_print("tm1640: write failed.\n"); /* write failed */
803
804 return 1; /* return error */
805 }
806 }
807
808 return 0; /* success return 0 */
809}
810
824uint8_t tm1640_set_reg(tm1640_handle_t *handle, uint8_t cmd, uint8_t *data, uint8_t len)
825{
826 uint8_t res;
827
828 if (handle == NULL) /* check handle */
829 {
830 return 2; /* return error */
831 }
832 if (handle->inited != 1) /* check handle initialization */
833 {
834 return 3; /* return error */
835 }
836
837 res = a_tm1640_write(handle, cmd, data, len); /* write */
838 if (res != 0) /* check error */
839 {
840 handle->debug_print("tm1640: write failed.\n"); /* write failed */
841
842 return 1; /* return error */
843 }
844
845 return 0; /* success return 0 */
846}
847
857{
858 if (info == NULL) /* check handle */
859 {
860 return 2; /* return error */
861 }
862
863 memset(info, 0, sizeof(tm1640_info_t)); /* initialize tm1640 info structure */
864 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
865 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
866 strncpy(info->interface, "GPIO", 8); /* copy interface name */
867 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
868 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
869 info->max_current_ma = MAX_CURRENT; /* set maximum current */
870 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
871 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
872 info->driver_version = DRIVER_VERSION; /* set driver version */
873
874 return 0; /* success return 0 */
875}
#define MAX_CURRENT
#define TM1640_COMMAND_ADDRESS
#define SUPPLY_VOLTAGE_MAX
#define TEMPERATURE_MAX
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define TM1640_COMMAND_DISPLAY
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
#define TM1640_COMMAND_DATA
chip command definition
driver tm1640 header file
uint8_t tm1640_deinit(tm1640_handle_t *handle)
close the chip
uint8_t tm1640_info(tm1640_info_t *info)
get chip's information
tm1640_pulse_width_t
tm1640 pulse width enumeration definition
#define TM1640_COMMAND_DATA_DELAY
tm1640 command data delay definition
uint8_t tm1640_get_address_mode(tm1640_handle_t *handle, tm1640_address_mode_t *mode)
get address mode
uint8_t tm1640_get_test_mode(tm1640_handle_t *handle, tm1640_bool_t *enable)
get test mode status
tm1640_bool_t
tm1640 bool enumeration definition
uint8_t tm1640_init(tm1640_handle_t *handle)
initialize the chip
uint8_t tm1640_set_pulse_width(tm1640_handle_t *handle, tm1640_pulse_width_t width)
set pulse width
tm1640_address_mode_t
tm1640 address mode enumeration definition
uint8_t tm1640_clear_segment(tm1640_handle_t *handle)
clear segment
struct tm1640_handle_s tm1640_handle_t
tm1640 handle structure definition
uint8_t tm1640_write_segment(tm1640_handle_t *handle, uint8_t addr, uint8_t *data, uint8_t len)
write segment
uint8_t tm1640_set_test_mode(tm1640_handle_t *handle, tm1640_bool_t enable)
enable or disable test mode
uint8_t tm1640_get_pulse_width(tm1640_handle_t *handle, tm1640_pulse_width_t *width)
get pulse width
uint8_t tm1640_set_address_mode(tm1640_handle_t *handle, tm1640_address_mode_t mode)
set address mode
uint8_t tm1640_get_display(tm1640_handle_t *handle, tm1640_bool_t *enable)
get display status
struct tm1640_info_s tm1640_info_t
tm1640 information structure definition
uint8_t tm1640_set_display(tm1640_handle_t *handle, tm1640_bool_t enable)
enable or disable display
uint8_t tm1640_set_reg(tm1640_handle_t *handle, uint8_t cmd, uint8_t *data, uint8_t len)
set the chip register
void(* delay_ms)(uint32_t ms)
uint8_t(* sclk_gpio_deinit)(void)
void(* debug_print)(const char *const fmt,...)
uint8_t(* din_gpio_init)(void)
uint8_t(* din_gpio_deinit)(void)
void(* delay_us)(uint32_t us)
uint8_t(* din_gpio_write)(uint8_t level)
uint8_t(* sclk_gpio_init)(void)
uint8_t(* sclk_gpio_write)(uint8_t level)
float supply_voltage_max_v
uint32_t driver_version
char manufacturer_name[32]
float supply_voltage_min_v
char chip_name[32]