Skip to content

Commit

Permalink
[test] add TX test cases for spi_device OTTF console
Browse files Browse the repository at this point in the history
This tests the OTTF spi_device console in TX direction.

Signed-off-by: Anthony Chen <antchen@google.com>
  • Loading branch information
anthonychen1251 committed Aug 22, 2024
1 parent 326cf75 commit 1f6fed2
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 9 deletions.
47 changes: 40 additions & 7 deletions sw/device/tests/spi_device_ottf_console_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

#include "sw/device/lib/dif/dif_spi_device.h"
#include "sw/device/lib/runtime/log.h"
#include "sw/device/lib/testing/spi_device_testutils.h"
#include "sw/device/lib/testing/test_framework/check.h"
#include "sw/device/lib/testing/test_framework/ottf_console.h"
#include "sw/device/lib/testing/test_framework/ottf_main.h"

#include "hw/top_earlgrey/sw/autogen/top_earlgrey.h"
Expand Down Expand Up @@ -93,22 +95,53 @@ static const char kTest4KbDataStr[] =
"B30518D571FDD6D38E0477F6CB83C7729A45494F5D7805CCC1432C816B7D8CB089CEA56216"
"9489E4F80E70FA685F39E1CD0AD7AD703C2E9601D442004F3D4CE043F0E84007FB7438FE82"
"DF4D9304C90B48BB25762DD29D";

static uint8_t input_buf[5120];

bool test_main(void) {
LOG_INFO("Sending empty string...");
LOG_INFO("Sending empty string to Host...");
LOG_INFO("");
LOG_INFO("Sending test string...");
LOG_INFO("Sending test string to Host...");
LOG_INFO("%s", kTestStr);
LOG_INFO("Sending 64B data...");
LOG_INFO("SYNC: Waiting for console data");
size_t received_data_len =
ottf_console_spi_device_read(sizeof(input_buf), input_buf);
CHECK(received_data_len == sizeof(kTestStr));
CHECK_ARRAYS_EQ(input_buf, kTestStr, ARRAYSIZE(kTestStr));

LOG_INFO("Sending 64B data to Host...");
LOG_INFO("%s", kTest64bDataStr);
LOG_INFO("Sending 256B data...");
LOG_INFO("SYNC: Waiting for console data");
received_data_len =
ottf_console_spi_device_read(sizeof(input_buf), input_buf);
CHECK(received_data_len == sizeof(kTest64bDataStr));
CHECK_ARRAYS_EQ(input_buf, kTest64bDataStr, ARRAYSIZE(kTest64bDataStr));

LOG_INFO("Sending 256B data to Host...");
LOG_INFO("%s", kTest256bDataStr);
LOG_INFO("Sending 1KB data...");
LOG_INFO("SYNC: Waiting for console data");
received_data_len =
ottf_console_spi_device_read(sizeof(input_buf), input_buf);
CHECK(received_data_len == sizeof(kTest256bDataStr));
CHECK_ARRAYS_EQ(input_buf, kTest256bDataStr, ARRAYSIZE(kTest256bDataStr));

LOG_INFO("Sending 1KB data to Host...");
for (int i = 1; i <= 2; i++) {
LOG_INFO("Round: %d", i);
LOG_INFO("%s", kTest1KbDataStr);
LOG_INFO("SYNC: Waiting for console data");
received_data_len =
ottf_console_spi_device_read(sizeof(input_buf), input_buf);
CHECK(received_data_len == sizeof(kTest1KbDataStr));
CHECK_ARRAYS_EQ(input_buf, kTest1KbDataStr, ARRAYSIZE(kTest1KbDataStr));
}
LOG_INFO("Sending 4KB data...");
LOG_INFO("%s", kTest4KbDataStr);

LOG_INFO("Sending 4KB data to Host...");
LOG_INFO("%s", kTest4KbDataStr);
LOG_INFO("SYNC: Waiting for console data");
received_data_len =
ottf_console_spi_device_read(sizeof(input_buf), input_buf);
CHECK(received_data_len == sizeof(kTest4KbDataStr));
CHECK_ARRAYS_EQ(input_buf, kTest4KbDataStr, ARRAYSIZE(kTest4KbDataStr));
return true;
}
27 changes: 25 additions & 2 deletions sw/host/tests/chip/spi_device_ottf_console/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::time::Duration;
use opentitanlib::app::TransportWrapper;
use opentitanlib::console::spi::SpiConsoleDevice;
use opentitanlib::execute_test;
use opentitanlib::io::console::ConsoleDevice;
use opentitanlib::test_utils;
use opentitanlib::test_utils::init::InitializeTest;
use opentitanlib::uart::console::{ExitStatus, UartConsole};
Expand All @@ -34,6 +35,8 @@ struct Opts {
firmware_elf: PathBuf,
}

const SYNC_MSG: &str = r"SYNC:.*\r\n";

fn spi_device_console_test(opts: &Opts, transport: &TransportWrapper) -> Result<()> {
let mut console = UartConsole {
timeout: Some(opts.timeout),
Expand All @@ -54,20 +57,40 @@ fn spi_device_console_test(opts: &Opts, transport: &TransportWrapper) -> Result<
let mut data = test_utils::object::symbol_data(&object, "kTestStr")?;
let mut data_str = std::str::from_utf8(&data)?.trim_matches(char::from(0));
_ = UartConsole::wait_for(&spi_console_device, data_str, opts.timeout)?;
log::info!("Sending test string to Device...");
_ = UartConsole::wait_for(&spi_console_device, SYNC_MSG, opts.timeout)?;
spi_console_device.console_write(&data)?;

data = test_utils::object::symbol_data(&object, "kTest64bDataStr")?;
data_str = std::str::from_utf8(&data)?.trim_matches(char::from(0));
_ = UartConsole::wait_for(&spi_console_device, data_str, opts.timeout)?;
log::info!("Sending 64B data to Device...");
_ = UartConsole::wait_for(&spi_console_device, SYNC_MSG, opts.timeout)?;
spi_console_device.console_write(&data)?;

data = test_utils::object::symbol_data(&object, "kTest256bDataStr")?;
data_str = std::str::from_utf8(&data)?.trim_matches(char::from(0));
_ = UartConsole::wait_for(&spi_console_device, data_str, opts.timeout)?;
log::info!("Sending 256 data to Device...");
_ = UartConsole::wait_for(&spi_console_device, SYNC_MSG, opts.timeout)?;
spi_console_device.console_write(&data)?;

data = test_utils::object::symbol_data(&object, "kTest1KbDataStr")?;
data_str = std::str::from_utf8(&data)?.trim_matches(char::from(0));
// 1KB data will be sent twice.
_ = UartConsole::wait_for(&spi_console_device, data_str, opts.timeout)?;
_ = UartConsole::wait_for(&spi_console_device, data_str, opts.timeout)?;
for _round in 0..2 {
_ = UartConsole::wait_for(&spi_console_device, data_str, opts.timeout)?;
log::info!("Sending 1KB data to Device...");
_ = UartConsole::wait_for(&spi_console_device, SYNC_MSG, opts.timeout)?;
spi_console_device.console_write(&data)?;
}

data = test_utils::object::symbol_data(&object, "kTest4KbDataStr")?;
data_str = std::str::from_utf8(&data)?.trim_matches(char::from(0));
_ = UartConsole::wait_for(&spi_console_device, data_str, opts.timeout)?;
log::info!("Sending 4KB data to Device...");
_ = UartConsole::wait_for(&spi_console_device, SYNC_MSG, opts.timeout)?;
spi_console_device.console_write(&data)?;

let result = console.interact(&spi_console_device, None, Some(&mut stdout))?;
match result {
Expand Down

0 comments on commit 1f6fed2

Please sign in to comment.