Software APIs
spi_device_ujson_console_test.c
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 
5 #include "sw/device/lib/base/status.h"
7 #include "sw/device/lib/testing/json/command.h"
8 #include "sw/device/lib/testing/json/mem.h"
9 #include "sw/device/lib/testing/json/provisioning_data.h"
10 #include "sw/device/lib/testing/spi_device_testutils.h"
11 #include "sw/device/lib/testing/test_framework/check.h"
13 #include "sw/device/lib/testing/test_framework/ujson_ottf.h"
14 #include "sw/device/lib/testing/test_framework/ujson_ottf_commands.h"
15 #include "sw/device/lib/ujson/ujson.h"
16 
18 
19 /**
20  * Tests SPI device integration with the ujson OTTF console.
21  *
22  * This test verifies the ability to send and receive personalized
23  * blob ujson structs and mem commands/payloads between the host
24  * and the SPI device using the OTTF console.
25  */
26 
27 OTTF_DEFINE_TEST_CONFIG(.console.type = kOttfConsoleSpiDevice,
28  .console.base_addr = TOP_EARLGREY_SPI_DEVICE_BASE_ADDR,
29  .console.test_may_clobber = false, );
30 
31 volatile uint8_t kTestBytes[256];
32 volatile uint32_t kTestWord;
33 volatile uint32_t kEndTest;
34 static perso_blob_t perso_blob_to_host;
35 static perso_blob_t perso_blob_from_host;
36 
37 status_t command_processor(ujson_t *uj) {
38  while (!kEndTest) {
39  test_command_t command;
40  TRY(UJSON_WITH_CRC(ujson_deserialize_test_command_t, uj, &command));
41  status_t status = ujson_ottf_dispatch(uj, command);
42  if (status_err(status) == kUnimplemented) {
43  RESP_ERR(uj, status);
44  } else if (status_err(status) != kOk) {
45  return status;
46  }
47  }
48  return OK_STATUS();
49 }
50 
51 status_t perso_blob_transaction_test(ujson_t *uj) {
52  LOG_INFO("SYNC: Sending perso blob");
53  TRY(RESP_OK(ujson_serialize_perso_blob_t, uj, &perso_blob_to_host));
54  memset(&perso_blob_from_host, 0xa5, sizeof(perso_blob_from_host));
55  LOG_INFO("SYNC: Waiting for perso blob");
56  TRY(ujson_deserialize_perso_blob_t(uj, &perso_blob_from_host));
57  CHECK_ARRAYS_EQ((uint8_t *)&perso_blob_to_host,
58  (uint8_t *)&perso_blob_from_host,
59  sizeof(perso_blob_from_host));
60  return OK_STATUS();
61 }
62 
63 bool test_main(void) {
64  kEndTest = 0;
65  kTestWord = 0xface1234u;
66  for (size_t i = 0; i < 256; ++i) {
67  kTestBytes[i] = (uint8_t)i;
68  }
69  ujson_t uj = ujson_ottf_console();
70 
71  status_t result = OK_STATUS();
72  EXECUTE_TEST(result, perso_blob_transaction_test, &uj);
73  EXECUTE_TEST(result, command_processor, &uj);
74 
75  return status_ok(result);
76 }