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