/* gcc -Wall -pthread -o x_pigpiod_if2 x_pigpiod_if2.c -lpigpiod_if2 ./x_pigpiod_if2 *** WARNING ************************************************ * * * All the tests make extensive use of gpio 25 (pin 22). * * Ensure that either nothing or just a LED is connected to * * gpio 25 before running any of the tests. * * * * Some tests are statistical in nature and so may on * * occasion fail. Repeated failures on the same test or * * many failures in a group of tests indicate a problem. * ************************************************************ */ #include #include #include #include #include #include #include #include "pigpiod_if2.h" #define GPIO 25 void CHECK(int t, int st, int got, int expect, int pc, char *desc) { if ((got >= (((1E2-pc)*expect)/1E2)) && (got <= (((1E2+pc)*expect)/1E2))) { printf("TEST %2d.%-2d PASS (%s: %d)\n", t, st, desc, expect); } else { fprintf(stderr, "TEST %2d.%-2d FAILED got %d (%s: %d)\n", t, st, got, desc, expect); } } void t0(int pi) { printf("\nTesting pigpiod C I/F 2\n"); printf("pigpio version %d.\n", get_pigpio_version(pi)); printf("Hardware revision %d.\n", get_hardware_revision(pi)); } void t1(int pi) { int v; printf("Mode/PUD/read/write tests.\n"); set_mode(pi, GPIO, PI_INPUT); v = get_mode(pi, GPIO); CHECK(1, 1, v, 0, 0, "set mode, get mode"); set_pull_up_down(pi, GPIO, PI_PUD_UP); v = gpio_read(pi, GPIO); CHECK(1, 2, v, 1, 0, "set pull up down, read"); set_pull_up_down(pi, GPIO, PI_PUD_DOWN); v = gpio_read(pi, GPIO); CHECK(1, 3, v, 0, 0, "set pull up down, read"); gpio_write(pi, GPIO, PI_LOW); v = get_mode(pi, GPIO); CHECK(1, 4, v, 1, 0, "write, get mode"); v = gpio_read(pi, GPIO); CHECK(1, 5, v, 0, 0, "read"); gpio_write(pi, GPIO, PI_HIGH); v = gpio_read(pi, GPIO); CHECK(1, 6, v, 1, 0, "write, read"); } int t2_count=0; void t2cb(int pi, unsigned gpio, unsigned level, uint32_t tick) { t2_count++; } void t2(int pi) { int dc, f, r, rr, oc, id; printf("PWM dutycycle/range/frequency tests.\n"); set_PWM_range(pi, GPIO, 255); set_PWM_frequency(pi, GPIO, 0); f = get_PWM_frequency(pi, GPIO); CHECK(2, 1, f, 10, 0, "set PWM range, set/get PWM frequency"); id = callback(pi, GPIO, EITHER_EDGE, t2cb); set_PWM_dutycycle(pi, GPIO, 0); dc = get_PWM_dutycycle(pi, GPIO); CHECK(2, 2, dc, 0, 0, "get PWM dutycycle"); time_sleep(0.5); /* allow old notifications to flush */ oc = t2_count; time_sleep(2); f = t2_count - oc; CHECK(2, 3, f, 0, 0, "set PWM dutycycle, callback"); set_PWM_dutycycle(pi, GPIO, 128); dc = get_PWM_dutycycle(pi, GPIO); CHECK(2, 4, dc, 128, 0, "get PWM dutycycle"); time_sleep(0.2); oc = t2_count; time_sleep(2); f = t2_count - oc; CHECK(2, 5, f, 40, 5, "set PWM dutycycle, callback"); set_PWM_frequency(pi, GPIO, 100); f = get_PWM_frequency(pi, GPIO); CHECK(2, 6, f, 100, 0, "set/get PWM frequency"); time_sleep(0.2); oc = t2_count; time_sleep(2); f = t2_count - oc; CHECK(2, 7, f, 400, 1, "callback"); set_PWM_frequency(pi, GPIO, 1000); f = get_PWM_frequency(pi, GPIO); CHECK(2, 8, f, 1000, 0, "set/get PWM frequency"); time_sleep(0.2); oc = t2_count; time_sleep(2); f = t2_count - oc; CHECK(2, 9, f, 4000, 1, "callback"); r = get_PWM_range(pi, GPIO); CHECK(2, 10, r, 255, 0, "get PWM range"); rr = get_PWM_real_range(pi, GPIO); CHECK(2, 11, rr, 200, 0, "get PWM real range"); set_PWM_range(pi, GPIO, 2000); r = get_PWM_range(pi, GPIO); CHECK(2, 12, r, 2000, 0, "set/get PWM range"); rr = get_PWM_real_range(pi, GPIO); CHECK(2, 13, rr, 200, 0, "get PWM real range"); set_PWM_dutycycle(pi, GPIO, 0); callback_cancel(id); } int t3_reset=1; int t3_count=0; uint32_t t3_tick=0; float t3_on=0.0; float t3_off=0.0; void t3cbf(int pi, unsigned gpio, unsigned level, uint32_t tick) { uint32_t td; // printf("pi=%d g=%d l=%d t=%u\n", pi, gpio, level, tick); if (t3_reset) { t3_count = 0; t3_on = 0.0; t3_off = 0.0; t3_reset = 0; } else { td = tick - t3_tick; if (level == 0) t3_on += td; else t3_off += td; } t3_count ++; t3_tick = tick; } void t3(int pi) { int pw[3]={500, 1500, 2500}; int dc[4]={20, 40, 60, 80}; int f, rr, v; float on, off; int t, id; printf("PWM/Servo pulse accuracy tests.\n"); id = callback(pi, GPIO, EITHER_EDGE, t3cbf); for (t=0; t<3; t++) { set_servo_pulsewidth(pi, GPIO, pw[t]); v = get_servo_pulsewidth(pi, GPIO); CHECK(3, t+t+1, v, pw[t], 0, "get servo pulsewidth"); time_sleep(1); t3_reset = 1; time_sleep(4); on = t3_on; off = t3_off; CHECK(3, t+t+2, (1000.0*(on+off))/on, 20000000.0/pw[t], 1, "set servo pulsewidth"); } set_servo_pulsewidth(pi, GPIO, 0); set_PWM_frequency(pi, GPIO, 1000); f = get_PWM_frequency(pi, GPIO); CHECK(3, 7, f, 1000, 0, "set/get PWM frequency"); rr = set_PWM_range(pi, GPIO, 100); CHECK(3, 8, rr, 200, 0, "set PWM range"); for (t=0; t<4; t++) { set_PWM_dutycycle(pi, GPIO, dc[t]); v = get_PWM_dutycycle(pi, GPIO); CHECK(3, t+t+9, v, dc[t], 0, "get PWM dutycycle"); time_sleep(1); t3_reset = 1; time_sleep(2); on = t3_on; off = t3_off; CHECK(3, t+t+10, (1000.0*on)/(on+off), 10.0*dc[t], 1, "set PWM dutycycle"); } set_PWM_dutycycle(pi, GPIO, 0); callback_cancel(id); } void t4(int pi) { int h, e, f, n, s, b, l, seq_ok, toggle_ok; gpioReport_t r; char p[32]; printf("Pipe notification tests.\n"); set_PWM_frequency(pi, GPIO, 0); set_PWM_dutycycle(pi, GPIO, 0); set_PWM_range(pi, GPIO, 100); h = notify_open(pi); sprintf(p, "/dev/pigpio%d", h); f = open(p, O_RDONLY); e = notify_begin(pi, h, (1< 0) text[c] = 0; /* null terminate string */ CHECK(5, 11, strcmp(TEXT, text), 0, 0, "wave tx busy, serial read"); e = bb_serial_read_close(pi, GPIO); CHECK(5, 12, e, 0, 0, "serial read close"); c = wave_get_micros(pi); CHECK(5, 13, c, 6158148, 0, "wave get micros"); c = wave_get_high_micros(pi); if (c > 6158148) c = 6158148; CHECK(5, 14, c, 6158148, 0, "wave get high micros"); c = wave_get_max_micros(pi); CHECK(5, 15, c, 1800000000, 0, "wave get max micros"); c = wave_get_pulses(pi); CHECK(5, 16, c, 3405, 0, "wave get pulses"); c = wave_get_high_pulses(pi); CHECK(5, 17, c, 3405, 0, "wave get high pulses"); c = wave_get_max_pulses(pi); CHECK(5, 18, c, 12000, 0, "wave get max pulses"); c = wave_get_cbs(pi); if (c < 6963) CHECK(5, 19, c, 6810, 0, "wave get cbs"); else CHECK(5, 19, c, 7115, 0, "wave get cbs"); c = wave_get_high_cbs(pi); if (c < 6963) CHECK(5, 20, c, 6810, 0, "wave get high cbs"); else CHECK(5, 20, c, 7115, 0, "wave get high cbs"); c = wave_get_max_cbs(pi); CHECK(5, 21, c, 25016, 0, "wave get max cbs"); callback_cancel(id); } int t6_count=0; int t6_on=0; uint32_t t6_on_tick=0; void t6cbf(int pi, unsigned gpio, unsigned level, uint32_t tick) { if (level == 1) { t6_on_tick = tick; t6_count++; } else { if (t6_on_tick) t6_on += (tick - t6_on_tick); } } void t6(int pi) { int tp, t, p, id; printf("Trigger tests.\n"); gpio_write(pi, GPIO, PI_LOW); tp = 0; id = callback(pi, GPIO, EITHER_EDGE, t6cbf); time_sleep(0.2); for (t=0; t<5; t++) { time_sleep(0.1); p = 10 + (t*10); tp += p; gpio_trigger(pi, GPIO, p, 1); } time_sleep(0.5); CHECK(6, 1, t6_count, 5, 0, "gpio trigger count"); CHECK(6, 2, t6_on, tp, 25, "gpio trigger pulse length"); callback_cancel(id); } int t7_count=0; void t7cbf(int pi, unsigned gpio, unsigned level, uint32_t tick) { if (level == PI_TIMEOUT) t7_count++; } void t7(int pi) { int c, oc, id; printf("Watchdog tests.\n"); /* type of edge shouldn't matter for watchdogs */ id = callback(pi, GPIO, FALLING_EDGE, t7cbf); set_watchdog(pi, GPIO, 50); /* 50 ms, 20 per second */ time_sleep(0.5); oc = t7_count; time_sleep(2); c = t7_count - oc; CHECK(7, 1, c, 39, 5, "set watchdog on count"); set_watchdog(pi, GPIO, 0); /* 0 switches watchdog off */ time_sleep(0.5); oc = t7_count; time_sleep(2); c = t7_count - oc; CHECK(7, 2, c, 0, 1, "set watchdog off count"); callback_cancel(id); } void t8(int pi) { int v; printf("Bank read/write tests.\n"); gpio_write(pi, GPIO, 0); v = read_bank_1(pi) & (1<= 0) text[b] = 0; CHECK(10, 7, strcmp(TEXT, text), 0, 0, "serial read"); b = serial_read_byte(pi, h); CHECK(10, 8, b, 0xAA, 0, "serial read byte"); b = serial_read_byte(pi, h); CHECK(10, 9, b, 0x55, 0, "serial read byte"); b = serial_read_byte(pi, h); CHECK(10, 10, b, 0x00, 0, "serial read byte"); b = serial_read_byte(pi, h); CHECK(10, 11, b, 0xFF, 0, "serial read byte"); b = serial_data_available(pi, h); CHECK(10, 12, b, 0, 0, "serial data availabe"); e = serial_close(pi, h); CHECK(10, 13, e, 0, 0, "serial close"); } void tb(int pi) { int h, e, b, len; char *exp; char buf[128]; printf("SMBus / I2C tests."); /* this test requires an ADXL345 on I2C bus 1 addr 0x53 */ h = i2c_open(pi, 1, 0x53, 0); CHECK(11, 1, h, 0, 0, "i2c open"); e = i2c_write_device(pi, h, "\x00", 1); /* move to known register */ CHECK(11, 2, e, 0, 0, "i2c write device"); b = i2c_read_device(pi, h, buf, 1); CHECK(11, 3, b, 1, 0, "i2c read device"); CHECK(11, 4, buf[0], 0xE5, 0, "i2c read device"); b = i2c_read_byte(pi, h); CHECK(11, 5, b, 0xE5, 0, "i2c read byte"); b = i2c_read_byte_data(pi, h, 0); CHECK(11, 6, b, 0xE5, 0, "i2c read byte data"); b = i2c_read_byte_data(pi, h, 48); CHECK(11, 7, b, 2, 0, "i2c read byte data"); exp = "\x1D[aBcDeFgHjKM]"; len = strlen(exp); e = i2c_write_device(pi, h, exp, len); CHECK(11, 8, e, 0, 0, "i2c write device"); e = i2c_write_device(pi, h, "\x1D", 1); b = i2c_read_device(pi, h, buf, len-1); CHECK(11, 9, b, len-1, 0, "i2c read device"); CHECK(11, 10, strncmp(buf, exp+1, len-1), 0, 0, "i2c read device"); if (strncmp(buf, exp+1, len-1)) printf("got [%.*s] expected [%.*s]\n", len-1, buf, len-1, exp+1); e = i2c_write_byte_data(pi, h, 0x1d, 0xAA); CHECK(11, 11, e, 0, 0, "i2c write byte data"); b = i2c_read_byte_data(pi, h, 0x1d); CHECK(11, 12, b, 0xAA, 0, "i2c read byte data"); e = i2c_write_byte_data(pi, h, 0x1d, 0x55); CHECK(11, 13, e, 0, 0, "i2c write byte data"); b = i2c_read_byte_data(pi, h, 0x1d); CHECK(11, 14, b, 0x55, 0, "i2c read byte data"); exp = "[1234567890#]"; len = strlen(exp); e = i2c_write_block_data(pi, h, 0x1C, exp, len); CHECK(11, 15, e, 0, 0, "i2c write block data"); e = i2c_write_device(pi, h, "\x1D", 1); b = i2c_read_device(pi, h, buf, len); CHECK(11, 16, b, len, 0, "i2c read device"); CHECK(11, 17, strncmp(buf, exp, len), 0, 0, "i2c read device"); if (strncmp(buf, exp, len)) printf("got [%.*s] expected [%.*s]\n", len, buf, len, exp); b = i2c_read_i2c_block_data(pi, h, 0x1D, buf, len); CHECK(11, 18, b, len, 0, "i2c read i2c block data"); CHECK(11, 19, strncmp(buf, exp, len), 0, 0, "i2c read i2c block data"); if (strncmp(buf, exp, len)) printf("got [%.*s] expected [%.*s]\n", len, buf, len, exp); exp = "(-+=;:,<>!%)"; len = strlen(exp); e = i2c_write_i2c_block_data(pi, h, 0x1D, exp, len); CHECK(11, 20, e, 0, 0, "i2c write i2c block data"); b = i2c_read_i2c_block_data(pi, h, 0x1D, buf, len); CHECK(11, 21, b, len, 0, "i2c read i2c block data"); CHECK(11, 22, strncmp(buf, exp, len), 0, 0, "i2c read i2c block data"); if (strncmp(buf, exp, len)) printf("got [%.*s] expected [%.*s]\n", len, buf, len, exp); e = i2c_close(pi, h); CHECK(11, 23, e, 0, 0, "i2c close"); } void tc(int pi) { int h, x, b, e; char buf[128]; printf("SPI tests."); /* this test requires a MCP3202 on SPI channel 1 */ h = spi_open(pi, 1, 50000, 0); CHECK(12, 1, h, 0, 0, "spi open"); for (x=0; x<5; x++) { sprintf(buf, "\x01\x80"); b = spi_xfer(pi, h, buf, buf, 3); CHECK(12, 2, b, 3, 0, "spi xfer"); if (b == 3) { time_sleep(1.0); printf("%d ", ((buf[1]&0x0F)*256)|buf[2]); } } e = spi_close(pi, h); CHECK(12, 99, e, 0, 0, "spi close"); } int main(int argc, char *argv[]) { int i, t, c, pi; char test[64]; if (argc > 1) { t = 0; for (i=0; i