zephyrproject-rtos / zephyr

Primary Git Repository for the Zephyr Project. Zephyr is a new generation, scalable, optimized, secure RTOS for multiple hardware architectures.
https://docs.zephyrproject.org
Apache License 2.0
10.44k stars 6.4k forks source link

DT Macro for counting nodes #38715

Open aaronemassey opened 2 years ago

aaronemassey commented 2 years ago

Is your feature request related to a problem? Please describe.

I have a use-case where I need to know the length of children nodes or nodes with a compatible string.

Describe the solution you'd like

Two DT macros like:

Describe alternatives you've considered Currently my solution is to write macro functions like:

#define _COUNT_STATUS_OKAY_COUNTER() 1 +
#define COUNT_STATUS_OKAY(compat) (DT_FOREACH_STATUS_OKAY(compat, _COUNT_STATUS_OKAY_COUNTER) 0)

But this seems kind of ugly and instead something that maybe should be usable from the DT macros.

Additional context I don't think any additional context is needed besides the example I supplied.

Thank you for all the work that has been put into Zephyr.

@yperess is also interested.

aaronemassey commented 2 years ago

I'll have a tiny pull-request for this fairly soon.

TomzBench commented 1 year ago

I currently embed a struct of dt_spec nodes which represent ethernet ports for a switch product that can have N number of ports. So this feature allows me to embed these nodes properly.

mbolivar-nordic commented 1 year ago

@aaronemassey thanks for volunteering to add this. Please use DT_NUM_ instead of DT_COUNT_ for consistency:

$ rg '#define DT_NUM' include/zephyr/devicetree.h
1620:#define DT_NUM_RANGES(node_id) DT_CAT(node_id, _RANGES_NUM)
2146:#define DT_NUM_REGS(node_id) DT_CAT(node_id, _REG_NUM)
2239:#define DT_NUM_IRQS(node_id) DT_CAT(node_id, _IRQ_NUM)
2955:#define DT_NUM_INST_STATUS_OKAY(compat)            \
mbolivar-nordic commented 1 year ago

I think you only need DT_NUM_CHILDREN, and DT_NUM_INST_STATUS_OKAY already exists for your other example.

TomzBench commented 1 year ago

I think this strategy does not work when passing to LISTIFY however...

aaronemassey commented 1 year ago

I'll have a tiny pull-request for this fairly soon.

Sorry for letting this drop. It only recently came back on my radar due to the issue activity :) Looking to have a PR up this or next week.

swift-tk commented 4 months ago

Hi @aaronemassey Is there a PR? I'm in need of this too.

aaronemassey commented 4 months ago

Hi @aaronemassey Is there a PR? I'm in need of this too.

Bah, I should never commit to things so quickly like in https://github.com/zephyrproject-rtos/zephyr/issues/38715#issuecomment-1489242523. Things have been busy, but we're all busy :)

Will try to have something by EO this week/end. @swift-tk, you're welcome to add it if you want it sooner. It should basically be @mbolivar-nordic suggestions in https://github.com/zephyrproject-rtos/zephyr/issues/38715#issuecomment-1474433804.

swift-tk commented 4 months ago

Hi @aaronemassey I think I have a better solution see https://github.com/zephyrproject-rtos/zephyr/pull/71933.