Rev 73 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 73 | Rev 127 | ||
|---|---|---|---|
| Line 1... | Line 1... | ||
| 1 | /** |
1 | /** |
| 2 | * CAN Interface. This is a general high-level interface for CAN communication. |
2 | * CAN Interface. This is a general high-level interface for CAN communication. |
| 3 | * Underlying CAN controllers are abstracted by this interface, so the |
3 | * Underlying CAN controllers are abstracted by this interface, so the |
| 4 | * application needs not work directly against the CAN controllers. |
4 | * application needs not work directly against the CAN controllers. |
| 5 | * |
5 | * |
| 6 | * @date 2006- |
6 | * @date 2006-11-19 |
| 7 | * |
7 | * |
| 8 | * @author Jimmy Myhrman |
8 | * @author Jimmy Myhrman |
| 9 | * @author Martin Thomas |
9 | * @author Martin Thomas |
| 10 | * |
10 | * |
| 11 | */ |
11 | */ |
| Line 28... | Line 28... | ||
| 28 | /*----------------------------------------------------------------------------- |
28 | /*----------------------------------------------------------------------------- |
| 29 | * Functions |
29 | * Functions |
| 30 | *---------------------------------------------------------------------------*/ |
30 | *---------------------------------------------------------------------------*/ |
| 31 | 31 | ||
| 32 | /** |
32 | /** |
| 33 | * Initializes the CAN interface. |
33 | * Initializes the CAN interface. Edit can_cfg.h to choose bitrate. |
| 34 | * |
34 | * |
| 35 | * @param bitrate |
- | |
| 36 | * The bitrate that should be used. |
- | |
| 37 | * @return |
35 | * @return |
| 38 | * CAN_OK if initialization was successful. |
36 | * CAN_OK if initialization was successful. |
| 39 | * CAN_FAILINIT otherwise. |
37 | * CAN_FAILINIT otherwise. |
| 40 | */ |
38 | */ |
| 41 |
|
39 | Can_Return_t Can_Init() { |
| 42 | - | ||
| - | 40 | Can_Bitrate_t bitrate = CAN_BITRATE; |
|
| 43 | #ifdef CAN_CONTROLLER_MCP2515 |
41 | #ifdef CAN_CONTROLLER_MCP2515 |
| 44 | if ( |
42 | if (MCP2515_Init(bitrate) != MCP2515_OK) { |
| 45 | return CAN_FAILINIT; |
43 | return CAN_FAILINIT; |
| 46 | } |
44 | } |
| 47 | if ( |
45 | if (MCP2515_SetCanCtrlMode(MODE_NORMAL) != MCP2515_OK) { |
| 48 | return CAN_FAILINIT; |
46 | return CAN_FAILINIT; |
| 49 | } |
47 | } |
| 50 | return CAN_OK; |
48 | return CAN_OK; |
| 51 | #endif |
49 | #endif |
| 52 | 50 | ||
| Line 63... | Line 61... | ||
| 63 | * Pointer to the CAN message storage buffer. |
61 | * Pointer to the CAN message storage buffer. |
| 64 | * @return |
62 | * @return |
| 65 | * CAN_OK if the message was successfully sent (or put in queue). |
63 | * CAN_OK if the message was successfully sent (or put in queue). |
| 66 | * CAN_FAILTX if the controller is busy AND the transmission queue is full. |
64 | * CAN_FAILTX if the controller is busy AND the transmission queue is full. |
| 67 | */ |
65 | */ |
| 68 |
|
66 | Can_Return_t Can_Send(Can_Message_t* msg) { |
| 69 | 67 | ||
| 70 | #ifdef CAN_CONTROLLER_MCP2515 |
68 | #ifdef CAN_CONTROLLER_MCP2515 |
| 71 | uint8_t res, txbuf_n; |
69 | uint8_t res, txbuf_n; |
| 72 | res = |
70 | res = MCP2515_GetNextFreeTXBuf(&txbuf_n); // info = addr. |
| 73 | if (res == MCP_ALLTXBUSY) { |
71 | if (res == MCP_ALLTXBUSY) { |
| 74 | return CAN_FAILTX; |
72 | return CAN_FAILTX; |
| 75 | } |
73 | } |
| 76 |
|
74 | MCP2515_WriteCanMsg(txbuf_n, msg); |
| 77 |
|
75 | MCP2515_StartTransmit(txbuf_n); |
| 78 | return CAN_OK; |
76 | return CAN_OK; |
| 79 | #endif |
77 | #endif |
| 80 | 78 | ||
| 81 | } |
79 | } |
| 82 | 80 | ||
| Line 90... | Line 88... | ||
| 90 | * Pointer to the message storage buffer. |
88 | * Pointer to the message storage buffer. |
| 91 | * @return |
89 | * @return |
| 92 | * CAN_OK if a received message was successfully copied into the buffer. |
90 | * CAN_OK if a received message was successfully copied into the buffer. |
| 93 | * CAN_NOMSG if there are no received messages available. |
91 | * CAN_NOMSG if there are no received messages available. |
| 94 | */ |
92 | */ |
| 95 |
|
93 | Can_Return_t Can_Receive(Can_Message_t *msg) { |
| 96 | 94 | ||
| 97 | #ifdef CAN_CONTROLLER_MCP2515 |
95 | #ifdef CAN_CONTROLLER_MCP2515 |
| 98 | uint8_t stat, res; |
96 | uint8_t stat, res; |
| 99 | stat = |
97 | stat = MCP2515_ReadStatus(); |
| 100 | if (stat & MCP_STAT_RX0IF) { |
98 | if (stat & MCP_STAT_RX0IF) { |
| 101 | // Msg in Buffer 0 |
99 | // Msg in Buffer 0 |
| 102 |
|
100 | MCP2515_ReadCanMsg( MCP_RXBUF_0, msg); |
| 103 |
|
101 | MCP2515_ModifyRegister(MCP_CANINTF, MCP_RX0IF, 0); |
| 104 | res = CAN_OK; |
102 | res = CAN_OK; |
| 105 | } |
103 | } |
| 106 | else if (stat & MCP_STAT_RX1IF) { |
104 | else if (stat & MCP_STAT_RX1IF) { |
| 107 | // Msg in Buffer 1 |
105 | // Msg in Buffer 1 |
| 108 |
|
106 | MCP2515_ReadCanMsg( MCP_RXBUF_1, msg); |
| 109 |
|
107 | MCP2515_ModifyRegister(MCP_CANINTF, MCP_RX1IF, 0); |
| 110 | res = CAN_OK; |
108 | res = CAN_OK; |
| 111 | } |
109 | } |
| 112 | else { |
110 | else { |
| 113 | res = CAN_NOMSG; |
111 | res = CAN_NOMSG; |
| 114 | } |
112 | } |
| Line 123... | Line 121... | ||
| 123 | * |
121 | * |
| 124 | * @return |
122 | * @return |
| 125 | * CAN_MSGAVAIL if there are messages available. |
123 | * CAN_MSGAVAIL if there are messages available. |
| 126 | * CAN_NOMSG if the queue is empty. |
124 | * CAN_NOMSG if the queue is empty. |
| 127 | */ |
125 | */ |
| 128 |
|
126 | Can_Return_t Can_ReceiveAvailable(void) { |
| 129 | 127 | ||
| 130 | #ifdef CAN_CONTROLLER_MCP2515 |
128 | #ifdef CAN_CONTROLLER_MCP2515 |
| 131 | uint8_t res; |
129 | uint8_t res; |
| 132 | res = |
130 | res = MCP2515_ReadStatus(); // RXnIF in Bit 1 and 0 |
| 133 | if ( res & MCP_STAT_RXIF_MASK ) { |
131 | if ( res & MCP_STAT_RXIF_MASK ) { |
| 134 | return CAN_MSGAVAIL; |
132 | return CAN_MSGAVAIL; |
| 135 | } |
133 | } |
| 136 | else { |
134 | else { |
| 137 | return CAN_NOMSG; |
135 | return CAN_NOMSG; |
| Line 148... | Line 146... | ||
| 148 | * CAN_OK if the controller is OK. |
146 | * CAN_OK if the controller is OK. |
| 149 | * CAN_CTRLERROR if errors have occured. |
147 | * CAN_CTRLERROR if errors have occured. |
| 150 | * @todo |
148 | * @todo |
| 151 | * Styr upp denna funktion lite bättre. |
149 | * Styr upp denna funktion lite bättre. |
| 152 | */ |
150 | */ |
| 153 |
|
151 | Can_Return_t Can_CheckError(void) { |
| 154 | 152 | ||
| 155 | #ifdef CAN_CONTROLLER_MCP2515 |
153 | #ifdef CAN_CONTROLLER_MCP2515 |
| 156 | uint8_t eflg = |
154 | uint8_t eflg = MCP2515_ReadRegister(MCP_EFLG); |
| 157 | if (eflg & MCP_EFLG_ERRORMASK) { |
155 | if (eflg & MCP_EFLG_ERRORMASK) { |
| 158 | return CAN_CTRLERROR; |
156 | return CAN_CTRLERROR; |
| 159 | } |
157 | } |
| 160 | else { |
158 | else { |
| 161 | return CAN_OK; |
159 | return CAN_OK; |