Subversion Repositories HomeAutomation

Rev

Rev 127 | 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.  * Defines
  13.  *---------------------------------------------------------------------------*/
  14.  
  15. /*
  16.  * The supported CAN controller devices are listed below.
  17.  */
  18. #define CAN_CONTROLLER_NULL     0       /* Virtual null-device */
  19. #define CAN_CONTROLLER_MCP2515  1       /* Microchip MCP2515 via SPI */
  20.  
  21.  
  22. /*-----------------------------------------------------------------------------
  23.  * Type Definitions
  24.  *---------------------------------------------------------------------------*/
  25.  
  26. /**
  27.  * CAN Bitrate Type. The supported bitrates.
  28.  */
  29. typedef enum {
  30.     CAN_BITRATE_125K = 0,           /* Max cable length: 500m */
  31.     CAN_BITRATE_250K = 1,           /* Max cable length: 250m */
  32.     CAN_BITRATE_500K = 2,           /* Max cable length: 100m */
  33.     CAN_BITRATE_1M = 3              /* Max cable length: 40m */
  34. } Can_Bitrate_t;
  35.  
  36.  
  37. /**
  38.  * CAN Return Type. Most CAN functions return one of these.
  39.  */
  40. typedef enum {
  41.     CAN_FAIL = -1,                  /* The function failed */
  42.     CAN_OK = 1,                     /* The function succeeded */
  43.    
  44.     CAN_MSG_AVAILABLE = 2,          /* Messsages are available */
  45.     CAN_NO_MSG_AVAILABLE = -2,      /* Messages are NOT available */
  46.    
  47.     //CAN_CTRLERROR = -4
  48. } Can_Return_t;
  49.  
  50.  
  51. /**
  52.  * CAN Message Type. Stores a complete CAN frame.
  53.  */
  54. typedef struct {
  55.     /*
  56.      * ID section. Contains either 29bit ID or 11bit ID (depending on ExtendedFlag).
  57.      */
  58.     uint32_t Id;
  59.    
  60.     /*
  61.      * DLC section. Contains the data length expressed in number of bytes. Range [0,8].
  62.      */
  63.     uint8_t DataLength;
  64.    
  65.     /**
  66.      * Extended frame flag. Set to 1 if the ID is 29bit, or 0 if it is 11bit.
  67.      */
  68.     uint8_t ExtendedFlag;
  69.    
  70.     /**
  71.      * Remote frame flag. Set to 1 if the frame is a remote frame. 0 otherwise.
  72.      */
  73.     uint8_t RemoteFlag;
  74.    
  75.     /*
  76.      * DATA section. Contains the 8 data bytes.
  77.      */
  78.     union {
  79.         /* read as 8bit unsigned data */
  80.         uint8_t bytes[8];
  81.        
  82.         /* read as 16bit unsigned data */
  83.         uint16_t words[4];
  84.        
  85.         /* read as 32bit unsigned data */
  86.         uint32_t dwords[2];
  87.     } Data;
  88. } Can_Message_t;
  89.  
  90.  
  91. /*-----------------------------------------------------------------------------
  92.  * Public Function Prototypes
  93.  *---------------------------------------------------------------------------*/
  94.  
  95. Can_Return_t Can_Init(void);
  96. Can_Return_t Can_Send(Can_Message_t* msg);
  97. void Can_Service(void);
  98. Can_Return_t Can_Receive(Can_Message_t *msg);
  99.  
  100. #endif
  101.