Add macros to handle endianess issues.
[bertos.git] / cpu.h
diff --git a/cpu.h b/cpu.h
index 626bd3e03b7dcf91dabe14d186672ce0b95c8694..09240a0a68a7faeffe3bcd9f518847efa3fe2bee 100755 (executable)
--- a/cpu.h
+++ b/cpu.h
@@ -3,7 +3,7 @@
  * <!--
  * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
  * Copyright 2004 Giovanni Bajo
- * All Rights Reserved.
+ * This file is part of DevLib - See devlib/README for information.
  * -->
  *
  * \brief CPU-specific definitions
 
 /*
  * $Log$
+ * Revision 1.4  2004/07/20 16:06:04  bernie
+ * Add macros to handle endianess issues.
+ *
+ * Revision 1.3  2004/07/18 21:49:51  bernie
+ * Fixes for GCC 3.5.
+ *
+ * Revision 1.2  2004/06/03 11:27:09  bernie
+ * Add dual-license information.
+ *
  * Revision 1.1  2004/05/23 17:48:35  bernie
  * Add top-level files.
  *
 //! Initialization value for registers in stack frame
 #define CPU_REG_INIT_VALUE(reg)     0
 
+// Macros for determining CPU endianness
+#define CPU_BIG_ENDIAN    0x1234
+#define CPU_LITTLE_ENDIAN 0x3412
+
+
 #if defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)  /* 80C196 */
 
        #define DISABLE_INTS            disable_interrupt()
@@ -40,6 +54,7 @@
        #define CPU_REGS_CNT            16
        #define CPU_STACK_GROWS_UPWARD  0
        #define CPU_SP_ON_EMPTY_SLOT    0
+       #define CPU_BYTE_ORDER          CPU_LITTLE_ENDIAN
 
 #elif defined(__i386__) || defined(_MSC_VER) /* x86 */
 
@@ -54,6 +69,7 @@
        #define CPU_REGS_CNT            7
        #define CPU_STACK_GROWS_UPWARD  0
        #define CPU_SP_ON_EMPTY_SLOT    0
+       #define CPU_BYTE_ORDER          CPU_LITTLE_ENDIAN
 
 #elif defined(__m56800E__) || defined(__m56800__) /* DSP56K */
 
@@ -74,6 +90,7 @@
        #define CPU_SAVED_REGS_CNT      28
        #define CPU_STACK_GROWS_UPWARD  1
        #define CPU_SP_ON_EMPTY_SLOT    0
+       #define CPU_BYTE_ORDER          CPU_BIG_ENDIAN
 
        #undef CPU_REG_INIT_VALUE
        INLINE uint16_t CPU_REG_INIT_VALUE(int reg)
 
 #elif defined (__AVR__)
 
-       #define NOP                     asm volatile ("nop")
-       #define DISABLE_INTS            cli()
-       #define ENABLE_INTS             sei()
+       #define NOP                     asm volatile ("nop" ::)
+       #define DISABLE_INTS            asm volatile ("cli" ::)
+       #define ENABLE_INTS             asm volatile ("sei" ::)
        #define SCHEDULER_IDLE          /* nothing */
 
        #define DISABLE_IRQSAVE(x) \
        #define CPU_SAVED_REGS_CNT      18
        #define CPU_STACK_GROWS_UPWARD  0
        #define CPU_SP_ON_EMPTY_SLOT    1
-
+       #define CPU_BYTE_ORDER          CPU_LITTLE_ENDIAN
 #else
        #error Unknown CPU
 #endif
                CPU_PUSH_WORD((sp), (func))
 #endif
 
+
+INLINE uint16_t htobe16(uint16_t n);
+INLINE uint16_t htobe16(uint16_t n)
+{
+       if (CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN)
+               n = n << 8 | n >> 8;
+
+       return n;
+}
+
+INLINE uint16_t htole16(uint16_t n);
+INLINE uint16_t htole16(uint16_t n)
+{
+       if (CPU_BYTE_ORDER == CPU_BIG_ENDIAN)
+               n = n << 8 | n >> 8;
+
+       return n;
+}
+
 #endif /* CPU_H */