Skip to content

Commit

Permalink
examples/spislv_test: Added app that allows users to test SPI Slave comm
Browse files Browse the repository at this point in the history
This application continuously reads the file system of the spislv. Each received message will be written to the user in hexadecimal form, and the same received data will be sent back. In this way, the user can test if their spislv driver and hardware setup are working properly before proceeding further.

On a master device, using the SPI tool, when sending the message: spi exch -x 4 deadbeef

The slave device will output:
Slave: 4 Bytes read
 Value in hex form from /dev/spislv2: de ad be ef
Slave: Writing value back to /dev/spislv2
  • Loading branch information
FelipeMdeO authored and cederom committed Oct 22, 2024
1 parent a7a03a9 commit f517b66
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 0 deletions.
31 changes: 31 additions & 0 deletions examples/spislv_test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# ##############################################################################
# apps/examples/spislv_test/CMakeLists.txt
#
# 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_EXAMPLES_SPISLV)
nuttx_add_application(
NAME
${CONFIG_EXAMPLES_SPISLV_PROGNAME}
SRCS
spislv_test.c
STACKSIZE
${CONFIG_EXAMPLES_SPISLV_STACKSIZE}
PRIORITY
${CONFIG_EXAMPLES_SPISLV_PRIORITY})
endif()
30 changes: 30 additions & 0 deletions examples/spislv_test/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#
# For a description of the syntax of this configuration file,
# see the file kconfig-language.txt in the NuttX tools repository.
#

config EXAMPLES_SPISLV
tristate "\"SPI Slave Test\" example"
default n
---help---
Enable the "SPI Slave Test" example.
This tool can be used together with the SPI tool to validate communication between two devices.

if EXAMPLES_SPISLV

config EXAMPLES_SPISLV_PROGNAME
string "Program name"
default "spislv"
---help---
This is the name of the program that will be used when the NSH ELF
program is installed.

config EXAMPLES_SPISLV_PRIORITY
int "Spislv task priority"
default 100

config EXAMPLES_SPISLV_STACKSIZE
int "Spislv stack size"
default DEFAULT_TASK_STACKSIZE

endif
23 changes: 23 additions & 0 deletions examples/spislv_test/Make.defs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
############################################################################
# apps/examples/spislv/Make.defs
#
# 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_EXAMPLES_SPISLV),)
CONFIGURED_APPS += $(APPDIR)/examples/spislv_test
endif
34 changes: 34 additions & 0 deletions examples/spislv_test/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
############################################################################
# apps/examples/spislv_test/Makefile
#
# 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

# SPI SLAVE TEST built-in application info

PROGNAME = $(CONFIG_EXAMPLES_SPISLV_PROGNAME)
PRIORITY = $(CONFIG_EXAMPLES_SPISLV_PRIORITY)
STACKSIZE = $(CONFIG_EXAMPLES_SPISLV_STACKSIZE)
MODULE = $(CONFIG_EXAMPLES_SPISLV)

# SPI SLAVE TEST Example

MAINSRC = spislv_test.c

include $(APPDIR)/Application.mk
102 changes: 102 additions & 0 deletions examples/spislv_test/spislv_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/****************************************************************************
* apps/examples/spislv_test/spislv_test.c
*
* 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 <nuttx/config.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>

#define SOURCE_FILE "/dev/spislv2"
#define BUFFER_SIZE 256

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

/****************************************************************************
* spislv_test
****************************************************************************/

int main(int argc, FAR char *argv[])
{
int fd;
char buffer[BUFFER_SIZE];
ssize_t bytes_read;
ssize_t i;

printf("Slave started!!\n");
fd = open(SOURCE_FILE, O_RDWR);

if (fd < 0)
{
printf("Failed to open %s: %s\n", SOURCE_FILE, strerror(errno));
return 0;
}

while (1)
{
/* Read the number from the source file */

printf("Slave: Reading from %s\n", SOURCE_FILE);
bytes_read = read(fd, buffer, BUFFER_SIZE - 1);

if (bytes_read < 0)
{
printf("Failed to read from %s: %s\n",
SOURCE_FILE, strerror(errno));
close(fd);
return 0;
}
else if (bytes_read > 0)
{
buffer[bytes_read] = '\0';

/* Print buffer in hexadecimal format */

printf("Slave: Read value in hex: ");
for (i = 0; i < bytes_read; ++i)
{
printf("%02x ", (unsigned char)buffer[i]);
}

printf("\n");

/* Write the same value back */

printf("Slave: Writing %d bytes back to %s\n",
bytes_read, SOURCE_FILE);
ssize_t bytes_written = write(fd, buffer, bytes_read);
if (bytes_written < 0)
{
printf("Failed to write to %s: %s\n",
SOURCE_FILE, strerror(errno));
close(fd);
return 0;
}
}
}
}

0 comments on commit f517b66

Please sign in to comment.