LibDriver TM1622
Loading...
Searching...
No Matches
driver_tm1622.c
Go to the documentation of this file.
1
36
37#include "driver_tm1622.h"
38
42#define CHIP_NAME "Titan Micro Electronics TM1622"
43#define MANUFACTURER_NAME "Titan Micro Electronics"
44#define SUPPLY_VOLTAGE_MIN 2.7f
45#define SUPPLY_VOLTAGE_MAX 5.2f
46#define MAX_CURRENT 4.5f
47#define TEMPERATURE_MIN -20.0f
48#define TEMPERATURE_MAX 85.0f
49#define DRIVER_VERSION 1000
50
54#define TM1622_COMMAND_SYS_DIS 0x00
55#define TM1622_COMMAND_SYS_EN 0x01
56#define TM1622_COMMAND_LCD_OFF 0x02
57#define TM1622_COMMAND_LCD_ON 0x03
58#define TM1622_COMMAND_TIMER_DIS 0x04
59#define TM1622_COMMAND_WDT_DIS 0x05
60#define TM1622_COMMAND_TIMER_EN 0x06
61#define TM1622_COMMAND_WDT_EN 0x07
62#define TM1622_COMMAND_TONE_OFF 0x08
63#define TM1622_COMMAND_TONE_ON 0x09
64#define TM1622_COMMAND_CLR_TIMER 0x0D
65#define TM1622_COMMAND_CLR_WDT 0x0F
66#define TM1622_COMMAND_RC_32K 0x18
67#define TM1622_COMMAND_EXT_32K 0x1C
68#define TM1622_COMMAND_TONE_4K 0x40
69#define TM1622_COMMAND_TONE_2K 0x60
70#define TM1622_COMMAND_IRQ_DIS 0x80
71#define TM1622_COMMAND_IRQ_EN 0x88
72#define TM1622_COMMAND_F1 0xA0
73#define TM1622_COMMAND_F2 0xA1
74#define TM1622_COMMAND_F4 0xA2
75#define TM1622_COMMAND_F8 0xA3
76#define TM1622_COMMAND_F16 0xA4
77#define TM1622_COMMAND_F32 0xA5
78#define TM1622_COMMAND_F64 0xA6
79#define TM1622_COMMAND_F128 0xA7
80#define TM1622_COMMAND_TOPT 0xE0
81#define TM1622_COMMAND_TNORMAL 0xE3
82
93static uint8_t a_tm1622_write_bits(tm1622_handle_t *handle, uint32_t data, uint8_t len)
94{
95 uint8_t res;
96 uint8_t i;
97
98 for (i = 0; i < len; i++) /* write bits */
99 {
100 res = handle->wr_gpio_write(0); /* set low */
101 if (res != 0) /* check the result */
102 {
103 handle->debug_print("tm1622: wr gpio write failed.\n"); /* wr gpio write failed */
104
105 return 1; /* return error */
106 }
107 handle->delay_us(TM1622_COMMAND_DATA_DELAY); /* delay */
108 if ((data & (1 << (len - 1 - i))) != 0) /* check bit */
109 {
110 res = handle->data_gpio_write(1); /* set high */
111 if (res != 0) /* check the result */
112 {
113 handle->debug_print("tm1622: data gpio write failed.\n"); /* data gpio write failed */
114
115 return 1; /* return error */
116 }
117 }
118 else
119 {
120 res = handle->data_gpio_write(0); /* set low */
121 if (res != 0) /* check the result */
122 {
123 handle->debug_print("tm1622: data gpio write failed.\n"); /* data gpio write failed */
124
125 return 1; /* return error */
126 }
127 }
128 handle->delay_us(TM1622_COMMAND_DATA_DELAY); /* delay */
129 res = handle->wr_gpio_write(1); /* set high */
130 if (res != 0) /* check the result */
131 {
132 handle->debug_print("tm1622: wr gpio write failed.\n"); /* wr gpio write failed */
133
134 return 1; /* return error */
135 }
136 handle->delay_us(TM1622_COMMAND_DATA_DELAY); /* delay */
137 }
138
139 return 0; /* success return 0 */
140}
141
152static uint8_t a_tm1622_read_bits(tm1622_handle_t *handle, uint32_t *data, uint8_t len)
153{
154 uint8_t res;
155 uint8_t level;
156 uint8_t i;
157
158 *data = 0;
159 for (i = 0; i < len; i++) /* read bits */
160 {
161 res = handle->rd_gpio_write(0); /* set low */
162 if (res != 0) /* check the result */
163 {
164 handle->debug_print("tm1622: rd gpio write failed.\n"); /* rd gpio write failed */
165
166 return 1; /* return error */
167 }
168 handle->delay_us(TM1622_COMMAND_DATA_DELAY); /* delay */
169 res = handle->data_gpio_read(&level); /* read level */
170 if (res != 0) /* check the result */
171 {
172 handle->debug_print("tm1622: data gpio read failed.\n"); /* data gpio read failed */
173
174 return 1; /* return error */
175 }
176 *data <<= 1; /* left shift */
177 *data |= level; /* set bit */
178 handle->delay_us(TM1622_COMMAND_DATA_DELAY); /* delay */
179 res = handle->rd_gpio_write(1); /* set high */
180 if (res != 0) /* check the result */
181 {
182 handle->debug_print("tm1622: rd gpio write failed.\n"); /* rd gpio write failed */
183
184 return 1; /* return error */
185 }
186 handle->delay_us(TM1622_COMMAND_DATA_DELAY); /* delay */
187 }
188
189 return 0; /* success return 0 */
190}
191
201static uint8_t a_tm1622_write_command(tm1622_handle_t *handle, uint16_t cmd)
202{
203 uint8_t res;
204
205 res = handle->cs_gpio_write(0); /* set low */
206 if (res != 0) /* check the result */
207 {
208 handle->debug_print("tm1622: cs gpio write failed.\n"); /* cs gpio write failed */
209
210 return 1; /* return error */
211 }
212 handle->delay_us(TM1622_COMMAND_DATA_DELAY); /* delay */
213 res = a_tm1622_write_bits(handle, (uint32_t)(0x04), 3); /* write bits */
214 if (res != 0) /* check the result */
215 {
216 return 1; /* return error */
217 }
218 res = a_tm1622_write_bits(handle, (uint32_t)(cmd << 1), 9); /* write bits */
219 if (res != 0) /* check the result */
220 {
221 return 1; /* return error */
222 }
223 res = handle->cs_gpio_write(1); /* set high */
224 if (res != 0) /* check the result */
225 {
226 handle->debug_print("tm1622: cs gpio write failed.\n"); /* cs gpio write failed */
227
228 return 1; /* return error */
229 }
230
231 return 0; /* success return 0 */
232}
233
245static uint8_t a_tm1622_write_ram(tm1622_handle_t *handle, uint8_t addr, uint8_t *data, uint8_t len)
246{
247 uint8_t res;
248 uint8_t i;
249
250 res = handle->cs_gpio_write(0); /* set low */
251 if (res != 0) /* check the result */
252 {
253 handle->debug_print("tm1622: cs gpio write failed.\n"); /* cs gpio write failed */
254
255 return 1; /* return error */
256 }
257 handle->delay_us(TM1622_COMMAND_DATA_DELAY); /* delay */
258 res = a_tm1622_write_bits(handle, (uint32_t)(0x05), 3); /* write bits */
259 if (res != 0) /* check the result */
260 {
261 return 1; /* return error */
262 }
263 res = a_tm1622_write_bits(handle, (uint32_t)(addr & 0x3F), 6); /* write bits */
264 if (res != 0) /* check the result */
265 {
266 return 1; /* return error */
267 }
268
269 for (i = 0; i < len; i++) /* loop */
270 {
271 res = a_tm1622_write_bits(handle, (uint32_t)((data[i] >> 0) & 0x0F), 4); /* write bits */
272 if (res != 0) /* check the result */
273 {
274 return 1; /* return error */
275 }
276 res = a_tm1622_write_bits(handle, (uint32_t)((data[i] >> 4) & 0x0F), 4); /* write bits */
277 if (res != 0) /* check the result */
278 {
279 return 1; /* return error */
280 }
281 }
282 res = handle->cs_gpio_write(1); /* set high */
283 if (res != 0) /* check the result */
284 {
285 handle->debug_print("tm1622: cs gpio write failed.\n"); /* cs gpio write failed */
286
287 return 1; /* return error */
288 }
289
290 return 0; /* success return 0 */
291}
292
304static uint8_t a_tm1622_read_ram(tm1622_handle_t *handle, uint8_t addr, uint8_t *data, uint8_t len)
305{
306 uint8_t res;
307 uint8_t i;
308
309 res = handle->cs_gpio_write(0); /* set low */
310 if (res != 0) /* check the result */
311 {
312 handle->debug_print("tm1622: cs gpio write failed.\n"); /* cs gpio write failed */
313
314 return 1; /* return error */
315 }
316 handle->delay_us(TM1622_COMMAND_DATA_DELAY); /* delay */
317 res = a_tm1622_write_bits(handle, (uint32_t)(0x06), 3); /* write bits */
318 if (res != 0) /* check the result */
319 {
320 return 1; /* return error */
321 }
322 res = a_tm1622_write_bits(handle, (uint32_t)(addr & 0x3F), 6); /* write bits */
323 if (res != 0) /* check the result */
324 {
325 return 1; /* return error */
326 }
327
328 for (i = 0; i < len; i++) /* loop */
329 {
330 uint32_t output = 0;
331
332 res = a_tm1622_read_bits(handle, &output, 4); /* read bits */
333 if (res != 0) /* check the result */
334 {
335 return 1; /* return error */
336 }
337 data[i] = (uint8_t)(output & 0xF); /* set data */
338 res = a_tm1622_read_bits(handle, &output, 4); /* read bits */
339 if (res != 0) /* check the result */
340 {
341 return 1; /* return error */
342 }
343 data[i] |= (uint8_t)(output & 0xF) << 4; /* set data */
344 }
345 res = handle->cs_gpio_write(1); /* set high */
346 if (res != 0) /* check the result */
347 {
348 handle->debug_print("tm1622: cs gpio write failed.\n"); /* cs gpio write failed */
349
350 return 1; /* return error */
351 }
352
353 return 0; /* success return 0 */
354}
355
367static uint8_t a_tm1622_read_modify_write(tm1622_handle_t *handle, uint8_t addr,
368 void (*and_or)(uint8_t addr, uint8_t lsb_msb, uint8_t input, uint8_t *output), uint8_t len)
369{
370 uint8_t res;
371 uint8_t i;
372 uint32_t data;
373
374 res = handle->cs_gpio_write(0); /* set low */
375 if (res != 0) /* check the result */
376 {
377 handle->debug_print("tm1622: cs gpio write failed.\n"); /* cs gpio write failed */
378
379 return 1; /* return error */
380 }
381 handle->delay_us(TM1622_COMMAND_DATA_DELAY); /* delay */
382 res = a_tm1622_write_bits(handle, (uint32_t)(0x05), 3); /* write bits */
383 if (res != 0) /* check the result */
384 {
385 return 1; /* return error */
386 }
387 res = a_tm1622_write_bits(handle, (uint32_t)(addr & 0x3F), 6); /* write bits */
388 if (res != 0) /* check the result */
389 {
390 return 1; /* return error */
391 }
392
393 for (i = 0; i < len; i++) /* loop */
394 {
395 uint8_t input_lsb;
396 uint8_t output_lsb = 0;
397 uint8_t input_msb;
398 uint8_t output_msb = 0;
399
400 res = a_tm1622_read_bits(handle, &data, 4); /* read bits */
401 if (res != 0) /* check the result */
402 {
403 return 1; /* return error */
404 }
405 input_lsb = (uint8_t)(data & 0xF); /* set input */
406 if (and_or != NULL) /* not null */
407 {
408 and_or(i, 0, input_lsb, &output_lsb); /* run and or function */
409 }
410 res = a_tm1622_write_bits(handle, (uint32_t)(output_lsb & 0x0F), 4); /* write bits */
411 if (res != 0) /* check the result */
412 {
413 return 1; /* return error */
414 }
415
416 res = a_tm1622_read_bits(handle, &data, 4); /* read bits */
417 if (res != 0) /* check the result */
418 {
419 return 1; /* return error */
420 }
421 input_msb = (uint8_t)(data & 0xF); /* set input */
422 if (and_or != NULL) /* not null */
423 {
424 and_or(i, 1, input_msb, &output_msb); /* run and or function */
425 }
426 res = a_tm1622_write_bits(handle, (uint32_t)(output_msb & 0x0F), 4); /* write bits */
427 if (res != 0) /* check the result */
428 {
429 return 1; /* return error */
430 }
431 }
432 res = handle->cs_gpio_write(1); /* set high */
433 if (res != 0) /* check the result */
434 {
435 handle->debug_print("tm1622: cs gpio write failed.\n"); /* cs gpio write failed */
436
437 return 1; /* return error */
438 }
439
440 return 0; /* success return 0 */
441}
442
455{
456 uint8_t res;
457
458 if (handle == NULL) /* check handle */
459 {
460 return 2; /* return error */
461 }
462 if (handle->inited != 1) /* check handle initialization */
463 {
464 return 3; /* return error */
465 }
466
467 if (enable != TM1622_BOOL_TRUE) /* disable */
468 {
469 res = a_tm1622_write_command(handle, TM1622_COMMAND_SYS_DIS); /* write command */
470 if (res != 0) /* check error */
471 {
472 return 1; /* return error */
473 }
474
475 return 0; /* success return 0 */
476 }
477 else /* enable */
478 {
479 res = a_tm1622_write_command(handle, TM1622_COMMAND_SYS_EN); /* write command */
480 if (res != 0) /* check error */
481 {
482 return 1; /* return error */
483 }
484
485 return 0; /* success return 0 */
486 }
487}
488
501{
502 uint8_t res;
503
504 if (handle == NULL) /* check handle */
505 {
506 return 2; /* return error */
507 }
508 if (handle->inited != 1) /* check handle initialization */
509 {
510 return 3; /* return error */
511 }
512
513 if (enable != TM1622_BOOL_TRUE) /* disable */
514 {
515 res = a_tm1622_write_command(handle, TM1622_COMMAND_LCD_OFF); /* write command */
516 if (res != 0) /* check error */
517 {
518 return 1; /* return error */
519 }
520
521 return 0; /* success return 0 */
522 }
523 else /* enable */
524 {
525 res = a_tm1622_write_command(handle, TM1622_COMMAND_LCD_ON); /* write command */
526 if (res != 0) /* check error */
527 {
528 return 1; /* return error */
529 }
530
531 return 0; /* success return 0 */
532 }
533}
534
547{
548 uint8_t res;
549
550 if (handle == NULL) /* check handle */
551 {
552 return 2; /* return error */
553 }
554 if (handle->inited != 1) /* check handle initialization */
555 {
556 return 3; /* return error */
557 }
558
559 if (enable != TM1622_BOOL_TRUE) /* disable */
560 {
561 res = a_tm1622_write_command(handle, TM1622_COMMAND_TIMER_DIS); /* write command */
562 if (res != 0) /* check error */
563 {
564 return 1; /* return error */
565 }
566
567 return 0; /* success return 0 */
568 }
569 else /* enable */
570 {
571 res = a_tm1622_write_command(handle, TM1622_COMMAND_TIMER_EN); /* write command */
572 if (res != 0) /* check error */
573 {
574 return 1; /* return error */
575 }
576
577 return 0; /* success return 0 */
578 }
579}
580
593{
594 uint8_t res;
595
596 if (handle == NULL) /* check handle */
597 {
598 return 2; /* return error */
599 }
600 if (handle->inited != 1) /* check handle initialization */
601 {
602 return 3; /* return error */
603 }
604
605 if (enable != TM1622_BOOL_TRUE) /* disable */
606 {
607 res = a_tm1622_write_command(handle, TM1622_COMMAND_WDT_DIS); /* write command */
608 if (res != 0) /* check error */
609 {
610 return 1; /* return error */
611 }
612
613 return 0; /* success return 0 */
614 }
615 else /* enable */
616 {
617 res = a_tm1622_write_command(handle, TM1622_COMMAND_WDT_EN); /* write command */
618 if (res != 0) /* check error */
619 {
620 return 1; /* return error */
621 }
622
623 return 0; /* success return 0 */
624 }
625}
626
639{
640 uint8_t res;
641
642 if (handle == NULL) /* check handle */
643 {
644 return 2; /* return error */
645 }
646 if (handle->inited != 1) /* check handle initialization */
647 {
648 return 3; /* return error */
649 }
650
651 if (enable != TM1622_BOOL_TRUE) /* disable */
652 {
653 res = a_tm1622_write_command(handle, TM1622_COMMAND_TONE_OFF); /* write command */
654 if (res != 0) /* check error */
655 {
656 return 1; /* return error */
657 }
658
659 return 0; /* success return 0 */
660 }
661 else /* enable */
662 {
663 res = a_tm1622_write_command(handle, TM1622_COMMAND_TONE_ON); /* write command */
664 if (res != 0) /* check error */
665 {
666 return 1; /* return error */
667 }
668
669 return 0; /* success return 0 */
670 }
671}
672
685{
686 uint8_t res;
687
688 if (handle == NULL) /* check handle */
689 {
690 return 2; /* return error */
691 }
692 if (handle->inited != 1) /* check handle initialization */
693 {
694 return 3; /* return error */
695 }
696
697 if (enable != TM1622_BOOL_TRUE) /* disable */
698 {
699 res = a_tm1622_write_command(handle, TM1622_COMMAND_IRQ_DIS); /* write command */
700 if (res != 0) /* check error */
701 {
702 return 1; /* return error */
703 }
704
705 return 0; /* success return 0 */
706 }
707 else /* enable */
708 {
709 res = a_tm1622_write_command(handle, TM1622_COMMAND_IRQ_EN); /* write command */
710 if (res != 0) /* check error */
711 {
712 return 1; /* return error */
713 }
714
715 return 0; /* success return 0 */
716 }
717}
718
730{
731 uint8_t res;
732
733 if (handle == NULL) /* check handle */
734 {
735 return 2; /* return error */
736 }
737 if (handle->inited != 1) /* check handle initialization */
738 {
739 return 3; /* return error */
740 }
741
742 res = a_tm1622_write_command(handle, TM1622_COMMAND_CLR_TIMER); /* write command */
743 if (res != 0) /* check error */
744 {
745 return 1; /* return error */
746 }
747
748 return 0; /* success return 0 */
749}
750
762{
763 uint8_t res;
764
765 if (handle == NULL) /* check handle */
766 {
767 return 2; /* return error */
768 }
769 if (handle->inited != 1) /* check handle initialization */
770 {
771 return 3; /* return error */
772 }
773
774 res = a_tm1622_write_command(handle, TM1622_COMMAND_CLR_WDT); /* write command */
775 if (res != 0) /* check error */
776 {
777 return 1; /* return error */
778 }
779
780 return 0; /* success return 0 */
781}
782
795{
796 uint8_t res;
797
798 if (handle == NULL) /* check handle */
799 {
800 return 2; /* return error */
801 }
802 if (handle->inited != 1) /* check handle initialization */
803 {
804 return 3; /* return error */
805 }
806
807 if (mode != TM1622_MODE_NORMAL) /* test mode */
808 {
809 res = a_tm1622_write_command(handle, TM1622_COMMAND_TOPT); /* write command */
810 if (res != 0) /* check error */
811 {
812 return 1; /* return error */
813 }
814
815 return 0; /* success return 0 */
816 }
817 else /* normal mode */
818 {
819 res = a_tm1622_write_command(handle, TM1622_COMMAND_TNORMAL); /* write command */
820 if (res != 0) /* check error */
821 {
822 return 1; /* return error */
823 }
824
825 return 0; /* success return 0 */
826 }
827}
828
841{
842 uint8_t res;
843
844 if (handle == NULL) /* check handle */
845 {
846 return 2; /* return error */
847 }
848 if (handle->inited != 1) /* check handle initialization */
849 {
850 return 3; /* return error */
851 }
852
853 if (clk == TM1622_CLOCK_RC_32K) /* rc 32k */
854 {
855 res = a_tm1622_write_command(handle, TM1622_COMMAND_RC_32K); /* write command */
856 if (res != 0) /* check error */
857 {
858 return 1; /* return error */
859 }
860
861 return 0; /* success return 0 */
862 }
863 else /* ext 32k */
864 {
865 res = a_tm1622_write_command(handle, TM1622_COMMAND_EXT_32K); /* write command */
866 if (res != 0) /* check error */
867 {
868 return 1; /* return error */
869 }
870
871 return 0; /* success return 0 */
872 }
873}
874
887{
888 uint8_t res;
889
890 if (handle == NULL) /* check handle */
891 {
892 return 2; /* return error */
893 }
894 if (handle->inited != 1) /* check handle initialization */
895 {
896 return 3; /* return error */
897 }
898
899 if (freq == TM1622_FREQ_F1) /* f1 */
900 {
901 res = a_tm1622_write_command(handle, TM1622_COMMAND_F1); /* write command */
902 if (res != 0) /* check error */
903 {
904 return 1; /* return error */
905 }
906
907 return 0; /* success return 0 */
908 }
909 else if (freq == TM1622_FREQ_F2) /* f2 */
910 {
911 res = a_tm1622_write_command(handle, TM1622_COMMAND_F2); /* write command */
912 if (res != 0) /* check error */
913 {
914 return 1; /* return error */
915 }
916
917 return 0; /* success return 0 */
918 }
919 else if (freq == TM1622_FREQ_F4) /* f4 */
920 {
921 res = a_tm1622_write_command(handle, TM1622_COMMAND_F4); /* write command */
922 if (res != 0) /* check error */
923 {
924 return 1; /* return error */
925 }
926
927 return 0; /* success return 0 */
928 }
929 else if (freq == TM1622_FREQ_F8) /* f8 */
930 {
931 res = a_tm1622_write_command(handle, TM1622_COMMAND_F8); /* write command */
932 if (res != 0) /* check error */
933 {
934 return 1; /* return error */
935 }
936
937 return 0; /* success return 0 */
938 }
939 else if (freq == TM1622_FREQ_F16) /* f16 */
940 {
941 res = a_tm1622_write_command(handle, TM1622_COMMAND_F16); /* write command */
942 if (res != 0) /* check error */
943 {
944 return 1; /* return error */
945 }
946
947 return 0; /* success return 0 */
948 }
949 else if (freq == TM1622_FREQ_F32) /* f32 */
950 {
951 res = a_tm1622_write_command(handle, TM1622_COMMAND_F32); /* write command */
952 if (res != 0) /* check error */
953 {
954 return 1; /* return error */
955 }
956
957 return 0; /* success return 0 */
958 }
959 else if (freq == TM1622_FREQ_F64) /* f64 */
960 {
961 res = a_tm1622_write_command(handle, TM1622_COMMAND_F64); /* write command */
962 if (res != 0) /* check error */
963 {
964 return 1; /* return error */
965 }
966
967 return 0; /* success return 0 */
968 }
969 else /* f128 */
970 {
971 res = a_tm1622_write_command(handle, TM1622_COMMAND_F128); /* write command */
972 if (res != 0) /* check error */
973 {
974 return 1; /* return error */
975 }
976
977 return 0; /* success return 0 */
978 }
979}
980
993{
994 uint8_t res;
995
996 if (handle == NULL) /* check handle */
997 {
998 return 2; /* return error */
999 }
1000 if (handle->inited != 1) /* check handle initialization */
1001 {
1002 return 3; /* return error */
1003 }
1004
1005 if (freq != TM1622_TONE_FREQ_4K) /* 2k */
1006 {
1007 res = a_tm1622_write_command(handle, TM1622_COMMAND_TONE_2K); /* write command */
1008 if (res != 0) /* check error */
1009 {
1010 return 1; /* return error */
1011 }
1012
1013 return 0; /* success return 0 */
1014 }
1015 else /* 4k */
1016 {
1017 res = a_tm1622_write_command(handle, TM1622_COMMAND_TONE_4K); /* write command */
1018 if (res != 0) /* check error */
1019 {
1020 return 1; /* return error */
1021 }
1022
1023 return 0; /* success return 0 */
1024 }
1025}
1026
1038{
1039 uint8_t res;
1040
1041 if (handle == NULL) /* check handle */
1042 {
1043 return 2; /* return error */
1044 }
1045 if (handle->debug_print == NULL) /* check debug_print */
1046 {
1047 return 3; /* return error */
1048 }
1049 if (handle->data_gpio_init == NULL) /* check data_gpio_init */
1050 {
1051 handle->debug_print("tm1622: data_gpio_init is null.\n"); /* data_gpio_init is null */
1052
1053 return 3; /* return error */
1054 }
1055 if (handle->data_gpio_deinit == NULL) /* check data_gpio_deinit */
1056 {
1057 handle->debug_print("tm1622: data_gpio_deinit is null.\n"); /* data_gpio_deinit is null */
1058
1059 return 3; /* return error */
1060 }
1061 if (handle->data_gpio_write == NULL) /* check data_gpio_write */
1062 {
1063 handle->debug_print("tm1622: data_gpio_write is null.\n"); /* data_gpio_write is null */
1064
1065 return 3; /* return error */
1066 }
1067 if (handle->data_gpio_read == NULL) /* check data_gpio_read */
1068 {
1069 handle->debug_print("tm1622: data_gpio_read is null.\n"); /* data_gpio_read is null */
1070
1071 return 3; /* return error */
1072 }
1073 if (handle->wr_gpio_init == NULL) /* check wr_gpio_init */
1074 {
1075 handle->debug_print("tm1622: wr_gpio_init is null.\n"); /* wr_gpio_init is null */
1076
1077 return 3; /* return error */
1078 }
1079 if (handle->wr_gpio_deinit == NULL) /* check wr_gpio_deinit */
1080 {
1081 handle->debug_print("tm1622: wr_gpio_deinit is null.\n"); /* wr_gpio_deinit is null */
1082
1083 return 3; /* return error */
1084 }
1085 if (handle->wr_gpio_write == NULL) /* check wr_gpio_write */
1086 {
1087 handle->debug_print("tm1622: wr_gpio_write is null.\n"); /* wr_gpio_write is null */
1088
1089 return 3; /* return error */
1090 }
1091 if (handle->rd_gpio_init == NULL) /* check rd_gpio_init */
1092 {
1093 handle->debug_print("tm1622: rd_gpio_init is null.\n"); /* rd_gpio_init is null */
1094
1095 return 3; /* return error */
1096 }
1097 if (handle->rd_gpio_deinit == NULL) /* check rd_gpio_deinit */
1098 {
1099 handle->debug_print("tm1622: rd_gpio_deinit is null.\n"); /* rd_gpio_deinit is null */
1100
1101 return 3; /* return error */
1102 }
1103 if (handle->rd_gpio_write == NULL) /* check rd_gpio_write */
1104 {
1105 handle->debug_print("tm1622: rd_gpio_write is null.\n"); /* rd_gpio_write is null */
1106
1107 return 3; /* return error */
1108 }
1109 if (handle->cs_gpio_init == NULL) /* check cs_gpio_init */
1110 {
1111 handle->debug_print("tm1622: cs_gpio_init is null.\n"); /* cs_gpio_init is null */
1112
1113 return 3; /* return error */
1114 }
1115 if (handle->cs_gpio_deinit == NULL) /* check cs_gpio_deinit */
1116 {
1117 handle->debug_print("tm1622: cs_gpio_deinit is null.\n"); /* cs_gpio_deinit is null */
1118
1119 return 3; /* return error */
1120 }
1121 if (handle->cs_gpio_write == NULL) /* check cs_gpio_write */
1122 {
1123 handle->debug_print("tm1622: cs_gpio_write is null.\n"); /* cs_gpio_write is null */
1124
1125 return 3; /* return error */
1126 }
1127 if (handle->delay_us == NULL) /* check delay_us */
1128 {
1129 handle->debug_print("tm1622: delay_us is null.\n"); /* delay_us is null */
1130
1131 return 3; /* return error */
1132 }
1133 if (handle->delay_ms == NULL) /* check delay_ms */
1134 {
1135 handle->debug_print("tm1622: delay_ms is null.\n"); /* delay_ms is null */
1136
1137 return 3; /* return error */
1138 }
1139
1140 res = handle->data_gpio_init(); /* data gpio init */
1141 if (res != 0) /* check result */
1142 {
1143 handle->debug_print("tm1622: data gpio init failed.\n"); /* data gpio init failed */
1144
1145 return 1; /* return error */
1146 }
1147 res = handle->wr_gpio_init(); /* wr gpio init */
1148 if (res != 0) /* check result */
1149 {
1150 handle->debug_print("tm1622: wr gpio init failed.\n"); /* wr gpio init failed */
1151 (void)handle->data_gpio_deinit(); /* data gpio deinit */
1152
1153 return 1; /* return error */
1154 }
1155 res = handle->rd_gpio_init(); /* rd gpio init */
1156 if (res != 0) /* check result */
1157 {
1158 handle->debug_print("tm1622: rd gpio init failed.\n"); /* rd gpio init failed */
1159 (void)handle->data_gpio_deinit(); /* data gpio deinit */
1160 (void)handle->wr_gpio_deinit(); /* wr gpio deinit */
1161
1162 return 1; /* return error */
1163 }
1164 res = handle->cs_gpio_init(); /* cs gpio init */
1165 if (res != 0) /* check result */
1166 {
1167 handle->debug_print("tm1622: cs gpio init failed.\n"); /* cs gpio init failed */
1168 (void)handle->data_gpio_deinit(); /* data gpio deinit */
1169 (void)handle->wr_gpio_deinit(); /* wr gpio deinit */
1170 (void)handle->rd_gpio_deinit(); /* rd gpio deinit */
1171
1172 return 1; /* return error */
1173 }
1174 res = handle->cs_gpio_write(1); /* set high */
1175 if (res != 0) /* check result */
1176 {
1177 handle->debug_print("tm1622: cs gpio write failed.\n"); /* cs gpio write failed */
1178 (void)handle->data_gpio_deinit(); /* data gpio deinit */
1179 (void)handle->wr_gpio_deinit(); /* wr gpio deinit */
1180 (void)handle->rd_gpio_deinit(); /* rd gpio deinit */
1181 (void)handle->cs_gpio_deinit(); /* cs gpio deinit */
1182
1183 return 1; /* return error */
1184 }
1185 res = handle->wr_gpio_write(1); /* set high */
1186 if (res != 0) /* check result */
1187 {
1188 handle->debug_print("tm1622: wr gpio write failed.\n"); /* wr gpio write failed */
1189 (void)handle->data_gpio_deinit(); /* data gpio deinit */
1190 (void)handle->wr_gpio_deinit(); /* wr gpio deinit */
1191 (void)handle->rd_gpio_deinit(); /* rd gpio deinit */
1192 (void)handle->cs_gpio_deinit(); /* cs gpio deinit */
1193
1194 return 1; /* return error */
1195 }
1196 res = handle->rd_gpio_write(1); /* set high */
1197 if (res != 0) /* check result */
1198 {
1199 handle->debug_print("tm1622: rd gpio write failed.\n"); /* rd gpio write failed */
1200 (void)handle->data_gpio_deinit(); /* data gpio deinit */
1201 (void)handle->wr_gpio_deinit(); /* wr gpio deinit */
1202 (void)handle->rd_gpio_deinit(); /* rd gpio deinit */
1203 (void)handle->cs_gpio_deinit(); /* cs gpio deinit */
1204
1205 return 1; /* return error */
1206 }
1207 res = handle->data_gpio_write(1); /* set high */
1208 if (res != 0) /* check result */
1209 {
1210 handle->debug_print("tm1622: data gpio write failed.\n"); /* data gpio write failed */
1211 (void)handle->data_gpio_deinit(); /* data gpio deinit */
1212 (void)handle->wr_gpio_deinit(); /* wr gpio deinit */
1213 (void)handle->rd_gpio_deinit(); /* rd gpio deinit */
1214 (void)handle->cs_gpio_deinit(); /* cs gpio deinit */
1215
1216 return 1; /* return error */
1217 }
1218 handle->inited = 1; /* flag inited */
1219
1220 return 0; /* success return 0 */
1221}
1222
1235{
1236 uint8_t res;
1237
1238 if (handle == NULL) /* check handle */
1239 {
1240 return 2; /* return error */
1241 }
1242 if (handle->inited != 1) /* check handle initialization */
1243 {
1244 return 3; /* return error */
1245 }
1246
1247 res = a_tm1622_write_command(handle, TM1622_COMMAND_SYS_DIS); /* write command */
1248 if (res != 0) /* check error */
1249 {
1250 return 4; /* return error */
1251 }
1252
1253 res = handle->data_gpio_deinit(); /* data gpio deinit */
1254 if (res != 0) /* check the result */
1255 {
1256 handle->debug_print("tm1622: data gpio deinit failed.\n"); /* data gpio deinit failed */
1257
1258 return 1; /* return error */
1259 }
1260 res = handle->wr_gpio_deinit(); /* wr gpio deinit */
1261 if (res != 0) /* check the result */
1262 {
1263 handle->debug_print("tm1622: wr gpio deinit failed.\n"); /* wr gpio deinit failed */
1264
1265 return 1; /* return error */
1266 }
1267 res = handle->rd_gpio_deinit(); /* rd gpio deinit */
1268 if (res != 0) /* check the result */
1269 {
1270 handle->debug_print("tm1622: rd gpio deinit failed.\n"); /* rd gpio deinit failed */
1271
1272 return 1; /* return error */
1273 }
1274 res = handle->cs_gpio_deinit(); /* cs gpio deinit */
1275 if (res != 0) /* check the result */
1276 {
1277 handle->debug_print("tm1622: cs gpio deinit failed.\n"); /* cs gpio deinit failed */
1278
1279 return 1; /* return error */
1280 }
1281 handle->inited = 0; /* flag close */
1282
1283 return 0; /* success return 0 */
1284}
1285
1300uint8_t tm1622_write_segment(tm1622_handle_t *handle, uint8_t addr, uint8_t *data, uint8_t len)
1301{
1302 uint8_t res;
1303
1304 if (handle == NULL) /* check handle */
1305 {
1306 return 2; /* return error */
1307 }
1308 if (handle->inited != 1) /* check handle initialization */
1309 {
1310 return 3; /* return error */
1311 }
1312 if (addr + len > 32) /* check range */
1313 {
1314 handle->debug_print("tm1622: addr + len > 32.\n"); /* addr + len > 32 */
1315
1316 return 4; /* return error */
1317 }
1318
1319 res = a_tm1622_write_ram(handle, addr * 2, data, len); /* write ram */
1320 if (res != 0) /* check error */
1321 {
1322 return 1; /* return error */
1323 }
1324
1325 return 0; /* success return 0 */
1326}
1327
1339{
1340 uint8_t res;
1341 uint8_t buf[32];
1342
1343 if (handle == NULL) /* check handle */
1344 {
1345 return 2; /* return error */
1346 }
1347 if (handle->inited != 1) /* check handle initialization */
1348 {
1349 return 3; /* return error */
1350 }
1351
1352 memset(buf, 0, sizeof(uint8_t) * 32); /* clear buffer */
1353 res = a_tm1622_write_ram(handle, 0, buf, 32); /* write ram */
1354 if (res != 0) /* check error */
1355 {
1356 return 1; /* return error */
1357 }
1358
1359 return 0; /* success return 0 */
1360}
1361
1373uint8_t tm1622_set_command(tm1622_handle_t *handle, uint16_t cmd)
1374{
1375 uint8_t res;
1376
1377 if (handle == NULL) /* check handle */
1378 {
1379 return 2; /* return error */
1380 }
1381 if (handle->inited != 1) /* check handle initialization */
1382 {
1383 return 3; /* return error */
1384 }
1385
1386 res = a_tm1622_write_command(handle, cmd); /* write */
1387 if (res != 0) /* check error */
1388 {
1389 return 1; /* return error */
1390 }
1391
1392 return 0; /* success return 0 */
1393}
1394
1408uint8_t tm1622_set_data(tm1622_handle_t *handle, uint8_t addr, uint8_t *data, uint8_t len)
1409{
1410 uint8_t res;
1411
1412 if (handle == NULL) /* check handle */
1413 {
1414 return 2; /* return error */
1415 }
1416 if (handle->inited != 1) /* check handle initialization */
1417 {
1418 return 3; /* return error */
1419 }
1420
1421 res = a_tm1622_write_ram(handle, addr, data, len); /* write */
1422 if (res != 0) /* check error */
1423 {
1424 return 1; /* return error */
1425 }
1426
1427 return 0; /* success return 0 */
1428}
1429
1443uint8_t tm1622_get_data(tm1622_handle_t *handle, uint8_t addr, uint8_t *data, uint8_t len)
1444{
1445 uint8_t res;
1446
1447 if (handle == NULL) /* check handle */
1448 {
1449 return 2; /* return error */
1450 }
1451 if (handle->inited != 1) /* check handle initialization */
1452 {
1453 return 3; /* return error */
1454 }
1455
1456 res = a_tm1622_read_ram(handle, addr, data, len); /* read */
1457 if (res != 0) /* check error */
1458 {
1459 return 1; /* return error */
1460 }
1461
1462 return 0; /* success return 0 */
1463}
1464
1478uint8_t tm1622_read_modify_write(tm1622_handle_t *handle, uint8_t addr, void (*and_or)(uint8_t addr, uint8_t lsb_msb, uint8_t input, uint8_t *output), uint8_t len)
1479{
1480 uint8_t res;
1481
1482 if (handle == NULL) /* check handle */
1483 {
1484 return 2; /* return error */
1485 }
1486 if (handle->inited != 1) /* check handle initialization */
1487 {
1488 return 3; /* return error */
1489 }
1490
1491 res = a_tm1622_read_modify_write(handle, addr, and_or, len); /* read write */
1492 if (res != 0) /* check error */
1493 {
1494 return 1; /* return error */
1495 }
1496
1497 return 0; /* success return 0 */
1498}
1499
1509{
1510 if (info == NULL) /* check handle */
1511 {
1512 return 2; /* return error */
1513 }
1514
1515 memset(info, 0, sizeof(tm1622_info_t)); /* initialize tm1622 info structure */
1516 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
1517 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
1518 strncpy(info->interface, "GPIO", 8); /* copy interface name */
1519 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
1520 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
1521 info->max_current_ma = MAX_CURRENT; /* set maximum current */
1522 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
1523 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
1524 info->driver_version = DRIVER_VERSION; /* set driver version */
1525
1526 return 0; /* success return 0 */
1527}
#define TM1622_COMMAND_TIMER_EN
#define TM1622_COMMAND_TIMER_DIS
#define TM1622_COMMAND_EXT_32K
#define TM1622_COMMAND_IRQ_DIS
#define TM1622_COMMAND_SYS_DIS
chip command definition
#define TM1622_COMMAND_WDT_EN
#define TM1622_COMMAND_IRQ_EN
#define MAX_CURRENT
#define TM1622_COMMAND_LCD_ON
#define TM1622_COMMAND_TOPT
#define TM1622_COMMAND_F2
#define TM1622_COMMAND_TONE_2K
#define TM1622_COMMAND_F4
#define SUPPLY_VOLTAGE_MAX
#define TM1622_COMMAND_TONE_4K
#define TM1622_COMMAND_F16
#define TM1622_COMMAND_F8
#define TM1622_COMMAND_SYS_EN
#define TM1622_COMMAND_TONE_ON
#define TM1622_COMMAND_RC_32K
#define TM1622_COMMAND_CLR_TIMER
#define TM1622_COMMAND_F32
#define TEMPERATURE_MAX
#define TM1622_COMMAND_TONE_OFF
#define TM1622_COMMAND_TNORMAL
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define TM1622_COMMAND_LCD_OFF
#define SUPPLY_VOLTAGE_MIN
#define TM1622_COMMAND_F1
#define TM1622_COMMAND_F128
#define TM1622_COMMAND_CLR_WDT
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
#define TM1622_COMMAND_F64
#define TM1622_COMMAND_WDT_DIS
driver tm1622 header file
tm1622_clock_t
tm1622 clock enumeration definition
uint8_t tm1622_clear_timer(tm1622_handle_t *handle)
clear timer
uint8_t tm1622_set_lcd_bias(tm1622_handle_t *handle, tm1622_bool_t enable)
enable or disable lcd bias
uint8_t tm1622_clear_segment(tm1622_handle_t *handle)
clear segment
uint8_t tm1622_write_segment(tm1622_handle_t *handle, uint8_t addr, uint8_t *data, uint8_t len)
write segment
uint8_t tm1622_clear_watchdog(tm1622_handle_t *handle)
clear watchdog
tm1622_freq_t
tm1622 freq enumeration definition
uint8_t tm1622_set_tone_freq(tm1622_handle_t *handle, tm1622_tone_freq_t freq)
set tone freq
tm1622_tone_freq_t
tm1622 tone freq enumeration definition
tm1622_mode_t
tm1622 mode enumeration definition
uint8_t tm1622_set_watchdog(tm1622_handle_t *handle, tm1622_bool_t enable)
enable or disable watchdog
struct tm1622_handle_s tm1622_handle_t
tm1622 handle structure definition
uint8_t tm1622_set_timer(tm1622_handle_t *handle, tm1622_bool_t enable)
enable or disable timer
struct tm1622_info_s tm1622_info_t
tm1622 information structure definition
uint8_t tm1622_set_clock(tm1622_handle_t *handle, tm1622_clock_t clk)
set clock
tm1622_bool_t
tm1622 bool enumeration definition
uint8_t tm1622_deinit(tm1622_handle_t *handle)
close the chip
uint8_t tm1622_set_freq(tm1622_handle_t *handle, tm1622_freq_t freq)
set freq
uint8_t tm1622_init(tm1622_handle_t *handle)
initialize the chip
uint8_t tm1622_set_tone(tm1622_handle_t *handle, tm1622_bool_t enable)
enable or disable tone
#define TM1622_COMMAND_DATA_DELAY
tm1622 command data delay definition
uint8_t tm1622_set_oscillator(tm1622_handle_t *handle, tm1622_bool_t enable)
enable or disable oscillator
uint8_t tm1622_set_irq(tm1622_handle_t *handle, tm1622_bool_t enable)
enable or disable irq
uint8_t tm1622_set_mode(tm1622_handle_t *handle, tm1622_mode_t mode)
set mode
uint8_t tm1622_info(tm1622_info_t *info)
get chip's information
@ TM1622_CLOCK_RC_32K
@ TM1622_FREQ_F64
@ TM1622_FREQ_F8
@ TM1622_FREQ_F2
@ TM1622_FREQ_F4
@ TM1622_FREQ_F1
@ TM1622_FREQ_F32
@ TM1622_FREQ_F16
@ TM1622_TONE_FREQ_4K
@ TM1622_MODE_NORMAL
@ TM1622_BOOL_TRUE
uint8_t tm1622_read_modify_write(tm1622_handle_t *handle, uint8_t addr, void(*and_or)(uint8_t addr, uint8_t lsb_msb, uint8_t input, uint8_t *output), uint8_t len)
read modify write
uint8_t tm1622_get_data(tm1622_handle_t *handle, uint8_t addr, uint8_t *data, uint8_t len)
get data
uint8_t tm1622_set_command(tm1622_handle_t *handle, uint16_t cmd)
set command
uint8_t tm1622_set_data(tm1622_handle_t *handle, uint8_t addr, uint8_t *data, uint8_t len)
set data
uint8_t(* data_gpio_write)(uint8_t level)
void(* delay_ms)(uint32_t ms)
uint8_t(* rd_gpio_write)(uint8_t level)
uint8_t(* wr_gpio_deinit)(void)
uint8_t(* cs_gpio_deinit)(void)
uint8_t(* data_gpio_init)(void)
void(* debug_print)(const char *const fmt,...)
uint8_t(* data_gpio_read)(uint8_t *level)
uint8_t(* rd_gpio_deinit)(void)
void(* delay_us)(uint32_t us)
uint8_t(* wr_gpio_init)(void)
uint8_t(* cs_gpio_init)(void)
uint8_t(* data_gpio_deinit)(void)
uint8_t(* cs_gpio_write)(uint8_t level)
uint8_t(* wr_gpio_write)(uint8_t level)
uint8_t(* rd_gpio_init)(void)
float supply_voltage_max_v
uint32_t driver_version
char manufacturer_name[32]
float supply_voltage_min_v
char chip_name[32]