/* This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means. In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. For more information, please refer to */ /* This version is for pigpio version 3+ */ #include #include #include #include #include #include "pigpio.h" #define GREENLED 16 #define SDDET 47 #define SDCLK 48 int test =1; int passes=0; int expect=0; struct timeval libInitTime; int GPIO=4; unsigned inited, count, onMicros, offMicros; void tick(void) { /* count ticks */ static struct timeval lastTime; struct tm tmp; struct timeval nowTime; char buf[32]; gettimeofday(&nowTime, NULL); localtime_r(&nowTime.tv_sec, &tmp); strftime(buf, sizeof(buf), "%F@%T", &tmp); printf("%s.%03d\n", buf, (int)nowTime.tv_usec/1000); /*timersub(&nowTime, &lastTime, &diffTime);*/ lastTime = nowTime; if (inited) { count++; } else { count = 1; gettimeofday(&lastTime, NULL); inited = 1; } } void tickEx(void * userdata) { } void alert(int gpio, int level, uint32_t tick) { /* accumulate number of level changes and average time gpio was on and off. Hopefully the ratio should reflect the selected pulsewidth. */ static uint32_t lastTick; uint32_t diffTick; if (inited) { count++; diffTick = tick - lastTick; if (level == 0) { /* elapsed time was on */ onMicros = onMicros + diffTick; } else { /* elapsed time was off */ offMicros = offMicros + diffTick; } lastTick = tick; } else { count = 1; lastTick = tick; onMicros = 0; offMicros = 0; inited = 1; } } void alertEx(int gpio, int level, uint32_t tick, void * userdata) { } static void timerTest(unsigned waitfor, unsigned ms) { unsigned ep, ep1, ep2; ep= (waitfor*1000)/ms; ep1=ep-1; ep2=ep+1; printf("Timer ticktest (%d ms), wait %d seconds\n", ms, waitfor); printf("Expect %d to %d ticks\n", ep1, ep2); inited = 0; gpioSetTimerFunc(0, ms, tick); sleep(waitfor); gpioSetTimerFunc(0, ms, NULL); /* and the stats were? */ printf("ticks=%d\n", count); if ((count>=ep1) && (count<=ep2)) { printf("TEST %d: PASS\n\n", test); ++passes; } else { printf("TEST %d: FAILED\n\n", test); } ++test; } static void servoTest(unsigned waitfor, unsigned pulsewidth) { int ticks, on, off; unsigned expectedPulses, ep1, ep2; float expectedRatio, er1, er2; float ratio; expectedPulses=(500*waitfor)/10; ep1=(490*waitfor)/10; ep2=(510*waitfor)/10; expectedRatio = (float)(20000-pulsewidth)/(float)pulsewidth; er1=expectedRatio*0.9; er2=expectedRatio*1.1; printf("Servo pulse test (%d micros), wait %d seconds\n", pulsewidth, waitfor); printf("Expect %d pulses and an off/on ratio of %.1f\n", expectedPulses, expectedRatio); gpioServo(GPIO, pulsewidth); inited = 0; gpioSetAlertFunc(GPIO, alert); sleep(waitfor); gpioSetAlertFunc(GPIO, NULL); gpioServo(GPIO, 0); /* and the stats were? */ ticks = count/2; on = onMicros/1000; off = offMicros/1000; ratio = (float)off/(float)on; printf("servo pulses=%d on ms=%d off ms=%d ratio=%.1f\n", ticks, on, off, ratio); if ( ((ticks>ep1) && (tickser1) && (ratioer1) && (ratio0; i--) { expMicros = i * 100000; if (timetype == PI_TIME_ABSOLUTE) { gpioTime(PI_TIME_ABSOLUTE, &secs, µs); secs += (i / 10); micros += (i % 10) * 100000; if (micros > 999999) { secs++; micros -= 1000000; } } else { secs = (i / 10); micros = (i % 10) * 100000; } gettimeofday(&t1, NULL); gpioSleep(timetype, secs, micros); gettimeofday(&t2, NULL); timersub(&t2, &t1, &tD); diffMicros = (tD.tv_sec*1000000)+tD.tv_usec; errMicros = diffMicros - expMicros; if (errMicros < 500) ok++; printf("secs=%d micros=%d err=%d\n", secs, micros, errMicros); } if (ok == 15) { printf("TEST %d: PASS\n\n", test); ++passes; } else printf("TEST %d: FAILED\n\n", test); ++test; } int countBank2PinChanges(int pin, int loops) { static uint32_t old=0; uint32_t new, changes; int i, count; count = 0; for (i=0;i200000 for pin 48.\n"); printf("Expect the green LED to flash.\n\n"); ok = 0; for (i=0; i<20; i++) { gpioWrite_Bits_0_31_Set(1< 200000)) ok++; } if (ok == 20) { printf("TEST %d: PASS\n\n", test); ++passes; } else printf("TEST %d: FAILED\n\n", test); ++test; } void checkGpioTick(void) { uint32_t startTick, endTick; int diffTick; printf("Library gpioTick Test\n"); printf("Expect approximately 2 million ticks to have elapsed.\n\n"); startTick = gpioTick(); sleep(2); /* 2 seconds being 2 million ticks */ endTick = gpioTick(); diffTick = endTick - startTick; printf("%d ticks have elapsed\n", diffTick); if ((diffTick >= 1990000) && (diffTick <= 2010000)) { printf("TEST %d: PASS\n\n", test); ++passes; } else printf("TEST %d: FAILED\n\n", test); ++test; } int main(int argc, char *argv[]) { int waitfor; int version, micros=5, millis=100; if (argc > 1) GPIO = atoi(argv[1]); fprintf(stderr, "*****************************************************************\n"\ "* WARNING: This program sends pulses to gpio #%02d *\n"\ "* Make sure that nothing which could be damaged is *\n"\ "* connected to this gpio. A LED or similar should be OK *\n"\ "* although nothing needs to be connected. *\n"\ "* *\n"\ "* NOTE: many of the tests are statistical in nature, assuming *\n"\ "* that events of a short nature will on average be detected *\n"\ "* by sampling. Don't fret if a particular test fails, try *\n"\ "* running the tests again. *\n"\ "* *\n"\ "* You may choose another gpio by specifying its number on *\n"\ "* the command line, e.g. sudo ./checklib 17 will use gpio 17. *\n"\ "* *\n"\ "* Press y (RETURN) to continue, any other character to cancel. *\n"\ "*****************************************************************\n", GPIO); if (getchar() != 'y') return 0; printf("Initialisation test\n"); if (argc > 2) micros = atoi(argv[2]); if (argc > 3) millis = atoi(argv[3]); gpioCfgBufferSize(millis); gpioCfgClock(micros, PI_CLOCK_PCM, PI_CLOCK_PLLD); gettimeofday(&libInitTime, NULL); version = gpioInitialise(); if (version<0) { printf("TEST %d: FAILED\nFATAL ERROR\n", test); return 1; } else { printf("TEST %d: PASS, pigpio version is %d\n\n", test, version); ++passes; } ++test; waitfor = 2; printf("Alert function test, wait %d seconds\n", waitfor); printf("No detected events on gpio 4 expected\n"); inited = 0; gpioSetAlertFunc(GPIO, alert); sleep(waitfor); gpioSetAlertFunc(GPIO, NULL); printf("Events=%d\n", count); if (count) printf("TEST %d: FAILED\n\n", test); else { printf("TEST %d: PASS\n\n", test); ++passes; } ++test; servoTest(10, 500); servoTest(10, 1500); servoTest(10, 2500); pwmTest(5, 50); pwmTest(5, 100); pwmTest(5, 150); pwmTest(5, 200); timerTest(5, 100); timerTest(5, 250); timerTest(5, 333); timerTest(5, 1000); checkValidation(); checkGpioTime(); checkGpioSleep(PI_TIME_RELATIVE); checkGpioSleep(PI_TIME_ABSOLUTE); checkReadWriteBits(); checkGpioTick(); printf("Hardware revision is %d\n\n", gpioHardwareRevision()); printf("Summary: %d tests, %d passes\n", test-1, passes); gpioTerminate(); /* stop DMA and free memory */ return (passes - (test-1)); }