Skip to content

Commit 02c1608

Browse files
QiuPeiyangAnas Nashif
authored andcommitted
tests: add zephyr SPI driver api test case
This commit verifies the below SPI driver apis: spi_configure() spi_slave_select() spi_write() spi_transceive() Verify SPI work in SPI_MODE_CPOL, SPI_MODE_CPHA, and SPI_MODE_CPOL | SPI_MODE_CPHA. Jira: ZEP-1626 Change-Id: I1985ac6ff8269ac908817644a844720f2d7a125c Signed-off-by: Qiu Peiyang <[email protected]> Signed-off-by: Sergio Rodriguez <[email protected]> (cherry picked from commit 1124da1)
1 parent c7d05f4 commit 02c1608

File tree

10 files changed

+167
-119
lines changed

10 files changed

+167
-119
lines changed
File renamed without changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CONFIG_SPI=y
2+
CONFIG_ZTEST=y
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
include $(ZEPHYR_BASE)/tests/Makefile.test
2+
3+
obj-y += main.o test_spi.o
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (c) 2016 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/**
8+
* @addtogroup t_driver_spi
9+
* @{
10+
* @defgroup t_spi_basic test_spi_basic_operations
11+
* @}
12+
*
13+
* Setup: Connect DUT MISO to DUT MOSI
14+
*
15+
* quark_se_c1000_devboard - x86
16+
* ----------------------
17+
*
18+
* SPI_0_MISO - SPI_0_MOSI
19+
* SPI_1_MISO - SPI_1_MOSI
20+
*
21+
* quark_se_c1000_ss_devboard - arc
22+
* ----------------------
23+
*
24+
* SPI_SS_0_MISO - SPI_SS_0_MOSI
25+
* SPI_SS_1_MISO - SPI_SS_1_MOSI
26+
*
27+
* quark_d2000 - x86
28+
* ----------------------
29+
*
30+
* SPI_M_MISO - SPI_M_MOSI
31+
*
32+
* arduino_101 - x86
33+
* ----------------------
34+
*
35+
* SPI1_M_MISO - SPI1_M_MOSI
36+
*/
37+
38+
#include <zephyr.h>
39+
#include <ztest.h>
40+
41+
void test_spi_cpol(void);
42+
void test_spi_cpha(void);
43+
void test_spi_cpol_cpha(void);
44+
45+
void test_main(void)
46+
{
47+
ztest_test_suite(spi_test,
48+
ztest_unit_test(test_spi_cpol),
49+
ztest_unit_test(test_spi_cpha),
50+
ztest_unit_test(test_spi_cpol_cpha));
51+
ztest_run_test_suite(spi_test);
52+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright (c) 2016 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/*
8+
* @addtogroup t_spi_basic
9+
* @{
10+
* @defgroup t_spi_basic_operations test_spi_basic_operation_mode
11+
* @brief TestPurpose: verify SPI basic operations in different mode
12+
* @}
13+
*/
14+
15+
#include <spi.h>
16+
#include <zephyr.h>
17+
#include <ztest.h>
18+
19+
#ifdef CONFIG_ARC
20+
#define SPI_DEV_NAME CONFIG_SPI_SS_0_NAME
21+
#else
22+
#ifdef CONFIG_BOARD_ARDUINO_101
23+
#define SPI_DEV_NAME CONFIG_SPI_1_NAME
24+
#else
25+
#define SPI_DEV_NAME CONFIG_SPI_0_NAME
26+
#endif
27+
#endif
28+
29+
#define SPI_SLAVE 1
30+
#define SPI_MAX_CLK_FREQ_250KHZ 128
31+
32+
static struct spi_config spi_conf = {
33+
.config = SPI_MODE_CPOL | SPI_MODE_CPHA | SPI_WORD(8),
34+
.max_sys_freq = SPI_MAX_CLK_FREQ_250KHZ,
35+
};
36+
37+
static unsigned char wbuf[16] = "Hello";
38+
static unsigned char rbuf[16] = {};
39+
40+
static int test_spi(uint32_t mode)
41+
{
42+
struct device *spi_dev = device_get_binding(SPI_DEV_NAME);
43+
uint32_t len = strlen(wbuf);
44+
45+
if (!spi_dev) {
46+
TC_PRINT("Cannot get SPI device\n");
47+
return TC_FAIL;
48+
}
49+
50+
spi_conf.config = mode | SPI_MODE_LOOP;
51+
/* 1. verify spi_configure() */
52+
if (spi_configure(spi_dev, &spi_conf)) {
53+
TC_PRINT("SPI config failed\n");
54+
return TC_FAIL;
55+
}
56+
57+
/* 2. verify spi_slave_select() */
58+
if (spi_slave_select(spi_dev, SPI_SLAVE)) {
59+
TC_PRINT("SPI slave select failed\n");
60+
return TC_FAIL;
61+
}
62+
63+
/* 3. verify spi_write() */
64+
if (spi_write(spi_dev, (uint8_t *) wbuf, 6) != 0) {
65+
TC_PRINT("SPI write failed\n");
66+
return TC_FAIL;
67+
}
68+
69+
strcpy((char *)wbuf, "So what then?");
70+
len = strlen(wbuf);
71+
72+
/* 4. verify spi_transceive() */
73+
TC_PRINT("SPI sent: %s\n", wbuf);
74+
if (spi_transceive(spi_dev, wbuf, len + 1, rbuf, len + 1) != 0) {
75+
TC_PRINT("SPI transceive failed\n");
76+
return TC_FAIL;
77+
}
78+
79+
TC_PRINT("SPI transceived: %s\n", rbuf);
80+
81+
if (!strcmp(wbuf, rbuf)) {
82+
return TC_PASS;
83+
} else {
84+
return TC_FAIL;
85+
}
86+
}
87+
88+
void test_spi_cpol(void)
89+
{
90+
TC_PRINT("Test SPI_MODE_CPOL\n");
91+
assert_true(test_spi(SPI_WORD(8) | SPI_MODE_CPOL) == TC_PASS, NULL);
92+
}
93+
94+
void test_spi_cpha(void)
95+
{
96+
TC_PRINT("Test SPI_MODE_CPHA\n");
97+
assert_true(test_spi(SPI_WORD(8) | SPI_MODE_CPHA) == TC_PASS, NULL);
98+
}
99+
100+
void test_spi_cpol_cpha(void)
101+
{
102+
TC_PRINT("Test SPI_MODE_CPOL | SPI_MODE_CPHA\n");
103+
assert_true(test_spi(SPI_WORD(8) | SPI_MODE_CPOL | SPI_MODE_CPHA)
104+
== TC_PASS, NULL);
105+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[test_spi]
2+
tags = apps
3+
arch_whitelist = x86 arc arm
4+
platform_whitelist = quark_se_c1000_devboard quark_d2000_crb arduino_101 galileo frdm_k64f
5+
platform_exclude = arduino_101_sss

tests/drivers/spi_test/prj.conf

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/drivers/spi_test/src/Makefile

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/drivers/spi_test/src/spi.c

Lines changed: 0 additions & 110 deletions
This file was deleted.

tests/drivers/spi_test/testcase.ini

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)