1 /*
2 * Copyright 2021 Google LLC
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6 #include <zephyr/device.h>
7 #include <zephyr/drivers/bbram.h>
8 #include <string.h>
9 #include <zephyr/ztest.h>
10
11 #define BBRAM_NODELABEL DT_NODELABEL(bbram)
12 #define BBRAM_SIZE DT_PROP(BBRAM_NODELABEL, size)
13
ZTEST(bbram,test_get_size)14 ZTEST(bbram, test_get_size)
15 {
16 const struct device *const dev = DEVICE_DT_GET(BBRAM_NODELABEL);
17 size_t size;
18
19 zassert_true(device_is_ready(dev), "Device is not ready");
20
21 zassert_ok(bbram_get_size(dev, &size));
22 zassert_equal(size, BBRAM_SIZE);
23 }
24
ZTEST(bbram,test_bbram_out_of_bounds)25 ZTEST(bbram, test_bbram_out_of_bounds)
26 {
27 const struct device *const dev = DEVICE_DT_GET(BBRAM_NODELABEL);
28 uint8_t buffer[BBRAM_SIZE];
29
30 zassert_equal(bbram_read(dev, 0, 0, buffer), -EFAULT);
31 zassert_equal(bbram_read(dev, 0, BBRAM_SIZE + 1, buffer), -EFAULT);
32 zassert_equal(bbram_read(dev, BBRAM_SIZE - 1, 2, buffer), -EFAULT);
33 zassert_equal(bbram_write(dev, 0, 0, buffer), -EFAULT);
34 zassert_equal(bbram_write(dev, 0, BBRAM_SIZE + 1, buffer), -EFAULT);
35 zassert_equal(bbram_write(dev, BBRAM_SIZE - 1, 2, buffer), -EFAULT);
36 }
37
ZTEST(bbram,test_read_write)38 ZTEST(bbram, test_read_write)
39 {
40 const struct device *const dev = DEVICE_DT_GET(BBRAM_NODELABEL);
41 uint8_t buffer[BBRAM_SIZE];
42 uint8_t expected[BBRAM_SIZE];
43
44 for (int i = 0; i < BBRAM_SIZE; ++i) {
45 expected[i] = i;
46 }
47 /* Set and verify content. */
48 zassert_ok(bbram_write(dev, 0, BBRAM_SIZE, expected));
49 zassert_ok(bbram_read(dev, 0, BBRAM_SIZE, buffer));
50 zassert_mem_equal(buffer, expected, BBRAM_SIZE, NULL);
51 }
52
ZTEST(bbram,test_set_invalid)53 ZTEST(bbram, test_set_invalid)
54 {
55 const struct device *const dev = DEVICE_DT_GET(BBRAM_NODELABEL);
56
57 zassert_equal(bbram_check_invalid(dev), 0);
58 zassert_ok(bbram_emul_set_invalid(dev, true));
59 zassert_equal(bbram_check_invalid(dev), 1);
60 zassert_equal(bbram_check_invalid(dev), 0);
61 }
62
ZTEST(bbram,test_set_standby)63 ZTEST(bbram, test_set_standby)
64 {
65 const struct device *const dev = DEVICE_DT_GET(BBRAM_NODELABEL);
66
67 zassert_equal(bbram_check_standby_power(dev), 0);
68 zassert_ok(bbram_emul_set_standby_power_state(dev, true));
69 zassert_equal(bbram_check_standby_power(dev), 1);
70 zassert_equal(bbram_check_standby_power(dev), 0);
71 }
72
ZTEST(bbram,test_set_power)73 ZTEST(bbram, test_set_power)
74 {
75 const struct device *const dev = DEVICE_DT_GET(BBRAM_NODELABEL);
76
77 zassert_equal(bbram_check_power(dev), 0);
78 zassert_ok(bbram_emul_set_power_state(dev, true));
79 zassert_equal(bbram_check_power(dev), 1);
80 zassert_equal(bbram_check_power(dev), 0);
81 }
82
ZTEST(bbram,test_reset_invalid_on_read)83 ZTEST(bbram, test_reset_invalid_on_read)
84 {
85 const struct device *const dev = DEVICE_DT_GET(BBRAM_NODELABEL);
86 uint8_t buffer[BBRAM_SIZE];
87
88 zassert_ok(bbram_emul_set_invalid(dev, true));
89 zassert_equal(bbram_read(dev, 0, BBRAM_SIZE, buffer), -EFAULT);
90 zassert_equal(bbram_check_invalid(dev), 0);
91 }
92
ZTEST(bbram,test_reset_invalid_on_write)93 ZTEST(bbram, test_reset_invalid_on_write)
94 {
95 const struct device *const dev = DEVICE_DT_GET(BBRAM_NODELABEL);
96 uint8_t buffer[BBRAM_SIZE];
97
98 zassert_ok(bbram_emul_set_invalid(dev, true));
99 zassert_equal(bbram_write(dev, 0, BBRAM_SIZE, buffer), -EFAULT);
100 zassert_equal(bbram_check_invalid(dev), 0);
101 }
102
before(void * data)103 static void before(void *data)
104 {
105 ARG_UNUSED(data);
106 const struct device *const dev = DEVICE_DT_GET(BBRAM_NODELABEL);
107
108 bbram_emul_set_invalid(dev, false);
109 bbram_emul_set_standby_power_state(dev, false);
110 bbram_emul_set_power_state(dev, false);
111 }
112
113 ZTEST_SUITE(bbram, NULL, NULL, before, NULL, NULL);
114