LibDriver SYN6658
Loading...
Searching...
No Matches
driver_syn6658.c
Go to the documentation of this file.
1
36
37#include "driver_syn6658.h"
38#include <stdarg.h>
39
43#define CHIP_NAME "Voicetx SYN6658"
44#define MANUFACTURER_NAME "Voicetx"
45#define SUPPLY_VOLTAGE_MIN 3.1f
46#define SUPPLY_VOLTAGE_MAX 4.5f
47#define MAX_CURRENT 53.0f
48#define TEMPERATURE_MIN -40.0f
49#define TEMPERATURE_MAX 85.0f
50#define DRIVER_VERSION 1000
51
61static uint8_t a_check_busy(syn6658_handle_t *handle, uint32_t timeout)
62{
63 uint8_t value;
64 uint32_t i;
65
66 for (i = 0; i < timeout; i++) /* loop all */
67 {
68 if (handle->gpio_ready_read(&value) != 0) /* read ready level */
69 {
70 return 1; /* return error */
71 }
72 if (value == 0) /* check value */
73 {
74 return 0; /* return ready */
75 }
76 handle->delay_ms(1); /* delay 1ms */
77 }
78
79 return 1; /* return timeout */
80}
81
92static uint8_t a_write(syn6658_handle_t *handle, uint8_t *buf, uint16_t len)
93{
94 uint8_t res;
95 uint16_t i;
96
97 if (handle->uart_spi != 0) /* spi interface */
98 {
99 for (i = 0; i < len; i++) /* write all */
100 {
101 uint8_t rx;
102
103 res = handle->spi_transmit(&buf[i], &rx, 1); /* write one byte */
104 if (res != 0) /* check result */
105 {
106 return 1; /* return error */
107 }
108 handle->delay_ms(1); /* delay 1ms */
109 }
110 }
111 else /* uart interface */
112 {
113 res = handle->uart_flush(); /* uart flush */
114 if (res != 0) /* check result */
115 {
116 return 1; /* return error */
117 }
118 res = handle->uart_write(buf, len); /* uart write */
119 if (res != 0) /* check result */
120 {
121 return 1; /* return error */
122 }
123 }
124
125 return 0; /* success return 0 */
126}
127
138static uint16_t a_read(syn6658_handle_t *handle, uint8_t *buf, uint16_t len)
139{
140 uint8_t res;
141 uint16_t i;
142
143 if (handle->uart_spi != 0) /* spi interface */
144 {
145 for (i = 0; i < len; i++) /* loop all */
146 {
147 uint8_t tx = 0xFF;
148
149 res = handle->spi_transmit(&tx, &buf[i], 1); /* read one byte */
150 if (res != 0) /* check result */
151 {
152 return 1; /* return error */
153 }
154 handle->delay_ms(1); /* delay 1ms */
155 }
156 }
157 else
158 {
159 if (handle->uart_read(buf, len) != len) /* read data */
160 {
161 return 1; /* return error */
162 }
163 }
164
165 return len; /* success return 0 */
166}
167
178{
179 if (handle == NULL) /* check handle */
180 {
181 return 2; /* return error */
182 }
183
184 handle->uart_spi = (uint8_t)interface; /* set interface */
185
186 return 0; /* success return 0 */
187}
188
199{
200 if (handle == NULL) /* check handle */
201 {
202 return 2; /* return error */
203 }
204
205 *interface = (syn6658_interface_t)(handle->uart_spi); /* get interface */
206
207 return 0; /* success return 0 */
208}
209
222{
223 if (handle == NULL) /* check handle */
224 {
225 return 2; /* return error */
226 }
227 if (handle->debug_print == NULL) /* check debug_print */
228 {
229 return 3; /* return error */
230 }
231 if (handle->uart_init == NULL) /* check uart_init */
232 {
233 handle->debug_print("syn6658: uart_init is null.\n"); /* uart_init is null */
234
235 return 3; /* return error */
236 }
237 if (handle->uart_deinit == NULL) /* check uart_deinit */
238 {
239 handle->debug_print("syn6658: uart_deinit is null.\n"); /* uart_deinit is null */
240
241 return 3; /* return error */
242 }
243 if (handle->uart_read == NULL) /* check uart_read */
244 {
245 handle->debug_print("syn6658: uart_read is null.\n"); /* uart_read is null */
246
247 return 3; /* return error */
248 }
249 if (handle->uart_write == NULL) /* check uart_write */
250 {
251 handle->debug_print("syn6658: uart_write is null.\n"); /* uart_write is null */
252
253 return 3; /* return error */
254 }
255 if (handle->uart_flush == NULL) /* check uart_flush */
256 {
257 handle->debug_print("syn6658: uart_flush is null.\n"); /* uart_flush is null */
258
259 return 3; /* return error */
260 }
261 if (handle->spi_init == NULL) /* check spi_init */
262 {
263 handle->debug_print("syn6658: spi_init is null.\n"); /* spi_init is null */
264
265 return 3; /* return error */
266 }
267 if (handle->spi_deinit == NULL) /* check spi_deinit */
268 {
269 handle->debug_print("syn6658: spi_deinit is null.\n"); /* spi_deinit is null */
270
271 return 3; /* return error */
272 }
273 if (handle->spi_transmit == NULL) /* check spi_transmit */
274 {
275 handle->debug_print("syn6658: spi_transmit is null.\n"); /* spi_transmit is null */
276
277 return 3; /* return error */
278 }
279 if (handle->gpio_ready_init == NULL) /* check gpio_ready_init */
280 {
281 handle->debug_print("syn6658: gpio_ready_init is null.\n"); /* gpio_ready_init is null */
282
283 return 3; /* return error */
284 }
285 if (handle->gpio_ready_deinit == NULL) /* check gpio_ready_deinit */
286 {
287 handle->debug_print("syn6658: gpio_ready_deinit is null.\n"); /* gpio_ready_deinit is null */
288
289 return 3; /* return error */
290 }
291 if (handle->gpio_ready_read == NULL) /* check gpio_ready_read */
292 {
293 handle->debug_print("syn6658: gpio_ready_read is null.\n"); /* gpio_ready_read is null */
294
295 return 3; /* return error */
296 }
297 if (handle->delay_ms == NULL) /* check delay_ms */
298 {
299 handle->debug_print("syn6658: delay_ms is null.\n"); /* delay_ms is null */
300
301 return 3; /* return error */
302 }
303
304 if (handle->uart_spi != 0) /* spi interface */
305 {
306 if (handle->spi_init() != 0) /* spi init */
307 {
308 handle->debug_print("syn6658: spi init failed.\n"); /* spi init failed */
309
310 return 1; /* return error */
311 }
312 }
313 else /* uart interface */
314 {
315 if (handle->uart_init() != 0) /* uart init */
316 {
317 handle->debug_print("syn6658: uart init failed.\n"); /* uart init failed */
318
319 return 1; /* return error */
320 }
321 }
322 if (handle->gpio_ready_init() != 0) /* gpio ready init */
323 {
324 if (handle->uart_spi != 0) /* spi interface */
325 {
326 (void)handle->spi_deinit(); /* spi deinit */
327 }
328 else /* uart interface */
329 {
330 (void)handle->uart_deinit(); /* uart deinit */
331 }
332 handle->debug_print("syn6658: gpio ready init failed.\n"); /* gpio ready init failed */
333
334 return 4; /* return error */
335 }
336 handle->inited = 1; /* flag finish initialization */
337
338 return 0; /* success return 0 */
339}
340
353{
354 if (handle == NULL) /* check handle */
355 {
356 return 2; /* return error */
357 }
358 if (handle->inited != 1) /* check handle initialization */
359 {
360 return 3; /* return error */
361 }
362
363 if (handle->uart_spi != 0) /* spi interface */
364 {
365 if (handle->spi_deinit() != 0) /* spi deinit */
366 {
367 handle->debug_print("syn6658: spi deinit failed.\n"); /* spi deinit failed */
368
369 return 1; /* return error */
370 }
371 }
372 else /* uart interface */
373 {
374 if (handle->uart_deinit() != 0) /* uart deinit */
375 {
376 handle->debug_print("syn6658: uart deinit failed.\n"); /* uart deinit failed */
377
378 return 1; /* return error */
379 }
380 }
381 if (handle->gpio_ready_deinit() != 0) /* gpio ready deinit */
382 {
383 handle->debug_print("syn6658: gpio ready deinit failed.\n"); /* gpio ready deinit failed */
384
385 return 4; /* return error */
386 }
387 handle->inited = 0; /* flag closed */
388
389 return 0; /* success return 0 */
390}
391
404{
405 uint8_t res;
406 uint8_t value;
407 uint8_t times = 3;
408 uint16_t len;
409 uint8_t temp[1];
410 uint8_t cmd[4];
411
412 if (handle == NULL) /* check handle */
413 {
414 return 2; /* return error */
415 }
416 if (handle->inited != 1) /* check handle initialization */
417 {
418 return 3; /* return error */
419 }
420 if (handle->uart_spi != 0) /* spi interface */
421 {
422 if (handle->gpio_ready_read(&value) != 0) /* read ready level */
423 {
424 return 1; /* return error */
425 }
426 if (value != 0) /* check value */
427 {
428 *status = (syn6658_status_t)(1); /* set status */
429 }
430 else
431 {
432 *status = (syn6658_status_t)(0); /* set status */
433 }
434
435 return 0; /* success return 0 */
436 }
437 else /* uart interface */
438 {
439 cmd[0] = 0xFD; /* frame header */
440 cmd[1] = 0x00; /* length msb */
441 cmd[2] = 0x01; /* length lsb */
442 cmd[3] = 0x21; /* command */
443 while (1) /* loop */
444 {
445 res = handle->uart_flush(); /* uart flush */
446 if (res != 0) /* check result */
447 {
448 handle->debug_print("syn6658: uart flush failed.\n"); /* uart flush failed */
449
450 return 1; /* return error */
451 }
452 res = handle->uart_write((uint8_t *)cmd, 4); /* uart write */
453 if (res != 0) /* check result */
454 {
455 handle->debug_print("syn6658: uart write failed.\n"); /* uart write failed */
456
457 return 1; /* return error */
458 }
459 handle->delay_ms(100); /* delay 100 ms */
460 memset(temp, 0, sizeof(uint8_t) * 1); /* clear the buffer */
461 len = handle->uart_read((uint8_t *)temp, 1); /* uart read */
462 if (len != 1) /* check result */
463 {
464 handle->debug_print("syn6658: uart read failed.\n"); /* uart read failed */
465
466 return 1; /* return error */
467 }
468 if (temp[0] == 0x4F) /* check frame */
469 {
470 *status = (syn6658_status_t)(0); /* set status */
471
472 return 0; /* success return 0 */
473 }
474 else if (temp[0] == 0x4E) /* check frame */
475 {
476 *status = (syn6658_status_t)(1); /* set status */
477
478 return 0; /* success return 0 */
479 }
480 else
481 {
482 if (times != 0) /* check times */
483 {
484 times--; /* retry times-- */
485 handle->delay_ms(100); /* delay 100 ms */
486
487 continue; /* continue */
488 }
489 handle->debug_print("syn6658: command receive failed.\n"); /* command receive failed */
490
491 return 1; /* return error */
492 }
493 }
494 }
495}
496
509{
510 uint8_t res;
511 uint16_t len;
512 uint8_t temp;
513 uint8_t cmd[4];
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 (handle->uart_spi != 0) /* check interface */
524 {
525 handle->debug_print("syn6658: spi interface \
526 can't use this function.\n"); /* spi interface can't use this function */
527
528 return 4; /* return error */
529 }
530
531 cmd[0] = 0xFD; /* frame header */
532 cmd[1] = 0x00; /* length msb */
533 cmd[2] = 0x01; /* length lsb */
534 cmd[3] = 0x02; /* command */
535 res = a_write(handle, (uint8_t *)cmd, 4); /* write data */
536 if (res != 0) /* check result */
537 {
538 handle->debug_print("syn6658: uart write failed.\n"); /* uart write failed */
539
540 return 1; /* return error */
541 }
542 handle->delay_ms(100); /* delay 100 ms */
543 len = a_read(handle, (uint8_t *)&temp, 1); /* read data */
544 if (len != 1) /* check result */
545 {
546 handle->debug_print("syn6658: uart read failed.\n"); /* uart read failed */
547
548 return 1; /* return error */
549 }
550 if (temp == 0x41) /* check return */
551 {
552 return 0; /* success return 0 */
553 }
554 else
555 {
556 handle->debug_print("syn6658: command receive failed.\n"); /* command receive failed */
557
558 return 1; /* return error */
559 }
560}
561
574{
575 uint8_t res;
576 uint16_t len;
577 uint8_t temp;
578 uint8_t cmd[4];
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 if (handle->uart_spi != 0) /* check interface */
589 {
590 handle->debug_print("syn6658: spi interface \
591 can't use this function.\n"); /* spi interface can't use this function */
592
593 return 4; /* return error */
594 }
595
596 cmd[0] = 0xFD; /* frame header */
597 cmd[1] = 0x00; /* length msb */
598 cmd[2] = 0x01; /* length lsb */
599 cmd[3] = 0x03; /* command */
600 res = a_write(handle, (uint8_t *)cmd, 4); /* write data */
601 if (res != 0) /* check result */
602 {
603 handle->debug_print("syn6658: uart write failed.\n"); /* uart write failed */
604
605 return 1; /* return error */
606 }
607 handle->delay_ms(100); /* delay 100 ms */
608 len = a_read(handle, (uint8_t *)&temp, 1); /* read data */
609 if (len != 1) /* check result */
610 {
611 handle->debug_print("syn6658: uart read failed.\n"); /* uart read failed */
612
613 return 1; /* return error */
614 }
615 if (temp == 0x41) /* check return */
616 {
617
618 return 0; /* success return 0 */
619 }
620 else
621 {
622 handle->debug_print("syn6658: command receive failed.\n"); /* command receive failed */
623
624 return 1; /* return error */
625 }
626}
627
640{
641 uint8_t res;
642 uint16_t len;
643 uint8_t temp;
644 uint8_t cmd[4];
645
646 if (handle == NULL) /* check handle */
647 {
648 return 2; /* return error */
649 }
650 if (handle->inited != 1) /* check handle initialization */
651 {
652 return 3; /* return error */
653 }
654 if (handle->uart_spi != 0) /* check interface */
655 {
656 handle->debug_print("syn6658: spi interface \
657 can't use this function.\n"); /* spi interface can't use this function */
658
659 return 4; /* return error */
660 }
661
662 cmd[0] = 0xFD; /* frame header */
663 cmd[1] = 0x00; /* length msb */
664 cmd[2] = 0x01; /* length lsb */
665 cmd[3] = 0x04; /* command */
666 res = a_write(handle, (uint8_t *)cmd, 4); /* write data */
667 if (res != 0) /* check result */
668 {
669 handle->debug_print("syn6658: uart write failed.\n"); /* uart write failed */
670
671 return 1; /* return error */
672 }
673 handle->delay_ms(100); /* delay 100 ms */
674 len = a_read(handle, (uint8_t *)&temp, 1); /* read data */
675 if (len != 1) /* check result */
676 {
677 handle->debug_print("syn6658: uart read failed.\n"); /* uart read failed */
678
679 return 1; /* return error */
680 }
681 if (temp == 0x41) /* check return */
682 {
683 return 0; /* success return 0 */
684 }
685 else
686 {
687 handle->debug_print("syn6658: command receive failed.\n"); /* command receive failed */
688
689 return 1; /* return error */
690 }
691}
692
705{
706 uint8_t res;
707 uint16_t len;
708 uint8_t temp;
709 uint8_t cmd[4];
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 if (handle->uart_spi != 0) /* check interface */
720 {
721 handle->debug_print("syn6658: spi interface \
722 can't use this function.\n"); /* spi interface can't use this function */
723
724 return 4; /* return error */
725 }
726
727 cmd[0] = 0xFD; /* frame header */
728 cmd[1] = 0x00; /* length msb */
729 cmd[2] = 0x01; /* length lsb */
730 cmd[3] = 0x22; /* command */
731 res = a_write(handle, (uint8_t *)cmd, 4); /* write data */
732 if (res != 0) /* check result */
733 {
734 handle->debug_print("syn6658: uart write failed.\n"); /* uart write failed */
735
736 return 1; /* return error */
737 }
738 handle->delay_ms(100); /* delay 100 ms */
739 len = a_read(handle, (uint8_t *)&temp, 1); /* read data */
740 if (len != 1) /* check result */
741 {
742 handle->debug_print("syn6658: uart read failed.\n"); /* uart read failed */
743
744 return 1; /* return error */
745 }
746 if (temp == 0x41) /* check return */
747 {
748 return 0; /* success return 0 */
749 }
750 else
751 {
752 handle->debug_print("syn6658: command receive failed.\n"); /* command receive failed */
753
754 return 1; /* return error */
755 }
756}
757
770{
771 uint8_t res;
772 uint16_t len;
773 uint8_t temp;
774 uint8_t cmd[4];
775
776 if (handle == NULL) /* check handle */
777 {
778 return 2; /* return error */
779 }
780 if (handle->inited != 1) /* check handle initialization */
781 {
782 return 3; /* return error */
783 }
784 if (handle->uart_spi != 0) /* check interface */
785 {
786 handle->debug_print("syn6658: spi interface \
787 can't use this function.\n"); /* spi interface can't use this function */
788
789 return 4; /* return error */
790 }
791
792 cmd[0] = 0xFD; /* frame header */
793 cmd[1] = 0x00; /* length msb */
794 cmd[2] = 0x01; /* length lsb */
795 cmd[3] = 0xFF; /* command */
796 res = a_write(handle, (uint8_t *)cmd, 4); /* write data */
797 if (res != 0) /* check result */
798 {
799 handle->debug_print("syn6658: uart write failed.\n"); /* uart write failed */
800
801 return 1; /* return error */
802 }
803 handle->delay_ms(100); /* delay 100 ms */
804 res = a_write(handle, (uint8_t *)cmd, 4); /* write data */
805 if (res != 0) /* check result */
806 {
807 handle->debug_print("syn6658: uart write failed.\n"); /* uart write failed */
808
809 return 1; /* return error */
810 }
811 handle->delay_ms(100); /* delay 100 ms */
812 len = a_read(handle, (uint8_t *)&temp, 1); /* read data */
813 if (len != 1) /* check result */
814 {
815 handle->debug_print("syn6658: uart read failed.\n"); /* uart read failed */
816
817 return 1; /* return error */
818 }
819 if (temp == 0x41) /* check return */
820 {
821 return 0; /* success return 0 */
822 }
823 else
824 {
825 handle->debug_print("syn6658: command receive failed.\n"); /* command receive failed */
826
827 return 1; /* return error */
828 }
829}
830
842{
843 if (handle == NULL) /* check handle */
844 {
845 return 2; /* return error */
846 }
847 if (handle->inited != 1) /* check handle initialization */
848 {
849 return 3; /* return error */
850 }
851
852 handle->type = (uint8_t)type; /* set type */
853
854 return 0; /* success return 0 */
855}
856
868{
869 if (handle == NULL) /* check handle */
870 {
871 return 2; /* return error */
872 }
873 if (handle->inited != 1) /* check handle initialization */
874 {
875 return 3; /* return error */
876 }
877
878 *type = (syn6658_type_t)(handle->type); /* get type */
879
880 return 0; /* success return 0 */
881}
882
896uint8_t syn6658_synthesis_text(syn6658_handle_t *handle, const char *const fmt, ...)
897{
898 va_list args;
899 uint8_t res;
900 uint8_t temp;
901 uint16_t l;
902 uint16_t len;
903 uint32_t timeout = SYN6658_BUSY_TIMEOUT;
904
905 if (handle == NULL) /* check handle */
906 {
907 return 2; /* return error */
908 }
909 if (handle->inited != 1) /* check handle initialization */
910 {
911 return 3; /* return error */
912 }
913
914 memset((char *)(handle->buf + 5), 0, sizeof(char) * 4091); /* clear buffer */
915 va_start(args, fmt); /* var start */
916 (void)vsnprintf((char *)(handle->buf + 5), 4091,
917 (char const *)fmt, args); /* print to buffer */
918 va_end(args); /* var end */
919 len = (uint16_t)strlen((const char *)(handle->buf + 5)); /* get length of txt */
920 handle->buf[0] = 0xFD; /* frame header */
921 handle->buf[1] = (uint8_t)((len + 2) / 256); /* length msb */
922 handle->buf[2] = (len + 2) % 256; /* length lsb */
923 handle->buf[3] = 0x01; /* command */
924 handle->buf[4] = handle->type; /* command type */
925 res = a_check_busy(handle, timeout);
926 if (res != 0) /* check result */
927 {
928 handle->debug_print("syn6658: chip is busy.\n"); /* chip is busy */
929
930 return 4; /* return error */
931 }
932 res = a_write(handle, handle->buf, len + 5); /* write data */
933 if (res != 0) /* check result */
934 {
935 handle->debug_print("syn6658: write failed.\n"); /* write failed */
936
937 return 1; /* return error */
938 }
939 if (handle->uart_spi == 0) /* uart interface */
940 {
941 handle->delay_ms(100); /* delay 100 ms */
942 }
943 l = a_read(handle, (uint8_t *)&temp, 1); /* read data */
944 if (l != 1) /* check result */
945 {
946 handle->debug_print("syn6658: uart read failed.\n"); /* uart read failed */
947
948 return 5; /* return error */
949 }
950 if (temp == 0x41) /* check return */
951 {
952 if (handle->uart_spi != 0) /* spi interface */
953 {
954 handle->delay_ms(200); /* delay 200 ms */
955 }
956
957 return 0; /* success return 0 */
958 }
959 else
960 {
961 handle->debug_print("syn6658: command receive failed.\n"); /* command receive failed */
962
963 return 5; /* return error */
964 }
965}
966
979uint8_t syn6658_set_synthesis_volume(syn6658_handle_t *handle, uint8_t volume)
980{
981 char cmd[8];
982
983 if (handle == NULL) /* check handle */
984 {
985 return 2; /* return error */
986 }
987 if (handle->inited != 1) /* check handle initialization */
988 {
989 return 3; /* return error */
990 }
991 if (volume > 10) /* check volume */
992 {
993 handle->debug_print("syn6658: volume is invalid.\n"); /* volume is invalid */
994
995 return 4; /* return error */
996 }
997
998 memset((char *)cmd, 0, sizeof(char) * 8); /* memory set 0 */
999 (void)snprintf((char *)cmd, 8, "[v%d]", (int16_t)volume); /* set command */
1000 if (syn6658_set_command_with_arg(handle, 0x01, 0x00, cmd, SYN6658_BUSY_TIMEOUT) != 0) /* write command */
1001 {
1002 return 1; /* return error */
1003 }
1004
1005 return 0; /* success return 0 */
1006}
1007
1020uint8_t syn6658_set_synthesis_speed(syn6658_handle_t *handle, uint8_t speed)
1021{
1022 char cmd[8];
1023
1024 if (handle == NULL) /* check handle */
1025 {
1026 return 2; /* return error */
1027 }
1028 if (handle->inited != 1) /* check handle initialization */
1029 {
1030 return 3; /* return error */
1031 }
1032 if (speed > 10) /* check volume */
1033 {
1034 handle->debug_print("syn6658: speed is invalid.\n"); /* speed is invalid */
1035
1036 return 4; /* return error */
1037 }
1038
1039 memset((char *)cmd, 0, sizeof(char) * 8); /* memory set 0 */
1040 (void)snprintf((char *)cmd, 8, "[s%d]", (int16_t)speed); /* set command */
1041 if (syn6658_set_command_with_arg(handle, 0x01, 0x00, cmd, SYN6658_BUSY_TIMEOUT) != 0) /* write command */
1042 {
1043 return 1; /* return error */
1044 }
1045
1046 return 0; /* success return 0 */
1047}
1048
1061{
1062 char cmd[8];
1063
1064 if (handle == NULL) /* check handle */
1065 {
1066 return 2; /* return error */
1067 }
1068 if (handle->inited != 1) /* check handle initialization */
1069 {
1070 return 3; /* return error */
1071 }
1072
1073 memset((char *)cmd, 0, sizeof(char) * 8); /* memory set 0 */
1074 (void)snprintf((char *)cmd, 8, "[m%d]", (int16_t)speaker); /* set command */
1075 if (syn6658_set_command_with_arg(handle, 0x01, 0x00, cmd, SYN6658_BUSY_TIMEOUT) != 0) /* write command */
1076 {
1077 return 1; /* return error */
1078 }
1079
1080 return 0; /* success return 0 */
1081}
1082
1099uint8_t syn6658_save_text(syn6658_handle_t *handle, uint8_t offset, char *text)
1100{
1101 if (handle == NULL) /* check handle */
1102 {
1103 return 2; /* return error */
1104 }
1105 if (handle->inited != 1) /* check handle initialization */
1106 {
1107 return 3; /* return error */
1108 }
1109 if (offset > 15) /* check offset */
1110 {
1111 handle->debug_print("syn6658: offset is invalid, 0<= offset <= 15.\n"); /* offset is invalid */
1112
1113 return 4; /* return error */
1114 }
1115 if ((uint16_t)strlen(text) > (uint16_t)(256 * (16 - offset))) /* check text length */
1116 {
1117 handle->debug_print("syn6658: text length is invalid.\n"); /* text length is invalid */
1118
1119 return 5; /* return error */
1120 }
1121 if (handle->uart_spi != 0) /* check interface */
1122 {
1123 handle->debug_print("syn6658: spi interface can't use this function.\n"); /* spi interface can't use this function */
1124
1125 return 6; /* return error */
1126 }
1127
1128 if (syn6658_set_command_with_arg(handle, 0x31, offset, text, SYN6658_BUSY_TIMEOUT) != 0) /* write command */
1129 {
1130 return 1; /* return error */
1131 }
1132
1133 return 0; /* success return 0 */
1134}
1135
1150uint8_t syn6658_play_text(syn6658_handle_t *handle, uint8_t times, syn6658_type_t type)
1151{
1152 if (handle == NULL) /* check handle */
1153 {
1154 return 2; /* return error */
1155 }
1156 if (handle->inited != 1) /* check handle initialization */
1157 {
1158 return 3; /* return error */
1159 }
1160 if ((times > 15) || (times < 1)) /* check times */
1161 {
1162 handle->debug_print("syn6658: times is invalid, 1<= times <= 15.\n"); /* times is invalid */
1163
1164 return 4; /* return error */
1165 }
1166 if (handle->uart_spi != 0) /* check interface */
1167 {
1168 handle->debug_print("syn6658: spi interface can't use this function.\n"); /* spi interface can't use this function */
1169
1170 return 5; /* return error */
1171 }
1172
1173 if (syn6658_set_command_with_arg(handle, 0x32, (times << 4) | (uint8_t)(type),
1174 (char *)"", SYN6658_BUSY_TIMEOUT) != 0) /* write command */
1175 {
1176 return 1; /* return error */
1177 }
1178
1179 return 0; /* success return 0 */
1180}
1181
1199uint8_t syn6658_set_command_with_arg(syn6658_handle_t *handle, uint8_t command, uint8_t param, char *txt, uint32_t timeout)
1200{
1201 uint8_t res;
1202 uint8_t temp;
1203 uint16_t l;
1204 uint16_t len;
1205
1206 if (handle == NULL) /* check handle */
1207 {
1208 return 2; /* return error */
1209 }
1210 if (handle->inited != 1) /* check handle initialization */
1211 {
1212 return 3; /* return error */
1213 }
1214
1215 len = (uint16_t)strlen(txt); /* get length of txt */
1216 if (len > 4091) /* check result */
1217 {
1218 handle->debug_print("syn6658: txt is too long.\n"); /* txt is too long */
1219
1220 return 4; /* return error */
1221 }
1222 handle->buf[0] = 0xFD; /* frame header */
1223 handle->buf[1] = (uint8_t)((len + 2) / 256); /* length msb */
1224 handle->buf[2] = (len + 2) % 256; /* length lsb */
1225 handle->buf[3] = command; /* command */
1226 handle->buf[4] = param; /* command param */
1227 strncpy((char *)&handle->buf[5], txt, len); /* copy command */
1228 res = a_check_busy(handle, timeout); /* check busy */
1229 if (res != 0) /* check result */
1230 {
1231 handle->debug_print("syn6658: chip is busy.\n"); /* chip is busy */
1232
1233 return 5; /* return error */
1234 }
1235 res = a_write(handle, handle->buf, len + 5); /* write data */
1236 if (res != 0) /* check result */
1237 {
1238 handle->debug_print("syn6658: write failed.\n"); /* write failed */
1239
1240 return 1; /* return error */
1241 }
1242 if (handle->uart_spi == 0) /* uart interface */
1243 {
1244 handle->delay_ms(100); /* delay 100 ms */
1245 }
1246 l = a_read(handle, (uint8_t *)&temp, 1); /* read data */
1247 if (l != 1) /* check result */
1248 {
1249 handle->debug_print("syn6658: uart read failed.\n"); /* uart read failed */
1250
1251 return 6; /* return error */
1252 }
1253 if (temp == 0x41) /* check return */
1254 {
1255 if (handle->uart_spi != 0) /* spi interface */
1256 {
1257 handle->delay_ms(200); /* delay 200 ms */
1258 }
1259
1260 return 0; /* success return 0 */
1261 }
1262 else
1263 {
1264 handle->debug_print("syn6658: command receive failed.\n"); /* command receive failed */
1265
1266 return 6; /* return error */
1267 }
1268}
1269
1284uint8_t syn6658_set_command(syn6658_handle_t *handle, uint8_t command, uint32_t timeout)
1285{
1286 uint8_t res;
1287 uint8_t temp;
1288 uint16_t l;
1289
1290 if (handle == NULL) /* check handle */
1291 {
1292 return 2; /* return error */
1293 }
1294 if (handle->inited != 1) /* check handle initialization */
1295 {
1296 return 3; /* return error */
1297 }
1298
1299 handle->buf[0] = 0xFD; /* frame header */
1300 handle->buf[1] = 0x00; /* length msb */
1301 handle->buf[2] = 0x01; /* length lsb */
1302 handle->buf[3] = command; /* command */
1303 res = a_check_busy(handle, timeout); /* check busy */
1304 if (res != 0) /* check result */
1305 {
1306 handle->debug_print("syn6658: chip is busy.\n"); /* chip is busy */
1307
1308 return 4; /* return error */
1309 }
1310 res = a_write(handle, handle->buf, 4); /* write data */
1311 if (res != 0) /* check result */
1312 {
1313 handle->debug_print("syn6658: write failed.\n"); /* write failed */
1314
1315 return 1; /* return error */
1316 }
1317 if (handle->uart_spi == 0) /* uart interface */
1318 {
1319 handle->delay_ms(100); /* delay 100 ms */
1320 }
1321 l = a_read(handle, (uint8_t *)&temp, 1); /* read data */
1322 if (l != 1) /* check result */
1323 {
1324 handle->debug_print("syn6658: uart read failed.\n"); /* uart read failed */
1325
1326 return 5; /* return error */
1327 }
1328 if (temp == 0x41) /* check return */
1329 {
1330 if (handle->uart_spi != 0) /* spi interface */
1331 {
1332 handle->delay_ms(200); /* delay 200 ms */
1333 }
1334
1335 return 0; /* success return 0 */
1336 }
1337 else
1338 {
1339 handle->debug_print("syn6658: command receive failed.\n"); /* command receive failed */
1340
1341 return 5; /* return error */
1342 }
1343}
1344
1354{
1355 if (info == NULL) /* check handle */
1356 {
1357 return 2; /* return error */
1358 }
1359
1360 memset(info, 0, sizeof(syn6658_info_t)); /* initialize syn6658 info structure */
1361 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
1362 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
1363 strncpy(info->interface, "UART SPI", 16); /* copy interface name */
1364 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
1365 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
1366 info->max_current_ma = MAX_CURRENT; /* set maximum current */
1367 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
1368 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
1369 info->driver_version = DRIVER_VERSION; /* set driver version */
1370
1371 return 0; /* success return 0 */
1372}
#define MAX_CURRENT
#define SUPPLY_VOLTAGE_MAX
#define TEMPERATURE_MAX
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
driver syn6658 header file
struct syn6658_info_s syn6658_info_t
syn6658 information structure definition
uint8_t syn6658_info(syn6658_info_t *info)
get chip's information
uint8_t syn6658_init(syn6658_handle_t *handle)
initialize the chip
uint8_t syn6658_play_text(syn6658_handle_t *handle, uint8_t times, syn6658_type_t type)
play text
uint8_t syn6658_set_synthesis_speaker(syn6658_handle_t *handle, syn6658_speaker_t speaker)
set the synthesis speaker
syn6658_type_t
syn6658 type enumeration definition
uint8_t syn6658_get_text_type(syn6658_handle_t *handle, syn6658_type_t *type)
get the chip text type
uint8_t syn6658_resume(syn6658_handle_t *handle)
resume the chip
uint8_t syn6658_get_status(syn6658_handle_t *handle, syn6658_status_t *status)
get the current status
syn6658_speaker_t
syn6658 speaker enumeration definition
uint8_t syn6658_set_synthesis_speed(syn6658_handle_t *handle, uint8_t speed)
set the synthesis speed
uint8_t syn6658_save_text(syn6658_handle_t *handle, uint8_t offset, char *text)
save text to the buffer
uint8_t syn6658_standby(syn6658_handle_t *handle)
set the chip to standby mode
uint8_t syn6658_set_synthesis_volume(syn6658_handle_t *handle, uint8_t volume)
set the chip synthesis volume
uint8_t syn6658_deinit(syn6658_handle_t *handle)
close the chip
struct syn6658_handle_s syn6658_handle_t
syn6658 handle structure definition
uint8_t syn6658_pause(syn6658_handle_t *handle)
pause the chip
uint8_t syn6658_set_text_type(syn6658_handle_t *handle, syn6658_type_t type)
set the chip text type
uint8_t syn6658_synthesis_text(syn6658_handle_t *handle, const char *const fmt,...)
synthesis the test
syn6658_status_t
syn6658 status enumeration definition
uint8_t syn6658_set_interface(syn6658_handle_t *handle, syn6658_interface_t interface)
set the chip interface
uint8_t syn6658_get_interface(syn6658_handle_t *handle, syn6658_interface_t *interface)
get the chip interface
syn6658_interface_t
syn6658 interface enumeration definition
#define SYN6658_BUSY_TIMEOUT
syn6658 timeout definition
uint8_t syn6658_wake_up(syn6658_handle_t *handle)
wake up the chip
uint8_t syn6658_stop(syn6658_handle_t *handle)
stop the chip
uint8_t syn6658_set_command_with_arg(syn6658_handle_t *handle, uint8_t command, uint8_t param, char *txt, uint32_t timeout)
send the command with arg to the chip
uint8_t syn6658_set_command(syn6658_handle_t *handle, uint8_t command, uint32_t timeout)
send the command to the chip
uint8_t(* uart_flush)(void)
uint8_t(* uart_write)(uint8_t *buf, uint16_t len)
uint8_t(* spi_init)(void)
void(* delay_ms)(uint32_t ms)
uint8_t(* gpio_ready_read)(uint8_t *value)
uint8_t(* uart_deinit)(void)
uint8_t(* gpio_ready_init)(void)
uint8_t buf[4096]
void(* debug_print)(const char *const fmt,...)
uint8_t(* spi_transmit)(uint8_t *tx, uint8_t *rx, uint16_t len)
uint8_t(* spi_deinit)(void)
uint16_t(* uart_read)(uint8_t *buf, uint16_t len)
uint8_t(* gpio_ready_deinit)(void)
uint8_t(* uart_init)(void)
uint32_t driver_version
char manufacturer_name[32]