x_pigpio.c
Go to the documentation of this file.
00001 /*
00002 gcc -Wall -pthread -o x_pigpio x_pigpio.c -lpigpio
00003 sudo ./x_pigpio
00004 
00005 *** WARNING ************************************************
00006 *                                                          *
00007 * All the tests make extensive use of gpio 25 (pin 22).    *
00008 * Ensure that either nothing or just a LED is connected to *
00009 * gpio 25 before running any of the tests.                 *
00010 *                                                          *
00011 * Some tests are statistical in nature and so may on       *
00012 * occasion fail.  Repeated failures on the same test or    *
00013 * many failures in a group of tests indicate a problem.    *
00014 ************************************************************
00015 */
00016 
00017 #include <stdio.h>
00018 #include <stdlib.h>
00019 #include <sys/types.h>
00020 #include <sys/stat.h>
00021 #include <fcntl.h>
00022 #include <unistd.h>
00023 #include <string.h>
00024 #include <ctype.h>
00025 
00026 #include "pigpio.h"
00027 
00028 #define USERDATA 18249013
00029 
00030 #define GPIO 25
00031 
00032 void CHECK(int t, int st, int got, int expect, int pc, char *desc)
00033 {
00034    if ((got >= (((1E2-pc)*expect)/1E2)) && (got <= (((1E2+pc)*expect)/1E2)))
00035    {
00036       printf("TEST %2d.%-2d PASS (%s: %d)\n", t, st, desc, expect);
00037    }
00038    else
00039    {
00040       fprintf(stderr,
00041               "TEST %2d.%-2d FAILED got %d (%s: %d)\n",
00042               t, st, got, desc, expect);
00043    }
00044 }
00045 
00046 void t0()
00047 {
00048    printf("\nTesting pigpio C I/F\n");
00049 
00050    printf("pigpio version %d.\n", gpioVersion());
00051 
00052    printf("Hardware revision %d.\n", gpioHardwareRevision());
00053 }
00054 
00055 void t1()
00056 {
00057    int v;
00058 
00059    printf("Mode/PUD/read/write tests.\n");
00060 
00061    gpioSetMode(GPIO, PI_INPUT);
00062    v = gpioGetMode(GPIO);
00063    CHECK(1, 1, v, 0, 0, "set mode, get mode");
00064 
00065    gpioSetPullUpDown(GPIO, PI_PUD_UP);
00066    v = gpioRead(GPIO);
00067    CHECK(1, 2, v, 1, 0, "set pull up down, read");
00068 
00069    gpioSetPullUpDown(GPIO, PI_PUD_DOWN);
00070    v = gpioRead(GPIO);
00071    CHECK(1, 3, v, 0, 0, "set pull up down, read");
00072 
00073    gpioWrite(GPIO, PI_LOW);
00074    v = gpioGetMode(GPIO);
00075    CHECK(1, 4, v, 1, 0, "write, get mode");
00076 
00077    v = gpioRead(GPIO);
00078    CHECK(1, 5, v, 0, 0, "read");
00079 
00080    gpioWrite(GPIO, PI_HIGH);
00081    gpioDelay(1); /* 1 micro delay to let GPIO reach level reliably */
00082    v = gpioRead(GPIO);
00083    CHECK(1, 6, v, 1, 0, "write, read");
00084 }
00085 
00086 int t2_count;
00087 
00088 void t2cb(int gpio, int level, uint32_t tick)
00089 {
00090    t2_count++;
00091 }
00092 
00093 void t2()
00094 {
00095    int dc, f, r, rr, oc;
00096 
00097    printf("PWM dutycycle/range/frequency tests.\n");
00098 
00099    gpioSetPWMrange(GPIO, 255);
00100    gpioSetPWMfrequency(GPIO, 0);
00101    f = gpioGetPWMfrequency(GPIO);
00102    CHECK(2, 1, f, 10, 0, "set PWM range, set/get PWM frequency");
00103 
00104    t2_count=0;
00105 
00106    gpioSetAlertFunc(GPIO, t2cb);
00107 
00108    gpioPWM(GPIO, 0);
00109    dc = gpioGetPWMdutycycle(GPIO);
00110    CHECK(2, 2, dc, 0, 0, "get PWM dutycycle");
00111 
00112    time_sleep(0.5); /* allow old notifications to flush */
00113    oc = t2_count;
00114    time_sleep(2);
00115    f = t2_count - oc;
00116    CHECK(2, 3, f, 0, 0, "set PWM dutycycle, callback");
00117 
00118    gpioPWM(GPIO, 128);
00119    dc = gpioGetPWMdutycycle(GPIO);
00120    CHECK(2, 4, dc, 128, 0, "get PWM dutycycle");
00121 
00122    oc = t2_count;
00123    time_sleep(2);
00124    f = t2_count - oc;
00125    CHECK(2, 5, f, 40, 5, "set PWM dutycycle, callback");
00126 
00127    gpioSetPWMfrequency(GPIO, 100);
00128    f = gpioGetPWMfrequency(GPIO);
00129    CHECK(2, 6, f, 100, 0, "set/get PWM frequency");
00130 
00131    oc = t2_count;
00132    time_sleep(2);
00133    f = t2_count - oc;
00134    CHECK(2, 7, f, 400, 1, "callback");
00135 
00136    gpioSetPWMfrequency(GPIO, 1000);
00137    f = gpioGetPWMfrequency(GPIO);
00138    CHECK(2, 8, f, 1000, 0, "set/get PWM frequency");
00139 
00140    oc = t2_count;
00141    time_sleep(2);
00142    f = t2_count - oc;
00143    CHECK(2, 9, f, 4000, 1, "callback");
00144 
00145    r = gpioGetPWMrange(GPIO);
00146    CHECK(2, 10, r, 255, 0, "get PWM range");
00147 
00148    rr = gpioGetPWMrealRange(GPIO);
00149    CHECK(2, 11, rr, 200, 0, "get PWM real range");
00150 
00151    gpioSetPWMrange(GPIO, 2000);
00152    r = gpioGetPWMrange(GPIO);
00153    CHECK(2, 12, r, 2000, 0, "set/get PWM range");
00154 
00155    rr = gpioGetPWMrealRange(GPIO);
00156    CHECK(2, 13, rr, 200, 0, "get PWM real range");
00157 
00158    gpioPWM(GPIO, 0);
00159 }
00160 
00161 int t3_val;
00162 int t3_reset;
00163 int t3_count;
00164 uint32_t t3_tick;
00165 float t3_on;
00166 float t3_off;
00167 
00168 void t3cbf(int gpio, int level, uint32_t tick, void *userdata)
00169 {
00170    static int unreported = 1;
00171 
00172    uint32_t td;
00173    int *val;
00174 
00175    val = userdata;
00176 
00177    if (*val != USERDATA)
00178    {
00179       if (unreported)
00180       {
00181          fprintf
00182          (
00183             stderr,
00184             "unexpected userdata %d (expected %d)\n",
00185             *val, USERDATA
00186          );
00187       }
00188       unreported = 0;
00189    }
00190 
00191    if (t3_reset)
00192    {
00193       t3_count = 0;
00194       t3_on = 0.0;
00195       t3_off = 0.0;
00196       t3_reset = 0;
00197    }
00198    else
00199    {
00200       td = tick - t3_tick;
00201 
00202       if (level == 0) t3_on += td;
00203       else            t3_off += td;
00204    }
00205 
00206    t3_count ++;
00207    t3_tick = tick;
00208 }
00209 
00210 void t3()
00211 {
00212    int f, rr;
00213 
00214    float on, off;
00215 
00216    int t, v;
00217 
00218    int pw[3]={500, 1500, 2500};
00219    int dc[4]={20, 40, 60, 80};
00220 
00221    printf("PWM/Servo pulse accuracy tests.\n");
00222 
00223    t3_val = USERDATA;
00224    t3_reset=1;
00225    t3_count=0;
00226    t3_tick=0;
00227    t3_on=0.0;
00228    t3_off=0.0;
00229 
00230    gpioSetAlertFuncEx(GPIO, t3cbf, &t3_val); /* test extended alert */
00231 
00232    for (t=0; t<3; t++)
00233    {
00234       gpioServo(GPIO, pw[t]);
00235       v = gpioGetServoPulsewidth(GPIO);
00236       CHECK(3, t+t+1, v, pw[t], 0, "get servo pulsewidth");
00237 
00238       time_sleep(1);
00239       t3_reset = 1;
00240       time_sleep(4);
00241       on = t3_on;
00242       off = t3_off;
00243       CHECK(3, t+t+2, (1E3*(on+off))/on, 2E7/pw[t], 1,
00244           "set servo pulsewidth");
00245    }
00246 
00247    gpioServo(GPIO, 0);
00248    gpioSetPWMfrequency(GPIO, 1000);
00249    f = gpioGetPWMfrequency(GPIO);
00250    CHECK(3, 7, f, 1000, 0, "set/get PWM frequency");
00251 
00252    rr = gpioSetPWMrange(GPIO, 100);
00253    CHECK(3, 8, rr, 200, 0, "set PWM range");
00254 
00255    for (t=0; t<4; t++)
00256    {
00257       gpioPWM(GPIO, dc[t]);
00258       v = gpioGetPWMdutycycle(GPIO);
00259       CHECK(3, t+t+9, v, dc[t], 0, "get PWM dutycycle");
00260 
00261       time_sleep(1);
00262       t3_reset = 1;
00263       time_sleep(2);
00264       on = t3_on;
00265       off = t3_off;
00266       CHECK(3, t+t+10, (1E3*on)/(on+off), 1E1*dc[t], 1,
00267          "set PWM dutycycle");
00268    }
00269 
00270    gpioPWM(GPIO, 0);
00271 }
00272 
00273 void t4()
00274 {
00275    int h, e, f, n, s, b, l, seq_ok, toggle_ok;
00276    gpioReport_t r;
00277    char p[32];
00278 
00279    printf("Pipe notification tests.\n");
00280 
00281    gpioSetPWMfrequency(GPIO, 0);
00282    gpioPWM(GPIO, 0);
00283    gpioSetPWMrange(GPIO, 100);
00284 
00285    h = gpioNotifyOpen();
00286 
00287    sprintf(p, "/dev/pigpio%d", h);
00288    f = open(p, O_RDONLY);
00289 
00290    e = gpioNotifyBegin(h, (1<<GPIO));
00291    CHECK(4, 1, e, 0, 0, "notify open/begin");
00292 
00293    gpioPWM(GPIO, 50);
00294    time_sleep(4);
00295    gpioPWM(GPIO, 0);
00296 
00297    e = gpioNotifyPause(h);
00298    CHECK(4, 2, e, 0, 0, "notify pause");
00299 
00300    e = gpioNotifyClose(h);
00301    CHECK(4, 3, e, 0, 0, "notify close");
00302 
00303    n = 0;
00304    s = 0;
00305    l = 0;
00306    seq_ok = 1;
00307    toggle_ok = 1;
00308 
00309    while (1)
00310    {
00311       b = read(f, &r, 12);
00312       if (b == 12)
00313       {
00314          if (s != r.seqno) seq_ok = 0;
00315 
00316          if (n) if (l != (r.level&(1<<GPIO))) toggle_ok = 0;
00317 
00318          if (r.level&(1<<GPIO)) l = 0;
00319          else                   l = (1<<GPIO);
00320            
00321          s++;
00322          n++;
00323 
00324          // printf("%d %d %d %X\n", r.seqno, r.flags, r.tick, r.level);
00325       }
00326       else break;
00327    }
00328 
00329    close(f);
00330 
00331    CHECK(4, 4, seq_ok, 1, 0, "sequence numbers ok");
00332 
00333    CHECK(4, 5, toggle_ok, 1, 0, "gpio toggled ok");
00334 
00335    CHECK(4, 6, n, 80, 10, "number of notifications");
00336 }
00337 
00338 int t5_count;
00339 
00340 void t5cbf(int gpio, int level, uint32_t tick)
00341 {
00342    if (level == 0) t5_count++; /* falling edges */
00343 }
00344 
00345 void t5()
00346 {
00347    int BAUD=4800;
00348 
00349    char *TEXT=
00350 "\n\
00351 Now is the winter of our discontent\n\
00352 Made glorious summer by this sun of York;\n\
00353 And all the clouds that lour'd upon our house\n\
00354 In the deep bosom of the ocean buried.\n\
00355 Now are our brows bound with victorious wreaths;\n\
00356 Our bruised arms hung up for monuments;\n\
00357 Our stern alarums changed to merry meetings,\n\
00358 Our dreadful marches to delightful measures.\n\
00359 Grim-visaged war hath smooth'd his wrinkled front;\n\
00360 And now, instead of mounting barded steeds\n\
00361 To fright the souls of fearful adversaries,\n\
00362 He capers nimbly in a lady's chamber\n\
00363 To the lascivious pleasing of a lute.\n\
00364 ";
00365 
00366    gpioPulse_t wf[] =
00367    {
00368       {1<<GPIO, 0,  10000},
00369       {0, 1<<GPIO,  30000},
00370       {1<<GPIO, 0,  60000},
00371       {0, 1<<GPIO, 100000},
00372    };
00373 
00374    int e, oc, c, wid;
00375 
00376    char text[2048];
00377 
00378    printf("Waveforms & serial read/write tests.\n");
00379 
00380    t5_count = 0;
00381 
00382    gpioSetAlertFunc(GPIO, t5cbf);
00383 
00384    gpioSetMode(GPIO, PI_OUTPUT);
00385 
00386    e = gpioWaveClear();
00387    CHECK(5, 1, e, 0, 0, "callback, set mode, wave clear");
00388 
00389    e = gpioWaveAddGeneric(4, wf);
00390    CHECK(5, 2, e, 4, 0, "pulse, wave add generic");
00391 
00392    wid = gpioWaveCreate();
00393    e = gpioWaveTxSend(wid, PI_WAVE_MODE_REPEAT);
00394    if (e < 14) CHECK(5, 3, e,  9, 0, "wave tx repeat");
00395    else        CHECK(5, 3, e, 19, 0, "wave tx repeat");
00396 
00397    oc = t5_count;
00398    time_sleep(5);
00399    c = t5_count - oc;
00400    CHECK(5, 4, c, 50, 1, "callback");
00401 
00402    e = gpioWaveTxStop();
00403    CHECK(5, 5, e, 0, 0, "wave tx stop");
00404 
00405    /* gpioSerialReadOpen changes the alert function */
00406 
00407    e = gpioSerialReadOpen(GPIO, BAUD, 8);
00408    CHECK(5, 6, e, 0, 0, "serial read open");
00409 
00410    gpioWaveClear();
00411    e = gpioWaveAddSerial(GPIO, BAUD, 8, 2, 5000000, strlen(TEXT), TEXT);
00412    CHECK(5, 7, e, 3405, 0, "wave clear, wave add serial");
00413 
00414    wid = gpioWaveCreate();
00415    e = gpioWaveTxSend(wid, PI_WAVE_MODE_ONE_SHOT);
00416    if (e < 6964) CHECK(5, 8, e, 6811, 0, "wave tx start");
00417    else          CHECK(5, 8, e, 7116, 0, "wave tx start");
00418 
00419    CHECK(5, 9, 0, 0, 0, "NOT APPLICABLE");
00420 
00421    CHECK(5, 10, 0, 0, 0, "NOT APPLICABLE");
00422 
00423    while (gpioWaveTxBusy()) time_sleep(0.1);
00424    time_sleep(0.1);
00425    c = gpioSerialRead(GPIO, text, sizeof(text)-1);
00426    if (c > 0) text[c] = 0;
00427    CHECK(5, 11, strcmp(TEXT, text), 0, 0, "wave tx busy, serial read");
00428 
00429    e = gpioSerialReadClose(GPIO);
00430    CHECK(5, 12, e, 0, 0, "serial read close");
00431 
00432    c = gpioWaveGetMicros();
00433    CHECK(5, 13, c, 6158148, 0, "wave get micros");
00434 
00435    c = gpioWaveGetHighMicros();
00436    CHECK(5, 14, c, 6158148, 0, "wave get high micros");
00437 
00438    c = gpioWaveGetMaxMicros();
00439    CHECK(5, 15, c, 1800000000, 0, "wave get max micros");
00440 
00441    c = gpioWaveGetPulses();
00442    CHECK(5, 16, c, 3405, 0, "wave get pulses");
00443 
00444    c = gpioWaveGetHighPulses();
00445    CHECK(5, 17, c, 3405, 0, "wave get high pulses");
00446 
00447    c = gpioWaveGetMaxPulses();
00448    CHECK(5, 18, c, 12000, 0, "wave get max pulses");
00449 
00450    c = gpioWaveGetCbs();
00451    if (e < 6963) CHECK(5, 19, c, 6810, 0, "wave get cbs");
00452    else          CHECK(5, 19, c, 7115, 0, "wave get cbs");
00453 
00454    c = gpioWaveGetHighCbs();
00455    if (e < 6963) CHECK(5, 20, c, 6810, 0, "wave get high cbs");
00456    else          CHECK(5, 20, c, 7115, 0, "wave get high cbs");
00457 
00458    c = gpioWaveGetMaxCbs();
00459    CHECK(5, 21, c, 25016, 0, "wave get max cbs");
00460 }
00461 
00462 int t6_count;
00463 int t6_on;
00464 uint32_t t6_on_tick;
00465 
00466 void t6cbf(int gpio, int level, uint32_t tick)
00467 {
00468    if (level == 1)
00469    {
00470       t6_on_tick = tick;
00471       t6_count++;
00472    }
00473    else
00474    {
00475       if (t6_on_tick) t6_on += (tick - t6_on_tick);
00476    }
00477 }
00478 
00479 void t6()
00480 {
00481    int tp, t, p;
00482 
00483    printf("Trigger tests\n");
00484 
00485    gpioWrite(GPIO, PI_LOW);
00486 
00487    tp = 0;
00488 
00489    t6_count=0;
00490    t6_on=0;
00491    t6_on_tick=0;
00492 
00493    gpioSetAlertFunc(GPIO, t6cbf);
00494 
00495    for (t=0; t<5; t++)
00496    {
00497       time_sleep(0.1);
00498       p = 10 + (t*10);
00499       tp += p;
00500       gpioTrigger(GPIO, p, 1);
00501    }
00502 
00503    time_sleep(0.2);
00504 
00505    CHECK(6, 1, t6_count, 5, 0, "gpio trigger count");
00506 
00507    CHECK(6, 2, t6_on, tp, 25, "gpio trigger pulse length");
00508 }
00509 
00510 int t7_count;
00511 
00512 void t7cbf(int gpio, int level, uint32_t tick)
00513 {
00514    if (level == PI_TIMEOUT) t7_count++;
00515 }
00516 
00517 void t7()
00518 {
00519    int c, oc;
00520 
00521    printf("Watchdog tests.\n");
00522 
00523    t7_count=0;
00524 
00525    /* type of edge shouldn't matter for watchdogs */
00526    gpioSetAlertFunc(GPIO, t7cbf);
00527 
00528    gpioSetWatchdog(GPIO, 50); /* 50 ms, 20 per second */
00529    time_sleep(0.5);
00530    oc = t7_count;
00531    time_sleep(2);
00532    c = t7_count - oc;
00533    CHECK(7, 1, c, 39, 5, "set watchdog on count");
00534 
00535    gpioSetWatchdog(GPIO, 0); /* 0 switches watchdog off */
00536    time_sleep(0.5);
00537    oc = t7_count;
00538    time_sleep(2);
00539    c = t7_count - oc;
00540    CHECK(7, 2, c, 0, 1, "set watchdog off count");
00541 }
00542 
00543 void t8()
00544 {
00545    int v;
00546 
00547    printf("Bank read/write tests.\n");
00548 
00549    gpioWrite(GPIO, 0);
00550    v = gpioRead_Bits_0_31() & (1<<GPIO);
00551    CHECK(8, 1, v, 0, 0, "read bank 1");
00552 
00553    gpioWrite(GPIO, 1);
00554    v = gpioRead_Bits_0_31() & (1<<GPIO);
00555    CHECK(8, 2, v, (1<<GPIO), 0, "read bank 1");
00556 
00557    gpioWrite_Bits_0_31_Clear(1<<GPIO);
00558    v = gpioRead(GPIO);
00559    CHECK(8, 3, v, 0, 0, "clear bank 1");
00560 
00561    gpioWrite_Bits_0_31_Set(1<<GPIO);
00562    v = gpioRead(GPIO);
00563    CHECK(8, 4, v, 1, 0, "set bank 1");
00564 
00565    v = gpioRead_Bits_32_53();
00566 
00567    if (v) v = 0; else v = 1;
00568 
00569    CHECK(8, 5, v, 0, 0, "read bank 2");
00570 
00571    v = gpioWrite_Bits_32_53_Clear(0);
00572    CHECK(8, 6, v, 0, 0, "clear bank 2");
00573 
00574    CHECK(8, 7, 0, 0, 0, "NOT APPLICABLE");
00575 
00576    v = gpioWrite_Bits_32_53_Set(0);
00577    CHECK(8, 8, v, 0, 0, "set bank 2");
00578 
00579    CHECK(8, 9, 0, 0, 0, "NOT APPLICABLE");
00580 }
00581 
00582 int t9_count;
00583 
00584 void t9cbf(int gpio, int level, uint32_t tick)
00585 {
00586    if (level == 1) t9_count++;
00587 }
00588 
00589 void t9()
00590 {
00591    int s, oc, c, e;
00592    uint32_t p[10];
00593 
00594    /*
00595    100 loops per second
00596    p0 number of loops
00597    p1 GPIO
00598    */
00599    char *script="\
00600    ld p9 p0\
00601    tag 0\
00602    w p1 1\
00603    mils 5\
00604    w p1 0\
00605    mils 5\
00606    dcr p9\
00607    jp 0";
00608 
00609    printf("Script store/run/status/stop/delete tests.\n");
00610 
00611    gpioWrite(GPIO, 0); /* need known state */
00612 
00613    t9_count = 0;
00614 
00615    gpioSetAlertFunc(GPIO, t9cbf);
00616 
00617    s = gpioStoreScript(script);
00618 
00619    while (1)
00620    {
00621       /* loop until script initialised */
00622       time_sleep(0.1);
00623       e = gpioScriptStatus(s, p);
00624       if (e != PI_SCRIPT_INITING) break;
00625    }
00626 
00627    oc = t9_count;
00628    p[0] = 99;
00629    p[1] = GPIO;
00630    gpioRunScript(s, 2, p);
00631    time_sleep(2);
00632    c = t9_count - oc;
00633    CHECK(9, 1, c, 100, 0, "store/run script");
00634 
00635    oc = t9_count;
00636    p[0] = 200;
00637    p[1] = GPIO;
00638    gpioRunScript(s, 2, p);
00639    time_sleep(0.1);
00640    while (1)
00641    {
00642       e = gpioScriptStatus(s, p);
00643       if (e != PI_SCRIPT_RUNNING) break;
00644       time_sleep(0.5);
00645    }
00646    c = t9_count - oc;
00647    time_sleep(0.1);
00648    CHECK(9, 2, c, 201, 0, "run script/script status");
00649 
00650    oc = t9_count;
00651    p[0] = 2000;
00652    p[1] = GPIO;
00653    gpioRunScript(s, 2, p);
00654    time_sleep(0.1);
00655    while (1)
00656    {
00657       e = gpioScriptStatus(s, p);
00658       if (e != PI_SCRIPT_RUNNING) break;
00659       if (p[9] < 1900) gpioStopScript(s);
00660       time_sleep(0.1);
00661    }
00662    c = t9_count - oc;
00663    time_sleep(0.1);
00664    CHECK(9, 3, c, 110, 10, "run/stop script/script status");
00665 
00666    e = gpioDeleteScript(s);
00667    CHECK(9, 4, e, 0, 0, "delete script");
00668 }
00669 
00670 void ta()
00671 {
00672    int h, b, e;
00673    char *TEXT;
00674    char text[2048];
00675 
00676    printf("Serial link tests.\n");
00677 
00678    /* this test needs RXD and TXD to be connected */
00679 
00680    h = serOpen("/dev/ttyAMA0", 57600, 0);
00681 
00682    CHECK(10, 1, h, 0, 0, "serial open");
00683 
00684    b = serRead(h, text, sizeof(text)); /* flush buffer */
00685 
00686    b = serDataAvailable(h);
00687    CHECK(10, 2, b, 0, 0, "serial data available");
00688 
00689    TEXT = "\
00690 To be, or not to be, that is the question-\
00691 Whether 'tis Nobler in the mind to suffer\
00692 The Slings and Arrows of outrageous Fortune,\
00693 Or to take Arms against a Sea of troubles,\
00694 ";
00695    e = serWrite(h, TEXT, strlen(TEXT));
00696    CHECK(10, 3, e, 0, 0, "serial write");
00697 
00698    e = serWriteByte(h, 0xAA);
00699    e = serWriteByte(h, 0x55);
00700    e = serWriteByte(h, 0x00);
00701    e = serWriteByte(h, 0xFF);
00702 
00703    CHECK(10, 4, e, 0, 0, "serial write byte");
00704 
00705    time_sleep(0.1); /* allow time for transmission */
00706 
00707    b = serDataAvailable(h);
00708    CHECK(10, 5, b, strlen(TEXT)+4, 0, "serial data available");
00709 
00710    b = serRead(h, text, strlen(TEXT));
00711    CHECK(10, 6, b, strlen(TEXT), 0, "serial read");
00712    if (b >= 0) text[b] = 0;
00713    CHECK(10, 7, strcmp(TEXT, text), 0, 0, "serial read");
00714 
00715    b = serReadByte(h);
00716    CHECK(10, 8, b, 0xAA, 0, "serial read byte");
00717 
00718    b = serReadByte(h);
00719    CHECK(10, 9, b, 0x55, 0, "serial read byte");
00720 
00721    b = serReadByte(h);
00722    CHECK(10, 10, b, 0x00, 0, "serial read byte");
00723 
00724    b = serReadByte(h);
00725    CHECK(10, 11, b, 0xFF, 0, "serial read byte");
00726 
00727    b = serDataAvailable(h);
00728    CHECK(10, 12, b, 0, 0, "serial data availabe");
00729 
00730    e = serClose(h);
00731    CHECK(10, 13, e, 0, 0, "serial close");
00732 }
00733 
00734 void tb()
00735 {
00736    int h, e, b, len;
00737    char *exp;
00738    char buf[128];
00739 
00740    printf("SMBus / I2C tests.");
00741 
00742    /* this test requires an ADXL345 on I2C bus 1 addr 0x53 */
00743 
00744    h = i2cOpen(1, 0x53, 0);
00745    CHECK(11, 1, h, 0, 0, "i2cOpen");
00746 
00747    e = i2cWriteDevice(h, "\x00", 1); /* move to known register */
00748    CHECK(11, 2, e, 0, 0, "i2cWriteDevice");
00749 
00750    b = i2cReadDevice(h, buf, 1);
00751    CHECK(11, 3, b, 1, 0, "i2cReadDevice");
00752    CHECK(11, 4, buf[0], 0xE5, 0, "i2cReadDevice");
00753 
00754    b = i2cReadByte(h);
00755    CHECK(11, 5, b, 0xE5, 0, "i2cReadByte");
00756 
00757    b = i2cReadByteData(h, 0);
00758    CHECK(11, 6, b, 0xE5, 0, "i2cReadByteData");
00759 
00760    b = i2cReadByteData(h, 48);
00761    CHECK(11, 7, b, 2, 0, "i2cReadByteData");
00762 
00763    exp = "\x1D[aBcDeFgHjKM]";
00764    len = strlen(exp);
00765 
00766    e = i2cWriteDevice(h, exp, len);
00767    CHECK(11, 8, e, 0, 0, "i2cWriteDevice");
00768 
00769    e = i2cWriteDevice(h, "\x1D", 1);
00770    b = i2cReadDevice(h, buf, len-1);
00771    CHECK(11, 9, b, len-1, 0, "i2cReadDevice");
00772    CHECK(11, 10, strncmp(buf, exp+1, len-1), 0, 0, "i2cReadDevice");
00773 
00774    if (strncmp(buf, exp+1, len-1))
00775       printf("got [%.*s] expected [%.*s]\n", len-1, buf, len-1, exp+1);
00776 
00777    e = i2cWriteByteData(h, 0x1d, 0xAA);
00778    CHECK(11, 11, e, 0, 0, "i2cWriteByteData");
00779 
00780    b = i2cReadByteData(h, 0x1d);
00781    CHECK(11, 12, b, 0xAA, 0, "i2cReadByteData");
00782 
00783    e = i2cWriteByteData(h, 0x1d, 0x55);
00784    CHECK(11, 13, e, 0, 0, "i2cWriteByteData");
00785 
00786    b = i2cReadByteData(h, 0x1d);
00787    CHECK(11, 14, b, 0x55, 0, "i2cReadByteData");
00788 
00789    exp = "[1234567890#]";
00790    len = strlen(exp);
00791 
00792    e = i2cWriteBlockData(h, 0x1C, exp, len);
00793    CHECK(11, 15, e, 0, 0, "i2c writeBlockData");
00794 
00795    e = i2cWriteDevice(h, "\x1D", 1);
00796    b = i2cReadDevice(h, buf, len);
00797    CHECK(11, 16, b, len, 0, "i2cReadDevice");
00798    CHECK(11, 17, strncmp(buf, exp, len), 0, 0, "i2cReadDevice");
00799 
00800    if (strncmp(buf, exp, len))
00801       printf("got [%.*s] expected [%.*s]\n", len, buf, len, exp);
00802 
00803    b = i2cReadI2CBlockData(h, 0x1D, buf, len);
00804    CHECK(11, 18, b, len, 0, "i2cReadI2CBlockData");
00805    CHECK(11, 19, strncmp(buf, exp, len), 0, 0, "i2cReadI2CBlockData");
00806 
00807    if (strncmp(buf, exp, len))
00808       printf("got [%.*s] expected [%.*s]\n", len, buf, len, exp);
00809 
00810    exp = "(-+=;:,<>!%)";
00811    len = strlen(exp);
00812 
00813    e = i2cWriteI2CBlockData(h, 0x1D, exp, len);
00814    CHECK(11, 20, e, 0, 0, "i2cWriteI2CBlockData");
00815 
00816    b = i2cReadI2CBlockData(h, 0x1D, buf, len);
00817    CHECK(11, 21, b, len, 0, "i2cReadI2CBlockData");
00818    CHECK(11, 22, strncmp(buf, exp, len), 0, 0, "i2cReadI2CBlockData");
00819 
00820    if (strncmp(buf, exp, len))
00821       printf("got [%.*s] expected [%.*s]\n", len, buf, len, exp);
00822 
00823    e = i2cClose(h);
00824    CHECK(11, 23, e, 0, 0, "i2cClose");
00825 }
00826 
00827 void tc()
00828 {
00829    int h, x, b, e;
00830    char txBuf[8], rxBuf[8];
00831 
00832    printf("SPI tests.");
00833 
00834    /* this test requires a MCP3202 on SPI channel 1 */
00835 
00836    h = spiOpen(1, 50000, 0);
00837    CHECK(12, 1, h, 0, 0, "spiOpen");
00838 
00839    sprintf(txBuf, "\x01\x80");
00840 
00841    for (x=0; x<5; x++)
00842    {
00843       b = spiXfer(h, txBuf, rxBuf, 3);
00844       CHECK(12, 2, b, 3, 0, "spiXfer");
00845       if (b == 3)
00846       {
00847          time_sleep(1.0);
00848          printf("%d ", ((rxBuf[1]&0x0F)*256)|rxBuf[2]);
00849       }
00850    }
00851 
00852    e = spiClose(h);
00853    CHECK(12, 99, e, 0, 0, "spiClose");
00854 }
00855 
00856 int main(int argc, char *argv[])
00857 {
00858    int i, t, c, status;
00859 
00860    char test[64]={0,};
00861 
00862    if (argc > 1)
00863    {
00864       t = 0;
00865 
00866       for (i=0; i<strlen(argv[1]); i++)
00867       {
00868          c = tolower(argv[1][i]);
00869 
00870          if (!strchr(test, c))
00871          {
00872             test[t++] = c;
00873             test[t] = 0;
00874          }
00875       }
00876    }
00877    else strcat(test, "0123456789");
00878 
00879    status = gpioInitialise();
00880 
00881    if (status < 0)
00882    {
00883       fprintf(stderr, "pigpio initialisation failed.\n");
00884       return 1;
00885    }
00886 
00887    if (strchr(test, '0')) t0();
00888    if (strchr(test, '1')) t1();
00889    if (strchr(test, '2')) t2();
00890    if (strchr(test, '3')) t3();
00891    if (strchr(test, '4')) t4();
00892    if (strchr(test, '5')) t5();
00893    if (strchr(test, '6')) t6();
00894    if (strchr(test, '7')) t7();
00895    if (strchr(test, '8')) t8();
00896    if (strchr(test, '9')) t9();
00897    if (strchr(test, 'a')) ta();
00898    if (strchr(test, 'b')) tb();
00899    if (strchr(test, 'c')) tc();
00900 
00901    gpioTerminate();
00902 
00903    return 0;
00904 }
00905 


cob_hand_bridge
Author(s): Mathias Lüdtke
autogenerated on Thu Jun 6 2019 20:43:57