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 1\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(
unsigned gpio,
unsigned level, uint32_t tick)
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");
164 void t3cbf(
unsigned gpio,
unsigned level, uint32_t tick)
189 int pw[3]={500, 1500, 2500};
190 int dc[4]={20, 40, 60, 80};
197 printf(
"PWM/Servo pulse accuracy tests.\n");
205 CHECK(3, t+t+1, v, pw[t], 0,
"get servo pulsewidth");
212 CHECK(3, t+t+2, (1000.0*(on+off))/on, 20000000.0/pw[t], 1,
213 "set servo pulsewidth");
219 CHECK(3, 7, f, 1000, 0,
"set/get PWM frequency");
222 CHECK(3, 8, rr, 200, 0,
"set PWM range");
228 CHECK(3, t+t+9, v, dc[t], 0,
"get PWM dutycycle");
235 CHECK(3, t+t+10, (1000.0*on)/(on+off), 10.0*dc[t], 1,
236 "set PWM dutycycle");
245 int h, e,
f, n,
s, b, l, seq_ok, toggle_ok;
249 printf(
"Pipe notification tests.\n");
257 sprintf(p,
"/dev/pigpio%d", h);
258 f = open(p, O_RDONLY);
261 CHECK(4, 1, e, 0, 0,
"notify open/begin");
268 CHECK(4, 2, e, 0, 0,
"notify pause");
271 CHECK(4, 3, e, 0, 0,
"notify close");
284 if (s != r.
seqno) seq_ok = 0;
286 if (n)
if (l != (r.
level&(1<<
GPIO))) toggle_ok = 0;
300 CHECK(4, 4, seq_ok, 1, 0,
"sequence numbers ok");
302 CHECK(4, 5, toggle_ok, 1, 0,
"gpio toggled ok");
304 CHECK(4, 6, n, 80, 10,
"number of notifications");
309 void t5cbf(
unsigned gpio,
unsigned level, uint32_t tick)
320 Now is the winter of our discontent\n\ 321 Made glorious summer by this sun of York;\n\ 322 And all the clouds that lour'd upon our house\n\ 323 In the deep bosom of the ocean buried.\n\ 324 Now are our brows bound with victorious wreaths;\n\ 325 Our bruised arms hung up for monuments;\n\ 326 Our stern alarums changed to merry meetings,\n\ 327 Our dreadful marches to delightful measures.\n\ 328 Grim-visaged war hath smooth'd his wrinkled front;\n\ 329 And now, instead of mounting barded steeds\n\ 330 To fright the souls of fearful adversaries,\n\ 331 He capers nimbly in a lady's chamber\n\ 332 To the lascivious pleasing of a lute.\n\ 340 {0, 1<<
GPIO, 100000},
347 printf(
"Waveforms & serial read/write tests.\n");
354 CHECK(5, 1, e, 0, 0,
"callback, set mode, wave clear");
357 CHECK(5, 2, e, 4, 0,
"pulse, wave add generic");
361 if (e < 14)
CHECK(5, 3, e, 9, 0,
"wave tx repeat");
362 else CHECK(5, 3, e, 19, 0,
"wave tx repeat");
367 CHECK(5, 4, c, 50, 2,
"callback");
370 CHECK(5, 5, e, 0, 0,
"wave tx stop");
373 CHECK(5, 6, e, 0, 0,
"serial read open");
377 CHECK(5, 7, e, 3405, 0,
"wave clear, wave add serial");
381 if (e < 6964)
CHECK(5, 8, e, 6811, 0,
"wave tx start");
382 else CHECK(5, 8, e, 7116, 0,
"wave tx start");
387 CHECK(5, 9, c, 0, 0,
"callback");
393 CHECK(5, 10, c, 1702, 0,
"wave tx busy, callback");
396 if (c > 0) text[
c] = 0;
397 CHECK(5, 11, strcmp(TEXT, text), 0, 0,
"wave tx busy, serial read");
400 CHECK(5, 12, e, 0, 0,
"serial read close");
403 CHECK(5, 13, c, 6158148, 0,
"wave get micros");
406 if (c > 6158148) c = 6158148;
407 CHECK(5, 14, c, 6158148, 0,
"wave get high micros");
410 CHECK(5, 15, c, 1800000000, 0,
"wave get max micros");
413 CHECK(5, 16, c, 3405, 0,
"wave get pulses");
416 CHECK(5, 17, c, 3405, 0,
"wave get high pulses");
419 CHECK(5, 18, c, 12000, 0,
"wave get max pulses");
422 if (c < 6963)
CHECK(5, 19, c, 6810, 0,
"wave get cbs");
423 else CHECK(5, 19, c, 7115, 0,
"wave get cbs");
426 if (c < 6963)
CHECK(5, 20, c, 6810, 0,
"wave get high cbs");
427 else CHECK(5, 20, c, 7115, 0,
"wave get high cbs");
430 CHECK(5, 21, c, 25016, 0,
"wave get max cbs");
437 void t6cbf(
unsigned gpio,
unsigned level, uint32_t tick)
454 printf(
"Trigger tests.\n");
476 CHECK(6, 2,
t6_on, tp, 25,
"gpio trigger pulse length");
481 void t7cbf(
unsigned gpio,
unsigned level, uint32_t tick)
490 printf(
"Watchdog tests.\n");
500 CHECK(7, 1, c, 39, 5,
"set watchdog on count");
507 CHECK(7, 2, c, 0, 1,
"set watchdog off count");
514 printf(
"Bank read/write tests.\n");
518 CHECK(8, 1, v, 0, 0,
"read bank 1");
522 CHECK(8, 2, v, (1<<
GPIO), 0,
"read bank 1");
526 CHECK(8, 3, v, 0, 0,
"clear bank 1");
530 CHECK(8, 4, v, 1, 0,
"set bank 1");
534 if (v) v = 0;
else v = 1;
536 CHECK(8, 5, v, 0, 0,
"read bank 2");
539 CHECK(8, 6, v, 0, 0,
"clear bank 2");
545 CHECK(8, 8, v, 0, 0,
"set bank 2");
553 void t9cbf(
unsigned gpio,
unsigned level, uint32_t tick)
563 printf(
"Script store/run/status/stop/delete tests.\n");
600 CHECK(9, 1, c, 100, 0,
"store/run script");
614 CHECK(9, 2, c, 201, 0,
"run script/script status");
629 CHECK(9, 3, c, 410, 10,
"run/stop script/script status");
632 CHECK(9, 4, e, 0, 0,
"delete script");
641 printf(
"Serial link tests.\n");
647 CHECK(10, 1, h, 0, 0,
"serial open");
654 CHECK(10, 2, b, 0, 0,
"serial data available");
657 To be, or not to be, that is the question-\ 658 Whether 'tis Nobler in the mind to suffer\ 659 The Slings and Arrows of outrageous Fortune,\ 660 Or to take Arms against a Sea of troubles,\ 663 CHECK(10, 3, e, 0, 0,
"serial write");
670 CHECK(10, 4, e, 0, 0,
"serial write byte");
675 CHECK(10, 5, b, strlen(TEXT)+4, 0,
"serial data available");
678 CHECK(10, 6, b, strlen(TEXT), 0,
"serial read");
679 if (b >= 0) text[b] = 0;
680 CHECK(10, 7, strcmp(TEXT, text), 0, 0,
"serial read");
683 CHECK(10, 8, b, 0xAA, 0,
"serial read byte");
686 CHECK(10, 9, b, 0x55, 0,
"serial read byte");
689 CHECK(10, 10, b, 0x00, 0,
"serial read byte");
692 CHECK(10, 11, b, 0xFF, 0,
"serial read byte");
695 CHECK(10, 12, b, 0, 0,
"serial data availabe");
698 CHECK(10, 13, e, 0, 0,
"serial close");
707 printf(
"SMBus / I2C tests.");
712 CHECK(11, 1, h, 0, 0,
"i2c open");
715 CHECK(11, 2, e, 0, 0,
"i2c write device");
718 CHECK(11, 3, b, 1, 0,
"i2c read device");
719 CHECK(11, 4, buf[0], 0xE5, 0,
"i2c read device");
722 CHECK(11, 5, b, 0xE5, 0,
"i2c read byte");
725 CHECK(11, 6, b, 0xE5, 0,
"i2c read byte data");
728 CHECK(11, 7, b, 2, 0,
"i2c read byte data");
730 exp =
"\x1D[aBcDeFgHjKM]";
734 CHECK(11, 8, e, 0, 0,
"i2c write device");
738 CHECK(11, 9, b, len-1, 0,
"i2c read device");
739 CHECK(11, 10, strncmp(buf, exp+1, len-1), 0, 0,
"i2c read device");
741 if (strncmp(buf, exp+1, len-1))
742 printf(
"got [%.*s] expected [%.*s]\n", len-1, buf, len-1, exp+1);
745 CHECK(11, 11, e, 0, 0,
"i2c write byte data");
748 CHECK(11, 12, b, 0xAA, 0,
"i2c read byte data");
751 CHECK(11, 13, e, 0, 0,
"i2c write byte data");
754 CHECK(11, 14, b, 0x55, 0,
"i2c read byte data");
756 exp =
"[1234567890#]";
760 CHECK(11, 15, e, 0, 0,
"i2c write block data");
764 CHECK(11, 16, b, len, 0,
"i2c read device");
765 CHECK(11, 17, strncmp(buf, exp, len), 0, 0,
"i2c read device");
767 if (strncmp(buf, exp, len))
768 printf(
"got [%.*s] expected [%.*s]\n", len, buf, len, exp);
771 CHECK(11, 18, b, len, 0,
"i2c read i2c block data");
772 CHECK(11, 19, strncmp(buf, exp, len), 0, 0,
"i2c read i2c block data");
774 if (strncmp(buf, exp, len))
775 printf(
"got [%.*s] expected [%.*s]\n", len, buf, len, exp);
777 exp =
"(-+=;:,<>!%)";
781 CHECK(11, 20, e, 0, 0,
"i2c write i2c block data");
784 CHECK(11, 21, b, len, 0,
"i2c read i2c block data");
785 CHECK(11, 22, strncmp(buf, exp, len), 0, 0,
"i2c read i2c block data");
787 if (strncmp(buf, exp, len))
788 printf(
"got [%.*s] expected [%.*s]\n", len, buf, len, exp);
791 CHECK(11, 23, e, 0, 0,
"i2c close");
799 printf(
"SPI tests.");
804 CHECK(12, 1, h, 0, 0,
"spi open");
809 sprintf(buf,
"\x01\x80");
811 CHECK(12, 2, b, 3, 0,
"spi xfer");
815 printf(
"%d ", ((buf[1]&0x0F)*256)|buf[2]);
820 CHECK(12, 99, e, 0, 0,
"spi close");
824 int main(
int argc,
char *argv[])
834 for (i=0; i<strlen(argv[1]); i++)
836 c = tolower(argv[1][i]);
838 if (!strchr(test, c))
845 else strcat(test,
"0123456789");
851 fprintf(stderr,
"pigpio initialisation failed.\n");
855 printf(
"Connected to pigpio daemon.\n");
857 if (strchr(test,
'0'))
t0();
858 if (strchr(test,
'1'))
t1();
859 if (strchr(test,
'2'))
t2();
860 if (strchr(test,
'3'))
t3();
861 if (strchr(test,
'4'))
t4();
862 if (strchr(test,
'5'))
t5();
863 if (strchr(test,
'6'))
t6();
864 if (strchr(test,
'7'))
t7();
865 if (strchr(test,
'8'))
t8();
866 if (strchr(test,
'9'))
t9();
867 if (strchr(test,
'a'))
ta();
868 if (strchr(test,
'b'))
tb();
869 if (strchr(test,
'c'))
tc();
int i2c_write_byte_data(unsigned handle, unsigned reg, uint32_t val)
int serial_write(unsigned handle, char *buf, unsigned count)
void t9cbf(unsigned gpio, unsigned level, uint32_t tick)
int bb_serial_read(unsigned user_gpio, void *buf, size_t bufSize)
int set_bank_1(uint32_t levels)
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)
void t5cbf(unsigned gpio, unsigned level, uint32_t tick)
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)
void t6cbf(unsigned gpio, unsigned level, uint32_t tick)
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)
void t3cbf(unsigned gpio, unsigned level, uint32_t tick)
#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)
int wave_get_max_pulses(void)
int get_PWM_range(unsigned user_gpio)
#define PI_SCRIPT_INITING
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 CHECK(int t, int st, int got, int expect, int pc, char *desc)
int gpio_write(unsigned gpio, unsigned level)
int gpio_trigger(unsigned user_gpio, unsigned pulseLen, uint32_t level)
uint32_t get_hardware_revision(void)
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)
void t2cb(unsigned gpio, unsigned level, uint32_t tick)
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)
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)
void t7cbf(unsigned gpio, unsigned level, uint32_t tick)
int serial_write_byte(unsigned handle, unsigned val)
int set_pull_up_down(unsigned gpio, unsigned pud)
int clear_bank_2(uint32_t levels)
int wave_get_high_micros(void)
int main(int argc, char *argv[])
int notify_begin(unsigned handle, uint32_t bits)
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)