Skip to content

Commit 51e774a

Browse files
henrikbrixandersencfriedt
authored andcommitted
device: add DEVICE_DT_GET_ONE macro
Add DEVICE_DT_GET_ONE() macro. This macro is similar to DEVICE_DT_GET_ANY() but instead of returning NULL if no enabled, compatible device instance is found, it throws a compilation error. This is useful for compile-time checks in samples. Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
1 parent de13034 commit 51e774a

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

include/device.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,29 @@ typedef int16_t device_handle_t;
277277
(DEVICE_DT_GET(DT_COMPAT_GET_ANY_STATUS_OKAY(compat))), \
278278
(NULL))
279279

280+
/**
281+
* @def DEVICE_DT_GET_ONE
282+
*
283+
* @brief Obtain a pointer to a device object by devicetree compatible
284+
*
285+
* If any enabled devicetree node has the given compatible and a
286+
* device object was created from it, this returns that device.
287+
*
288+
* If there no such devices, this throws a compilation error.
289+
*
290+
* If there are multiple, this returns an arbitrary one.
291+
*
292+
* If this returns non-NULL, the device must be checked for readiness
293+
* before use, e.g. with device_is_ready().
294+
*
295+
* @param compat lowercase-and-underscores devicetree compatible
296+
* @return a pointer to a device
297+
*/
298+
#define DEVICE_DT_GET_ONE(compat) \
299+
COND_CODE_1(DT_HAS_COMPAT_STATUS_OKAY(compat), \
300+
(DEVICE_DT_GET(DT_COMPAT_GET_ANY_STATUS_OKAY(compat))), \
301+
(ZERO_OR_COMPILE_ERROR(0)))
302+
280303
/**
281304
* @def DEVICE_GET
282305
*

0 commit comments

Comments
 (0)