In the most recent project, a high-precision real-time clock needs to be used in VxWorks, with a precision of 1 ms and overflow time greater than 5 hours. VxWorks provides a system clock, which starts counting after the operating system starts. The precision is 1 tick. You can get the current Count value through tickget. Because the default operating frequency of the system clock is 60Hz, one tick is equivalent to 16.7 MS, which does not represent our precision requirements. Although the accuracy can be improved to 1 ms through sysclkrateset (1000), the Interruption Frequency of the 1 kHz system clock will increase the CPU overhead. Considering that the timing precision of applications such as nanosleep () can reach the nanosecond level, the CPU must have a corresponding NS-level clock. The hardware platform used by the project is PC104, and the processor is 300 MHz x86 compatible CPU. In the VxWorks function manual (vxworks_ OS _libraries_api_reference), check the CPU-related function library, and find pentiumtscget32 () in pentiumalib () pentiumtscget64 () and pentiumtscreset () APIs can be used to operate the built-in TSC In the CPU. TSC is the time stamp counter, which provides a 64-bit timestamp counter for the Pentium series CPU. This counter counts every instruction cycle once after the CPU is powered on or reset, intel ensures that the TSC overflow period is greater than 10 years. Like the 19303 MHz CPU we use, the TSC precision is about 33ns, And the overflow period is about years, which fully meets the requirements of our project. However, the faint is that after the header file pentiumlib. H is included in the downloadable project, the project cannot be compiled and passed! However, let's take a closer look at the description of the TSC-related functions in the manual. The manual says: these three functions are all implemented through Assembly. to read the TSC register, you only need to use the rdtsc command, it will put the current low 32-bit TSC value into the eax register, and the high 32-bit value into the edX register. Then I don't have to insert such an assembly in my application. The key is how to use C language and Assembly hybrid programming in VxWorks? Fortunately, I have read a little bit of "Linux kernel source code Scenario Analysis" before. In the first chapter of the book, I will introduce the C language Assembly hybrid programming in GCC. The Compiler used by VxWorks is GCC! Let's try it, so we have the following code: /*************************************** * *********************************** gettsc-Get TSC count ** get TSC (time stamp counter) count value: store the counter in the highest position into Phi, and store the counter in the lowest position into 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 the TSC (time stamp counter) low 32-bit counter value ** returns: TSC low 32-bit counter value */unsigned int gettsc32 (void) {unsigned int TMP; _ ASM _ volatile _ ("rdtsc/n movl % edX, % 0": "= C" (TMP): "Memory "); return TMP;} if the PowerPC platform is used, the TB (Time Base) registers provided by PowerPC are similar to the TSC of Pentium. vxalib provides vxtimebaseset () and vxtimebaseget () two functions are used to read and write the TB register. (Thanks to Bai Fei's fzz for providing powrpc technical support !)