Software APIs
i2c_host_accelerometer_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 #include <assert.h>
5 
14 #include "sw/device/lib/testing/i2c_testutils.h"
15 #include "sw/device/lib/testing/rv_core_ibex_testutils.h"
16 #include "sw/device/lib/testing/test_framework/check.h"
18 
20 #include "i2c_regs.h" // Generated.
21 
22 static_assert(__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__,
23  "This test assumes the target platform is little endian.");
24 
25 OTTF_DEFINE_TEST_CONFIG();
26 
27 enum {
28  kDeviceAddr = 0x1D,
29 
30  // Registers
31  kDeviceIdReg = 0x00,
32  kThreshTapReg = 0x1D,
33  kPowerCtrlReg = 0x2D,
34  kIntSourceReg = 0x30,
35  kDataX0Reg = 0x32,
36  kDataX1Reg = 0x33,
37  kDataY0Reg = 0x34,
38  kDataY1Reg = 0x35,
39  kDataZ0Reg = 0x36,
40  kDataZ1Reg = 0x37,
41 
42  // Registers values
43  kDeviceId = 0xE5,
44  kMeasure = 0x08,
45 
46  // kIntSourceReg masks
47  kIntSourceDataReadyBit = 0x01 << 7,
48 
49  // Timing specification.
50  kDefaultTimeoutMicros = 5000,
51  kTurnOnTimeMicros = 11000,
52 };
53 
54 static dif_rv_core_ibex_t rv_core_ibex;
55 static dif_pinmux_t pinmux;
56 static dif_i2c_t i2c;
57 
58 static status_t read_device_id(void) {
59  uint8_t reg = kDeviceIdReg, data = 0;
60  TRY(i2c_testutils_write(&i2c, kDeviceAddr, 1, &reg, true));
61  TRY(i2c_testutils_read(&i2c, kDeviceAddr, 1, &data, kDefaultTimeoutMicros));
62  TRY_CHECK(data == kDeviceId, "Unexpected value %x", data);
63  return OK_STATUS();
64 }
65 
66 static status_t read_write_thresh_tap(void) {
67  uint8_t reg = kThreshTapReg;
68 
69  // Write some value to the THRESH_TAP register.
70  uint8_t write_data[2] = {reg, 0xAB};
71  TRY(i2c_testutils_write(&i2c, kDeviceAddr, sizeof(write_data), write_data,
72  false));
73 
74  // Read the value back to confirm the write.
75  uint8_t read_data = 0;
76  TRY(i2c_testutils_write(&i2c, kDeviceAddr, 1, &reg, true));
77  TRY(i2c_testutils_read(&i2c, kDeviceAddr, 1, &read_data,
78  kDefaultTimeoutMicros));
79  TRY_CHECK(read_data == 0xAB, "Unexpected value %x", read_data);
80 
81  return OK_STATUS();
82 }
83 
84 static status_t read_register(uint8_t reg) {
85  uint8_t data = 0;
86  TRY(i2c_testutils_write(&i2c, kDeviceAddr, 1, &reg, true));
87  TRY(i2c_testutils_read(&i2c, kDeviceAddr, sizeof(data), &data,
88  kDefaultTimeoutMicros));
89  return OK_STATUS(data);
90 }
91 
92 static status_t take_measurement(void) {
93  // Set the power mode to enable measurements.
94  uint8_t write_data[2] = {kPowerCtrlReg, kMeasure};
95  TRY(i2c_testutils_write(&i2c, kDeviceAddr, sizeof(write_data), write_data,
96  false));
97 
99  (uint8_t)TRY(read_register(kIntSourceReg)) & kIntSourceDataReadyBit,
100  kTurnOnTimeMicros);
101 
102  // Read all six data measurements starting from DATAX0.
103  uint8_t data_x_reg = kDataX0Reg;
104  uint8_t read_data[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
105  TRY(i2c_testutils_write(&i2c, kDeviceAddr, 1, &data_x_reg, true));
106  TRY(i2c_testutils_read(&i2c, kDeviceAddr, sizeof(read_data), read_data,
107  kDefaultTimeoutMicros));
108 
109  // Check the registers didn't all read as 0x00, which could be legitimate
110  // measurements, but are more likely to show a read failure.
111  uint8_t all_bits = 0x00;
112  for (size_t i = 0; i < ARRAYSIZE(read_data); ++i) {
113  all_bits |= read_data[i];
114  }
115  TRY_CHECK(all_bits != 0x00, "All measurement registers zero");
116 
117  return OK_STATUS();
118 }
119 
120 static status_t test_init(void) {
121  mmio_region_t base_addr =
123 
124  TRY(dif_rv_core_ibex_init(base_addr, &rv_core_ibex));
125 
127  TRY(dif_i2c_init(base_addr, &i2c));
128 
130  TRY(dif_pinmux_init(base_addr, &pinmux));
131 
132  TRY(i2c_testutils_select_pinmux(&pinmux, 2, I2cPinmuxPlatformIdCw310Pmod));
133 
135 
136  return OK_STATUS();
137 }
138 
139 bool test_main(void) {
140  status_t test_result;
141  CHECK_STATUS_OK(test_init());
142 
145 
146  test_result = OK_STATUS();
147  for (size_t i = 0; i < ARRAYSIZE(speeds); ++i) {
148  CHECK_STATUS_OK(i2c_testutils_set_speed(&i2c, speeds[i]));
149  EXECUTE_TEST(test_result, read_device_id);
150  EXECUTE_TEST(test_result, read_write_thresh_tap);
151  EXECUTE_TEST(test_result, take_measurement);
152  }
153 
154  return status_ok(test_result);
155 }