source: trunk/MagicSoft/Cosy/candrv/canopen.cc@ 1275

Last change on this file since 1275 was 1275, checked in by tbretz, 22 years ago
*** empty log message ***
File size: 11.6 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of Stesy, the MAGIC Steering System
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz <mailto:tbretz@uni-sw.gwdg.de>, 2001
19!
20! Copyright: MAGIC Software Development, 2000-2001
21!
22!
23\* ======================================================================== */
24
25///////////////////////////////////////////////////////////////////////
26//
27// CanOpen
28//
29// implements the canopen layer over the raw device driver
30//
31///////////////////////////////////////////////////////////////////////
32#include "canopen.h"
33
34#include <iostream.h> // cout
35#include <iomanip.h> // setw, setfill
36
37ClassImp(CanOpen);
38
39// --------------------------------------------------------------------------
40//
41// Initializes a conditional and a mutex semaphore for all possible
42// PDO combinations
43//
44CanOpen::CanOpen(const char *dev, const int baud, MLog &out) : VmodIcan(dev, baud, out)
45{
46 for (int i=0; i<32; i++)
47 for (int j=0; j<4; j++)
48 {
49 pthread_cond_init(&fPdoCond[i][j], NULL);
50 pthread_mutex_init(&fPdoMux[i][j], NULL);
51 pthread_mutex_lock(&fPdoMux[i][j]);
52 }
53
54 lout << "- CanOpen initialized." << endl;
55}
56
57// --------------------------------------------------------------------------
58//
59// Destroys all conditional and mutex semaphores
60//
61CanOpen::~CanOpen()
62{
63 for (int i=0; i<32; i++)
64 for (int j=0; j<4; j++)
65 {
66 pthread_cond_destroy(&fPdoCond[i][j]);
67 pthread_mutex_destroy(&fPdoMux[i][j]);
68 }
69 lout << "- CanOpen destroyed." << endl;
70}
71
72// --------------------------------------------------------------------------
73//
74// This overloads VmodIcan::HandleCanMessage. It is called if a can
75// message was received with all message relevant data (COBId, data, time)
76// It distributes the data depending on its function code to several
77// functions (to be obverloaded)
78// In case of a PDO the conditional semaphore corresponding to this PDO
79// is raised (WaitForNextPDO)
80// HandleSDO: handles a SDO message
81// HandlePDO1/2/3/4:handles received PDOs
82//
83void CanOpen::HandleCanMessage(WORD_t cobid, BYTE_t *data, timeval_t *tv)
84{
85 const WORD_t fcode = cobid >> 7;
86
87 switch (fcode)
88 {
89 case kNMT:
90 cout << "NMT: " << hex ;
91 cout << "CobId: 0x" << cobid << " ";
92 cout << "cmd=0x" << (int)data[0] << " ";
93 cout << "node=" << dec << (int)data[1] << endl;
94 return;
95
96 case kSYNC:
97 cout << "Sync" << endl;
98 return;
99
100 case kSDO_RX:
101 {
102 const BYTE_t node = cobid & 0x1f;
103 const BYTE_t cmd = data[0];
104 const LWORD_t dat = data[4] | (data[5]<<8) | (data[6]<<16) | (data[7]<<24);
105 const WORD_t idx = data[1] | (data[2]<<8);
106 const WORD_t subidx = data[3];
107
108 HandleSDO(node, cmd, idx, subidx, dat, tv);
109
110 fSdoList.Del(node, idx, subidx);
111 }
112 return;
113
114 case kPDO1_TX:
115 {
116 const BYTE_t node = cobid & 0x1f;
117 HandlePDO1(node, data, tv);
118 pthread_cond_broadcast(&fPdoCond[node-1][0]);
119 }
120 return;
121
122 case kPDO2_TX:
123 {
124 const BYTE_t node = cobid & 0x1f;
125 HandlePDO2(node, data, tv);
126 pthread_cond_broadcast(&fPdoCond[node-1][1]);
127 }
128 return;
129
130 case kPDO3_TX:
131 {
132 const BYTE_t node = cobid & 0x1f;
133 HandlePDO3(node, data, tv);
134 pthread_cond_broadcast(&fPdoCond[node-1][2]);
135 }
136 return;
137
138 case kPDO4_TX:
139 {
140 const BYTE_t node = cobid & 0x1f;
141 HandlePDO4(node, data, tv);
142 pthread_cond_broadcast(&fPdoCond[node-1][3]);
143 }
144 return;
145 }
146
147 const BYTE_t node = cobid & 0x1f;
148 cout << "Function Code: 0x" << hex << fcode << " Node: " << dec << (int)node << endl;
149}
150
151// --------------------------------------------------------------------------
152//
153// Enables can messaged for a given node ID and function code.
154//
155void CanOpen::EnableCanMsg(BYTE_t node, BYTE_t fcode, int flag)
156{
157 if (node>=0x20)
158 return;
159
160 EnableCobId(CobId(node, fcode), flag);
161}
162
163// --------------------------------------------------------------------------
164//
165// Enables Emergency messages for a given node
166//
167void CanOpen::EnableEmcy(BYTE_t node)
168{
169 EnableCanMsg(node, kEMERGENCY);
170}
171
172
173// --------------------------------------------------------------------------
174//
175// Enables SDO rx messages for a given node
176//
177void CanOpen::EnableSdoRx(BYTE_t node)
178{
179 EnableCanMsg(node, kSDO_RX);
180}
181
182// --------------------------------------------------------------------------
183//
184// Enables PDO1 tx messages for a given node
185//
186void CanOpen::EnablePdo1Rx(BYTE_t node)
187{
188 EnableCanMsg(node, kPDO1_TX);
189}
190
191// --------------------------------------------------------------------------
192//
193// Enables PDO2 tx messages for a given node
194//
195void CanOpen::EnablePdo2Rx(BYTE_t node)
196{
197 EnableCanMsg(node, kPDO2_TX);
198}
199
200// --------------------------------------------------------------------------
201//
202// Enables PDO3 rx messages for a given node
203//
204void CanOpen::EnablePdo3Rx(BYTE_t node)
205{
206 EnableCanMsg(node, kPDO1_TX);
207}
208
209// --------------------------------------------------------------------------
210//
211// Enables PDO4 rx messages for a given node
212//
213void CanOpen::EnablePdo4Rx(BYTE_t node)
214{
215 EnableCanMsg(node, kPDO2_TX);
216}
217
218// --------------------------------------------------------------------------
219//
220// Sends a PDO1 message with the given data to the given node
221//
222void CanOpen::SendPDO1(BYTE_t node, BYTE_t data[8])
223{
224 SendCanFrame(CobId(node, kPDO1_TX), data);
225}
226
227// --------------------------------------------------------------------------
228//
229// Sends a PDO2 message with the given data to the given node
230//
231void CanOpen::SendPDO2(BYTE_t node, BYTE_t data[8])
232{
233 SendCanFrame(CobId(node, kPDO2_TX), data);
234}
235
236// --------------------------------------------------------------------------
237//
238// Sends a PDO3 message with the given data to the given node
239//
240void CanOpen::SendPDO3(BYTE_t node, BYTE_t data[8])
241{
242 SendCanFrame(CobId(node, kPDO3_TX), data);
243}
244
245// --------------------------------------------------------------------------
246//
247// Sends a PDO4 message with the given data to the given node
248//
249void CanOpen::SendPDO4(BYTE_t node, BYTE_t data[8])
250{
251 SendCanFrame(CobId(node, kPDO4_TX), data);
252}
253
254// --------------------------------------------------------------------------
255//
256// Sends a PDO1 message with the given data to the given node
257//
258void CanOpen::SendPDO1(BYTE_t node,
259 BYTE_t m0=0, BYTE_t m1=0, BYTE_t m2=0, BYTE_t m3=0,
260 BYTE_t m4=0, BYTE_t m5=0, BYTE_t m6=0, BYTE_t m7=0)
261{
262 BYTE_t msg[8] = { m0, m1, m2, m3, m4, m5, m6, m7 };
263 SendCanFrame(CobId(node, kPDO2_TX), msg);
264}
265
266// --------------------------------------------------------------------------
267//
268// Sends a PDO2 message with the given data to the given node
269//
270void CanOpen::SendPDO2(BYTE_t node,
271 BYTE_t m0=0, BYTE_t m1=0, BYTE_t m2=0, BYTE_t m3=0,
272 BYTE_t m4=0, BYTE_t m5=0, BYTE_t m6=0, BYTE_t m7=0)
273{
274 BYTE_t msg[8] = { m0, m1, m2, m3, m4, m5, m6, m7 };
275 SendCanFrame(CobId(node, kPDO2_TX), msg);
276}
277
278// --------------------------------------------------------------------------
279//
280// Sends a PDO3 message with the given data to the given node
281//
282void CanOpen::SendPDO3(BYTE_t node,
283 BYTE_t m0=0, BYTE_t m1=0, BYTE_t m2=0, BYTE_t m3=0,
284 BYTE_t m4=0, BYTE_t m5=0, BYTE_t m6=0, BYTE_t m7=0)
285{
286 BYTE_t msg[8] = { m0, m1, m2, m3, m4, m5, m6, m7 };
287 SendCanFrame(CobId(node, kPDO3_TX), msg);
288}
289
290// --------------------------------------------------------------------------
291//
292// Sends a PDO4 message with the given data to the given node
293//
294void CanOpen::SendPDO4(BYTE_t node,
295 BYTE_t m0=0, BYTE_t m1=0, BYTE_t m2=0, BYTE_t m3=0,
296 BYTE_t m4=0, BYTE_t m5=0, BYTE_t m6=0, BYTE_t m7=0)
297{
298 BYTE_t msg[8] = { m0, m1, m2, m3, m4, m5, m6, m7 };
299 SendCanFrame(CobId(node, kPDO4_TX), msg);
300}
301
302// --------------------------------------------------------------------------
303//
304// Sends a SDO message with the given data to the given node:
305// - index describing the dictionary index to set
306// - subindex describing the dictionary subindex of theindex to set
307// - val describing the value to set.
308// - store describes whether the sdo should be stored in a list to
309// be able to wait for an answer
310//
311void CanOpen::SendSDO(BYTE_t node, WORD_t idx, BYTE_t subidx, BYTE_t val, bool store)
312{
313 if (store)
314 fSdoList.Add(node, idx, subidx);
315
316 SendCanFrame(CobId(node, kSDO_TX), kSDO_RX1,
317 word_to_lsb(idx), word_to_msb(idx), subidx, val);
318}
319
320// --------------------------------------------------------------------------
321//
322// Sends a SDO message with the given data to the given node:
323// - index describing the dictionary index to set
324// - subindex describing the dictionary subindex of theindex to set
325// - val describing the value to set.
326// - store describes whether the sdo should be stored in a list to
327// be able to wait for an answer
328//
329void CanOpen::SendSDO(BYTE_t node, WORD_t idx, BYTE_t subidx, WORD_t val, bool store)
330{
331 if (store)
332 fSdoList.Add(node, idx, subidx);
333
334 SendCanFrame(CobId(node, kSDO_TX), kSDO_RX2,
335 word_to_lsb(idx), word_to_msb(idx), subidx,
336 word_to_lsb(val), word_to_msb(val));
337}
338
339// --------------------------------------------------------------------------
340//
341// Sends a SDO message with the given data to the given node:
342// - index describing the dictionary index to set
343// - subindex describing the dictionary subindex of theindex to set
344// - val describing the value to set.
345// - store describes whether the sdo should be stored in a list to
346// be able to wait for an answer
347//
348void CanOpen::SendSDO(BYTE_t node, WORD_t idx, BYTE_t subidx, LWORD_t val, bool store)
349{
350 if (store)
351 fSdoList.Add(node, idx, subidx);
352
353 SendCanFrame(CobId(node, kSDO_TX), kSDO_RX4,
354 word_to_lsb(idx), word_to_msb(idx), subidx,
355 word_to_lsb(val&0xffff), word_to_msb(val&0xffff),
356 word_to_lsb(val>>16), word_to_msb(val>>16));
357}
358
359// --------------------------------------------------------------------------
360//
361// Request a SDO message from the given node:
362// - index describing the dictionary index to request
363// - subindex describing the dictionary subindex of the index to request
364//
365void CanOpen::RequestSDO(BYTE_t node, WORD_t idx, BYTE_t subidx)
366{
367 fSdoList.Add(node, idx, subidx);
368
369 SendCanFrame(CobId(node, kSDO_TX), kSDO_RX_DATA, word_to_lsb(idx), word_to_msb(idx), subidx);
370}
371
372// --------------------------------------------------------------------------
373//
374// Send an NMT Message to the given node with command cmd
375//
376void CanOpen::SendNMT(BYTE_t node, BYTE_t cmd)
377{
378 SendCanFrame(CobId(0, kNMT), cmd, node);
379}
380
381// --------------------------------------------------------------------------
382//
383// Decodes node and function code into a CobId
384//
385WORD_t CanOpen::CobId(BYTE_t node, BYTE_t fcode) const
386{
387 return (fcode<<7) | node&0x1f;
388}
Note: See TracBrowser for help on using the repository browser.