mb_slave.c
changeset 9 d6effe86bc2f
parent 8 f14859c24751
child 11 7c955a1d39e8
equal deleted inserted replaced
8:f14859c24751 9:d6effe86bc2f
   105   static u16 next_id = 0;
   105   static u16 next_id = 0;
   106   return next_id++;
   106   return next_id++;
   107 }
   107 }
   108 
   108 
   109 
   109 
   110 /* Determine endianess of platform... */
   110 /*
   111 
   111  * Functions to convert u16 variables
   112 /* WARNING: The following files are being included:
   112  * between network and host byte order
   113  *              <stdib.h>  -->  <endian.h>  -->  <bits/endian.h>
   113  *
   114  * 
   114  * NOTE: Modbus uses MSByte first, just like
   115  *          endian.h defines the following constants as:
   115  *       tcp/ip, so we could be tempted to use the htons() and
   116  *            #define __LITTLE_ENDIAN  and LITTLE_ENDIAN  as 1234
   116  *       ntohs() functions to guarantee code portability.
   117  *            #define    __BIG_ENDIAN  and    BIG_ENDIAN  as 4321
   117  *
   118  *            #define    __PDP_ENDIAN  and    PDP_ENDIAN  as 3412
   118  *       However, on some embedded systems running Linux
   119  * 
   119  *       these functions only work if the 16 bit words are 
   120  *          bits/endian.h defines the constant BYTE_ORDER as:
   120  *       stored on even addresses. This is not always the 
   121  *              #define __BYTE_ORDER as __LITTLE_ENDIAN
   121  *       case in our code, so we have to define our own
   122  * 
   122  *       conversion functions...
   123  *          endian.h then sets the following constants
   123  */
   124  *          (if __USE_BSD is set, which seems to be true):
   124 
   125  *            # define LITTLE_ENDIAN    __LITTLE_ENDIAN
   125 /* if using gcc, use it to determine byte order... */
   126  *            # define BIG_ENDIAN       __BIG_ENDIAN
       
   127  *            # define PDP_ENDIAN       __PDP_ENDIAN
       
   128  *            # define BYTE_ORDER       __BYTE_ORDER
       
   129  */ 
       
   130 
       
   131 /* If we still don't know byte order, try to get it from <endian.h> */
       
   132 #ifndef __BYTE_ORDER
       
   133 #include <endian.h>
       
   134 #endif
       
   135 
       
   136 
       
   137 /* If we still don't know byte order => if using gcc, use it to determine byte order... */
       
   138 #ifndef __BYTE_ORDER
   126 #ifndef __BYTE_ORDER
   139 #if defined(__GNUC__) 
   127 #if defined(__GNUC__) 
   140   /* We have GCC, which should define __LITTLE_ENDIAN__ */ 
   128   /* We have GCC, which should define __LITTLE_ENDIAN__ */ 
   141 #  if defined(__LITTLE_ENDIAN__)
   129 #  if defined(__LITTLE_ENDIAN__)
   142 #    define __BYTE_ORDER __LITTLE_ENDIAN
   130 #    define __BYTE_ORDER __LITTLE_ENDIAN
   145 #  endif
   133 #  endif
   146 #endif /* __GNUC__ */ 
   134 #endif /* __GNUC__ */ 
   147 #endif /* __BYTE_ORDER */
   135 #endif /* __BYTE_ORDER */
   148 
   136 
   149 
   137 
       
   138 /* If we still don't know byte order, try to get it from <sys/param.h> */
   150 #ifndef __BYTE_ORDER
   139 #ifndef __BYTE_ORDER
   151 #  error   "Unable to determine platform's byte order. Aborting compilation."
   140 #include <sys/param.h>
   152 #elif   __BYTE_ORDER == __BIG_ENDIAN
       
   153 #  warning "Compiling for BIG endian platform."
       
   154 #elif   __BYTE_ORDER == __LITTLE_ENDIAN
       
   155 #  warning "Compiling for LITTLE endian platform."
       
   156 #else
       
   157 #  error   "Aborting compilation due to unsuported byte order (neither BIG not LITTLE endian)."
       
   158 #endif
   141 #endif
   159 
   142 
   160 
   143 
   161 
   144 #ifndef __BYTE_ORDER
   162 /*
   145 #  ifdef BYTE_ORDER
   163  * Functions to convert u16 variables
   146 #   if BYTE_ORDER == LITTLE_ENDIAN
   164  * between network and host byte order
   147 #    define __BYTE_ORDER __LITTLE_ENDIAN
   165  *
   148 #   else
   166  * NOTE: Modbus uses MSByte first, just like
   149 #    if BYTE_ORDER == BIG_ENDIAN
   167  *       tcp/ip, so we could be tempted to use the htons() and
   150 #      define __BYTE_ORDER __BIG_ENDIAN
   168  *       ntohs() functions to guarantee code portability.
   151 #    endif
   169  *
   152 #   endif
   170  *       However, on some embedded systems running Linux
   153 #  endif /* BYTE_ORDER */
   171  *       these functions only work if the 16 bit words are 
   154 #endif /* __BYTE_ORDER */
   172  *       stored on even addresses. This is not always the 
   155 
   173  *       case in our code, so we have to define our own
   156 
   174  *       conversion functions...
   157 
   175  */
       
   176 
   158 
   177 
   159 
   178 #ifdef __BYTE_ORDER
   160 #ifdef __BYTE_ORDER
   179 # if __BYTE_ORDER == __LITTLE_ENDIAN
   161 # if __BYTE_ORDER == __LITTLE_ENDIAN
   180 
   162