Skip to content

Commit 77a403c

Browse files
mbolivar-nordicgalak
authored andcommitted
cmake: extensions: add NAME parameter to DT register helpers
Allow register address and size access by name. Example devicetree fragment: / { foo@deadbeef { reg = <0xdeadbeef 0x1000>, <0xfeedface 0x2000>; reg-names = "bar", "baz"; }; }; Example usage: dt_reg_addr(bar_addr PATH "/foo@deadbeef" NAME bar) dt_reg_size(bar_size PATH "/foo@deadbeef" NAME bar) dt_reg_addr(baz_addr PATH "/foo@deadbeef" NAME baz) dt_reg_size(baz_size PATH "/foo@deadbeef" NAME baz) Results: - bar_addr is 0xdeadbeef - bar_size is 0x1000 - baz_addr is 0xfeedface - baz_size is 0x2000 Signed-off-by: Martí Bolívar <[email protected]>
1 parent 843ea97 commit 77a403c

File tree

1 file changed

+44
-13
lines changed

1 file changed

+44
-13
lines changed

cmake/extensions.cmake

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2859,10 +2859,11 @@ function(dt_num_regs var)
28592859
endfunction()
28602860

28612861
# Usage:
2862-
# dt_reg_addr(<var> PATH <path> [INDEX <idx>])
2862+
# dt_reg_addr(<var> PATH <path> [INDEX <idx>] [NAME <name>])
28632863
#
2864-
# Get the base address of the register block at index <idx>.
2865-
# If <idx> is omitted, then the value at index 0 will be returned.
2864+
# Get the base address of the register block at index <idx>, or with
2865+
# name <name>. If <idx> and <name> are both omitted, the value at
2866+
# index 0 will be returned. Do not give both <idx> and <name>.
28662867
#
28672868
# The value will be returned in the <var> parameter.
28682869
#
@@ -2875,14 +2876,15 @@ endfunction()
28752876
# Results can be:
28762877
# - The base address of the register block
28772878
# - <var> will be undefined if node does not exists or does not have a register
2878-
# block at the requested index.
2879+
# block at the requested index or with the requested name
28792880
#
28802881
# <var> : Return variable where the address value will be stored
28812882
# PATH <path> : Node path
2882-
# INDEX <idx> : Index number
2883+
# INDEX <idx> : Register block index number
2884+
# NAME <name> : Register block name
28832885
function(dt_reg_addr var)
28842886
set(req_single_args "PATH")
2885-
set(single_args "INDEX")
2887+
set(single_args "INDEX;NAME")
28862888
cmake_parse_arguments(DT_REG "" "${req_single_args};${single_args}" "" ${ARGN})
28872889

28882890
if(${ARGV0} IN_LIST req_single_args)
@@ -2897,8 +2899,16 @@ function(dt_reg_addr var)
28972899
endif()
28982900
endforeach()
28992901

2900-
if(NOT DEFINED DT_REG_INDEX)
2902+
if(DEFINED DT_REG_INDEX AND DEFINED DT_REG_NAME)
2903+
message(FATAL_ERROR "dt_reg_addr(${ARGV0} ...) given both INDEX and NAME")
2904+
elseif(NOT DEFINED DT_REG_INDEX AND NOT DEFINED DT_REG_NAME)
29012905
set(DT_REG_INDEX 0)
2906+
elseif(DEFINED DT_REG_NAME)
2907+
dt_reg_index_private(DT_REG_INDEX "${DT_REG_PATH}" "${DT_REG_NAME}")
2908+
if(DT_REG_INDEX EQUAL "-1")
2909+
set(${var} PARENT_SCOPE)
2910+
return()
2911+
endif()
29022912
endif()
29032913

29042914
dt_path_internal(canonical "${DT_REG_PATH}")
@@ -2914,10 +2924,11 @@ function(dt_reg_addr var)
29142924
endfunction()
29152925

29162926
# Usage:
2917-
# dt_reg_size(<var> PATH <path> [INDEX <idx>])
2927+
# dt_reg_size(<var> PATH <path> [INDEX <idx>] [NAME <name>])
29182928
#
2919-
# Get the size of the register block at index <idx>.
2920-
# If INDEX is omitted, then the value at index 0 will be returned.
2929+
# Get the size of the register block at index <idx>, or with
2930+
# name <name>. If <idx> and <name> are both omitted, the value at
2931+
# index 0 will be returned. Do not give both <idx> and <name>.
29212932
#
29222933
# The value will be returned in the <value> parameter.
29232934
#
@@ -2929,10 +2940,11 @@ endfunction()
29292940
#
29302941
# <var> : Return variable where the size value will be stored
29312942
# PATH <path> : Node path
2932-
# INDEX <idx> : Index number
2943+
# INDEX <idx> : Register block index number
2944+
# NAME <name> : Register block name
29332945
function(dt_reg_size var)
29342946
set(req_single_args "PATH")
2935-
set(single_args "INDEX")
2947+
set(single_args "INDEX;NAME")
29362948
cmake_parse_arguments(DT_REG "" "${req_single_args};${single_args}" "" ${ARGN})
29372949

29382950
if(${ARGV0} IN_LIST req_single_args)
@@ -2947,8 +2959,16 @@ function(dt_reg_size var)
29472959
endif()
29482960
endforeach()
29492961

2950-
if(NOT DEFINED DT_REG_INDEX)
2962+
if(DEFINED DT_REG_INDEX AND DEFINED DT_REG_NAME)
2963+
message(FATAL_ERROR "dt_reg_size(${ARGV0} ...) given both INDEX and NAME")
2964+
elseif(NOT DEFINED DT_REG_INDEX AND NOT DEFINED DT_REG_NAME)
29512965
set(DT_REG_INDEX 0)
2966+
elseif(DEFINED DT_REG_NAME)
2967+
dt_reg_index_private(DT_REG_INDEX "${DT_REG_PATH}" "${DT_REG_NAME}")
2968+
if(DT_REG_INDEX EQUAL "-1")
2969+
set(${var} PARENT_SCOPE)
2970+
return()
2971+
endif()
29522972
endif()
29532973

29542974
dt_path_internal(canonical "${DT_REG_PATH}")
@@ -2963,6 +2983,17 @@ function(dt_reg_size var)
29632983
set(${var} ${${var}} PARENT_SCOPE)
29642984
endfunction()
29652985

2986+
# Internal helper for dt_reg_addr/dt_reg_size; not meant to be used directly
2987+
function(dt_reg_index_private var path name)
2988+
dt_prop(reg_names PATH "${path}" PROPERTY "reg-names")
2989+
if(NOT DEFINED reg_names)
2990+
set(index "-1")
2991+
else()
2992+
list(FIND reg_names "${name}" index)
2993+
endif()
2994+
set(${var} "${index}" PARENT_SCOPE)
2995+
endfunction()
2996+
29662997
# Usage:
29672998
# dt_has_chosen(<var> PROPERTY <prop>)
29682999
#

0 commit comments

Comments
 (0)