Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add to nuttx apps a simple app to test spi slave driver. #2765

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
}
}
Loading