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