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 ISR_SHARED \
21  void __attribute__((signal)) // This function type has to be used for all the interrupt handlers that have to be changed at runtime
22 
23 #define INLINE \
24  static inline __attribute__((always_inline))
25 
26 #ifndef STRINGIFY
27 #define STRINGIFY(x) #x
28 #endif
29 
30 #define ARRAY_COUNT(x) \
31  (sizeof(x) / sizeof(x[0]))
32 
33 #define NIBBLE_TO_HEXCHAR(x) ( (x) < 0x0A ? (x) + '0' : (x) + 'A' - 0x0A )
34 #define HEXCHAR_TO_NIBBLE(x) ( (x) < 'A' ? (x) - '0' : (x) - 'A' + 0x0A )
35 #define VALID_HEXCHAR(x) ( ( (x) >= '0' && (x) <= '9' ) || ( (x) >= 'A' && (x) <= 'F' ) )
36 #define MIN(x,y) ( (x) < (y) ? (x) : (y) )
37 #define MAX(x,y) ( (x) > (y) ? (x) : (y) )
38 #define SYSTICK_DIFF(since) ((uint16_t) (SystemGetSysTick() - since))
39 #define SYSTICK_DIFF_100MS(since) (SYSTICK_DIFF(since) / 100)
40 
41 #define BITS_PER_BYTE 8
42 #define ARCH_BIG_ENDIAN
43 
44 uint16_t BufferToHexString(char *HexOut, uint16_t MaxChars, const void *Buffer, uint16_t ByteCount);
45 uint16_t HexStringToBuffer(void *Buffer, uint16_t MaxBytes, const char *HexIn);
46 
47 INLINE uint8_t BitReverseByte(uint8_t Byte) {
48  extern const uint8_t PROGMEM BitReverseByteTable[];
49 
50  return pgm_read_byte(&BitReverseByteTable[Byte]);
51 }
52 
53 INLINE uint8_t OddParityBit(uint8_t Byte) {
54  extern const uint8_t PROGMEM OddParityByteTable[];
55 
56  return pgm_read_byte(&OddParityByteTable[Byte]);
57 }
58 
59 INLINE uint8_t StringLength(const char *Str, uint8_t MaxLen) {
60  uint8_t StrLen = 0;
61 
62  while (MaxLen > 0) {
63  if (*Str++ == '\0')
64  break;
65 
66  MaxLen--;
67  StrLen++;
68  }
69 
70  return StrLen;
71 }
72 
73 #endif /* COMMON_H_ */