device: add DEVICE_DT_GET_OR_NULL utility macro

Add a new utility macro to obtain an optional reference to a device. If
the provided node_id is not enabled, the macro falls back to NULL.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
This commit is contained in:
Gerard Marull-Paretas 2021-09-06 15:45:34 +02:00 committed by Christopher Friedt
commit 4df1d15618

View file

@ -301,6 +301,19 @@ typedef int16_t device_handle_t;
(DEVICE_DT_GET(DT_COMPAT_GET_ANY_STATUS_OKAY(compat))), \
(ZERO_OR_COMPILE_ERROR(0)))
/**
* @def DEVICE_DT_GET_OR_NULL
*
* @brief Utility macro to obtain an optional reference to a device.
*
* @param node_id Node identifier.
*
* @return Pointer to a device if it exists, NULL otherwise.
*/
#define DEVICE_DT_GET_OR_NULL(node_id) \
COND_CODE_1(DT_NODE_HAS_STATUS(node_id, okay), \
(DEVICE_DT_GET(node_id)), (NULL))
/**
* @def DEVICE_GET
*