Chameleon-Mini
Common.h
1 /*
2  * Common.h
3  *
4  * Created on: 20.03.2013
5  * Author: skuser
6  */
7 
8 #ifndef COMMON_H_
9 #define COMMON_H_
10 
11 #include <stdio.h>
12 #include <stdbool.h>
13 #include <util/parity.h>
14 #include <util/delay.h>
15 #include <avr/pgmspace.h>
16 #include <avr/io.h>
17 
18 #define ODD_PARITY(Value) OddParityBit(Value)//(parity_even_bit(Value) ? 0 : 1)
19 
20 #define INLINE \
21  static inline __attribute__((always_inline))
22 
23 #define ARRAY_COUNT(x) \
24  (sizeof(x) / sizeof(x[0]))
25 
26 #define NIBBLE_TO_HEXCHAR(x) ( (x) < 0x0A ? (x) + '0' : (x) + 'A' - 0x0A )
27 #define HEXCHAR_TO_NIBBLE(x) ( (x) < 'A' ? (x) - '0' : (x) - 'A' + 0x0A )
28 #define VALID_HEXCHAR(x) ( ( (x) >= '0' && (x) <= '9' ) || ( (x) >= 'A' && (x) <= 'F' ) )
29 #define MIN(x,y) ( (x) < (y) ? (x) : (y) )
30 #define MAX(x,y) ( (x) > (y) ? (x) : (y) )
31 #define SYSTICK_DIFF(since) ((uint16_t) (SystemGetSysTick() - since))
32 #define SYSTICK_DIFF_100MS(since) (SYSTICK_DIFF(since) / 100)
33 
34 #define BITS_PER_BYTE 8
35 
36 uint16_t BufferToHexString(char* HexOut, uint16_t MaxChars, const void* Buffer, uint16_t ByteCount);
37 uint16_t HexStringToBuffer(void* Buffer, uint16_t MaxBytes, const char* HexIn);
38 
39 INLINE uint8_t BitReverseByte(uint8_t Byte)
40 {
41  extern const uint8_t PROGMEM BitReverseByteTable[];
42 
43  return pgm_read_byte(&BitReverseByteTable[Byte]);
44 }
45 
46 INLINE uint8_t OddParityBit(uint8_t Byte)
47 {
48  extern const uint8_t PROGMEM OddParityByteTable[];
49 
50  return pgm_read_byte(&OddParityByteTable[Byte]);
51 }
52 
53 INLINE uint8_t StringLength(const char* Str, uint8_t MaxLen)
54 {
55  uint8_t StrLen = 0;
56 
57  while(MaxLen > 0) {
58  if (*Str++ == '\0')
59  break;
60 
61  MaxLen--;
62  StrLen++;
63  }
64 
65  return StrLen;
66 }
67 
68 #endif /* COMMON_H_ */