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 |
|
|
24 |
#define DEBUG_WAR_CONSOLE_ON
|
|
25 |
#define DEBUG_ERR_CONSOLE_ON
|
|
26 |
|
|
27 |
#include <stddef.h> /* for NULL */
|
|
28 |
|
|
29 |
#include <asm-m68hc12/portsaccess.h>
|
|
30 |
#include <asm-m68hc12/ports_def.h>
|
|
31 |
#include <asm-m68hc12/ports.h>
|
|
32 |
#include <interrupt.h>
|
|
33 |
|
|
34 |
#include <applicfg.h>
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
#include <../include/hcs12/candriver.h>
|
|
39 |
|
|
40 |
#include "../include/def.h"
|
|
41 |
#include "../include/can.h"
|
|
42 |
#include "../include/objdictdef.h"
|
|
43 |
#include "../include/objacces.h"
|
|
44 |
#include "../include/sdo.h"
|
|
45 |
#include "../include/pdo.h"
|
|
46 |
#include "../include/timer.h"
|
|
47 |
#include "../include/lifegrd.h"
|
|
48 |
#include "../include/sync.h"
|
|
49 |
|
|
50 |
|
|
51 |
#include "../include/nmtSlave.h"
|
|
52 |
#include "objdict.h"
|
|
53 |
|
|
54 |
|
|
55 |
// HCS12 configuration
|
|
56 |
// -----------------------------------------------------
|
|
57 |
|
|
58 |
enum E_CanBaudrate
|
|
59 |
{
|
|
60 |
CAN_BAUDRATE_250K,
|
|
61 |
CAN_BAUDRATE_500K,
|
|
62 |
CAN_BAUDRATE_1M,
|
|
63 |
};
|
|
64 |
|
|
65 |
|
|
66 |
const canBusTime CAN_Baudrates[] =
|
|
67 |
{
|
|
68 |
{
|
|
69 |
1, /* clksrc: Use the bus clock : 16 MHz, the freq. of the quartz's board */
|
|
70 |
3, /* brp : chose btw 0 and 63 (6 bits). freq time quantum = 16MHz / (brp + 1) */
|
|
71 |
0, /* sjw : chose btw 0 and 3 (2 bits). Sync on (sjw + 1 ) time quantum */
|
|
72 |
0, /* samp : chose btw 0 and 3 (2 bits) (samp + 1 ) samples per bit */
|
|
73 |
1, /* tseg2 : chose btw 0 and 7 (3 bits) Segment 2 width = (tseg2 + 1) tq */
|
|
74 |
12, /* tseg1 : chose btw 0 and 15 (4 bits) Segment 1 width = (tseg1 + 1) tq */
|
|
75 |
|
|
76 |
/*
|
|
77 |
With these values,
|
|
78 |
- The width of the bit time is 16 time quantum :
|
|
79 |
- 1 tq for the SYNC segment (could not be modified)
|
|
80 |
- 13 tq for the TIME 1 segment (tseg1 = 12)
|
|
81 |
- 2 tq for the TIME 2 segment (tseg2 = 1)
|
|
82 |
- Because the bus clock of the MSCAN is 16 MHZ, and the
|
|
83 |
freq of the time quantum is 4 MHZ (brp = 3+1), and there are 16 tq in the bit time,
|
|
84 |
so the freq of the bit time is 250 kHz.
|
|
85 |
*/
|
|
86 |
},
|
|
87 |
|
|
88 |
{
|
|
89 |
1, /* clksrc: Use the bus clock : 16 MHz, the freq. of the quartz's board */
|
|
90 |
1, /* brp : chose btw 0 and 63 (6 bits). freq time quantum = 16MHz / (brp + 1) */
|
|
91 |
0, /* sjw : chose btw 0 and 3 (2 bits). Sync on (sjw + 1 ) time quantum */
|
|
92 |
0, /* samp : chose btw 0 and 3 (2 bits) (samp + 1 ) samples per bit */
|
|
93 |
1, /* tseg2 : chose btw 0 and 7 (3 bits) Segment 2 width = (tseg2 + 1) tq */
|
|
94 |
12, /* tseg1 : chose btw 0 and 15 (4 bits) Segment 1 width = (tseg1 + 1) tq */
|
|
95 |
|
|
96 |
/*
|
|
97 |
With these values,
|
|
98 |
- The width of the bit time is 16 time quantum :
|
|
99 |
- 1 tq for the SYNC segment (could not be modified)
|
|
100 |
- 13 tq for the TIME 1 segment (tseg1 = 12)
|
|
101 |
- 2 tq for the TIME 2 segment (tseg2 = 1)
|
|
102 |
- Because the bus clock of the MSCAN is 16 MHZ, and the
|
|
103 |
freq of the time quantum is 8 MHZ (brp = 1+1), and there are 16 tq in the bit time,
|
|
104 |
so the freq of the bit time is 500 kHz.
|
|
105 |
*/
|
|
106 |
},
|
|
107 |
|
|
108 |
{
|
|
109 |
1, /* clksrc: Use the bus clock : 16 MHz, the freq. of the quartz's board */
|
|
110 |
1, /* brp : chose btw 0 and 63 (6 bits). freq time quantum = 16MHz / (brp + 1) */
|
|
111 |
0, /* sjw : chose btw 0 and 3 (2 bits). Sync on (sjw + 1 ) time quantum */
|
|
112 |
0, /* samp : chose btw 0 and 3 (2 bits) (samp +MSG_WAR(0x3F33, "Je suis le noeud ", getNodeId()); 1 ) samples per bit */
|
|
113 |
1, /* tseg2 : chose btw 0 and 7 (3 bits) Segment 2 width = (tseg2 + 1) tq */
|
|
114 |
4, /* tseg1 : chose btw 0 and 15 (4 bits) Segment 1 width = (tseg1 + 1) tq */
|
|
115 |
|
|
116 |
/*
|
|
117 |
With these values,
|
|
118 |
- The width of the bit time is 16 time quantum :
|
|
119 |
- 1 tq for the SYNC segment (could not be modified)
|
|
120 |
- 5 tq for the TIME 1 segment (tseg1 = 4)
|
|
121 |
- 2 tq for the TIME 2 segment (tseg2 = 1)
|
|
122 |
- Because the bus clock of the MSCAN is 16 MHZ, and the
|
|
123 |
freq of the time quantum is 8 MHZ (brp = 1+1), and there are 8 tq in the bit time,
|
|
124 |
so the freq of the bit time is 1 MHz.
|
|
125 |
*/
|
|
126 |
}
|
|
127 |
};
|
|
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
|
132 |
/**************************prototypes*****************************************/
|
|
133 |
|
|
134 |
//fonction d'initialisation du bus can et la couche CANOPEN pour le capteur
|
|
135 |
void initCanopencapteur (void);
|
|
136 |
// les fonctions d'initialisation du capteur: timer, compteurs logiciel
|
|
137 |
void initSensor(void);
|
|
138 |
void initPortB(void);
|
|
139 |
|
|
140 |
//------------------------------------------------------------------------------
|
|
141 |
//Initialisation of the port B for the leds.
|
|
142 |
void initPortB(void)
|
|
143 |
{
|
|
144 |
// Port B is output
|
|
145 |
IO_PORTS_8(DDRB)= 0XFF;
|
|
146 |
// RAZ
|
|
147 |
IO_PORTS_8(PORTB) = 0xFF;
|
|
148 |
}
|
|
149 |
|
|
150 |
|
|
151 |
|
|
152 |
//------------------------------------------------------------------------------
|
|
153 |
void initSensor(void)
|
|
154 |
{
|
|
155 |
UNS8 baudrate = 0;
|
|
156 |
MSG_WAR(0x3F33, "I am the node : ", getNodeId(&gene_SYNC_Data));
|
|
157 |
// Init led control
|
|
158 |
initPortB();
|
|
159 |
IO_PORTS_8(PORTB) &= ~ 0x01; //One led ON
|
|
160 |
// Init port to choose se CAN baudrate by switch
|
|
161 |
IO_PORTS_8(ATD0DIEN) = 0x03;
|
|
162 |
|
|
163 |
canBusInit bi0 = {
|
|
164 |
0, /* no low power */
|
|
165 |
0, /* no time stamp */
|
|
166 |
1, /* enable MSCAN */
|
|
167 |
0, /* clock source : oscillator (In fact, it is not used) */
|
|
168 |
0, /* no loop back */
|
|
169 |
0, /* no listen only */
|
|
170 |
0, /* no low pass filter for wk up */
|
|
171 |
CAN_Baudrates[CAN_BAUDRATE_250K],
|
|
172 |
{
|
|
173 |
0x00, /* Filter on 16 bits. See Motorola Block Guide V02.14 fig 4-3 */
|
|
174 |
0x00, 0xFF, /* filter 0 hight accept all msg */
|
|
175 |
0x00, 0xFF, /* filter 0 low accept all msg */
|
|
176 |
0x00, 0xFF, /* filter 1 hight filter all of msg */
|
|
177 |
0x00, 0xFF, /* filter 1 low filter all of msg */
|
|
178 |
0x00, 0xFF, /* filter 2 hight filter most of msg */
|
|
179 |
0x00, 0xFF, /* filter 2 low filter most of msg */
|
|
180 |
0x00, 0xFF, /* filter 3 hight filter most of msg */
|
|
181 |
0x00, 0xFF, /* filter 3 low filter most of msg */
|
|
182 |
}
|
|
183 |
};
|
|
184 |
|
|
185 |
//Init the HCS12 microcontroler for CanOpen
|
|
186 |
initHCS12();
|
|
187 |
|
|
188 |
// Chose the CAN rate
|
|
189 |
baudrate = IO_PORTS_8(PORTAD0) & 0x03;
|
|
190 |
switch (baudrate) {
|
|
191 |
case 1:
|
|
192 |
bi0.clk = CAN_Baudrates[CAN_BAUDRATE_250K];
|
|
193 |
MSG_WAR(0x3F30, "CAN 250 kbps ", 0);
|
|
194 |
break;
|
|
195 |
case 2:
|
|
196 |
bi0.clk = CAN_Baudrates[CAN_BAUDRATE_500K];
|
|
197 |
MSG_WAR(0x3F31, "CAN 500 kbps ", 0);
|
|
198 |
break;
|
|
199 |
case 3:
|
|
200 |
bi0.clk = CAN_Baudrates[CAN_BAUDRATE_1M];
|
|
201 |
MSG_WAR(0x3F31, "CAN 1000 kbps ", 0);
|
|
202 |
break;
|
|
203 |
default:
|
|
204 |
MSG_WAR(0x2F32, "CAN BAUD RATE NOT DEFINED ", 0);
|
|
205 |
}
|
|
206 |
|
|
207 |
MSG_WAR(0x3F33, "SYNC signal generator", 0);
|
|
208 |
|
|
209 |
canInit(CANOPEN_LINE_NUMBER_USED, bi0); //initialize filters...
|
|
210 |
initTimer(); // Init hcs12 timer used by CanFestival. (see timerhw.c)
|
|
211 |
unlock(); // Allow interruptions
|
|
212 |
}
|
|
213 |
|
|
214 |
|
|
215 |
|
|
216 |
|
|
217 |
|
|
218 |
|
|
219 |
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
|
|
220 |
// FUNCTIONS WHICH ARE PART OF CANFESTIVAL and *must* be implemented here.
|
|
221 |
|
|
222 |
//------------------------------------------------------------------------------
|
|
223 |
void gene_SYNC_heartbeatError( UNS8 heartbeatID )
|
|
224 |
{
|
|
225 |
|
|
226 |
MSG_ERR(0x1F00, "HeartBeat not received from node : ", heartbeatID);
|
|
227 |
}
|
|
228 |
|
|
229 |
//------------------------------------------------------------------------------
|
|
230 |
void gene_SYNC_SDOtimeoutError (UNS8 line)
|
|
231 |
{
|
|
232 |
MSG_ERR(0x1F01, "SDO timeout for line : ", line);
|
|
233 |
}
|
|
234 |
|
|
235 |
//------------------------------------------------------------------------------
|
|
236 |
UNS8 gene_SYNC_canSend(Message *m)
|
|
237 |
{
|
|
238 |
// HCS12 driver function to send the CAN msg
|
|
239 |
canMsgTransmit(CAN0, *m);
|
|
240 |
return 0;
|
|
241 |
}
|
|
242 |
|
|
243 |
//------------------------------------------------------------------------------
|
|
244 |
void gene_SYNC_initialisation()
|
|
245 |
{
|
|
246 |
MSG_WAR (0x3F00, "Entering in INIT ", 0);
|
|
247 |
initSensor();
|
|
248 |
IO_PORTS_8(PORTB) &= ~ 0x01; // led 0 : ON
|
|
249 |
IO_PORTS_8(PORTB) |= 0x0E; // leds 1, 2, 3 : OFF
|
|
250 |
}
|
|
251 |
|
|
252 |
|
|
253 |
//------------------------------------------------------------------------------
|
|
254 |
void gene_SYNC_preOperational()
|
|
255 |
{
|
|
256 |
MSG_WAR (0x3F01, "Entering in PRE-OPERATIONAL ", 0);
|
|
257 |
IO_PORTS_8(PORTB) &= ~ 0x03; // leds 0, 1 : ON
|
|
258 |
IO_PORTS_8(PORTB) |= 0x0C; // leds 2, 3 : OFF
|
|
259 |
}
|
|
260 |
|
|
261 |
|
|
262 |
//------------------------------------------------------------------------------
|
|
263 |
void gene_SYNC_operational()
|
|
264 |
{
|
|
265 |
MSG_WAR (0x3F02, "Entering in OPERATIONAL ", 0);
|
|
266 |
IO_PORTS_8(PORTB) &= ~ 0x07; // leds 0, 1, 2 : ON
|
|
267 |
IO_PORTS_8(PORTB) |= 0x08; // leds 3 : OFF
|
|
268 |
}
|
|
269 |
|
|
270 |
//------------------------------------------------------------------------------
|
|
271 |
void gene_SYNC_stopped()
|
|
272 |
{
|
|
273 |
MSG_WAR (0x3F02, "Entering in STOPPED ", 0);
|
|
274 |
}
|
|
275 |
|
|
276 |
//------------------------------------------------------------------------------
|
|
277 |
void gene_SYNC_post_sync()
|
|
278 |
{
|
|
279 |
}
|
|
280 |
|
|
281 |
//------------------------------------------------------------------------------
|
|
282 |
void gene_SYNC_post_TPDO()
|
|
283 |
{
|
|
284 |
}
|
|
285 |
|
|
286 |
// End functions which are part of Canfestival
|
|
287 |
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
|
|
288 |
|
|
289 |
|
|
290 |
/******************************************************************************/
|
|
291 |
/********************************* MAIN ***************************************/
|
|
292 |
/******************************************************************************/
|
|
293 |
|
|
294 |
|
|
295 |
UNS8 main (void)
|
|
296 |
{
|
|
297 |
|
|
298 |
MSG_WAR(0x3F34, "Entering in the main", 0);
|
|
299 |
//----------------------------- INITIALISATION --------------------------------
|
|
300 |
/* Defining the node Id */
|
|
301 |
setNodeId(&gene_SYNC_Data, 0x03);
|
|
302 |
|
|
303 |
/* Put the node in Initialisation mode */
|
|
304 |
MSG_WAR(0x3F35, "va passer en init", 0);
|
|
305 |
setState(&gene_SYNC_Data, Initialisation);
|
|
306 |
|
|
307 |
//----------------------------- START -----------------------------------------
|
|
308 |
/* Put the node in pre-operational mode */
|
|
309 |
//MSG_WAR(0x3F36, "va passer en pre-op", 0);
|
|
310 |
//setState(&gene_SYNC_Data, Pre_operational);
|
|
311 |
|
|
312 |
while (1) {
|
|
313 |
{
|
|
314 |
Message m;
|
|
315 |
if (f_can_receive(0, &m)) {
|
|
316 |
MSG_WAR(0x3F36, "Msg received", m.cob_id.w);
|
|
317 |
canDispatch(&gene_SYNC_Data, &m);
|
|
318 |
}
|
|
319 |
|
|
320 |
|
|
321 |
}
|
|
322 |
|
|
323 |
}
|
|
324 |
|
|
325 |
return (0);
|
|
326 |
}
|
|
327 |
|
|
328 |
|
|
329 |
|