enjoy-digital / litex

Build your hardware, easily!
Other
3.03k stars 573 forks source link

Error When Trying to Measure Time in C Language #2008

Open fzhwenzhou opened 4 months ago

fzhwenzhou commented 4 months ago

I tried to measure the execution time using the clock() function in <time.h>. However, it throws the following error:

undefined reference to `times`

Thus, I cannot measure the execution time precisely. Is this a bug? If so, how can I fix it? If not, how can I measure the time in C language? The FPGA board is Tang Nano 20K, and the softcore CPU it uses is PicoRV32 standard version. Thank you!

enjoy-digital commented 4 months ago

Hi @fzhwenzhou,

to measure time, you can enable uptime to the Timer by adding --timer-uptime to your build command. It will add the uptime to the gateware and corresponding CSR to thecsr.h that you can then use on your C code. You'll then just need to latch to uptime and read it, more information on the logic/CSR registers can be found here: https://github.com/enjoy-digital/litex/blob/master/litex/soc/cores/timer.py#L92-L102

fzhwenzhou commented 4 months ago

Thank you! It sounds workable, but I may need more time to figure out how to do that lol. In the meanwhile, I still hope we can fix the clock function in C language and enable it to invoke the uptime register, in order to ease software developers like me. Thank you anyway.