Real-Time Measurement and Control

cancel
Showing results for 
Search instead for 
Did you mean: 

Get a precise timing in a C++ shared library in VxWorks

Solved!
Go to solution

Hi everyone,

 

I'm currently developing a VxWorks shared library for my compactRio to access a 'non mass strorage' usb device.

 

I'm working with gcc (see Developing Shared Libraries for the cRIO-901x and Other VxWorks Targets for details).

 

The usb part works fine, but I now need to get a precise timing in one of the c++ functions of the shared library. I tried the clockLib functions (e.g. clock_gettime( ) - see page 8 of http://www-ad.fnal.gov/controls/micro_p/manuals/vxworks_application_api_reference_6.6.pdf for details) but these seem to have a resolution of 1ms, which is not enough for me.

 

There's a faster clock source on the cRio (at least 1Mhz, the one used for Timed Loops) but I have no idea if such a source could be used in a shared library. If yes, does anybody know what functions to call ?

 

Thanks a lot,

Martin

BR,

Martin
CLD
0 Kudos
Message 1 of 7
(5,321 Views)

There's not a simple way to read a standard clock from the FPGA that I know of, but there are some functions that NI added to the VxWorks kernel that read the system timebase.  Those functions look like this:

 

extern unsigned long niTimestamp32();
extern unsigned long long niTimestamp64();
extern void niTimestampHiLo(unsigned long*, unsigned long*);
extern unsigned long niTimestampFreq();

 

A few notes:

 

* The timestamp is based on a divided down system clock, which is different on different controllers.  The ones that I know of are between 33MHz and 50 MHz.

* On top of that, the frequency can sometimes drift a little bit with temperature, so don't be surprised if the frequency number you see is not 100% constant.

 

Hope that works for you.

Ross Houston
Principal Software Engineer
Timing & Sync Software R&D
Message 2 of 7
(5,317 Views)

Dear Ross,

 

Thanks for your answer.

 

I tried to implement your functions, but simply declaring them as extern don't seem to work. As soon as I add one, the library fails to load on the cRio (with the typical error message LabVIEW:  Failed to load shared library example.*:getTime:C. Ensure that the library is present on the RT target. Use MAX to install NI software or FTP to transfer custom libraries to the RT target).

 

I guess either I have some library file missing on the cRio or I have to include some header file, but I don't know which one.

 

Any guess ?

 

Thanks,

Martin

BR,

Martin
CLD
0 Kudos
Message 3 of 7
(5,302 Views)

That's strange, these symbols are in the kernel, so there shouldn't be any way that you don't have them.  What kind of controller are you using?  I'll try to create some example code and maybe in the process of that I'll figure out the problem.

Ross Houston
Principal Software Engineer
Timing & Sync Software R&D
0 Kudos
Message 4 of 7
(5,295 Views)
Solution
Accepted by topic author mhiernaux

OK, I figured it out.  Those functions are exported as C functions, so you have to use extern "C" instead of just extern by itself as I originally did.  Once I did that I was able to compile the following code into its own .OUT file and it works just fine:

 

#include <stdio.h>
extern "C" unsigned long niTimestamp32();

extern "C" void TimeTest()
{
   unsigned long ts = niTimestamp32();
   printf("niTimestamp32() = 0x%08X\n", ts);
}

 

Good luck, and let me know if you have any more trouble.

Ross Houston
Principal Software Engineer
Timing & Sync Software R&D
0 Kudos
Message 5 of 7
(5,293 Views)

Hi Ross,

 

I'm using a cRio 9022 with LV RT 2010 installed.

 

For the toolchain, I'm using the GNU toolchain distribution provided by NI (ftp://ftp.ni.com/pub/devzone/tut/updated_vxworks63gccdist.zip).

 

What I did is simply declare the functions you mentioned as extern, then write a simple wrap function to access them.

 

Martin

BR,

Martin
CLD
0 Kudos
Message 6 of 7
(5,292 Views)

Hi Ross,

 

You're right, it works perfectly now. FYI, on the cRio 9022, the clock runs at 66MHz

 

Thanks again for your help !

 

Martin

BR,

Martin
CLD
0 Kudos
Message 7 of 7
(5,288 Views)