1717#include <zephyr/shell/shell.h>
1818#include <zephyr/sys/util.h>
1919
20+ #ifdef CONFIG_FLASH_SHELL_TEST_COMMANDS
21+ #include <zephyr/timing/timing.h>
22+ #endif
23+
2024/* Buffer is only needed for bytes that follow command and offset */
2125#define BUF_ARRAY_CNT (CONFIG_SHELL_ARGC_MAX - 2)
2226
@@ -354,23 +358,25 @@ static int read_write_erase_validate(const struct shell *sh, size_t argc, char *
354358 return 0 ;
355359}
356360
357- static void speed_output (const struct shell * sh , uint64_t total_time , double loops , double size )
361+ static void speed_output (const struct shell * sh , uint64_t total_time , uint32_t loops , uint32_t size )
358362{
359- double time_per_loop = ( double ) total_time / loops ;
363+ uint64_t time_per_loop = timing_cycles_to_ns_avg ( total_time , loops ) ;
360364 double throughput = size ;
361365 uint8_t speed_index = 0 ;
362366
363367 if (time_per_loop > 0 ) {
364- throughput /= (time_per_loop / 1000.0 );
368+ throughput *= NSEC_PER_SEC ;
369+ throughput /= time_per_loop ;
365370 }
366371
367372 while (throughput >= (double )speed_divisor && speed_index < ARRAY_SIZE (speed_types )) {
368373 throughput /= (double )speed_divisor ;
369374 ++ speed_index ;
370375 }
371376
372- shell_print (sh , "Total: %llums, Per loop: ~%.0fms, Speed: ~%.1f%sps" ,
373- total_time , time_per_loop , throughput , speed_types [speed_index ]);
377+ shell_print (sh , "Total: %llu ns, Per loop: %llu ns, Speed: ~%.1f%sps" ,
378+ timing_cycles_to_ns (total_time ), time_per_loop , throughput ,
379+ speed_types [speed_index ]);
374380}
375381
376382static int cmd_read_test (const struct shell * sh , size_t argc , char * argv [])
@@ -381,7 +387,7 @@ static int cmd_read_test(const struct shell *sh, size_t argc, char *argv[])
381387 int result ;
382388 uint32_t addr ;
383389 uint32_t size ;
384- uint64_t start_time ;
390+ timing_t start_time , stop_time ;
385391 uint64_t loop_time ;
386392 uint64_t total_time = 0 ;
387393 uint32_t loops = 0 ;
@@ -396,10 +402,14 @@ static int cmd_read_test(const struct shell *sh, size_t argc, char *argv[])
396402 return result ;
397403 }
398404
405+ timing_init ();
406+ timing_start ();
407+
399408 while (repeat -- ) {
400- start_time = k_uptime_get ();
409+ start_time = timing_counter_get ();
401410 result = flash_read (flash_dev , addr , test_arr , size );
402- loop_time = k_uptime_delta (& start_time );
411+ stop_time = timing_counter_get ();
412+ loop_time = timing_cycles_get (& start_time , & stop_time );
403413
404414 if (result ) {
405415 shell_error (sh , "Read failed: %d" , result );
@@ -408,13 +418,15 @@ static int cmd_read_test(const struct shell *sh, size_t argc, char *argv[])
408418
409419 ++ loops ;
410420 total_time += loop_time ;
411- shell_print (sh , "Loop #%u done in %llums ." , loops , loop_time );
421+ shell_print (sh , "Loop #%u done in %llu ns ." , loops , timing_cycles_to_ns ( loop_time ) );
412422 }
413423
414424 if (result == 0 ) {
415- speed_output (sh , total_time , ( double ) loops , ( double ) size );
425+ speed_output (sh , total_time , loops , size );
416426 }
417427
428+ timing_stop ();
429+
418430 return result ;
419431}
420432
@@ -425,7 +437,7 @@ static int cmd_write_test(const struct shell *sh, size_t argc, char *argv[])
425437 int result ;
426438 uint32_t addr ;
427439 uint32_t size ;
428- uint64_t start_time ;
440+ timing_t start_time , stop_time ;
429441 uint64_t loop_time ;
430442 uint64_t total_time = 0 ;
431443 uint32_t loops = 0 ;
@@ -444,10 +456,14 @@ static int cmd_write_test(const struct shell *sh, size_t argc, char *argv[])
444456 test_arr [i ] = (uint8_t )i ;
445457 }
446458
459+ timing_init ();
460+ timing_start ();
461+
447462 while (repeat -- ) {
448- start_time = k_uptime_get ();
463+ start_time = timing_counter_get ();
449464 result = flash_write (flash_dev , addr , test_arr , size );
450- loop_time = k_uptime_delta (& start_time );
465+ stop_time = timing_counter_get ();
466+ loop_time = timing_cycles_get (& start_time , & stop_time );
451467
452468 if (result ) {
453469 shell_error (sh , "Write failed: %d" , result );
@@ -456,13 +472,15 @@ static int cmd_write_test(const struct shell *sh, size_t argc, char *argv[])
456472
457473 ++ loops ;
458474 total_time += loop_time ;
459- shell_print (sh , "Loop #%u done in %llu ticks ." , loops , loop_time );
475+ shell_print (sh , "Loop #%u done in %llu ns ." , loops , timing_cycles_to_ns ( loop_time ) );
460476 }
461477
462478 if (result == 0 ) {
463- speed_output (sh , total_time , ( double ) loops , ( double ) size );
479+ speed_output (sh , total_time , loops , size );
464480 }
465481
482+ timing_stop ();
483+
466484 return result ;
467485}
468486
@@ -473,7 +491,7 @@ static int cmd_erase_test(const struct shell *sh, size_t argc, char *argv[])
473491 int result ;
474492 uint32_t addr ;
475493 uint32_t size ;
476- uint64_t start_time ;
494+ timing_t start_time , stop_time ;
477495 uint64_t loop_time ;
478496 uint64_t total_time = 0 ;
479497 uint32_t loops = 0 ;
@@ -492,10 +510,14 @@ static int cmd_erase_test(const struct shell *sh, size_t argc, char *argv[])
492510 test_arr [i ] = (uint8_t )i ;
493511 }
494512
513+ timing_init ();
514+ timing_start ();
515+
495516 while (repeat -- ) {
496- start_time = k_uptime_get ();
517+ start_time = timing_counter_get ();
497518 result = flash_erase (flash_dev , addr , size );
498- loop_time = k_uptime_delta (& start_time );
519+ stop_time = timing_counter_get ();
520+ loop_time = timing_cycles_get (& start_time , & stop_time );
499521
500522 if (result ) {
501523 shell_error (sh , "Erase failed: %d" , result );
@@ -504,13 +526,15 @@ static int cmd_erase_test(const struct shell *sh, size_t argc, char *argv[])
504526
505527 ++ loops ;
506528 total_time += loop_time ;
507- shell_print (sh , "Loop #%u done in %llums ." , loops , loop_time );
529+ shell_print (sh , "Loop #%u done in %llu ns ." , loops , timing_cycles_to_ns ( loop_time ) );
508530 }
509531
510532 if (result == 0 ) {
511- speed_output (sh , total_time , ( double ) loops , ( double ) size );
533+ speed_output (sh , total_time , loops , size );
512534 }
513535
536+ timing_stop ();
537+
514538 return result ;
515539}
516540
@@ -522,7 +546,7 @@ static int cmd_erase_write_test(const struct shell *sh, size_t argc, char *argv[
522546 int result_write = 0 ;
523547 uint32_t addr ;
524548 uint32_t size ;
525- uint64_t start_time ;
549+ timing_t start_time , stop_time ;
526550 uint64_t loop_time ;
527551 uint64_t total_time = 0 ;
528552 uint32_t loops = 0 ;
@@ -541,11 +565,15 @@ static int cmd_erase_write_test(const struct shell *sh, size_t argc, char *argv[
541565 test_arr [i ] = (uint8_t )i ;
542566 }
543567
568+ timing_init ();
569+ timing_start ();
570+
544571 while (repeat -- ) {
545- start_time = k_uptime_get ();
572+ start_time = timing_counter_get ();
546573 result_erase = flash_erase (flash_dev , addr , size );
547574 result_write = flash_write (flash_dev , addr , test_arr , size );
548- loop_time = k_uptime_delta (& start_time );
575+ stop_time = timing_counter_get ();
576+ loop_time = timing_cycles_get (& start_time , & stop_time );
549577
550578 if (result_erase ) {
551579 shell_error (sh , "Erase failed: %d" , result_erase );
@@ -559,13 +587,15 @@ static int cmd_erase_write_test(const struct shell *sh, size_t argc, char *argv[
559587
560588 ++ loops ;
561589 total_time += loop_time ;
562- shell_print (sh , "Loop #%u done in %llums ." , loops , loop_time );
590+ shell_print (sh , "Loop #%u done in %llu ns ." , loops , timing_cycles_to_ns ( loop_time ) );
563591 }
564592
565593 if (result_erase == 0 && result_write == 0 ) {
566- speed_output (sh , total_time , ( double ) loops , ( double ) size );
594+ speed_output (sh , total_time , loops , size );
567595 }
568596
597+ timing_stop ();
598+
569599 return (result_erase != 0 ? result_erase : result_write );
570600}
571601#endif
0 commit comments