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

Last change on this file since 926 was 926, checked in by tbretz, 23 years ago
*** empty log message ***
File size: 5.8 KB
Line 
1#include "canopen.h"
2
3#include <iostream.h> // cout
4#include <iomanip.h> // setw, setfill
5
6CanOpen::CanOpen(const char *dev, const int baud, MLog &out) : VmodIcan(dev, baud, out)
7{
8 for (int i=0; i<32; i++)
9 for (int j=0; j<4; j++)
10 {
11 pthread_cond_init(&fPdoCond[i][j], NULL);
12 pthread_mutex_init(&fPdoMux[i][j], NULL);
13 pthread_mutex_lock(&fPdoMux[i][j]);
14 }
15}
16
17CanOpen::~CanOpen()
18{
19 for (int i=0; i<32; i++)
20 for (int j=0; j<4; j++)
21 {
22 pthread_cond_destroy(&fPdoCond[i][j]);
23 pthread_mutex_destroy(&fPdoMux[i][j]);
24 }
25}
26
27void CanOpen::HandleCanMessage(WORD_t cobid, BYTE_t *data, struct timeval *tv)
28{
29 const WORD_t fcode = cobid >> 7;
30
31 switch (fcode)
32 {
33 case kNMT:
34 cout << "NMT: " << hex ;
35 cout << "CobId: 0x" << cobid << " ";
36 cout << "cmd=0x" << (int)data[0] << " ";
37 cout << "node=" << dec << (int)data[1] << endl;
38 return;
39
40 case kSYNC:
41 cout << "Sync" << endl;
42 return;
43
44 case kSDO_RX:
45 {
46 const BYTE_t node = cobid & 0x1f;
47 const BYTE_t cmd = data[0];
48 const LWORD_t dat = data[4] | (data[5]<<8) | (data[6]<<16) | (data[7]<<24);
49 const WORD_t idx = data[1] | (data[2]<<8);
50 const WORD_t subidx = data[3];
51
52 HandleSDO(node, cmd, idx, subidx, dat, tv);
53
54 fSdoList.Del(node, idx, subidx);
55 }
56 return;
57
58 case kPDO1_TX:
59 {
60 const BYTE_t node = cobid & 0x1f;
61 HandlePDO1(node, data, tv);
62 pthread_cond_broadcast(&fPdoCond[node-1][0]);
63 }
64 return;
65
66 case kPDO2_TX:
67 {
68 const BYTE_t node = cobid & 0x1f;
69 HandlePDO2(node, data, tv);
70 pthread_cond_broadcast(&fPdoCond[node-1][1]);
71 }
72 return;
73
74 case kPDO3_TX:
75 {
76 const BYTE_t node = cobid & 0x1f;
77 HandlePDO3(node, data, tv);
78 pthread_cond_broadcast(&fPdoCond[node-1][2]);
79 }
80 return;
81
82 case kPDO4_TX:
83 {
84 const BYTE_t node = cobid & 0x1f;
85 HandlePDO4(node, data, tv);
86 pthread_cond_broadcast(&fPdoCond[node-1][3]);
87 }
88 return;
89 }
90
91 const BYTE_t node = cobid & 0x1f;
92 cout << "Function Code: 0x" << hex << fcode << " Node: " << dec << (int)node << endl;
93}
94
95void CanOpen::EnableCanMsg(BYTE_t node, BYTE_t fcode, int flag)
96{
97 if (node>=0x20)
98 return;
99
100 EnableCobId(CobId(node, fcode), flag);
101}
102
103void CanOpen::EnableEmcy(BYTE_t node)
104{
105 EnableCanMsg(node, kEMERGENCY);
106}
107
108
109void CanOpen::EnableSdoRx(BYTE_t node)
110{
111 EnableCanMsg(node, kSDO_RX);
112}
113
114void CanOpen::EnablePdo1Rx(BYTE_t node)
115{
116 EnableCanMsg(node, kPDO1_TX);
117}
118
119void CanOpen::EnablePdo2Rx(BYTE_t node)
120{
121 EnableCanMsg(node, kPDO2_TX);
122}
123
124void CanOpen::EnablePdo3Rx(BYTE_t node)
125{
126 EnableCanMsg(node, kPDO1_TX);
127}
128
129void CanOpen::EnablePdo4Rx(BYTE_t node)
130{
131 EnableCanMsg(node, kPDO2_TX);
132}
133
134void CanOpen::SendPDO1(BYTE_t node, BYTE_t data[8])
135{
136 SendCanFrame(CobId(node, kPDO1_TX), data);
137}
138
139void CanOpen::SendPDO2(BYTE_t node, BYTE_t data[8])
140{
141 SendCanFrame(CobId(node, kPDO2_TX), data);
142}
143
144void CanOpen::SendPDO3(BYTE_t node, BYTE_t data[8])
145{
146 SendCanFrame(CobId(node, kPDO3_TX), data);
147}
148
149void CanOpen::SendPDO4(BYTE_t node, BYTE_t data[8])
150{
151 SendCanFrame(CobId(node, kPDO4_TX), data);
152}
153
154void CanOpen::SendPDO1(BYTE_t node,
155 BYTE_t m0=0, BYTE_t m1=0, BYTE_t m2=0, BYTE_t m3=0,
156 BYTE_t m4=0, BYTE_t m5=0, BYTE_t m6=0, BYTE_t m7=0)
157{
158 BYTE_t msg[8] = { m0, m1, m2, m3, m4, m5, m6, m7 };
159 SendCanFrame(CobId(node, kPDO2_TX), msg);
160}
161
162void CanOpen::SendPDO2(BYTE_t node,
163 BYTE_t m0=0, BYTE_t m1=0, BYTE_t m2=0, BYTE_t m3=0,
164 BYTE_t m4=0, BYTE_t m5=0, BYTE_t m6=0, BYTE_t m7=0)
165{
166 BYTE_t msg[8] = { m0, m1, m2, m3, m4, m5, m6, m7 };
167 SendCanFrame(CobId(node, kPDO2_TX), msg);
168}
169
170void CanOpen::SendPDO3(BYTE_t node,
171 BYTE_t m0=0, BYTE_t m1=0, BYTE_t m2=0, BYTE_t m3=0,
172 BYTE_t m4=0, BYTE_t m5=0, BYTE_t m6=0, BYTE_t m7=0)
173{
174 BYTE_t msg[8] = { m0, m1, m2, m3, m4, m5, m6, m7 };
175 SendCanFrame(CobId(node, kPDO3_TX), msg);
176}
177
178void CanOpen::SendPDO4(BYTE_t node,
179 BYTE_t m0=0, BYTE_t m1=0, BYTE_t m2=0, BYTE_t m3=0,
180 BYTE_t m4=0, BYTE_t m5=0, BYTE_t m6=0, BYTE_t m7=0)
181{
182 BYTE_t msg[8] = { m0, m1, m2, m3, m4, m5, m6, m7 };
183 SendCanFrame(CobId(node, kPDO4_TX), msg);
184}
185
186void CanOpen::SendSDO(BYTE_t node, WORD_t idx, BYTE_t subidx, BYTE_t val, bool store)
187{
188 if (store)
189 fSdoList.Add(node, idx, subidx);
190
191 SendCanFrame(CobId(node, kSDO_TX), kSDO_RX1,
192 word_to_lsb(idx), word_to_msb(idx), subidx, val);
193}
194
195void CanOpen::SendSDO(BYTE_t node, WORD_t idx, BYTE_t subidx, WORD_t val, bool store)
196{
197 if (store)
198 fSdoList.Add(node, idx, subidx);
199
200 SendCanFrame(CobId(node, kSDO_TX), kSDO_RX2,
201 word_to_lsb(idx), word_to_msb(idx), subidx,
202 word_to_lsb(val), word_to_msb(val));
203}
204
205void CanOpen::SendSDO(BYTE_t node, WORD_t idx, BYTE_t subidx, LWORD_t val, bool store)
206{
207 if (store)
208 fSdoList.Add(node, idx, subidx);
209
210 SendCanFrame(CobId(node, kSDO_TX), kSDO_RX4,
211 word_to_lsb(idx), word_to_msb(idx), subidx,
212 word_to_lsb(val&0xffff), word_to_msb(val&0xffff),
213 word_to_lsb(val>>16), word_to_msb(val>>16));
214}
215
216void CanOpen::RequestSDO(BYTE_t node, WORD_t idx, BYTE_t subidx)
217{
218 fSdoList.Add(node, idx, subidx);
219
220 SendCanFrame(CobId(node, kSDO_TX), kSDO_RX_DATA, word_to_lsb(idx), word_to_msb(idx), subidx);
221}
222
223void CanOpen::SendNMT(BYTE_t node, BYTE_t cmd)
224{
225 SendCanFrame(CobId(0, kNMT), cmd, node);
226}
227
228WORD_t CanOpen::CobId(BYTE_t node, BYTE_t fcode) const
229{
230 return (fcode<<7) | node&0x1f;
231}
Note: See TracBrowser for help on using the repository browser.