In a recent project, a high-precision real-time clock is required under VxWorks, with a precision of 1ms and an overflow time of more than 5 hours. The VxWorks provides a system clock that starts counting after the operating system starts, with a precision of 1 ticks, and can get the current count value through Tickget (). Because the system clock default operating frequency is 60Hz, then 1 ticks equal to 16.7ms, do not sign our accuracy requirements. Although it is possible to increase the accuracy to 1ms through Sysclkrateset (1000), the 1KHZ system clock interrupt frequency can make the CPU overhead increase greatly. Considering applications like Nanosleep () whose timing accuracy can reach nanosecond levels, there must be a corresponding NS-class clock available in the CPU. The hardware platform used by the project is PC104, the processor is 300MHZ X86 compatible CPU, and in the VxWorks function manual (vxworks_os_libraries_api_reference) The CPU-related function library is the 3 APIs pentiumTscGet32 (), PentiumTscGet64 (), Pentiumtscreset () are found in pentiumALib.h to operate on the built-in TSC in the CPU. TSC is time Stamp Counter, a 64-bit timestamp counter provided for Pentium series CPUs, which is counted once per instruction cycle after the CPU is power up or reset, and Intel guarantees that the TSC overflow period is greater than 10. Like the 300MHz CPU we use, its TSC accuracy is about 33ns and the overflow period is about 19,303 years, which is perfectly in line with our project requirements.
However, it is faint that the project cannot be compiled after pentiumALib.h this header file is included in downloadable project! Helpless under the Manual of the TSC related functions of the description, the manual said: These three functions are implemented through the Assembly, to read the TSC register only use RDTSC this instruction, it will be the current value of the TSC low 32 bits into the EAX register, high 32 bits into the EDX register. I would not just insert such a piece of assembly in my application, the key is how to use C language and assembly mixed programming under VxWorks? Fortunately, I've seen a little bit of the Linux kernel source scenario analysis, and in the first chapter of the book there is an introduction to the mixed programming of C language assembly in GCC, and the compiler used by VxWorks is gcc!
You might as well try it out, so you have the following code:
- /*****************gettsc-get TSC count*************************************
- Get the TSC (timestamp counter) count value, put the counter high in Phi, low deposit plo** returns:n/a
- ****************************************************************************/
- void gettsc (unsigned int *phi, unsigned int *plo)
- {
- unsigned int hi, lo;
- __asm__ __volatile__ ("rdtsc\n movl%%eax,%0\n movl%%edx,%1":"=b" (LO),"=c" (HI)::"Memory");
- *phi = Hi;
- *plo = lo;
- }
- /****************gettsc-get the lower half of TSC count***************
- Get TSC (timestamp counter) low 32-bit meter value * * RETURNS:TSC low 32-bit meter value
- ***********************************************************************/
- unsigned int getTsc32 (void)
- {
- unsigned int tmp;
- __asm__ __volatile__ ("rdtsc\n movl%%edx,%0":"=c" (TMP)::"Memory");
- return tmp;
- }
If you use the PowerPC platform, the PowerPC provides a TB (time Base) register similar to Pentium in Tsc,vxalib Vxtimebaseset () and Vxtimebaseget () Two functions to read and write TB registers.
Realization of high-precision real-time clock in "Turn" VxWorks and mixed programming of C language Assembly