mbgtools-lx  4.2.8
mbgfasttstamp.c
Go to the documentation of this file.
1 
2 /**************************************************************************
3  *
4  * $Id: mbgfasttstamp.c 1.8 2018/11/15 12:12:32 martin TRASH $
5  *
6  * Description:
7  * Main file for mbgfasttstamp program which demonstrates how to access
8  * a Meinberg device via memory mapped I/O, if supported by the device.
9  *
10  * -----------------------------------------------------------------------
11  * $Log: mbgfasttstamp.c $
12  * Revision 1.8 2018/11/15 12:12:32 martin
13  * Individual MBG_MICRO_VERSION codes are now obsolete.
14  * Revision 1.7 2017/07/05 18:55:22 martin
15  * New way to maintain version information.
16  * Support build under Windows.
17  * Abort if MM access not supported on the target OS.
18  * Support signal handler to catch CTRL-C.
19  * Added options to sleep between calls.
20  * Parameters -u and -s imply -c.
21  * Use more functions from common library modules.
22  * Use codes and inline functions from mbgerror.h.
23  * Proper return codes and exit codes.
24  * Revision 1.6 2010/05/21 12:54:33 martin
25  * Print warning if no cycles supported on the target platform
26  * and thus latencies can not be computed.
27  * Revision 1.5 2009/09/29 15:02:14 martin
28  * Updated version number to 3.4.0.
29  * Revision 1.4 2009/07/24 09:50:08 martin
30  * Updated version number to 3.3.0.
31  * Revision 1.3 2009/06/19 14:01:32 martin
32  * Made print_hr_timestamp() to toolutil.c as mbg_print_hr_timestamp().
33  * Updated version number to 3.2.0.
34  * Revision 1.2 2009/03/20 11:48:19 martin
35  * Updated version number to 3.1.0.
36  * Updated copyright year to include 2009.
37  * New parameter -r lets the program read raw timestamps
38  * using the new API call mbg_get_fast_hr_timestamp().
39  * Revision 1.1 2008/12/22 11:45:01 martin
40  * Initial revision.
41  * Revision 1.1 2008/12/22 11:05:24 martin
42  * Initial revision.
43  *
44  **************************************************************************/
45 
46 // include Meinberg headers
47 #include <mbgdevio.h>
48 #include <toolutil.h> // common utility functions
49 
50 // include system headers
51 #include <stdio.h>
52 #include <stdlib.h>
53 
54 
55 #define MBG_FIRST_COPYRIGHT_YEAR 2007
56 #define MBG_LAST_COPYRIGHT_YEAR 0 // use default
57 
58 static const char *pname = "mbgfasttstamp";
59 
60 
61 #define MAX_TS_BURST 1000
62 
63 static int loops;
64 static int burst_read;
65 static int read_raw;
66 static long sleep_secs;
67 static long sleep_usecs;
68 
69 
70 
71 // The function below reads a number of time stamps and prints
72 // each timestamp immediately. This is not as fast as possible
73 // since printing the time stamp takes some time to execute.
74 // However, this can run continuously forever.
75 
76 static /*HDR*/
78 {
79  int this_loops = loops;
80  PCPS_TIME_STAMP ts;
81  int32_t hns_latency = 0;
82 
83  for (;;)
84  {
85  int rc = read_raw ? mbg_get_fast_hr_timestamp( dh, &ts ) :
86  mbg_get_fast_hr_timestamp_comp( dh, &ts, &hns_latency );
87 
88  if ( mbg_cond_err_msg( rc, "mbg_get_fast_hr_timestamp..." ) )
89  return rc;
90 
91  mbg_print_hr_timestamp( &ts, hns_latency, NULL, read_raw, 1 ); // raw timestamp?
92 
93  if ( this_loops > 0 )
94  this_loops--;
95 
96  if ( this_loops == 0 )
97  break;
98 
99  if ( sleep_secs )
100  sleep( sleep_secs );
101  else
102  if ( sleep_usecs )
103  usleep( sleep_usecs );
104 
105  // if this_loops is < 0 then loop forever
106  }
107 
108  return MBG_SUCCESS;
109 
110 } // show_fast_hr_timestamp
111 
112 
113 
114 // The function below takes a number of time stamps and saves
115 // them to a buffer. After the time stamps have been read
116 // a second loop prints the time stamps from the buffer.
117 // This way the time stamps are read in shorter intervals.
118 // However, this can not run continuously forever since
119 // the buffer size is somewhat limited.
120 
121 static /*HDR*/
123 {
125  int32_t hns_latency[MAX_TS_BURST];
126  int this_loops;
127  int i;
128 
129  this_loops = ( loops && ( loops < MAX_TS_BURST ) ) ? loops : MAX_TS_BURST;
130 
131 
132  if ( read_raw )
133  {
134  for ( i = 0; i < this_loops; i++ )
135  {
136  int rc = mbg_get_fast_hr_timestamp( dh, &ts[i] );
137 
138  if ( mbg_cond_err_msg( rc, "mbg_get_fast_hr_timestamp" ) )
139  return rc;
140  }
141  }
142  else
143  for ( i = 0; i < this_loops; i++ )
144  {
145  int rc = mbg_get_fast_hr_timestamp_comp( dh, &ts[i], &hns_latency[i] );
146 
147  if ( mbg_cond_err_msg( rc, "mbg_get_fast_hr_timestamp_comp" ) )
148  return rc;
149  }
150 
151  for ( i = 0; i < this_loops; i++ )
152  {
153  PCPS_TIME_STAMP *p_prv_ts = i ? &ts[i - 1] : NULL;
154  mbg_print_hr_timestamp( &ts[i], hns_latency[i], p_prv_ts, read_raw, 0 ); // raw timestamp?
155  }
156 
157  return MBG_SUCCESS;
158 
159 } // show_fast_hr_timestamp_burst
160 
161 
162 
163 static /*HDR*/
164 int do_mbgfasttstamp( MBG_DEV_HANDLE dh, const PCPS_DEV *p_dev )
165 {
166  int rc = mbg_chk_dev_has_fast_hr_timestamp( dh );
167 
168  if ( mbg_rc_is_error( rc ) )
169  {
170  mbg_cond_err_msg_info( rc, "mbg_chk_dev_has_fast_hr_timestamp",
171  "support fast (memory mapped) time stamps" );
172  return rc;
173  }
174 
175  if ( burst_read )
177  else
179 
180  return MBG_SUCCESS;
181 
182 } // do_mbgfasttstamp
183 
185 
186 
187 
188 static /*HDR*/
189 void usage( void )
190 {
192  "This example program reads fast high resolution time stamps.\n"
193  "\n"
194  "This is done using memory mapped I/O in the kernel driver, so\n"
195  "this works only with cards which support memory mapped I/O."
196  );
198  mbg_print_opt_info( "-c", "run continuously" );
199  mbg_print_opt_info( "-n num", "run num loops" );
200  mbg_print_opt_info( "-b", "burst read" );
201  mbg_print_opt_info( "-r", "read raw time stamps, no cycles" );
202  mbg_print_opt_info( "-s num", "sleep num seconds between calls (implies -c)" );
203  mbg_print_opt_info( "-u num", "sleep num microseconds between calls (implies -c)" );
205  puts( "" );
206 
207 } // usage
208 
209 
210 
211 int main( int argc, char *argv[] )
212 {
213  int rc;
214  int c;
215 
217 
218  // check command line parameters
219  while ( ( c = getopt( argc, argv, "bcn:rs:u:h?" ) ) != -1 )
220  {
221  switch ( c )
222  {
223  case 'b':
224  burst_read = 1;
225  break;
226 
227  case 'c':
228  loops = -1;
229  break;
230 
231  case 'n':
232  loops = atoi( optarg );
233  break;
234 
235  case 'r':
236  read_raw = 1;
237  break;
238 
239  case 's':
240  sleep_secs = atoi( optarg );
241  loops = -1;
242  break;
243 
244  case 'u':
245  sleep_usecs = atoi( optarg );
246  loops = -1;
247  break;
248 
249  case 'h':
250  case '?':
251  default:
252  must_print_usage = 1;
253  }
254  }
255 
256  if ( must_print_usage )
257  {
258  usage();
259  return MBG_EXIT_CODE_USAGE;
260  }
261 
262  #if !MBG_TGT_SUPP_MEM_ACC
263  printf( "** Memory mapped access not supported on this target platform.\n\n" );
264  return 1;
265  #endif
266 
267  #if !MBG_PC_CYCLES_SUPPORTED
268  printf( "** Warning: No cycles support to compute real latencies on this platform!\n" );
269 
270  if ( !read_raw )
271  {
272  read_raw = 1;
273  printf( "** Falling back to raw mode.\n" );
274  }
275 
276  printf( "\n" );
277  #endif
278 
279  // Handle each of the specified devices.
280  rc = mbg_handle_devices( argc, argv, optind, do_mbgfasttstamp, 0 );
281 
283 }
int mbg_handle_devices(int argc, char *argv[], int optind, MBG_DEV_HANDLER_FNC *fnc, int chk_dev_flags)
Main action handler that can be called by utility programs.
Definition: toolutil.c:655
#define MAX_TS_BURST
Definition: mbgfasttstamp.c:61
int must_print_usage
static int show_fast_hr_timestamp_burst(MBG_DEV_HANDLE dh)
static int burst_read
Definition: mbgfasttstamp.c:64
static int loops
Definition: mbgfasttstamp.c:63
Requested action completed successfully.
Definition: mbgerror.h:631
static const char * pname
Definition: mbgfasttstamp.c:58
bool mbg_cond_err_msg(int rc, const char *what)
Check if a value is an error code and print an associated error message.
Definition: mbgerror.c:714
Unable to handle requested action, usage printed.
Definition: mbgerror.h:632
#define mbg_rc_is_success(_rc)
Definition: mbgerror.h:618
_MBG_API_ATTR int _MBG_API mbg_get_fast_hr_timestamp(MBG_DEV_HANDLE dh, PCPS_TIME_STAMP *p)
Read a high resolution PCPS_TIME_STAMP structure via memory mapped access.
Definition: mbgdevio.c:7066
#define MBG_LAST_COPYRIGHT_YEAR
Definition: mbgfasttstamp.c:56
void mbg_print_hr_timestamp(PCPS_TIME_STAMP *p_ts, int32_t hns_latency, PCPS_TIME_STAMP *p_prv_ts, int no_latency, int show_raw)
Print date and time from a PCPS_TIME_STAMP structure.
Definition: toolutil.c:912
static long sleep_usecs
Definition: mbgfasttstamp.c:67
_MBG_API_ATTR int _MBG_API mbg_get_fast_hr_timestamp_comp(MBG_DEV_HANDLE dh, PCPS_TIME_STAMP *p, int32_t *hns_latency)
Read a high resolution timestamp and compensate the latency of the call.
Definition: mbgdevio.c:7017
void mbg_print_device_options(void)
Print common info on how to specify devices on the command line.
Definition: toolutil.c:270
void mbg_print_usage_intro(const char *pname, const char *info)
Print usage intro to console.
Definition: toolutil.c:217
bool mbg_cond_err_msg_info(int rc, const char *what, const char *info)
Check if a value is an general or a "not supported" error code and print an associated message...
Definition: mbgerror.c:742
static MBG_DEV_HANDLER_FNC do_mbgfasttstamp
int MBG_DEV_HANDLER_FNC(MBG_DEV_HANDLE, const PCPS_DEV *)
The type of functions to called to handle a device in a specific way.
Definition: toolutil.h:144
#define MBG_SUCCESS
Error codes used with Meinberg devices and drivers.
Definition: mbgerror.h:259
static void usage(void)
MBG_CHK_SUPP_FNC mbg_chk_dev_has_fast_hr_timestamp
Check if a device supports the mbg_get_fast_hr_timestamp... calls.
Definition: mbgdevio.c:1387
int main(int argc, char *argv[])
Action failed for specified device.
Definition: mbgerror.h:634
static long sleep_secs
Definition: mbgfasttstamp.c:66
#define MBG_FIRST_COPYRIGHT_YEAR
Definition: mbgfasttstamp.c:55
void mbg_print_help_options(void)
Print info on common program help arguments.
Definition: toolutil.c:257
A high resolution time stamp.
Definition: pcpsdefs.h:972
void mbg_print_opt_info(const char *opt_name, const char *opt_info)
Print info on a single program option / argument.
Definition: toolutil.c:236
#define mbg_rc_is_error(_rc)
Definition: mbgerror.h:617
void mbg_print_program_info(const char *pname, int first_year, int last_year)
Print program info to console.
Definition: toolutil.c:193
static int read_raw
Definition: mbgfasttstamp.c:65
Device info structure.
Definition: pcpsdev.h:1043
static int show_fast_hr_timestamp(MBG_DEV_HANDLE dh)
Definition: mbgfasttstamp.c:77