From f517b66d607ba185622bc6562778da6660ad82e3 Mon Sep 17 00:00:00 2001 From: Felipe Moura Date: Mon, 21 Oct 2024 22:51:30 -0300 Subject: [PATCH] examples/spislv_test: Added app that allows users to test SPI Slave comm 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 --- examples/spislv_test/CMakeLists.txt | 31 +++++++++ examples/spislv_test/Kconfig | 30 ++++++++ examples/spislv_test/Make.defs | 23 +++++++ examples/spislv_test/Makefile | 34 ++++++++++ examples/spislv_test/spislv_test.c | 102 ++++++++++++++++++++++++++++ 5 files changed, 220 insertions(+) create mode 100644 examples/spislv_test/CMakeLists.txt create mode 100644 examples/spislv_test/Kconfig create mode 100644 examples/spislv_test/Make.defs create mode 100644 examples/spislv_test/Makefile create mode 100644 examples/spislv_test/spislv_test.c diff --git a/examples/spislv_test/CMakeLists.txt b/examples/spislv_test/CMakeLists.txt new file mode 100644 index 0000000000..d4406bae99 --- /dev/null +++ b/examples/spislv_test/CMakeLists.txt @@ -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() diff --git a/examples/spislv_test/Kconfig b/examples/spislv_test/Kconfig new file mode 100644 index 0000000000..ab273e0aa0 --- /dev/null +++ b/examples/spislv_test/Kconfig @@ -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 diff --git a/examples/spislv_test/Make.defs b/examples/spislv_test/Make.defs new file mode 100644 index 0000000000..e8b23330ea --- /dev/null +++ b/examples/spislv_test/Make.defs @@ -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 diff --git a/examples/spislv_test/Makefile b/examples/spislv_test/Makefile new file mode 100644 index 0000000000..44b099d1d0 --- /dev/null +++ b/examples/spislv_test/Makefile @@ -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 diff --git a/examples/spislv_test/spislv_test.c b/examples/spislv_test/spislv_test.c new file mode 100644 index 0000000000..1ac4e5b334 --- /dev/null +++ b/examples/spislv_test/spislv_test.c @@ -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 +#include +#include +#include +#include +#include +#include + +#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; + } + } + } +}