4
4
* SPDX-License-Identifier: Apache-2.0
5
5
*/
6
6
7
- #define LOG_LEVEL CONFIG_LOG_DEFAULT_LEVEL
8
- #include <zephyr/logging/log.h>
9
- LOG_MODULE_REGISTER (spi_loopback );
7
+ /* To run this loopback test, connect MOSI pin to the MISO of the SPI */
10
8
11
- #include <zephyr/kernel.h>
12
- #include <zephyr/sys/printk.h>
13
- #include <string.h>
14
- #include <stdio.h>
15
- #include <assert.h>
16
- #include <zephyr/ztest.h>
9
+ /*
10
+ ************************
11
+ * Include dependencies *
12
+ ************************
13
+ */
17
14
15
+ #include <zephyr/ztest.h>
18
16
#include <zephyr/drivers/spi.h>
17
+ #include <zephyr/kernel.h>
18
+ #include <stdio.h>
19
+ #include <zephyr/logging/log.h>
19
20
20
- #define SPI_FAST_DEV DT_COMPAT_GET_ANY_STATUS_OKAY(test_spi_loopback_fast)
21
- #define SPI_SLOW_DEV DT_COMPAT_GET_ANY_STATUS_OKAY(test_spi_loopback_slow)
22
-
23
- #if CONFIG_SPI_LOOPBACK_MODE_LOOP
24
- #define MODE_LOOP SPI_MODE_LOOP
25
- #else
26
- #define MODE_LOOP 0
27
- #endif
28
-
29
- #ifdef CONFIG_SPI_LOOPBACK_16BITS_FRAMES
30
- #define FRAME_SIZE (16)
31
- #define FRAME_SIZE_STR ", frame size = 16"
32
- #else
33
- #define FRAME_SIZE (8)
34
- #define FRAME_SIZE_STR ", frame size = 8"
35
- #endif /* CONFIG_SPI_LOOPBACK_16BITS_FRAMES */
36
-
37
- #ifdef CONFIG_DMA
38
-
39
- #ifdef CONFIG_NOCACHE_MEMORY
40
- #define DMA_ENABLED_STR ", DMA enabled"
41
- #else /* CONFIG_NOCACHE_MEMORY */
42
- #define DMA_ENABLED_STR ", DMA enabled (without CONFIG_NOCACHE_MEMORY)"
43
- #endif
21
+ LOG_MODULE_REGISTER (spi_loopback );
44
22
45
- #else /* CONFIG_DMA */
23
+ /*
24
+ **********************
25
+ * SPI configurations *
26
+ **********************
27
+ */
46
28
47
- #define DMA_ENABLED_STR
48
- #endif /* CONFIG_DMA */
29
+ #define FRAME_SIZE COND_CODE_1(CONFIG_SPI_LOOPBACK_16BITS_FRAMES, (16), (8))
30
+ #define MODE_LOOP COND_CODE_1(CONFIG_SPI_LOOPBACK_MODE_LOOP, (SPI_MODE_LOOP), (0))
49
31
50
- #define SPI_OP (frame_size ) SPI_OP_MODE_MASTER | SPI_MODE_CPOL | MODE_LOOP | \
51
- SPI_MODE_CPHA | SPI_WORD_SET(frame_size) | SPI_LINES_SINGLE
32
+ #define SPI_OP (frame_size ) \
33
+ SPI_OP_MODE_MASTER | SPI_MODE_CPOL | MODE_LOOP | SPI_MODE_CPHA | \
34
+ SPI_WORD_SET(frame_size) | SPI_LINES_SINGLE
52
35
36
+ #define SPI_FAST_DEV DT_COMPAT_GET_ANY_STATUS_OKAY(test_spi_loopback_fast)
53
37
static struct spi_dt_spec spi_fast = SPI_DT_SPEC_GET (SPI_FAST_DEV , SPI_OP (FRAME_SIZE ), 0 );
54
- static struct spi_dt_spec spi_slow = SPI_DT_SPEC_GET (SPI_SLOW_DEV , SPI_OP (FRAME_SIZE ), 0 );
55
38
56
- /* to run this test, connect MOSI pin to the MISO of the SPI */
57
-
58
- #define STACK_SIZE (512 + CONFIG_TEST_EXTRA_STACK_SIZE)
59
- #define BUF_SIZE 18
60
- #define BUF2_SIZE 36
61
- #define BUF3_SIZE CONFIG_SPI_LARGE_BUFFER_SIZE
39
+ #define SPI_SLOW_DEV DT_COMPAT_GET_ANY_STATUS_OKAY(test_spi_loopback_slow)
40
+ static struct spi_dt_spec spi_slow = SPI_DT_SPEC_GET (SPI_SLOW_DEV , SPI_OP (FRAME_SIZE ), 0 );
62
41
42
+ /*
43
+ ********************
44
+ * SPI test buffers *
45
+ ********************
46
+ */
63
47
64
48
#if CONFIG_NOCACHE_MEMORY
65
49
#define __NOCACHE __attribute__((__section__(".nocache")))
@@ -69,26 +53,40 @@ static struct spi_dt_spec spi_slow = SPI_DT_SPEC_GET(SPI_SLOW_DEV, SPI_OP(FRAME_
69
53
#define __NOCACHE
70
54
#endif /* CONFIG_NOCACHE_MEMORY */
71
55
56
+ #define BUF_SIZE 18
72
57
static const char tx_data [BUF_SIZE ] = "0123456789abcdef-\0" ;
73
- static __aligned (32 ) char buffer_tx [BUF_SIZE ] __used __NOCACHE ;
74
- static __aligned (32 ) char buffer_rx [BUF_SIZE ] __used __NOCACHE ;
58
+ static __aligned (32 ) char buffer_tx [BUF_SIZE ] __NOCACHE ;
59
+ static __aligned (32 ) char buffer_rx [BUF_SIZE ] __NOCACHE ;
60
+
61
+ #define BUF2_SIZE 36
75
62
static const char tx2_data [BUF2_SIZE ] = "Thequickbrownfoxjumpsoverthelazydog\0" ;
76
- static __aligned (32 ) char buffer2_tx [BUF2_SIZE ] __used __NOCACHE ;
77
- static __aligned (32 ) char buffer2_rx [BUF2_SIZE ] __used __NOCACHE ;
63
+ static __aligned (32 ) char buffer2_tx [BUF2_SIZE ] __NOCACHE ;
64
+ static __aligned (32 ) char buffer2_rx [BUF2_SIZE ] __NOCACHE ;
65
+
66
+ #define BUF3_SIZE CONFIG_SPI_LARGE_BUFFER_SIZE
78
67
static const char large_tx_data [BUF3_SIZE ] = "Thequickbrownfoxjumpsoverthelazydog\0" ;
79
- static __aligned (32 ) char large_buffer_tx [BUF3_SIZE ] __used __NOCACHE ;
80
- static __aligned (32 ) char large_buffer_rx [BUF3_SIZE ] __used __NOCACHE ;
68
+ static __aligned (32 ) char large_buffer_tx [BUF3_SIZE ] __NOCACHE ;
69
+ static __aligned (32 ) char large_buffer_rx [BUF3_SIZE ] __NOCACHE ;
70
+
71
+ /*
72
+ ********************
73
+ * Helper functions *
74
+ ********************
75
+ */
81
76
82
77
/*
83
78
* We need 5x(buffer size) + 1 to print a comma-separated list of each
84
79
* byte in hex, plus a null.
85
80
*/
86
- static uint8_t buffer_print_tx [BUF_SIZE * 5 + 1 ];
87
- static uint8_t buffer_print_rx [BUF_SIZE * 5 + 1 ];
81
+ #define PRINT_BUF_SIZE (size ) ((size * 5) + 1)
88
82
89
- static uint8_t buffer_print_tx2 [ BUF2_SIZE * 5 + 1 ];
90
- static uint8_t buffer_print_rx2 [ BUF2_SIZE * 5 + 1 ];
83
+ static uint8_t buffer_print_tx [ PRINT_BUF_SIZE ( BUF_SIZE ) ];
84
+ static uint8_t buffer_print_rx [ PRINT_BUF_SIZE ( BUF_SIZE ) ];
91
85
86
+ static uint8_t buffer_print_tx2 [PRINT_BUF_SIZE (BUF2_SIZE )];
87
+ static uint8_t buffer_print_rx2 [PRINT_BUF_SIZE (BUF2_SIZE )];
88
+
89
+ /* function for displaying the data in the buffers */
92
90
static void to_display_format (const uint8_t * src , size_t size , char * dst )
93
91
{
94
92
size_t i ;
@@ -98,6 +96,12 @@ static void to_display_format(const uint8_t *src, size_t size, char *dst)
98
96
}
99
97
}
100
98
99
+ /*
100
+ **************
101
+ * Test cases *
102
+ **************
103
+ */
104
+
101
105
/* test transferring different buffers on the same dma channels */
102
106
static int spi_complete_multiple (struct spi_dt_spec * spec )
103
107
{
@@ -576,7 +580,6 @@ static struct k_poll_event async_evt =
576
580
K_POLL_MODE_NOTIFY_ONLY ,
577
581
& async_sig );
578
582
static K_SEM_DEFINE (caller , 0 , 1 ) ;
579
- K_THREAD_STACK_DEFINE (spi_async_stack , STACK_SIZE );
580
583
static int result = 1 ;
581
584
582
585
static void spi_async_call_cb (void * p1 ,
@@ -722,6 +725,29 @@ static int spi_resource_lock_test(struct spi_dt_spec *lock_spec,
722
725
return 0 ;
723
726
}
724
727
728
+ /*
729
+ *************************
730
+ * Test suite definition *
731
+ *************************
732
+ */
733
+
734
+ #define STACK_SIZE (512 + CONFIG_TEST_EXTRA_STACK_SIZE)
735
+ K_THREAD_STACK_DEFINE (spi_async_stack , STACK_SIZE );
736
+
737
+ #if defined(CONFIG_DMA ) && defined(CONFIG_NOCACHE_MEMORY )
738
+ #define DMA_ENABLED_STR ", DMA enabled"
739
+ #elif defined(CONFIG_DMA )
740
+ #define DMA_ENABLED_STR ", DMA enabled (without CONFIG_NOCACHE_MEMORY)"
741
+ #else
742
+ #define DMA_ENABLED_STR
743
+ #endif /* CONFIG_DMA */
744
+
745
+ #ifdef CONFIG_SPI_LOOPBACK_16BITS_FRAMES
746
+ #define FRAME_SIZE_STR ", frame size = 16"
747
+ #else
748
+ #define FRAME_SIZE_STR ", frame size = 8"
749
+ #endif /* CONFIG_SPI_LOOPBACK_16BITS_FRAMES */
750
+
725
751
ZTEST (spi_loopback , test_spi_loopback )
726
752
{
727
753
#if (CONFIG_SPI_ASYNC )
0 commit comments