当前位置:网站首页>Soem EtherCAT source code analysis I (data type definition)

Soem EtherCAT source code analysis I (data type definition)

2022-07-05 08:04:00 Changjiang houlang blog

Friends , Today, let's look at Ethercat Master site source code , Enter the world of source code .

/**
  * Simple Open EtherCAT Master Library
  *  Common types and EtherCAT Definition 
  */

#ifndef _EC_TYPE_H
#define _EC_TYPE_H

#include <stdint.h>

#ifdef __cplusplus
extern "C"
{
#endif

/** Define Little or Big endian target */
/* Define communication as small end mode */
#define EC_LITTLE_ENDIAN

/** define EC_VER1 if version 1 default context and functions are needed
 * comment if application uses only ecx_ functions and own context */
/* Use V1 Version context function */
#define EC_VER1

/** return value general error */
/* General error return value */
#define EC_ERROR           -3

/** return value no frame returned */
/* No data frame returned */
#define EC_NOFRAME         -1

/** return value unknown frame received */
/* Unrecognized packet reception */
#define EC_OTHERFRAME      -2

/** maximum EtherCAT frame length in bytes */
/* Maximum ethercat Packet length */
#define EC_MAXECATFRAME    1518

/** maximum EtherCAT LRW frame length in bytes */
/* Maximum EtherCAT LRW Packet length */
/* MTU - Ethernet header - length - datagram header - WCK - FCS */
#define EC_MAXLRWDATA      (EC_MAXECATFRAME - 14 - 2 - 10 - 2 - 4)

/** size of DC datagram used in first LRW frame */
/* At the first frame LRW In the packet DC Synchronization data frame size */
#define EC_FIRSTDCDATAGRAM 20

/** standard frame buffer size in bytes */
/* Standard data frame buffer size */
#define EC_BUFSIZE         EC_MAXECATFRAME

/** datagram type EtherCAT */
/*EtherCAT  Packet type */
#define EC_ECATTYPE        0x1000

/** number of frame buffers per channel (tx, rx1 rx2) */
/* Per channel (tx, rx1 rx2) Number of packet caches */
#define EC_MAXBUF          16

/** timeout value in us for tx frame to return to rx */
/* Timeout from sending packet to receiving packet */
#define EC_TIMEOUTRET      2000

/** timeout value in us for safe data transfer, max. triple retry */
/* Security data sending timeout , The maximum number of attempts to send is three */
#define EC_TIMEOUTRET3     (EC_TIMEOUTRET * 3)

/** timeout value in us for return "safe" variant (f.e. wireless) */
/*safe  Variant return timeout (us)*/
#define EC_TIMEOUTSAFE     20000

/** timeout value in us for EEPROM access */
/*EEPROM  operation - Timeout time (us)*/
#define EC_TIMEOUTEEP      20000

/** timeout value in us for tx mailbox cycle */
/*tx Mailbox cycle timeout (us)*/
#define EC_TIMEOUTTXM      20000

/** timeout value in us for rx mailbox cycle */
/*rx Mailbox cycle timeout (us)*/
#define EC_TIMEOUTRXM      700000

/** timeout value in us for check statechange */
/* State switch detection timeout (us)*/
#define EC_TIMEOUTSTATE    2000000

/** size of EEPROM bitmap cache */
/*EEPROM  Bit buffer size */
#define EC_MAXEEPBITMAP    128

/** size of EEPROM cache buffer */
/*EEPROM  Buffer size -4096*/
#define EC_MAXEEPBUF       EC_MAXEEPBITMAP << 5

/** default number of retries if wkc <= 0 */
/* Default number of attempts -3 Time */
#define EC_DEFAULTRETRIES  3

/* Data macro definition */
/* General types */
typedef uint8_t             boolean;    // Unsigned 8 as bool type 
#define TRUE                1
#define FALSE               0
typedef int8_t              int8;       // A signed 8 position 
typedef int16_t             int16;      // A signed 16 position 
typedef int32_t             int32;      // A signed 32 position 
typedef uint8_t             uint8;      // Unsigned 8 position 
typedef uint16_t            uint16;     // Unsigned 16 position 
typedef uint32_t            uint32;     // Unsigned 32 position 
typedef int64_t             int64;      // A signed 64 position 
typedef uint64_t            uint64;     // Unsigned 64 position 
typedef float               float32;    // Single floating point 
typedef double              float64;    // Double floating point 

typedef struct
{
    uint32 sec;     /*< Seconds elapsed since the Epoch (Jan 1, 1970) */
    int32 usec;      /*< Microseconds elapsed since last second boundary */
} ec_timet;         /* Time unit structure */

typedef struct osal_timer
{
    ec_timet stop_time;
} osal_timert;      /* Timer records time structure */


/** definition for frame buffers */
/* Define packet cache size */
typedef uint8 ec_bufT[EC_BUFSIZE];

/* Structure prefix header */
#if defined(WIN32)
    // Structure prefix 
    #ifndef PACKED
        #define PACKED_BEGIN __pragma(pack(push, 1))
        #define PACKED
        #define PACKED_END __pragma(pack(pop))
    #endif

    // Function prefixes 
    #define OSAL_THREAD_HANDLE HANDLE
    #define OSAL_THREAD_FUNC void
    #define OSAL_THREAD_FUNC_RT void
#elif defined(__linux__)
    // Structure prefix 
    #ifndef PACKED
        #define PACKED_BEGIN
        #define PACKED  __attribute__((__packed__))
        #define PACKED_END
    #endif

    // Function prefixes 
    #define OSAL_THREAD_HANDLE task_t *
    #define OSAL_THREAD_FUNC void
    #define OSAL_THREAD_FUNC_RT void
#else
    // Structure prefix 
    #ifndef PACKED
        #define PACKED_BEGIN
        #define PACKED  __attribute__((__packed__))
        #define PACKED_END
    #endif

    // Function prefixes 
    #define OSAL_THREAD_HANDLE task_t *
    #define OSAL_THREAD_FUNC void
    #define OSAL_THREAD_FUNC_RT void
#endif


/** ethernet header definition */
/* Ethernet header packets */
PACKED_BEGIN
typedef struct PACKED
{
   /** destination MAC  Destination address */
   uint16  da0,da1,da2;
   /** source MAC   source address */
   uint16  sa0,sa1,sa2;
   /** ethernet type  The frame type 0x88A4*/
   uint16  etype;
} ec_etherheadert;
PACKED_END

/** ethernet header size */
/* Ethernet header packet size */
#define ETH_HEADERSIZE      sizeof(ec_etherheadert)

/** EtherCAT datagram header definition */
/*EtherCAT Packet header definition */
PACKED_BEGIN
typedef struct PACKED
{
   /** length of EtherCAT datagram  Packet length */
   uint16  elength;
   /** EtherCAT command, see ec_cmdtype  command  */
   uint8   command;
   /** index, used in SOEM for Tx to Rx recombination  Indexes */
   uint8   index;
   /** ADP  Address area */
   uint16  ADP;
   /** ADO  Address area */
   uint16  ADO;
   /** length of data portion in datagram  Packet length */
   uint16  dlength;
   /** interrupt, currently unused  interrupt ( not used )*/
   uint16  irpt;
} ec_comt;
PACKED_END

/** EtherCAT header size */
/*EtherCAT Packet header size */
#define EC_HEADERSIZE       sizeof(ec_comt)

/** size of ec_comt.elength item in EtherCAT header */
/*EtherCAT Data packet head   length   size -2*/
#define EC_ELENGTHSIZE      sizeof(uint16)

/** offset position of command in EtherCAT header */
/*EtherCAT Data packet head   Starting position of sub message */
#define EC_CMDOFFSET        EC_ELENGTHSIZE

/** size of workcounter item in EtherCAT datagram */
/*EtherCAT Data packet head  WKC Count size  -2*/
#define EC_WKCSIZE          sizeof(uint16)

/** definition of datagram follows bit in ec_comt.dlength */
/* Flag bit of subsequent message in the sub message */
#define EC_DATAGRAMFOLLOWS  (1 << 15)

/** Possible error codes returned. */
/* Possible returned errors */
typedef enum
{
   /** No error OK state */
   EC_ERR_OK         = 0,
   /** Library already initialized.  The library has been initialized */
   EC_ERR_ALREADY_INITIALIZED   =1,
   /** Library not initialized.      Library not initialized */
   EC_ERR_NOT_INITIALIZED   =2,
   /** Timeout occured during execution of the function.  The execution of the function timed out */
   EC_ERR_TIMEOUT   =3,
   /** No slaves were found.  No slave node found */
   EC_ERR_NO_SLAVES =4,
   /** Function failed.  Function execution failed */
   EC_ERR_NOK =5
} ec_err;

/** Possible EtherCAT slave states */
/*EtherCAT  Status of slave station operation */
typedef enum
{
   /** Init state  initialization */
   EC_STATE_INIT           = 0x01,
   /** Pre-operational.  Pre operation */
   EC_STATE_PRE_OP         = 0x02,
   /** Boot state  Upgrade mode */
   EC_STATE_BOOT            = 0x03,
   /** Safe-operational.  safe mode */
   EC_STATE_SAFE_OP        = 0x04,
   /** Operational  Operation mode */
   EC_STATE_OPERATIONAL    = 0x08,
   /** Error or ACK error  error   perhaps  ACK error */
   EC_STATE_ACK            = 0x10,
   EC_STATE_ERROR          = 0x10
} ec_state;

/** Possible buffer states */
/*ECT Buffer state */
typedef enum
{
   /** Empty  empty */
   EC_BUF_EMPTY        = 0x00,
   /** Allocated, but not filled  Memory allocated but not filled*/
   EC_BUF_ALLOC        = 0x01,
   /** Transmitted  send out */
   EC_BUF_TX           = 0x02,
   /** Received, but not consumed  receive   But not consumed*/
   EC_BUF_RCVD         = 0x03,
   /** Cycle completed  Cycle completion */
   EC_BUF_COMPLETE     = 0x04
} ec_bufstate;

/** Ethercat data types */
/*Ethercat  data type */
typedef enum
{
   ECT_BOOLEAN         = 0x0001,
   ECT_INTEGER8        = 0x0002,
   ECT_INTEGER16       = 0x0003,
   ECT_INTEGER32       = 0x0004,
   ECT_UNSIGNED8       = 0x0005,
   ECT_UNSIGNED16      = 0x0006,
   ECT_UNSIGNED32      = 0x0007,
   ECT_REAL32          = 0x0008,
   ECT_VISIBLE_STRING  = 0x0009,
   ECT_OCTET_STRING    = 0x000A,
   ECT_UNICODE_STRING  = 0x000B,
   ECT_TIME_OF_DAY     = 0x000C,
   ECT_TIME_DIFFERENCE = 0x000D,
   ECT_DOMAIN          = 0x000F,
   ECT_INTEGER24       = 0x0010,
   ECT_REAL64          = 0x0011,
   ECT_INTEGER64       = 0x0015,
   ECT_UNSIGNED24      = 0x0016,
   ECT_UNSIGNED64      = 0x001B,
   ECT_BIT1            = 0x0030,
   ECT_BIT2            = 0x0031,
   ECT_BIT3            = 0x0032,
   ECT_BIT4            = 0x0033,
   ECT_BIT5            = 0x0034,
   ECT_BIT6            = 0x0035,
   ECT_BIT7            = 0x0036,
   ECT_BIT8            = 0x0037
} ec_datatype;

/** Ethercat command types */
/*Ethercat  Command type */
typedef enum 
{
   /** No operation  An empty command */
   EC_CMD_NOP          = 0x00,
   /** Auto Increment Read  Auto increment read */
   EC_CMD_APRD,
   /** Auto Increment Write  Auto increment write */
   EC_CMD_APWR,
   /** Auto Increment Read Write  Self increasing reading and writing */
   EC_CMD_APRW,
   /** Configured Address Read  To configure Alisa Read */
   EC_CMD_FPRD,
   /** Configured Address Write  To configure Alisa write in */
   EC_CMD_FPWR,
   /** Configured Address Read Write  To configure Alisa Reading and writing */
   EC_CMD_FPRW,
   /** Broadcast Read  Broadcast read */
   EC_CMD_BRD,
   /** Broaddcast Write  Broadcast write */
   EC_CMD_BWR,
   /** Broadcast Read Write  Radio reading and writing */
   EC_CMD_BRW,
   /** Logical Memory Read  Logical memory read */
   EC_CMD_LRD,
   /** Logical Memory Write  Logical memory write */
   EC_CMD_LWR,
   /** Logical Memory Read Write  Logical memory read / write */
   EC_CMD_LRW,
   /** Auto Increment Read Mulitple Write  Write more about self increase */
   EC_CMD_ARMW,
   /** Configured Read Mulitple Write  Write more configurations */
   EC_CMD_FRMW
   /** Reserved */
} ec_cmdtype;

/** Ethercat EEprom command types */
/*Ethercat EEprom Command type */
typedef enum 
{
   /** No operation  No operation */
   EC_ECMD_NOP         = 0x0000,
   /** Read  read */
   EC_ECMD_READ        = 0x0100,
   /** Write  Write */
   EC_ECMD_WRITE       = 0x0201,
   /** Reload  load */
   EC_ECMD_RELOAD      = 0x0300
} ec_ecmdtype;

/*EEprom state */
/** EEprom state machine read size EEprom State device read size */
#define EC_ESTAT_R64    0x0040
/** EEprom state machine busy flag EEprom Status device busy flag */
#define EC_ESTAT_BUSY   0x8000
/** EEprom state machine error flag mask EEprom Status device error flag mask */
#define EC_ESTAT_EMASK  0x7800
/** EEprom state machine error acknowledge EEprom Status device error response */
#define EC_ESTAT_NACK   0x2000

/* Ethercat SSI (Slave Information Interface) * Slave information interface /

/** Start address SII sections in Eeprom  stay Eeprom Start in SSI Address segment */
#define ECT_SII_START   0x0040

enum
{
   /** SII category strings SSI Directory string */
   ECT_SII_STRING      = 10,
   /** SII category general SSI The directory is general */
   ECT_SII_GENERAL     = 30,
   /** SII category FMMU SSI Catalog FMMU*/
   ECT_SII_FMMU        = 40,
   /** SII category SM  SSI Catalog SM*/
   ECT_SII_SM          = 41,
   /** SII category PDO SSI Catalog PDO*/
   ECT_SII_PDO         = 50
};

/** Item offsets in SII general section  stay SSI Configuration of items in the general segment  */
enum
{
   ECT_SII_MANUF       = 0x0008,
   ECT_SII_ID          = 0x000a,
   ECT_SII_REV         = 0x000c,
   ECT_SII_BOOTRXMBX   = 0x0014,
   ECT_SII_BOOTTXMBX   = 0x0016,
   ECT_SII_MBXSIZE     = 0x0019,
   ECT_SII_TXMBXADR    = 0x001a,
   ECT_SII_RXMBXADR    = 0x0018,
   ECT_SII_MBXPROTO    = 0x001c
};

/** Mailbox types definitions */
/* Mailbox type definition */
enum
{
   /** Error mailbox type  Wrong mailbox type */
   ECT_MBXT_ERR        = 0x00,
   /** ADS over EtherCAT mailbox type AOE agreement */
   ECT_MBXT_AOE,
   /** Ethernet over EtherCAT mailbox type EOE agreement */
   ECT_MBXT_EOE,
   /** CANopen over EtherCAT mailbox type COE agreement */
   ECT_MBXT_COE,
   /** File over EtherCAT mailbox type FOE agreement */
   ECT_MBXT_FOE,
   /** Servo over EtherCAT mailbox type SOE agreement */
   ECT_MBXT_SOE,
   /** Vendor over EtherCAT mailbox type VOE agreement */
   ECT_MBXT_VOE        = 0x0f
};

/** CoE mailbox types */
/*COE  Mailbox type */
enum
{
   ECT_COES_EMERGENCY  = 0x01,
   ECT_COES_SDOREQ,
   ECT_COES_SDORES,
   ECT_COES_TXPDO,
   ECT_COES_RXPDO,
   ECT_COES_TXPDO_RR,
   ECT_COES_RXPDO_RR,
   ECT_COES_SDOINFO
};

/** CoE SDO commands */
/*COE SDO command */
enum
{
   ECT_SDO_DOWN_INIT    = 0x21,
   ECT_SDO_DOWN_EXP     = 0x23,
   ECT_SDO_DOWN_INIT_CA = 0x31,
   ECT_SDO_UP_REQ       = 0x40,
   ECT_SDO_UP_REQ_CA    = 0x50,
   ECT_SDO_SEG_UP_REQ   = 0x60,
   ECT_SDO_ABORT        = 0x80
};

/** CoE Object Description commands */
/*COE Object dictionary describes commands */
enum
{
   ECT_GET_ODLIST_REQ  = 0x01,
   ECT_GET_ODLIST_RES  = 0x02,
   ECT_GET_OD_REQ      = 0x03,
   ECT_GET_OD_RES      = 0x04,
   ECT_GET_OE_REQ      = 0x05,
   ECT_GET_OE_RES      = 0x06,
   ECT_SDOINFO_ERROR   = 0x07
};

/** FoE opcodes */
/*FOE Opcode commands */
enum
{
   ECT_FOE_READ        = 0x01,
   ECT_FOE_WRITE,
   ECT_FOE_DATA,
   ECT_FOE_ACK,
   ECT_FOE_ERROR,
   ECT_FOE_BUSY
};

/** SoE opcodes */
/*SOE Opcode commands */
enum
{
   ECT_SOE_READREQ     = 0x01,
   ECT_SOE_READRES,
   ECT_SOE_WRITEREQ,
   ECT_SOE_WRITERES,
   ECT_SOE_NOTIFICATION,
   ECT_SOE_EMERGENCY
};

/** Ethercat registers */
/*Ethercat register */
enum 
{
   ECT_REG_TYPE        = 0x0000,
   ECT_REG_PORTDES     = 0x0007,
   ECT_REG_ESCSUP      = 0x0008,
   ECT_REG_STADR       = 0x0010,
   ECT_REG_ALIAS       = 0x0012,
   ECT_REG_DLCTL       = 0x0100,
   ECT_REG_DLPORT      = 0x0101,
   ECT_REG_DLALIAS     = 0x0103,
   ECT_REG_DLSTAT      = 0x0110,
   ECT_REG_ALCTL       = 0x0120,
   ECT_REG_ALSTAT      = 0x0130,
   ECT_REG_ALSTATCODE  = 0x0134,
   ECT_REG_PDICTL      = 0x0140,
   ECT_REG_IRQMASK     = 0x0200,
   ECT_REG_RXERR       = 0x0300,
   ECT_REG_FRXERR      = 0x0308,
   ECT_REG_EPUECNT     = 0x030C,
   ECT_REG_PECNT       = 0x030D,
   ECT_REG_PECODE      = 0x030E,
   ECT_REG_LLCNT       = 0x0310,
   ECT_REG_WDCNT       = 0x0442,
   ECT_REG_EEPCFG      = 0x0500,
   ECT_REG_EEPCTL      = 0x0502,
   ECT_REG_EEPSTAT     = 0x0502,
   ECT_REG_EEPADR      = 0x0504,
   ECT_REG_EEPDAT      = 0x0508,
   ECT_REG_FMMU0       = 0x0600,
   ECT_REG_FMMU1       = ECT_REG_FMMU0 + 0x10,
   ECT_REG_FMMU2       = ECT_REG_FMMU1 + 0x10,
   ECT_REG_FMMU3       = ECT_REG_FMMU2 + 0x10,
   ECT_REG_SM0         = 0x0800,
   ECT_REG_SM1         = ECT_REG_SM0 + 0x08,
   ECT_REG_SM2         = ECT_REG_SM1 + 0x08,
   ECT_REG_SM3         = ECT_REG_SM2 + 0x08,
   ECT_REG_SM0STAT     = ECT_REG_SM0 + 0x05,
   ECT_REG_SM1STAT     = ECT_REG_SM1 + 0x05,
   ECT_REG_SM1ACT      = ECT_REG_SM1 + 0x06,
   ECT_REG_SM1CONTR    = ECT_REG_SM1 + 0x07,
   ECT_REG_DCTIME0     = 0x0900,
   ECT_REG_DCTIME1     = 0x0904,
   ECT_REG_DCTIME2     = 0x0908,
   ECT_REG_DCTIME3     = 0x090C,
   ECT_REG_DCSYSTIME   = 0x0910,
   ECT_REG_DCSOF       = 0x0918,
   ECT_REG_DCSYSOFFSET = 0x0920,
   ECT_REG_DCSYSDELAY  = 0x0928,
   ECT_REG_DCSYSDIFF   = 0x092C,
   ECT_REG_DCSPEEDCNT  = 0x0930,
   ECT_REG_DCTIMEFILT  = 0x0934,
   ECT_REG_DCCUC       = 0x0980,
   ECT_REG_DCSYNCACT   = 0x0981,
   ECT_REG_DCSTART0    = 0x0990,
   ECT_REG_DCCYCLE0    = 0x09A0,
   ECT_REG_DCCYCLE1    = 0x09A4
};

/** standard SDO Sync Manager Communication Type  standard SDO Synchronous management communication address 0X1COO*/
#define ECT_SDO_SMCOMMTYPE      0x1c00

/** standard SDO PDO assignment  standard SDO Distribute PDO object */
#define ECT_SDO_PDOASSIGN       0x1c10

/** standard SDO RxPDO assignment  standard SDO To configure RxPDO queue */
#define ECT_SDO_RXPDOASSIGN     0x1c12

/** standard SDO TxPDO assignment  standard SDO To configure TxPDO queue */
#define ECT_SDO_TXPDOASSIGN     0x1c13

/** Ethercat packet type Ethercat Packet type */
#define ETH_P_ECAT              0x88A4

/** Error types  Wrong type */
typedef enum
{
   EC_ERR_TYPE_SDO_ERROR        = 0,
   EC_ERR_TYPE_EMERGENCY        = 1,
   EC_ERR_TYPE_PACKET_ERROR     = 3,
   EC_ERR_TYPE_SDOINFO_ERROR    = 4,
   EC_ERR_TYPE_FOE_ERROR        = 5,
   EC_ERR_TYPE_FOE_BUF2SMALL    = 6,
   EC_ERR_TYPE_FOE_PACKETNUMBER = 7,
   EC_ERR_TYPE_SOE_ERROR        = 8,
   EC_ERR_TYPE_MBX_ERROR        = 9
} ec_err_type;

/** Struct to retrieve errors.  Record the error structure */
typedef struct
{
   /** Time at which the error was generated.  Error occurrence time */
   ec_timet Time;
   /** Signal bit, error set but not read  Alarm occurrence signal */
   boolean     Signal;
   /** Slave number that generated the error  The slave node with error */
   uint16      Slave;
   /** CoE SDO index that generated the error  Something went wrong COE Address */
   uint16      Index;
   /** CoE SDO subindex that generated the error  Something went wrong COE Subaddress */
   uint8       SubIdx;
   /** Type of error  Type of error occurred */
   ec_err_type Etype;
   union
   {
      /** General abortcode  Error termination code */
      int32   AbortCode;
      /** Specific error for Emergency mailbox  Emergency time assignment error code */
      struct
      {
         uint16  ErrorCode;
         uint8   ErrorReg;
         uint8   b1;
         uint16  w1;
         uint16  w2;
      };
   };
} ec_errort;

/** Helper macros  Common meta action macros */
/** Macro to make a word from 2 bytes  Two byte assembly word macro */
#define MK_WORD(msb, lsb)   ((((uint16)(msb))<<8) | (lsb))

/** Macro to get hi byte of a word  Get high byte macro  (((w) >> 8) & 0x00ff ) */
#define HI_BYTE(w)          ((w) >> 8)

/** Macro to get low byte of a word  Take the low byte macro */
#define LO_BYTE(w)          ((w) & 0x00ff)

/** Macro to swap hi and low byte of a word  High and low byte exchange macro */
#define SWAP(w)             ((((w)& 0xff00) >> 8) | (((w) & 0x00ff) << 8))

/** Macro to get hi word of a dword  Take the lower word macro */
#define LO_WORD(l)          ((l) & 0xffff)

/** Macro to get hi word of a dword  Take the high word macro */
#define HI_WORD(l)          ((l) >> 16)

/**  Memory address alignment macro */
#define get_unaligned(ptr) \
  ({ __typeof__(*(ptr)) __tmp; memcpy(&__tmp, (ptr), sizeof(*(ptr))); __tmp; })

#define put_unaligned32(val, ptr)        \
  (memcpy((ptr), &(val), 4))

#define put_unaligned64(val, ptr)        \
  (memcpy((ptr), &(val), 8))

/**  Size conversion macro */
#if !defined(EC_BIG_ENDIAN) && defined(EC_LITTLE_ENDIAN)

  #define htoes(A) (A)
  #define htoel(A) (A)
  #define htoell(A) (A)
  #define etohs(A) (A)
  #define etohl(A) (A)
  #define etohll(A) (A)

#elif !defined(EC_LITTLE_ENDIAN) && defined(EC_BIG_ENDIAN)

  #define htoes(A) ((((uint16)(A) & 0xff00) >> 8) | \
                    (((uint16)(A) & 0x00ff) << 8))
  #define htoel(A) ((((uint32)(A) & 0xff000000) >> 24) | \
                    (((uint32)(A) & 0x00ff0000) >> 8)  | \
                    (((uint32)(A) & 0x0000ff00) << 8)  | \
                    (((uint32)(A) & 0x000000ff) << 24))
  #define htoell(A) ((((uint64)(A) & (uint64)0xff00000000000000ULL) >> 56) | \
                     (((uint64)(A) & (uint64)0x00ff000000000000ULL) >> 40) | \
                     (((uint64)(A) & (uint64)0x0000ff0000000000ULL) >> 24) | \
                     (((uint64)(A) & (uint64)0x000000ff00000000ULL) >> 8)  | \
                     (((uint64)(A) & (uint64)0x00000000ff000000ULL) << 8)  | \
                     (((uint64)(A) & (uint64)0x0000000000ff0000ULL) << 24) | \
                     (((uint64)(A) & (uint64)0x000000000000ff00ULL) << 40) | \
                     (((uint64)(A) & (uint64)0x00000000000000ffULL) << 56))

  #define etohs  htoes
  #define etohl  htoel
  #define etohll htoell

#else

  #error "Must define one of EC_BIG_ENDIAN or EC_LITTLE_ENDIAN"

#endif

#ifdef __cplusplus
}
#endif

#endif /* _EC_TYPE_H */

原网站

版权声明
本文为[Changjiang houlang blog]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140547277551.html