x_pigpiod_if2.c
Go to the documentation of this file.
1 /*
2 gcc -Wall -pthread -o x_pigpiod_if2 x_pigpiod_if2.c -lpigpiod_if2
3 ./x_pigpiod_if2
4 
5 *** WARNING ************************************************
6 * *
7 * All the tests make extensive use of gpio 25 (pin 22). *
8 * Ensure that either nothing or just a LED is connected to *
9 * gpio 25 before running any of the tests. *
10 * *
11 * Some tests are statistical in nature and so may on *
12 * occasion fail. Repeated failures on the same test or *
13 * many failures in a group of tests indicate a problem. *
14 ************************************************************
15 */
16 
17 #include <stdio.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <ctype.h>
24 
25 #include "pigpiod_if2.h"
26 
27 #define GPIO 25
28 
29 void CHECK(int t, int st, int got, int expect, int pc, char *desc)
30 {
31  if ((got >= (((1E2-pc)*expect)/1E2)) && (got <= (((1E2+pc)*expect)/1E2)))
32  {
33  printf("TEST %2d.%-2d PASS (%s: %d)\n", t, st, desc, expect);
34  }
35  else
36  {
37  fprintf(stderr,
38  "TEST %2d.%-2d FAILED got %d (%s: %d)\n",
39  t, st, got, desc, expect);
40  }
41 }
42 
43 void t0(int pi)
44 {
45  printf("\nTesting pigpiod C I/F 2\n");
46 
47  printf("pigpio version %d.\n", get_pigpio_version(pi));
48 
49  printf("Hardware revision %d.\n", get_hardware_revision(pi));
50 }
51 
52 void t1(int pi)
53 {
54  int v;
55 
56  printf("Mode/PUD/read/write tests.\n");
57 
58  set_mode(pi, GPIO, PI_INPUT);
59  v = get_mode(pi, GPIO);
60  CHECK(1, 1, v, 0, 0, "set mode, get mode");
61 
63  v = gpio_read(pi, GPIO);
64  CHECK(1, 2, v, 1, 0, "set pull up down, read");
65 
67  v = gpio_read(pi, GPIO);
68  CHECK(1, 3, v, 0, 0, "set pull up down, read");
69 
70  gpio_write(pi, GPIO, PI_LOW);
71  v = get_mode(pi, GPIO);
72  CHECK(1, 4, v, 1, 0, "write, get mode");
73 
74  v = gpio_read(pi, GPIO);
75  CHECK(1, 5, v, 0, 0, "read");
76 
77  gpio_write(pi, GPIO, PI_HIGH);
78  v = gpio_read(pi, GPIO);
79  CHECK(1, 6, v, 1, 0, "write, read");
80 }
81 
82 int t2_count=0;
83 
84 void t2cb(int pi, unsigned gpio, unsigned level, uint32_t tick)
85 {
86  t2_count++;
87 }
88 
89 void t2(int pi)
90 {
91  int dc, f, r, rr, oc, id;
92 
93  printf("PWM dutycycle/range/frequency tests.\n");
94 
95  set_PWM_range(pi, GPIO, 255);
96  set_PWM_frequency(pi, GPIO, 0);
97  f = get_PWM_frequency(pi, GPIO);
98  CHECK(2, 1, f, 10, 0, "set PWM range, set/get PWM frequency");
99 
100  id = callback(pi, GPIO, EITHER_EDGE, t2cb);
101 
102  set_PWM_dutycycle(pi, GPIO, 0);
103  dc = get_PWM_dutycycle(pi, GPIO);
104  CHECK(2, 2, dc, 0, 0, "get PWM dutycycle");
105 
106  time_sleep(0.5); /* allow old notifications to flush */
107  oc = t2_count;
108  time_sleep(2);
109  f = t2_count - oc;
110  CHECK(2, 3, f, 0, 0, "set PWM dutycycle, callback");
111 
112  set_PWM_dutycycle(pi, GPIO, 128);
113  dc = get_PWM_dutycycle(pi, GPIO);
114  CHECK(2, 4, dc, 128, 0, "get PWM dutycycle");
115 
116  time_sleep(0.2);
117  oc = t2_count;
118  time_sleep(2);
119  f = t2_count - oc;
120  CHECK(2, 5, f, 40, 5, "set PWM dutycycle, callback");
121 
122  set_PWM_frequency(pi, GPIO, 100);
123  f = get_PWM_frequency(pi, GPIO);
124  CHECK(2, 6, f, 100, 0, "set/get PWM frequency");
125 
126  time_sleep(0.2);
127  oc = t2_count;
128  time_sleep(2);
129  f = t2_count - oc;
130  CHECK(2, 7, f, 400, 1, "callback");
131 
132  set_PWM_frequency(pi, GPIO, 1000);
133  f = get_PWM_frequency(pi, GPIO);
134  CHECK(2, 8, f, 1000, 0, "set/get PWM frequency");
135 
136  time_sleep(0.2);
137  oc = t2_count;
138  time_sleep(2);
139  f = t2_count - oc;
140  CHECK(2, 9, f, 4000, 1, "callback");
141 
142  r = get_PWM_range(pi, GPIO);
143  CHECK(2, 10, r, 255, 0, "get PWM range");
144 
145  rr = get_PWM_real_range(pi, GPIO);
146  CHECK(2, 11, rr, 200, 0, "get PWM real range");
147 
148  set_PWM_range(pi, GPIO, 2000);
149  r = get_PWM_range(pi, GPIO);
150  CHECK(2, 12, r, 2000, 0, "set/get PWM range");
151 
152  rr = get_PWM_real_range(pi, GPIO);
153  CHECK(2, 13, rr, 200, 0, "get PWM real range");
154 
155  set_PWM_dutycycle(pi, GPIO, 0);
156 
157  callback_cancel(id);
158 }
159 
160 int t3_reset=1;
161 int t3_count=0;
162 uint32_t t3_tick=0;
163 float t3_on=0.0;
164 float t3_off=0.0;
165 
166 void t3cbf(int pi, unsigned gpio, unsigned level, uint32_t tick)
167 {
168  uint32_t td;
169 
170 // printf("pi=%d g=%d l=%d t=%u\n", pi, gpio, level, tick);
171  if (t3_reset)
172  {
173  t3_count = 0;
174  t3_on = 0.0;
175  t3_off = 0.0;
176  t3_reset = 0;
177  }
178  else
179  {
180  td = tick - t3_tick;
181 
182  if (level == 0) t3_on += td;
183  else t3_off += td;
184  }
185 
186  t3_count ++;
187  t3_tick = tick;
188 }
189 
190 void t3(int pi)
191 {
192  int pw[3]={500, 1500, 2500};
193  int dc[4]={20, 40, 60, 80};
194 
195  int f, rr, v;
196  float on, off;
197 
198  int t, id;
199 
200  printf("PWM/Servo pulse accuracy tests.\n");
201 
202  id = callback(pi, GPIO, EITHER_EDGE, t3cbf);
203 
204  for (t=0; t<3; t++)
205  {
206  set_servo_pulsewidth(pi, GPIO, pw[t]);
207  v = get_servo_pulsewidth(pi, GPIO);
208  CHECK(3, t+t+1, v, pw[t], 0, "get servo pulsewidth");
209 
210  time_sleep(1);
211  t3_reset = 1;
212  time_sleep(4);
213  on = t3_on;
214  off = t3_off;
215  CHECK(3, t+t+2, (1000.0*(on+off))/on, 20000000.0/pw[t], 1,
216  "set servo pulsewidth");
217  }
218 
219  set_servo_pulsewidth(pi, GPIO, 0);
220  set_PWM_frequency(pi, GPIO, 1000);
221  f = get_PWM_frequency(pi, GPIO);
222  CHECK(3, 7, f, 1000, 0, "set/get PWM frequency");
223 
224  rr = set_PWM_range(pi, GPIO, 100);
225  CHECK(3, 8, rr, 200, 0, "set PWM range");
226 
227  for (t=0; t<4; t++)
228  {
229  set_PWM_dutycycle(pi, GPIO, dc[t]);
230  v = get_PWM_dutycycle(pi, GPIO);
231  CHECK(3, t+t+9, v, dc[t], 0, "get PWM dutycycle");
232 
233  time_sleep(1);
234  t3_reset = 1;
235  time_sleep(2);
236  on = t3_on;
237  off = t3_off;
238  CHECK(3, t+t+10, (1000.0*on)/(on+off), 10.0*dc[t], 1,
239  "set PWM dutycycle");
240  }
241 
242  set_PWM_dutycycle(pi, GPIO, 0);
243 
244  callback_cancel(id);
245 }
246 
247 void t4(int pi)
248 {
249  int h, e, f, n, s, b, l, seq_ok, toggle_ok;
250  gpioReport_t r;
251  char p[32];
252 
253  printf("Pipe notification tests.\n");
254 
255  set_PWM_frequency(pi, GPIO, 0);
256  set_PWM_dutycycle(pi, GPIO, 0);
257  set_PWM_range(pi, GPIO, 100);
258 
259  h = notify_open(pi);
260 
261  sprintf(p, "/dev/pigpio%d", h);
262  f = open(p, O_RDONLY);
263 
264  e = notify_begin(pi, h, (1<<GPIO));
265  CHECK(4, 1, e, 0, 0, "notify open/begin");
266 
267  set_PWM_dutycycle(pi, GPIO, 50);
268  time_sleep(4);
269  set_PWM_dutycycle(pi, GPIO, 0);
270 
271  e = notify_pause(pi, h);
272  CHECK(4, 2, e, 0, 0, "notify pause");
273 
274  e = notify_close(pi, h);
275  CHECK(4, 3, e, 0, 0, "notify close");
276 
277  n = 0;
278  s = 0;
279  l = 0;
280  seq_ok = 1;
281  toggle_ok = 1;
282 
283  while (1)
284  {
285  b = read(f, &r, 12);
286  if (b == 12)
287  {
288  if (s != r.seqno) seq_ok = 0;
289 
290  if (n) if (l != (r.level&(1<<GPIO))) toggle_ok = 0;
291 
292  if (r.level&(1<<GPIO)) l = 0;
293  else l = (1<<GPIO);
294 
295  s++;
296  n++;
297 
298  // printf("%d %d %d %X\n", r.seqno, r.flags, r.tick, r.level);
299  }
300  else break;
301  }
302  close(f);
303 
304  CHECK(4, 4, seq_ok, 1, 0, "sequence numbers ok");
305 
306  CHECK(4, 5, toggle_ok, 1, 0, "gpio toggled ok");
307 
308  CHECK(4, 6, n, 80, 10, "number of notifications");
309 }
310 
311 int t5_count = 0;
312 
313 void t5cbf(int pi, unsigned gpio, unsigned level, uint32_t tick)
314 {
315  t5_count++;
316 }
317 
318 void t5(int pi)
319 {
320  int BAUD=4800;
321 
322  char *TEXT=
323 "\n\
324 Now is the winter of our discontent\n\
325 Made glorious summer by this sun of York;\n\
326 And all the clouds that lour'd upon our house\n\
327 In the deep bosom of the ocean buried.\n\
328 Now are our brows bound with victorious wreaths;\n\
329 Our bruised arms hung up for monuments;\n\
330 Our stern alarums changed to merry meetings,\n\
331 Our dreadful marches to delightful measures.\n\
332 Grim-visaged war hath smooth'd his wrinkled front;\n\
333 And now, instead of mounting barded steeds\n\
334 To fright the souls of fearful adversaries,\n\
335 He capers nimbly in a lady's chamber\n\
336 To the lascivious pleasing of a lute.\n\
337 ";
338 
339  gpioPulse_t wf[] =
340  {
341  {1<<GPIO, 0, 10000},
342  {0, 1<<GPIO, 30000},
343  {1<<GPIO, 0, 60000},
344  {0, 1<<GPIO, 100000},
345  };
346 
347  int e, oc, c, wid, id;
348 
349  char text[2048];
350 
351  printf("Waveforms & serial read/write tests.\n");
352 
353  id = callback(pi, GPIO, FALLING_EDGE, t5cbf);
354 
355  set_mode(pi, GPIO, PI_OUTPUT);
356 
357  e = wave_clear(pi);
358  CHECK(5, 1, e, 0, 0, "callback, set mode, wave clear");
359 
360  e = wave_add_generic(pi, 4, wf);
361  CHECK(5, 2, e, 4, 0, "pulse, wave add generic");
362 
363  wid = wave_create(pi);
364  e = wave_send_repeat(pi, wid);
365  if (e < 14) CHECK(5, 3, e, 9, 0, "wave tx repeat");
366  else CHECK(5, 3, e, 19, 0, "wave tx repeat");
367 
368  oc = t5_count;
369  time_sleep(5.05);
370  c = t5_count - oc;
371  CHECK(5, 4, c, 50, 2, "callback");
372 
373  e = wave_tx_stop(pi);
374  CHECK(5, 5, e, 0, 0, "wave tx stop");
375 
376  e = bb_serial_read_open(pi, GPIO, BAUD, 8);
377  CHECK(5, 6, e, 0, 0, "serial read open");
378 
379  wave_clear(pi);
380  e = wave_add_serial(pi, GPIO, BAUD, 8, 2, 5000000, strlen(TEXT), TEXT);
381  CHECK(5, 7, e, 3405, 0, "wave clear, wave add serial");
382 
383  wid = wave_create(pi);
384  e = wave_send_once(pi, wid);
385  if (e < 6964) CHECK(5, 8, e, 6811, 0, "wave tx start");
386  else CHECK(5, 8, e, 7116, 0, "wave tx start");
387 
388  oc = t5_count;
389  time_sleep(3);
390  c = t5_count - oc;
391  CHECK(5, 9, c, 0, 0, "callback");
392 
393  oc = t5_count;
394  while (wave_tx_busy(pi)) time_sleep(0.1);
395  time_sleep(0.1);
396  c = t5_count - oc;
397  CHECK(5, 10, c, 1702, 0, "wave tx busy, callback");
398 
399  c = bb_serial_read(pi, GPIO, text, sizeof(text)-1);
400  if (c > 0) text[c] = 0; /* null terminate string */
401  CHECK(5, 11, strcmp(TEXT, text), 0, 0, "wave tx busy, serial read");
402 
403  e = bb_serial_read_close(pi, GPIO);
404  CHECK(5, 12, e, 0, 0, "serial read close");
405 
406  c = wave_get_micros(pi);
407  CHECK(5, 13, c, 6158148, 0, "wave get micros");
408 
409  c = wave_get_high_micros(pi);
410  if (c > 6158148) c = 6158148;
411  CHECK(5, 14, c, 6158148, 0, "wave get high micros");
412 
413  c = wave_get_max_micros(pi);
414  CHECK(5, 15, c, 1800000000, 0, "wave get max micros");
415 
416  c = wave_get_pulses(pi);
417  CHECK(5, 16, c, 3405, 0, "wave get pulses");
418 
419  c = wave_get_high_pulses(pi);
420  CHECK(5, 17, c, 3405, 0, "wave get high pulses");
421 
422  c = wave_get_max_pulses(pi);
423  CHECK(5, 18, c, 12000, 0, "wave get max pulses");
424 
425  c = wave_get_cbs(pi);
426  if (c < 6963) CHECK(5, 19, c, 6810, 0, "wave get cbs");
427  else CHECK(5, 19, c, 7115, 0, "wave get cbs");
428 
429  c = wave_get_high_cbs(pi);
430  if (c < 6963) CHECK(5, 20, c, 6810, 0, "wave get high cbs");
431  else CHECK(5, 20, c, 7115, 0, "wave get high cbs");
432 
433  c = wave_get_max_cbs(pi);
434  CHECK(5, 21, c, 25016, 0, "wave get max cbs");
435 
436  callback_cancel(id);
437 }
438 
439 int t6_count=0;
440 int t6_on=0;
441 uint32_t t6_on_tick=0;
442 
443 void t6cbf(int pi, unsigned gpio, unsigned level, uint32_t tick)
444 {
445  if (level == 1)
446  {
447  t6_on_tick = tick;
448  t6_count++;
449  }
450  else
451  {
452  if (t6_on_tick) t6_on += (tick - t6_on_tick);
453  }
454 }
455 
456 void t6(int pi)
457 {
458  int tp, t, p, id;
459 
460  printf("Trigger tests.\n");
461 
462  gpio_write(pi, GPIO, PI_LOW);
463 
464  tp = 0;
465 
466  id = callback(pi, GPIO, EITHER_EDGE, t6cbf);
467 
468  time_sleep(0.2);
469 
470  for (t=0; t<5; t++)
471  {
472  time_sleep(0.1);
473  p = 10 + (t*10);
474  tp += p;
475  gpio_trigger(pi, GPIO, p, 1);
476  }
477 
478  time_sleep(0.5);
479 
480  CHECK(6, 1, t6_count, 5, 0, "gpio trigger count");
481 
482  CHECK(6, 2, t6_on, tp, 25, "gpio trigger pulse length");
483 
484  callback_cancel(id);
485 }
486 
487 int t7_count=0;
488 
489 void t7cbf(int pi, unsigned gpio, unsigned level, uint32_t tick)
490 {
491  if (level == PI_TIMEOUT) t7_count++;
492 }
493 
494 void t7(int pi)
495 {
496  int c, oc, id;
497 
498  printf("Watchdog tests.\n");
499 
500  /* type of edge shouldn't matter for watchdogs */
501  id = callback(pi, GPIO, FALLING_EDGE, t7cbf);
502 
503  set_watchdog(pi, GPIO, 50); /* 50 ms, 20 per second */
504  time_sleep(0.5);
505  oc = t7_count;
506  time_sleep(2);
507  c = t7_count - oc;
508  CHECK(7, 1, c, 39, 5, "set watchdog on count");
509 
510  set_watchdog(pi, GPIO, 0); /* 0 switches watchdog off */
511  time_sleep(0.5);
512  oc = t7_count;
513  time_sleep(2);
514  c = t7_count - oc;
515  CHECK(7, 2, c, 0, 1, "set watchdog off count");
516 
517  callback_cancel(id);
518 }
519 
520 void t8(int pi)
521 {
522  int v;
523 
524  printf("Bank read/write tests.\n");
525 
526  gpio_write(pi, GPIO, 0);
527  v = read_bank_1(pi) & (1<<GPIO);
528  CHECK(8, 1, v, 0, 0, "read bank 1");
529 
530  gpio_write(pi, GPIO, 1);
531  v = read_bank_1(pi) & (1<<GPIO);
532  CHECK(8, 2, v, (1<<GPIO), 0, "read bank 1");
533 
534  clear_bank_1(pi, 1<<GPIO);
535  v = gpio_read(pi, GPIO);
536  CHECK(8, 3, v, 0, 0, "clear bank 1");
537 
538  set_bank_1(pi, 1<<GPIO);
539  v = gpio_read(pi, GPIO);
540  CHECK(8, 4, v, 1, 0, "set bank 1");
541 
542  v = read_bank_2(pi);
543 
544  if (v) v = 0; else v = 1;
545 
546  CHECK(8, 5, v, 0, 0, "read bank 2");
547 
548  v = clear_bank_2(pi, 0);
549  CHECK(8, 6, v, 0, 0, "clear bank 2");
550 
551  v = clear_bank_2(pi, 0xffffff);
552  CHECK(8, 7, v, PI_SOME_PERMITTED, 0, "clear bank 2");
553 
554  v = set_bank_2(pi, 0);
555  CHECK(8, 8, v, 0, 0, "set bank 2");
556 
557  v = set_bank_2(pi, 0xffffff);
558  CHECK(8, 9, v, PI_SOME_PERMITTED, 0, "set bank 2");
559 }
560 
561 int t9_count = 0;
562 
563 void t9cbf(int pi, unsigned gpio, unsigned level, uint32_t tick)
564 {
565  t9_count++;
566 }
567 
568 void t9(int pi)
569 {
570  int s, oc, c, e, id;
571  uint32_t p[10];
572 
573  printf("Script store/run/status/stop/delete tests.\n");
574 
575  gpio_write(pi, GPIO, 0); /* need known state */
576 
577  /*
578  100 loops per second
579  p0 number of loops
580  p1 GPIO
581  */
582  char *script="\
583  ld p9 p0\
584  tag 0\
585  w p1 1\
586  mils 5\
587  w p1 0\
588  mils 5\
589  dcr p9\
590  jp 0";
591 
592  id = callback(pi, GPIO, RISING_EDGE, t9cbf);
593 
594  s = store_script(pi, script);
595 
596  /* Wait for script to finish initing. */
597  while (1)
598  {
599  time_sleep(0.1);
600  e = script_status(pi, s, p);
601  if (e != PI_SCRIPT_INITING) break;
602  }
603 
604  oc = t9_count;
605  p[0] = 99;
606  p[1] = GPIO;
607  run_script(pi, s, 2, p);
608  time_sleep(2);
609  c = t9_count - oc;
610  CHECK(9, 1, c, 100, 0, "store/run script");
611 
612  oc = t9_count;
613  p[0] = 200;
614  p[1] = GPIO;
615  run_script(pi, s, 2, p);
616  while (1)
617  {
618  time_sleep(0.1);
619  e = script_status(pi, s, p);
620  if (e != PI_SCRIPT_RUNNING) break;
621  }
622  c = t9_count - oc;
623  time_sleep(0.1);
624  CHECK(9, 2, c, 201, 0, "run script/script status");
625 
626  oc = t9_count;
627  p[0] = 2000;
628  p[1] = GPIO;
629  run_script(pi, s, 2, p);
630  while (1)
631  {
632  time_sleep(0.1);
633  e = script_status(pi, s, p);
634  if (e != PI_SCRIPT_RUNNING) break;
635  if (p[9] < 1600) stop_script(pi, s);
636  }
637  c = t9_count - oc;
638  time_sleep(0.1);
639  CHECK(9, 3, c, 410, 10, "run/stop script/script status");
640 
641  e = delete_script(pi, s);
642  CHECK(9, 4, e, 0, 0, "delete script");
643 
644  callback_cancel(id);
645 }
646 
647 void ta(int pi)
648 {
649  int h, b, e;
650  char *TEXT;
651  char text[2048];
652 
653  printf("Serial link tests.\n");
654 
655  /* this test needs RXD and TXD to be connected */
656 
657  h = serial_open(pi, "/dev/ttyAMA0", 57600, 0);
658 
659  CHECK(10, 1, h, 0, 0, "serial open");
660 
661  time_sleep(0.1); /* allow time for transmission */
662 
663  b = serial_read(pi, h, text, sizeof(text)); /* flush buffer */
664 
665  b = serial_data_available(pi, h);
666  CHECK(10, 2, b, 0, 0, "serial data available");
667 
668  TEXT = "\
669 To be, or not to be, that is the question-\
670 Whether 'tis Nobler in the mind to suffer\
671 The Slings and Arrows of outrageous Fortune,\
672 Or to take Arms against a Sea of troubles,\
673 ";
674  e = serial_write(pi, h, TEXT, strlen(TEXT));
675  CHECK(10, 3, e, 0, 0, "serial write");
676 
677  e = serial_write_byte(pi, h, 0xAA);
678  e = serial_write_byte(pi, h, 0x55);
679  e = serial_write_byte(pi, h, 0x00);
680  e = serial_write_byte(pi, h, 0xFF);
681 
682  CHECK(10, 4, e, 0, 0, "serial write byte");
683 
684  time_sleep(0.1); /* allow time for transmission */
685 
686  b = serial_data_available(pi, h);
687  CHECK(10, 5, b, strlen(TEXT)+4, 0, "serial data available");
688 
689  b = serial_read(pi, h, text, strlen(TEXT));
690  CHECK(10, 6, b, strlen(TEXT), 0, "serial read");
691  if (b >= 0) text[b] = 0;
692  CHECK(10, 7, strcmp(TEXT, text), 0, 0, "serial read");
693 
694  b = serial_read_byte(pi, h);
695  CHECK(10, 8, b, 0xAA, 0, "serial read byte");
696 
697  b = serial_read_byte(pi, h);
698  CHECK(10, 9, b, 0x55, 0, "serial read byte");
699 
700  b = serial_read_byte(pi, h);
701  CHECK(10, 10, b, 0x00, 0, "serial read byte");
702 
703  b = serial_read_byte(pi, h);
704  CHECK(10, 11, b, 0xFF, 0, "serial read byte");
705 
706  b = serial_data_available(pi, h);
707  CHECK(10, 12, b, 0, 0, "serial data availabe");
708 
709  e = serial_close(pi, h);
710  CHECK(10, 13, e, 0, 0, "serial close");
711 }
712 
713 void tb(int pi)
714 {
715  int h, e, b, len;
716  char *exp;
717  char buf[128];
718 
719  printf("SMBus / I2C tests.");
720 
721  /* this test requires an ADXL345 on I2C bus 1 addr 0x53 */
722 
723  h = i2c_open(pi, 1, 0x53, 0);
724  CHECK(11, 1, h, 0, 0, "i2c open");
725 
726  e = i2c_write_device(pi, h, "\x00", 1); /* move to known register */
727  CHECK(11, 2, e, 0, 0, "i2c write device");
728 
729  b = i2c_read_device(pi, h, buf, 1);
730  CHECK(11, 3, b, 1, 0, "i2c read device");
731  CHECK(11, 4, buf[0], 0xE5, 0, "i2c read device");
732 
733  b = i2c_read_byte(pi, h);
734  CHECK(11, 5, b, 0xE5, 0, "i2c read byte");
735 
736  b = i2c_read_byte_data(pi, h, 0);
737  CHECK(11, 6, b, 0xE5, 0, "i2c read byte data");
738 
739  b = i2c_read_byte_data(pi, h, 48);
740  CHECK(11, 7, b, 2, 0, "i2c read byte data");
741 
742  exp = "\x1D[aBcDeFgHjKM]";
743  len = strlen(exp);
744 
745  e = i2c_write_device(pi, h, exp, len);
746  CHECK(11, 8, e, 0, 0, "i2c write device");
747 
748  e = i2c_write_device(pi, h, "\x1D", 1);
749  b = i2c_read_device(pi, h, buf, len-1);
750  CHECK(11, 9, b, len-1, 0, "i2c read device");
751  CHECK(11, 10, strncmp(buf, exp+1, len-1), 0, 0, "i2c read device");
752 
753  if (strncmp(buf, exp+1, len-1))
754  printf("got [%.*s] expected [%.*s]\n", len-1, buf, len-1, exp+1);
755 
756  e = i2c_write_byte_data(pi, h, 0x1d, 0xAA);
757  CHECK(11, 11, e, 0, 0, "i2c write byte data");
758 
759  b = i2c_read_byte_data(pi, h, 0x1d);
760  CHECK(11, 12, b, 0xAA, 0, "i2c read byte data");
761 
762  e = i2c_write_byte_data(pi, h, 0x1d, 0x55);
763  CHECK(11, 13, e, 0, 0, "i2c write byte data");
764 
765  b = i2c_read_byte_data(pi, h, 0x1d);
766  CHECK(11, 14, b, 0x55, 0, "i2c read byte data");
767 
768  exp = "[1234567890#]";
769  len = strlen(exp);
770 
771  e = i2c_write_block_data(pi, h, 0x1C, exp, len);
772  CHECK(11, 15, e, 0, 0, "i2c write block data");
773 
774  e = i2c_write_device(pi, h, "\x1D", 1);
775  b = i2c_read_device(pi, h, buf, len);
776  CHECK(11, 16, b, len, 0, "i2c read device");
777  CHECK(11, 17, strncmp(buf, exp, len), 0, 0, "i2c read device");
778 
779  if (strncmp(buf, exp, len))
780  printf("got [%.*s] expected [%.*s]\n", len, buf, len, exp);
781 
782  b = i2c_read_i2c_block_data(pi, h, 0x1D, buf, len);
783  CHECK(11, 18, b, len, 0, "i2c read i2c block data");
784  CHECK(11, 19, strncmp(buf, exp, len), 0, 0, "i2c read i2c block data");
785 
786  if (strncmp(buf, exp, len))
787  printf("got [%.*s] expected [%.*s]\n", len, buf, len, exp);
788 
789  exp = "(-+=;:,<>!%)";
790  len = strlen(exp);
791 
792  e = i2c_write_i2c_block_data(pi, h, 0x1D, exp, len);
793  CHECK(11, 20, e, 0, 0, "i2c write i2c block data");
794 
795  b = i2c_read_i2c_block_data(pi, h, 0x1D, buf, len);
796  CHECK(11, 21, b, len, 0, "i2c read i2c block data");
797  CHECK(11, 22, strncmp(buf, exp, len), 0, 0, "i2c read i2c block data");
798 
799  if (strncmp(buf, exp, len))
800  printf("got [%.*s] expected [%.*s]\n", len, buf, len, exp);
801 
802  e = i2c_close(pi, h);
803  CHECK(11, 23, e, 0, 0, "i2c close");
804 }
805 
806 void tc(int pi)
807 {
808  int h, x, b, e;
809  char buf[128];
810 
811  printf("SPI tests.");
812 
813  /* this test requires a MCP3202 on SPI channel 1 */
814 
815  h = spi_open(pi, 1, 50000, 0);
816  CHECK(12, 1, h, 0, 0, "spi open");
817 
818 
819  for (x=0; x<5; x++)
820  {
821  sprintf(buf, "\x01\x80");
822  b = spi_xfer(pi, h, buf, buf, 3);
823  CHECK(12, 2, b, 3, 0, "spi xfer");
824  if (b == 3)
825  {
826  time_sleep(1.0);
827  printf("%d ", ((buf[1]&0x0F)*256)|buf[2]);
828  }
829  }
830 
831  e = spi_close(pi, h);
832  CHECK(12, 99, e, 0, 0, "spi close");
833 }
834 
835 
836 int main(int argc, char *argv[])
837 {
838  int i, t, c, pi;
839 
840  char test[64]={0,};
841 
842  if (argc > 1)
843  {
844  t = 0;
845 
846  for (i=0; i<strlen(argv[1]); i++)
847  {
848  c = tolower(argv[1][i]);
849 
850  if (!strchr(test, c))
851  {
852  test[t++] = c;
853  test[t] = 0;
854  }
855  }
856  }
857  else strcat(test, "0123456789");
858 
859  pi = pigpio_start(0, 0);
860 
861  if (pi < 0)
862  {
863  fprintf(stderr, "pigpio initialisation failed (%d).\n", pi);
864  return 1;
865  }
866 
867  printf("Connected to pigpio daemon (%d).\n", pi);
868 
869  if (strchr(test, '0')) t0(pi);
870  if (strchr(test, '1')) t1(pi);
871  if (strchr(test, '2')) t2(pi);
872  if (strchr(test, '3')) t3(pi);
873  if (strchr(test, '4')) t4(pi);
874  if (strchr(test, '5')) t5(pi);
875  if (strchr(test, '6')) t6(pi);
876  if (strchr(test, '7')) t7(pi);
877  if (strchr(test, '8')) t8(pi);
878  if (strchr(test, '9')) t9(pi);
879  if (strchr(test, 'a')) ta(pi);
880  if (strchr(test, 'b')) tb(pi);
881  if (strchr(test, 'c')) tc(pi);
882 
883  pigpio_stop(pi);
884 
885  return 0;
886 }
887 
void t1(int pi)
Definition: x_pigpiod_if2.c:52
int wave_tx_busy(void)
Definition: pigpiod_if.c:772
void CHECK(int t, int st, int got, int expect, int pc, char *desc)
Definition: x_pigpiod_if2.c:29
s
Definition: DHT22.py:257
int i2c_write_byte_data(unsigned handle, unsigned reg, uint32_t val)
Definition: pigpiod_if.c:1007
void t5(int pi)
int t6_count
int serial_write(unsigned handle, char *buf, unsigned count)
Definition: pigpiod_if.c:1430
void t2(int pi)
Definition: x_pigpiod_if2.c:89
int bb_serial_read(unsigned user_gpio, void *buf, size_t bufSize)
Definition: pigpiod_if.c:954
int set_bank_1(uint32_t levels)
Definition: pigpiod_if.c:637
void t6cbf(int pi, unsigned gpio, unsigned level, uint32_t tick)
void t0(int pi)
Definition: x_pigpiod_if2.c:43
int gpio_read(unsigned gpio)
Definition: pigpiod_if.c:577
int i2c_read_i2c_block_data(unsigned handle, unsigned reg, char *buf, uint32_t count)
Definition: pigpiod_if.c:1136
int spi_xfer(unsigned handle, char *txBuf, char *rxBuf, unsigned count)
Definition: pigpiod_if.c:1370
f
int set_bank_2(uint32_t levels)
Definition: pigpiod_if.c:640
int i2c_write_device(unsigned handle, char *buf, unsigned count)
Definition: pigpiod_if.c:1203
int i2c_read_byte_data(unsigned handle, unsigned reg)
Definition: pigpiod_if.c:1045
int bb_serial_read_open(unsigned user_gpio, unsigned baud, uint32_t bbBits)
Definition: pigpiod_if.c:935
int t5_count
uint32_t t3_tick
int wave_get_pulses(void)
Definition: pigpiod_if.c:787
int wave_send_repeat(unsigned wave_id)
Definition: pigpiod_if.c:750
void callback(uint32_t hash)
int wave_add_serial(unsigned user_gpio, unsigned baud, uint32_t databits, uint32_t stophalfbits, uint32_t offset, unsigned numChar, char *str)
Definition: pigpiod_if.c:701
void t4(int pi)
int script_status(unsigned script_id, uint32_t *param)
Definition: pigpiod_if.c:910
void tc(int pi)
uint32_t t6_on_tick
int wave_create(void)
Definition: pigpiod_if.c:735
int i2c_open(unsigned i2c_bus, unsigned i2c_addr, uint32_t i2c_flags)
Definition: pigpiod_if.c:976
uint32_t read_bank_2(void)
Definition: pigpiod_if.c:628
int store_script(char *script)
Definition: pigpiod_if.c:846
int spi_open(unsigned channel, unsigned speed, uint32_t flags)
Definition: pigpiod_if.c:1312
int serial_data_available(unsigned handle)
Definition: pigpiod_if.c:1466
int set_PWM_frequency(unsigned user_gpio, unsigned frequency)
Definition: pigpiod_if.c:598
void t3(int pi)
#define PI_SCRIPT_RUNNING
Definition: pigpio.h:805
void pigpio_stop(void)
Definition: pigpiod_if.c:533
int i2c_close(unsigned handle)
Definition: pigpiod_if.c:995
#define FALLING_EDGE
Definition: pigpio.h:877
#define PI_SOME_PERMITTED
Definition: pigpio.h:6306
int wave_get_max_cbs(void)
Definition: pigpiod_if.c:802
#define PI_PUD_UP
Definition: pigpio.h:594
#define GPIO
Definition: x_pigpiod_if2.c:27
int i2c_read_device(unsigned handle, char *buf, unsigned count)
Definition: pigpiod_if.c:1187
int t6_on
int t3_count
int serial_open(char *dev, unsigned baud, unsigned flags)
Definition: pigpiod_if.c:1399
int get_servo_pulsewidth(unsigned user_gpio)
Definition: pigpiod_if.c:607
void t3cbf(int pi, unsigned gpio, unsigned level, uint32_t tick)
int wave_get_max_pulses(void)
Definition: pigpiod_if.c:793
void t9(int pi)
int get_PWM_range(unsigned user_gpio)
Definition: pigpiod_if.c:592
int t7_count
#define PI_SCRIPT_INITING
Definition: pigpio.h:803
int main(int argc, char *argv[])
int t3_reset
int serial_read_byte(unsigned handle)
Definition: pigpiod_if.c:1427
int wave_get_high_cbs(void)
Definition: pigpiod_if.c:799
int t9_count
int get_PWM_real_range(unsigned user_gpio)
Definition: pigpiod_if.c:595
#define RISING_EDGE
Definition: pigpio.h:876
int set_mode(unsigned gpio, unsigned mode)
Definition: pigpiod_if.c:568
int delete_script(unsigned script_id)
Definition: pigpiod_if.c:932
#define PI_INPUT
Definition: pigpio.h:581
int r
Definition: DHT22.py:259
int run_script(unsigned script_id, unsigned numPar, uint32_t *param)
Definition: pigpiod_if.c:869
int pigpio_start(char *addrStr, char *portStr)
Definition: pigpiod_if.c:493
int set_PWM_dutycycle(unsigned user_gpio, unsigned dutycycle)
Definition: pigpiod_if.c:583
int wave_add_generic(unsigned numPulses, gpioPulse_t *pulses)
Definition: pigpiod_if.c:680
int set_servo_pulsewidth(unsigned user_gpio, unsigned pulsewidth)
Definition: pigpiod_if.c:604
uint32_t read_bank_1(void)
Definition: pigpiod_if.c:625
void t9cbf(int pi, unsigned gpio, unsigned level, uint32_t tick)
int wave_tx_stop(void)
Definition: pigpiod_if.c:775
int gpio_write(unsigned gpio, unsigned level)
Definition: pigpiod_if.c:580
int gpio_trigger(unsigned user_gpio, unsigned pulseLen, uint32_t level)
Definition: pigpiod_if.c:805
uint32_t get_hardware_revision(void)
Definition: pigpiod_if.c:668
void t5cbf(int pi, unsigned gpio, unsigned level, uint32_t tick)
int wave_get_max_micros(void)
Definition: pigpiod_if.c:784
static rawWave_t wf[3][PI_WAVE_MAX_PULSES]
Definition: pigpio.c:1228
void ta(int pi)
int wave_get_high_pulses(void)
Definition: pigpiod_if.c:790
int get_mode(unsigned gpio)
Definition: pigpiod_if.c:571
#define PI_OUTPUT
Definition: pigpio.h:582
dc
uint32_t get_pigpio_version(void)
Definition: pigpiod_if.c:671
void t6(int pi)
#define PI_LOW
Definition: pigpio.h:572
void t7(int pi)
int serial_close(unsigned handle)
Definition: pigpiod_if.c:1421
int i2c_write_i2c_block_data(unsigned handle, unsigned reg, char *buf, unsigned count)
Definition: pigpiod_if.c:1167
int wave_get_cbs(void)
Definition: pigpiod_if.c:796
void tb(int pi)
int serial_read(unsigned handle, char *buf, unsigned count)
Definition: pigpiod_if.c:1449
int i2c_write_block_data(unsigned handle, unsigned reg, char *buf, unsigned count)
Definition: pigpiod_if.c:1070
int clear_bank_1(uint32_t levels)
Definition: pigpiod_if.c:631
#define EITHER_EDGE
Definition: pigpio.h:878
float t3_on
Definition: gpio.h:23
pi
Definition: dht11.py:156
int get_PWM_frequency(unsigned user_gpio)
Definition: pigpiod_if.c:601
def td()
Definition: x_pigpio.py:859
void time_sleep(double seconds)
Definition: pigpio.c:8374
int notify_open(void)
Definition: pigpiod_if.c:610
void t7cbf(int pi, unsigned gpio, unsigned level, uint32_t tick)
int bb_serial_read_close(unsigned user_gpio)
Definition: pigpiod_if.c:970
void t8(int pi)
int wave_send_once(unsigned wave_id)
Definition: pigpiod_if.c:747
int i2c_read_byte(unsigned handle)
Definition: pigpiod_if.c:1004
int set_PWM_range(unsigned user_gpio, unsigned range)
Definition: pigpiod_if.c:589
int spi_close(unsigned handle)
Definition: pigpiod_if.c:1331
int wave_get_micros(void)
Definition: pigpiod_if.c:778
int serial_write_byte(unsigned handle, unsigned val)
Definition: pigpiod_if.c:1424
int set_pull_up_down(unsigned gpio, unsigned pud)
Definition: pigpiod_if.c:574
void t2cb(int pi, unsigned gpio, unsigned level, uint32_t tick)
Definition: x_pigpiod_if2.c:84
int clear_bank_2(uint32_t levels)
Definition: pigpiod_if.c:634
int wave_get_high_micros(void)
Definition: pigpiod_if.c:781
#define PI_HIGH
Definition: pigpio.h:573
int notify_begin(unsigned handle, uint32_t bits)
Definition: pigpiod_if.c:613
#define PI_TIMEOUT
Definition: pigpio.h:577
int wave_clear(void)
Definition: pigpiod_if.c:674
int callback_cancel(unsigned id)
Definition: pigpiod_if.c:1526
#define PI_PUD_DOWN
Definition: pigpio.h:593
int notify_close(unsigned handle)
Definition: pigpiod_if.c:619
int get_PWM_dutycycle(unsigned user_gpio)
Definition: pigpiod_if.c:586
uint16_t seqno
Definition: pigpio.h:416
uint32_t level
Definition: pigpio.h:419
float t3_off
int set_watchdog(unsigned user_gpio, unsigned timeout)
Definition: pigpiod_if.c:622
int notify_pause(unsigned handle)
Definition: pigpiod_if.c:616
int stop_script(unsigned script_id)
Definition: pigpiod_if.c:929
int t2_count
Definition: x_pigpiod_if2.c:82


cob_hand_bridge
Author(s): Mathias Lüdtke
autogenerated on Tue Oct 20 2020 03:35:57