Software APIs
gpio_smoketest.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 "dt/dt_gpio.h"
6 #include "dt/dt_pinmux.h"
12 #include "sw/device/lib/testing/pinmux_testutils.h"
13 #include "sw/device/lib/testing/test_framework/check.h"
15 
16 static const dt_gpio_t kGpioDt = kDtGpio;
17 static const dt_pinmux_t kPinmuxDt = kDtPinmuxAon;
18 
19 static dif_gpio_t gpio;
20 static dif_pinmux_t pinmux;
21 OTTF_DEFINE_TEST_CONFIG();
22 
23 // Assume that the pins in dt_gpio_periph_io_t are numbered 0, 1, and so on.
24 static_assert(kDtGpioPeriphIoGpio0 == 0, "kDtGpioPinGpio0 is expected to be 0");
25 static_assert(kDtGpioPeriphIoCount == kDifGpioNumPins,
26  "kDtGpioPinCount does not match kDifGpioNumPins");
27 
28 /**
29  * A known pattern written to GPIOs.
30  *
31  * On FPGA, these patterns are tested as-is. In DV, this symbol is overwritten
32  * by the testbench to test completely random patterns. This is done in
33  * `hw/top_earlgrey/dv/env/seq_lib/chip_sw_gpio_smoke_vseq.sv`. The DV test
34  * also checks GPIO pin values at the chip periphery for correctness.
35  */
36 static const uint32_t kGpioVals[] = {0xAAAAAAAA, 0x55555555, 0xA5A5A5A5,
37  0xFFFFFFFF, 0};
38 
39 /**
40  * Writes the given value to GPIO pins and compares it against the value read.
41  *
42  * Masks the bits that correspond to the pins that we cannot test.
43  *
44  * See also: `kGpioMask`.
45  *
46  * @param write_val Value to write.
47  * @param compare_mask The GPIOs compared.
48  */
49 static void test_gpio_write(uint32_t write_val, uint32_t compare_mask) {
50  CHECK_DIF_OK(dif_gpio_write_all(&gpio, write_val));
51 
52  // The GPIO output signals are routed through pinmux back to the GPIO block
53  // and there are synchronizers involved so the inputs may not be available
54  // immediately, and may in fact arrive at different times.
56 
57  uint32_t read_val = 0;
58  CHECK_DIF_OK(dif_gpio_read_all(&gpio, &read_val));
59 
60  uint32_t expected = write_val & compare_mask;
61  uint32_t actual = read_val & compare_mask;
62  CHECK(expected == actual, "%X != %X", expected, actual);
63 }
64 
65 /**
66  * Smoke test for the GPIO peripheral.
67  *
68  * Performs a loopback test by writing various values and reading them back.
69  * NOTE: This test can currently run only on FPGA and DV.
70  */
71 bool test_main(void) {
72  uint32_t gpio_mask = pinmux_testutils_get_testable_gpios_mask();
73  CHECK_DIF_OK(dif_pinmux_init_from_dt(kPinmuxDt, &pinmux));
74  // Assign GPIOs in the pinmux
75  for (size_t i = 0; i < kDifGpioNumPins; ++i) {
76  if (gpio_mask & (1u << i)) {
77  // Assume that the I/Os in dt_gpio_periph_io_t are numbered 0, 1, and so
78  // on.
79  dt_periph_io_t periph_io =
80  dt_gpio_periph_io(kGpioDt, kDtGpioPeriphIoGpio0 + i);
81  dt_pad_t pad = kPinmuxTestutilsGpioPads[i];
82  CHECK_STATUS_OK(pinmux_testutils_connect(&pinmux, periph_io,
83  kDtPeriphIoDirInout, pad));
84  }
85  }
86  CHECK_DIF_OK(dif_gpio_init_from_dt(kGpioDt, &gpio));
87  CHECK_DIF_OK(dif_gpio_output_set_enabled_all(&gpio, gpio_mask));
88 
89  for (uint8_t i = 0; i < ARRAYSIZE(kGpioVals); ++i) {
90  test_gpio_write(kGpioVals[i], gpio_mask);
91  }
92 
93  // Walking 1s and 0s. Iterates over every integer with one bit set and every
94  // integer with all but one bit set.
95  for (uint32_t i = 1; i > 0; i <<= 1) {
96  test_gpio_write(i, gpio_mask);
97  test_gpio_write(~i, gpio_mask);
98  }
99 
100  return true;
101 }