Subversion Repositories HomeAutomation

Rev

Rev 59 | Blame | Last modification | View Log | SVN | RSS feed

  1. #ifndef CAN_H_
  2. #define CAN_H_
  3.  
  4. /*-----------------------------------------------------------------------------
  5.  * Includes
  6.  *---------------------------------------------------------------------------*/
  7. #include <inttypes.h>
  8. #include <can_cfg.h>
  9.  
  10.  
  11. /*-----------------------------------------------------------------------------
  12.  * Type Definitions
  13.  *---------------------------------------------------------------------------*/
  14. /**
  15.  * CAN Bitrate type.
  16.  */
  17. typedef enum {
  18.     CAN_BITRATE_125K = 0,
  19.     CAN_BITRATE_250K = 1,
  20.     CAN_BITRATE_500K = 2,
  21.     CAN_BITRATE_1M = 3
  22. } CanBitrate_t;
  23.  
  24. /**
  25.  * CAN Return Type. Most CAN functions return one of these.
  26.  */
  27. typedef enum {
  28.     CAN_OK = 0,
  29.     CAN_FAILINIT = 1,
  30.     CAN_FAILTX = 2,
  31.     CAN_MSGAVAIL = 3,
  32.     CAN_NOMSG = 4,
  33.     CAN_CTRLERROR = 5,
  34.     CAN_FAIL = 0xFF
  35. } CanReturn_t;
  36.  
  37. /**
  38.  * CAN Message Type. Stores a complete CAN frame.
  39.  */
  40. typedef struct {
  41.     /*
  42.      * ID section. Contains either 29bit ID or 11bit ID (depending on ExtFlag).
  43.      */
  44.     uint32_t Id;
  45.    
  46.     /*
  47.      * DLC section. Contains the data length expressed in number of bytes. Range [0,8].
  48.      */
  49.     uint8_t DataLength;
  50.    
  51.     /**
  52.      * Extended frame flag. Set to 1 if the ID is 29bit, or 0 if it is 11bit.
  53.      */
  54.     uint8_t ExtendedFlag;
  55.    
  56.     /**
  57.      * Remote frame flag. Set to 1 if the frame is a remote frame. 0 otherwise.
  58.      */
  59.     uint8_t RemoteFlag;
  60.    
  61.     /*
  62.      * DATA section. Contains the 8 data bytes.
  63.      */
  64.     union {
  65.         /* read as 8bit unsigned data */
  66.         uint8_t bytes[8];
  67.        
  68.         /* read as 16bit unsigned data */
  69.         uint16_t words[4];
  70.        
  71.         /* read as 32bit unsigned data */
  72.         uint32_t dwords[2];
  73.     } Data;
  74. } CanMessage_t;
  75.  
  76.  
  77. /*-----------------------------------------------------------------------------
  78.  * Public Function Prototypes
  79.  *---------------------------------------------------------------------------*/
  80.  
  81. CanReturn_t CanInit(CanBitrate_t bitrate);
  82. void can_initMessageStruct(CanMessage_t* msg);
  83. CanReturn_t CanSend(CanMessage_t* msg);
  84. CanReturn_t CanReceiveAvailable(void);
  85. CanReturn_t CanReceive(CanMessage_t *msg);
  86. CanReturn_t CanCheckError(void);
  87.  
  88. #if (CANDEBUG)
  89. void can_debug1(void);
  90. void can_dumpMessage(CanMessage *msg);
  91. uint8_t can_testTransmit(const uint8_t ext, const uint8_t data2);
  92. void can_dumpStatus(void);
  93. #endif
  94.  
  95. #endif
  96.