0
|
1 |
/*
|
|
2 |
This file is part of CanFestival, a library implementing CanOpen Stack.
|
|
3 |
|
|
4 |
Copyright (C): Edouard TISSERANT and Francis DUPIN
|
|
5 |
|
|
6 |
See COPYING file for copyrights details.
|
|
7 |
|
|
8 |
This library is free software; you can redistribute it and/or
|
|
9 |
modify it under the terms of the GNU Lesser General Public
|
|
10 |
License as published by the Free Software Foundation; either
|
|
11 |
version 2.1 of the License, or (at your option) any later version.
|
|
12 |
|
|
13 |
This library is distributed in the hope that it will be useful,
|
|
14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
16 |
Lesser General Public License for more details.
|
|
17 |
|
|
18 |
You should have received a copy of the GNU Lesser General Public
|
|
19 |
License along with this library; if not, write to the Free Software
|
|
20 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
21 |
*/
|
|
22 |
|
|
23 |
#ifndef __CANDRIVER__
|
|
24 |
#define __CANDRIVER__
|
|
25 |
|
|
26 |
//#include DEBUG_CAN
|
|
27 |
|
|
28 |
#include <can.h>
|
|
29 |
#include <objdictdef.h>
|
|
30 |
|
|
31 |
|
|
32 |
/*
|
|
33 |
The CAN message received are stored in a fifo stack
|
|
34 |
We consider one stack for all the 5 can devices. It is a choice !
|
|
35 |
*/
|
|
36 |
|
|
37 |
/* Be free to change this value */
|
|
38 |
#define MAX_STACK_MSG_RCV 5
|
|
39 |
|
|
40 |
/* Number of incomings and outcomings CAN Line. The layer CanOpen must be
|
|
41 |
used only for ONE line CAN. But you may used one or more CAN drivers, without
|
|
42 |
a CanOpen layer.
|
|
43 |
Only 2 lines are implemented. If more lines are needed, copy void __attribute__((interrupt)) can0HdlRcv (void) to void __attribute__((interrupt)) canXHdlRcv (void) and make
|
|
44 |
changes : [0] to [x], CAN0 to CANX, etc
|
|
45 |
*/
|
|
46 |
#define NB_LINE_CAN 1
|
|
47 |
|
|
48 |
/* Whose hardware HCS12 CAN block is used for CanOpen ? Chose between CAN0, ..., CAN4
|
|
49 |
If you use CANOPEN_LINE_NUMBER_USED = CANI, the value of NB_LINE_CAN must be
|
|
50 |
more or equal to I + 1
|
|
51 |
Value other than CAN0 not tested, but should work fine.
|
|
52 |
*/
|
|
53 |
#define CANOPEN_LINE_NUMBER_USED CAN0
|
|
54 |
|
|
55 |
/* Stack of received messages
|
|
56 |
MSG received on CAN0 module is stored in stackMsgRcv[0], etc
|
|
57 |
*/
|
|
58 |
extern volatile Message stackMsgRcv[NB_LINE_CAN][MAX_STACK_MSG_RCV];
|
|
59 |
|
|
60 |
|
|
61 |
/* Copy from the stack of the message to treat */
|
|
62 |
extern Message msgRcv;
|
|
63 |
|
|
64 |
|
|
65 |
/* To move on the stack of messages
|
|
66 |
*/
|
|
67 |
typedef struct {
|
|
68 |
UNS8 w ; /* received */
|
|
69 |
UNS8 r ; /* To transmit */
|
|
70 |
} t_pointerStack;
|
|
71 |
|
|
72 |
|
|
73 |
/* Pointer for write or read a message in/from the reception stack */
|
|
74 |
extern volatile t_pointerStack ptrMsgRcv[NB_LINE_CAN];
|
|
75 |
|
|
76 |
/*
|
|
77 |
Parameters to define the clock system for the CAN bus
|
|
78 |
example :
|
|
79 |
CAN_BUS_TIME clk = {
|
|
80 |
1, // clksrc: Use the bus clock : 16 MHz, the freq. of the quartz's board
|
|
81 |
0, // brp : chose btw 0 and 63 (6 bits). freq time quantum = 16MHz / (brp + 1)
|
|
82 |
1, // sjw : chose btw 0 and 3 (2 bits). Sync on (sjw + 1 ) time quantum
|
|
83 |
1, // samp : chose btw 0 and 3 (2 bits) (samp + 1 ) samples per bit
|
|
84 |
4, // tseg2 : chose btw 0 and 7 (3 bits) Segment 2 width = (tseg2 + 1) tq
|
|
85 |
9, // tseg1 : chose btw 0 and 15 (4 bits) Segment 1 width = (tseg1 + 1) tq
|
|
86 |
|
|
87 |
|
|
88 |
With these values,
|
|
89 |
- The width of the bit time is 16 time quantum :
|
|
90 |
- 1 tq for the SYNC segment (could not be modified)
|
|
91 |
- 10 tq for the TIME 1 segment (tseg1 = 9)
|
|
92 |
- 5 tq for the TIME 2 segment (tseg2 = 4)
|
|
93 |
- Because the bus clock of the MSCAN is 16 MHZ, and the
|
|
94 |
freq of the time quantum is 16 MHZ (brp = 0), and there are 16 tq in the bit time,
|
|
95 |
so the freq of the bit time is 1 MHz.
|
|
96 |
|
|
97 |
};
|
|
98 |
*/
|
|
99 |
typedef struct {
|
|
100 |
UNS8 clksrc; /* use of internal clock or clock bus */
|
|
101 |
UNS8 brp; /* define the bus speed */
|
|
102 |
UNS8 sjw; /* Number of time quantum for synchro - 1 */
|
|
103 |
UNS8 samp; /* Number of sample per bit (1 or 3) */
|
|
104 |
UNS8 tseg2; /* Width of the time segment 2 (in tq) - 1 */
|
|
105 |
UNS8 tseg1; /* Width of the time segment 1 (in tq) - 1 */
|
|
106 |
} canBusTime;
|
|
107 |
|
|
108 |
/*
|
|
109 |
Parameters to init the filters for received messages
|
|
110 |
*/
|
|
111 |
typedef struct {
|
|
112 |
UNS8 idam; /* Put 0x01 for 16 bits acceptance filter */
|
|
113 |
UNS8 canidar0;
|
|
114 |
UNS8 canidmr0;
|
|
115 |
UNS8 canidar1;
|
|
116 |
UNS8 canidmr1;
|
|
117 |
UNS8 canidar2;
|
|
118 |
UNS8 canidmr2;
|
|
119 |
UNS8 canidar3;
|
|
120 |
UNS8 canidmr3;
|
|
121 |
UNS8 canidar4;
|
|
122 |
UNS8 canidmr4;
|
|
123 |
UNS8 canidar5;
|
|
124 |
UNS8 canidmr5;
|
|
125 |
UNS8 canidar6;
|
|
126 |
UNS8 canidmr6;
|
|
127 |
UNS8 canidar7;
|
|
128 |
UNS8 canidmr7;
|
|
129 |
} canBusFilterInit;
|
|
130 |
|
|
131 |
/*
|
|
132 |
Parameters to init MSCAN
|
|
133 |
Example
|
|
134 |
CAN_BUS_INIT bi = {
|
|
135 |
0, no low power
|
|
136 |
0, no time stamp
|
|
137 |
1, enable MSCAN
|
|
138 |
0, clock source : oscillator
|
|
139 |
0, no loop back
|
|
140 |
0, no listen only
|
|
141 |
0, no low pass filter for wk up
|
|
142 |
{
|
|
143 |
1, Use the oscillator clock
|
|
144 |
0, Quartz oscillator : freq time quantum = freq oscillator clock / (0 + 1)
|
|
145 |
1, Sync on (1 + 1) time quantum
|
|
146 |
1, 1 sample per bit
|
|
147 |
4, time segment 2 width : (4 + 1) tq
|
|
148 |
9, time segment 1 width : (9 + 1) tq
|
|
149 |
}
|
|
150 |
};
|
|
151 |
*/
|
|
152 |
|
|
153 |
typedef struct {
|
|
154 |
UNS8 cswai; /* Low power/normal in wait mode (1/0) */
|
|
155 |
UNS8 time; /* Timer for time-stamp enable/disable (1/0) */
|
|
156 |
UNS8 cane; /* Enable MSCAN (yes=1) Do it ! */
|
|
157 |
UNS8 clksrc; /* clock source bus/oscillator (1/0) */
|
|
158 |
UNS8 loopb; /* loop back mode for test (yes=1/no=0) */
|
|
159 |
UNS8 listen; /* MSCAN is listen only (yes=1/no=0 ie normal)*/
|
|
160 |
UNS8 wupm; /* low pas filter for wake up (yes=1/no=0) */
|
|
161 |
canBusTime
|
|
162 |
clk; /* Values for clock system init */
|
|
163 |
canBusFilterInit
|
|
164 |
fi; /* Values for filter acceptance msg init */
|
|
165 |
|
|
166 |
} canBusInit;
|
|
167 |
|
|
168 |
extern canBusInit bi;
|
|
169 |
|
|
170 |
|
7
|
171 |
|
0
|
172 |
/*
|
|
173 |
For the received messsage, add a Identificator to
|
|
174 |
the list of ID to accept.
|
|
175 |
You can use several times this function to accept several messages.
|
|
176 |
It configures registers on 16 bits.
|
|
177 |
Automatically, it configure the filter to
|
|
178 |
- not accepting the msg on 29 bits (ide=1 refused)
|
|
179 |
- not filtering on rtr state (rtr = 1 and rtr = 0 are accepted)
|
|
180 |
Algo :
|
|
181 |
if CANIDARx = 0 then CANIDARx = id . else do nothing
|
|
182 |
CANIDMRx = CANIDMRx OR (CANIDARx XOR id )
|
|
183 |
nFilter : 0 to 3
|
|
184 |
Must be in init mode before.
|
|
185 |
*/
|
|
186 |
char canAddIdToFilter (
|
|
187 |
UNS16 adrCAN,
|
|
188 |
UNS8 nFilter,
|
|
189 |
UNS16 id /* 11 bits, the 5 msb not used */
|
|
190 |
);
|
|
191 |
|
|
192 |
/*
|
7
|
193 |
Use this function to change the CAN message acceptance filters and masks.
|
|
194 |
*/
|
|
195 |
char canChangeFilter (UNS16 adrCAN, canBusFilterInit fi);
|
|
196 |
|
|
197 |
|
|
198 |
/*
|
0
|
199 |
Enable one of the 5 MSCAN.
|
|
200 |
Must be done only one time after a reset of the CPU.
|
|
201 |
To do before any CAN initialisation
|
|
202 |
*/
|
|
203 |
char canEnable (
|
|
204 |
UNS16 adrCAN /* First address of MSCANx registers */
|
|
205 |
);
|
|
206 |
|
|
207 |
|
|
208 |
/*
|
|
209 |
Initialize one of the 5 mscan
|
|
210 |
can be done multiple times in your code
|
|
211 |
Return 0 : OK
|
|
212 |
When it return from the function,
|
|
213 |
mscan is on sleep mode with wake up disabled.
|
|
214 |
is not on init mode
|
|
215 |
*/
|
|
216 |
char canInit (
|
|
217 |
UNS16 adrCAN, /* First address of MSCANx registers */
|
|
218 |
canBusInit
|
|
219 |
bi /* All the parameters to init the bus */
|
|
220 |
);
|
|
221 |
/*
|
|
222 |
Initialize the parameters of the system clock for the MSCAN
|
|
223 |
You must put the MSCAN in sleep mode before with canSleepMode()
|
|
224 |
Return 0 : OK
|
|
225 |
1 : Not in sleep mode. Unable to init MSCAN
|
|
226 |
*/
|
|
227 |
char canInitClock (
|
|
228 |
UNS16 adrCAN, /* First address of MSCANx registers */
|
|
229 |
canBusTime clk);
|
|
230 |
|
|
231 |
/*
|
|
232 |
Initialize one filter for acceptance of received msg.
|
|
233 |
Filters MUST be configured on 16 bits
|
|
234 |
(See doc Motorola mscan bloc guide fig 4.3)
|
|
235 |
Must be in init mode before.
|
|
236 |
adrCAN : adress of the first register of the mscan module
|
|
237 |
nFilter : the filter : 0 to 3.
|
|
238 |
ar : Value to write in acceptance register
|
|
239 |
Beware ! hight byte and low byte inverted.
|
|
240 |
for example if nFilter = 0, hight byte of ar -> CANIDAR0
|
|
241 |
low byte of ar -> CANIDAR1
|
|
242 |
mr : Value to write in mask register
|
|
243 |
Beware ! hight byte and low byte inverted.
|
|
244 |
for example if nFilter = 2, hight byte of ar -> CANIDMR4
|
|
245 |
low byte of ar -> CANIDMR5
|
|
246 |
*/
|
|
247 |
char canInit1Filter (
|
|
248 |
UNS16 adrCAN,
|
|
249 |
UNS8 nFilter,
|
|
250 |
UNS16 ar,
|
|
251 |
UNS16 mr
|
|
252 |
);
|
|
253 |
|
|
254 |
/*
|
|
255 |
Initialise the parameters for filtering the messages received.
|
|
256 |
You must put the MSCAN in init mode before with canInitMode()
|
|
257 |
Return 0 : OK
|
|
258 |
1 : Not in init mode. Unable to init MSCAN
|
|
259 |
*/
|
|
260 |
|
|
261 |
char canInitFilter (
|
|
262 |
UNS16 adrCAN, /* First address of MSCANx registers */
|
|
263 |
canBusFilterInit fi);
|
|
264 |
/*
|
|
265 |
Put one of the 5 mscan in Init mode
|
|
266 |
Loop until init mode is reached.
|
|
267 |
*/
|
|
268 |
|
|
269 |
char canInitMode (
|
|
270 |
UNS16 adrCAN /* First address of MSCANx registers */
|
|
271 |
);
|
|
272 |
|
|
273 |
/*
|
|
274 |
Leave the Init mode
|
|
275 |
loop until init mode leaved.
|
|
276 |
*/
|
|
277 |
|
|
278 |
char canInitModeQ (
|
|
279 |
UNS16 adrCAN /* First address of MSCANx registers */
|
|
280 |
);
|
|
281 |
|
|
282 |
|
|
283 |
|
|
284 |
/*
|
|
285 |
Transmit a msg on CAN "adrCan"
|
|
286 |
Return : 0 No error
|
|
287 |
other error : no buffer available to make the transmission
|
|
288 |
*/
|
|
289 |
|
|
290 |
char canMsgTransmit (
|
|
291 |
UNS16 adrCAN, /* First address of MSCANx registers */
|
|
292 |
Message msg /* Message to transmit */
|
|
293 |
);
|
|
294 |
|
|
295 |
/*
|
7
|
296 |
Set the interruptions. Must be call just after having left the init mode.
|
|
297 |
*/
|
|
298 |
char canSetInterrupt (UNS16 adrCAN);
|
|
299 |
|
|
300 |
/*
|
0
|
301 |
Put one of the 5 mscan in sleep mode
|
|
302 |
Beware! If some messages are to be sent,
|
|
303 |
or if it is receiving, going into sleep mode
|
|
304 |
may take time.
|
|
305 |
Wake up is disabled : stay in sleep mode even if
|
|
306 |
bus traffic detected.
|
|
307 |
return 0 if 0K, other if error : mscan is on init mode.
|
|
308 |
Stay in this function until the sleep mode
|
|
309 |
is reached.
|
|
310 |
*/
|
|
311 |
char canSleepMode (
|
|
312 |
UNS16 adrCAN /* First address of MSCANx registers */
|
|
313 |
);
|
|
314 |
|
|
315 |
/*
|
|
316 |
Leave the sleep mode
|
|
317 |
loop until sleep mode leaved.
|
|
318 |
return 0 : OK
|
|
319 |
return 1 : error : in init mode
|
|
320 |
*/
|
|
321 |
char canSleepModeQ (
|
|
322 |
UNS16 adrCAN /* First address of MSCANx registers */
|
|
323 |
);
|
|
324 |
|
|
325 |
/*
|
|
326 |
Put one of the 5 mscan in sleep mode
|
|
327 |
MSCAN must not be in init mode.
|
|
328 |
wake up is enabled : wake up if traffic on CAN is detected
|
|
329 |
Beware! If some messages are to be sent,
|
|
330 |
or if it is receiving, going into sleep mode
|
|
331 |
may take time.
|
|
332 |
Loop until sleep mode reached.
|
|
333 |
return 0 if 0K, other if error
|
|
334 |
*/
|
|
335 |
char canSleepWupMode (
|
|
336 |
UNS16 adrCAN /* First address of MSCANx registers */
|
|
337 |
);
|
|
338 |
|
|
339 |
/*
|
|
340 |
Test if one of the 5 mscan is in init mode.
|
|
341 |
Return
|
|
342 |
0 -> Not in init mode
|
|
343 |
other -> In init mode
|
|
344 |
*/
|
|
345 |
char canTestInitMode (
|
|
346 |
UNS16 adrCAN /* First address of MSCANx registers */
|
|
347 |
);
|
|
348 |
|
|
349 |
/*
|
|
350 |
Test if one of the 5 mscan is in sleep mode.
|
|
351 |
Return
|
|
352 |
0 -> Not in sleep mode
|
|
353 |
other -> In sleep mode
|
|
354 |
*/
|
|
355 |
char canTestSleepMode (
|
|
356 |
UNS16 adrCAN /* First address of MSCANx registers */
|
|
357 |
);
|
|
358 |
|
|
359 |
|
|
360 |
|
|
361 |
#endif /*__CANDRIVER__*/
|
|
362 |
|