Skip to content

Commit 362218c

Browse files
decsnynashif
authored andcommitted
tests: spi_loopback: Clean up file defines
Following the boy scout rule, since I am using this test a lot lately and going to improve it, the first step will be to clean up the structure of the file. The following changes are made: - Remove unused #includes - Condense some #if #else macro defines or make into single line, to simplify the readability of the preprocessor code - Move definitions around so that related things are all next to each other, instead of randomly scattered around the file - Create section header comments for broadly related things in the file to improve developer navigation experience - Introduce macro for copy pasted print buf size calculation - A few minor comment edits Signed-off-by: Declan Snyder <[email protected]>
1 parent 04ffa4e commit 362218c

File tree

1 file changed

+82
-56
lines changed
  • tests/drivers/spi/spi_loopback/src

1 file changed

+82
-56
lines changed

tests/drivers/spi/spi_loopback/src/spi.c

Lines changed: 82 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,62 +4,46 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

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 */
108

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+
*/
1714

15+
#include <zephyr/ztest.h>
1816
#include <zephyr/drivers/spi.h>
17+
#include <zephyr/kernel.h>
18+
#include <stdio.h>
19+
#include <zephyr/logging/log.h>
1920

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);
4422

45-
#else /* CONFIG_DMA */
23+
/*
24+
**********************
25+
* SPI configurations *
26+
**********************
27+
*/
4628

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))
4931

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
5235

36+
#define SPI_FAST_DEV DT_COMPAT_GET_ANY_STATUS_OKAY(test_spi_loopback_fast)
5337
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);
5538

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);
6241

42+
/*
43+
********************
44+
* SPI test buffers *
45+
********************
46+
*/
6347

6448
#if CONFIG_NOCACHE_MEMORY
6549
#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_
6953
#define __NOCACHE
7054
#endif /* CONFIG_NOCACHE_MEMORY */
7155

56+
#define BUF_SIZE 18
7257
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
7562
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
7867
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+
*/
8176

8277
/*
8378
* We need 5x(buffer size) + 1 to print a comma-separated list of each
8479
* byte in hex, plus a null.
8580
*/
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)
8882

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)];
9185

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 */
9290
static void to_display_format(const uint8_t *src, size_t size, char *dst)
9391
{
9492
size_t i;
@@ -98,6 +96,12 @@ static void to_display_format(const uint8_t *src, size_t size, char *dst)
9896
}
9997
}
10098

99+
/*
100+
**************
101+
* Test cases *
102+
**************
103+
*/
104+
101105
/* test transferring different buffers on the same dma channels */
102106
static int spi_complete_multiple(struct spi_dt_spec *spec)
103107
{
@@ -576,7 +580,6 @@ static struct k_poll_event async_evt =
576580
K_POLL_MODE_NOTIFY_ONLY,
577581
&async_sig);
578582
static K_SEM_DEFINE(caller, 0, 1);
579-
K_THREAD_STACK_DEFINE(spi_async_stack, STACK_SIZE);
580583
static int result = 1;
581584

582585
static void spi_async_call_cb(void *p1,
@@ -722,6 +725,29 @@ static int spi_resource_lock_test(struct spi_dt_spec *lock_spec,
722725
return 0;
723726
}
724727

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+
725751
ZTEST(spi_loopback, test_spi_loopback)
726752
{
727753
#if (CONFIG_SPI_ASYNC)

0 commit comments

Comments
 (0)