/* * bench_inject — measure pog422_inject_burst tx_ns on the Pi. * * make bench_inject * ./bench_inject /dev/ttyXRUSB1 --len 12 --count 50 */ #include #include #include #include #include #include #include #include #include "pog422_inject.h" static void usage(const char *argv0) { fprintf( stderr, "Usage: %s [--len N] [--count N] [--pre-us N] [--post-us N] [--rt]\n", argv0 ); } static int open_port(const char *path) { int fd = open(path, O_RDWR | O_NOCTTY | O_NONBLOCK); if (fd < 0) { return -1; } if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_NONBLOCK) != 0) { close(fd); return -1; } struct termios tio; if (tcgetattr(fd, &tio) != 0) { close(fd); return -1; } cfsetispeed(&tio, B57600); cfsetospeed(&tio, B57600); tio.c_cflag = CS8 | CLOCAL | CREAD; tio.c_iflag = 0; tio.c_oflag = 0; tio.c_lflag = 0; if (tcsetattr(fd, TCSANOW, &tio) != 0) { close(fd); return -1; } pog422_gpio7_rx(fd); return fd; } int main(int argc, char **argv) { if (argc < 2) { usage(argv[0]); return 1; } const char *tty = argv[1]; size_t frame_len = 12; int count = 50; uint32_t pre_us = 0; uint32_t post_us = 2500; int use_rt = 0; for (int i = 2; i < argc; i++) { if (strcmp(argv[i], "--len") == 0 && i + 1 < argc) { frame_len = (size_t)atoi(argv[++i]); } else if (strcmp(argv[i], "--count") == 0 && i + 1 < argc) { count = atoi(argv[++i]); } else if (strcmp(argv[i], "--pre-us") == 0 && i + 1 < argc) { pre_us = (uint32_t)atoi(argv[++i]); } else if (strcmp(argv[i], "--post-us") == 0 && i + 1 < argc) { post_us = (uint32_t)atoi(argv[++i]); } else if (strcmp(argv[i], "--rt") == 0) { use_rt = 1; } else { usage(argv[0]); return 1; } } int fd = open_port(tty); if (fd < 0) { fprintf(stderr, "open %s: %s\n", tty, strerror(errno)); return 1; } uint8_t *frame = calloc(1, frame_len); if (!frame) { close(fd); return 1; } frame[0] = 0x05; if (frame_len > 1) { frame[1] = 0x0B; } if (frame_len > 2) { frame[frame_len - 1] = 0x0D; } pog422_inject_opts_t opts = { .pre_tx_us = pre_us, .post_hold_us = post_us, .flush_rx_before_tx = 0, .use_realtime = (uint8_t)use_rt, .rt_priority = 50, }; printf( "pog422_inject %s tty=%s len=%zu count=%d air_us=%u\n", pog422_inject_version(), tty, frame_len, count, (unsigned)((frame_len * 11ull * 1000000ull) / 57600ull) ); uint64_t min_tx = UINT64_MAX; uint64_t max_tx = 0; uint64_t sum_tx = 0; for (int n = 0; n < count; n++) { pog422_inject_result_t res; int rc = pog422_inject_burst(fd, frame, frame_len, &opts, &res); if (rc != 0) { fprintf(stderr, "burst %d failed err=%d\n", n, res.err); break; } if (res.tx_ns < min_tx) { min_tx = res.tx_ns; } if (res.tx_ns > max_tx) { max_tx = res.tx_ns; } sum_tx += res.tx_ns; } if (count > 0 && min_tx != UINT64_MAX) { printf( "tx_us: min=%.1f max=%.1f avg=%.1f\n", min_tx / 1000.0, max_tx / 1000.0, (sum_tx / (double)count) / 1000.0 ); } free(frame); close(fd); return 0; }