Details | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 59 | jimmy | 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 | /*----------------------------------------------------------------------------- |
||
| 142 | jimmy | 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 | /*----------------------------------------------------------------------------- |
||
| 59 | jimmy | 23 | * Type Definitions |
| 24 | *---------------------------------------------------------------------------*/ |
||
| 142 | jimmy | 25 | |
| 59 | jimmy | 26 | /** |
| 142 | jimmy | 27 | * CAN Bitrate Type. The supported bitrates. |
| 59 | jimmy | 28 | */ |
| 29 | typedef enum { |
||
| 142 | jimmy | 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 */ |
||
| 127 | jimmy | 34 | } Can_Bitrate_t; |
| 59 | jimmy | 35 | |
| 142 | jimmy | 36 | |
| 59 | jimmy | 37 | /** |
| 38 | * CAN Return Type. Most CAN functions return one of these. |
||
| 39 | */ |
||
| 40 | typedef enum { |
||
| 142 | jimmy | 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 |
||
| 127 | jimmy | 48 | } Can_Return_t; |
| 59 | jimmy | 49 | |
| 142 | jimmy | 50 | |
| 59 | jimmy | 51 | /** |
| 52 | * CAN Message Type. Stores a complete CAN frame. |
||
| 53 | */ |
||
| 54 | typedef struct { |
||
| 55 | /* |
||
| 69 | jimmy | 56 | * ID section. Contains either 29bit ID or 11bit ID (depending on ExtendedFlag). |
| 59 | jimmy | 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; |
||
| 127 | jimmy | 88 | } Can_Message_t; |
| 59 | jimmy | 89 | |
| 90 | |||
| 91 | /*----------------------------------------------------------------------------- |
||
| 92 | * Public Function Prototypes |
||
| 93 | *---------------------------------------------------------------------------*/ |
||
| 94 | |||
| 127 | jimmy | 95 | Can_Return_t Can_Init(void); |
| 96 | Can_Return_t Can_Send(Can_Message_t* msg); |
||
| 142 | jimmy | 97 | void Can_Service(void); |
| 127 | jimmy | 98 | Can_Return_t Can_Receive(Can_Message_t *msg); |
| 59 | jimmy | 99 | |
| 100 | #endif |