|
| 1 | +/** |
| 2 | + * Copyright (c) 2025 Basalte bv |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +#include <zephyr/ztest.h> |
| 7 | +#include <zephyr/nvmem.h> |
| 8 | + |
| 9 | +#define consumer0 DT_NODELABEL(test_consumer0) |
| 10 | +#define nvmem0 DT_ALIAS(nvmem0) |
| 11 | + |
| 12 | +static const struct nvmem_cell cell0 = NVMEM_CELL_GET_BY_IDX(consumer0, 0); |
| 13 | +static const struct nvmem_cell cell10 = NVMEM_CELL_GET_BY_NAME(consumer0, cell10); |
| 14 | + |
| 15 | +ZTEST(nvmem_api, test_nvmem_api) |
| 16 | +{ |
| 17 | + uint8_t buf[0x10]; |
| 18 | + int ret; |
| 19 | + |
| 20 | + zexpect_equal_ptr(cell0.dev, DEVICE_DT_GET(nvmem0)); |
| 21 | + zexpect_equal(cell0.offset, 0); |
| 22 | + zexpect_equal(cell0.size, 0x10); |
| 23 | + zexpect_false(cell0.read_only); |
| 24 | + |
| 25 | + zexpect_equal_ptr(cell10.dev, DEVICE_DT_GET(nvmem0)); |
| 26 | + zexpect_equal(cell10.offset, 0x10); |
| 27 | + zexpect_equal(cell10.size, 0x10); |
| 28 | + zexpect_true(cell10.read_only); |
| 29 | + |
| 30 | + for (size_t i = 0; i < sizeof(buf); ++i) { |
| 31 | + buf[i] = 2 * i; |
| 32 | + } |
| 33 | + |
| 34 | + ret = nvmem_cell_write(&cell0, buf, 0, sizeof(buf)); |
| 35 | + zassert_ok(ret, "Failed to write NVMEM"); |
| 36 | + |
| 37 | + memset(buf, 0, sizeof(buf)); |
| 38 | + |
| 39 | + ret = nvmem_cell_read(&cell0, buf, 0, sizeof(buf)); |
| 40 | + zassert_ok(ret, "Failed to read NVMEM"); |
| 41 | + |
| 42 | + for (size_t i = 0; i < sizeof(buf); ++i) { |
| 43 | + zexpect_equal(buf[i], 2 * i); |
| 44 | + } |
| 45 | + |
| 46 | + ret = nvmem_cell_write(&cell10, buf, 0, sizeof(buf)); |
| 47 | + zassert_equal(ret, -EROFS, "Expected read-only NVMEM"); |
| 48 | +} |
| 49 | + |
| 50 | +ZTEST_SUITE(nvmem_api, NULL, NULL, NULL, NULL, NULL); |
0 commit comments