18 #include <sys/types.h> 29 void CHECK(
int t,
int st,
int got,
int expect,
int pc,
char *desc)
31 if ((got >= (((1E2-pc)*expect)/1E2)) && (got <= (((1E2+pc)*expect)/1E2)))
33 printf(
"TEST %2d.%-2d PASS (%s: %d)\n", t, st, desc, expect);
38 "TEST %2d.%-2d FAILED got %d (%s: %d)\n",
39 t, st, got, desc, expect);
45 printf(
"\nTesting pigpiod C I/F 2\n");
56 printf(
"Mode/PUD/read/write tests.\n");
60 CHECK(1, 1, v, 0, 0,
"set mode, get mode");
64 CHECK(1, 2, v, 1, 0,
"set pull up down, read");
68 CHECK(1, 3, v, 0, 0,
"set pull up down, read");
72 CHECK(1, 4, v, 1, 0,
"write, get mode");
75 CHECK(1, 5, v, 0, 0,
"read");
79 CHECK(1, 6, v, 1, 0,
"write, read");
84 void t2cb(
int pi,
unsigned gpio,
unsigned level, uint32_t tick)
91 int dc,
f,
r, rr, oc, id;
93 printf(
"PWM dutycycle/range/frequency tests.\n");
98 CHECK(2, 1, f, 10, 0,
"set PWM range, set/get PWM frequency");
104 CHECK(2, 2, dc, 0, 0,
"get PWM dutycycle");
110 CHECK(2, 3, f, 0, 0,
"set PWM dutycycle, callback");
114 CHECK(2, 4, dc, 128, 0,
"get PWM dutycycle");
120 CHECK(2, 5, f, 40, 5,
"set PWM dutycycle, callback");
124 CHECK(2, 6, f, 100, 0,
"set/get PWM frequency");
130 CHECK(2, 7, f, 400, 1,
"callback");
134 CHECK(2, 8, f, 1000, 0,
"set/get PWM frequency");
140 CHECK(2, 9, f, 4000, 1,
"callback");
143 CHECK(2, 10, r, 255, 0,
"get PWM range");
146 CHECK(2, 11, rr, 200, 0,
"get PWM real range");
150 CHECK(2, 12, r, 2000, 0,
"set/get PWM range");
153 CHECK(2, 13, rr, 200, 0,
"get PWM real range");
166 void t3cbf(
int pi,
unsigned gpio,
unsigned level, uint32_t tick)
192 int pw[3]={500, 1500, 2500};
193 int dc[4]={20, 40, 60, 80};
200 printf(
"PWM/Servo pulse accuracy tests.\n");
208 CHECK(3, t+t+1, v, pw[t], 0,
"get servo pulsewidth");
215 CHECK(3, t+t+2, (1000.0*(on+off))/on, 20000000.0/pw[t], 1,
216 "set servo pulsewidth");
222 CHECK(3, 7, f, 1000, 0,
"set/get PWM frequency");
225 CHECK(3, 8, rr, 200, 0,
"set PWM range");
231 CHECK(3, t+t+9, v, dc[t], 0,
"get PWM dutycycle");
238 CHECK(3, t+t+10, (1000.0*on)/(on+off), 10.0*dc[t], 1,
239 "set PWM dutycycle");
249 int h, e,
f, n,
s, b, l, seq_ok, toggle_ok;
253 printf(
"Pipe notification tests.\n");
261 sprintf(p,
"/dev/pigpio%d", h);
262 f = open(p, O_RDONLY);
265 CHECK(4, 1, e, 0, 0,
"notify open/begin");
272 CHECK(4, 2, e, 0, 0,
"notify pause");
275 CHECK(4, 3, e, 0, 0,
"notify close");
288 if (s != r.
seqno) seq_ok = 0;
290 if (n)
if (l != (r.
level&(1<<
GPIO))) toggle_ok = 0;
304 CHECK(4, 4, seq_ok, 1, 0,
"sequence numbers ok");
306 CHECK(4, 5, toggle_ok, 1, 0,
"gpio toggled ok");
308 CHECK(4, 6, n, 80, 10,
"number of notifications");
313 void t5cbf(
int pi,
unsigned gpio,
unsigned level, uint32_t tick)
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\ 344 {0, 1<<
GPIO, 100000},
347 int e, oc,
c, wid, id;
351 printf(
"Waveforms & serial read/write tests.\n");
358 CHECK(5, 1, e, 0, 0,
"callback, set mode, wave clear");
361 CHECK(5, 2, e, 4, 0,
"pulse, wave add generic");
365 if (e < 14)
CHECK(5, 3, e, 9, 0,
"wave tx repeat");
366 else CHECK(5, 3, e, 19, 0,
"wave tx repeat");
371 CHECK(5, 4, c, 50, 2,
"callback");
374 CHECK(5, 5, e, 0, 0,
"wave tx stop");
377 CHECK(5, 6, e, 0, 0,
"serial read open");
381 CHECK(5, 7, e, 3405, 0,
"wave clear, wave add serial");
385 if (e < 6964)
CHECK(5, 8, e, 6811, 0,
"wave tx start");
386 else CHECK(5, 8, e, 7116, 0,
"wave tx start");
391 CHECK(5, 9, c, 0, 0,
"callback");
397 CHECK(5, 10, c, 1702, 0,
"wave tx busy, callback");
400 if (c > 0) text[
c] = 0;
401 CHECK(5, 11, strcmp(TEXT, text), 0, 0,
"wave tx busy, serial read");
404 CHECK(5, 12, e, 0, 0,
"serial read close");
407 CHECK(5, 13, c, 6158148, 0,
"wave get micros");
410 if (c > 6158148) c = 6158148;
411 CHECK(5, 14, c, 6158148, 0,
"wave get high micros");
414 CHECK(5, 15, c, 1800000000, 0,
"wave get max micros");
417 CHECK(5, 16, c, 3405, 0,
"wave get pulses");
420 CHECK(5, 17, c, 3405, 0,
"wave get high pulses");
423 CHECK(5, 18, c, 12000, 0,
"wave get max pulses");
426 if (c < 6963)
CHECK(5, 19, c, 6810, 0,
"wave get cbs");
427 else CHECK(5, 19, c, 7115, 0,
"wave get cbs");
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");
434 CHECK(5, 21, c, 25016, 0,
"wave get max cbs");
443 void t6cbf(
int pi,
unsigned gpio,
unsigned level, uint32_t tick)
460 printf(
"Trigger tests.\n");
482 CHECK(6, 2,
t6_on, tp, 25,
"gpio trigger pulse length");
489 void t7cbf(
int pi,
unsigned gpio,
unsigned level, uint32_t tick)
498 printf(
"Watchdog tests.\n");
508 CHECK(7, 1, c, 39, 5,
"set watchdog on count");
515 CHECK(7, 2, c, 0, 1,
"set watchdog off count");
524 printf(
"Bank read/write tests.\n");
528 CHECK(8, 1, v, 0, 0,
"read bank 1");
532 CHECK(8, 2, v, (1<<
GPIO), 0,
"read bank 1");
536 CHECK(8, 3, v, 0, 0,
"clear bank 1");
540 CHECK(8, 4, v, 1, 0,
"set bank 1");
544 if (v) v = 0;
else v = 1;
546 CHECK(8, 5, v, 0, 0,
"read bank 2");
549 CHECK(8, 6, v, 0, 0,
"clear bank 2");
555 CHECK(8, 8, v, 0, 0,
"set bank 2");
563 void t9cbf(
int pi,
unsigned gpio,
unsigned level, uint32_t tick)
573 printf(
"Script store/run/status/stop/delete tests.\n");
610 CHECK(9, 1, c, 100, 0,
"store/run script");
624 CHECK(9, 2, c, 201, 0,
"run script/script status");
639 CHECK(9, 3, c, 410, 10,
"run/stop script/script status");
642 CHECK(9, 4, e, 0, 0,
"delete script");
653 printf(
"Serial link tests.\n");
659 CHECK(10, 1, h, 0, 0,
"serial open");
666 CHECK(10, 2, b, 0, 0,
"serial data available");
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,\ 675 CHECK(10, 3, e, 0, 0,
"serial write");
682 CHECK(10, 4, e, 0, 0,
"serial write byte");
687 CHECK(10, 5, b, strlen(TEXT)+4, 0,
"serial data available");
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");
695 CHECK(10, 8, b, 0xAA, 0,
"serial read byte");
698 CHECK(10, 9, b, 0x55, 0,
"serial read byte");
701 CHECK(10, 10, b, 0x00, 0,
"serial read byte");
704 CHECK(10, 11, b, 0xFF, 0,
"serial read byte");
707 CHECK(10, 12, b, 0, 0,
"serial data availabe");
710 CHECK(10, 13, e, 0, 0,
"serial close");
719 printf(
"SMBus / I2C tests.");
724 CHECK(11, 1, h, 0, 0,
"i2c open");
727 CHECK(11, 2, e, 0, 0,
"i2c write device");
730 CHECK(11, 3, b, 1, 0,
"i2c read device");
731 CHECK(11, 4, buf[0], 0xE5, 0,
"i2c read device");
734 CHECK(11, 5, b, 0xE5, 0,
"i2c read byte");
737 CHECK(11, 6, b, 0xE5, 0,
"i2c read byte data");
740 CHECK(11, 7, b, 2, 0,
"i2c read byte data");
742 exp =
"\x1D[aBcDeFgHjKM]";
746 CHECK(11, 8, e, 0, 0,
"i2c write device");
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");
753 if (strncmp(buf, exp+1, len-1))
754 printf(
"got [%.*s] expected [%.*s]\n", len-1, buf, len-1, exp+1);
757 CHECK(11, 11, e, 0, 0,
"i2c write byte data");
760 CHECK(11, 12, b, 0xAA, 0,
"i2c read byte data");
763 CHECK(11, 13, e, 0, 0,
"i2c write byte data");
766 CHECK(11, 14, b, 0x55, 0,
"i2c read byte data");
768 exp =
"[1234567890#]";
772 CHECK(11, 15, e, 0, 0,
"i2c write block data");
776 CHECK(11, 16, b, len, 0,
"i2c read device");
777 CHECK(11, 17, strncmp(buf, exp, len), 0, 0,
"i2c read device");
779 if (strncmp(buf, exp, len))
780 printf(
"got [%.*s] expected [%.*s]\n", len, buf, len, exp);
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");
786 if (strncmp(buf, exp, len))
787 printf(
"got [%.*s] expected [%.*s]\n", len, buf, len, exp);
789 exp =
"(-+=;:,<>!%)";
793 CHECK(11, 20, e, 0, 0,
"i2c write i2c block data");
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");
799 if (strncmp(buf, exp, len))
800 printf(
"got [%.*s] expected [%.*s]\n", len, buf, len, exp);
803 CHECK(11, 23, e, 0, 0,
"i2c close");
811 printf(
"SPI tests.");
816 CHECK(12, 1, h, 0, 0,
"spi open");
821 sprintf(buf,
"\x01\x80");
823 CHECK(12, 2, b, 3, 0,
"spi xfer");
827 printf(
"%d ", ((buf[1]&0x0F)*256)|buf[2]);
832 CHECK(12, 99, e, 0, 0,
"spi close");
836 int main(
int argc,
char *argv[])
846 for (i=0; i<strlen(argv[1]); i++)
848 c = tolower(argv[1][i]);
850 if (!strchr(test, c))
857 else strcat(test,
"0123456789");
863 fprintf(stderr,
"pigpio initialisation failed (%d).\n", pi);
867 printf(
"Connected to pigpio daemon (%d).\n", pi);
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);
void CHECK(int t, int st, int got, int expect, int pc, char *desc)
int i2c_write_byte_data(unsigned handle, unsigned reg, uint32_t val)
int serial_write(unsigned handle, char *buf, unsigned count)
int bb_serial_read(unsigned user_gpio, void *buf, size_t bufSize)
int set_bank_1(uint32_t levels)
void t6cbf(int pi, unsigned gpio, unsigned level, uint32_t tick)
int gpio_read(unsigned gpio)
int i2c_read_i2c_block_data(unsigned handle, unsigned reg, char *buf, uint32_t count)
int spi_xfer(unsigned handle, char *txBuf, char *rxBuf, unsigned count)
int set_bank_2(uint32_t levels)
int i2c_write_device(unsigned handle, char *buf, unsigned count)
int i2c_read_byte_data(unsigned handle, unsigned reg)
int bb_serial_read_open(unsigned user_gpio, unsigned baud, uint32_t bbBits)
int wave_get_pulses(void)
int wave_send_repeat(unsigned wave_id)
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)
int script_status(unsigned script_id, uint32_t *param)
int i2c_open(unsigned i2c_bus, unsigned i2c_addr, uint32_t i2c_flags)
uint32_t read_bank_2(void)
int store_script(char *script)
int spi_open(unsigned channel, unsigned speed, uint32_t flags)
int serial_data_available(unsigned handle)
int set_PWM_frequency(unsigned user_gpio, unsigned frequency)
#define PI_SCRIPT_RUNNING
int i2c_close(unsigned handle)
#define PI_SOME_PERMITTED
int wave_get_max_cbs(void)
int i2c_read_device(unsigned handle, char *buf, unsigned count)
int serial_open(char *dev, unsigned baud, unsigned flags)
int get_servo_pulsewidth(unsigned user_gpio)
void t3cbf(int pi, unsigned gpio, unsigned level, uint32_t tick)
int wave_get_max_pulses(void)
int get_PWM_range(unsigned user_gpio)
#define PI_SCRIPT_INITING
int main(int argc, char *argv[])
int serial_read_byte(unsigned handle)
int wave_get_high_cbs(void)
int get_PWM_real_range(unsigned user_gpio)
int set_mode(unsigned gpio, unsigned mode)
int delete_script(unsigned script_id)
int run_script(unsigned script_id, unsigned numPar, uint32_t *param)
int pigpio_start(char *addrStr, char *portStr)
int set_PWM_dutycycle(unsigned user_gpio, unsigned dutycycle)
int wave_add_generic(unsigned numPulses, gpioPulse_t *pulses)
int set_servo_pulsewidth(unsigned user_gpio, unsigned pulsewidth)
uint32_t read_bank_1(void)
void t9cbf(int pi, unsigned gpio, unsigned level, uint32_t tick)
int gpio_write(unsigned gpio, unsigned level)
int gpio_trigger(unsigned user_gpio, unsigned pulseLen, uint32_t level)
uint32_t get_hardware_revision(void)
void t5cbf(int pi, unsigned gpio, unsigned level, uint32_t tick)
int wave_get_max_micros(void)
static rawWave_t wf[3][PI_WAVE_MAX_PULSES]
int wave_get_high_pulses(void)
int get_mode(unsigned gpio)
uint32_t get_pigpio_version(void)
int serial_close(unsigned handle)
int i2c_write_i2c_block_data(unsigned handle, unsigned reg, char *buf, unsigned count)
int serial_read(unsigned handle, char *buf, unsigned count)
int i2c_write_block_data(unsigned handle, unsigned reg, char *buf, unsigned count)
int clear_bank_1(uint32_t levels)
int get_PWM_frequency(unsigned user_gpio)
void time_sleep(double seconds)
void t7cbf(int pi, unsigned gpio, unsigned level, uint32_t tick)
int bb_serial_read_close(unsigned user_gpio)
int wave_send_once(unsigned wave_id)
int i2c_read_byte(unsigned handle)
int set_PWM_range(unsigned user_gpio, unsigned range)
int spi_close(unsigned handle)
int wave_get_micros(void)
int serial_write_byte(unsigned handle, unsigned val)
int set_pull_up_down(unsigned gpio, unsigned pud)
void t2cb(int pi, unsigned gpio, unsigned level, uint32_t tick)
int clear_bank_2(uint32_t levels)
int wave_get_high_micros(void)
int notify_begin(unsigned handle, uint32_t bits)
int callback_cancel(unsigned id)
int notify_close(unsigned handle)
int get_PWM_dutycycle(unsigned user_gpio)
int set_watchdog(unsigned user_gpio, unsigned timeout)
int notify_pause(unsigned handle)
int stop_script(unsigned script_id)