author | Edouard Tisserant |
Thu, 24 Jan 2019 13:53:01 +0100 | |
changeset 808 | de1fc3261f21 |
parent 801 | 32d146b64a35 |
permissions | -rw-r--r-- |
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 |
#define DEBUG_WAR_CONSOLE_ON |
|
24 |
#define DEBUG_ERR_CONSOLE_ON |
|
25 |
||
26 |
#include <stddef.h> /* for NULL */ |
|
27 |
||
28 |
#include "../include/hcs12/asm-m68hc12/portsaccess.h" |
|
29 |
#include "../include/hcs12/asm-m68hc12/ports_def.h" |
|
30 |
#include "../include/hcs12/asm-m68hc12/ports.h" |
|
7 | 31 |
#include "../include/data.h" |
0 | 32 |
#include "../include/hcs12/applicfg.h" |
33 |
#include "../include/hcs12/candriver.h" |
|
34 |
#include "../include/hcs12/interrupt.h" |
|
35 |
#include "../include/hcs12/canOpenDriver.h" |
|
36 |
#include "../include/can.h" |
|
37 |
#include "../include/objdictdef.h" |
|
801
32d146b64a35
Rename timer.h into timers.h to avoid clash with Xenomai includes.
Edouard Tisserant
parents:
384
diff
changeset
|
38 |
#include "../include/timers.h" |
0 | 39 |
|
40 |
||
41 |
||
42 |
||
43 |
||
44 |
||
45 |
volatile static Message stackMsgRcv[NB_LINE_CAN][MAX_STACK_MSG_RCV]; |
|
46 |
volatile static t_pointerStack ptrMsgRcv[NB_LINE_CAN]; |
|
47 |
||
48 |
volatile static TIMEVAL last_time_set = TIMEVAL_MAX; |
|
49 |
static UNS8 timer_is_set = 0; |
|
50 |
||
51 |
/* Prototypes */ |
|
52 |
UNS8 f_can_receive(UNS8 notused, Message *m); |
|
53 |
UNS8 canSend(UNS8 notused, Message *m); |
|
54 |
void __attribute__((interrupt)) timer4Hdl (void); |
|
55 |
||
56 |
#define max(a,b) a>b?a:b |
|
57 |
||
58 |
/******************************************************************************/ |
|
59 |
void setTimer(TIMEVAL value) |
|
60 |
{ |
|
61 |
IO_PORTS_16(TC4H) += value; |
|
62 |
timer_is_set = 1; |
|
63 |
} |
|
64 |
||
65 |
/******************************************************************************/ |
|
66 |
TIMEVAL getElapsedTime() |
|
67 |
{ |
|
68 |
return (IO_PORTS_16(TC4H) > last_time_set ? IO_PORTS_16(TC4H) - last_time_set : last_time_set - IO_PORTS_16(TC4H)); |
|
69 |
} |
|
70 |
||
71 |
||
72 |
/******************************************************************************/ |
|
73 |
void resetTimer(void) |
|
74 |
{ |
|
75 |
||
76 |
} |
|
77 |
||
78 |
/******************************************************************************/ |
|
79 |
void initTimer(void) |
|
80 |
{ |
|
81 |
lock(); // Inhibition of interruptions |
|
82 |
||
83 |
// Configure the timer channel 4 |
|
84 |
IO_PORTS_8(TIOS) |= 0x10; // Canal 4 in output |
|
85 |
IO_PORTS_8(TCTL1) &= ~(0x01 + 0x02); // Canal 4 unconnected to pin output |
|
86 |
IO_PORTS_8(TIE) |= 0x10; // allow interruption channel 4 |
|
87 |
IO_PORTS_8(TSCR2) |= 0X05; // Pre-scaler = 32 |
|
88 |
// If this value is changed, change must be done also |
|
89 |
// in void __attribute__((interrupt)) timer4Hdl (void) |
|
90 |
||
91 |
IO_PORTS_8(TSCR1) |= 0x80; // Start timer |
|
92 |
unlock(); // Allow interruptions |
|
93 |
} |
|
94 |
||
95 |
/******************************************************************************/ |
|
96 |
void __attribute__((interrupt)) timer4Hdl (void) |
|
97 |
{ |
|
98 |
lock(); |
|
99 |
last_time_set = IO_PORTS_16(TC4H); |
|
100 |
IO_PORTS_8(TFLG1) = 0x10; // RAZ flag interruption timer channel 4 |
|
101 |
// Compute the next event : When the timer reach the value of TC4, an interrupt is |
|
102 |
// started |
|
103 |
//IO_PORTS_16(TC4H) += 250; // To have an interruption every 1 ms |
|
104 |
//timerInterrupt(0); |
|
105 |
//MSG_WAR(0xFFFF, "timer4 IT", 0); |
|
106 |
{ |
|
107 |
//MSG_WAR(0xFFFF, "t4 ", IO_PORTS_16(TCNTH) - IO_PORTS_16(TC4H)); |
|
108 |
} |
|
109 |
TimeDispatch(); |
|
110 |
unlock(); |
|
111 |
} |
|
112 |
||
113 |
||
114 |
/******************************************************************************/ |
|
115 |
void initSCI_0(void) |
|
116 |
{ |
|
117 |
IO_PORTS_16(SCI0 + SCIBDH) = |
|
118 |
((1000000 / SERIAL_SCI0_BAUD_RATE) * BUS_CLOCK) >> 4 ; |
|
119 |
IO_PORTS_8(SCI0 + SCICR1) = 0; // format 8N1 |
|
120 |
IO_PORTS_8(SCI0 + SCICR2) = 0x08; // Transmit enable only |
|
121 |
} |
|
122 |
||
123 |
/******************************************************************************/ |
|
124 |
void initSCI_1(void) |
|
125 |
{ |
|
126 |
IO_PORTS_16(SCI1 + SCIBDH) = |
|
127 |
((1000000 / SERIAL_SCI1_BAUD_RATE) * BUS_CLOCK) >> 4 ; |
|
128 |
IO_PORTS_8(SCI1 + SCICR1) = 0; // format 8N1 |
|
129 |
IO_PORTS_8(SCI1 + SCICR2) = 0x08; // Transmit enable only |
|
130 |
} |
|
131 |
||
132 |
||
133 |
/******************************************************************************/ |
|
134 |
char * |
|
135 |
hex_convert (char *buf, unsigned long value, char lastCar) |
|
136 |
{ |
|
384 | 137 |
//Thanks to Stphane Carrez for this function |
0 | 138 |
char num[32]; |
139 |
int pos; |
|
140 |
||
141 |
*buf++ = '0'; |
|
142 |
*buf++ = 'x'; |
|
143 |
||
144 |
pos = 0; |
|
145 |
while (value != 0) { |
|
146 |
char c = value & 0x0F; |
|
147 |
num[pos++] = "0123456789ABCDEF"[(unsigned) c]; |
|
148 |
value = (value >> 4) & (0x0fffffffL); |
|
149 |
} |
|
150 |
if (pos == 0) |
|
151 |
num[pos++] = '0'; |
|
152 |
||
153 |
while (--pos >= 0) |
|
154 |
*buf++ = num[pos]; |
|
155 |
||
156 |
*buf++ = lastCar; |
|
157 |
*buf = 0; |
|
158 |
return buf; |
|
159 |
} |
|
160 |
||
161 |
/******************************************************************************/ |
|
162 |
void printSCI_str(char sci, const char * str) |
|
163 |
{ |
|
164 |
char i = 0; |
|
165 |
||
166 |
while ((*(str + i) != 0) && (i < 0xFF)) { |
|
167 |
if (*(str + i) == '\n') |
|
168 |
{ |
|
169 |
while ((IO_PORTS_8(sci + SCISR1) & 0X80) == 0); // wait if buffer not empty |
|
170 |
IO_PORTS_8(sci + SCIDRL) = 13; // return to start of line |
|
171 |
} |
|
172 |
while ((IO_PORTS_8(sci + SCISR1) & 0X80) == 0); // wait if buffer not empty |
|
173 |
IO_PORTS_8(sci + SCIDRL) = *(str + i++); |
|
174 |
} |
|
175 |
||
176 |
} |
|
177 |
||
178 |
/******************************************************************************/ |
|
179 |
void printSCI_nbr(char sci, unsigned long nbr, char lastCar) |
|
180 |
{ |
|
181 |
char strNbr[12]; |
|
182 |
hex_convert(strNbr, nbr, lastCar); |
|
183 |
printSCI_str(sci, strNbr); |
|
184 |
} |
|
185 |
||
186 |
/******************************************************************************/ |
|
187 |
// PLL 24 MHZ if quartz on board is 16 MHZ |
|
188 |
void initPLL(void) |
|
189 |
{ |
|
190 |
IO_PORTS_8(CLKSEL) &= ~0x80; // unselect the PLL |
|
191 |
IO_PORTS_8(PLLCTL) |= 0X60; // PLL ON and bandwidth auto |
|
192 |
IO_PORTS_8(SYNR) = 0x02; |
|
193 |
IO_PORTS_8(REFDV) = 0x01; |
|
194 |
while ((IO_PORTS_8(CRGFLG) & 0x08) == 0); |
|
195 |
IO_PORTS_8(CLKSEL) |= 0x80; |
|
196 |
} |
|
197 |
||
198 |
/******************************************************************************/ |
|
199 |
void initHCS12(void) |
|
200 |
{ |
|
201 |
||
202 |
# ifdef USE_PLL |
|
203 |
MSG_WAR(0x3620, "Use the PLL ", 0); |
|
204 |
initPLL(); |
|
205 |
# endif |
|
206 |
||
207 |
} |
|
208 |
||
209 |
/***************************************************************************/ |
|
210 |
char canAddIdToFilter(UNS16 adrCAN, UNS8 nFilter, UNS16 id) |
|
211 |
{ |
|
212 |
UNS8 fiMsb; |
|
213 |
UNS8 fiLsb; |
|
214 |
UNS8 idMsb = (UNS8) (id >> 3); |
|
215 |
UNS8 idLsb = (UNS8) (id << 5); |
|
216 |
||
217 |
if (! canTestInitMode(adrCAN)) { |
|
218 |
/* Error because not init mode */ |
|
219 |
MSG_WAR(0X2600, "Not in init mode ", 0); |
|
220 |
return 1; |
|
221 |
} |
|
222 |
switch (nFilter) { |
|
223 |
case 0: |
|
224 |
nFilter = CANIDAR0; /* First bank */ |
|
225 |
break; |
|
226 |
case 1: |
|
227 |
nFilter = CANIDAR2; /* First bank */ |
|
228 |
break; |
|
229 |
case 2: |
|
230 |
nFilter = CANIDAR4; /* Second bank */ |
|
231 |
break; |
|
232 |
case 3: |
|
233 |
nFilter = CANIDAR6; /* Second bank */ |
|
234 |
} |
|
235 |
if (! IO_PORTS_16(adrCAN + nFilter)) { |
|
236 |
/* if CANIDARx = 0 */ |
|
237 |
IO_PORTS_8(adrCAN + nFilter) = idMsb; |
|
238 |
IO_PORTS_8(adrCAN + nFilter + 1) = idLsb; |
|
239 |
} |
|
240 |
fiMsb = IO_PORTS_8(adrCAN + nFilter) ^ idMsb; |
|
241 |
fiLsb = IO_PORTS_8(adrCAN + nFilter + 1) ^ idLsb; |
|
242 |
/* address of CANIDMRx */ |
|
243 |
IO_PORTS_8(adrCAN + nFilter + 4) = IO_PORTS_8(adrCAN + nFilter + 4) | fiMsb; |
|
244 |
IO_PORTS_8(adrCAN + nFilter + 5) = IO_PORTS_8(adrCAN + nFilter + 5) | fiLsb; |
|
245 |
IO_PORTS_8(adrCAN + nFilter + 5) |= 0x10; /* Not filtering on rtr value */ |
|
246 |
return 0; |
|
247 |
} |
|
248 |
||
249 |
/***************************************************************************/ |
|
7 | 250 |
char canChangeFilter(UNS16 adrCAN, canBusFilterInit fi) |
251 |
{ |
|
252 |
/* If not in init mode, go to sleep before going in init mode*/ |
|
253 |
if (! canTestInitMode(adrCAN)) { |
|
254 |
canSleepMode(adrCAN); |
|
255 |
canInitMode(adrCAN); |
|
256 |
} |
|
257 |
//update the filters configuration |
|
258 |
canInitFilter(adrCAN, fi); |
|
259 |
canInitModeQ(adrCAN); |
|
260 |
canSleepModeQ(adrCAN); |
|
261 |
canSetInterrupt(adrCAN); |
|
262 |
return 0; |
|
263 |
} |
|
264 |
||
265 |
/***************************************************************************/ |
|
0 | 266 |
char canEnable(UNS16 adrCAN) |
267 |
{ |
|
268 |
/* Register CANCTL1 |
|
269 |
bit 7 : 1 MSCAN enabled |
|
270 |
Other bits : default reset values |
|
271 |
*/ |
|
272 |
IO_PORTS_8(adrCAN + CANCTL1) = 0X80; |
|
273 |
return 0; |
|
274 |
} |
|
275 |
||
276 |
/***************************************************************************/ |
|
277 |
char canInit(UNS16 adrCAN, canBusInit bi) |
|
278 |
{ |
|
279 |
/* If not in init mode, go to sleep before going in init mode*/ |
|
280 |
if (! canTestInitMode(adrCAN)) { |
|
281 |
canSleepMode(adrCAN); |
|
282 |
canInitMode(adrCAN); |
|
283 |
} |
|
284 |
||
285 |
canEnable(adrCAN); /* Does nothing if already enable */ |
|
286 |
/* The most secure way to go in init mode : put before MSCAN in sleep mode */ |
|
287 |
//canSleepMode(adrCAN); |
|
288 |
/* Put MSCAN in Init mode */ |
|
289 |
//canInitMode(adrCAN); |
|
290 |
canInitClock(adrCAN, bi.clk); |
|
291 |
/* Init CANCTL1 register. Must be in init mode */ |
|
292 |
IO_PORTS_8(adrCAN + CANCTL1) &=0xC4;// 0xCB; /* Clr the bits that may be modified */ |
|
293 |
IO_PORTS_8(adrCAN + CANCTL1) = (bi.cane << 7) | (bi.loopb << 5 ) | |
|
294 |
(bi.listen << 4) | (bi.wupm << 2); |
|
295 |
/* Initialize the filters for received msgs */ |
|
296 |
/* We should decide to accept all the msgs */ |
|
297 |
canInitFilter(adrCAN, bi.fi); |
|
298 |
/* Before to modify CANCTL0, we must leave the init mode */ |
|
299 |
canInitModeQ(adrCAN); |
|
300 |
/* Init CANCTL0 register. MSCAN must not be in init mode */ |
|
301 |
/* Do not change the value of wupe (should be 0) and slprq (should be 1) */ |
|
302 |
/* Do not change the value of initrq (should be 0) */ |
|
303 |
/* rxfrm is cleared, mupe also (should be before)*/ |
|
304 |
IO_PORTS_8(adrCAN + CANCTL0) &= 0x53; /* Clr the bits that may be modified */ |
|
305 |
IO_PORTS_8(adrCAN + CANCTL0) = (bi.cswai << 5) | (bi.time << 3); |
|
7 | 306 |
canSetInterrupt(adrCAN); |
0 | 307 |
canInitModeQ(adrCAN); /* Leave the init mode */ |
308 |
canSleepModeQ(adrCAN); /* Leave the sleep mode */ |
|
309 |
return 0; |
|
310 |
} |
|
311 |
||
312 |
/***************************************************************************/ |
|
313 |
char canInitClock(UNS16 adrCAN, canBusTime clk) |
|
314 |
{ |
|
315 |
if (! canTestInitMode(adrCAN)) { |
|
316 |
/* Not in Init mode */ |
|
317 |
MSG_WAR(0X2601, "not in init mode ", 0); |
|
318 |
return 1; |
|
319 |
} |
|
320 |
/* Set or reset CLKSRC (register CANCTL1). Does not change the other bits*/ |
|
321 |
clk.clksrc = clk.clksrc << 6; |
|
322 |
IO_PORTS_8(adrCAN + CANCTL1) &= 0xBF; |
|
323 |
IO_PORTS_8(adrCAN + CANCTL1) |= clk.clksrc; |
|
324 |
/* Build the CANBTR0 register */ |
|
325 |
IO_PORTS_8(adrCAN + CANBTR0) = 0x00; /* Clear before changes */ |
|
326 |
IO_PORTS_8(adrCAN + CANBTR0) = (clk.sjw << 6) | (clk.brp); |
|
327 |
/* Build the CANBTR1 register */ |
|
328 |
IO_PORTS_8(adrCAN + CANBTR1) = 0x00; /* Clear before changes */ |
|
329 |
IO_PORTS_8(adrCAN + CANBTR1) = (clk.samp << 7) | (clk.tseg2 << 4) | |
|
330 |
(clk.tseg1); |
|
331 |
return 0; |
|
332 |
} |
|
333 |
||
334 |
/***************************************************************************/ |
|
335 |
char canInit1Filter(UNS16 adrCAN, UNS8 nFilter, UNS16 ar, UNS16 mr) |
|
336 |
{ |
|
337 |
if (! canTestInitMode(adrCAN)) { |
|
338 |
/* Error because not init mode */ |
|
339 |
MSG_WAR(0X2602, "not in init mode ", 0); |
|
340 |
return 1; |
|
341 |
} |
|
342 |
switch (nFilter) { |
|
343 |
case 0: |
|
344 |
nFilter = CANIDAR0; /* First bank */ |
|
345 |
break; |
|
346 |
case 1: |
|
347 |
nFilter = CANIDAR2; /* First bank */ |
|
348 |
break; |
|
349 |
case 2: |
|
350 |
nFilter = CANIDAR4; /* Second bank */ |
|
351 |
break; |
|
352 |
case 3: |
|
353 |
nFilter = CANIDAR6; /* Second bank */ |
|
354 |
} |
|
355 |
/* address of CANIDARx */ |
|
356 |
IO_PORTS_8(adrCAN + nFilter) = (UNS8) (ar >> 8); |
|
357 |
IO_PORTS_8(adrCAN + nFilter + 1) = (UNS8) (ar); |
|
358 |
IO_PORTS_8(adrCAN + nFilter + 4) = (UNS8) (mr >> 8); |
|
359 |
IO_PORTS_8(adrCAN + nFilter + 5) = (UNS8) (mr); |
|
360 |
return 0; |
|
361 |
} |
|
362 |
||
363 |
/***************************************************************************/ |
|
364 |
char canInitFilter(UNS16 adrCAN, canBusFilterInit fi) |
|
365 |
{ |
|
366 |
if (! canTestInitMode(adrCAN)) { |
|
367 |
/* Error because not init mode */ |
|
368 |
MSG_WAR(0X2603, "not in init mode ", 0); |
|
369 |
return 1; |
|
370 |
} |
|
371 |
IO_PORTS_8(adrCAN + CANIDAC) = fi.idam << 4; |
|
372 |
IO_PORTS_8(adrCAN + CANIDAR0) = fi.canidar0; |
|
373 |
IO_PORTS_8(adrCAN + CANIDMR0) = fi.canidmr0; |
|
374 |
IO_PORTS_8(adrCAN + CANIDAR1) = fi.canidar1; |
|
375 |
IO_PORTS_8(adrCAN + CANIDMR1) = fi.canidmr1; |
|
376 |
IO_PORTS_8(adrCAN + CANIDAR2) = fi.canidar2; |
|
377 |
IO_PORTS_8(adrCAN + CANIDMR2) = fi.canidmr2; |
|
378 |
IO_PORTS_8(adrCAN + CANIDAR3) = fi.canidar3; |
|
379 |
IO_PORTS_8(adrCAN + CANIDMR3) = fi.canidmr3; |
|
380 |
IO_PORTS_8(adrCAN + CANIDAR4) = fi.canidar4; |
|
381 |
IO_PORTS_8(adrCAN + CANIDMR4) = fi.canidmr4; |
|
382 |
IO_PORTS_8(adrCAN + CANIDAR5) = fi.canidar5; |
|
383 |
IO_PORTS_8(adrCAN + CANIDMR5) = fi.canidmr5; |
|
384 |
IO_PORTS_8(adrCAN + CANIDAR6) = fi.canidar6; |
|
385 |
IO_PORTS_8(adrCAN + CANIDMR6) = fi.canidmr6; |
|
386 |
IO_PORTS_8(adrCAN + CANIDAR7) = fi.canidar7; |
|
387 |
IO_PORTS_8(adrCAN + CANIDMR7) = fi.canidmr7; |
|
388 |
return 0; |
|
389 |
} |
|
390 |
||
391 |
/***************************************************************************/ |
|
392 |
char canInitMode(UNS16 adrCAN) |
|
393 |
{ |
|
394 |
IO_PORTS_8(adrCAN + CANCTL0) |= 0x01; /* Set the bit INITRQ */ |
|
395 |
while (! canTestInitMode(adrCAN)) { |
|
396 |
} |
|
397 |
return 0; |
|
398 |
} |
|
399 |
||
400 |
/***************************************************************************/ |
|
401 |
char canInitModeQ(UNS16 adrCAN) |
|
402 |
{ |
|
403 |
IO_PORTS_8(adrCAN + CANCTL0) &= 0xFE; /* Clear the bit INITRQ */ |
|
404 |
while (canTestInitMode(adrCAN)) { |
|
405 |
} |
|
406 |
return 0; |
|
407 |
} |
|
408 |
||
409 |
/***************************************************************************/ |
|
410 |
char canMsgTransmit(UNS16 adrCAN, Message msg) |
|
411 |
{ |
|
412 |
/* Remind : only CAN A msg implemented. ie id on 11 bits, not 29 */ |
|
413 |
UNS8 cantflg; |
|
414 |
UNS8 i; |
|
415 |
/* Looking for a free buffer */ |
|
416 |
cantflg = IO_PORTS_8(adrCAN + CANTFLG); |
|
417 |
if ( cantflg == 0) { /* all the TXEx are set */ |
|
418 |
MSG_WAR(0X2604, "No buffer free. Msg to transmit is losted ", 0); |
|
419 |
return 1; /* No buffer free */ |
|
420 |
} |
|
421 |
else{ |
|
422 |
/* Selecting a buffer */ |
|
423 |
IO_PORTS_8(adrCAN + CANTBSEL) = cantflg; |
|
424 |
/* We put ide = 0 because id is on 11 bits only */ |
|
365 | 425 |
IO_PORTS_8(adrCAN + CANTRSID) = (UNS8)(msg.cob_id >> 3); |
426 |
IO_PORTS_8(adrCAN + CANTRSID + 1) = (UNS8)((msg.cob_id << 5)| |
|
0 | 427 |
(msg.rtr << 4)); |
428 |
||
429 |
IO_PORTS_8(adrCAN + CANTRSLEN) = msg.len & 0X0F; |
|
430 |
/* For the priority, we put the highter bits of the cob_id */ |
|
431 |
IO_PORTS_8(adrCAN + CANTRSPRI) = IO_PORTS_8(adrCAN + CANTRSID); |
|
432 |
for (i = 0 ; i < msg.len ; i++) { |
|
433 |
IO_PORTS_8(adrCAN + CANTRSDTA + i) = msg.data[i]; |
|
434 |
} |
|
435 |
/* Transmitting the message */ |
|
436 |
cantflg = IO_PORTS_8(adrCAN + CANTBSEL);/* to know which buf is selected */ |
|
437 |
IO_PORTS_8(adrCAN + CANTBSEL) = 0x00; |
|
438 |
IO_PORTS_8(adrCAN + CANTFLG) = cantflg; /* Ready to transmit ! */ |
|
439 |
||
440 |
} |
|
441 |
return 0; |
|
442 |
} |
|
443 |
||
444 |
/***************************************************************************/ |
|
7 | 445 |
char canSetInterrupt(UNS16 adrCAN) |
446 |
{ |
|
447 |
IO_PORTS_8(adrCAN + CANRIER) = 0X01; /* Allow interruptions on receive */ |
|
448 |
IO_PORTS_8(adrCAN + CANTIER) = 0X00; /* disallow interruptions on transmit */ |
|
449 |
return 0; |
|
450 |
} |
|
451 |
/***************************************************************************/ |
|
0 | 452 |
char canSleepMode(UNS16 adrCAN) |
453 |
{ |
|
454 |
IO_PORTS_8(adrCAN + CANCTL0) &= 0xFB; /* clr the bit WUPE to avoid a wake-up*/ |
|
455 |
IO_PORTS_8(adrCAN + CANCTL0) |= 0x02; /* Set the bit SLPRQ. go to Sleep !*/ |
|
456 |
||
457 |
// IO_PORTS_8(adrCAN + CANCTL1) |= 0x04; |
|
458 |
// IO_PORTS_8(adrCAN + CANCTL0) |= 0x02; /* Set the bit SLPRQ */ |
|
459 |
while ( ! canTestSleepMode(adrCAN)) { |
|
460 |
} |
|
461 |
||
462 |
return 0; |
|
463 |
} |
|
464 |
||
465 |
/***************************************************************************/ |
|
466 |
char canSleepModeQ(UNS16 adrCAN) |
|
467 |
{ |
|
468 |
if (canTestInitMode(adrCAN)) { |
|
469 |
/* Error because in init mode */ |
|
470 |
MSG_WAR(0X2606, "not in init mode ", 0); |
|
471 |
return 1; |
|
472 |
} |
|
473 |
IO_PORTS_8(adrCAN + CANCTL0) &= 0xFD; /* clr the bit SLPRQ */ |
|
474 |
while ( canTestSleepMode(adrCAN)) { |
|
475 |
} |
|
476 |
return 0; |
|
477 |
} |
|
478 |
||
479 |
/***************************************************************************/ |
|
480 |
char canSleepWupMode(UNS16 adrCAN) |
|
481 |
{ |
|
482 |
if (canTestInitMode(adrCAN)) { |
|
483 |
MSG_WAR(0X2607, "not in init mode ", 0); |
|
484 |
return 1; |
|
485 |
} |
|
486 |
IO_PORTS_8(adrCAN + CANCTL0) |= 0x06; /* Set the bits WUPE & SLPRQ */ |
|
487 |
while ( ! canTestSleepMode(adrCAN)) { |
|
488 |
} |
|
489 |
return 0; |
|
490 |
} |
|
491 |
||
492 |
/***************************************************************************/ |
|
493 |
char canTestInitMode(UNS16 adrCAN) |
|
494 |
{ |
|
495 |
return IO_PORTS_8(adrCAN + CANCTL1) & 0x01; /* Test the bit INITAK */ |
|
496 |
} |
|
497 |
||
498 |
/***************************************************************************/ |
|
499 |
char canTestSleepMode(UNS16 adrCAN) |
|
500 |
{ |
|
501 |
return IO_PORTS_8(adrCAN + CANCTL1) & 0x02; /* Test the bit SLPAK */ |
|
502 |
} |
|
503 |
||
504 |
/***************************************************************************/ |
|
505 |
UNS8 canSend(UNS8 notused, Message *m) |
|
506 |
{ |
|
507 |
canMsgTransmit(CANOPEN_LINE_NUMBER_USED, *m); |
|
508 |
return 0; |
|
509 |
} |
|
510 |
||
384 | 511 |
UNS8 canChangeBaudRate_driver( CAN_HANDLE fd, char* baud) |
512 |
{ |
|
513 |
return 0; |
|
514 |
} |
|
0 | 515 |
/**************************************************************************/ |
516 |
UNS8 f_can_receive(UNS8 notused, Message *msgRcv) |
|
517 |
{ |
|
518 |
UNS8 i, j; |
|
519 |
||
520 |
switch (CANOPEN_LINE_NUMBER_USED) { |
|
521 |
case CAN0 : j = 0; break; |
|
522 |
case CAN1 : j = 1; break; |
|
523 |
case CAN2 : j = 2; break; |
|
524 |
case CAN3 : j = 3; break; |
|
525 |
case CAN4 : j = 4; break; |
|
526 |
} |
|
527 |
||
528 |
/* See if a message is pending in the stack */ |
|
529 |
if (ptrMsgRcv[j].r == ptrMsgRcv[j].w) |
|
530 |
return 0x0; // No new message |
|
531 |
||
532 |
/* Increment the reading pointer of the stack */ |
|
533 |
if (ptrMsgRcv[j].r == (MAX_STACK_MSG_RCV - 1)) |
|
534 |
ptrMsgRcv[j].r = 0; |
|
535 |
else |
|
536 |
ptrMsgRcv[j].r ++; |
|
537 |
||
538 |
/* Store the message from the stack*/ |
|
365 | 539 |
msgRcv->cob_id = stackMsgRcv[j][ptrMsgRcv[j].r].cob_id; |
0 | 540 |
msgRcv->len = stackMsgRcv[j][ptrMsgRcv[j].r].len; |
541 |
msgRcv->rtr = stackMsgRcv[j][ptrMsgRcv[j].r].rtr; |
|
542 |
for (i = 0 ; i < stackMsgRcv[j][ptrMsgRcv[j].r].len ; i++) |
|
543 |
msgRcv->data[i] = stackMsgRcv[j][ptrMsgRcv[j].r].data[i]; |
|
544 |
return 0xFF; |
|
545 |
} |
|
546 |
||
547 |
||
548 |
/****************************************************************************** |
|
549 |
******************************* CAN INTERRUPT *******************************/ |
|
550 |
||
551 |
void __attribute__((interrupt)) can0HdlTra (void) |
|
552 |
{ |
|
553 |
||
554 |
} |
|
555 |
||
556 |
void __attribute__((interrupt)) can0HdlRcv (void) |
|
557 |
{ |
|
558 |
UNS8 i; |
|
559 |
lock(); |
|
560 |
IO_PORTS_8(PORTB) &= ~ 0x40; // led 6 port B : ON |
|
561 |
UNS8 NewPtrW; |
|
562 |
/* We are obliged to save the message while the interruption is pending */ |
|
563 |
/* Increment the writing stack pointer before writing the msg */ |
|
564 |
if (ptrMsgRcv[0].w == (MAX_STACK_MSG_RCV - 1)) |
|
565 |
NewPtrW = 0; |
|
566 |
else |
|
567 |
NewPtrW = ptrMsgRcv[0].w + 1; |
|
568 |
||
569 |
if (NewPtrW == ptrMsgRcv[0].r) { |
|
570 |
/* The stack is full. The last msg received before this one is lost */ |
|
571 |
MSG_WAR(0X1620, "Stack for received msg is full", 0); |
|
572 |
//IO_PORTS_8(PORTB) &= ~0x40; // led 6 : ON (for debogue) |
|
573 |
} |
|
574 |
else |
|
575 |
ptrMsgRcv[0].w = NewPtrW; |
|
576 |
||
577 |
/* Store the message */ |
|
365 | 578 |
stackMsgRcv[0][ptrMsgRcv[0].w].cob_id = IO_PORTS_16(CAN0 + CANRCVID) >> 5; |
0 | 579 |
stackMsgRcv[0][ptrMsgRcv[0].w].len = IO_PORTS_8(CAN0 + CANRCVLEN) & 0x0F; |
580 |
stackMsgRcv[0][ptrMsgRcv[0].w].rtr = (IO_PORTS_8(CAN0 + CANRCVID + 1) >> 4) & 0x01; |
|
581 |
for (i = 0 ; i < stackMsgRcv[0][ptrMsgRcv[0].w].len ; i++) |
|
582 |
stackMsgRcv[0][ptrMsgRcv[0].w].data[i] = IO_PORTS_8(CAN0 + CANRCVDTA + i); |
|
583 |
||
584 |
// The message is stored , so |
|
585 |
// we can now release the receive foreground buffer |
|
586 |
// and acknowledge the interruption |
|
587 |
IO_PORTS_8(CAN0 + CANRFLG) |= 0x01; |
|
588 |
// Not very usefull |
|
589 |
IO_PORTS_8(CAN0 + CANCTL0) |= 0x80; |
|
7 | 590 |
IO_PORTS_8(PORTB) |= 0x40; // led 6 port B : OFF |
0 | 591 |
unlock(); |
592 |
} |
|
593 |
||
594 |
void __attribute__((interrupt)) can0HdlWup (void) |
|
595 |
{ |
|
596 |
||
597 |
} |
|
598 |
||
599 |
void __attribute__((interrupt)) can0HdlErr (void) |
|
600 |
{ |
|
601 |
||
602 |
} |
|
603 |
||
604 |
void __attribute__((interrupt)) can1HdlTra (void) |
|
605 |
{ |
|
606 |
||
607 |
} |
|
608 |
||
609 |
void __attribute__((interrupt)) can1HdlRcv (void) |
|
610 |
{ |
|
611 |
UNS8 i; |
|
612 |
lock(); |
|
613 |
UNS8 NewPtrW; |
|
614 |
/* We are obliged to save the message while the interruption is pending */ |
|
615 |
/* Increment the writing stack pointer before writing the msg */ |
|
616 |
if (ptrMsgRcv[1].w == (MAX_STACK_MSG_RCV - 1)) |
|
617 |
NewPtrW = 0; |
|
618 |
else |
|
619 |
NewPtrW = ptrMsgRcv[1].w + 1; |
|
620 |
||
621 |
if (NewPtrW == ptrMsgRcv[1].r) { |
|
622 |
/* The stack is full. The last msg received before this one is lost */ |
|
623 |
MSG_WAR(0X2620, "Stack for received msg is full", 0); |
|
624 |
} |
|
625 |
else |
|
626 |
ptrMsgRcv[1].w = NewPtrW; |
|
627 |
||
628 |
/* Store the message */ |
|
365 | 629 |
stackMsgRcv[1][ptrMsgRcv[1].w].cob_id = IO_PORTS_16(CAN1 + CANRCVID) >> 5; |
0 | 630 |
stackMsgRcv[1][ptrMsgRcv[1].w].len = IO_PORTS_8(CAN1 + CANRCVLEN) & 0x0F; |
631 |
stackMsgRcv[0][ptrMsgRcv[0].w].rtr = (IO_PORTS_8(CAN1 + CANRCVID + 1) >> 4) & 0x01; |
|
632 |
for (i = 0 ; i < stackMsgRcv[1][ptrMsgRcv[1].w].len ; i++) |
|
633 |
stackMsgRcv[1][ptrMsgRcv[1].w].data[i] = IO_PORTS_8(CAN1 + CANRCVDTA + i); |
|
634 |
||
635 |
// The message is stored , so |
|
636 |
// we can now release the receive foreground buffer |
|
637 |
// and acknowledge the interruption |
|
638 |
IO_PORTS_8(CAN1 + CANRFLG) |= 0x01; |
|
639 |
// Not very usefull |
|
640 |
IO_PORTS_8(CAN1 + CANCTL0) |= 0x80; |
|
641 |
unlock(); |
|
642 |
} |
|
643 |
||
644 |
void __attribute__((interrupt)) can1HdlWup (void) |
|
645 |
{ |
|
646 |
||
647 |
} |
|
648 |
||
649 |
void __attribute__((interrupt)) can1HdlErr (void) |
|
650 |
{ |
|
651 |
||
652 |
} |
|
653 |
||
654 |
void __attribute__((interrupt)) can2HdlTra (void) |
|
655 |
{ |
|
656 |
||
657 |
} |
|
658 |
||
659 |
void __attribute__((interrupt)) can2HdlRcv (void) |
|
660 |
{ |
|
661 |
||
662 |
} |
|
663 |
||
664 |
void __attribute__((interrupt)) can2HdlWup (void) |
|
665 |
{ |
|
666 |
||
667 |
} |
|
668 |
||
669 |
void __attribute__((interrupt)) can2HdlErr (void) |
|
670 |
{ |
|
671 |
||
672 |
} |
|
673 |
||
674 |
void __attribute__((interrupt)) can3HdlTra (void) |
|
675 |
{ |
|
676 |
||
677 |
} |
|
678 |
||
679 |
void __attribute__((interrupt)) can3HdlRcv (void) |
|
680 |
{ |
|
681 |
||
682 |
} |
|
683 |
||
684 |
void __attribute__((interrupt)) can3HdlWup (void) |
|
685 |
{ |
|
686 |
||
687 |
} |
|
688 |
||
689 |
void __attribute__((interrupt)) can3HdlErr (void) |
|
690 |
{ |
|
691 |
||
692 |
} |
|
693 |
||
694 |
void __attribute__((interrupt)) can4HdlTra (void) |
|
695 |
{ |
|
696 |
||
697 |
} |
|
698 |
||
699 |
void __attribute__((interrupt)) can4HdlRcv (void) |
|
700 |
{ |
|
701 |
||
702 |
} |
|
703 |
||
704 |
void __attribute__((interrupt)) can4HdlWup (void) |
|
705 |
{ |
|
706 |
||
707 |
} |
|
708 |
||
709 |
void __attribute__((interrupt)) can4HdlErr (void) |
|
710 |
{ |
|
711 |
||
712 |
} |
|
713 |
||
714 |
||
715 |