Skip to content

Commit b019728

Browse files
decsnynashif
authored andcommitted
tests: spi_loopback: Create transceive ztest wrapper
There was a lot of copy paste of this code: int ret; ret = spi_transceive_dt(spec, &tx, &rx); if (ret) { LOG_ERR("Code %d", ret); zassert_false(ret, "SPI transceive failed"); return ret; } This is quite redundant and can be simplified. Also, return is not needed because a failed zassert will not continue to return anyways. Signed-off-by: Declan Snyder <[email protected]>
1 parent 362218c commit b019728

File tree

1 file changed

+19
-62
lines changed
  • tests/drivers/spi/spi_loopback/src

1 file changed

+19
-62
lines changed

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

Lines changed: 19 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
* Copyright 2025 NXP
23
* Copyright (c) 2017 Intel Corporation.
34
*
45
* SPDX-License-Identifier: Apache-2.0
@@ -96,6 +97,16 @@ static void to_display_format(const uint8_t *src, size_t size, char *dst)
9697
}
9798
}
9899

100+
/* just a wrapper of the driver transceive call with ztest error assert */
101+
static void spi_loopback_transceive(struct spi_dt_spec *const spec,
102+
const struct spi_buf_set *const tx,
103+
const struct spi_buf_set *const rx)
104+
{
105+
int ret = spi_transceive_dt(spec, tx, rx);
106+
107+
zassert_false(ret, "SPI transceive failed, code %d", ret);
108+
}
109+
99110
/*
100111
**************
101112
* Test cases *
@@ -130,16 +141,10 @@ static int spi_complete_multiple(struct spi_dt_spec *spec)
130141
rx_bufs[1].buf = buffer2_rx;
131142
rx_bufs[1].len = BUF2_SIZE;
132143

133-
int ret;
134144

135145
LOG_INF("Start complete multiple");
136146

137-
ret = spi_transceive_dt(spec, &tx, &rx);
138-
if (ret) {
139-
LOG_ERR("Code %d", ret);
140-
zassert_false(ret, "SPI transceive failed");
141-
return ret;
142-
}
147+
spi_loopback_transceive(spec, &tx, &rx);
143148

144149
if (memcmp(buffer_tx, buffer_rx, BUF_SIZE)) {
145150
to_display_format(buffer_tx, BUF_SIZE, buffer_print_tx);
@@ -187,16 +192,9 @@ static int spi_complete_loop(struct spi_dt_spec *spec)
187192
.count = ARRAY_SIZE(rx_bufs)
188193
};
189194

190-
int ret;
191-
192195
LOG_INF("Start complete loop");
193196

194-
ret = spi_transceive_dt(spec, &tx, &rx);
195-
if (ret) {
196-
LOG_ERR("Code %d", ret);
197-
zassert_false(ret, "SPI transceive failed");
198-
return ret;
199-
}
197+
spi_loopback_transceive(spec, &tx, &rx);
200198

201199
if (memcmp(buffer_tx, buffer_rx, BUF_SIZE)) {
202200
to_display_format(buffer_tx, BUF_SIZE, buffer_print_tx);
@@ -242,17 +240,9 @@ static int spi_null_tx_buf(struct spi_dt_spec *spec)
242240
.count = ARRAY_SIZE(rx_bufs)
243241
};
244242

245-
int ret;
246-
247243
LOG_INF("Start null tx");
248244

249-
ret = spi_transceive_dt(spec, &tx, &rx);
250-
if (ret) {
251-
LOG_ERR("Code %d", ret);
252-
zassert_false(ret, "SPI transceive failed");
253-
return ret;
254-
}
255-
245+
spi_loopback_transceive(spec, &tx, &rx);
256246

257247
if (memcmp(buffer_rx, EXPECTED_NOP_RETURN_BUF, BUF_SIZE)) {
258248
to_display_format(buffer_rx, BUF_SIZE, buffer_print_rx);
@@ -289,18 +279,10 @@ static int spi_rx_half_start(struct spi_dt_spec *spec)
289279
.buffers = rx_bufs,
290280
.count = ARRAY_SIZE(rx_bufs)
291281
};
292-
int ret;
293-
294-
LOG_INF("Start half start");
295282

296283
(void)memset(buffer_rx, 0, BUF_SIZE);
297284

298-
ret = spi_transceive_dt(spec, &tx, &rx);
299-
if (ret) {
300-
LOG_ERR("Code %d", ret);
301-
zassert_false(ret, "SPI transceive failed");
302-
return -1;
303-
}
285+
spi_loopback_transceive(spec, &tx, &rx);
304286

305287
if (memcmp(buffer_tx, buffer_rx, 8)) {
306288
to_display_format(buffer_tx, 8, buffer_print_tx);
@@ -342,7 +324,6 @@ static int spi_rx_half_end(struct spi_dt_spec *spec)
342324
.buffers = rx_bufs,
343325
.count = ARRAY_SIZE(rx_bufs)
344326
};
345-
int ret;
346327

347328
if (IS_ENABLED(CONFIG_SPI_STM32_DMA)) {
348329
LOG_INF("Skip half end");
@@ -353,12 +334,7 @@ static int spi_rx_half_end(struct spi_dt_spec *spec)
353334

354335
(void)memset(buffer_rx, 0, BUF_SIZE);
355336

356-
ret = spi_transceive_dt(spec, &tx, &rx);
357-
if (ret) {
358-
LOG_ERR("Code %d", ret);
359-
zassert_false(ret, "SPI transceive failed");
360-
return -1;
361-
}
337+
spi_loopback_transceive(spec, &tx, &rx);
362338

363339
if (memcmp(buffer_tx + 8, buffer_rx, 8)) {
364340
to_display_format(buffer_tx + 8, 8, buffer_print_tx);
@@ -408,7 +384,6 @@ static int spi_rx_every_4(struct spi_dt_spec *spec)
408384
.buffers = rx_bufs,
409385
.count = ARRAY_SIZE(rx_bufs)
410386
};
411-
int ret;
412387

413388
if (IS_ENABLED(CONFIG_SPI_STM32_DMA)) {
414389
LOG_INF("Skip every 4");
@@ -424,12 +399,7 @@ static int spi_rx_every_4(struct spi_dt_spec *spec)
424399

425400
(void)memset(buffer_rx, 0, BUF_SIZE);
426401

427-
ret = spi_transceive_dt(spec, &tx, &rx);
428-
if (ret) {
429-
LOG_ERR("Code %d", ret);
430-
zassert_false(ret, "SPI transceive failed");
431-
return -1;
432-
}
402+
spi_loopback_transceive(spec, &tx, &rx);
433403

434404
if (memcmp(buffer_tx + 4, buffer_rx, 4)) {
435405
to_display_format(buffer_tx + 4, 4, buffer_print_tx);
@@ -479,7 +449,6 @@ static int spi_rx_bigger_than_tx(struct spi_dt_spec *spec)
479449
.buffers = rx_bufs,
480450
.count = ARRAY_SIZE(rx_bufs)
481451
};
482-
int ret;
483452

484453
if (IS_ENABLED(CONFIG_SPI_STM32_DMA)) {
485454
LOG_INF("Skip rx bigger than tx");
@@ -495,12 +464,7 @@ static int spi_rx_bigger_than_tx(struct spi_dt_spec *spec)
495464

496465
(void)memset(buffer_rx, 0xff, BUF_SIZE);
497466

498-
ret = spi_transceive_dt(spec, &tx, &rx);
499-
if (ret) {
500-
LOG_ERR("Code %d", ret);
501-
zassert_false(ret, "SPI transceive failed");
502-
return -1;
503-
}
467+
spi_loopback_transceive(spec, &tx, &rx);
504468

505469
if (memcmp(buffer_tx, buffer_rx, tx_buf_size)) {
506470
to_display_format(buffer_tx, tx_buf_size, buffer_print_tx);
@@ -553,16 +517,9 @@ static int spi_complete_large_transfers(struct spi_dt_spec *spec)
553517
rx_bufs.buf = large_buffer_rx;
554518
rx_bufs.len = BUF3_SIZE;
555519

556-
int ret;
557-
558520
LOG_INF("Start complete large transfers");
559521

560-
ret = spi_transceive_dt(spec, &tx, &rx);
561-
if (ret) {
562-
LOG_ERR("Code %d", ret);
563-
zassert_false(ret, "SPI transceive failed");
564-
return ret;
565-
}
522+
spi_loopback_transceive(spec, &tx, &rx);
566523

567524
if (memcmp(large_buffer_tx, large_buffer_rx, BUF3_SIZE)) {
568525
zassert_false(1, "Large Buffer contents are different");

0 commit comments

Comments
 (0)