src.dualinventive.com/dinet/sec-multi-proxy/libdi/include/di/can/bitops.h

94 lines
2.8 KiB
C

/**
* @ingroup can
* @defgroup can_bitops Bit operations and constants
* @brief Bit operations and constants
* @copyright 2016 Dual Inventive Technology Centre B.V.
*
* descr
* @{
*/
#ifndef LIBDI_INCLUDE_DI_CAN_BITOPS_H_
#define LIBDI_INCLUDE_DI_CAN_BITOPS_H_
#include <limits.h>
/**
* CAN ID field offsets
*/
enum di_can_canid_offsets {
DI_CAN_CANID_OFFSET_MSGTYPE = 27,
DI_CAN_CANID_OFFSET_DATATYPE = 16,
DI_CAN_CANID_OFFSET_TRANSFERTYPE = 14,
DI_CAN_CANID_OFFSET_FRAME = 5,
DI_CAN_CANID_OFFSET_TRANSACTION = 1
};
/** Set canid msgtype */
#define DI_CAN_SET_MSGTYPE(canid, msgtype) \
canid = ((canid) & (uint32_t)(~0x18000000)) | \
(uint32_t)(((msgtype) & 0x3) << DI_CAN_CANID_OFFSET_MSGTYPE)
/** Set canid datatype */
#define DI_CAN_SET_DATATYPE(canid, datatype) \
canid = ((canid) & (uint32_t)(~0x07ff0000)) | \
(uint32_t)(((datatype) & 0x7ff) << DI_CAN_CANID_OFFSET_DATATYPE)
/** Set canid transfertype */
#define DI_CAN_SET_TRANSFERTYPE(canid, transfertype) \
canid = ((canid) & (uint32_t)(~0x0000c000)) | \
(uint32_t)(((transfertype) & 0x3) << DI_CAN_CANID_OFFSET_TRANSFERTYPE)
/** Set canid frame index */
#define DI_CAN_SET_FRAME(canid, frame) \
canid = ((canid) & (uint32_t)(~0x00003fe0)) | \
(uint32_t)(((frame) & 0x1ff) << DI_CAN_CANID_OFFSET_FRAME)
/** Set canid next frame index */
#define DI_CAN_SET_FRAME_NEXT(canid) \
DI_CAN_SET_FRAME(canid, DI_CAN_GET_FRAME(canid) + 1)
/** Set canid transaction id */
#define DI_CAN_SET_TRANSACTION(canid, transaction) \
canid = ((canid) & (uint32_t)(~0x0000001e)) | \
(uint32_t)((transaction & 0xf) << DI_CAN_CANID_OFFSET_TRANSACTION)
/** Set canid last frame marker */
#define DI_CAN_SET_LAST_FRAME(canid, last) \
canid = ((canid) & (uint32_t)(~0x00000001)) | (uint32_t)(last & 0x1)
/** Extract message type from canid */
#define DI_CAN_GET_MSGTYPE(canid) \
(enum di_can_msgtype)(((canid) >> DI_CAN_CANID_OFFSET_MSGTYPE) & 0x3)
/** Extract data type id from canid */
#define DI_CAN_GET_DATATYPE(canid) \
(((canid) >> DI_CAN_CANID_OFFSET_DATATYPE) & 0x7ff)
/** Extract transfer type id from canid */
#define DI_CAN_GET_TRANSFERTYPE(canid) \
(enum di_can_transfertype)(((canid) >> DI_CAN_CANID_OFFSET_TRANSFERTYPE) & 0x3)
/** Extract frame index from canid */
#define DI_CAN_GET_FRAME(canid) \
(((canid) >> DI_CAN_CANID_OFFSET_FRAME) & 0x1ff)
/** Extract transaction id from canid */
#define DI_CAN_GET_TRANSACTION(canid) \
(((canid) >> DI_CAN_CANID_OFFSET_TRANSACTION) & 0xf)
/** Extract last frame flag from canid */
#define DI_CAN_GET_LAST_FRAME(canid) \
((canid) & 0x1)
/** Calculate the amount of frames */
#define DI_CAN_GET_FRAME_COUNT(bytes) \
((bytes) >> 3)
/** Calculate the amount of remaining bytes */
#define DI_CAN_GET_REMAINING(bytes) \
((bytes) & (DI_CAN_FRAME_SIZE_MASK))
/** @} */
#endif /* LIBDI_INCLUDE_DI_CAN_BITOPS_H_ */