LibDriver PCA9685  1.0.0
PCA9685 full-featured driver
driver_pca9685_register_test.c
Go to the documentation of this file.
1 
38 #include <stdlib.h>
39 
40 static pca9685_handle_t gs_handle;
51 {
52  uint8_t res;
53  uint8_t i;
54  uint8_t addr_value, addr_value_check;
55  uint8_t prescaler, prescaler_check;
56  uint16_t output_freq, output_freq_check;
57  uint8_t reg;
58  float delay_percent, high_duty_cycle_percent;
59  float delay_percent_check, high_duty_cycle_percent_check;
60  uint16_t on_count, off_count;
61  uint16_t on_count_check, off_count_check;
62  pca9685_address_t addr_pin, addr_pin_check;
63  pca9685_bool_t enable;
67  pca9685_channel_t channel;
68  pca9685_info_t info;
69 
70  /* link interface function */
81 
82  /* get information */
83  res = pca9685_info(&info);
84  if (res != 0)
85  {
86  pca9685_interface_debug_print("pca9685: get info failed.\n");
87 
88  return 1;
89  }
90  else
91  {
92  /* print chip info */
93  pca9685_interface_debug_print("pca9685: chip is %s.\n", info.chip_name);
94  pca9685_interface_debug_print("pca9685: manufacturer is %s.\n", info.manufacturer_name);
95  pca9685_interface_debug_print("pca9685: interface is %s.\n", info.interface);
96  pca9685_interface_debug_print("pca9685: driver version is %d.%d.\n", info.driver_version / 1000, (info.driver_version % 1000) / 100);
97  pca9685_interface_debug_print("pca9685: min supply voltage is %0.1fV.\n", info.supply_voltage_min_v);
98  pca9685_interface_debug_print("pca9685: max supply voltage is %0.1fV.\n", info.supply_voltage_max_v);
99  pca9685_interface_debug_print("pca9685: max current is %0.2fmA.\n", info.max_current_ma);
100  pca9685_interface_debug_print("pca9685: max temperature is %0.1fC.\n", info.temperature_max);
101  pca9685_interface_debug_print("pca9685: min temperature is %0.1fC.\n", info.temperature_min);
102  }
103 
104  /* start register test */
105  pca9685_interface_debug_print("pca9685: start register test.\n");
106 
107  /* pca9685_set_addr/pca9685_get_addr test */
108  pca9685_interface_debug_print("pca9685: pca9685_set_addr/pca9685_get_addr test.\n");
109 
110  addr_value = rand() % 256;
111  res = pca9685_set_addr(&gs_handle, addr_value);
112  if (res != 0)
113  {
114  pca9685_interface_debug_print("pca9685: set addr failed.\n");
115 
116  return 1;
117  }
118  pca9685_interface_debug_print("pca9685: set addr 0x%02X.\n", addr_value);
119  res = pca9685_get_addr(&gs_handle, (uint8_t *)&addr_value_check);
120  if (res != 0)
121  {
122  pca9685_interface_debug_print("pca9685: set addr failed.\n");
123 
124  return 1;
125  }
126  pca9685_interface_debug_print("pca9685: check addr %s.\n", addr_value == addr_value_check ? "ok" : "error");
127 
128  /* pca9685_set_addr_pin/pca9685_get_addr_pin test */
129  pca9685_interface_debug_print("pca9685: pca9685_set_addr_pin/pca9685_get_addr_pin test.\n");
130 
131  for (i = 0; i < 64; i++)
132  {
133  addr_pin = (pca9685_address_t)(i);
134  res = pca9685_set_addr_pin(&gs_handle, addr_pin);
135  if (res != 0)
136  {
137  pca9685_interface_debug_print("pca9685: set addr pin failed.\n");
138 
139  return 1;
140  }
141  pca9685_interface_debug_print("pca9685: set addr pin 0x%02X.\n", addr_pin);
142  res = pca9685_get_addr_pin(&gs_handle, (pca9685_address_t *)&addr_pin_check);
143  if (res != 0)
144  {
145  pca9685_interface_debug_print("pca9685: get addr pin failed.\n");
146 
147  return 1;
148  }
149  pca9685_interface_debug_print("pca9685: check addr pin %s.\n", addr_pin_check == addr_pin ? "ok" : "error");
150  }
151 
152  /* set addr pin */
153  res = pca9685_set_addr_pin(&gs_handle, addr);
154  if (res != 0)
155  {
156  pca9685_interface_debug_print("pca9685: set addr pin failed.\n");
157 
158  return 1;
159  }
160 
161  /* pca9685 init */
162  res = pca9685_init(&gs_handle);
163  if (res != 0)
164  {
165  pca9685_interface_debug_print("pca9685: init failed.\n");
166 
167  return 1;
168  }
169 
170  /* inactive */
171  res = pca9685_set_active(&gs_handle, PCA9685_BOOL_FALSE);
172  if (res != 0)
173  {
174  pca9685_interface_debug_print("pca9685: set active failed.\n");
175  (void)pca9685_deinit(&gs_handle);
176 
177  return 1;
178  }
179 
180  /* set sleep mode */
181  res = pca9685_set_sleep_mode(&gs_handle, PCA9685_BOOL_TRUE);
182  if (res != 0)
183  {
184  pca9685_interface_debug_print("pca9685: set sleep mode failed.\n");
185  (void)pca9685_deinit(&gs_handle);
186 
187  return 1;
188  }
189 
190  /* pca9685_set_prescaler/pca9685_get_prescaler test */
191  pca9685_interface_debug_print("pca9685: pca9685_set_prescaler/pca9685_get_prescaler test.\n");
192 
193  prescaler = rand() % 200 + 3;
194  res = pca9685_set_prescaler(&gs_handle, prescaler);
195  if (res != 0)
196  {
197  pca9685_interface_debug_print("pca9685: set pres cale failed.\n");
198  (void)pca9685_deinit(&gs_handle);
199 
200  return 1;
201  }
202  pca9685_interface_debug_print("pca9685: set prescaler 0x%02X.\n", prescaler);
203  res = pca9685_get_prescaler(&gs_handle, (uint8_t *)&prescaler_check);
204  if (res != 0)
205  {
206  pca9685_interface_debug_print("pca9685: get prescaler failed.\n");
207  (void)pca9685_deinit(&gs_handle);
208 
209  return 1;
210  }
211  pca9685_interface_debug_print("pca9685: check prescaler %s.\n", prescaler == prescaler_check ? "ok" : "error");
212 
213  /* set sleep mode */
214  res = pca9685_set_sleep_mode(&gs_handle, PCA9685_BOOL_FALSE);
215  if (res != 0)
216  {
217  pca9685_interface_debug_print("pca9685: set sleep mode failed.\n");
218 
219  return 1;
220  }
222 
223  /* pca9685_set_restart/pca9685_get_restart test */
224  pca9685_interface_debug_print("pca9685: pca9685_set_restart/pca9685_get_restart test.\n");
225 
226  /* disable restart */
227  res = pca9685_set_restart(&gs_handle, PCA9685_BOOL_FALSE);
228  if (res != 0)
229  {
230  pca9685_interface_debug_print("pca9685: set restart failed.\n");
231  (void)pca9685_deinit(&gs_handle);
232 
233  return 1;
234  }
235  pca9685_interface_debug_print("pca9685: disable restart.\n");
236  res = pca9685_get_restart(&gs_handle, (pca9685_bool_t *)&enable);
237  if (res != 0)
238  {
239  pca9685_interface_debug_print("pca9685: get restart failed.\n");
240  (void)pca9685_deinit(&gs_handle);
241 
242  return 1;
243  }
244  pca9685_interface_debug_print("pca9685: check restart %s.\n", PCA9685_BOOL_FALSE == enable ? "ok" : "error");
245 
246  /* enable restart */
247  res = pca9685_set_restart(&gs_handle, PCA9685_BOOL_TRUE);
248  if (res != 0)
249  {
250  pca9685_interface_debug_print("pca9685: set restart failed.\n");
251  (void)pca9685_deinit(&gs_handle);
252 
253  return 1;
254  }
255  pca9685_interface_debug_print("pca9685: enable restart.\n");
257  res = pca9685_get_restart(&gs_handle, (pca9685_bool_t *)&enable);
258  if (res != 0)
259  {
260  pca9685_interface_debug_print("pca9685: get restart failed.\n");
261  (void)pca9685_deinit(&gs_handle);
262 
263  return 1;
264  }
265  pca9685_interface_debug_print("pca9685: check restart %s.\n", PCA9685_BOOL_FALSE == enable ? "ok" : "error");
266 
267  /* pca9685_set_external_clock_pin/pca9685_get_external_clock_pin test */
268  pca9685_interface_debug_print("pca9685: pca9685_set_external_clock_pin/pca9685_get_external_clock_pin test.\n");
269 
270  /* disable */
272  if (res != 0)
273  {
274  pca9685_interface_debug_print("pca9685: set external clock pin failed.\n");
275  (void)pca9685_deinit(&gs_handle);
276 
277  return 1;
278  }
279  pca9685_interface_debug_print("pca9685: disable external clock pin.\n");
280  res = pca9685_get_external_clock_pin(&gs_handle, &enable);
281  if (res != 0)
282  {
283  pca9685_interface_debug_print("pca9685: get external clock pin failed.\n");
284  (void)pca9685_deinit(&gs_handle);
285 
286  return 1;
287  }
288  pca9685_interface_debug_print("pca9685: check external clock pin %s.\n", enable == PCA9685_BOOL_FALSE ? "ok" : "error");
289 
290  /* pca9685_set_register_auto_increment/pca9685_get_register_auto_increment test */
291  pca9685_interface_debug_print("pca9685: pca9685_set_register_auto_increment/pca9685_get_register_auto_increment test.\n");
292 
293  /* disable */
295  if (res != 0)
296  {
297  pca9685_interface_debug_print("pca9685: set register auto increment failed.\n");
298  (void)pca9685_deinit(&gs_handle);
299 
300  return 1;
301  }
302  pca9685_interface_debug_print("pca9685: disable register auto increment.\n");
303  res = pca9685_get_register_auto_increment(&gs_handle, &enable);
304  if (res != 0)
305  {
306  pca9685_interface_debug_print("pca9685: get register auto increment failed.\n");
307  (void)pca9685_deinit(&gs_handle);
308 
309  return 1;
310  }
311  pca9685_interface_debug_print("pca9685: check register auto increment %s.\n", enable == PCA9685_BOOL_FALSE ? "ok" : "error");
312 
313  /* enable */
315  if (res != 0)
316  {
317  pca9685_interface_debug_print("pca9685: set register auto increment failed.\n");
318  (void)pca9685_deinit(&gs_handle);
319 
320  return 1;
321  }
322  pca9685_interface_debug_print("pca9685: enable register auto increment.\n");
323  res = pca9685_get_register_auto_increment(&gs_handle, &enable);
324  if (res != 0)
325  {
326  pca9685_interface_debug_print("pca9685: get register auto increment failed.\n");
327  (void)pca9685_deinit(&gs_handle);
328 
329  return 1;
330  }
331  pca9685_interface_debug_print("pca9685: check register auto increment %s.\n", enable == PCA9685_BOOL_TRUE ? "ok" : "error");
332 
333  /* pca9685_set_sleep_mode/pca9685_get_sleep_mode test */
334  pca9685_interface_debug_print("pca9685: pca9685_set_sleep_mode/pca9685_get_sleep_mode test.\n");
335 
336  /* disable */
337  res = pca9685_set_sleep_mode(&gs_handle, PCA9685_BOOL_FALSE);
338  if (res != 0)
339  {
340  pca9685_interface_debug_print("pca9685: set sleep mode failed.\n");
341  (void)pca9685_deinit(&gs_handle);
342 
343  return 1;
344  }
345  pca9685_interface_debug_print("pca9685: disable sleep mode.\n");
347  res = pca9685_get_sleep_mode(&gs_handle, &enable);
348  if (res != 0)
349  {
350  pca9685_interface_debug_print("pca9685: get sleep mode failed.\n");
351  (void)pca9685_deinit(&gs_handle);
352 
353  return 1;
354  }
355  pca9685_interface_debug_print("pca9685: check sleep mode %s.\n", enable == PCA9685_BOOL_FALSE ? "ok" : "error");
356 
357  /* enable */
358  res = pca9685_set_sleep_mode(&gs_handle, PCA9685_BOOL_TRUE);
359  if (res != 0)
360  {
361  pca9685_interface_debug_print("pca9685: set sleep mode failed.\n");
362  (void)pca9685_deinit(&gs_handle);
363 
364  return 1;
365  }
366  pca9685_interface_debug_print("pca9685: enable sleep mode.\n");
367  res = pca9685_get_sleep_mode(&gs_handle, &enable);
368  if (res != 0)
369  {
370  pca9685_interface_debug_print("pca9685: get sleep mode failed.\n");
371  (void)pca9685_deinit(&gs_handle);
372 
373  return 1;
374  }
375  pca9685_interface_debug_print("pca9685: check sleep mode %s.\n", enable == PCA9685_BOOL_TRUE ? "ok" : "error");
376 
377  /* pca9685_set_respond_subaddress_1/pca9685_get_respond_subaddress_1 test */
378  pca9685_interface_debug_print("pca9685: pca9685_set_respond_subaddress_1/pca9685_get_respond_subaddress_1 test.\n");
379 
380  /* disable */
382  if (res != 0)
383  {
384  pca9685_interface_debug_print("pca9685: set respond sub address 1 failed.\n");
385  (void)pca9685_deinit(&gs_handle);
386 
387  return 1;
388  }
389  pca9685_interface_debug_print("pca9685: disable respond sub address 1.\n");
390  res = pca9685_get_respond_subaddress_1(&gs_handle, &enable);
391  if (res != 0)
392  {
393  pca9685_interface_debug_print("pca9685: get respond sub address 1 failed.\n");
394  (void)pca9685_deinit(&gs_handle);
395 
396  return 1;
397  }
398  pca9685_interface_debug_print("pca9685: check respond sub address 1 %s.\n", enable == PCA9685_BOOL_FALSE ? "ok" : "error");
399 
400  /* enable */
402  if (res != 0)
403  {
404  pca9685_interface_debug_print("pca9685: set respond sub address 1 failed.\n");
405  (void)pca9685_deinit(&gs_handle);
406 
407  return 1;
408  }
409  pca9685_interface_debug_print("pca9685: enable respond sub address 1.\n");
410  res = pca9685_get_respond_subaddress_1(&gs_handle, &enable);
411  if (res != 0)
412  {
413  pca9685_interface_debug_print("pca9685: get respond sub address 1 failed.\n");
414  (void)pca9685_deinit(&gs_handle);
415 
416  return 1;
417  }
418  pca9685_interface_debug_print("pca9685: check respond sub address 1 %s.\n", enable == PCA9685_BOOL_TRUE ? "ok" : "error");
419 
420  /* pca9685_set_respond_subaddress_2/pca9685_get_respond_subaddress_2 test */
421  pca9685_interface_debug_print("pca9685: pca9685_set_respond_subaddress_2/pca9685_get_respond_subaddress_2 test.\n");
422 
423  /* disable */
425  if (res != 0)
426  {
427  pca9685_interface_debug_print("pca9685: set respond sub address 2 failed.\n");
428  (void)pca9685_deinit(&gs_handle);
429 
430  return 1;
431  }
432  pca9685_interface_debug_print("pca9685: disable respond sub address 2.\n");
433  res = pca9685_get_respond_subaddress_2(&gs_handle, &enable);
434  if (res != 0)
435  {
436  pca9685_interface_debug_print("pca9685: get respond sub address 2 failed.\n");
437  (void)pca9685_deinit(&gs_handle);
438 
439  return 1;
440  }
441  pca9685_interface_debug_print("pca9685: check respond sub address 2 %s.\n", enable == PCA9685_BOOL_FALSE ? "ok" : "error");
442 
443  /* enable */
445  if (res != 0)
446  {
447  pca9685_interface_debug_print("pca9685: set respond sub address 2 failed.\n");
448  (void)pca9685_deinit(&gs_handle);
449 
450  return 1;
451  }
452  pca9685_interface_debug_print("pca9685: enable respond sub address 2.\n");
453  res = pca9685_get_respond_subaddress_2(&gs_handle, &enable);
454  if (res != 0)
455  {
456  pca9685_interface_debug_print("pca9685: get respond sub address 2 failed.\n");
457  (void)pca9685_deinit(&gs_handle);
458 
459  return 1;
460  }
461  pca9685_interface_debug_print("pca9685: check respond sub address 2 %s.\n", enable == PCA9685_BOOL_TRUE ? "ok" : "error");
462 
463  /* pca9685_set_respond_subaddress_3/pca9685_get_respond_subaddress_3 test */
464  pca9685_interface_debug_print("pca9685: pca9685_set_respond_subaddress_3/pca9685_get_respond_subaddress_3 test.\n");
465 
466  /* disable */
468  if (res != 0)
469  {
470  pca9685_interface_debug_print("pca9685: set respond sub address 3 failed.\n");
471  (void)pca9685_deinit(&gs_handle);
472 
473  return 1;
474  }
475  pca9685_interface_debug_print("pca9685: disable respond sub address 3.\n");
476  res = pca9685_get_respond_subaddress_3(&gs_handle, &enable);
477  if (res != 0)
478  {
479  pca9685_interface_debug_print("pca9685: get respond sub address 3 failed.\n");
480  (void)pca9685_deinit(&gs_handle);
481 
482  return 1;
483  }
484  pca9685_interface_debug_print("pca9685: check respond sub address 3 %s.\n", enable == PCA9685_BOOL_FALSE ? "ok" : "error");
485 
486  /* enable */
488  if (res != 0)
489  {
490  pca9685_interface_debug_print("pca9685: set respond sub address 3 failed.\n");
491  (void)pca9685_deinit(&gs_handle);
492 
493  return 1;
494  }
495  pca9685_interface_debug_print("pca9685: enable respond sub address 3.\n");
496  res = pca9685_get_respond_subaddress_3(&gs_handle, &enable);
497  if (res != 0)
498  {
499  pca9685_interface_debug_print("pca9685: get respond sub address 3 failed.\n");
500  (void)pca9685_deinit(&gs_handle);
501 
502  return 1;
503  }
504  pca9685_interface_debug_print("pca9685: check respond sub address 3 %s.\n", enable == PCA9685_BOOL_TRUE ? "ok" : "error");
505 
506  /* pca9685_set_respond_all_call/pca9685_get_respond_all_call test */
507  pca9685_interface_debug_print("pca9685: pca9685_set_respond_all_call/pca9685_get_respond_all_call test.\n");
508 
509  /* disable */
511  if (res != 0)
512  {
513  pca9685_interface_debug_print("pca9685: set respond all call failed.\n");
514  (void)pca9685_deinit(&gs_handle);
515 
516  return 1;
517  }
518  pca9685_interface_debug_print("pca9685: disable respond all call.\n");
519  res = pca9685_get_respond_all_call(&gs_handle, &enable);
520  if (res != 0)
521  {
522  pca9685_interface_debug_print("pca9685: get respond all call failed.\n");
523  (void)pca9685_deinit(&gs_handle);
524 
525  return 1;
526  }
527  pca9685_interface_debug_print("pca9685: check respond all call %s.\n", enable == PCA9685_BOOL_FALSE ? "ok" : "error");
528 
529  /* enable */
531  if (res != 0)
532  {
533  pca9685_interface_debug_print("pca9685: set respond all call failed.\n");
534  (void)pca9685_deinit(&gs_handle);
535 
536  return 1;
537  }
538  pca9685_interface_debug_print("pca9685: enable respond all call.\n");
539  res = pca9685_get_respond_all_call(&gs_handle, &enable);
540  if (res != 0)
541  {
542  pca9685_interface_debug_print("pca9685: get respond all call failed.\n");
543  (void)pca9685_deinit(&gs_handle);
544 
545  return 1;
546  }
547  pca9685_interface_debug_print("pca9685: check respond all call %s.\n", enable == PCA9685_BOOL_TRUE ? "ok" : "error");
548 
549  /* pca9685_set_output_invert/pca9685_get_output_invert test */
550  pca9685_interface_debug_print("pca9685: pca9685_set_output_invert/pca9685_get_output_invert test.\n");
551 
552  /* disable */
554  if (res != 0)
555  {
556  pca9685_interface_debug_print("pca9685: set output invert failed.\n");
557  (void)pca9685_deinit(&gs_handle);
558 
559  return 1;
560  }
561  pca9685_interface_debug_print("pca9685: disable output invert.\n");
562  res = pca9685_get_output_invert(&gs_handle, &enable);
563  if (res != 0)
564  {
565  pca9685_interface_debug_print("pca9685: get output invert failed.\n");
566  (void)pca9685_deinit(&gs_handle);
567 
568  return 1;
569  }
570  pca9685_interface_debug_print("pca9685: check output invert %s.\n", enable == PCA9685_BOOL_FALSE ? "ok" : "error");
571 
572  /* enable */
573  res = pca9685_set_output_invert(&gs_handle, PCA9685_BOOL_TRUE);
574  if (res != 0)
575  {
576  pca9685_interface_debug_print("pca9685: set output invert failed.\n");
577  (void)pca9685_deinit(&gs_handle);
578 
579  return 1;
580  }
581  pca9685_interface_debug_print("pca9685: enable output invert.\n");
582  res = pca9685_get_output_invert(&gs_handle, &enable);
583  if (res != 0)
584  {
585  pca9685_interface_debug_print("pca9685: get output invert failed.\n");
586  (void)pca9685_deinit(&gs_handle);
587 
588  return 1;
589  }
590  pca9685_interface_debug_print("pca9685: check output invert %s.\n", enable == PCA9685_BOOL_TRUE ? "ok" : "error");
591 
592  /* pca9685_set_output_change/pca9685_get_output_change test */
593  pca9685_interface_debug_print("pca9685: pca9685_set_output_change/pca9685_get_output_change test.\n");
594 
595  /* set stop change */
597  if (res != 0)
598  {
599  pca9685_interface_debug_print("pca9685: set output change failed.\n");
600  (void)pca9685_deinit(&gs_handle);
601 
602  return 1;
603  }
604  pca9685_interface_debug_print("pca9685: set output stop change.\n");
605  res =pca9685_get_output_change(&gs_handle, &change);
606  if (res != 0)
607  {
608  pca9685_interface_debug_print("pca9685: get output change failed.\n");
609  (void)pca9685_deinit(&gs_handle);
610 
611  return 1;
612  }
613  pca9685_interface_debug_print("pca9685: check output change %s.\n", change == PCA9685_OUTPUT_CHANGE_STOP ? "ok" : "error");
614 
615  /* set ack change */
617  if (res != 0)
618  {
619  pca9685_interface_debug_print("pca9685: set output change failed.\n");
620  (void)pca9685_deinit(&gs_handle);
621 
622  return 1;
623  }
624  pca9685_interface_debug_print("pca9685: set output ack change.\n");
625  res =pca9685_get_output_change(&gs_handle, &change);
626  if (res != 0)
627  {
628  pca9685_interface_debug_print("pca9685: get output change failed.\n");
629  (void)pca9685_deinit(&gs_handle);
630 
631  return 1;
632  }
633  pca9685_interface_debug_print("pca9685: check output change %s.\n", change == PCA9685_OUTPUT_CHANGE_ACK ? "ok" : "error");
634 
635  /* pca9685_set_output_driver/pca9685_get_output_driver test */
636  pca9685_interface_debug_print("pca9685: pca9685_set_output_driver/pca9685_get_output_driver test.\n");
637 
638  /* open drain */
640  if (res != 0)
641  {
642  pca9685_interface_debug_print("pca9685: set output driver failed.\n");
643  (void)pca9685_deinit(&gs_handle);
644 
645  return 1;
646  }
647  pca9685_interface_debug_print("pca9685: set output driver open drain.\n");
648  res = pca9685_get_output_driver(&gs_handle, &driver);
649  if (res != 0)
650  {
651  pca9685_interface_debug_print("pca9685: get output driver failed.\n");
652  (void)pca9685_deinit(&gs_handle);
653 
654  return 1;
655  }
656  pca9685_interface_debug_print("pca9685: check output driver %s.\n", driver == PCA9685_OUTPUT_DRIVER_OPEN_DRAIN ? "ok" : "error");
657 
658  /* totem pole */
660  if (res != 0)
661  {
662  pca9685_interface_debug_print("pca9685: set output driver failed.\n");
663  (void)pca9685_deinit(&gs_handle);
664 
665  return 1;
666  }
667  pca9685_interface_debug_print("pca9685: set output driver totem pole.\n");
668  res = pca9685_get_output_driver(&gs_handle, &driver);
669  if (res != 0)
670  {
671  pca9685_interface_debug_print("pca9685: get output driver failed.\n");
672  (void)pca9685_deinit(&gs_handle);
673 
674  return 1;
675  }
676  pca9685_interface_debug_print("pca9685: check output driver %s.\n", driver == PCA9685_OUTPUT_DRIVER_TOTEM_POLE ? "ok" : "error");
677 
678  /* pca9685_set_output_disable_type/pca9685_get_output_disable_type test */
679  pca9685_interface_debug_print("pca9685: pca9685_set_output_disable_type/pca9685_get_output_disable_type test.\n");
680 
681  /* low */
683  if (res != 0)
684  {
685  pca9685_interface_debug_print("pca9685: set output disable type failed.\n");
686  (void)pca9685_deinit(&gs_handle);
687 
688  return 1;
689  }
690  pca9685_interface_debug_print("pca9685: set output disable type low.\n");
691  res = pca9685_get_output_disable_type(&gs_handle, &type);
692  if (res != 0)
693  {
694  pca9685_interface_debug_print("pca9685: get output disable type failed.\n");
695  (void)pca9685_deinit(&gs_handle);
696 
697  return 1;
698  }
699  pca9685_interface_debug_print("pca9685: check output disable type %s.\n", type == PCA9685_OUTPUT_DISABLE_TYPE_LEDN_LOW ? "ok" : "error");
700 
701  /* low or high impedance */
703  if (res != 0)
704  {
705  pca9685_interface_debug_print("pca9685: set output disable type failed.\n");
706  (void)pca9685_deinit(&gs_handle);
707 
708  return 1;
709  }
710  pca9685_interface_debug_print("pca9685: set output disable type low or high impedance.\n");
711  res = pca9685_get_output_disable_type(&gs_handle, &type);
712  if (res != 0)
713  {
714  pca9685_interface_debug_print("pca9685: get output disable type failed.\n");
715  (void)pca9685_deinit(&gs_handle);
716 
717  return 1;
718  }
719  pca9685_interface_debug_print("pca9685: check output disable type %s.\n", type == PCA9685_OUTPUT_DISABLE_TYPE_OUTDRV_1_LEDN_LOW_OUTDRV_0_HIGH_IMPEDANCE ? "ok" : "error");
720 
721  /* high impedance */
723  if (res != 0)
724  {
725  pca9685_interface_debug_print("pca9685: set output disable type failed.\n");
726  (void)pca9685_deinit(&gs_handle);
727 
728  return 1;
729  }
730  pca9685_interface_debug_print("pca9685: set output disable type high impedance.\n");
731  res = pca9685_get_output_disable_type(&gs_handle, &type);
732  if (res != 0)
733  {
734  pca9685_interface_debug_print("pca9685: get output disable type failed.\n");
735  (void)pca9685_deinit(&gs_handle);
736 
737  return 1;
738  }
739  pca9685_interface_debug_print("pca9685: check output disable type %s.\n", type == PCA9685_OUTPUT_DISABLE_TYPE_HIGH_IMPEDANCE ? "ok" : "error");
740 
741  /* pca9685_set_subaddress_1/pca9685_get_subaddress_1 test */
742  pca9685_interface_debug_print("pca9685: pca9685_set_subaddress_1/pca9685_get_subaddress_1 test.\n");
743 
744  addr_value = rand() % 256;
745  addr_value &= ~(1 << 0);
746  res = pca9685_set_subaddress_1(&gs_handle, addr_value);
747  if (res != 0)
748  {
749  pca9685_interface_debug_print("pca9685: set sub address 1 failed.\n");
750  (void)pca9685_deinit(&gs_handle);
751 
752  return 1;
753  }
754  pca9685_interface_debug_print("pca9685: set sub address 1 0x%02X.\n", addr_value);
755  res = pca9685_get_subaddress_1(&gs_handle, (uint8_t *)&addr_value_check);
756  if (res != 0)
757  {
758  pca9685_interface_debug_print("pca9685: get sub address 1 failed.\n");
759  (void)pca9685_deinit(&gs_handle);
760 
761  return 1;
762  }
763  pca9685_interface_debug_print("pca9685: check sub address 1 %s.\n", addr_value_check == addr_value ? "ok" : "error");
764 
765  /* pca9685_set_subaddress_2/pca9685_get_subaddress_2 test */
766  pca9685_interface_debug_print("pca9685: pca9685_set_subaddress_2/pca9685_get_subaddress_2 test.\n");
767 
768  addr_value = rand() % 256;
769  addr_value &= ~(1 << 0);
770  res = pca9685_set_subaddress_2(&gs_handle, addr_value);
771  if (res != 0)
772  {
773  pca9685_interface_debug_print("pca9685: set sub address 2 failed.\n");
774  (void)pca9685_deinit(&gs_handle);
775 
776  return 1;
777  }
778  pca9685_interface_debug_print("pca9685: set sub address 2 0x%02X.\n", addr_value);
779  res = pca9685_get_subaddress_2(&gs_handle, (uint8_t *)&addr_value_check);
780  if (res != 0)
781  {
782  pca9685_interface_debug_print("pca9685: get sub address 2 failed.\n");
783  (void)pca9685_deinit(&gs_handle);
784 
785  return 1;
786  }
787  pca9685_interface_debug_print("pca9685: check sub address 2 %s.\n", addr_value_check == addr_value ? "ok" : "error");
788 
789  /* pca9685_set_subaddress_3/pca9685_get_subaddress_3 test */
790  pca9685_interface_debug_print("pca9685: pca9685_set_subaddress_3/pca9685_get_subaddress_3 test.\n");
791 
792  addr_value = rand() % 256;
793  addr_value &= ~(1 << 0);
794  res = pca9685_set_subaddress_3(&gs_handle, addr_value);
795  if (res != 0)
796  {
797  pca9685_interface_debug_print("pca9685: set sub address 3 failed.\n");
798  (void)pca9685_deinit(&gs_handle);
799 
800  return 1;
801  }
802  pca9685_interface_debug_print("pca9685: set sub address 3 0x%02X.\n", addr_value);
803  res = pca9685_get_subaddress_3(&gs_handle, (uint8_t *)&addr_value_check);
804  if (res != 0)
805  {
806  pca9685_interface_debug_print("pca9685: get sub address 3 failed.\n");
807  (void)pca9685_deinit(&gs_handle);
808 
809  return 1;
810  }
811  pca9685_interface_debug_print("pca9685: check sub address 3 %s.\n", addr_value_check == addr_value ? "ok" : "error");
812 
813  /* pca9685_set_all_call_address/pca9685_get_all_call_address test */
814  pca9685_interface_debug_print("pca9685: pca9685_set_all_call_address/pca9685_get_all_call_address test.\n");
815 
816  addr_value = rand() % 256;
817  addr_value &= ~(1 << 0);
818  res = pca9685_set_all_call_address(&gs_handle, addr_value);
819  if (res != 0)
820  {
821  pca9685_interface_debug_print("pca9685: set all call address failed.\n");
822  (void)pca9685_deinit(&gs_handle);
823 
824  return 1;
825  }
826  pca9685_interface_debug_print("pca9685: set all call address 0x%02X.\n", addr_value);
827  res = pca9685_get_all_call_address(&gs_handle, (uint8_t *)&addr_value_check);
828  if (res != 0)
829  {
830  pca9685_interface_debug_print("pca9685: get all call address failed.\n");
831  (void)pca9685_deinit(&gs_handle);
832 
833  return 1;
834  }
835  pca9685_interface_debug_print("pca9685: check all call address %s.\n", addr_value_check == addr_value ? "ok" : "error");
836 
837  /* pca9685_output_frequency_convert_to_register/pca9685_output_frequency_convert_to_data test */
838  pca9685_interface_debug_print("pca9685: pca9685_output_frequency_convert_to_register/pca9685_output_frequency_convert_to_data test.\n");
839 
840  output_freq = rand() % (1526 - 24) + 24;
841  res = pca9685_output_frequency_convert_to_register(&gs_handle, PCA9685_OSCILLATOR_INTERNAL_FREQUENCY, output_freq, (uint8_t *)&reg);
842  if (res != 0)
843  {
844  pca9685_interface_debug_print("pca9685: output frequency convert to register failed.\n");
845  (void)pca9685_deinit(&gs_handle);
846 
847  return 1;
848  }
849  pca9685_interface_debug_print("pca9685: set output frequency %d.\n", output_freq);
850  res = pca9685_output_frequency_convert_to_data(&gs_handle, PCA9685_OSCILLATOR_INTERNAL_FREQUENCY, reg, (uint16_t *)&output_freq_check);
851  if (res != 0)
852  {
853  pca9685_interface_debug_print("pca9685: output frequency convert to data failed.\n");
854  (void)pca9685_deinit(&gs_handle);
855 
856  return 1;
857  }
858  pca9685_interface_debug_print("pca9685: check output frequency %d.\n", output_freq_check);
859 
860  /* pca9685_pwm_convert_to_register/pca9685_pwm_convert_to_data test */
861  pca9685_interface_debug_print("pca9685: pca9685_pwm_convert_to_register/pca9685_pwm_convert_to_data test.\n");
862 
863  delay_percent = rand() % 10 + 10.5f;
864  high_duty_cycle_percent = rand() % 50 + 5.6f;
865  res = pca9685_pwm_convert_to_register(&gs_handle, delay_percent, high_duty_cycle_percent, (uint16_t *)&on_count, (uint16_t *)&off_count);
866  if (res != 0)
867  {
868  pca9685_interface_debug_print("pca9685: pwm convert to register failed.\n");
869  (void)pca9685_deinit(&gs_handle);
870 
871  return 1;
872  }
873  pca9685_interface_debug_print("pca9685: set delay percent %0.2f.\n", delay_percent);
874  pca9685_interface_debug_print("pca9685: set high duty cycle percent %0.2f.\n", high_duty_cycle_percent);
875  pca9685_interface_debug_print("pca9685: on_count is 0x%04X.\n", on_count);
876  pca9685_interface_debug_print("pca9685: off_count is 0x%04X.\n", off_count);
877  res = pca9685_pwm_convert_to_data(&gs_handle, on_count, off_count, (float *)&delay_percent_check, (float *)&high_duty_cycle_percent_check);
878  if (res != 0)
879  {
880  pca9685_interface_debug_print("pca9685: pwm convert to data failed.\n");
881  (void)pca9685_deinit(&gs_handle);
882 
883  return 1;
884  }
885  pca9685_interface_debug_print("pca9685: check delay percent %0.2f.\n", delay_percent_check);
886  pca9685_interface_debug_print("pca9685: check high duty cycle percent %0.2f.\n", high_duty_cycle_percent_check);
887 
888  /* pca9685_write_channel/pca9685_read_channel test */
889  pca9685_interface_debug_print("pca9685: pca9685_write_channel/pca9685_read_channel test.\n");
890 
891  for (i = 0; i < 16; i++)
892  {
893  channel = (pca9685_channel_t)(i);
894  on_count = rand() % 2048;
895  off_count = on_count + rand() % 2048;
896  res = pca9685_write_channel(&gs_handle, channel, on_count, off_count);
897  if (res != 0)
898  {
899  pca9685_interface_debug_print("pca9685: write channel failed.\n");
900  (void)pca9685_deinit(&gs_handle);
901 
902  return 1;
903  }
904  pca9685_interface_debug_print("pca9685: set channel %d.\n", i);
905  pca9685_interface_debug_print("pca9685: set on_count 0x%04X.\n", on_count);
906  pca9685_interface_debug_print("pca9685: set off_count 0x%04X.\n", off_count);
907  res = pca9685_read_channel(&gs_handle, channel, (uint16_t *)&on_count_check, (uint16_t *)&off_count_check);
908  if (res != 0)
909  {
910  pca9685_interface_debug_print("pca9685: read channel failed.\n");
911  (void)pca9685_deinit(&gs_handle);
912 
913  return 1;
914  }
915  pca9685_interface_debug_print("pca9685: check on_count %s.\n", on_count == on_count_check ? "ok" : "error");
916  pca9685_interface_debug_print("pca9685: check off_count %s.\n", off_count == off_count_check ? "ok" : "error");
917  }
918 
919  /* pca9685_write_all_channel/pca9685_read_all_channel test */
920  pca9685_interface_debug_print("pca9685: pca9685_write_all_channel/pca9685_read_all_channel test.\n");
921 
922  on_count = rand() % 2048;
923  off_count = on_count + rand() % 2048;
924  res = pca9685_write_all_channel(&gs_handle, on_count, off_count);
925  if (res != 0)
926  {
927  pca9685_interface_debug_print("pca9685: write all channel failed.\n");
928  (void)pca9685_deinit(&gs_handle);
929 
930  return 1;
931  }
932  pca9685_interface_debug_print("pca9685: set on_count 0x%04X.\n", on_count);
933  pca9685_interface_debug_print("pca9685: set off_count 0x%04X.\n", off_count);
934  res = pca9685_read_channel(&gs_handle, PCA9685_CHANNEL_0, (uint16_t *)&on_count_check, (uint16_t *)&off_count_check);
935  if (res != 0)
936  {
937  pca9685_interface_debug_print("pca9685: read all channel failed.\n");
938  (void)pca9685_deinit(&gs_handle);
939 
940  return 1;
941  }
942  pca9685_interface_debug_print("pca9685: check on_count %s.\n", on_count == on_count_check ? "ok" : "error");
943  pca9685_interface_debug_print("pca9685: check off_count %s.\n", off_count == off_count_check ? "ok" : "error");
944 
945  /* finish register test */
946  pca9685_interface_debug_print("pca9685: finish register test.\n");
947  (void)pca9685_deinit(&gs_handle);
948 
949  return 0;
950 }
driver pca9685 register test 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
#define PCA9685_OSCILLATOR_INTERNAL_FREQUENCY
pca9685 internal oscillator frequency
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
@ PCA9685_BOOL_FALSE
@ PCA9685_BOOL_TRUE
@ PCA9685_OUTPUT_DISABLE_TYPE_LEDN_LOW
@ PCA9685_OUTPUT_DISABLE_TYPE_OUTDRV_1_LEDN_LOW_OUTDRV_0_HIGH_IMPEDANCE
@ PCA9685_OUTPUT_DISABLE_TYPE_HIGH_IMPEDANCE
@ PCA9685_CHANNEL_0
@ PCA9685_OUTPUT_DRIVER_TOTEM_POLE
@ PCA9685_OUTPUT_DRIVER_OPEN_DRAIN
@ PCA9685_OUTPUT_CHANGE_STOP
@ PCA9685_OUTPUT_CHANGE_ACK
void pca9685_interface_delay_ms(uint32_t ms)
interface delay ms
uint8_t pca9685_interface_oe_deinit(void)
interface oe deinit
uint8_t pca9685_interface_oe_init(void)
interface oe init
uint8_t pca9685_interface_iic_deinit(void)
interface iic bus deinit
uint8_t pca9685_interface_iic_read(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
interface iic bus read
uint8_t pca9685_interface_iic_init(void)
interface iic bus init
void pca9685_interface_debug_print(const char *const fmt,...)
interface print format data
uint8_t pca9685_interface_oe_write(uint8_t value)
interface oe write
uint8_t pca9685_interface_iic_write(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
interface iic bus write
uint8_t pca9685_register_test(pca9685_address_t addr)
register test
pca9685 handle structure definition
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]