Skip to content

Commit 9f794fe

Browse files
AlexJones0alees24
andcommitted
Enable SPI external loopback test between MB3 & MB4
Enables the SPI external loopback test by adding an external jumper cable between pins MB3 and MB4 on the mikroBUS click header. Performs appropriate pinmuxing if the SPI test is configured to run this test, so that the test can pass. Co-authored-by: Adrian Lees <[email protected]>
1 parent cc2514b commit 9f794fe

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

sw/cheri/tests/spi_tests.hh

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "../common/console.hh"
77
#include "../common/flash-utils.hh"
88
#include "../common/uart-utils.hh"
9+
#include "../common/platform-pinmux.hh"
910
#include "test_runner.hh"
1011
#include <cheri.hh>
1112
#include <ds/xoroshiro.h>
@@ -42,14 +43,16 @@ using namespace CHERI;
4243
/**
4344
* Configures which of the SPI controllers shall use an external loopback
4445
* via a jumper cable, and not just the internal loopback within the SPI
45-
* block itself. (-1 = no jumper cable present)
46+
* block itself. (-1 = no jumper cable present).
47+
*
48+
* Defaults to using SPI controller 4, which requires you to have
49+
* a loopback jumper cable between the MB3 and MB4 pins of the mikroBUS
50+
* Click header.
51+
*
52+
* This can be overriden via a compilation flag.
4653
*/
4754
#ifndef SPI_TEST_EXT_LOOPBACK_CONN
48-
// Try an external loopback test with another SPI controller;
49-
// Note: we can install a loopback wire between pins MB3 and MB4 of the
50-
// mikroBUS Click header in CI and test this using SPI controller 4.
51-
// #define SPI_TEST_EXT_LOOPBACK_CONN (4)
52-
#define SPI_TEST_EXT_LOOPBACK_CONN (-1)
55+
#define SPI_TEST_EXT_LOOPBACK_CONN (4)
5356
#endif
5457

5558
// The expected JEDEC ID to read from the SPI Flash
@@ -417,7 +420,10 @@ int spi_irq_test(SpiPtr spi, ds::xoroshiro::P32R8 &prng, Log &log) {
417420
*/
418421
int spi_loopback_test(SpiPtr spi, bool external, bool cpol, bool cpha, bool msb_first, ds::xoroshiro::P32R8 &prng,
419422
Log &log) {
420-
constexpr uint32_t kSpiSpeed = 0u; // Let's go as fast as possible.
423+
// Go as fast as possible, unless using an external loopback, as the
424+
// Verilator simulation includes a single cycle delay forbidding
425+
// this from running at full speed.
426+
const uint32_t kSpiSpeed = external ? 1u : 0u;
421427
// Take a copy of the PRNG so that we can predict the read-side data.
422428
ds::xoroshiro::P32R8 read_prng = prng;
423429
size_t bytes_read = 0u;
@@ -491,6 +497,17 @@ void spi_tests(CapRoot root, Log &log) {
491497

492498
SpiFlash spi_flash(spis[2]);
493499

500+
// Create a bounded capability for pinmux & initialise the driver
501+
Capability<volatile uint8_t> pinmux = root.cast<volatile uint8_t>();
502+
pinmux.address() = PINMUX_ADDRESS;
503+
pinmux.bounds() = PINMUX_BOUNDS;
504+
SonataPinmux Pinmux = SonataPinmux(pinmux);
505+
506+
if (SPI_TEST_EXT_LOOPBACK_CONN == 4) {
507+
// Mux SPI2 to receive from MB3.
508+
Pinmux.block_input_select(SonataPinmux::BlockInput::spi_2_cipo, 2);
509+
}
510+
494511
// Initialise 8-bit PRNG for use in random test data
495512
ds::xoroshiro::P32R8 prng;
496513
prng.set_state(0xDEAD, 0xBEEF);
@@ -570,4 +587,9 @@ void spi_tests(CapRoot root, Log &log) {
570587

571588
check_result(log, !test_failed);
572589
}
590+
591+
if (SPI_TEST_EXT_LOOPBACK_CONN == 4) {
592+
// Mux SPI2 back to its default
593+
Pinmux.block_input_select(SonataPinmux::BlockInput::spi_2_cipo, 1);
594+
}
573595
}

0 commit comments

Comments
 (0)