Skip to content

Commit

Permalink
tests: lib: devicetree: api: Add test_fixed_partitions
Browse files Browse the repository at this point in the history
Add test suite for the DT fixed-partitions API. It is verified on two
kinds of MTD nodes, one of which is meant to represent external memory.

Signed-off-by: Grzegorz Swiderski <[email protected]>
  • Loading branch information
57300 authored and carlescufi committed Aug 7, 2023
1 parent b69be2c commit 04a7fcf
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
39 changes: 39 additions & 0 deletions tests/lib/devicetree/api/app.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,45 @@
zephyr,memory-attr = "RAM_NOCACHE";
};

test-mtd@ffeeddcc {
reg = < 0x0 0x1000 >;
#address-cells = < 1 >;
#size-cells = < 1 >;

flash@20000000 {
compatible = "soc-nv-flash";
reg = < 0x20000000 0x100 >;
partitions {
compatible = "fixed-partitions";
#address-cells = < 1 >;
#size-cells = < 1 >;

partition@0 {
reg = < 0x0 0xc0 >;
label = "test-partition-0";
};
partition@c0 {
reg = < 0xc0 0x40 >;
label = "test-partition-1";
};
};
};
};

test-mtd@33221100 {
reg = < 0x33221100 0x1000 >;

partitions {
compatible = "fixed-partitions";
#address-cells = < 1 >;
#size-cells = < 1 >;

partition@6ff80 {
reg = < 0x6ff80 0x80 >;
label = "test-partition-2";
};
};
};
};

test_64 {
Expand Down
81 changes: 81 additions & 0 deletions tests/lib/devicetree/api/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@
#define TEST_RANGES_OTHER DT_NODELABEL(test_ranges_other)
#define TEST_RANGES_EMPTY DT_NODELABEL(test_ranges_empty)

#define TEST_MTD_0 DT_PATH(test, test_mtd_ffeeddcc)
#define TEST_MTD_1 DT_PATH(test, test_mtd_33221100)

#define TEST_MEM_0 DT_CHILD(TEST_MTD_0, flash_20000000)

#define TEST_PARTITION_0 DT_PATH(test, test_mtd_ffeeddcc, flash_20000000, partitions, partition_0)
#define TEST_PARTITION_1 DT_PATH(test, test_mtd_ffeeddcc, flash_20000000, partitions, partition_c0)
#define TEST_PARTITION_2 DT_PATH(test, test_mtd_33221100, partitions, partition_6ff80)

#define ZEPHYR_USER DT_PATH(zephyr_user)

#define TA_HAS_COMPAT(compat) DT_NODE_HAS_COMPAT(TEST_ARRAYS, compat)
Expand Down Expand Up @@ -2772,6 +2781,78 @@ ZTEST(devicetree_api, test_memory_attr)
#undef TEST_FUNC
}

ZTEST(devicetree_api, test_fixed_partitions)
{
/* Test finding fixed partitions by the 'label' property. */
zassert_true(DT_HAS_FIXED_PARTITION_LABEL(test_partition_0));
zassert_true(DT_HAS_FIXED_PARTITION_LABEL(test_partition_1));
zassert_true(DT_HAS_FIXED_PARTITION_LABEL(test_partition_2));

zassert_true(DT_SAME_NODE(TEST_PARTITION_0,
DT_NODE_BY_FIXED_PARTITION_LABEL(test_partition_0)));
zassert_true(DT_SAME_NODE(TEST_PARTITION_1,
DT_NODE_BY_FIXED_PARTITION_LABEL(test_partition_1)));
zassert_true(DT_SAME_NODE(TEST_PARTITION_2,
DT_NODE_BY_FIXED_PARTITION_LABEL(test_partition_2)));

zassert_true(DT_FIXED_PARTITION_EXISTS(TEST_PARTITION_0));
zassert_true(DT_FIXED_PARTITION_EXISTS(TEST_PARTITION_1));
zassert_true(DT_FIXED_PARTITION_EXISTS(TEST_PARTITION_2));

/* There should not be a node with `label = "test_partition_3"`. */
zassert_false(DT_HAS_FIXED_PARTITION_LABEL(test_partition_3));
zassert_false(DT_NODE_EXISTS(DT_NODE_BY_FIXED_PARTITION_LABEL(test_partition_3)));

/* There is a node with `label = "TEST_GPIO_0"`, but it is not a fixed partition. */
zassert_false(DT_HAS_FIXED_PARTITION_LABEL(TEST_GPIO_0));
zassert_false(DT_NODE_EXISTS(DT_NODE_BY_FIXED_PARTITION_LABEL(TEST_GPIO_0)));

/* Test DT_MTD_FROM_FIXED_PARTITION. */
zassert_true(DT_NODE_EXISTS(DT_MTD_FROM_FIXED_PARTITION(TEST_PARTITION_0)));
zassert_true(DT_NODE_EXISTS(DT_MTD_FROM_FIXED_PARTITION(TEST_PARTITION_1)));
zassert_true(DT_NODE_EXISTS(DT_MTD_FROM_FIXED_PARTITION(TEST_PARTITION_2)));

zassert_true(DT_SAME_NODE(TEST_MTD_0, DT_MTD_FROM_FIXED_PARTITION(TEST_PARTITION_0)));
zassert_true(DT_SAME_NODE(TEST_MTD_0, DT_MTD_FROM_FIXED_PARTITION(TEST_PARTITION_1)));
zassert_true(DT_SAME_NODE(TEST_MTD_1, DT_MTD_FROM_FIXED_PARTITION(TEST_PARTITION_2)));

/* Test DT_MEM_FROM_FIXED_PARTITION. */
zassert_true(DT_NODE_EXISTS(DT_MEM_FROM_FIXED_PARTITION(TEST_PARTITION_0)));
zassert_true(DT_NODE_EXISTS(DT_MEM_FROM_FIXED_PARTITION(TEST_PARTITION_1)));
zassert_false(DT_NODE_EXISTS(DT_MEM_FROM_FIXED_PARTITION(TEST_PARTITION_2)));

zassert_true(DT_SAME_NODE(TEST_MEM_0, DT_MEM_FROM_FIXED_PARTITION(TEST_PARTITION_0)));
zassert_true(DT_SAME_NODE(TEST_MEM_0, DT_MEM_FROM_FIXED_PARTITION(TEST_PARTITION_1)));

/* Test DT_FIXED_PARTITION_ADDR. */
zassert_equal(DT_FIXED_PARTITION_ADDR(TEST_PARTITION_0), 0x20000000);
zassert_equal(DT_FIXED_PARTITION_ADDR(TEST_PARTITION_1), 0x200000c0);

/* DT_FIXED_PARTITION_ADDR(TEST_PARTITION_2) expands to an invalid expression.
* Test this by way of string comparison.
*/
zassert_true(!strcmp(TO_STRING(DT_FIXED_PARTITION_ADDR(TEST_PARTITION_2)),
"(__REG_IDX_0_VAL_ADDRESS + 458624)"));
zassert_equal(DT_REG_ADDR(TEST_PARTITION_2), 458624);

/* Test that all DT_FIXED_PARTITION_ID are defined and unique. */
#define FIXED_PARTITION_ID_COMMA(node_id) DT_FIXED_PARTITION_ID(node_id),

static const int ids[] = {
DT_FOREACH_STATUS_OKAY_VARGS(fixed_partitions, DT_FOREACH_CHILD,
FIXED_PARTITION_ID_COMMA)
};
bool found[ARRAY_SIZE(ids)] = { false };

for (int i = 0; i < ARRAY_SIZE(ids); i++) {
zassert_between_inclusive(ids[i], 0, ARRAY_SIZE(ids) - 1, "");
zassert_false(found[ids[i]]);
found[ids[i]] = true;
}

#undef FIXED_PARTITION_ID_COMMA
}

ZTEST(devicetree_api, test_string_token)
{
#undef DT_DRV_COMPAT
Expand Down

0 comments on commit 04a7fcf

Please sign in to comment.