Skip to content

Commit 98b6e4f

Browse files
committed
devicetree: generate extern's for devicetree struct devices
Generate a header (device_extern.h) that handles extern of possible device structs that would come from devicetree. This removes the need for DEVICE_DT_DECLARE and DEVICE_DT_INST_DECLARE which we can remove. Signed-off-by: Kumar Gala <[email protected]>
1 parent 09e9736 commit 98b6e4f

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

cmake/dts.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ set(ZEPHYR_DTS ${PROJECT_BINARY_DIR}/zephyr.dts)
2121
# and should not be made part of the documentation.
2222
set(EDT_PICKLE ${PROJECT_BINARY_DIR}/edt.pickle)
2323
set(DEVICETREE_UNFIXED_H ${PROJECT_BINARY_DIR}/include/generated/devicetree_unfixed.h)
24+
set(DEVICE_EXTERN_H ${PROJECT_BINARY_DIR}/include/generated/device_extern.h)
2425
set(DTS_POST_CPP ${PROJECT_BINARY_DIR}/${BOARD}.dts.pre.tmp)
2526

2627
set_ifndef(DTS_SOURCE ${BOARD_DIR}/${BOARD}.dts)
@@ -214,6 +215,7 @@ if(SUPPORTS_DTS)
214215
--dtc-flags '${EXTRA_DTC_FLAGS}'
215216
--bindings-dirs ${DTS_ROOT_BINDINGS}
216217
--header-out ${DEVICETREE_UNFIXED_H}
218+
--device-header-out ${DEVICE_EXTERN_H}
217219
--dts-out ${ZEPHYR_DTS} # As a debugging aid
218220
--edt-pickle-out ${EDT_PICKLE}
219221
)
@@ -228,6 +230,7 @@ if(SUPPORTS_DTS)
228230
else()
229231
message(STATUS "Generated zephyr.dts: ${ZEPHYR_DTS}")
230232
message(STATUS "Generated devicetree_unfixed.h: ${DEVICETREE_UNFIXED_H}")
233+
message(STATUS "Generated device_extern.h: ${DEVICE_EXTERN_H}")
231234
endif()
232235

233236
# A file that used to be generated by 'dtc'. zephyr.dts is the new
@@ -237,4 +240,5 @@ if(SUPPORTS_DTS)
237240

238241
else()
239242
file(WRITE ${DEVICETREE_UNFIXED_H} "/* WARNING. THIS FILE IS AUTO-GENERATED. DO NOT MODIFY! */")
243+
file(WRITE ${DEVICE_EXTERN_H} "/* WARNING. THIS FILE IS AUTO-GENERATED. DO NOT MODIFY! */")
240244
endif(SUPPORTS_DTS)

include/device.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,9 @@ static inline int device_pm_put_sync(const struct device *dev) { return -ENOTSUP
767767
}
768768
#endif
769769

770+
/* device_extern is generated base on devicetree nodes */
771+
#include <device_extern.h>
772+
770773
#include <syscalls/device.h>
771774

772775
#endif /* ZEPHYR_INCLUDE_DEVICE_H_ */

scripts/dts/gen_defines.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,35 @@ def main():
110110
write_chosen(edt)
111111
write_global_compat_info(edt)
112112

113+
write_device_extern_header(args.device_header_out, edt)
114+
113115
if args.edt_pickle_out:
114116
write_pickled_edt(edt, args.edt_pickle_out)
115117

118+
119+
def write_device_extern_header(device_header_out, edt):
120+
# Generate header that will extern devicetree struct device's
121+
122+
with open(device_header_out, "w", encoding="utf-8") as dev_header_file:
123+
print("#ifndef DEVICE_EXTERN_GEN_H", file=dev_header_file)
124+
print("#define DEVICE_EXTERN_GEN_H", file=dev_header_file)
125+
print("", file=dev_header_file)
126+
print("#ifdef __cplusplus", file=dev_header_file)
127+
print('extern "C" {', file=dev_header_file)
128+
print("#endif", file=dev_header_file)
129+
print("", file=dev_header_file)
130+
131+
for node in sorted(edt.nodes, key=lambda node: node.dep_ordinal):
132+
print(f"extern const struct device DEVICE_DT_NAME_GET(DT_{node.z_path_id});", file=dev_header_file)
133+
134+
print("", file=dev_header_file)
135+
print("#ifdef __cplusplus", file=dev_header_file)
136+
print("}", file=dev_header_file)
137+
print("#endif", file=dev_header_file)
138+
print("", file=dev_header_file)
139+
print("#endif /* DEVICE_EXTERN_GEN_H */", file=dev_header_file)
140+
141+
116142
def setup_edtlib_logging():
117143
# The edtlib module emits logs using the standard 'logging' module.
118144
# Configure it so that warnings and above are printed to stderr,
@@ -159,6 +185,8 @@ def parse_args():
159185
parser.add_argument("--dts-out", required=True,
160186
help="path to write merged DTS source code to (e.g. "
161187
"as a debugging aid)")
188+
parser.add_argument("--device-header-out", required=True,
189+
help="path to write device struct extern header to")
162190
parser.add_argument("--edt-pickle-out",
163191
help="path to write pickled edtlib.EDT object to")
164192

0 commit comments

Comments
 (0)