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>
This commit is contained in:
Henrik Brix Andersen 2021-07-01 22:02:10 +02:00 committed by Christopher Friedt
commit 51e774a3f4

View file

@ -277,6 +277,29 @@ typedef int16_t device_handle_t;
(DEVICE_DT_GET(DT_COMPAT_GET_ANY_STATUS_OKAY(compat))), \
(NULL))
/**
* @def DEVICE_DT_GET_ONE
*
* @brief Obtain a pointer to a device object by devicetree compatible
*
* If any enabled devicetree node has the given compatible and a
* device object was created from it, this returns that device.
*
* If there no such devices, this throws a compilation error.
*
* If there are multiple, this returns an arbitrary one.
*
* If this returns non-NULL, the device must be checked for readiness
* before use, e.g. with device_is_ready().
*
* @param compat lowercase-and-underscores devicetree compatible
* @return a pointer to a device
*/
#define DEVICE_DT_GET_ONE(compat) \
COND_CODE_1(DT_HAS_COMPAT_STATUS_OKAY(compat), \
(DEVICE_DT_GET(DT_COMPAT_GET_ANY_STATUS_OKAY(compat))), \
(ZERO_OR_COMPILE_ERROR(0)))
/**
* @def DEVICE_GET
*