Skip to content

Commit

Permalink
examples/qencoder: obtain samples using the QEIOC_GETINDEX ioctl call
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
zdebanos authored and xiaoxiang781216 committed Jul 29, 2024
1 parent 366d8db commit 1a7da54
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
1 change: 1 addition & 0 deletions examples/qencoder/qe.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
};
Expand Down
55 changes: 43 additions & 12 deletions examples/qencoder/qe_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down Expand Up @@ -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; )
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 */

Expand Down Expand Up @@ -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 */
Expand Down

0 comments on commit 1a7da54

Please sign in to comment.