testing: Add Serial Error Reporting testing app

This commit adds support to Serial Error Reporting using the ioctl
TIOCGICOUNT.

This examples was inspired on this sample code:
https://stackoverflow.com/questions/78796301/c-serial-communication-why-does-read-lose-some-data-bytes-at-high-baud-rates/78800245#78800245

Signed-off-by: Alan C. Assis <acassis@gmail.com>
This commit is contained in:
Alan Carvalho de Assis 2025-02-06 20:09:21 -03:00 committed by Xiang Xiao
parent fc142045a4
commit cdc5968c67
5 changed files with 230 additions and 0 deletions

View file

@ -0,0 +1,33 @@
# ##############################################################################
# apps/testing/drivers/ser/CMakeLists.txt
#
# SPDX-License-Identifier: Apache-2.0
#
# 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
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# 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.
#
# ##############################################################################
if(CONFIG_TESTING_SERIAL)
nuttx_add_application(
NAME
${CONFIG_TESTING_SERIAL_PROGNAME}
PRIORITY
${CONFIG_TESTING_SERIAL_PRIORITY}
STACKSIZE
${CONFIG_TESTING_SERIAL_STACKSIZE}
SRCS
ser.c)
endif()

View file

@ -0,0 +1,38 @@
#
# For a description of the syntax of this configuration file,
# see the file kconfig-language.txt in the NuttX tools repository.
#
config TESTING_SERIAL
bool "Serial Error Report (ser)"
default n
depends on SERIAL_TIOCGICOUNT
---help---
Enable the serial error reporting test. This test
could report U[S]ART erros like frame, overrun, parity, etc
if TESTING_SERIAL
config TESTING_SERIAL_PORT
string "Serial Port to Use"
default "/dev/ttyS1"
---help---
This is the name of serial port to use. Please avoid using
the ttyS0 because it is used as console.
config TESTING_SERIAL_PROGNAME
string "Program name"
default "ser"
---help---
This is the name of the program that will be used when the NSH ELF
program is installed.
config TESTING_SERIAL_PRIORITY
int "ser utility task priority"
default 100
config TESTING_SERIAL_STACKSIZE
int "ser utility stack size"
default DEFAULT_TASK_STACKSIZE
endif

View file

@ -0,0 +1,25 @@
############################################################################
# apps/testing/drivers/ser/Make.defs
#
# SPDX-License-Identifier: Apache-2.0
#
# 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
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# 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.
#
############################################################################
ifneq ($(CONFIG_TESTING_SERIAL),)
CONFIGURED_APPS += $(APPDIR)/testing/drivers/ser
endif

View file

@ -0,0 +1,35 @@
############################################################################
# apps/testing/drivers/ser/Makefile
#
# SPDX-License-Identifier: Apache-2.0
#
# 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
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# 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.
#
############################################################################
include $(APPDIR)/Make.defs
# ser built-in application info
PROGNAME = $(CONFIG_TESTING_SERIAL_PROGNAME)
PRIORITY = $(CONFIG_TESTING_SERIAL_PRIORITY)
STACKSIZE = $(CONFIG_TESTING_SERIAL_STACKSIZE)
# ser main source
MAINSRC = ser.c
include $(APPDIR)/Application.mk

99
testing/drivers/ser/ser.c Normal file
View file

@ -0,0 +1,99 @@
/****************************************************************************
* apps/testing/drivers/ser/ser.c
*
* SPDX-License-Identifier: Apache-2.0
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <fcntl.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <nuttx/serial/serial.h>
/****************************************************************************
* Public Functions
****************************************************************************/
int main(int argc, FAR char *argv[])
{
struct serial_icounter_s rcount;
struct serial_icounter_s scount;
int fd;
int ret;
fd = open(CONFIG_TESTING_SERIAL_PORT, O_RDONLY);
if (fd < 0)
{
ret = -ENODEV;
goto out;
}
ret = ioctl(fd, TIOCGICOUNT, &scount);
if (ret < 0)
{
goto out_close;
}
while (1)
{
ret = ioctl(fd, TIOCGICOUNT, &rcount);
if (rcount.frame > scount.frame)
{
printf("ERROR: framing %d\n", rcount.frame - scount.frame);
}
if (rcount.overrun > scount.overrun)
{
printf("ERROR: overrun %d\n", rcount.overrun - scount.overrun);
}
if (rcount.parity > scount.parity)
{
printf("ERROR: parity %d\n", rcount.parity - scount.parity);
}
if (rcount.brk > scount.brk)
{
printf("ERROR: break %d\n", rcount.brk - scount.brk);
}
if (rcount.buf_overrun > scount.buf_overrun)
{
printf("ERROR: buffer overrun %d\n",
rcount.buf_overrun - scount.buf_overrun);
}
scount = rcount;
usleep(1000000);
}
out_close:
close(fd);
out:
return ret;
}