LibDriver PCA9685  1.0.0
PCA9685 full-featured driver
driver_pca9685.c
Go to the documentation of this file.
1 
37 #include "driver_pca9685.h"
38 #include <math.h>
39 
43 #define CHIP_NAME "NXP PCA9685"
44 #define MANUFACTURER_NAME "NXP"
45 #define SUPPLY_VOLTAGE_MIN 2.3f
46 #define SUPPLY_VOLTAGE_MAX 5.5f
47 #define MAX_CURRENT 400.0f
48 #define TEMPERATURE_MIN -40.0f
49 #define TEMPERATURE_MAX 85.0f
50 #define DRIVER_VERSION 1000
55 #define PCA9685_REG_MODE1 0x00
56 #define PCA9685_REG_MODE2 0x01
57 #define PCA9685_REG_SUBADR1 0x02
58 #define PCA9685_REG_SUBADR2 0x03
59 #define PCA9685_REG_SUBADR3 0x04
60 #define PCA9685_REG_ALLCALLADR 0x05
61 #define PCA9685_REG_LED0_ON_L 0x06
62 #define PCA9685_REG_LED0_ON_H 0x07
63 #define PCA9685_REG_LED0_OFF_L 0x08
64 #define PCA9685_REG_LED0_OFF_H 0x09
65 #define PCA9685_REG_ALL_LED_ON_L 0xFA
66 #define PCA9685_REG_ALL_LED_ON_H 0xFB
67 #define PCA9685_REG_ALL_LED_OFF_L 0xFC
68 #define PCA9685_REG_ALL_LED_OFF_H 0xFD
69 #define PCA9685_REG_PRE_SCALE 0xFE
81 {
82  if (handle == NULL) /* check handle */
83  {
84  return 2; /* return error */
85  }
86 
87  handle->iic_addr = 0x80; /* set 0x80*/
88  handle->iic_addr |= addr_pin << 1; /* set iic addr */
89 
90  return 0; /* success return 0 */
91 }
92 
103 {
104  if (handle == NULL) /* check handle */
105  {
106  return 2; /* return error */
107  }
108 
109  *addr_pin = (pca9685_address_t)((handle->iic_addr & (~0x80)) >> 1); /*get iic address */
110 
111  return 0; /* success return 0 */
112 }
113 
123 uint8_t pca9685_set_addr(pca9685_handle_t *handle, uint8_t addr)
124 {
125  if (handle == NULL) /* check handle */
126  {
127  return 2; /* return error */
128  }
129 
130  handle->iic_addr = addr; /* set iic addr */
131 
132  return 0; /* success return 0 */
133 }
134 
144 uint8_t pca9685_get_addr(pca9685_handle_t *handle, uint8_t *addr)
145 {
146  if (handle == NULL) /* check handle */
147  {
148  return 2; /* return error */
149  }
150 
151  *addr = handle->iic_addr; /* get iic addr */
152 
153  return 0; /* success return 0 */
154 }
155 
168 {
169  if (handle == NULL) /* check handle */
170  {
171  return 2; /* return error */
172  }
173  if (handle->inited != 1) /* check handle initialization */
174  {
175  return 3; /* return error */
176  }
177 
178  if (handle->oe_gpio_write(!enable) != 0) /* gpio write */
179  {
180  handle->debug_print("pcf8591: gpio write failed.\n"); /* gpio writer failed */
181 
182  return 1; /* return error */
183  }
184 
185  return 0; /* success return 0 */
186 }
187 
200 {
201  uint8_t res;
202  uint8_t prev;
203 
204  if (handle == NULL) /* check handle */
205  {
206  return 2; /* return error */
207  }
208  if (handle->inited != 1) /* check handle initialization */
209  {
210  return 3; /* return error */
211  }
212 
213  res = handle->iic_read(handle->iic_addr, PCA9685_REG_MODE1, (uint8_t *)&prev, 1); /* read mode 1 register */
214  if (res != 0) /* check result */
215  {
216  handle->debug_print("pcf8591: read mode 1 register failed.\n"); /* read mode 1 register failed */
217 
218  return 1; /* return error */
219  }
220  prev &= ~(1 << 7); /* clear conf */
221  prev |= enable << 7; /* set conf */
222  res = handle->iic_write(handle->iic_addr, PCA9685_REG_MODE1, (uint8_t *)&prev, 1); /* write mode 1 register */
223  if (res != 0) /* check result */
224  {
225  handle->debug_print("pcf8591: write mode 1 register failed.\n"); /* write mode 1 register failed */
226 
227  return 1; /* return error */
228  }
229 
230  return 0; /* success return 0 */
231 }
232 
245 {
246  uint8_t res;
247  uint8_t prev;
248 
249  if (handle == NULL) /* check handle */
250  {
251  return 2; /* return error */
252  }
253  if (handle->inited != 1) /* check handle initialization */
254  {
255  return 3; /* return error */
256  }
257 
258  res = handle->iic_read(handle->iic_addr, PCA9685_REG_MODE1, (uint8_t *)&prev, 1); /* read mode 1 register */
259  if (res != 0) /* check result */
260  {
261  handle->debug_print("pcf8591: read mode 1 register failed.\n"); /* read mode 1 register failed */
262 
263  return 1; /* return error */
264  }
265  *enable = (pca9685_bool_t)((prev >> 7) & 0x01); /* get bool */
266 
267  return 0; /* success return 0 */
268 }
269 
282 {
283  uint8_t res;
284  uint8_t prev;
285 
286  if (handle == NULL) /* check handle */
287  {
288  return 2; /* return error */
289  }
290  if (handle->inited != 1) /* check handle initialization */
291  {
292  return 3; /* return error */
293  }
294 
295  res = handle->iic_read(handle->iic_addr, PCA9685_REG_MODE1, (uint8_t *)&prev, 1); /* read mode 1 register */
296  if (res != 0) /* check result */
297  {
298  handle->debug_print("pcf8591: read mode 1 register failed.\n"); /* read mode 1 register failed */
299 
300  return 1; /* return error */
301  }
302  prev &= ~(1 << 6); /* clear conf */
303  prev |= enable << 6; /* set conf */
304  res = handle->iic_write(handle->iic_addr, PCA9685_REG_MODE1, (uint8_t *)&prev, 1); /* write mode 1 register */
305  if (res != 0) /* check result */
306  {
307  handle->debug_print("pcf8591: write mode 1 register failed.\n"); /* write mode 1 register failed */
308 
309  return 1; /* return error */
310  }
311 
312  return 0; /* success return 0 */
313 }
314 
327 {
328  uint8_t res;
329  uint8_t prev;
330 
331  if (handle == NULL) /* check handle */
332  {
333  return 2; /* return error */
334  }
335  if (handle->inited != 1) /* check handle initialization */
336  {
337  return 3; /* return error */
338  }
339 
340  res = handle->iic_read(handle->iic_addr, PCA9685_REG_MODE1, (uint8_t *)&prev, 1); /* read mode 1 register */
341  if (res != 0) /* check result */
342  {
343  handle->debug_print("pcf8591: read mode 1 register failed.\n"); /* read mode 1 register failed */
344 
345  return 1; /* return error */
346  }
347  *enable = (pca9685_bool_t)((prev >> 6) & 0x01); /* get bool */
348 
349  return 0; /* success return 0 */
350 }
351 
364 {
365  uint8_t res;
366  uint8_t prev;
367 
368  if (handle == NULL) /* check handle */
369  {
370  return 2; /* return error */
371  }
372  if (handle->inited != 1) /* check handle initialization */
373  {
374  return 3; /* return error */
375  }
376 
377  res = handle->iic_read(handle->iic_addr, PCA9685_REG_MODE1, (uint8_t *)&prev, 1); /* read mode 1 register */
378  if (res != 0) /* check result */
379  {
380  handle->debug_print("pcf8591: read mode 1 register failed.\n"); /* read mode 1 register failed */
381 
382  return 1; /* return error */
383  }
384  prev &= ~(1 << 5); /* clear conf */
385  prev |= enable << 5; /* set conf */
386  res = handle->iic_write(handle->iic_addr, PCA9685_REG_MODE1, (uint8_t *)&prev, 1); /* write mode 1 register */
387  if (res != 0) /* check result */
388  {
389  handle->debug_print("pcf8591: write mode 1 register failed.\n"); /* write mode 1 register failed */
390 
391  return 1; /* return error */
392  }
393 
394  return 0; /* success return 0 */
395 }
396 
409 {
410  uint8_t res;
411  uint8_t prev;
412 
413  if (handle == NULL) /* check handle */
414  {
415  return 2; /* return error */
416  }
417  if (handle->inited != 1) /* check handle initialization */
418  {
419  return 3; /* return error */
420  }
421 
422  res = handle->iic_read(handle->iic_addr, PCA9685_REG_MODE1, (uint8_t *)&prev, 1); /* read mode 1 register */
423  if (res != 0) /* check result */
424  {
425  handle->debug_print("pcf8591: read mode 1 register failed.\n"); /* read mode 1 register failed */
426 
427  return 1; /* return error */
428  }
429  *enable = (pca9685_bool_t)((prev >> 5) & 0x01); /* get bool */
430 
431  return 0; /* success return 0 */
432 }
433 
446 {
447  uint8_t res;
448  uint8_t prev;
449 
450  if (handle == NULL) /* check handle */
451  {
452  return 2; /* return error */
453  }
454  if (handle->inited != 1) /* check handle initialization */
455  {
456  return 3; /* return error */
457  }
458 
459  res = handle->iic_read(handle->iic_addr, PCA9685_REG_MODE1, (uint8_t *)&prev, 1); /* read mode 1 register */
460  if (res != 0) /* check result */
461  {
462  handle->debug_print("pcf8591: read mode 1 register failed.\n"); /* read mode 1 register failed */
463 
464  return 1; /* return error */
465  }
466  prev &= ~(1 << 4); /* clear conf */
467  prev |= enable << 4; /* set conf */
468  res = handle->iic_write(handle->iic_addr, PCA9685_REG_MODE1, (uint8_t *)&prev, 1); /* write mode 1 register */
469  if (res != 0) /* check result */
470  {
471  handle->debug_print("pcf8591: write mode 1 register failed.\n"); /* write mode 1 register failed */
472 
473  return 1; /* return error */
474  }
475 
476  return 0; /* success return 0 */
477 }
478 
491 {
492  uint8_t res;
493  uint8_t prev;
494 
495  if (handle == NULL) /* check handle */
496  {
497  return 2; /* return error */
498  }
499  if (handle->inited != 1) /* check handle initialization */
500  {
501  return 3; /* return error */
502  }
503 
504  res = handle->iic_read(handle->iic_addr, PCA9685_REG_MODE1, (uint8_t *)&prev, 1); /* read mode 1 register */
505  if (res != 0) /* check result */
506  {
507  handle->debug_print("pcf8591: read mode 1 register failed.\n"); /* read mode 1 register failed */
508 
509  return 1; /* return error */
510  }
511  *enable = (pca9685_bool_t)((prev >> 4) & 0x01); /* get bool */
512 
513  return 0; /* success return 0 */
514 }
515 
528 {
529  uint8_t res;
530  uint8_t prev;
531 
532  if (handle == NULL) /* check handle */
533  {
534  return 2; /* return error */
535  }
536  if (handle->inited != 1) /* check handle initialization */
537  {
538  return 3; /* return error */
539  }
540 
541  res = handle->iic_read(handle->iic_addr, PCA9685_REG_MODE1, (uint8_t *)&prev, 1); /* read mode 1 register */
542  if (res != 0) /* check result */
543  {
544  handle->debug_print("pcf8591: read mode 1 register failed.\n"); /* read mode 1 register failed */
545 
546  return 1; /* return error */
547  }
548  prev &= ~(1 << 3); /* clear conf */
549  prev |= enable << 3; /* set conf */
550  res = handle->iic_write(handle->iic_addr, PCA9685_REG_MODE1, (uint8_t *)&prev, 1); /* write mode 1 register */
551  if (res != 0) /* check result */
552  {
553  handle->debug_print("pcf8591: write mode 1 register failed.\n"); /* write mode 1 register failed */
554 
555  return 1; /* return error */
556  }
557 
558  return 0; /* success return 0 */
559 }
560 
573 {
574  uint8_t res;
575  uint8_t prev;
576 
577  if (handle == NULL) /* check handle */
578  {
579  return 2; /* return error */
580  }
581  if (handle->inited != 1) /* check handle initialization */
582  {
583  return 3; /* return error */
584  }
585 
586  res = handle->iic_read(handle->iic_addr, PCA9685_REG_MODE1, (uint8_t *)&prev, 1); /* read mode 1 register */
587  if (res != 0) /* check result */
588  {
589  handle->debug_print("pcf8591: read mode 1 register failed.\n"); /* read mode 1 register failed */
590 
591  return 1; /* return error */
592  }
593  *enable = (pca9685_bool_t)((prev >> 3) & 0x01); /* get bool */
594 
595  return 0; /* success return 0 */
596 }
597 
610 {
611  uint8_t res;
612  uint8_t prev;
613 
614  if (handle == NULL) /* check handle */
615  {
616  return 2; /* return error */
617  }
618  if (handle->inited != 1) /* check handle initialization */
619  {
620  return 3; /* return error */
621  }
622 
623  res = handle->iic_read(handle->iic_addr, PCA9685_REG_MODE1, (uint8_t *)&prev, 1); /* read mode 1 register */
624  if (res != 0) /* check result */
625  {
626  handle->debug_print("pcf8591: read mode 1 register failed.\n"); /* read mode 1 register failed */
627 
628  return 1; /* return error */
629  }
630  prev &= ~(1 << 2); /* clear conf */
631  prev |= enable << 2; /* set conf */
632  res = handle->iic_write(handle->iic_addr, PCA9685_REG_MODE1, (uint8_t *)&prev, 1); /* write mode 1 register */
633  if (res != 0) /* check result */
634  {
635  handle->debug_print("pcf8591: write mode 1 register failed.\n"); /* write mode 1 register failed */
636 
637  return 1; /* return error */
638  }
639 
640  return 0; /* success return 0 */
641 }
642 
655 {
656  uint8_t res;
657  uint8_t prev;
658 
659  if (handle == NULL) /* check handle */
660  {
661  return 2; /* return error */
662  }
663  if (handle->inited != 1) /* check handle initialization */
664  {
665  return 3; /* return error */
666  }
667 
668  res = handle->iic_read(handle->iic_addr, PCA9685_REG_MODE1, (uint8_t *)&prev, 1); /* read mode 1 register */
669  if (res != 0) /* check result */
670  {
671  handle->debug_print("pcf8591: read mode 1 register failed.\n"); /* read mode 1 register failed */
672 
673  return 1; /* return error */
674  }
675  *enable = (pca9685_bool_t)((prev >> 2) & 0x01); /* get bool */
676 
677  return 0; /* success return 0 */
678 }
679 
692 {
693  uint8_t res;
694  uint8_t prev;
695 
696  if (handle == NULL) /* check handle */
697  {
698  return 2; /* return error */
699  }
700  if (handle->inited != 1) /* check handle initialization */
701  {
702  return 3; /* return error */
703  }
704 
705  res = handle->iic_read(handle->iic_addr, PCA9685_REG_MODE1, (uint8_t *)&prev, 1); /* read mode 1 register */
706  if (res != 0) /* check result */
707  {
708  handle->debug_print("pcf8591: read mode 1 register failed.\n"); /* read mode 1 register failed */
709 
710  return 1; /* return error */
711  }
712  prev &= ~(1 << 1); /* clear conf */
713  prev |= enable << 1; /* set conf */
714  res = handle->iic_write(handle->iic_addr, PCA9685_REG_MODE1, (uint8_t *)&prev, 1); /* write mode 1 register */
715  if (res != 0) /* check result */
716  {
717  handle->debug_print("pcf8591: write mode 1 register failed.\n"); /* write mode 1 register failed */
718 
719  return 1; /* return error */
720  }
721 
722  return 0; /* success return 0 */
723 }
724 
737 {
738  uint8_t res;
739  uint8_t prev;
740 
741  if (handle == NULL) /* check handle */
742  {
743  return 2; /* return error */
744  }
745  if (handle->inited != 1) /* check handle initialization */
746  {
747  return 3; /* return error */
748  }
749 
750  res = handle->iic_read(handle->iic_addr, PCA9685_REG_MODE1, (uint8_t *)&prev, 1); /* read mode 1 register */
751  if (res != 0) /* check result */
752  {
753  handle->debug_print("pcf8591: read mode 1 register failed.\n"); /* read mode 1 register failed */
754 
755  return 1; /* return error */
756  }
757  *enable = (pca9685_bool_t)((prev >> 1) & 0x01); /* get bool */
758 
759  return 0; /* success return 0 */
760 }
761 
774 {
775  uint8_t res;
776  uint8_t prev;
777 
778  if (handle == NULL) /* check handle */
779  {
780  return 2; /* return error */
781  }
782  if (handle->inited != 1) /* check handle initialization */
783  {
784  return 3; /* return error */
785  }
786 
787  res = handle->iic_read(handle->iic_addr, PCA9685_REG_MODE1, (uint8_t *)&prev, 1); /* read mode 1 register */
788  if (res != 0) /* check result */
789  {
790  handle->debug_print("pcf8591: read mode 1 register failed.\n"); /* read mode 1 register failed */
791 
792  return 1; /* return error */
793  }
794  prev &= ~(1 << 0); /* clear conf */
795  prev |= enable << 0; /* set conf */
796  res = handle->iic_write(handle->iic_addr, PCA9685_REG_MODE1, (uint8_t *)&prev, 1); /* write mode 1 register */
797  if (res != 0) /* check result */
798  {
799  handle->debug_print("pcf8591: write mode 1 register failed.\n"); /* write mode 1 register failed */
800 
801  return 1; /* return error */
802  }
803 
804  return 0; /* success return 0 */
805 }
806 
819 {
820  uint8_t res;
821  uint8_t prev;
822 
823  if (handle == NULL) /* check handle */
824  {
825  return 2; /* return error */
826  }
827  if (handle->inited != 1) /* check handle initialization */
828  {
829  return 3; /* return error */
830  }
831 
832  res = handle->iic_read(handle->iic_addr, PCA9685_REG_MODE1, (uint8_t *)&prev, 1); /* read mode 1 register */
833  if (res != 0) /* check result */
834  {
835  handle->debug_print("pcf8591: read mode 1 register failed.\n"); /* read mode 1 register failed */
836 
837  return 1; /* return error */
838  }
839  *enable = (pca9685_bool_t)((prev >> 0) & 0x01); /* get bool */
840 
841  return 0; /* success return 0 */
842 }
843 
856 {
857  uint8_t res;
858  uint8_t prev;
859 
860  if (handle == NULL) /* check handle */
861  {
862  return 2; /* return error */
863  }
864  if (handle->inited != 1) /* check handle initialization */
865  {
866  return 3; /* return error */
867  }
868 
869  res = handle->iic_read(handle->iic_addr, PCA9685_REG_MODE2, (uint8_t *)&prev, 1); /* read mode 2 register */
870  if (res != 0) /* check result */
871  {
872  handle->debug_print("pcf8591: read mode 2 register failed.\n"); /* read mode 2 register failed */
873 
874  return 1; /* return error */
875  }
876  prev &= ~(1 << 4); /* clear conf */
877  prev |= enable << 4; /* set conf */
878  res = handle->iic_write(handle->iic_addr, PCA9685_REG_MODE2, (uint8_t *)&prev, 1); /* write mode 2 register */
879  if (res != 0) /* check result */
880  {
881  handle->debug_print("pcf8591: write mode 2 register failed.\n"); /* write mode 2 register failed */
882 
883  return 1; /* return error */
884  }
885 
886  return 0; /* success return 0 */
887 }
888 
901 {
902  uint8_t res;
903  uint8_t prev;
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  res = handle->iic_read(handle->iic_addr, PCA9685_REG_MODE2, (uint8_t *)&prev, 1); /* read mode 2 register */
915  if (res != 0) /* check result */
916  {
917  handle->debug_print("pcf8591: read mode 2 register failed.\n"); /* read mode 2 register failed */
918 
919  return 1; /* return error */
920  }
921  *enable = (pca9685_bool_t)((prev >> 4) & 0x01); /* get bool */
922 
923  return 0; /* success return 0 */
924 }
925 
938 {
939  uint8_t res;
940  uint8_t prev;
941 
942  if (handle == NULL) /* check handle */
943  {
944  return 2; /* return error */
945  }
946  if (handle->inited != 1) /* check handle initialization */
947  {
948  return 3; /* return error */
949  }
950 
951  res = handle->iic_read(handle->iic_addr, PCA9685_REG_MODE2, (uint8_t *)&prev, 1); /* read mode 2 register */
952  if (res != 0) /* check result */
953  {
954  handle->debug_print("pcf8591: read mode 2 register failed.\n"); /* read mode 2 register failed */
955 
956  return 1; /* return error */
957  }
958  prev &= ~(1 << 3); /* clear conf */
959  prev |= change << 3; /* set conf */
960  res = handle->iic_write(handle->iic_addr, PCA9685_REG_MODE2, (uint8_t *)&prev, 1); /* write mode 2 register */
961  if (res != 0) /* check result */
962  {
963  handle->debug_print("pcf8591: write mode 2 register failed.\n"); /* write mode 2 register failed */
964 
965  return 1; /* return error */
966  }
967 
968  return 0; /* success return 0 */
969 }
970 
983 {
984  uint8_t res;
985  uint8_t prev;
986 
987  if (handle == NULL) /* check handle */
988  {
989  return 2; /* return error */
990  }
991  if (handle->inited != 1) /* check handle initialization */
992  {
993  return 3; /* return error */
994  }
995 
996  res = handle->iic_read(handle->iic_addr, PCA9685_REG_MODE2, (uint8_t *)&prev, 1); /* read mode 2 register */
997  if (res != 0) /* check result */
998  {
999  handle->debug_print("pcf8591: read mode 2 register failed.\n"); /* read mode 2 register failed */
1000 
1001  return 1; /* return error */
1002  }
1003  *change = (pca9685_output_change_t)((prev >> 3) & 0x01); /* get change */
1004 
1005  return 0; /* success return 0 */
1006 }
1007 
1020 {
1021  uint8_t res;
1022  uint8_t prev;
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 
1033  res = handle->iic_read(handle->iic_addr, PCA9685_REG_MODE2, (uint8_t *)&prev, 1); /* read mode 2 register */
1034  if (res != 0) /* check result */
1035  {
1036  handle->debug_print("pcf8591: read mode 2 register failed.\n"); /* read mode 2 register failed */
1037 
1038  return 1; /* return error */
1039  }
1040  prev &= ~(1 << 2); /* clear conf */
1041  prev |= driver << 2; /* set conf */
1042  res = handle->iic_write(handle->iic_addr, PCA9685_REG_MODE2, (uint8_t *)&prev, 1); /* write mode 2 register */
1043  if (res != 0) /* check result */
1044  {
1045  handle->debug_print("pcf8591: write mode 2 register failed.\n"); /* write mode 2 register failed */
1046 
1047  return 1; /* return error */
1048  }
1049 
1050  return 0; /* success return 0 */
1051 }
1052 
1065 {
1066  uint8_t res;
1067  uint8_t prev;
1068 
1069  if (handle == NULL) /* check handle */
1070  {
1071  return 2; /* return error */
1072  }
1073  if (handle->inited != 1) /* check handle initialization */
1074  {
1075  return 3; /* return error */
1076  }
1077 
1078  res = handle->iic_read(handle->iic_addr, PCA9685_REG_MODE2, (uint8_t *)&prev, 1); /* read mode 2 register */
1079  if (res != 0) /* check result */
1080  {
1081  handle->debug_print("pcf8591: read mode 2 register failed.\n"); /* read mode 2 register failed */
1082 
1083  return 1; /* return error */
1084  }
1085  *driver = (pca9685_output_driver_t)((prev >> 2) & 0x01); /* get change */
1086 
1087  return 0; /* success return 0 */
1088 }
1089 
1102 {
1103  uint8_t res;
1104  uint8_t prev;
1105 
1106  if (handle == NULL) /* check handle */
1107  {
1108  return 2; /* return error */
1109  }
1110  if (handle->inited != 1) /* check handle initialization */
1111  {
1112  return 3; /* return error */
1113  }
1114 
1115  res = handle->iic_read(handle->iic_addr, PCA9685_REG_MODE2, (uint8_t *)&prev, 1); /* read mode 2 register */
1116  if (res != 0) /* check result */
1117  {
1118  handle->debug_print("pcf8591: read mode 2 register failed.\n"); /* read mode 2 register failed */
1119 
1120  return 1; /* return error */
1121  }
1122  prev &= ~(3 << 0); /* clear conf */
1123  prev |= type << 0; /* set conf */
1124  res = handle->iic_write(handle->iic_addr, PCA9685_REG_MODE2, (uint8_t *)&prev, 1); /* write mode 2 register */
1125  if (res != 0) /* check result */
1126  {
1127  handle->debug_print("pcf8591: write mode 2 register failed.\n"); /* write mode 2 register failed */
1128 
1129  return 1; /* return error */
1130  }
1131 
1132  return 0; /* success return 0 */
1133 }
1134 
1147 {
1148  uint8_t res;
1149  uint8_t prev;
1150 
1151  if (handle == NULL) /* check handle */
1152  {
1153  return 2; /* return error */
1154  }
1155  if (handle->inited != 1) /* check handle initialization */
1156  {
1157  return 3; /* return error */
1158  }
1159 
1160  res = handle->iic_read(handle->iic_addr, PCA9685_REG_MODE2, (uint8_t *)&prev, 1); /* read mode 2 register */
1161  if (res != 0) /* check result */
1162  {
1163  handle->debug_print("pcf8591: read mode 2 register failed.\n"); /* read mode 2 register failed */
1164 
1165  return 1; /* return error */
1166  }
1167  *type = (pca9685_output_disable_type_t)((prev >> 0) & (0x3)); /* get type */
1168 
1169  return 0; /* success return 0 */
1170 }
1171 
1183 uint8_t pca9685_set_subaddress_1(pca9685_handle_t *handle, uint8_t addr)
1184 {
1185  uint8_t res;
1186 
1187  if (handle == NULL) /* check handle */
1188  {
1189  return 2; /* return error */
1190  }
1191  if (handle->inited != 1) /* check handle initialization */
1192  {
1193  return 3; /* return error */
1194  }
1195 
1196  res = handle->iic_write(handle->iic_addr, PCA9685_REG_SUBADR1, (uint8_t *)&addr, 1); /* write sub address 1 register */
1197  if (res != 0) /* check result */
1198  {
1199  handle->debug_print("pcf8591: write sub address 1 register failed.\n"); /* write sub address 1 register failed */
1200 
1201  return 1; /* return error */
1202  }
1203 
1204  return 0; /* success return 0 */
1205 }
1206 
1218 uint8_t pca9685_get_subaddress_1(pca9685_handle_t *handle, uint8_t *addr)
1219 {
1220  uint8_t res;
1221 
1222  if (handle == NULL) /* check handle */
1223  {
1224  return 2; /* return error */
1225  }
1226  if (handle->inited != 1) /* check handle initialization */
1227  {
1228  return 3; /* return error */
1229  }
1230 
1231  res = handle->iic_read(handle->iic_addr, PCA9685_REG_SUBADR1, (uint8_t *)addr, 1); /* read sub address 1 register */
1232  if (res != 0) /* check result */
1233  {
1234  handle->debug_print("pcf8591: read sub address 1 register failed.\n"); /* read sub address 1 register failed */
1235 
1236  return 1; /* return error */
1237  }
1238 
1239  return 0; /* success return 0 */
1240 }
1241 
1253 uint8_t pca9685_set_subaddress_2(pca9685_handle_t *handle, uint8_t addr)
1254 {
1255  uint8_t res;
1256 
1257  if (handle == NULL) /* check handle */
1258  {
1259  return 2; /* return error */
1260  }
1261  if (handle->inited != 1) /* check handle initialization */
1262  {
1263  return 3; /* return error */
1264  }
1265 
1266  res = handle->iic_write(handle->iic_addr, PCA9685_REG_SUBADR2, (uint8_t *)&addr, 1); /* write sub address 2 register */
1267  if (res != 0) /* check result */
1268  {
1269  handle->debug_print("pcf8591: write sub address 2 register failed.\n"); /* write sub address 2 register failed */
1270 
1271  return 1; /* return error */
1272  }
1273 
1274  return 0; /* success return 0 */
1275 }
1276 
1288 uint8_t pca9685_get_subaddress_2(pca9685_handle_t *handle, uint8_t *addr)
1289 {
1290  uint8_t res;
1291 
1292  if (handle == NULL) /* check handle */
1293  {
1294  return 2; /* return error */
1295  }
1296  if (handle->inited != 1) /* check handle initialization */
1297  {
1298  return 3; /* return error */
1299  }
1300 
1301  res = handle->iic_read(handle->iic_addr, PCA9685_REG_SUBADR2, (uint8_t *)addr, 1); /* read sub address 2 register */
1302  if (res != 0) /* check result */
1303  {
1304  handle->debug_print("pcf8591: read sub address 2 register failed.\n"); /* read sub address 2 register failed */
1305 
1306  return 1; /* return error */
1307  }
1308 
1309  return 0; /* success return 0 */
1310 }
1311 
1323 uint8_t pca9685_set_subaddress_3(pca9685_handle_t *handle, uint8_t addr)
1324 {
1325  uint8_t res;
1326 
1327  if (handle == NULL) /* check handle */
1328  {
1329  return 2; /* return error */
1330  }
1331  if (handle->inited != 1) /* check handle initialization */
1332  {
1333  return 3; /* return error */
1334  }
1335 
1336  res = handle->iic_write(handle->iic_addr, PCA9685_REG_SUBADR3, (uint8_t *)&addr, 1); /* write sub address 3 register */
1337  if (res != 0) /* check result */
1338  {
1339  handle->debug_print("pcf8591: write sub address 3 register failed.\n"); /* write sub address 3 register failed */
1340 
1341  return 1; /* return error */
1342  }
1343 
1344  return 0; /* success return 0 */
1345 }
1346 
1358 uint8_t pca9685_get_subaddress_3(pca9685_handle_t *handle, uint8_t *addr)
1359 {
1360  uint8_t res;
1361 
1362  if (handle == NULL) /* check handle */
1363  {
1364  return 2; /* return error */
1365  }
1366  if (handle->inited != 1) /* check handle initialization */
1367  {
1368  return 3; /* return error */
1369  }
1370 
1371  res = handle->iic_read(handle->iic_addr, PCA9685_REG_SUBADR3, (uint8_t *)addr, 1); /* read sub address 3 register */
1372  if (res != 0) /* check result */
1373  {
1374  handle->debug_print("pcf8591: read sub address 3 register failed.\n"); /* read sub address 3 register failed */
1375 
1376  return 1; /* return error */
1377  }
1378 
1379  return 0; /* success return 0 */
1380 }
1381 
1393 uint8_t pca9685_set_all_call_address(pca9685_handle_t *handle, uint8_t addr)
1394 {
1395  uint8_t res;
1396 
1397  if (handle == NULL) /* check handle */
1398  {
1399  return 2; /* return error */
1400  }
1401  if (handle->inited != 1) /* check handle initialization */
1402  {
1403  return 3; /* return error */
1404  }
1405 
1406  res = handle->iic_write(handle->iic_addr, PCA9685_REG_ALLCALLADR, (uint8_t *)&addr, 1); /* write all call address register */
1407  if (res != 0) /* check result */
1408  {
1409  handle->debug_print("pcf8591: write all call address register failed.\n"); /* write all call address register failed */
1410 
1411  return 1; /* return error */
1412  }
1413 
1414  return 0; /* success return 0 */
1415 }
1416 
1428 uint8_t pca9685_get_all_call_address(pca9685_handle_t *handle, uint8_t *addr)
1429 {
1430  uint8_t res;
1431 
1432  if (handle == NULL) /* check handle */
1433  {
1434  return 2; /* return error */
1435  }
1436  if (handle->inited != 1) /* check handle initialization */
1437  {
1438  return 3; /* return error */
1439  }
1440 
1441  res = handle->iic_read(handle->iic_addr, PCA9685_REG_ALLCALLADR, (uint8_t *)addr, 1); /* read all call address register */
1442  if (res != 0) /* check result */
1443  {
1444  handle->debug_print("pcf8591: read all call address register failed.\n"); /* read all call address register failed */
1445 
1446  return 1; /* return error */
1447  }
1448 
1449  return 0; /* success return 0 */
1450 }
1451 
1466 uint8_t pca9685_write_channel(pca9685_handle_t *handle, pca9685_channel_t channel, uint16_t on_count, uint16_t off_count)
1467 {
1468  uint8_t res;
1469  uint8_t buf[4];
1470 
1471  if (handle == NULL) /* check handle */
1472  {
1473  return 2; /* return error */
1474  }
1475  if (handle->inited != 1) /* check handle initialization */
1476  {
1477  return 3; /* return error */
1478  }
1479  if ((on_count > 4096) || (off_count > 4096)) /* check result */
1480  {
1481  handle->debug_print("pcf8591: on_count or off_count is over 4096.\n"); /* on_count or off_count is over 4096 */
1482 
1483  return 4; /* return error */
1484  }
1485 
1486  buf[0] = (on_count >> 0) & 0xFF; /* set off count lsb */
1487  buf[1] = (on_count >> 8) & 0x1F; /* set on count msb */
1488  buf[2] = (off_count >> 0) & 0xFF; /* set off count lsb */
1489  buf[3] = (off_count >> 8) & 0x1F; /* set off count msb */
1490  res = handle->iic_write(handle->iic_addr, (uint8_t)(PCA9685_REG_LED0_ON_L + channel * 4),
1491  (uint8_t *)buf, 4); /* write led register */
1492  if (res != 0) /* check result */
1493  {
1494  handle->debug_print("pcf8591: write led register failed.\n"); /* write led register failed */
1495 
1496  return 1; /* return error */
1497  }
1498 
1499  return 0; /* success return 0 */
1500 }
1501 
1515 uint8_t pca9685_read_channel(pca9685_handle_t *handle, pca9685_channel_t channel, uint16_t *on_count, uint16_t *off_count)
1516 {
1517  uint8_t res;
1518  uint8_t buf[4];
1519 
1520  if (handle == NULL) /* check handle */
1521  {
1522  return 2; /* return error */
1523  }
1524  if (handle->inited != 1) /* check handle initialization */
1525  {
1526  return 3; /* return error */
1527  }
1528 
1529  memset(buf, 0, sizeof(uint8_t) * 4); /* clear the buffer */
1530  res = handle->iic_read(handle->iic_addr, (uint8_t)(PCA9685_REG_LED0_ON_L + channel * 4),
1531  (uint8_t *)buf, 4); /* read led register */
1532  if (res != 0) /* check result */
1533  {
1534  handle->debug_print("pcf8591: read led register failed.\n"); /* read led register failed */
1535 
1536  return 1; /* return error */
1537  }
1538  *on_count = ((uint16_t)(buf[1]) & 0x1F) << 8 | buf[0]; /* set on count */
1539  *off_count = ((uint16_t)(buf[3]) & 0x1F) << 8 | buf[2]; /* set off count */
1540 
1541  return 0; /* success return 0 */
1542 }
1543 
1557 uint8_t pca9685_write_all_channel(pca9685_handle_t *handle, uint16_t on_count, uint16_t off_count)
1558 {
1559  uint8_t res;
1560  uint8_t buf[4];
1561 
1562  if (handle == NULL) /* check handle */
1563  {
1564  return 2; /* return error */
1565  }
1566  if (handle->inited != 1) /* check handle initialization */
1567  {
1568  return 3; /* return error */
1569  }
1570  if ((on_count > 4096) || (off_count > 4096)) /* check result */
1571  {
1572  handle->debug_print("pcf8591: on_count or off_count is over 4096.\n"); /* on_count or off_count is over 4096 */
1573 
1574  return 4; /* return error */
1575  }
1576 
1577  buf[0] = (on_count >> 0) & 0xFF; /* set off count lsb */
1578  buf[1] = (on_count >> 8) & 0x1F; /* set on count msb */
1579  buf[2] = (off_count >> 0) & 0xFF; /* set off count lsb */
1580  buf[3] = (off_count >> 8) & 0x1F; /* set off count msb */
1581  res = handle->iic_write(handle->iic_addr, PCA9685_REG_ALL_LED_ON_L, (uint8_t *)buf, 4); /* write all led register */
1582  if (res != 0) /* check result */
1583  {
1584  handle->debug_print("pcf8591: write all led register failed.\n"); /* write all led register failed */
1585 
1586  return 1; /* return error */
1587  }
1588 
1589  return 0; /* success return 0 */
1590 }
1591 
1604 uint8_t pca9685_set_prescaler(pca9685_handle_t *handle, uint8_t prescaler)
1605 {
1606  uint8_t res;
1607 
1608  if (handle == NULL) /* check handle */
1609  {
1610  return 2; /* return error */
1611  }
1612  if (handle->inited != 1) /* check handle initialization */
1613  {
1614  return 3; /* return error */
1615  }
1616  if (prescaler < 3) /* check result */
1617  {
1618  handle->debug_print("pcf8591: pre scale must be >= 3.\n"); /* pre scale must be >= 3 */
1619 
1620  return 4; /* return error */
1621  }
1622 
1623  res = handle->iic_write(handle->iic_addr, PCA9685_REG_PRE_SCALE, (uint8_t *)&prescaler, 1); /* write pre scale register */
1624  if (res != 0) /* check result */
1625  {
1626  handle->debug_print("pcf8591: write pre scale register failed.\n"); /* write pre scale register failed */
1627 
1628  return 1; /* return error */
1629  }
1630 
1631  return 0; /* success return 0 */
1632 }
1633 
1645 uint8_t pca9685_get_prescaler(pca9685_handle_t *handle, uint8_t *prescaler)
1646 {
1647  uint8_t res;
1648 
1649  if (handle == NULL) /* check handle */
1650  {
1651  return 2; /* return error */
1652  }
1653  if (handle->inited != 1) /* check handle initialization */
1654  {
1655  return 3; /* return error */
1656  }
1657 
1658  res = handle->iic_read(handle->iic_addr, PCA9685_REG_PRE_SCALE, (uint8_t *)prescaler, 1); /* read pre scale register */
1659  if (res != 0) /* check result */
1660  {
1661  handle->debug_print("pcf8591: read pre scale register failed.\n"); /* read pre scale register failed */
1662 
1663  return 1; /* return error */
1664  }
1665 
1666  return 0; /* success return 0 */
1667 }
1668 
1681 uint8_t pca9685_output_frequency_convert_to_register(pca9685_handle_t *handle, uint32_t oscillator, uint16_t output_freq, uint8_t *reg)
1682 {
1683  if (handle == NULL) /* check handle */
1684  {
1685  return 2; /* return error */
1686  }
1687  if (handle->inited != 1) /* check handle initialization */
1688  {
1689  return 3; /* return error */
1690  }
1691 
1692  *reg = (uint8_t)(round((double)oscillator / ((double)output_freq * 4096.0))) - 1; /* convert real data to register data */
1693 
1694  return 0; /* success return 0 */
1695 }
1696 
1709 uint8_t pca9685_output_frequency_convert_to_data(pca9685_handle_t *handle, uint32_t oscillator, uint8_t reg, uint16_t *output_freq)
1710 {
1711  if (handle == NULL) /* check handle */
1712  {
1713  return 2; /* return error */
1714  }
1715  if (handle->inited != 1) /* check handle initialization */
1716  {
1717  return 3; /* return error */
1718  }
1719 
1720  *output_freq = (uint16_t)(round((double)oscillator / ((reg + 1) * 4096.0))); /* convert raw data to real data */
1721 
1722  return 0; /* success return 0 */
1723 }
1724 
1741 uint8_t pca9685_pwm_convert_to_register(pca9685_handle_t *handle, float delay_percent, float high_duty_cycle_percent,
1742  uint16_t *on_count, uint16_t *off_count)
1743 {
1744  if (handle == NULL) /* check handle */
1745  {
1746  return 2; /* return error */
1747  }
1748  if (handle->inited != 1) /* check handle initialization */
1749  {
1750  return 3; /* return error */
1751  }
1752  if (delay_percent + high_duty_cycle_percent >=100.0f) /* check result */
1753  {
1754  handle->debug_print("pcf8591: delay_percent + high_duty_cycle_percent can't be over 100.0.\n"); /* delay_percent + high_duty_cycle_percent can't be over 100.0 */
1755 
1756  return 4; /* return error */
1757  }
1758 
1759  *on_count = (uint16_t)(roundf(delay_percent / 100.0f * 4096.0f)); /* set on count */
1760  *off_count = (uint16_t)(roundf((delay_percent + high_duty_cycle_percent) / 100.0f * 4096.0f)); /* set off count */
1761 
1762  return 0; /* success return 0 */
1763 }
1764 
1779 uint8_t pca9685_pwm_convert_to_data(pca9685_handle_t *handle, uint16_t on_count, uint16_t off_count,
1780  float *delay_percent, float *high_duty_cycle_percent)
1781 {
1782  if (handle == NULL) /* check handle */
1783  {
1784  return 2; /* return error */
1785  }
1786  if (handle->inited != 1) /* check handle initialization */
1787  {
1788  return 3; /* return error */
1789  }
1790  if ((on_count > 4096) || (off_count > 4096)) /* check result */
1791  {
1792  handle->debug_print("pcf8591: on_count or off_count is over 4096.\n"); /* on_count or off_count is over 4096 */
1793 
1794  return 4; /* return error */
1795  }
1796 
1797  *delay_percent = (float)(on_count) / 4096.0f * 100.0f; /* set the delay_percent */
1798  *high_duty_cycle_percent = (float)(off_count - on_count) / 4096.0f * 100.0f; /* set the high_duty_cycle_percent */
1799 
1800  return 0; /* success return 0 */
1801 }
1802 
1815 {
1816  uint8_t res, prev;
1817 
1818  if (handle == NULL) /* check handle */
1819  {
1820  return 2; /* return error */
1821  }
1822  if (handle->debug_print == NULL) /* check debug_print */
1823  {
1824  return 3; /* return error */
1825  }
1826  if (handle->iic_init == NULL) /* check iic_init */
1827  {
1828  handle->debug_print("pca9685: iic_init is null.\n"); /* iic_init is null */
1829 
1830  return 3; /* return error */
1831  }
1832  if (handle->iic_deinit == NULL) /* check iic_deinit */
1833  {
1834  handle->debug_print("pca9685: iic_deinit is null.\n"); /* iic_deinit is null */
1835 
1836  return 3; /* return error */
1837  }
1838  if (handle->iic_read == NULL) /* check iic_read */
1839  {
1840  handle->debug_print("pca9685: iic_read is null.\n"); /* iic_read is null */
1841 
1842  return 3; /* return error */
1843  }
1844  if (handle->iic_write == NULL) /* check iic_write */
1845  {
1846  handle->debug_print("pca9685: iic_write is null.\n"); /* iic_write is null */
1847 
1848  return 3; /* return error */
1849  }
1850  if (handle->oe_gpio_init == NULL) /* check oe_gpio_init */
1851  {
1852  handle->debug_print("pca9685: oe_gpio_init is null.\n"); /* oe_gpio_init is null */
1853 
1854  return 3; /* return error */
1855  }
1856  if (handle->oe_gpio_deinit == NULL) /* check oe_gpio_deinit */
1857  {
1858  handle->debug_print("pca9685: oe_gpio_deinit is null.\n"); /* oe_gpio_deinit is null */
1859 
1860  return 3; /* return error */
1861  }
1862  if (handle->oe_gpio_write == NULL) /* check oe_gpio_write */
1863  {
1864  handle->debug_print("pca9685: oe_gpio_write is null.\n"); /* oe_gpio_write is null */
1865 
1866  return 3; /* return error */
1867  }
1868 
1869  if (handle->iic_init() != 0) /* iic init */
1870  {
1871  handle->debug_print("pca9685: iic init failed.\n"); /* iic init failed */
1872 
1873  return 1; /* return error */
1874  }
1875  if (handle->oe_gpio_init() != 0) /* oe gpio init */
1876  {
1877  handle->debug_print("pca9685: oe gpio init failed.\n"); /* oe gpio init failed */
1878  (void)handle->iic_deinit(); /* iic deinit */
1879 
1880  return 1; /* return error */
1881  }
1882 
1883  res = handle->iic_read(handle->iic_addr, PCA9685_REG_MODE1, (uint8_t *)&prev, 1); /* read mode 1 register */
1884  if (res != 0) /* check result */
1885  {
1886  handle->debug_print("pcf8591: read mode 1 register failed.\n"); /* read mode 1 register failed */
1887  (void)handle->oe_gpio_deinit(); /* oe gpio deinit */
1888  (void)handle->iic_deinit(); /* iic deinit */
1889 
1890  return 4; /* return error */
1891  }
1892  if ((prev & 0x80) != 0) /* check reset bit */
1893  {
1894  prev &= ~(1 << 4); /* clear sleep bit */
1895  res = handle->iic_write(handle->iic_addr, PCA9685_REG_MODE1, (uint8_t *)&prev, 1); /* write mode 1 register */
1896  if (res != 0) /* check result */
1897  {
1898  handle->debug_print("pcf8591: write mode 1 register failed.\n"); /* write mode 1 register failed */
1899  (void)handle->oe_gpio_deinit(); /* oe gpio deinit */
1900  (void)handle->iic_deinit(); /* iic deinit */
1901 
1902  return 4; /* return error */
1903  }
1904  handle->delay_ms(2); /* delay 2 ms */
1905  }
1906  prev |= (1 << 7); /* set reset bit */
1907  prev &= ~(1 << 6); /* clear extern clock bit */
1908  prev &= ~(1 << 4); /* clear sleep bit */
1909  res = handle->iic_write(handle->iic_addr, PCA9685_REG_MODE1, (uint8_t *)&prev, 1); /* write mode 1 register */
1910  if (res != 0) /* check result */
1911  {
1912  handle->debug_print("pcf8591: write mode 1 register failed.\n"); /* write mode 1 register failed */
1913  (void)handle->oe_gpio_deinit(); /* oe gpio deinit */
1914  (void)handle->iic_deinit(); /* iic deinit */
1915 
1916  return 4; /* return error */
1917  }
1918  handle->delay_ms(2); /* delay 2 ms */
1919  handle->inited = 1; /* flag finish initialization */
1920 
1921  return 0; /* success return 0 */
1922 }
1923 
1936 {
1937  uint8_t res, prev;
1938 
1939  if (handle == NULL) /* check handle */
1940  {
1941  return 2; /* return error */
1942  }
1943  if (handle->inited != 1) /* check handle initialization */
1944  {
1945  return 3; /* return error */
1946  }
1947 
1948  res = handle->iic_read(handle->iic_addr, PCA9685_REG_MODE1, (uint8_t *)&prev, 1); /* read mode 1 register */
1949  if (res != 0) /* check result */
1950  {
1951  handle->debug_print("pcf8591: power down failed failed.\n"); /* power down failed */
1952 
1953  return 1; /* return error */
1954  }
1955  prev |= (1 << 4); /* set sleep bit */
1956  res = handle->iic_write(handle->iic_addr, PCA9685_REG_MODE1, (uint8_t *)&prev, 1); /* write mode 1 register */
1957  if (res != 0) /* check result */
1958  {
1959  handle->debug_print("pcf8591: power down failed failed.\n"); /* power down failed */
1960 
1961  return 1; /* return error */
1962  }
1963  if (handle->oe_gpio_deinit() != 0) /* oe gpio deinit */
1964  {
1965  handle->debug_print("pcf8591: oe gpio deinit failed.\n"); /* oe gpio deinit failed */
1966 
1967  return 4; /* return error */
1968  }
1969  if (handle->iic_deinit() != 0) /* iic deinit */
1970  {
1971  handle->debug_print("pcf8591: iic deinit failed.\n"); /* iic deinit failed */
1972 
1973  return 4; /* return error */
1974  }
1975  handle->inited = 0; /* set closed flag */
1976 
1977  return 0; /* success return 0 */
1978 }
1979 
1993 uint8_t pca9685_set_reg(pca9685_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
1994 {
1995  if (handle == NULL) /* check handle */
1996  {
1997  return 2; /* return error */
1998  }
1999  if (handle->inited != 1) /* check handle initialization */
2000  {
2001  return 3; /* return error */
2002  }
2003 
2004  if (handle->iic_write(handle->iic_addr, reg, buf, len) != 0) /* write data */
2005  {
2006  return 1; /* return error */
2007  }
2008  else
2009  {
2010  return 0; /* success return 0 */
2011  }
2012 }
2013 
2027 uint8_t pca9685_get_reg(pca9685_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
2028 {
2029  if (handle == NULL) /* check handle */
2030  {
2031  return 2; /* return error */
2032  }
2033  if (handle->inited != 1) /* check handle initialization */
2034  {
2035  return 3; /* return error */
2036  }
2037 
2038  if (handle->iic_read(handle->iic_addr, reg, buf, len) != 0) /* read data */
2039  {
2040  return 1; /* return error */
2041  }
2042  else
2043  {
2044  return 0; /* success return 0 */
2045  }
2046 }
2047 
2057 {
2058  if (info == NULL) /* check handle */
2059  {
2060  return 2; /* return error */
2061  }
2062 
2063  memset(info, 0, sizeof(pca9685_info_t)); /* initialize pca9685 info structure */
2064  strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
2065  strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
2066  strncpy(info->interface, "IIC", 8); /* copy interface name */
2067  info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
2068  info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
2069  info->max_current_ma = MAX_CURRENT; /* set maximum current */
2070  info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
2071  info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
2072  info->driver_version = DRIVER_VERSION; /* set driver version */
2073 
2074  return 0; /* success return 0 */
2075 }
#define PCA9685_REG_SUBADR3
#define MAX_CURRENT
#define PCA9685_REG_ALL_LED_ON_L
#define SUPPLY_VOLTAGE_MAX
#define PCA9685_REG_MODE2
#define PCA9685_REG_SUBADR1
#define PCA9685_REG_MODE1
chip register definition
#define TEMPERATURE_MAX
#define PCA9685_REG_SUBADR2
#define PCA9685_REG_PRE_SCALE
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define PCA9685_REG_LED0_ON_L
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
#define PCA9685_REG_ALLCALLADR
driver pca9685 header file
uint8_t pca9685_pwm_convert_to_register(pca9685_handle_t *handle, float delay_percent, float high_duty_cycle_percent, uint16_t *on_count, uint16_t *off_count)
convert the pwm to the register raw data
uint8_t pca9685_set_prescaler(pca9685_handle_t *handle, uint8_t prescaler)
set the clock pres cale
uint8_t pca9685_write_channel(pca9685_handle_t *handle, pca9685_channel_t channel, uint16_t on_count, uint16_t off_count)
write led channels
pca9685_bool_t
pca9685 bool enumeration definition
uint8_t pca9685_get_subaddress_1(pca9685_handle_t *handle, uint8_t *addr)
get the sub address 1
uint8_t pca9685_write_all_channel(pca9685_handle_t *handle, uint16_t on_count, uint16_t off_count)
write all led channels
pca9685_address_t
pca9685 address enumeration definition
uint8_t pca9685_get_addr_pin(pca9685_handle_t *handle, pca9685_address_t *addr_pin)
get the address pin
uint8_t pca9685_set_addr(pca9685_handle_t *handle, uint8_t addr)
set the address
uint8_t pca9685_get_respond_all_call(pca9685_handle_t *handle, pca9685_bool_t *enable)
get the respond all call status
uint8_t pca9685_set_respond_all_call(pca9685_handle_t *handle, pca9685_bool_t enable)
enable or disable respond all call
uint8_t pca9685_init(pca9685_handle_t *handle)
initialize the chip
uint8_t pca9685_set_active(pca9685_handle_t *handle, pca9685_bool_t enable)
set the chip active
uint8_t pca9685_get_output_change(pca9685_handle_t *handle, pca9685_output_change_t *change)
get the output change type
uint8_t pca9685_get_all_call_address(pca9685_handle_t *handle, uint8_t *addr)
set the all call address
uint8_t pca9685_set_addr_pin(pca9685_handle_t *handle, pca9685_address_t addr_pin)
set the address pin
uint8_t pca9685_get_external_clock_pin(pca9685_handle_t *handle, pca9685_bool_t *enable)
get the external clock pin status
pca9685_output_disable_type_t
pca9685 output disable type enumeration definition
uint8_t pca9685_get_restart(pca9685_handle_t *handle, pca9685_bool_t *enable)
get the restart status
uint8_t pca9685_set_output_driver(pca9685_handle_t *handle, pca9685_output_driver_t driver)
set the output driver type
uint8_t pca9685_set_respond_subaddress_2(pca9685_handle_t *handle, pca9685_bool_t enable)
enable or disable respond sub address 2
uint8_t pca9685_get_prescaler(pca9685_handle_t *handle, uint8_t *prescaler)
get the clock pre scale
uint8_t pca9685_get_addr(pca9685_handle_t *handle, uint8_t *addr)
get the address
uint8_t pca9685_get_sleep_mode(pca9685_handle_t *handle, pca9685_bool_t *enable)
get the sleep mode status
uint8_t pca9685_set_output_disable_type(pca9685_handle_t *handle, pca9685_output_disable_type_t type)
set the output disable type
uint8_t pca9685_get_subaddress_3(pca9685_handle_t *handle, uint8_t *addr)
get the sub address 3
uint8_t pca9685_read_channel(pca9685_handle_t *handle, pca9685_channel_t channel, uint16_t *on_count, uint16_t *off_count)
read led channels
uint8_t pca9685_get_output_driver(pca9685_handle_t *handle, pca9685_output_driver_t *driver)
get the output driver type
uint8_t pca9685_set_subaddress_2(pca9685_handle_t *handle, uint8_t addr)
set the sub address 2
uint8_t pca9685_output_frequency_convert_to_register(pca9685_handle_t *handle, uint32_t oscillator, uint16_t output_freq, uint8_t *reg)
convert the output frequency to the register raw data
uint8_t pca9685_set_register_auto_increment(pca9685_handle_t *handle, pca9685_bool_t enable)
enable or disable the register auto increment
uint8_t pca9685_set_respond_subaddress_3(pca9685_handle_t *handle, pca9685_bool_t enable)
enable or disable respond sub address 3
uint8_t pca9685_info(pca9685_info_t *info)
get chip's information
uint8_t pca9685_set_output_invert(pca9685_handle_t *handle, pca9685_bool_t enable)
enable or disable output invert
uint8_t pca9685_get_respond_subaddress_2(pca9685_handle_t *handle, pca9685_bool_t *enable)
get the respond sub address 2 status
uint8_t pca9685_get_subaddress_2(pca9685_handle_t *handle, uint8_t *addr)
get the sub address 2
pca9685_channel_t
pca9685 channel enumeration definition
uint8_t pca9685_get_output_disable_type(pca9685_handle_t *handle, pca9685_output_disable_type_t *type)
get the output disable type
uint8_t pca9685_set_subaddress_1(pca9685_handle_t *handle, uint8_t addr)
set the sub address 1
uint8_t pca9685_set_restart(pca9685_handle_t *handle, pca9685_bool_t enable)
enable or disable restart
uint8_t pca9685_output_frequency_convert_to_data(pca9685_handle_t *handle, uint32_t oscillator, uint8_t reg, uint16_t *output_freq)
convert the register raw data to the output frequency
pca9685_output_driver_t
pca9685 output driver enumeration definition
uint8_t pca9685_pwm_convert_to_data(pca9685_handle_t *handle, uint16_t on_count, uint16_t off_count, float *delay_percent, float *high_duty_cycle_percent)
convert the register raw data to the pwm
uint8_t pca9685_set_sleep_mode(pca9685_handle_t *handle, pca9685_bool_t enable)
enable or disable the sleep mode
uint8_t pca9685_get_respond_subaddress_1(pca9685_handle_t *handle, pca9685_bool_t *enable)
get the respond sub address 1 status
uint8_t pca9685_get_output_invert(pca9685_handle_t *handle, pca9685_bool_t *enable)
get the output invert status
uint8_t pca9685_set_external_clock_pin(pca9685_handle_t *handle, pca9685_bool_t enable)
enable or disable the external clock pin
pca9685_output_change_t
pca9685 output change enumeration definition
uint8_t pca9685_deinit(pca9685_handle_t *handle)
close the chip
uint8_t pca9685_get_respond_subaddress_3(pca9685_handle_t *handle, pca9685_bool_t *enable)
get the respond sub address 3 status
uint8_t pca9685_set_respond_subaddress_1(pca9685_handle_t *handle, pca9685_bool_t enable)
enable or disable respond sub address 1
uint8_t pca9685_set_subaddress_3(pca9685_handle_t *handle, uint8_t addr)
set the sub address 3
uint8_t pca9685_set_all_call_address(pca9685_handle_t *handle, uint8_t addr)
set the all call address
uint8_t pca9685_set_output_change(pca9685_handle_t *handle, pca9685_output_change_t change)
set the output change type
uint8_t pca9685_get_register_auto_increment(pca9685_handle_t *handle, pca9685_bool_t *enable)
get the register auto increment status
uint8_t pca9685_set_reg(pca9685_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
set the chip register
uint8_t pca9685_get_reg(pca9685_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
get the chip register
pca9685 handle structure definition
void(* delay_ms)(uint32_t ms)
uint8_t(* oe_gpio_init)(void)
void(* debug_print)(const char *const fmt,...)
uint8_t(* iic_init)(void)
uint8_t(* oe_gpio_deinit)(void)
uint8_t(* oe_gpio_write)(uint8_t value)
uint8_t(* iic_write)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* iic_read)(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
uint8_t(* iic_deinit)(void)
pca9685 information structure definition
float supply_voltage_max_v
uint32_t driver_version
char manufacturer_name[32]
float supply_voltage_min_v
char chip_name[32]