Skip to content

Commit f517b66

Browse files
FelipeMdeOcederom
authored andcommitted
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
1 parent a7a03a9 commit f517b66

File tree

5 files changed

+220
-0
lines changed

5 files changed

+220
-0
lines changed

examples/spislv_test/CMakeLists.txt

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# ##############################################################################
2+
# apps/examples/spislv_test/CMakeLists.txt
3+
#
4+
# Licensed to the Apache Software Foundation (ASF) under one or more contributor
5+
# license agreements. See the NOTICE file distributed with this work for
6+
# additional information regarding copyright ownership. The ASF licenses this
7+
# file to you under the Apache License, Version 2.0 (the "License"); you may not
8+
# use this file except in compliance with the License. You may obtain a copy of
9+
# the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
# License for the specific language governing permissions and limitations under
17+
# the License.
18+
#
19+
# ##############################################################################
20+
21+
if(CONFIG_EXAMPLES_SPISLV)
22+
nuttx_add_application(
23+
NAME
24+
${CONFIG_EXAMPLES_SPISLV_PROGNAME}
25+
SRCS
26+
spislv_test.c
27+
STACKSIZE
28+
${CONFIG_EXAMPLES_SPISLV_STACKSIZE}
29+
PRIORITY
30+
${CONFIG_EXAMPLES_SPISLV_PRIORITY})
31+
endif()

examples/spislv_test/Kconfig

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#
2+
# For a description of the syntax of this configuration file,
3+
# see the file kconfig-language.txt in the NuttX tools repository.
4+
#
5+
6+
config EXAMPLES_SPISLV
7+
tristate "\"SPI Slave Test\" example"
8+
default n
9+
---help---
10+
Enable the "SPI Slave Test" example.
11+
This tool can be used together with the SPI tool to validate communication between two devices.
12+
13+
if EXAMPLES_SPISLV
14+
15+
config EXAMPLES_SPISLV_PROGNAME
16+
string "Program name"
17+
default "spislv"
18+
---help---
19+
This is the name of the program that will be used when the NSH ELF
20+
program is installed.
21+
22+
config EXAMPLES_SPISLV_PRIORITY
23+
int "Spislv task priority"
24+
default 100
25+
26+
config EXAMPLES_SPISLV_STACKSIZE
27+
int "Spislv stack size"
28+
default DEFAULT_TASK_STACKSIZE
29+
30+
endif

examples/spislv_test/Make.defs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
############################################################################
2+
# apps/examples/spislv/Make.defs
3+
#
4+
# Licensed to the Apache Software Foundation (ASF) under one or more
5+
# contributor license agreements. See the NOTICE file distributed with
6+
# this work for additional information regarding copyright ownership. The
7+
# ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance with the
9+
# License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
# License for the specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
############################################################################
20+
21+
ifneq ($(CONFIG_EXAMPLES_SPISLV),)
22+
CONFIGURED_APPS += $(APPDIR)/examples/spislv_test
23+
endif

examples/spislv_test/Makefile

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
############################################################################
2+
# apps/examples/spislv_test/Makefile
3+
#
4+
# Licensed to the Apache Software Foundation (ASF) under one or more
5+
# contributor license agreements. See the NOTICE file distributed with
6+
# this work for additional information regarding copyright ownership. The
7+
# ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance with the
9+
# License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
# License for the specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
############################################################################
20+
21+
include $(APPDIR)/Make.defs
22+
23+
# SPI SLAVE TEST built-in application info
24+
25+
PROGNAME = $(CONFIG_EXAMPLES_SPISLV_PROGNAME)
26+
PRIORITY = $(CONFIG_EXAMPLES_SPISLV_PRIORITY)
27+
STACKSIZE = $(CONFIG_EXAMPLES_SPISLV_STACKSIZE)
28+
MODULE = $(CONFIG_EXAMPLES_SPISLV)
29+
30+
# SPI SLAVE TEST Example
31+
32+
MAINSRC = spislv_test.c
33+
34+
include $(APPDIR)/Application.mk

examples/spislv_test/spislv_test.c

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/****************************************************************************
2+
* apps/examples/spislv_test/spislv_test.c
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one or more
5+
* contributor license agreements. See the NOTICE file distributed with
6+
* this work for additional information regarding copyright ownership. The
7+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance with the
9+
* License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
* License for the specific language governing permissions and limitations
17+
* under the License.
18+
*
19+
****************************************************************************/
20+
21+
/****************************************************************************
22+
* Included Files
23+
****************************************************************************/
24+
25+
#include <nuttx/config.h>
26+
#include <stdio.h>
27+
#include <fcntl.h>
28+
#include <unistd.h>
29+
#include <errno.h>
30+
#include <string.h>
31+
#include <stdlib.h>
32+
33+
#define SOURCE_FILE "/dev/spislv2"
34+
#define BUFFER_SIZE 256
35+
36+
/****************************************************************************
37+
* Public Functions
38+
****************************************************************************/
39+
40+
/****************************************************************************
41+
* spislv_test
42+
****************************************************************************/
43+
44+
int main(int argc, FAR char *argv[])
45+
{
46+
int fd;
47+
char buffer[BUFFER_SIZE];
48+
ssize_t bytes_read;
49+
ssize_t i;
50+
51+
printf("Slave started!!\n");
52+
fd = open(SOURCE_FILE, O_RDWR);
53+
54+
if (fd < 0)
55+
{
56+
printf("Failed to open %s: %s\n", SOURCE_FILE, strerror(errno));
57+
return 0;
58+
}
59+
60+
while (1)
61+
{
62+
/* Read the number from the source file */
63+
64+
printf("Slave: Reading from %s\n", SOURCE_FILE);
65+
bytes_read = read(fd, buffer, BUFFER_SIZE - 1);
66+
67+
if (bytes_read < 0)
68+
{
69+
printf("Failed to read from %s: %s\n",
70+
SOURCE_FILE, strerror(errno));
71+
close(fd);
72+
return 0;
73+
}
74+
else if (bytes_read > 0)
75+
{
76+
buffer[bytes_read] = '\0';
77+
78+
/* Print buffer in hexadecimal format */
79+
80+
printf("Slave: Read value in hex: ");
81+
for (i = 0; i < bytes_read; ++i)
82+
{
83+
printf("%02x ", (unsigned char)buffer[i]);
84+
}
85+
86+
printf("\n");
87+
88+
/* Write the same value back */
89+
90+
printf("Slave: Writing %d bytes back to %s\n",
91+
bytes_read, SOURCE_FILE);
92+
ssize_t bytes_written = write(fd, buffer, bytes_read);
93+
if (bytes_written < 0)
94+
{
95+
printf("Failed to write to %s: %s\n",
96+
SOURCE_FILE, strerror(errno));
97+
close(fd);
98+
return 0;
99+
}
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)