From c0267a06748e200a124c443ec9543f1c35dd67d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pressl=2C=20=C5=A0t=C4=9Bp=C3=A1n?= Date: Mon, 29 Jul 2024 11:41:54 +0200 Subject: [PATCH] examples/qencoder: obtain samples using the QEIOC_GETINDEX ioctl call Normally, the qencoder position is obtained using the QEIOC_POSITION ioctl call. Adding the -i argument uses the QEIOC_GETINDEX to obtain the qe_index_s struct containing the position alongisde other fields. Signed-off-by: Stepan Pressl --- examples/qencoder/qe.h | 1 + examples/qencoder/qe_main.c | 55 +++++++++++++++++++++++++++++-------- 2 files changed, 44 insertions(+), 12 deletions(-) diff --git a/examples/qencoder/qe.h b/examples/qencoder/qe.h index ab243c6d87c..e233c8c4b76 100644 --- a/examples/qencoder/qe.h +++ b/examples/qencoder/qe.h @@ -39,6 +39,7 @@ struct qe_example_s { FAR char *devpath; /* Path to the QE device */ bool reset; /* True: set the count back to zero */ + bool use_qeindex; /* True: use the QEIOC_GETINDEX call to get samples */ unsigned int nloops; /* Collect this number of samples */ unsigned int delay; /* Delay this number of seconds between samples */ }; diff --git a/examples/qencoder/qe_main.c b/examples/qencoder/qe_main.c index eb23b5b3cd9..be9a5a26a34 100644 --- a/examples/qencoder/qe_main.c +++ b/examples/qencoder/qe_main.c @@ -103,6 +103,7 @@ static void qe_help(void) printf(" [-n samples] Number of samples\n"); printf(" [-t msec] Delay between samples (msec)\n"); printf(" [-r] Reset the position to zero\n"); + printf(" [-i] Use the QEIOC_GETINDEX call to obtain samples\n"); printf(" [-h] Shows this message and exits\n\n"); } @@ -152,9 +153,10 @@ static void parse_args(int argc, FAR char **argv) int index; int nargs; - g_qeexample.reset = false; - g_qeexample.nloops = CONFIG_EXAMPLES_QENCODER_NSAMPLES; - g_qeexample.delay = CONFIG_EXAMPLES_QENCODER_DELAY; + g_qeexample.reset = false; + g_qeexample.use_qeindex = false; + g_qeexample.nloops = CONFIG_EXAMPLES_QENCODER_NSAMPLES; + g_qeexample.delay = CONFIG_EXAMPLES_QENCODER_DELAY; for (index = 1; index < argc; ) { @@ -202,6 +204,11 @@ static void parse_args(int argc, FAR char **argv) index++; break; + case 'i': + g_qeexample.use_qeindex = true; + index++; + break; + case 'h': qe_help(); exit(EXIT_SUCCESS); @@ -229,6 +236,7 @@ int main(int argc, FAR char *argv[]) int exitval = EXIT_SUCCESS; int ret; int nloops; + struct qe_index_s index; /* Set the default values */ @@ -295,19 +303,42 @@ int main(int argc, FAR char *argv[]) /* Get the positions data using the ioctl */ - ret = ioctl(fd, QEIOC_POSITION, (unsigned long)((uintptr_t)&position)); - if (ret < 0) + if (g_qeexample.use_qeindex) { - printf("qe_main: ioctl(QEIOC_POSITION) failed: %d\n", errno); - exitval = EXIT_FAILURE; - goto errout_with_dev; + ret = ioctl(fd, QEIOC_GETINDEX, (unsigned long)((uintptr_t)&index)); + if (ret < 0) + { + printf("qe_main: ioctl(QEIOC_GETINDEX) failed: %d\n", errno); + printf("Your MCU probably does not support this ioctl call.\n"); + printf("Consider using this example without the -i option.\n"); + exitval = EXIT_FAILURE; + goto errout_with_dev; + } + + /* GETINDEX succesful */ + + else + { + printf("qe_main: %3d. pos: %" PRIi32 ", last index: %" PRIi32 ", hit indexes: %" \ + PRIi16 "\n", nloops + 1, index.qenc_pos, index.indx_pos, index.indx_cnt); + } } - - /* Print the sample data on successful return */ - else { - printf("qe_main: %3d. %" PRIi32 "\n", nloops + 1, position); + ret = ioctl(fd, QEIOC_POSITION, (unsigned long)((uintptr_t)&position)); + if (ret < 0) + { + printf("qe_main: ioctl(QEIOC_POSITION) failed: %d\n", errno); + exitval = EXIT_FAILURE; + goto errout_with_dev; + } + + /* Print the sample data on successful return */ + + else + { + printf("qe_main: %3d. %" PRIi32 "\n", nloops + 1, position); + } } /* Delay a little bit */