Realization of high-precision real-time clock in "Turn" VxWorks and mixed programming of C language Assembly

Source: Internet
Author: User

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:


  1. /*****************gettsc-get TSC count*************************************
  2. Get the TSC (timestamp counter) count value, put the counter high in Phi, low deposit plo** returns:n/a
  3. ****************************************************************************/
  4. void gettsc (unsigned int *phi, unsigned int *plo)
  5. {
  6. unsigned int hi, lo;
  7. __asm__ __volatile__ ("rdtsc\n movl%%eax,%0\n movl%%edx,%1":"=b" (LO),"=c" (HI)::"Memory");
  8. *phi = Hi;
  9. *plo = lo;
  10. }
  11. /****************gettsc-get the lower half of TSC count***************
  12. Get TSC (timestamp counter) low 32-bit meter value * * RETURNS:TSC low 32-bit meter value
  13. ***********************************************************************/
  14. unsigned int getTsc32 (void)
  15. {
  16. unsigned int tmp;
  17. __asm__ __volatile__ ("rdtsc\n movl%%edx,%0":"=c" (TMP)::"Memory");
  18. return tmp;
  19. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.