19 #include <sys/types.h> 28 #define USERDATA 18249013 32 void CHECK(
int t,
int st,
int got,
int expect,
int pc,
char *desc)
34 if ((got >= (((1E2-pc)*expect)/1E2)) && (got <= (((1E2+pc)*expect)/1E2)))
36 printf(
"TEST %2d.%-2d PASS (%s: %d)\n", t, st, desc, expect);
41 "TEST %2d.%-2d FAILED got %d (%s: %d)\n",
42 t, st, got, desc, expect);
48 printf(
"\nTesting pigpio C I/F\n");
59 printf(
"Mode/PUD/read/write tests.\n");
63 CHECK(1, 1, v, 0, 0,
"set mode, get mode");
67 CHECK(1, 2, v, 1, 0,
"set pull up down, read");
71 CHECK(1, 3, v, 0, 0,
"set pull up down, read");
75 CHECK(1, 4, v, 1, 0,
"write, get mode");
78 CHECK(1, 5, v, 0, 0,
"read");
83 CHECK(1, 6, v, 1, 0,
"write, read");
88 void t2cb(
int gpio,
int level, uint32_t tick)
97 printf(
"PWM dutycycle/range/frequency tests.\n");
102 CHECK(2, 1, f, 10, 0,
"set PWM range, set/get PWM frequency");
110 CHECK(2, 2, dc, 0, 0,
"get PWM dutycycle");
116 CHECK(2, 3, f, 0, 0,
"set PWM dutycycle, callback");
120 CHECK(2, 4, dc, 128, 0,
"get PWM dutycycle");
125 CHECK(2, 5, f, 40, 5,
"set PWM dutycycle, callback");
129 CHECK(2, 6, f, 100, 0,
"set/get PWM frequency");
134 CHECK(2, 7, f, 400, 1,
"callback");
138 CHECK(2, 8, f, 1000, 0,
"set/get PWM frequency");
143 CHECK(2, 9, f, 4000, 1,
"callback");
146 CHECK(2, 10, r, 255, 0,
"get PWM range");
149 CHECK(2, 11, rr, 200, 0,
"get PWM real range");
153 CHECK(2, 12, r, 2000, 0,
"set/get PWM range");
156 CHECK(2, 13, rr, 200, 0,
"get PWM real range");
168 void t3cbf(
int gpio,
int level, uint32_t tick,
void *userdata)
170 static int unreported = 1;
184 "unexpected userdata %d (expected %d)\n",
218 int pw[3]={500, 1500, 2500};
219 int dc[4]={20, 40, 60, 80};
221 printf(
"PWM/Servo pulse accuracy tests.\n");
236 CHECK(3, t+t+1, v, pw[t], 0,
"get servo pulsewidth");
243 CHECK(3, t+t+2, (1E3*(on+off))/on, 2E7/pw[t], 1,
244 "set servo pulsewidth");
250 CHECK(3, 7, f, 1000, 0,
"set/get PWM frequency");
253 CHECK(3, 8, rr, 200, 0,
"set PWM range");
259 CHECK(3, t+t+9, v, dc[t], 0,
"get PWM dutycycle");
266 CHECK(3, t+t+10, (1E3*on)/(on+off), 1E1*dc[t], 1,
267 "set PWM dutycycle");
275 int h, e,
f, n,
s, b, l, seq_ok, toggle_ok;
279 printf(
"Pipe notification tests.\n");
287 sprintf(p,
"/dev/pigpio%d", h);
288 f = open(p, O_RDONLY);
291 CHECK(4, 1, e, 0, 0,
"notify open/begin");
298 CHECK(4, 2, e, 0, 0,
"notify pause");
301 CHECK(4, 3, e, 0, 0,
"notify close");
314 if (s != r.
seqno) seq_ok = 0;
316 if (n)
if (l != (r.
level&(1<<
GPIO))) toggle_ok = 0;
331 CHECK(4, 4, seq_ok, 1, 0,
"sequence numbers ok");
333 CHECK(4, 5, toggle_ok, 1, 0,
"gpio toggled ok");
335 CHECK(4, 6, n, 80, 10,
"number of notifications");
340 void t5cbf(
int gpio,
int level, uint32_t tick)
351 Now is the winter of our discontent\n\ 352 Made glorious summer by this sun of York;\n\ 353 And all the clouds that lour'd upon our house\n\ 354 In the deep bosom of the ocean buried.\n\ 355 Now are our brows bound with victorious wreaths;\n\ 356 Our bruised arms hung up for monuments;\n\ 357 Our stern alarums changed to merry meetings,\n\ 358 Our dreadful marches to delightful measures.\n\ 359 Grim-visaged war hath smooth'd his wrinkled front;\n\ 360 And now, instead of mounting barded steeds\n\ 361 To fright the souls of fearful adversaries,\n\ 362 He capers nimbly in a lady's chamber\n\ 363 To the lascivious pleasing of a lute.\n\ 371 {0, 1<<
GPIO, 100000},
378 printf(
"Waveforms & serial read/write tests.\n");
387 CHECK(5, 1, e, 0, 0,
"callback, set mode, wave clear");
390 CHECK(5, 2, e, 4, 0,
"pulse, wave add generic");
394 if (e < 14)
CHECK(5, 3, e, 9, 0,
"wave tx repeat");
395 else CHECK(5, 3, e, 19, 0,
"wave tx repeat");
400 CHECK(5, 4, c, 50, 1,
"callback");
403 CHECK(5, 5, e, 0, 0,
"wave tx stop");
408 CHECK(5, 6, e, 0, 0,
"serial read open");
412 CHECK(5, 7, e, 3405, 0,
"wave clear, wave add serial");
416 if (e < 6964)
CHECK(5, 8, e, 6811, 0,
"wave tx start");
417 else CHECK(5, 8, e, 7116, 0,
"wave tx start");
419 CHECK(5, 9, 0, 0, 0,
"NOT APPLICABLE");
421 CHECK(5, 10, 0, 0, 0,
"NOT APPLICABLE");
426 if (c > 0) text[
c] = 0;
427 CHECK(5, 11, strcmp(TEXT, text), 0, 0,
"wave tx busy, serial read");
430 CHECK(5, 12, e, 0, 0,
"serial read close");
433 CHECK(5, 13, c, 6158148, 0,
"wave get micros");
436 CHECK(5, 14, c, 6158148, 0,
"wave get high micros");
439 CHECK(5, 15, c, 1800000000, 0,
"wave get max micros");
442 CHECK(5, 16, c, 3405, 0,
"wave get pulses");
445 CHECK(5, 17, c, 3405, 0,
"wave get high pulses");
448 CHECK(5, 18, c, 12000, 0,
"wave get max pulses");
451 if (e < 6963)
CHECK(5, 19, c, 6810, 0,
"wave get cbs");
452 else CHECK(5, 19, c, 7115, 0,
"wave get cbs");
455 if (e < 6963)
CHECK(5, 20, c, 6810, 0,
"wave get high cbs");
456 else CHECK(5, 20, c, 7115, 0,
"wave get high cbs");
459 CHECK(5, 21, c, 25016, 0,
"wave get max cbs");
466 void t6cbf(
int gpio,
int level, uint32_t tick)
483 printf(
"Trigger tests\n");
507 CHECK(6, 2,
t6_on, tp, 25,
"gpio trigger pulse length");
512 void t7cbf(
int gpio,
int level, uint32_t tick)
521 printf(
"Watchdog tests.\n");
533 CHECK(7, 1, c, 39, 5,
"set watchdog on count");
540 CHECK(7, 2, c, 0, 1,
"set watchdog off count");
547 printf(
"Bank read/write tests.\n");
551 CHECK(8, 1, v, 0, 0,
"read bank 1");
555 CHECK(8, 2, v, (1<<
GPIO), 0,
"read bank 1");
559 CHECK(8, 3, v, 0, 0,
"clear bank 1");
563 CHECK(8, 4, v, 1, 0,
"set bank 1");
567 if (v) v = 0;
else v = 1;
569 CHECK(8, 5, v, 0, 0,
"read bank 2");
572 CHECK(8, 6, v, 0, 0,
"clear bank 2");
574 CHECK(8, 7, 0, 0, 0,
"NOT APPLICABLE");
577 CHECK(8, 8, v, 0, 0,
"set bank 2");
579 CHECK(8, 9, 0, 0, 0,
"NOT APPLICABLE");
584 void t9cbf(
int gpio,
int level, uint32_t tick)
609 printf(
"Script store/run/status/stop/delete tests.\n");
633 CHECK(9, 1, c, 100, 0,
"store/run script");
648 CHECK(9, 2, c, 201, 0,
"run script/script status");
664 CHECK(9, 3, c, 110, 10,
"run/stop script/script status");
667 CHECK(9, 4, e, 0, 0,
"delete script");
676 printf(
"Serial link tests.\n");
680 h =
serOpen(
"/dev/ttyAMA0", 57600, 0);
682 CHECK(10, 1, h, 0, 0,
"serial open");
684 b =
serRead(h, text,
sizeof(text));
687 CHECK(10, 2, b, 0, 0,
"serial data available");
690 To be, or not to be, that is the question-\ 691 Whether 'tis Nobler in the mind to suffer\ 692 The Slings and Arrows of outrageous Fortune,\ 693 Or to take Arms against a Sea of troubles,\ 695 e =
serWrite(h, TEXT, strlen(TEXT));
696 CHECK(10, 3, e, 0, 0,
"serial write");
703 CHECK(10, 4, e, 0, 0,
"serial write byte");
708 CHECK(10, 5, b, strlen(TEXT)+4, 0,
"serial data available");
710 b =
serRead(h, text, strlen(TEXT));
711 CHECK(10, 6, b, strlen(TEXT), 0,
"serial read");
712 if (b >= 0) text[b] = 0;
713 CHECK(10, 7, strcmp(TEXT, text), 0, 0,
"serial read");
716 CHECK(10, 8, b, 0xAA, 0,
"serial read byte");
719 CHECK(10, 9, b, 0x55, 0,
"serial read byte");
722 CHECK(10, 10, b, 0x00, 0,
"serial read byte");
725 CHECK(10, 11, b, 0xFF, 0,
"serial read byte");
728 CHECK(10, 12, b, 0, 0,
"serial data availabe");
731 CHECK(10, 13, e, 0, 0,
"serial close");
740 printf(
"SMBus / I2C tests.");
745 CHECK(11, 1, h, 0, 0,
"i2cOpen");
748 CHECK(11, 2, e, 0, 0,
"i2cWriteDevice");
751 CHECK(11, 3, b, 1, 0,
"i2cReadDevice");
752 CHECK(11, 4, buf[0], 0xE5, 0,
"i2cReadDevice");
755 CHECK(11, 5, b, 0xE5, 0,
"i2cReadByte");
758 CHECK(11, 6, b, 0xE5, 0,
"i2cReadByteData");
761 CHECK(11, 7, b, 2, 0,
"i2cReadByteData");
763 exp =
"\x1D[aBcDeFgHjKM]";
767 CHECK(11, 8, e, 0, 0,
"i2cWriteDevice");
771 CHECK(11, 9, b, len-1, 0,
"i2cReadDevice");
772 CHECK(11, 10, strncmp(buf, exp+1, len-1), 0, 0,
"i2cReadDevice");
774 if (strncmp(buf, exp+1, len-1))
775 printf(
"got [%.*s] expected [%.*s]\n", len-1, buf, len-1, exp+1);
778 CHECK(11, 11, e, 0, 0,
"i2cWriteByteData");
781 CHECK(11, 12, b, 0xAA, 0,
"i2cReadByteData");
784 CHECK(11, 13, e, 0, 0,
"i2cWriteByteData");
787 CHECK(11, 14, b, 0x55, 0,
"i2cReadByteData");
789 exp =
"[1234567890#]";
793 CHECK(11, 15, e, 0, 0,
"i2c writeBlockData");
797 CHECK(11, 16, b, len, 0,
"i2cReadDevice");
798 CHECK(11, 17, strncmp(buf, exp, len), 0, 0,
"i2cReadDevice");
800 if (strncmp(buf, exp, len))
801 printf(
"got [%.*s] expected [%.*s]\n", len, buf, len, exp);
804 CHECK(11, 18, b, len, 0,
"i2cReadI2CBlockData");
805 CHECK(11, 19, strncmp(buf, exp, len), 0, 0,
"i2cReadI2CBlockData");
807 if (strncmp(buf, exp, len))
808 printf(
"got [%.*s] expected [%.*s]\n", len, buf, len, exp);
810 exp =
"(-+=;:,<>!%)";
814 CHECK(11, 20, e, 0, 0,
"i2cWriteI2CBlockData");
817 CHECK(11, 21, b, len, 0,
"i2cReadI2CBlockData");
818 CHECK(11, 22, strncmp(buf, exp, len), 0, 0,
"i2cReadI2CBlockData");
820 if (strncmp(buf, exp, len))
821 printf(
"got [%.*s] expected [%.*s]\n", len, buf, len, exp);
824 CHECK(11, 23, e, 0, 0,
"i2cClose");
830 char txBuf[8], rxBuf[8];
832 printf(
"SPI tests.");
837 CHECK(12, 1, h, 0, 0,
"spiOpen");
839 sprintf(txBuf,
"\x01\x80");
843 b =
spiXfer(h, txBuf, rxBuf, 3);
844 CHECK(12, 2, b, 3, 0,
"spiXfer");
848 printf(
"%d ", ((rxBuf[1]&0x0F)*256)|rxBuf[2]);
853 CHECK(12, 99, e, 0, 0,
"spiClose");
856 int main(
int argc,
char *argv[])
866 for (i=0; i<strlen(argv[1]); i++)
868 c = tolower(argv[1][i]);
870 if (!strchr(test, c))
877 else strcat(test,
"0123456789");
883 fprintf(stderr,
"pigpio initialisation failed.\n");
887 if (strchr(test,
'0'))
t0();
888 if (strchr(test,
'1'))
t1();
889 if (strchr(test,
'2'))
t2();
890 if (strchr(test,
'3'))
t3();
891 if (strchr(test,
'4'))
t4();
892 if (strchr(test,
'5'))
t5();
893 if (strchr(test,
'6'))
t6();
894 if (strchr(test,
'7'))
t7();
895 if (strchr(test,
'8'))
t8();
896 if (strchr(test,
'9'))
t9();
897 if (strchr(test,
'a'))
ta();
898 if (strchr(test,
'b'))
tb();
899 if (strchr(test,
'c'))
tc();
void t3cbf(int gpio, int level, uint32_t tick, void *userdata)
int gpioServo(unsigned gpio, unsigned val)
int i2cReadI2CBlockData(unsigned handle, unsigned reg, char *buf, unsigned count)
int i2cWriteI2CBlockData(unsigned handle, unsigned reg, char *buf, unsigned count)
int serWriteByte(unsigned handle, unsigned bVal)
int gpioWaveGetHighCbs(void)
int serRead(unsigned handle, char *buf, unsigned count)
int gpioSetAlertFunc(unsigned gpio, gpioAlertFunc_t f)
void t2cb(int gpio, int level, uint32_t tick)
int gpioNotifyBegin(unsigned handle, uint32_t bits)
int gpioSetWatchdog(unsigned gpio, unsigned timeout)
void t7cbf(int gpio, int level, uint32_t tick)
int gpioSetPWMrange(unsigned gpio, unsigned range)
int gpioSerialRead(unsigned gpio, void *buf, size_t bufSize)
int i2cReadByteData(unsigned handle, unsigned reg)
int gpioWaveGetHighMicros(void)
#define PI_WAVE_MODE_REPEAT
#define PI_WAVE_MODE_ONE_SHOT
void t9cbf(int gpio, int level, uint32_t tick)
int gpioGetServoPulsewidth(unsigned gpio)
int main(int argc, char *argv[])
int gpioGetMode(unsigned gpio)
int gpioWaveAddSerial(unsigned gpio, unsigned baud, unsigned data_bits, unsigned stop_bits, unsigned offset, unsigned numBytes, char *bstr)
int gpioSetPWMfrequency(unsigned gpio, unsigned frequency)
int gpioWaveGetMaxCbs(void)
int serDataAvailable(unsigned handle)
int gpioSetMode(unsigned gpio, unsigned mode)
uint32_t gpioRead_Bits_32_53(void)
int i2cWriteByteData(unsigned handle, unsigned reg, unsigned bVal)
#define PI_SCRIPT_RUNNING
int gpioWaveGetHighPulses(void)
int serClose(unsigned handle)
int gpioSerialReadClose(unsigned gpio)
int gpioStoreScript(char *script)
int gpioGetPWMrange(unsigned gpio)
int gpioGetPWMdutycycle(unsigned gpio)
int i2cWriteDevice(unsigned handle, char *buf, unsigned count)
#define PI_SCRIPT_INITING
unsigned gpioVersion(void)
int gpioWaveGetMaxMicros(void)
uint32_t gpioDelay(uint32_t micros)
int gpioPWM(unsigned gpio, unsigned val)
void t5cbf(int gpio, int level, uint32_t tick)
int gpioWaveGetPulses(void)
int gpioScriptStatus(unsigned script_id, uint32_t *param)
int gpioRead(unsigned gpio)
int i2cClose(unsigned handle)
int gpioSerialReadOpen(unsigned gpio, unsigned baud, unsigned data_bits)
int gpioSetAlertFuncEx(unsigned gpio, gpioAlertFuncEx_t f, void *userdata)
int gpioDeleteScript(unsigned script_id)
int gpioWaveGetMaxPulses(void)
static rawWave_t wf[3][PI_WAVE_MAX_PULSES]
int gpioWrite_Bits_0_31_Clear(uint32_t bits)
int gpioGetPWMfrequency(unsigned gpio)
int serWrite(unsigned handle, char *buf, unsigned count)
int gpioWrite(unsigned gpio, unsigned level)
unsigned gpioHardwareRevision(void)
int gpioTrigger(unsigned gpio, unsigned pulseLen, unsigned level)
int spiOpen(unsigned spiChan, unsigned baud, unsigned spiFlags)
int gpioSetPullUpDown(unsigned gpio, unsigned pud)
int gpioWrite_Bits_32_53_Clear(uint32_t bits)
int gpioWaveTxSend(unsigned wave_id, unsigned wave_mode)
int i2cReadDevice(unsigned handle, char *buf, unsigned count)
int spiClose(unsigned handle)
int i2cOpen(unsigned i2cBus, unsigned i2cAddr, unsigned i2cFlags)
int gpioWrite_Bits_0_31_Set(uint32_t bits)
int spiXfer(unsigned handle, char *txBuf, char *rxBuf, unsigned count)
int i2cWriteBlockData(unsigned handle, unsigned reg, char *buf, unsigned count)
void time_sleep(double seconds)
int gpioNotifyPause(unsigned handle)
int gpioNotifyClose(unsigned handle)
uint32_t gpioRead_Bits_0_31(void)
int gpioWrite_Bits_32_53_Set(uint32_t bits)
int gpioGetPWMrealRange(unsigned gpio)
int gpioRunScript(unsigned script_id, unsigned numParam, uint32_t *param)
int serReadByte(unsigned handle)
void CHECK(int t, int st, int got, int expect, int pc, char *desc)
int gpioWaveGetMicros(void)
void t6cbf(int gpio, int level, uint32_t tick)
int gpioStopScript(unsigned script_id)
int gpioWaveAddGeneric(unsigned numPulses, gpioPulse_t *pulses)
int serOpen(char *tty, unsigned serBaud, unsigned serFlags)
int i2cReadByte(unsigned handle)