1 /*
2  * Copyright 2024 Google LLC
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #include <zephyr/device.h>
7 #include <zephyr/drivers/bbram.h>
8 #include <zephyr/drivers/emul.h>
9 #include <zephyr/drivers/emul_bbram.h>
10 #include <zephyr/ztest.h>
11 
12 #include "fixture.h"
13 
run_test_write_invalid_size(const struct device * dev,const struct emul * emulator)14 static void run_test_write_invalid_size(const struct device *dev, const struct emul *emulator)
15 {
16 	uint8_t data = 0;
17 	size_t bbram_size = 0;
18 	int rc;
19 
20 	ARG_UNUSED(emulator);
21 
22 	rc = bbram_write(dev, 0, 0, &data);
23 	zassert_equal(-EINVAL, rc, "got %d", rc);
24 
25 	rc = bbram_get_size(dev, &bbram_size);
26 	zassert_ok(rc, "got %d", rc);
27 
28 	rc = bbram_write(dev, 0, bbram_size + 1, &data);
29 	zassert_equal(-EINVAL, rc, "got %d", rc);
30 }
31 
run_test_write_bytes(const struct device * dev,const struct emul * emulator)32 static void run_test_write_bytes(const struct device *dev, const struct emul *emulator)
33 {
34 	uint8_t data = 0;
35 	uint8_t expected_data = 0;
36 	size_t bbram_size = 0;
37 	int rc;
38 
39 	rc = bbram_get_size(dev, &bbram_size);
40 	zassert_ok(rc, "got %d", rc);
41 
42 	for (size_t i = 0; i < bbram_size; ++i) {
43 		expected_data = i % BIT(8);
44 		rc = bbram_write(dev, i, 1, &expected_data);
45 		zassert_ok(rc, "Failed to set expected data at offset %zu", i);
46 
47 		rc = emul_bbram_backend_get_data(emulator, i, 1, &data);
48 		zassert_ok(rc, "Failed to get byte at offset %zu", i);
49 		zassert_equal(expected_data, data, "Expected %u, but got %u", expected_data, data);
50 	}
51 }
52 
53 #define DECLARE_ZTEST_PER_DEVICE(inst)                                                             \
54 	BBRAM_TEST_IMPL(write_invalid_size, inst)                                                  \
55 	BBRAM_TEST_IMPL(write_bytes, inst)
56 
57 BBRAM_FOR_EACH(DECLARE_ZTEST_PER_DEVICE)
58