9
9
#include <zephyr/device.h>
10
10
#include <zephyr/drivers/sensor.h>
11
11
#include <zephyr/drivers/gpio.h>
12
+ #include <zephyr/pm/device_runtime.h>
12
13
13
14
static K_SEM_DEFINE (sem , 0 , 1 ) ;
14
15
static const struct gpio_dt_spec phase_a = GPIO_DT_SPEC_GET (DT_ALIAS (qenca ), gpios );
@@ -144,6 +145,10 @@ ZTEST(qdec_sensor, test_sensor_trigger_set_and_disable)
144
145
{
145
146
int rc ;
146
147
148
+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
149
+ pm_device_runtime_get (qdec_dev );
150
+ }
151
+
147
152
qdec_trigger .type = SENSOR_TRIG_DATA_READY ;
148
153
qdec_trigger .chan = SENSOR_CHAN_ALL ;
149
154
rc = sensor_trigger_set (qdec_dev , & qdec_trigger , qdec_trigger_handler );
@@ -162,10 +167,18 @@ ZTEST(qdec_sensor, test_sensor_trigger_set_and_disable)
162
167
/* emulation not working, but there maybe old trigger, ignore */
163
168
rc = k_sem_take (& sem , K_MSEC (200 ));
164
169
170
+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
171
+ pm_device_runtime_put (qdec_dev );
172
+ }
173
+
165
174
/* there should be no triggers now*/
166
175
rc = k_sem_take (& sem , K_MSEC (200 ));
167
176
zassert_true (rc == - EAGAIN , "qdec handler should not be triggered (%d)" , rc );
168
177
178
+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
179
+ pm_device_runtime_get (qdec_dev );
180
+ }
181
+
169
182
/* register empty trigger - disable trigger */
170
183
rc = sensor_trigger_set (qdec_dev , & qdec_trigger , NULL );
171
184
zassert_true (rc == 0 , "sensor_trigger_set failed: %d" , rc );
@@ -175,6 +188,10 @@ ZTEST(qdec_sensor, test_sensor_trigger_set_and_disable)
175
188
/* emulation working, but handler not set, thus should not be called */
176
189
rc = k_sem_take (& sem , K_MSEC (200 ));
177
190
zassert_true (rc == - EAGAIN , "qdec handler should not be triggered (%d)" , rc );
191
+
192
+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
193
+ pm_device_runtime_put (qdec_dev );
194
+ }
178
195
}
179
196
180
197
/**
@@ -188,6 +205,10 @@ ZTEST(qdec_sensor, test_sensor_trigger_set)
188
205
int rc ;
189
206
struct sensor_value val = {0 };
190
207
208
+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
209
+ pm_device_runtime_get (qdec_dev );
210
+ }
211
+
191
212
qdec_trigger .type = SENSOR_TRIG_DATA_READY ;
192
213
qdec_trigger .chan = SENSOR_CHAN_ROTATION ;
193
214
rc = sensor_trigger_set (qdec_dev , & qdec_trigger , qdec_trigger_handler );
@@ -209,6 +230,10 @@ ZTEST(qdec_sensor, test_sensor_trigger_set)
209
230
210
231
TC_PRINT ("QDEC reading: %d\n" , val .val1 );
211
232
zassert_true (val .val1 != 0 , "No readings from QDEC" );
233
+
234
+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
235
+ pm_device_runtime_put (qdec_dev );
236
+ }
212
237
}
213
238
214
239
/**
@@ -221,6 +246,10 @@ ZTEST(qdec_sensor, test_sensor_trigger_set_negative)
221
246
{
222
247
int rc ;
223
248
249
+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
250
+ pm_device_runtime_get (qdec_dev );
251
+ }
252
+
224
253
rc = sensor_trigger_set (qdec_dev , & qdec_trigger , qdec_trigger_handler );
225
254
zassume_true (rc != - ENOSYS , "sensor_trigger_set not supported" );
226
255
@@ -235,6 +264,10 @@ ZTEST(qdec_sensor, test_sensor_trigger_set_negative)
235
264
236
265
rc = sensor_trigger_set (qdec_dev , & qdec_trigger , qdec_trigger_handler );
237
266
zassume_true (rc < 0 , "sensor_trigger_set should fail due to invalid channel" );
267
+
268
+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
269
+ pm_device_runtime_put (qdec_dev );
270
+ }
238
271
}
239
272
240
273
/**
@@ -245,11 +278,19 @@ ZTEST(qdec_sensor, test_sensor_trigger_set_negative)
245
278
*/
246
279
ZTEST (qdec_sensor , test_qdec_readings )
247
280
{
281
+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
282
+ pm_device_runtime_get (qdec_dev );
283
+ }
284
+
248
285
qenc_emulate_verify_reading (10 , 100 , true, false);
249
286
qenc_emulate_verify_reading (2 , 500 , true, false);
250
287
qenc_emulate_verify_reading (10 , 200 , false, false);
251
288
qenc_emulate_verify_reading (1 , 1000 , false, true);
252
289
qenc_emulate_verify_reading (1 , 1000 , true, true);
290
+
291
+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
292
+ pm_device_runtime_put (qdec_dev );
293
+ }
253
294
}
254
295
255
296
/**
@@ -263,9 +304,16 @@ ZTEST(qdec_sensor, test_sensor_channel_get_empty)
263
304
int rc ;
264
305
struct sensor_value val = {0 };
265
306
307
+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
308
+ pm_device_runtime_get (qdec_dev );
309
+ }
310
+
266
311
rc = sensor_sample_fetch (qdec_dev );
267
312
zassert_true (rc == 0 , "Failed to fetch sample (%d)" , rc );
268
313
314
+ /* wait for potential new readings */
315
+ k_msleep (100 );
316
+
269
317
/* get readings but ignore them, as they may include reading from time
270
318
* when emulation was still working (i.e. during previous test)
271
319
*/
@@ -283,6 +331,10 @@ ZTEST(qdec_sensor, test_sensor_channel_get_empty)
283
331
zassert_true (rc == 0 , "Failed to get sample (%d)" , rc );
284
332
zassert_true (val .val1 == 0 , "Expected no readings but got: %d" , val .val1 );
285
333
zassert_true (val .val2 == 0 , "Expected no readings but got: %d" , val .val2 );
334
+
335
+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
336
+ pm_device_runtime_put (qdec_dev );
337
+ }
286
338
}
287
339
288
340
/**
@@ -297,6 +349,10 @@ ZTEST(qdec_sensor, test_sensor_channel_get)
297
349
struct sensor_value val_first = {0 };
298
350
struct sensor_value val_second = {0 };
299
351
352
+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
353
+ pm_device_runtime_get (qdec_dev );
354
+ }
355
+
300
356
qenc_emulate_start (K_MSEC (10 ), true);
301
357
302
358
/* wait for some readings*/
@@ -328,6 +384,10 @@ ZTEST(qdec_sensor, test_sensor_channel_get)
328
384
"Expected the same readings: %d vs %d" ,
329
385
val_first .val2 ,
330
386
val_second .val2 );
387
+
388
+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
389
+ pm_device_runtime_put (qdec_dev );
390
+ }
331
391
}
332
392
333
393
/**
@@ -341,6 +401,10 @@ ZTEST(qdec_sensor, test_sensor_channel_get_negative)
341
401
int rc ;
342
402
struct sensor_value val = {0 };
343
403
404
+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
405
+ pm_device_runtime_get (qdec_dev );
406
+ }
407
+
344
408
qenc_emulate_start (K_MSEC (10 ), true);
345
409
346
410
/* wait for some readings*/
@@ -353,6 +417,10 @@ ZTEST(qdec_sensor, test_sensor_channel_get_negative)
353
417
zassert_true (rc < 0 , "Should failed to get sample (%d)" , rc );
354
418
zassert_true (val .val1 == 0 , "Some readings from QDEC: %d" , val .val1 );
355
419
zassert_true (val .val2 == 0 , "Some readings from QDEC: %d" , val .val2 );
420
+
421
+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
422
+ pm_device_runtime_put (qdec_dev );
423
+ }
356
424
}
357
425
358
426
/**
@@ -365,6 +433,10 @@ ZTEST(qdec_sensor, test_sensor_sample_fetch)
365
433
{
366
434
int rc ;
367
435
436
+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
437
+ pm_device_runtime_get (qdec_dev );
438
+ }
439
+
368
440
rc = sensor_sample_fetch (qdec_dev );
369
441
zassert_true (rc == 0 , "Failed to fetch sample (%d)" , rc );
370
442
@@ -373,6 +445,10 @@ ZTEST(qdec_sensor, test_sensor_sample_fetch)
373
445
374
446
rc = sensor_sample_fetch_chan (qdec_dev , SENSOR_CHAN_MAX );
375
447
zassert_true (rc < 0 , "Should fail to fetch sample from invalid channel (%d)" , rc );
448
+
449
+ if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
450
+ pm_device_runtime_put (qdec_dev );
451
+ }
376
452
}
377
453
378
454
static void * setup (void )
0 commit comments