User avatar
HermannSW
Posts: 5180
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany

gettimeofday() not working with SDK?

Mon Jan 25, 2021 9:00 pm

Long ago I used code that just did excessive search for minimal magic 3x3 square consisting of distinct primes for comparing different microcontrollers. Later I added numbers for Intel laptop and Raspberry PIs as well:
viewtopic.php?p=1290845#p1291239


I only needed small changes to get q32.c running on Pico with USB output:
https://stamm-wilbrandt.de/en/forum/q32.c

Code: Select all

$ diff q32.c ../../../hello_world/usb/hello_usb.c
3a4
> #include "pico/stdlib.h"
21,22c22,25
<   gettimeofday(&tv1, NULL);      // wait for usec change
<   do  gettimeofday(&tv0, NULL);  while (tv0.tv_usec == tv1.tv_usec);
---
>   stdio_init_all();
>   sleep_ms(5000);
> 
>   gettimeofday(&tv0, NULL);
$ 

It was strange that loop waiting for microsecond change did not work and I had to remove.
But final microsecond duration output of 0 seems to indicate that gettimeofday() is not implemented as on Raspberry Pi OS:

Code: Select all

Compiled on Aug 13 2017, 15:25:34.                                        
Port /dev/ttyACM0, 21:47:03                                                                                                                        

Press CTRL-A Z for help on special keys                                   
                                                                          
 47| 29|101|                                                              
113| 59|  5|                                                              
 17| 89| 71|                                                              
                                                                          
0us

Is it correct that gettimeofday() cannot be used to measure microsecond precision time deltas?
If that is true, is there an alternative microsecond precision time delta measurement method in Pico SDK?
https://hermann-sw.github.io/planar_graph_playground
https://stamm-wilbrandt.de/en#raspcatbt
https://github.com/Hermann-SW/memrun
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/en/Raspberry_camera.html

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 30979
Joined: Sat Jul 30, 2011 7:41 pm

Re: gettimeofday() not working with SDK?

Mon Jan 25, 2021 10:26 pm

Principal Software Engineer at Raspberry Pi Ltd.
Working in the Applications Team.

kilograham
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1158
Joined: Fri Apr 12, 2019 11:00 am
Location: austin tx

Re: gettimeofday() not working with SDK?

Mon Jan 25, 2021 11:23 pm

or time_us32() or time_us64()

User avatar
HermannSW
Posts: 5180
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany

Re: gettimeofday() not working with SDK?

Tue Jan 26, 2021 7:17 am

jamesh wrote:
Mon Jan 25, 2021 10:26 pm
Perhaps

https://raspberrypi.github.io/pico-sdk- ... stamp.html
Does not work when using sleep_ms() between taking two timestamps, but does work when doing something.
This is output for 1000 loops incrementing another integer as well, 1700us:

Code: Select all

Port /dev/ttyACM0, 08:11:40

Press CTRL-A Z for help on special keys


10000
1700us

This is the code -- does timer sleep while sleep_ms();

Code: Select all

#include <stdio.h>
#include "pico/stdlib.h"

absolute_time_t t0,t1;
int i,k;

int main(void)
{
  stdio_init_all();
  sleep_ms(5000);

Code: Select all

  t0 = get_absolute_time();
//  sleep_ms(3333);
  k=0;
  for(i=0; i<10000; ++i)
    k++;

  t1 = get_absolute_time();
  printf("\n%d\n%ldus\n", k, absolute_time_diff_us(t0, t1));

  return 0;
}
https://hermann-sw.github.io/planar_graph_playground
https://stamm-wilbrandt.de/en#raspcatbt
https://github.com/Hermann-SW/memrun
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/en/Raspberry_camera.html

User avatar
HermannSW
Posts: 5180
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany

Re: gettimeofday() not working with SDK?

Tue Jan 26, 2021 7:24 am

kilograham wrote:
Mon Jan 25, 2021 11:23 pm
or time_us32() or time_us64()
Thanks, time_us_32() does work even when sleeping in between.
And wraps only after more than an hour, I want to measure sub millisecond time deltas:
https://raspberrypi.github.io/pico-sdk- ... ea1610c55d

Below code (correctly) outputs this for sleeping 3.333 seconds:

Code: Select all

...
Press CTRL-A Z for help on special keys                
                                                       
                                                       
3333004us                                              

Code: Select all

#include <stdio.h>
#include "pico/stdlib.h"

uint32_t t0,t1;

int main(void)
{
  stdio_init_all();
  sleep_ms(5000);

Code: Select all

  t0 = time_us_32();
  sleep_ms(3333);
  t1 = time_us_32();
  printf("\n%ldus\n", t1-t0);

  return 0;
}
https://hermann-sw.github.io/planar_graph_playground
https://stamm-wilbrandt.de/en#raspcatbt
https://github.com/Hermann-SW/memrun
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/en/Raspberry_camera.html

User avatar
HermannSW
Posts: 5180
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany

Re: gettimeofday() not working with SDK?

Tue Jan 26, 2021 7:40 am

time_us_32() resolved the issues with q32.c measurement, magic 3x3 distinct prime square computed in 397us:

Code: Select all

Compiled on Aug 13 2017, 15:25:34.
Port /dev/ttyACM0, 08:32:34

Press CTRL-A Z for help on special keys                
                                                       
 47| 29|101|                                           
113| 59|  5|                                           
 17| 89| 71|                                           

397us

Find small diff to q32.c further below, q32.pico.c is here:
https://gist.github.com/Hermann-SW/6772 ... 543d9d3dbe


397us is slower than ESP microcontrollers (and PIs of course), but faster than all Arduinos tested.
Need to figure out how to compile with -O3 for the Pico.

Did figure out (not perfect), added "-O3" after 1st "$(C_FLAGS)" to
/home/pi/pico/pico-examples/build/hello_world/usbCMakeFiles/hello_usb.dir/build.make

-O3 reports 357us:

Code: Select all

Compiled on Aug 13 2017, 15:25:34.
Port /dev/ttyACM0, 09:01:21

Press CTRL-A Z for help on special keys

 47| 29|101|
113| 59|  5|
 17| 89| 71|

357us

This is simple pure integer arithmetic single threaded code, but I have numbers for many different MCUs/CPUs:
viewtopic.php?p=1290845#p1291239
Image


Small diff of Pico code to q32.c:

Code: Select all

$ diff q32.c ../../../hello_world/usb/hello_usb.c
3a4
> #include "pico/stdlib.h"
17c18
< struct timeval tv0,tv1;
---
> uint32_t t0,t1;
21,22c22,26
<   gettimeofday(&tv1, NULL);      // wait for usec change
<   do  gettimeofday(&tv0, NULL);  while (tv0.tv_usec == tv1.tv_usec);
---
>   stdio_init_all();
>   sleep_ms(5000);
> 
>   t1 = time_us_32();      // wait for usec change
>   do  t0 = time_us_32();  while (t0 == t1);
41c45
<                   gettimeofday(&tv1, NULL);
---
>                   t1 = time_us_32();
47c51
<                     1000000*(tv1.tv_sec-tv0.tv_sec)+tv1.tv_usec-tv0.tv_usec);
---
>                     t1 - t0);
$ 
https://hermann-sw.github.io/planar_graph_playground
https://stamm-wilbrandt.de/en#raspcatbt
https://github.com/Hermann-SW/memrun
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/en/Raspberry_camera.html

kilograham
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1158
Joined: Fri Apr 12, 2019 11:00 am
Location: austin tx

Re: gettimeofday() not working with SDK?

Tue Jan 26, 2021 2:33 pm

HermannSW wrote:
Tue Jan 26, 2021 7:17 am

Does not work when using sleep_ms() between taking two timestamps, but does work when doing something.
This is output for 1000 loops incrementing another integer as well, 1700us:
Can you clarify this?

User avatar
HermannSW
Posts: 5180
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany

Re: gettimeofday() not working with SDK?

Tue Jan 26, 2021 9:32 pm

kilograham wrote:
Tue Jan 26, 2021 2:33 pm
HermannSW wrote:
Tue Jan 26, 2021 7:17 am

Does not work when using sleep_ms() between taking two timestamps, but does work when doing something.
This is output for 1000 loops incrementing another integer as well, 1700us:
Can you clarify this?
As I showed, correct time delta was reported when doing something between two calls of get_absolute_time().

Using time_us_32() also works if only sleeping between taking timestamps:

Code: Select all

#include <stdio.h>
#include "pico/stdlib.h"
#include <tusb.h>

int main(void)
{
  stdio_init_all();
  while (!tud_cdc_connected()) { sleep_ms(100);  }
  printf("tud_cdc_connected()\n");

  uint32_t t0 = time_us_32();
  sleep_ms(3333);
  uint32_t t1 = time_us_32();
  printf("\n%ldus\n", t1-t0);

  return 0;
}

Here the correct time delta gets reported:

Code: Select all

tud_cdc_connected()

3333003us

Taking timestamps with get_absolute_time() and sleeping in between does not work:

Code: Select all

...
  printf("tud_cdc_connected()\n");

  absolute_time_t t0 = get_absolute_time();
  sleep_ms(3333);
  absolute_time_t t1 = get_absolute_time();
  printf("\n%ldus\n", absolute_time_diff_us(t0, t1));
...

Incorrectly 0us gets reported:

Code: Select all

Press CTRL-A Z for help on special keys

tud_cdc_connected()

0us

So we have bug1 that gettimeofday does not work at all.
And bug2 that get_absolute_time() does not work for sleeping between taking timestamps.

time_us_32() just works correctly.
https://hermann-sw.github.io/planar_graph_playground
https://stamm-wilbrandt.de/en#raspcatbt
https://github.com/Hermann-SW/memrun
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/en/Raspberry_camera.html

kilograham
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1158
Joined: Fri Apr 12, 2019 11:00 am
Location: austin tx

Re: gettimeofday() not working with SDK?

Wed Jan 27, 2021 1:45 am

Code: Select all

printf("\n%ldus\n", absolute_time_diff_us(t0, t1));
it is a 64 bit value on a 32bit architecture, so use %lld or cast it to a 32 bit value

as for gettimeofday() - yes that might be nice to have work from the RTC; it doesn't today, feel free to file an issue against pico-sdk in github

User avatar
HermannSW
Posts: 5180
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany

Re: gettimeofday() not working with SDK?

Wed Jan 27, 2021 5:05 am

kilograham wrote:
Wed Jan 27, 2021 1:45 am

Code: Select all

printf("\n%ldus\n", absolute_time_diff_us(t0, t1));
it is a 64 bit value on a 32bit architecture, so use %lld or cast it to a 32 bit value
You are right, with that format change time delta gets reported correctly:

Code: Select all

tud_cdc_connected()                                    

3333004us
as for gettimeofday() - yes that might be nice to have work from the RTC; it doesn't today, feel free to file an issue against pico-sdk in github
Done:
https://github.com/raspberrypi/pico-feedback/issues/24
https://hermann-sw.github.io/planar_graph_playground
https://stamm-wilbrandt.de/en#raspcatbt
https://github.com/Hermann-SW/memrun
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/en/Raspberry_camera.html

User avatar
HermannSW
Posts: 5180
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany

Re: gettimeofday() not working with SDK?

Wed Jan 27, 2021 1:15 pm

Issue got closed, I am not able to reopen, but commented why reopen is really needed for Pico gettimeofday():
https://github.com/raspberrypi/pico-fee ... -768276323
https://hermann-sw.github.io/planar_graph_playground
https://stamm-wilbrandt.de/en#raspcatbt
https://github.com/Hermann-SW/memrun
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/en/Raspberry_camera.html

Return to “SDK”