Subversion Repositories HomeAutomation

Rev

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