| 1 |
cafagna |
1.1 |
/****************************************************************************
|
| 2 |
|
|
* F i l e D a t a
|
| 3 |
|
|
* $Id: CRC.h,v 1.6 2004/03/16 10:18:28 nagni Exp $
|
| 4 |
|
|
* $Revision: 1.6 $
|
| 5 |
|
|
* $Date: 2004/03/16 10:18:28 $
|
| 6 |
|
|
* $RCSfile: CRC.h,v $
|
| 7 |
|
|
*
|
| 8 |
|
|
****************************************************************************
|
| 9 |
|
|
* S W D e v e l o p m e n t E n v i r o n m e n t
|
| 10 |
|
|
*
|
| 11 |
|
|
* $Author: nagni $
|
| 12 |
|
|
* :
|
| 13 |
|
|
*****************************************************************************/
|
| 14 |
|
|
|
| 15 |
|
|
#ifndef CRC_Hr
|
| 16 |
|
|
#define CRC_Hr
|
| 17 |
|
|
|
| 18 |
|
|
#define BYTE unsigned char
|
| 19 |
|
|
#define UINT32 unsigned int
|
| 20 |
|
|
#define UINT16 unsigned short
|
| 21 |
|
|
|
| 22 |
|
|
|
| 23 |
|
|
/** Example of CAST macro at work. public domain demo by Bob Stout.
|
| 24 |
|
|
*
|
| 25 |
|
|
* Example of CAST macro at work
|
| 26 |
|
|
*
|
| 27 |
|
|
* union {
|
| 28 |
|
|
* char ch[4];
|
| 29 |
|
|
* int i[2];
|
| 30 |
|
|
* } my_union;
|
| 31 |
|
|
*
|
| 32 |
|
|
* long longvar;
|
| 33 |
|
|
*
|
| 34 |
|
|
* longvar = (long)my_union; Illegal cast
|
| 35 |
|
|
* longvar = CAST(long, my_union); Legal cast
|
| 36 |
|
|
*
|
| 37 |
|
|
*/
|
| 38 |
|
|
#define CM_CAST(new_type,old_object) (*((new_type *)&(old_object)))
|
| 39 |
|
|
|
| 40 |
|
|
|
| 41 |
|
|
#define CM_HI_UINT16(x) ( (BYTE)( ((x) & 0xff00)>>8 ) )
|
| 42 |
|
|
#define CM_LO_UINT16(x) ( (BYTE)( ((x) & 0x00ff) ) )
|
| 43 |
|
|
|
| 44 |
|
|
#define CM_HI_UINT32(x) ( (UINT16)( ((x) & 0xffff0000)>>16 ) )
|
| 45 |
|
|
#define CM_LO_UINT32(x) ( (UINT16)( ((x) & 0x0000ffff) ) )
|
| 46 |
|
|
|
| 47 |
|
|
|
| 48 |
|
|
#define CM_HIHI_UINT32(x) ( (BYTE)( ((x) & 0xff000000)>>24 ) )
|
| 49 |
|
|
#define CM_LOHI_UINT32(x) ( (BYTE)( ((x) & 0x00ff0000)>>16 ) )
|
| 50 |
|
|
#define CM_HILO_UINT32(x) ( (BYTE)( ((x) & 0x0000ff00)>> 8 ) )
|
| 51 |
|
|
#define CM_LOLO_UINT32(x) ( (BYTE)( ((x) & 0x000000ff) ) )
|
| 52 |
|
|
|
| 53 |
|
|
|
| 54 |
|
|
|
| 55 |
|
|
/**
|
| 56 |
|
|
Macro READ_NEXT_BITS_UINT(wordlen,p,offset,n,res)
|
| 57 |
|
|
|
| 58 |
|
|
this mascro scans bits for a 'wordlen'-bit long word. 'wordlen' can be 8,16,32...
|
| 59 |
|
|
It reads the next 'n' bits starting after the 'offset'-th bit of the 'wordlen'-bit word pointed by p.
|
| 60 |
|
|
store the result in 'res', and increment properly both 'offset' and 'p' such that they can be reused
|
| 61 |
|
|
in the text invocation of the macro.
|
| 62 |
|
|
|
| 63 |
|
|
\a p unsigned 'wordlen'-bit pointer (ie. wordlen==16, then unsinged short int * in i386 arch)
|
| 64 |
|
|
varibale name.
|
| 65 |
|
|
it points to the word to scan. it is incremented automatically by the macro.
|
| 66 |
|
|
\a offset unsigned 8 bit variable name (unsigned char?). Must be an l-value.
|
| 67 |
|
|
it holds the number of bit already read in the most significant part of the
|
| 68 |
|
|
current pointer (*p). Should be used always the same variable while a scanning
|
| 69 |
|
|
session. This value must be initialized to zero and should be always less then 'wordlen'.
|
| 70 |
|
|
\a n the number of bits to read. Can be a r-value. Must be 0 <= n <= 'wordlen'.
|
| 71 |
|
|
if n is zero (senseless), both 'p' and 'offset' are unchanged and 0 is returned in 'res'.
|
| 72 |
|
|
\a res unsigned 'wordlen'-bit variable name (i.e wordlen==16, then unsigned short int? on i386 arch)
|
| 73 |
|
|
in which to store the requested 'n' bits.
|
| 74 |
|
|
they are returned in the less significat part of 'res'. If n<'wordlen', then the most
|
| 75 |
|
|
significant bits of 'res' are padded to zero.
|
| 76 |
|
|
|
| 77 |
|
|
before using he macro the first time you have to: (let's assume 'wordlen' fixed to 16 and an i386 arch.)
|
| 78 |
|
|
1) choose an unsigned short pointer to pass as 'p', and initialize it to the sequence
|
| 79 |
|
|
of bits to scan from.
|
| 80 |
|
|
2) choose an unsigned char variable to pass as 'offset' and initialize it to zero
|
| 81 |
|
|
(or some other value X<16, if you want to skip the first X bits)
|
| 82 |
|
|
3) choose an unsigned short to return the result.
|
| 83 |
|
|
|
| 84 |
|
|
*/
|
| 85 |
|
|
|
| 86 |
|
|
#define CM_READ_NEXT_BITS_UINT(wordlen,p,offset,n,res) \
|
| 87 |
|
|
do { \
|
| 88 |
|
|
res = (*p << offset); \
|
| 89 |
|
|
res >>= (wordlen-(n)); \
|
| 90 |
|
|
if(n<=wordlen-offset) { \
|
| 91 |
|
|
offset=(offset+(n))%wordlen; \
|
| 92 |
|
|
if(offset==0) \
|
| 93 |
|
|
p++; \
|
| 94 |
|
|
}else{ \
|
| 95 |
|
|
p++; \
|
| 96 |
|
|
res |= *p>>(wordlen*2-offset-(n)); \
|
| 97 |
|
|
offset+=(n)-wordlen; \
|
| 98 |
|
|
} \
|
| 99 |
|
|
}while(0)
|
| 100 |
|
|
|
| 101 |
|
|
/* Wrapper to 8,16,32,64 bit link wrapper to READ_NEXT_BITS_UINT : */
|
| 102 |
|
|
#define CM_READ_NEXT_BITS_UINT8(p,offset,n,res) CM_READ_NEXT_BITS_UINT(8 ,p,offset,n,res)
|
| 103 |
|
|
#define CM_READ_NEXT_BITS_UINT16(p,offset,n,res) CM_READ_NEXT_BITS_UINT(16,p,offset,n,res)
|
| 104 |
|
|
#define CM_READ_NEXT_BITS_UINT32(p,offset,n,res) CM_READ_NEXT_BITS_UINT(32,p,offset,n,res)
|
| 105 |
|
|
#define CM_READ_NEXT_BITS_UINT64(p,offset,n,res) CM_READ_NEXT_BITS_UINT(64,p,offset,n,res)
|
| 106 |
|
|
|
| 107 |
|
|
#define CM_GET_BIT(exp,n) (((exp) >> ((n)-1)) & 0x1)
|
| 108 |
|
|
|
| 109 |
|
|
|
| 110 |
|
|
|
| 111 |
|
|
|
| 112 |
|
|
/* Thees macros write a 16 or 32 bits in BigEndian fascion in a (unsigned char*) avoiding the
|
| 113 |
|
|
addressing alignement problem and also increment ptr
|
| 114 |
|
|
- ptr must be a (unsigned char*) pointer (l-value)
|
| 115 |
|
|
- byte must be a (unsigned char) (r-value)
|
| 116 |
|
|
- word must be a (unsigned short int) (r-value)
|
| 117 |
|
|
- dword must be a (unsigned int) (r-value)
|
| 118 |
|
|
*/
|
| 119 |
|
|
#define CM_WRITE_BE_UINT8(ptr,byte) do { *ptr = (unsigned char)(byte); ptr++;} while(0)
|
| 120 |
|
|
|
| 121 |
|
|
#define CM_WRITE_BE_UINT16(ptr,word) do { \
|
| 122 |
|
|
CM_WRITE_BE_UINT8(ptr,((word)>>8)); \
|
| 123 |
|
|
CM_WRITE_BE_UINT8(ptr,(word)); \
|
| 124 |
|
|
} while(0)
|
| 125 |
|
|
|
| 126 |
|
|
#define CM_WRITE_BE_UINT32(ptr,dword) do { \
|
| 127 |
|
|
CM_WRITE_BE_UINT8(ptr,(dword)>>24); \
|
| 128 |
|
|
CM_WRITE_BE_UINT8(ptr,(dword)>>16); \
|
| 129 |
|
|
CM_WRITE_BE_UINT8(ptr,(dword)>>8); \
|
| 130 |
|
|
CM_WRITE_BE_UINT8(ptr,(dword)); \
|
| 131 |
|
|
} while(0)
|
| 132 |
|
|
|
| 133 |
|
|
/* Thees macros read a 16 or 32 bits in BigEndian fascion from a (unsigned char*) avoiding the
|
| 134 |
|
|
addressing alignement problem and also increment ptr
|
| 135 |
|
|
- ptr must be a (unsigned char*) pointer (l-value)
|
| 136 |
|
|
- byte and temp must be a (unsigned char) (l-value)
|
| 137 |
|
|
- word must be a (unsigned short int) (l-value)
|
| 138 |
|
|
- dword must be a (unsigned int) (l-value)
|
| 139 |
|
|
*/
|
| 140 |
|
|
#define CM_READ_BE_UINT8(ptr,byte) do { byte = *(ptr); (ptr)++; } while(0)
|
| 141 |
|
|
|
| 142 |
|
|
#define CM_READ_BE_UINT16(ptr,word,temp) do { \
|
| 143 |
|
|
CM_READ_BE_UINT8(ptr,temp); \
|
| 144 |
|
|
word = ((unsigned short int)temp) << 8; \
|
| 145 |
|
|
CM_READ_BE_UINT8(ptr,temp); \
|
| 146 |
|
|
word |= temp; \
|
| 147 |
|
|
} while(0)
|
| 148 |
|
|
|
| 149 |
|
|
#define CM_READ_BE_UINT32(ptr,word,temp) do { \
|
| 150 |
|
|
CM_READ_BE_UINT8(ptr,temp); \
|
| 151 |
|
|
word = ((unsigned int)temp) << 24; \
|
| 152 |
|
|
CM_READ_BE_UINT8(ptr,temp); \
|
| 153 |
|
|
word |= ((unsigned int)temp) << 16; \
|
| 154 |
|
|
CM_READ_BE_UINT8(ptr,temp); \
|
| 155 |
|
|
word |= ((unsigned int)temp) << 8; \
|
| 156 |
|
|
CM_READ_BE_UINT8(ptr,temp); \
|
| 157 |
|
|
word |= ((unsigned int)temp); \
|
| 158 |
|
|
} while(0)
|
| 159 |
|
|
|
| 160 |
|
|
|
| 161 |
|
|
/** Univeral BIT converter to 2 registers set with different resolution.
|
| 162 |
|
|
Used in Microsecond to register converte for data time out and event time
|
| 163 |
|
|
out and other same-style register.
|
| 164 |
|
|
|
| 165 |
|
|
\a v is the value in some unit
|
| 166 |
|
|
\a unit_h is the resolution of the MSD-part. must be in same unit of v.
|
| 167 |
|
|
\a no_bit_h is the number of bits of the MSD-part.
|
| 168 |
|
|
\a unit_l is the resolution of the LSD-part. must be in same unit of v.
|
| 169 |
|
|
\a no_bit_l is the number of bits of the LSD-part.
|
| 170 |
|
|
|
| 171 |
|
|
\example (case of ETO)
|
| 172 |
|
|
Suppose to have 2 8-bit registers ETO1 (8 bit,LSB) and ETO2 (8 bit,HSB):
|
| 173 |
|
|
ETO2 have a resoluion of 65 microsec;
|
| 174 |
|
|
ETOHSB have a resolution of 16 millisec;
|
| 175 |
|
|
|
| 176 |
|
|
This will format a value of 1 millisecond (unit is in microsecond)
|
| 177 |
|
|
the usage is: CM_TIME2REG(1000,16*1000,8,65,8)
|
| 178 |
|
|
|
| 179 |
|
|
*/
|
| 180 |
|
|
|
| 181 |
|
|
//#define CM_TIME2REG(v,unit_h,no_bit_h,unit_l,no_bit_l) ((((v)/(unit_h))<<(no_bit_l) ) | ((((v)%(unit_h))/(unit_l)) & ~((0xffffffff)<<((no_bit_l)+(no_bit_h)))))
|
| 182 |
|
|
|
| 183 |
|
|
//#define CM_INT_UNUSED 0
|
| 184 |
|
|
|
| 185 |
|
|
|
| 186 |
|
|
/**
|
| 187 |
|
|
Compute a 8 bit CRC based on a \a data, whith a \a old pre-computed crc.
|
| 188 |
|
|
*/
|
| 189 |
|
|
/** old ---> the old pre-computed crc */
|
| 190 |
|
|
/** data ---> the data to compute crc for */
|
| 191 |
|
|
BYTE CM_crc8_8(BYTE old, BYTE data);
|
| 192 |
|
|
|
| 193 |
|
|
UINT32 CM_Compute_CRC8_8(UINT32 oldcrc
|
| 194 |
|
|
,BYTE *buffer
|
| 195 |
|
|
,UINT32 length
|
| 196 |
|
|
);
|
| 197 |
|
|
|
| 198 |
|
|
|
| 199 |
|
|
UINT16 CM_CRC16(BYTE* adrs, UINT16 Crc);
|
| 200 |
|
|
UINT16 CM_Compute_CRC16(UINT16 oldcrc,BYTE *buffer,UINT32 length);
|
| 201 |
|
|
|
| 202 |
|
|
BYTE* charToUnsignedChar(char buffer[], UINT32 length);
|
| 203 |
|
|
|
| 204 |
|
|
#endif /* CRC_H */
|
| 205 |
|
|
|
| 206 |
|
|
|
| 207 |
|
|
|