@@ -136,13 +136,14 @@ typedef int16_t device_handle_t;
136
136
#endif
137
137
138
138
/**
139
- * @brief Create a device object and set it up for boot time initialization.
139
+ * @brief Create a device object and set it up for boot time initialization,
140
+ * with de-init capabilities.
140
141
*
141
142
* This macro defines a @ref device that is automatically configured by the
142
143
* kernel during system initialization. This macro should only be used when the
143
144
* device is not being allocated from a devicetree node. If you are allocating a
144
- * device from a devicetree node, use DEVICE_DT_DEFINE () or
145
- * DEVICE_DT_INST_DEFINE () instead.
145
+ * device from a devicetree node, use DEVICE_DT_DEINIT_DEFINE () or
146
+ * DEVICE_DT_INST_DEINIT_DEFINE () instead.
146
147
*
147
148
* @param dev_id A unique token which is used in the name of the global device
148
149
* structure as a C identifier.
@@ -152,6 +153,9 @@ typedef int16_t device_handle_t;
152
153
* (including terminating `NULL`) in order to be looked up from user mode.
153
154
* @param init_fn Pointer to the device's initialization function, which will be
154
155
* run by the kernel during system initialization. Can be `NULL`.
156
+ * @param deinit_fn Pointer to the device's de-initialization function. Can be
157
+ * `NULL`. It must release any acquired resources (e.g. pins, bus, clock...) and
158
+ * leave the device in its reset state.
155
159
* @param pm Pointer to the device's power management resources, a
156
160
* @ref pm_device, which will be stored in @ref device.pm field. Use `NULL` if
157
161
* the device does not use PM.
@@ -165,13 +169,23 @@ typedef int16_t device_handle_t;
165
169
* SYS_INIT() for details.
166
170
* @param api Pointer to the device's API structure. Can be `NULL`.
167
171
*/
168
- #define DEVICE_DEFINE (dev_id , name , init_fn , pm , data , config , level , prio , \
169
- api ) \
172
+ #define DEVICE_DEINIT_DEFINE (dev_id , name , init_fn , deinit_fn , pm , data , \
173
+ config , level , prio , api ) \
170
174
Z_DEVICE_STATE_DEFINE(dev_id); \
171
- Z_DEVICE_DEFINE(DT_INVALID_NODE, dev_id, name, init_fn, NULL , 0U, pm, \
172
- data, config, level, prio, api, \
175
+ Z_DEVICE_DEFINE(DT_INVALID_NODE, dev_id, name, init_fn, deinit_fn , 0U, \
176
+ pm, data, config, level, prio, api, \
173
177
&Z_DEVICE_STATE_NAME(dev_id))
174
178
179
+ /**
180
+ * @brief Create a device object and set it up for boot time initialization.
181
+ *
182
+ * @see DEVICE_DEINIT_DEFINE()
183
+ */
184
+ #define DEVICE_DEFINE (dev_id , name , init_fn , pm , data , config , level , prio , \
185
+ api ) \
186
+ DEVICE_DEINIT_DEFINE(dev_id, name, init_fn, NULL, pm, data, config, \
187
+ level, prio, api)
188
+
175
189
/**
176
190
* @brief Return a string name for a devicetree node.
177
191
*
@@ -214,6 +228,9 @@ typedef int16_t device_handle_t;
214
228
* @param node_id The devicetree node identifier.
215
229
* @param init_fn Pointer to the device's initialization function, which will be
216
230
* run by the kernel during system initialization. Can be `NULL`.
231
+ * @param deinit_fn Pointer to the device's de-initialization function. Can be
232
+ * `NULL`. It must release any acquired resources (e.g. pins, bus, clock...) and
233
+ * leave the device in its reset state.
217
234
* @param pm Pointer to the device's power management resources, a
218
235
* @ref pm_device, which will be stored in @ref device.pm. Use `NULL` if the
219
236
* device does not use PM.
@@ -227,16 +244,38 @@ typedef int16_t device_handle_t;
227
244
* SYS_INIT() for details.
228
245
* @param api Pointer to the device's API structure. Can be `NULL`.
229
246
*/
230
- #define DEVICE_DT_DEFINE (node_id , init_fn , pm , data , config , level , prio , api , \
231
- ...) \
247
+ #define DEVICE_DT_DEINIT_DEFINE (node_id , init_fn , deinit_fn , pm , data , config , \
248
+ level , prio , api , ...) \
232
249
Z_DEVICE_STATE_DEFINE(Z_DEVICE_DT_DEV_ID(node_id)); \
233
250
Z_DEVICE_DEFINE(node_id, Z_DEVICE_DT_DEV_ID(node_id), \
234
- DEVICE_DT_NAME(node_id), init_fn, NULL, \
251
+ DEVICE_DT_NAME(node_id), init_fn, deinit_fn, \
235
252
Z_DEVICE_DT_FLAGS(node_id), pm, data, config, level, \
236
253
prio, api, \
237
254
&Z_DEVICE_STATE_NAME(Z_DEVICE_DT_DEV_ID(node_id)), \
238
255
__VA_ARGS__)
239
256
257
+ /**
258
+ * @brief Create a device object from a devicetree node identifier and set it up
259
+ * for boot time initialization.
260
+ *
261
+ * @see DEVICE_DT_DEINIT_DEFINE()
262
+ */
263
+ #define DEVICE_DT_DEFINE (node_id , init_fn , pm , data , config , level , prio , api , \
264
+ ...) \
265
+ DEVICE_DT_DEINIT_DEFINE(node_id, init_fn, NULL, pm, data, config, \
266
+ level, prio, api, __VA_ARGS__)
267
+
268
+ /**
269
+ * @brief Like DEVICE_DT_DEINIT_DEFINE(), but uses an instance of a
270
+ * `DT_DRV_COMPAT` compatible instead of a node identifier.
271
+ *
272
+ * @param inst Instance number. The `node_id` argument to DEVICE_DT_DEFINE() is
273
+ * set to `DT_DRV_INST(inst)`.
274
+ * @param ... Other parameters as expected by DEVICE_DT_DEFINE().
275
+ */
276
+ #define DEVICE_DT_INST_DEINIT_DEFINE (inst , ...) \
277
+ DEVICE_DT_DEINIT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
278
+
240
279
/**
241
280
* @brief Like DEVICE_DT_DEFINE(), but uses an instance of a `DT_DRV_COMPAT`
242
281
* compatible instead of a node identifier.
0 commit comments