Skip to content

Commit

Permalink
examples/uart_bridge: Add an example for bridging data
Browse files Browse the repository at this point in the history
Add an example for bridging data between UART0 and UART2.
  • Loading branch information
SPRESENSE committed Nov 27, 2024
1 parent 8d1921a commit 59996a7
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 0 deletions.
12 changes: 12 additions & 0 deletions examples/uart_bridge/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/Make.dep
/.depend
/.built
/*.asm
/*.obj
/*.rel
/*.lst
/*.sym
/*.adb
/*.lib
/*.src
/*.spk
25 changes: 25 additions & 0 deletions examples/uart_bridge/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

config EXAMPLES_UART_BRIDGE
tristate "UART Bridge Example"
default n
---help---
Enable the uart_bridge app. This app is bridging data between UART0 and UART2.

if EXAMPLES_UART_BRIDGE

config EXAMPLES_UART_BRIDGE_PROGNAME
string "Program name"
default "uart_bridge"
---help---
This is the name of the program that will be use when the NSH ELF
program is installed.

config EXAMPLES_UART_BRIDGE_PRIORITY
int "uart_bridge task priority"
default 100

config EXAMPLES_UART_BRIDGE_STACKSIZE
int "uart_bridge stack size"
default 2048

endif
4 changes: 4 additions & 0 deletions examples/uart_bridge/Make.defs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

ifneq ($(CONFIG_EXAMPLES_UART_BRIDGE),)
CONFIGURED_APPS += uart_bridge
endif
14 changes: 14 additions & 0 deletions examples/uart_bridge/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

include $(APPDIR)/Make.defs
include $(SDKDIR)/Make.defs

PROGNAME = $(CONFIG_EXAMPLES_UART_BRIDGE_PROGNAME)
PRIORITY = $(CONFIG_EXAMPLES_UART_BRIDGE_PRIORITY)
STACKSIZE = $(CONFIG_EXAMPLES_UART_BRIDGE_STACKSIZE)
MODULE = $(CONFIG_EXAMPLES_UART_BRIDGE)

ASRCS =
CSRCS =
MAINSRC = uart_bridge_main.c

include $(APPDIR)/Application.mk
125 changes: 125 additions & 0 deletions examples/uart_bridge/uart_bridge_main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/****************************************************************************
* examples/uart_bridge/uart_bridge_main.c
*
* Copyright 2024 Sony Semiconductor Solutions Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name of Sony Semiconductor Solutions Corporation nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/

/****************************************************************************
* Included Files
****************************************************************************/

/****************************************************************************
* Included Files
****************************************************************************/

#include <nuttx/config.h>
#include <stdio.h>
#include <stdlib.h>
#include <poll.h>
#include <fcntl.h>

/****************************************************************************
* Preprocessor Definitions
****************************************************************************/

#define DEV_UART0 "/dev/ttyS0"
#define DEV_UART2 "/dev/ttyS2"

#define BUFLEN (256)

/****************************************************************************
* Privete Data
****************************************************************************/

static char buf[BUFLEN];

/****************************************************************************
* Public Functions
****************************************************************************/

int main(int argc, FAR char *argv[])
{
int ret;
int uart0;
int uart2;
struct pollfd pfd[2];

uart0 = open(DEV_UART0, O_RDWR);
uart2 = open(DEV_UART2, O_RDWR);

if (uart0 < 0 || uart2 < 0)
{
printf("Could not open device files\n");
close(uart0); /* Maybe opened, try close */
close(uart2); /* Maybe opened, try close */
return -1;
}

fclose(stdin);
fclose(stdout);

while (1)
{
pfd[0].fd = uart0;
pfd[0].events = POLLIN;

pfd[1].fd = uart2;
pfd[1].events = POLLIN;

/* Wait for receiving data from both side */

poll(pfd, 2, -1);

/* Check if it receives some from uart0 */

if (pfd[0].revents & POLLIN)
{
ret = read(uart0, buf, BUFLEN);
write(uart2, buf, ret);
}

/* Check if it receives some from uart2 */

if (pfd[1].revents & POLLIN)
{
ret = read(uart2, buf, BUFLEN);
write(uart0, buf, ret);
}
}

/* Never reach here, but just in case */

close(uart0);
close(uart2);

return 0;
}

0 comments on commit 59996a7

Please sign in to comment.