@@ -130,8 +130,13 @@ static void qenc_emulate_verify_reading(struct qdec_qenc_loopback *loopback,
130130 int rc ;
131131 struct sensor_value val = {0 };
132132 int32_t expected_steps = emulation_duration_ms / emulator_period_ms ;
133- int32_t expected_reading = 360 * expected_steps / loopback -> qdec_config_step ;
134- int32_t delta = expected_reading / 4 ;
133+ int32_t expected_reading = 360 * expected_steps / config_step ;
134+ int32_t delta = expected_reading / 5 ;
135+ int8_t rotation_shift ;
136+ q31_t rotation_value ;
137+ struct sensor_chan_spec channels [1 ] = {
138+ {.chan_type = SENSOR_CHAN_ROTATION , .chan_idx = 0 },
139+ };
135140
136141 if (!forward ) {
137142 expected_reading *= -1 ;
@@ -142,16 +147,16 @@ static void qenc_emulate_verify_reading(struct qdec_qenc_loopback *loopback,
142147 /* wait for some readings */
143148 k_msleep (emulation_duration_ms );
144149
145- rc = sensor_sample_fetch (loopback -> qdec );
150+ rc = sensor_read_and_decode (loopback -> qdec , channels , ARRAY_SIZE (channels ), & rotation_shift ,
151+ & rotation_value , 1 );
146152
147153 if (!overflow_expected ) {
148154 zassert_true (rc == 0 , "Failed to fetch sample (%d)" , rc );
149155 } else {
150156 zassert_true (rc == - EOVERFLOW , "Failed to detect overflow" );
151157 }
152158
153- rc = sensor_channel_get (loopback -> qdec , SENSOR_CHAN_ROTATION , & val );
154- zassert_true (rc == 0 , "Failed to get sample (%d)" , rc );
159+ q31_to_sensor_value (rotation_value , rotation_shift , & val );
155160
156161 TC_PRINT ("Expected reading: %d, actual value: %d, delta: %d\n" ,
157162 expected_reading , val .val1 , delta );
@@ -165,11 +170,9 @@ static void qenc_emulate_verify_reading(struct qdec_qenc_loopback *loopback,
165170 /* wait and get readings to clear state */
166171 k_msleep (100 );
167172
168- rc = sensor_sample_fetch (loopback -> qdec );
169- zassert_true (rc == 0 , "Failed to fetch sample (%d)" , rc );
170-
171- rc = sensor_channel_get (loopback -> qdec , SENSOR_CHAN_ROTATION , & val );
172- zassert_true (rc == 0 , "Failed to get sample (%d)" , rc );
173+ rc = sensor_read_and_decode (loopback -> qdec , channels , ARRAY_SIZE (channels ), & rotation_shift ,
174+ & rotation_value , 1 );
175+ zassert_true (rc == 0 , "Failed to read sample (%d)" , rc );
173176}
174177
175178static void sensor_trigger_set_and_disable (struct qdec_qenc_loopback * loopback )
@@ -247,6 +250,11 @@ ZTEST(qdec_sensor, test_sensor_trigger_set_and_disable)
247250static void sensor_trigger_set_test (struct qdec_qenc_loopback * loopback )
248251{
249252 int rc ;
253+ int8_t rotation_shift ;
254+ q31_t rotation_value ;
255+ struct sensor_chan_spec channels [1 ] = {
256+ {.chan_type = SENSOR_CHAN_ROTATION , .chan_idx = 0 },
257+ };
250258 struct sensor_value val = {0 };
251259
252260 if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
@@ -266,11 +274,11 @@ static void sensor_trigger_set_test(struct qdec_qenc_loopback *loopback)
266274 rc = k_sem_take (& sem , K_MSEC (200 ));
267275 zassert_true (rc == 0 , "qdec handler should be triggered (%d)" , rc );
268276
269- rc = sensor_sample_fetch (loopback -> qdec );
270- zassert_true (rc == 0 , "Failed to fetch sample (%d)" , rc );
277+ rc = sensor_read_and_decode (loopback -> qdec , channels , ARRAY_SIZE (channels ), & rotation_shift ,
278+ & rotation_value , 1 );
279+ zassert_true (rc == 0 , "Failed to read sample (%d)" , rc );
271280
272- rc = sensor_channel_get (loopback -> qdec , SENSOR_CHAN_ROTATION , & val );
273- zassert_true (rc == 0 , "Failed to fetch sample (%d)" , rc );
281+ q31_to_sensor_value (rotation_value , rotation_shift , & val );
274282
275283 TC_PRINT ("QDEC reading: %d\n" , val .val1 );
276284 zassert_true (val .val1 != 0 , "No readings from QDEC" );
@@ -368,6 +376,11 @@ ZTEST(qdec_sensor, test_qdec_readings)
368376static void sensor_channel_get_empty (const struct device * const dev )
369377{
370378 int rc ;
379+ int8_t rotation_shift ;
380+ q31_t rotation_value ;
381+ struct sensor_chan_spec channels [1 ] = {
382+ {.chan_type = SENSOR_CHAN_ROTATION , .chan_idx = 0 },
383+ };
371384 struct sensor_value val = {0 };
372385
373386 if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
@@ -377,24 +390,19 @@ static void sensor_channel_get_empty(const struct device *const dev)
377390 /* wait for potential new readings */
378391 k_msleep (100 );
379392
380- rc = sensor_sample_fetch (dev );
381- zassert_true (rc == 0 , "Failed to fetch sample (%d)" , rc );
382-
383- /* get readings but ignore them, as they may include reading from time
384- * when emulation was still working (i.e. during previous test)
385- */
386- rc = sensor_channel_get (dev , SENSOR_CHAN_ROTATION , & val );
387- zassert_true (rc == 0 , "Failed to get sample (%d)" , rc );
393+ rc = sensor_read_and_decode (dev , channels , ARRAY_SIZE (channels ), & rotation_shift ,
394+ & rotation_value , 1 );
395+ zassert_true (rc == 0 , "Failed to read sample (%d)" , rc );
388396
389397 /* wait for potential new readings */
390398 k_msleep (100 );
391399
392- rc = sensor_sample_fetch (dev );
400+ rc = sensor_read_and_decode (dev , channels , ARRAY_SIZE (channels ), & rotation_shift ,
401+ & rotation_value , 1 );
393402 zassert_true (rc == 0 , "Failed to fetch sample (%d)" , rc );
394403
395404 /* emulation was not working, expect no readings */
396- rc = sensor_channel_get (dev , SENSOR_CHAN_ROTATION , & val );
397- zassert_true (rc == 0 , "Failed to get sample (%d)" , rc );
405+ q31_to_sensor_value (rotation_value , rotation_shift , & val );
398406 zassert_true (val .val1 == 0 , "Expected no readings but got: %d" , val .val1 );
399407 zassert_true (val .val2 == 0 , "Expected no readings but got: %d" , val .val2 );
400408
@@ -420,8 +428,12 @@ ZTEST(qdec_sensor, test_sensor_channel_get_empty)
420428static void sensor_channel_get_test (struct qdec_qenc_loopback * loopback )
421429{
422430 int rc ;
423- struct sensor_value val_first = {0 };
424- struct sensor_value val_second = {0 };
431+ int8_t rotation_shift ;
432+ q31_t rotation_value ;
433+ struct sensor_chan_spec channels [1 ] = {
434+ {.chan_type = SENSOR_CHAN_ROTATION , .chan_idx = 0 },
435+ };
436+ struct sensor_value val = {0 };
425437
426438 if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
427439 pm_device_runtime_get (loopback -> qdec );
@@ -432,32 +444,12 @@ static void sensor_channel_get_test(struct qdec_qenc_loopback *loopback)
432444 /* wait for some readings*/
433445 k_msleep (100 );
434446
435- rc = sensor_sample_fetch (loopback -> qdec );
436- zassert_true (rc == 0 , "Failed to fetch sample (%d)" , rc );
437-
438- rc = sensor_channel_get (loopback -> qdec , SENSOR_CHAN_ROTATION , & val_first );
439- zassert_true (rc == 0 , "Failed to get sample (%d)" , rc );
440- zassert_true (val_first .val1 != 0 , "No readings from QDEC" );
447+ rc = sensor_read_and_decode (loopback -> qdec , channels , ARRAY_SIZE (channels ), & rotation_shift ,
448+ & rotation_value , 1 );
449+ zassert_true (rc == 0 , "Failed to read sample (%d)" , rc );
441450
442- /* wait for more readings*/
443- k_msleep (200 );
444-
445- rc = sensor_channel_get (loopback -> qdec , SENSOR_CHAN_ROTATION , & val_second );
446- zassert_true (rc == 0 , "Failed to fetch sample (%d)" , rc );
447- zassert_true (val_second .val1 != 0 , "No readings from QDEC" );
448-
449- /* subsequent calls of sensor_channel_get without calling sensor_sample_fetch
450- * should yield the same value
451- */
452- zassert_true (val_first .val1 == val_second .val1 ,
453- "Expected the same readings: %d vs %d" ,
454- val_first .val1 ,
455- val_second .val1 );
456-
457- zassert_true (val_first .val2 == val_second .val2 ,
458- "Expected the same readings: %d vs %d" ,
459- val_first .val2 ,
460- val_second .val2 );
451+ q31_to_sensor_value (rotation_value , rotation_shift , & val );
452+ zassert_true (val .val1 != 0 , "No readings from QDEC" );
461453
462454 if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
463455 pm_device_runtime_put (loopback -> qdec );
@@ -481,6 +473,11 @@ ZTEST(qdec_sensor, test_sensor_channel_get)
481473static void sensor_channel_get_negative (struct qdec_qenc_loopback * loopback )
482474{
483475 int rc ;
476+ int8_t max_shift ;
477+ q31_t max_value ;
478+ struct sensor_chan_spec channels [1 ] = {
479+ {.chan_type = SENSOR_CHAN_MAX , .chan_idx = 0 },
480+ };
484481 struct sensor_value val = {0 };
485482
486483 if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
@@ -492,11 +489,11 @@ static void sensor_channel_get_negative(struct qdec_qenc_loopback *loopback)
492489 /* wait for some readings*/
493490 k_msleep (100 );
494491
495- rc = sensor_sample_fetch (loopback -> qdec );
496- zassert_true (rc == 0 , "Failed to fetch sample (%d)" , rc );
492+ rc = sensor_read_and_decode (loopback -> qdec , channels , ARRAY_SIZE (channels ), & max_shift ,
493+ & max_value , 1 );
494+ zassert_true (rc == 0 , "Failed to read sample (%d)" , rc );
497495
498- rc = sensor_channel_get (loopback -> qdec , SENSOR_CHAN_MAX , & val );
499- zassert_true (rc < 0 , "Should failed to get sample (%d)" , rc );
496+ q31_to_sensor_value (max_value , max_shift , & val );
500497 zassert_true (val .val1 == 0 , "Some readings from QDEC: %d" , val .val1 );
501498 zassert_true (val .val2 == 0 , "Some readings from QDEC: %d" , val .val2 );
502499
@@ -522,18 +519,21 @@ ZTEST(qdec_sensor, test_sensor_channel_get_negative)
522519static void sensor_sample_fetch_test (const struct device * const dev )
523520{
524521 int rc ;
522+ int8_t shift ;
523+ q31_t value ;
524+ struct sensor_chan_spec channels [2 ] = {
525+ {.chan_type = SENSOR_CHAN_ROTATION , .chan_idx = 0 },
526+ {.chan_type = SENSOR_CHAN_MAX , .chan_idx = 0 },
527+ };
525528
526529 if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
527530 pm_device_runtime_get (dev );
528531 }
529532
530- rc = sensor_sample_fetch (dev );
531- zassert_true (rc == 0 , "Failed to fetch sample (%d)" , rc );
532-
533- rc = sensor_sample_fetch_chan (dev , SENSOR_CHAN_ROTATION );
533+ rc = sensor_read_and_decode (dev , & channels [0 ], 1 , & shift , & value , 1 );
534534 zassert_true (rc == 0 , "Failed to fetch sample (%d)" , rc );
535535
536- rc = sensor_sample_fetch_chan (dev , SENSOR_CHAN_MAX );
536+ rc = sensor_read_and_decode (dev , & channels [ 1 ], 1 , & shift , & value , 1 );
537537 zassert_true (rc < 0 , "Should fail to fetch sample from invalid channel (%d)" , rc );
538538
539539 if (IS_ENABLED (CONFIG_PM_DEVICE_RUNTIME )) {
@@ -542,7 +542,7 @@ static void sensor_sample_fetch_test(const struct device *const dev)
542542}
543543
544544/**
545- * @brief sensor_sample_fetch(_chan) test
545+ * @brief Reading sample test
546546 *
547547 * Confirm fetching work with QDEC specific channel - rotation
548548 *
0 commit comments