mrfioc2  2.3.0
mrfCommon.h
Go to the documentation of this file.
1 /***************************************************************************************************
2 |* mrfCommon.h -- Micro-Research Finland (MRF) Event System Series Common Defintions
3 |*
4 |*--------------------------------------------------------------------------------------------------
5 |* Author: Eric Bjorklund
6 |* Date: 19 October 2009
7 |*
8 |*--------------------------------------------------------------------------------------------------
9 |* MODIFICATION HISTORY:
10 |* 19 Oct 2008 E.Bjorklund Adapted from the original software for the APS Register Map
11 |*
12 |*--------------------------------------------------------------------------------------------------
13 |* MODULE DESCRIPTION:
14 |* This header file contains various constants and defintions used by the Micro Research Finland
15 |* event system. The definitions in this file are used by both driver and device support modules,
16 |* as well as user code that calls the device support interface.
17 |*
18 |*------------------------------------------------------------------------------
19 |* HARDWARE SUPPORTED:
20 |* Series 2xx Event Generator and Event Receiver Cards
21 |* APS Register Mask
22 |* Modular Register Mask
23 |*
24 |*------------------------------------------------------------------------------
25 |* OPERATING SYSTEMS SUPPORTED:
26 |* vxWorks
27 |* RTEMS
28 |*
29 \**************************************************************************************************/
30 
31 /**************************************************************************************************
32 |* COPYRIGHT NOTIFICATION
33 |**************************************************************************************************
34 |*
35 |* THE FOLLOWING IS A NOTICE OF COPYRIGHT, AVAILABILITY OF THE CODE,
36 |* AND DISCLAIMER WHICH MUST BE INCLUDED IN THE PROLOGUE OF THE CODE
37 |* AND IN ALL SOURCE LISTINGS OF THE CODE.
38 |*
39 |**************************************************************************************************
40 |*
41 |* This software is distributed under the EPICS Open License Agreement which
42 |* can be found in the file, LICENSE, included with this distribution.
43 |*
44 \*************************************************************************************************/
45 
46 #ifndef MRF_COMMON_H
47 #define MRF_COMMON_H
48 
49 #ifdef __cplusplus
50 #include <ostream>
51 #include <sstream>
52 #include <string>
53 #include <memory>
54 
55 // ugly hack to avoid copious deprecation warnings when building c++11
56 namespace mrf {
57 #if __cplusplus>=201103L
58 template<typename T>
59 using auto_ptr = std::unique_ptr<T>;
60 #define PTRMOVE(AUTO) std::move(AUTO)
61 #else
62 using std::auto_ptr;
63 #define PTRMOVE(AUTO) (AUTO)
64 #endif
65 }
66 
67 #endif
68 
69 /**************************************************************************************************/
70 /* Include Header Files from the Common Utilities */
71 /**************************************************************************************************/
72 
73 #include <epicsVersion.h> /* EPICS Version definition */
74 #include <epicsTypes.h> /* EPICS Architecture-independent type definitions */
75 #include <epicsTime.h> /* EPICS Time definitions */
76 #include <epicsMath.h> /* EPICS Common math functions & definitions */
77 #include <epicsInterrupt.h>
78 #include <epicsStdlib.h>
79 #include <epicsThread.h>
80 
81 #include <alarm.h> /* EPICS Alarm status and severity definitions */
82 #include <dbAccess.h> /* EPICS Database Access definitions */
83 #include <dbCommon.h> /* EPICS Common record field definitions */
84 #include <devSup.h> /* EPICS Device support messages and definitions */
85 #include <recGbl.h> /* EPICS recGblRecordError function */
86 #include <menuYesNo.h> /* EPICS Yes/No record-support menu */
87 
88 #include <limits.h> /* Standard C numeric limits */
89 
90 #include <shareLib.h>
91 
92 /**************************************************************************************************/
93 /* MRF Event System Constants */
94 /**************************************************************************************************/
95 
96 #define MRF_NUM_EVENTS 256 /* Number of possible events */
97 #define MRF_EVENT_FIFO_SIZE 512 /* Size of EVR/EVG event FIFO */
98 #define MRF_MAX_DATA_BUFFER 2048 /* Maximum size of the distributed data buffer */
99 #define MRF_FRAC_SYNTH_REF 24.0 /* Fractional Synth reference frequency (MHz). */
100 #define MRF_DEF_CLOCK_SPEED 125.0 /* Default event clock speed is 125 MHz. */
101 #define MRF_SN_BYTES 6 /* Number of bytes in serial number */
102 #define MRF_SN_STRING_SIZE 18 /* Size of serial number string (including NULL) */
103 #define MRF_DESCRIPTION_SIZE 80 /* Size of description text string (inclucing NULL)*/
104 
105 /* Event system codes with special meanings.
106  */
107 
108 /* The idle event. Not usable */
109 #define MRF_EVENT_NULL 0x00
110 /* Sending 0x70 or 0x71 will cause the value in the seconds shift register to
111  * be shifted up by 1. The low bit will be set to 0 or 1 as appropriate.
112  */
113 #define MRF_EVENT_TS_SHIFT_0 0x70
114 #define MRF_EVENT_TS_SHIFT_1 0x71
115 /* Reset the heartbeat timeout counter */
116 #define MRF_EVENT_HEARTBEAT 0x7A
117 /* Reset prescaler dividers. Synchronizes the phase of all frequency outputs */
118 #define MRF_EVENT_RST_PRESCALERS 0x7B
119 /* Increment the fractional part of the timestamp when TS source is Mapped Code */
120 #define MRF_EVENT_TS_COUNTER_INC 0x7C
121 /* Zeros the fractional part of TS, and copys the seconds shift register to the
122  * primary seconds register.
123  */
124 #define MRF_EVENT_TS_COUNTER_RST 0x7D
125 /* Special code for use in sequencer. Used in other contexts is not recommended. */
126 #define MRF_EVENT_END_OF_SEQUENCE 0x7F
127 
128 
131 #define ER_PROVIDER_PRIORITY 50
132 
133 /**************************************************************************************************/
134 /* MRF Supported Bus Types */
135 /**************************************************************************************************/
136 
137 #define MRF_BUS_COMPACT_PCI 0 /* 0 = Compact PCI (3U) */
138 #define MRF_BUS_PMC 1 /* 1 = PMC */
139 #define MRF_BUS_VME 2 /* 2 = VME 64x */
140 
141 
142 /**************************************************************************************************/
143 /* MRF Board Types */
144 /**************************************************************************************************/
145 
146 #define MRF_CARD_TYPE_EVR 1 /* 1 = Event Receiver */
147 #define MRF_CARD_TYPE_EVG 2 /* 2 = Event Generator */
148 
149 
150 /**************************************************************************************************/
151 /* MRF Board Series Codes */
152 /**************************************************************************************************/
153 
154 #define MRF_SERIES_200 0x000000C8 /* Series 200 Code (in Hex) */
155 #define MRF_SERIES_220 0x000000DC /* Series 220 Code (in Hex) */
156 #define MRF_SERIES_230 0x000000E6 /* Series 230 Code (in Hex) */
157 
158 
159 /**************************************************************************************************/
160 /* Site-Specific Defaults */
161 /* (these parameters take their values from the MRF_CONFIG_SITE* files) */
162 /**************************************************************************************************/
163 
164 /*=====================
165  * Default Event Clock Frequency (in MegaHertz)
166  */
167 #ifdef EVENT_CLOCK_FREQ
168  #define EVENT_CLOCK_DEFAULT EVENT_CLOCK_FREQ /* Use site-selected event clock speed */
169 #else
170  #define EVENT_CLOCK_DEFAULT 0.00 /* Defaults to cntrl word value or 125.0 */
171 #endif
172 
173 /**************************************************************************************************/
174 /* Function Prototype Definitions */
175 /**************************************************************************************************/
176 
177 #ifdef __cplusplus
178 
179 /* C++11 keywords
180  @code
181  struct Base {
182  virtual void foo();
183  };
184  struct Class : public Base {
185  virtual void foo() OVERRIDE FINAL;
186  };
187  @endcode
188  */
189 #ifndef FINAL
190 # if __cplusplus>=201103L
191 # define FINAL final
192 # else
193 # define FINAL
194 # endif
195 #endif
196 #ifndef OVERRIDE
197 # if __cplusplus>=201103L
198 # define OVERRIDE override
199 # else
200 # define OVERRIDE
201 # endif
202 #endif
203 
204 template<class Mutex>
205 class scopedLock
206 {
207  Mutex& m;
208  bool locked;
209 public:
210  scopedLock(Mutex& mutex, bool lock=true) : m(mutex), locked(lock)
211  {
212  if (lock) m.lock();
213  }
214  ~scopedLock()
215  {
216  unlock();
217  }
218  inline void lock(){if (!locked) m.lock();locked=true;}
219  inline void unlock(){if (locked) m.unlock();locked=false;}
220 };
221 #define SCOPED_LOCK2(m, name) scopedLock<epicsMutex> name(m)
222 #define SCOPED_LOCK(m) SCOPED_LOCK2(m, m##_guard)
223 
224 class interruptLock
225 {
226  int key;
227 public:
228  interruptLock()
229  :key(epicsInterruptLock())
230  {}
231  ~interruptLock()
232  { epicsInterruptUnlock(key); }
233 };
234 
235 
236 // inline string builder
237 // std::string X(SB()<<"test "<<4);
238 struct SB {
239  std::ostringstream strm;
240  SB() {}
241  operator std::string() const { return strm.str(); }
242  template<typename T>
243  SB& operator<<(T i) { strm<<i; return *this; }
244 };
245 
246 /* MRF firmware version numbers have become interesting.
247  *
248  * Storage is 0x__CCAABB
249  * AA - Firmware ID
250  * BB - Revision ID
251  * CC - Subrelease ID
252  *
253  * To quote the manual:
254  * Subrelease ID For production releases the subrelease ID counts up from 00.
255  * For pre-releases this ID is used “backwards” counting down from ff i.e. when
256  * approacing release 12000207, we have prereleases 12FF0206, 12FE0206,
257  * 12FD0206 etc. in this order.
258  */
259 class epicsShareClass MRFVersion
260 {
261  const epicsUInt16 m_major;
262  const epicsInt8 m_minor;
263 public:
264 
265  explicit MRFVersion(epicsUInt32 regval)
266  :m_major(regval&0xffff) ,m_minor((regval>>16)&0xff)
267  {}
268  inline MRFVersion(unsigned fw, unsigned rev, unsigned sub=0)
269  :m_major((fw<<8)|rev), m_minor(sub)
270  {}
271 
272  inline unsigned firmware() const { return m_major>>8; }
273  inline unsigned revision() const { return m_major&0xff; }
274  inline int subrelease() const { return m_minor; }
275 
276  int compare(const MRFVersion& o) const;
277  inline bool operator>(const MRFVersion& o) const { return compare(o)==1; }
278  inline bool operator<(const MRFVersion& o) const { return compare(o)==-1; }
279  inline bool operator==(const MRFVersion& o) const { return compare(o)==0; }
280  inline bool operator>=(const MRFVersion& o) const { return compare(o)!=-1; }
281  inline bool operator<=(const MRFVersion& o) const { return compare(o)!=1; }
282 
283  std::string str() const;
284 };
285 
286 epicsShareExtern
287 std::ostream& operator<<(std::ostream& strm, const MRFVersion& ver);
288 
289 
291 template<class C,void (C::*Method)()>
292 class epicsShareClass epicsThreadRunableMethod : public epicsThreadRunable
293 {
294  C& owner;
295 public:
296  explicit epicsThreadRunableMethod(C& o)
297  :owner(o)
298  {}
299  virtual ~epicsThreadRunableMethod(){}
300  virtual void run()
301  {
302  (owner.*Method)();
303  }
304 };
305 
306 #endif /* __cplusplus */
307 
308 /**************************************************************************************************/
309 /* Definitions for Compatibiliby with Older Versions of EPICS */
310 /**************************************************************************************************/
311 
312 
313 /*---------------------
314  * Macros for version comparison
315  */
316 #ifndef VERSION_INT
317 # define VERSION_INT(V,R,M,P) ( ((V)<<24) | ((R)<<16) | ((M)<<8) | (P))
318 # define EPICS_VERSION_INT VERSION_INT(EPICS_VERSION, EPICS_REVISION, EPICS_MODIFICATION, EPICS_PATCH_LEVEL)
319 #endif
320 
321 /*---------------------
322  * epicsMath.h defines "finite()" for vxWorks, but "isfinite()" is the standard.
323  * finite() does not appear to be supported in epicsMath.h for all architectures.
324  */
325 #ifndef isfinite
326 # define isfinite finite
327 #endif
328 
329 #ifdef __cplusplus
330 /* Round down and convert float to unsigned int
331  * throws std::range_error for NaN and out of range inputs
332  */
333 epicsShareFunc epicsUInt32 roundToUInt(double val, epicsUInt32 maxresult=0xffffffff);
334 
335 epicsShareFunc char *allocSNPrintf(size_t N, const char *fmt, ...) EPICS_PRINTF_STYLE(2,3);
336 #endif
337 
338 /**************************************************************************************************/
339 /* Make Sure That EPICS 64-Bit Integer Types Are Defined */
340 /**************************************************************************************************/
341 
342 /*---------------------
343  * If we are using an ISO C99 compliant compiler and the EPICS version is 3.14.9 or above,
344  * then EPICS 64-bit integer types have already been defined by the epicsTypes.h file.
345  * We can quit now.
346  */
347 #if (EPICS_VERSION_INT < VERSION_INT(3,15,0,2))
348 #if (__STDC_VERSION__ < 19990L) || (EPICS_VERSION_INT < VERSION_INT(3,14,9,0))
349 
350  /*---------------------
351  * If are using an ISO C99 compliant compiler, then we can get to the int64_t and uint64_t
352  * definitions through inttypes.h
353  */
354 # if __STDC_VERSION__ >= 1990L
355 # include <inttypes.h>
356  typedef int64_t epicsInt64;
357  typedef uint64_t epicsUInt64;
358 
359  /*---------------------
360  * If we are not using an ISO C99 compliant compiler, define the 64 bit integer types
361  * based on whether this is a 32-bit or 64-bit architecture.
362  */
363 # elif LONG_MAX > 0x7fffffffL
364  typedef long epicsInt64;
365  typedef unsigned long epicsUInt64;
366 # else
367  typedef long long epicsInt64;
368  typedef unsigned long long epicsUInt64;
369 # endif
370 
371 #endif /*EPICS 64-bit integer types need defining*/
372 
373 #define M_stdlib (527 <<16) /*EPICS Standard library*/
374 
375 #define S_stdlib_noConversion (M_stdlib | 1) /* No digits to convert */
376 #define S_stdlib_extraneous (M_stdlib | 2) /* Extraneous characters */
377 #define S_stdlib_underflow (M_stdlib | 3) /* Too small to represent */
378 #define S_stdlib_overflow (M_stdlib | 4) /* Too large to represent */
379 #define S_stdlib_badBase (M_stdlib | 5) /* Number base not supported */
380 
381 #ifdef __cplusplus
382 extern "C" {
383 #endif
384 epicsShareFunc int
385 epicsParseUInt32(const char *str, epicsUInt32 *to, int base, char **units);
386 #ifdef __cplusplus
387 }
388 #endif
389 
390 #endif
391 
392 
393 #endif
std::ostream & operator<<(std::ostream &strm, const MRFVersion &ver)
Definition: mrfCommon.cpp:33
epicsMutex mutex
Definition: spi.cpp:56
char * allocSNPrintf(size_t N, const char *fmt,...)
Definition: mrfCommon.cpp:59
unsigned long long epicsUInt64
Definition: mrfCommon.h:368
epicsShareFunc int epicsParseUInt32(const char *str, epicsUInt32 *to, int base, char **units)
Definition: mrfCommon.cpp:113
epicsUInt32 roundToUInt(double val, epicsUInt32 max)
Definition: mrfCommon.cpp:43
Definition: flash.cpp:23
long long epicsInt64
Definition: mrfCommon.h:367