2014-04-22 16:40:48 +02:00
|
|
|
/****************************************************************************
|
2021-06-16 09:22:16 +02:00
|
|
|
* apps/examples/serialrx/serialrx_main.c
|
2014-04-22 16:40:48 +02:00
|
|
|
*
|
2022-03-08 09:55:07 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2014-04-22 16:40:48 +02:00
|
|
|
*
|
2022-03-08 09:55:07 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-04-22 16:40:48 +02:00
|
|
|
*
|
2022-03-08 09:55:07 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2014-04-22 16:40:48 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2015-11-24 20:48:44 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdbool.h>
|
2014-04-22 16:40:48 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2023-02-04 14:56:06 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2019-10-11 15:23:39 +02:00
|
|
|
#include <nuttx/arch.h>
|
2014-04-22 16:40:48 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-10-30 23:33:40 +01:00
|
|
|
* serialrx_main
|
2014-04-22 16:40:48 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2014-09-06 17:23:23 +02:00
|
|
|
int main(int argc, FAR char *argv[])
|
2014-04-22 16:40:48 +02:00
|
|
|
{
|
2015-11-24 20:48:44 +01:00
|
|
|
#ifdef CONFIG_EXAMPLES_SERIALRX_BUFFERED
|
2014-04-22 16:40:48 +02:00
|
|
|
FAR FILE *f;
|
2019-10-11 15:23:39 +02:00
|
|
|
int fd;
|
|
|
|
int nbytes;
|
|
|
|
int cnt;
|
2015-11-24 20:48:44 +01:00
|
|
|
#else
|
|
|
|
int fd;
|
2019-10-11 15:23:39 +02:00
|
|
|
int cnt;
|
|
|
|
int bytecount = 0;
|
2015-11-24 20:48:44 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_EXAMPLES_SERIALRX_PRINTHYPHEN
|
|
|
|
int count = 0;
|
|
|
|
#endif
|
2022-03-07 17:10:51 +01:00
|
|
|
#if defined (CONFIG_EXAMPLES_SERIALRX_PRINTHEX) || \
|
|
|
|
defined (CONFIG_EXAMPLES_SERIALRX_PRINTSTR)
|
2015-11-24 20:48:44 +01:00
|
|
|
int i;
|
|
|
|
#endif
|
|
|
|
bool eof = false;
|
|
|
|
FAR char *buf;
|
|
|
|
FAR char *devpath;
|
|
|
|
|
|
|
|
if (argc == 1)
|
|
|
|
{
|
|
|
|
devpath = CONFIG_EXAMPLES_SERIALRX_DEVPATH;
|
|
|
|
}
|
|
|
|
else if (argc == 2)
|
|
|
|
{
|
|
|
|
devpath = argv[1];
|
|
|
|
}
|
2019-10-11 15:23:39 +02:00
|
|
|
else if (argc == 3)
|
|
|
|
{
|
|
|
|
devpath = argv[1];
|
|
|
|
bytecount = strtol(argv[2], NULL, 10);
|
|
|
|
}
|
2015-11-24 20:48:44 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Usage: %s [devpath]\n", argv[0]);
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_EXAMPLES_SERIALRX_PRINTSTR
|
|
|
|
buf = (FAR char *)malloc(CONFIG_EXAMPLES_SERIALRX_BUFSIZE + 1);
|
|
|
|
#else
|
|
|
|
buf = (FAR char *)malloc(CONFIG_EXAMPLES_SERIALRX_BUFSIZE);
|
|
|
|
#endif
|
2019-10-11 15:23:39 +02:00
|
|
|
|
2015-11-24 20:48:44 +01:00
|
|
|
if (buf == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: malloc failed: %d\n", errno);
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_EXAMPLES_SERIALRX_BUFFERED
|
|
|
|
f = fopen(devpath, "r");
|
|
|
|
if (f == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: fopen failed: %d\n", errno);
|
|
|
|
goto errout_with_buf;
|
|
|
|
}
|
|
|
|
#else
|
2019-10-11 15:23:39 +02:00
|
|
|
fd = open(devpath, O_RDWR);
|
2015-11-24 20:48:44 +01:00
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: open failed: %d\n", errno);
|
|
|
|
goto errout_with_buf;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
printf("Reading from %s\n", devpath);
|
|
|
|
fflush(stdout);
|
2019-10-11 15:23:39 +02:00
|
|
|
cnt = 0;
|
|
|
|
while (cnt < bytecount)
|
2014-04-22 16:40:48 +02:00
|
|
|
{
|
2015-11-24 20:48:44 +01:00
|
|
|
#ifdef CONFIG_EXAMPLES_SERIALRX_BUFFERED
|
2019-10-11 15:23:39 +02:00
|
|
|
size_t n = fread(buf, 1, 26, f);
|
|
|
|
cnt++;
|
2015-11-25 16:29:48 +01:00
|
|
|
if (feof(f))
|
2015-11-24 20:48:44 +01:00
|
|
|
{
|
2015-11-25 16:29:48 +01:00
|
|
|
eof = true;
|
|
|
|
}
|
|
|
|
else if (ferror(f))
|
|
|
|
{
|
|
|
|
printf("fread failed: %d\n", errno);
|
|
|
|
fflush(stdout);
|
|
|
|
clearerr(f);
|
2015-11-24 20:48:44 +01:00
|
|
|
}
|
2014-04-22 16:40:48 +02:00
|
|
|
#else
|
2015-11-24 20:48:44 +01:00
|
|
|
ssize_t n = read(fd, buf, CONFIG_EXAMPLES_SERIALRX_BUFSIZE);
|
2019-10-11 15:23:39 +02:00
|
|
|
up_udelay(1000);
|
2015-11-24 20:48:44 +01:00
|
|
|
if (n == 0)
|
|
|
|
{
|
|
|
|
eof = true;
|
|
|
|
}
|
|
|
|
else if (n < 0)
|
|
|
|
{
|
|
|
|
printf("read failed: %d\n", errno);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
2014-04-22 16:40:48 +02:00
|
|
|
#endif
|
2015-11-24 20:48:44 +01:00
|
|
|
else
|
2014-04-22 16:40:48 +02:00
|
|
|
{
|
2015-11-24 20:48:44 +01:00
|
|
|
#if defined(CONFIG_EXAMPLES_SERIALRX_PRINTHYPHEN)
|
|
|
|
count += (int)n;
|
|
|
|
if (count >= CONFIG_EXAMPLES_SERIALRX_BUFSIZE)
|
|
|
|
{
|
|
|
|
printf("-");
|
|
|
|
fflush(stdout);
|
|
|
|
count -= CONFIG_EXAMPLES_SERIALRX_BUFSIZE;
|
|
|
|
}
|
|
|
|
#elif defined(CONFIG_EXAMPLES_SERIALRX_PRINTHEX)
|
|
|
|
for (i = 0; i < (int)n; i++)
|
|
|
|
{
|
2020-11-13 06:46:17 +01:00
|
|
|
printf("0x%02x ", buf[i]);
|
2015-11-24 20:48:44 +01:00
|
|
|
}
|
2019-10-11 15:23:39 +02:00
|
|
|
|
2015-11-24 20:48:44 +01:00
|
|
|
fflush(stdout);
|
2022-03-07 17:10:51 +01:00
|
|
|
#elif defined(CONFIG_EXAMPLES_SERIALRX_PRINTSTR)
|
|
|
|
for (i = 0; i < (int)n; i++)
|
|
|
|
{
|
|
|
|
printf("%c", buf[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
fflush(stdout);
|
|
|
|
#endif
|
2019-10-11 15:23:39 +02:00
|
|
|
cnt += n;
|
2014-04-22 16:40:48 +02:00
|
|
|
}
|
|
|
|
|
2019-10-11 15:23:39 +02:00
|
|
|
UNUSED(eof);
|
|
|
|
}
|
2020-11-13 06:44:51 +01:00
|
|
|
|
2015-11-24 20:48:44 +01:00
|
|
|
#ifdef CONFIG_EXAMPLES_SERIALRX_PRINTHYPHEN
|
|
|
|
printf("\n");
|
|
|
|
#endif
|
|
|
|
printf("EOF reached\n");
|
2019-10-11 15:23:39 +02:00
|
|
|
printf("Total bytes received = %d\n", cnt); /* Kamal */
|
2015-11-24 20:48:44 +01:00
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
#ifdef CONFIG_EXAMPLES_SERIALRX_BUFFERED
|
|
|
|
fclose(f);
|
|
|
|
#else
|
2019-10-11 15:23:39 +02:00
|
|
|
up_udelay(1000000);
|
2015-11-24 20:48:44 +01:00
|
|
|
close(fd);
|
|
|
|
#endif
|
|
|
|
|
2014-04-22 16:40:48 +02:00
|
|
|
free(buf);
|
2015-11-24 20:48:44 +01:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
|
|
|
errout_with_buf:
|
|
|
|
free(buf);
|
|
|
|
|
|
|
|
errout:
|
|
|
|
fflush(stderr);
|
|
|
|
return EXIT_FAILURE;
|
2014-04-22 16:40:48 +02:00
|
|
|
}
|