mbgtools-lx  4.2.8
myutil.h
Go to the documentation of this file.
1 
2 /**************************************************************************
3  *
4  * $Id: myutil.h 1.22 2018/11/21 16:04:27 martin TRASH $
5  *
6  * Copyright (c) Meinberg Funkuhren, Bad Pyrmont, Germany
7  *
8  * Description:
9  * Definitions and prototypes for myutil.c.
10  *
11  * -----------------------------------------------------------------------
12  * $Log: myutil.h $
13  * Revision 1.22 2018/11/21 16:04:27 martin
14  * Doxygen fixes.
15  * Revision 1.21 2018/11/01 11:10:57 martin
16  * Updated function prototypes.
17  * Revision 1.20 2017/03/22 15:09:03Z andre.hartmann
18  * added macros for lin. interpolation
19  * Revision 1.19 2017/01/27 12:24:08Z martin
20  * Moved STRINGIFY() macro to words.h.
21  * Revision 1.18 2014/10/20 12:30:38 martin
22  * Moved macro _isdigit() to words.h.
23  * Revision 1.17 2014/07/15 06:19:48Z andre
24  * added function _sgn( _x ), returns sign of _x
25  * Revision 1.16 2013/12/16 13:32:57Z martin
26  * Don't redefine MIN and MAX.
27  * Revision 1.15 2012/10/02 18:51:52 martin
28  * Updated handling of pragma pack().
29  * Revision 1.14 2011/02/16 14:02:35 martin
30  * Added STRINGIFY() macro.
31  * Revision 1.13 2010/12/13 15:59:39 martin
32  * Moved definition of macro _frac() here.
33  * Revision 1.12 2008/01/30 10:28:17Z martin
34  * Moved some macro definitions to words.h.
35  * Revision 1.11 2004/11/09 14:20:24Z martin
36  * Redefined some data types using C99 fixed-size definitions.
37  * Removed duplicate definition of macro _mask().
38  * Revision 1.10 2004/04/14 08:57:59Z martin
39  * Pack structures 1 byte aligned.
40  * Revision 1.9 2003/05/20 10:22:25Z MARTIN
41  * Corrected endianess of union UL for CC51.
42  * Revision 1.8 2002/09/03 13:40:43 MARTIN
43  * New macros _memfill() and _memclr().
44  * Revision 1.7 2002/03/14 13:45:56 MARTIN
45  * Changed type CSUM from short to ushort.
46  * Revision 1.6 2002/03/05 14:14:21 MARTIN
47  * New macro _isdigit() to avoid inclusion of ctype.h.
48  * Revision 1.5 2002/01/25 10:54:26 MARTIN
49  * Added some useful macros.
50  * Revision 1.4 2001/03/30 09:07:52 Andre
51  * union UL byte order set to Big Endian if SH2 is used
52  * Revision 1.3 2000/08/18 07:22:07Z MARTIN
53  * Modified the _csum() macro to support far data objects.
54  * Revision 1.2 2000/07/21 13:50:49 MARTIN
55  * Added some definitions and macros.
56  *
57  **************************************************************************/
58 
59 #ifndef _MYUTIL_H
60 #define _MYUTIL_H
61 
62 
63 /* Other headers to be included */
64 
65 #include <words.h>
66 #include <use_pack.h>
67 
68 
69 // _CS_FAR should be define far if the csum of far data
70 // structures must be computed
71 #if !defined( _CSFAR )
72  #define _CSFAR
73 #endif
74 
75 
76 #ifdef _MYUTIL
77  #define _ext
78 #else
79  #define _ext extern
80 #endif
81 
82 
83 /* Start of header body */
84 
85 #if defined( _USE_PACK )
86  #pragma pack( 1 ) // set byte alignment
87  #define _USING_BYTE_ALIGNMENT
88 #endif
89 
90 
91 #if MBG_TGT_HAS_64BIT_TYPES
92  #define _frac( _x ) ( ( (_x) == 0.0 ) ? 0.0 : ( (_x) - (double) ( (int64_t) (_x) ) ) )
93 #else
94  #define _frac( _x ) ( ( (_x) == 0.0 ) ? 0.0 : ( (_x) - (double) ( (long) (_x) ) ) )
95 #endif
96 
97 
98 #define _eos( _s ) ( &(_s)[strlen( _s )] )
99 
100 #if !defined( MIN )
101  #define MIN( _x, _y ) ( ( (_x) < (_y) ) ? (_x) : (_y) )
102 #endif
103 
104 #if !defined( MAX )
105  #define MAX( _x, _y ) ( ( (_x) > (_y) ) ? (_x) : (_y) )
106 #endif
107 
108 #define SWAP( _x, _y ) { temp = (_x); (_x) = (_y); (_y) = temp; }
109 #define SQR( _x ) ( (_x) * (_x) )
110 
111 #define DP (double *)
112 
113 #define bcd_from_bin( _x ) ( ( ( (_x) / 10 ) << 4 ) | ( (_x) % 10 ) )
114 #define bin_from_bcd( _x ) ( ( ( (_x) >> 4 ) * 10 ) + ( (_x) & 0x0F ) )
115 
116 
117 typedef union
118 {
119  uint32_t ul;
120 
121  struct
122  {
123  #if defined( _CC51 ) || defined( _SH2 )
124  uint16_t hi; // big endian
125  uint16_t lo;
126  #else
127  uint16_t lo; // little endian
129  #endif
130  } us;
131 
132 } UL;
133 
134 
135 #ifndef _CSUM_DEFINED
136  typedef uint16_t CSUM;
137  #define _CSUM_DEFINED
138 #endif
139 
140 
141 // compute the csum of a structure
142 #define _csum( _p ) checksum( (void _CSFAR *)(_p), sizeof( *(_p) ) )
143 
144 // set a structure's csum
145 #define _set_csum( _p ) (_p)->csum = _csum( (_p) )
146 
147 // compare a structure's computed csum with its csum field
148 #define _valid_csum( _p ) ( (_p)->csum == _csum( (_p) ) )
149 
150 // check if a value is in range
151 #define _inrange( _val, _min, _max ) \
152  ( ( (_val) >= (_min) ) && ( (_val) <= (_max) ) )
153 
154 // Return a bit mask with (_n) LSBs set to 1
155 #define _mask( _n ) \
156  ( ( 1UL << (_n) ) - 1 )
157 
158 // Return a bit mask with the (_i)th LSB set to 1
159 #define _idx_bit( _i ) \
160  ( 1UL << (_i) )
161 
162 // Check if the (_i)th bit is set in a mask (_msk)
163 #define _is_supported( _i, _msk ) \
164  ( ( (_msk) & _idx_bit( _i ) ) != 0 )
165 
166 // return the sign of a number
167 #define _sgn( _x ) ( ( ( _x ) < 0 ) ? -1 : 1 )
168 
169 
170 // macro for linear interpolation y = _m * x + _b between two points ( X1; Y1 ), ( X2; Y2 )
171 #define _m( _Y1, _X1, _Y2, _X2 ) ( ( _Y2 -_Y1 ) / ( _X2 -_X1 ) )
172 #define _b( _Y1, _X1, _Y2, _X2 ) ( ( ( _Y1 * _X2 ) - ( _Y2 * _X1 ) ) / ( _X2 -_X1 ) )
173 
174 
175 /*
176  * The macro below copies a string, taking care not to
177  * write past the end of the destination buffer, and
178  * making sure the string is terminated by 0.
179  */
180 #define _strncpy_0( _dst, _src ) \
181 { \
182  int n = sizeof( _dst ) - 1; \
183  \
184  strncpy( _dst, _src, n ); \
185  (_dst)[n] = 0; \
186 }
187 
188 
189 /*
190  * The macros below set a memory range used by a variable
191  * to a specified value, avoiding the need to type the name
192  * twice for base address and size.
193  */
194 #define _memfill( _p, _v ) \
195  memset( _p, _v, sizeof( *(_p) ) )
196 
197 #define _memclr( _p ) \
198  _memfill( _p, 0 )
199 
200 
201 
202 // generate a DOS idle interrupt to release CPU time
203 #define _dos_idle() geninterrupt( 0x28 )
204 
205 
206 #ifdef __cplusplus
207 extern "C" {
208 #endif
209 
210 /* ----- function prototypes begin ----- */
211 
212 /* This section was generated automatically */
213 /* by MAKEHDR, do not remove the comments. */
214 
220  void spaces_to_zeros( char *s ) ;
221 
230  CSUM checksum( const void _CSFAR *vp, int n ) ;
231 
232 
233 /* ----- function prototypes end ----- */
234 
235 #ifdef __cplusplus
236 }
237 #endif
238 
239 
240 #if defined( _USING_BYTE_ALIGNMENT )
241  #pragma pack() // set default alignment
242  #undef _USING_BYTE_ALIGNMENT
243 #endif
244 
245 /* End of header body */
246 
247 
248 #undef _ext
249 
250 
251 #endif /* _MYUTIL_H */
uint16_t CSUM
Definition: myutil.h:136
uint16_t CSUM
checksum used by some structures stored in non-volatile memory
Definition: gpsdefs.h:705
unsigned short uint16_t
Definition: words.h:213
uint16_t hi
Definition: myutil.h:128
void spaces_to_zeros(char *s)
Replace whitespace &#39; &#39; characters by 0 characters.
uint16_t lo
Definition: myutil.h:127
Definition: myutil.h:117
CSUM checksum(const void *vp, int n)
Create a simple checksum.
#define _CSFAR
Definition: myutil.h:72
uint32_t ul
Definition: myutil.h:119