How does the clock () function in <ctime> access the system clock?

I use the STM32 microcontroller with minimal libraries connected. I would like to use the clock() function from <ctime> (and possibly the new std::chrono ), but I am stuck in clock() returning -1.

This is not surprising since I do not expect the libraries in gcc-arm-none-eabi to know the peripheral layout of my microcontroller.

How can I tell clock where it gets its tick counter? Can I just update it or is there a lower level function that I need to implement?

+4
source share
1 answer

This is usually part of porting the libc implementation to a new platform. When you compile a copy of glibc or newlib (the two most popular implementations of the C standard library), you need to provide a number of stub methods, one of which will provide time. gcc-arm-none-eabi is a predefined target in newlib that implements a lot of these stubs, but not the ones needed to get the time, because, as you said, it is application specific.

It would be best to build the tool chain yourself by implementing this method. You can follow the instructions here , but before you create newlib, change the target to fill this stub. A good link to the implementation of stubs is available here .

Alternatively, you can implement your own clock function directly, when you link your application, it will prefer your version over the one in the library. However, be careful, this means that everything in the library that calls clock will still call the broken version of the library, so you may need to override many functions.

+1
source

All Articles