Software APIs
sw_straps_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 
7 #include "sw/device/lib/base/status.h"
11 #include "sw/device/lib/testing/json/command.h"
12 #include "sw/device/lib/testing/pinmux_testutils.h"
13 #include "sw/device/lib/testing/test_framework/check.h"
15 #include "sw/device/lib/testing/test_framework/ujson_ottf.h"
16 #include "sw/device/lib/ujson/ujson.h"
17 
19 
20 static dif_gpio_t gpio;
21 static dif_pinmux_t pinmux;
22 OTTF_DEFINE_TEST_CONFIG();
23 
24 status_t test_sw_strap_read(ujson_t *uj) {
25  const char bits[] = {'s', 'w', 'W', 'S'};
26  char pattern[4] = {0};
27  uint32_t strap = pinmux_testutils_read_straps(&pinmux, &gpio);
28  for (size_t i = 0; i < 3; ++i) {
29  uint32_t v = (strap >> (2 * i)) & 3;
30  pattern[2 - i] = bits[v];
31  }
32  LOG_INFO("strap = %02x, pattern = %s", strap, pattern);
33  return RESP_OK_STATUS(uj, (int32_t)strap);
34 }
35 
36 status_t command_processor(ujson_t *uj) {
37  while (true) {
38  test_command_t command;
39  TRY(ujson_deserialize_test_command_t(uj, &command));
40  switch (command) {
41  case kTestCommandSwStrapRead:
42  RESP_ERR(uj, test_sw_strap_read(uj));
43  break;
44  default:
45  LOG_ERROR("Unrecognized command: %d", command);
46  RESP_ERR(uj, INVALID_ARGUMENT());
47  }
48  }
49  return OK_STATUS(0);
50 }
51 
52 bool test_main(void) {
53  CHECK_DIF_OK(dif_pinmux_init(
55  CHECK_DIF_OK(
56  dif_gpio_init(mmio_region_from_addr(TOP_EARLGREY_GPIO_BASE_ADDR), &gpio));
57 
58  uint32_t strap = pinmux_testutils_read_straps(&pinmux, &gpio);
59  LOG_INFO("Initial sw_strap=%d", strap);
60 
61  ujson_t uj = ujson_ottf_console();
62  return status_ok(command_processor(&uj));
63 }