| 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@astro.uni-wuerzburg.de>, 2003
|
|---|
| 19 | !
|
|---|
| 20 | ! Copyright: MAGIC Software Development, 2000-2003
|
|---|
| 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 |
|
|---|
| 37 | ClassImp(CanOpen);
|
|---|
| 38 |
|
|---|
| 39 | // --------------------------------------------------------------------------
|
|---|
| 40 | //
|
|---|
| 41 | // Initializes a conditional and a mutex semaphore for all possible
|
|---|
| 42 | // PDO combinations
|
|---|
| 43 | //
|
|---|
| 44 | CanOpen::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 | //
|
|---|
| 61 | CanOpen::~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 | //
|
|---|
| 83 | void CanOpen::HandleCanMessage(WORD_t cobid, BYTE_t *data, timeval_t *tv)
|
|---|
| 84 | {
|
|---|
| 85 | const WORD_t fcode = cobid >> 7;
|
|---|
| 86 | const BYTE_t node = cobid & 0x1f;
|
|---|
| 87 |
|
|---|
| 88 | switch (fcode)
|
|---|
| 89 | {
|
|---|
| 90 | case kNMT:
|
|---|
| 91 | cout << "NMT: " << hex ;
|
|---|
| 92 | cout << "CobId: 0x" << cobid << " ";
|
|---|
| 93 | cout << "cmd=0x" << (int)data[0] << " ";
|
|---|
| 94 | cout << "node=" << dec << (int)data[1] << endl;
|
|---|
| 95 | return;
|
|---|
| 96 |
|
|---|
| 97 | case kEMERGENCY: // also case kSYNC:
|
|---|
| 98 | if (cobid==0)
|
|---|
| 99 | cout << "Sync" << endl;
|
|---|
| 100 | else
|
|---|
| 101 | {
|
|---|
| 102 | cout << "EMERGENCY Node #" << dec << (int)data[1] << endl;
|
|---|
| 103 | HandleEmergency(node, tv);
|
|---|
| 104 | }
|
|---|
| 105 | return;
|
|---|
| 106 |
|
|---|
| 107 | case kNodeguard:
|
|---|
| 108 | //cout << "Nodeguard Node #" << dec << (int)node << endl;
|
|---|
| 109 | HandleNodeguard(node, tv);
|
|---|
| 110 | return;
|
|---|
| 111 |
|
|---|
| 112 | case kSDO_RX:
|
|---|
| 113 | {
|
|---|
| 114 | const BYTE_t cmd = data[0];
|
|---|
| 115 | const LWORD_t dat = data[4] | (data[5]<<8) | (data[6]<<16) | (data[7]<<24);
|
|---|
| 116 | const WORD_t idx = data[1] | (data[2]<<8);
|
|---|
| 117 | const WORD_t subidx = data[3];
|
|---|
| 118 |
|
|---|
| 119 | HandleSDO(node, cmd, idx, subidx, dat, tv);
|
|---|
| 120 |
|
|---|
| 121 | fSdoList.Del(node, idx, subidx);
|
|---|
| 122 | }
|
|---|
| 123 | return;
|
|---|
| 124 |
|
|---|
| 125 | case kPDO1_TX:
|
|---|
| 126 | {
|
|---|
| 127 | HandlePDO1(node, data, tv);
|
|---|
| 128 | pthread_cond_broadcast(&fPdoCond[node-1][0]);
|
|---|
| 129 | }
|
|---|
| 130 | return;
|
|---|
| 131 |
|
|---|
| 132 | case kPDO2_TX:
|
|---|
| 133 | {
|
|---|
| 134 | HandlePDO2(node, data, tv);
|
|---|
| 135 | pthread_cond_broadcast(&fPdoCond[node-1][1]);
|
|---|
| 136 | }
|
|---|
| 137 | return;
|
|---|
| 138 |
|
|---|
| 139 | case kPDO3_TX:
|
|---|
| 140 | {
|
|---|
| 141 | HandlePDO3(node, data, tv);
|
|---|
| 142 | pthread_cond_broadcast(&fPdoCond[node-1][2]);
|
|---|
| 143 | }
|
|---|
| 144 | return;
|
|---|
| 145 |
|
|---|
| 146 | case kPDO4_TX:
|
|---|
| 147 | {
|
|---|
| 148 | HandlePDO4(node, data, tv);
|
|---|
| 149 | pthread_cond_broadcast(&fPdoCond[node-1][3]);
|
|---|
| 150 | }
|
|---|
| 151 | return;
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | cout << "CanOpen::HandleCanMessage - Unhandled Message: Function Code=0x" << hex << fcode << " Node #" << dec << (int)node << endl;
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | // --------------------------------------------------------------------------
|
|---|
| 158 | //
|
|---|
| 159 | // Enables can messaged for a given node ID and function code.
|
|---|
| 160 | //
|
|---|
| 161 | void CanOpen::EnableCanMsg(BYTE_t node, BYTE_t fcode, int flag)
|
|---|
| 162 | {
|
|---|
| 163 | if (node>=0x20)
|
|---|
| 164 | return;
|
|---|
| 165 |
|
|---|
| 166 | EnableCobId(CobId(node, fcode), flag);
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | // --------------------------------------------------------------------------
|
|---|
| 170 | //
|
|---|
| 171 | // Enables Emergency messages for a given node
|
|---|
| 172 | //
|
|---|
| 173 | void CanOpen::EnableEmcy(BYTE_t node)
|
|---|
| 174 | {
|
|---|
| 175 | EnableCanMsg(node, kEMERGENCY);
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | // --------------------------------------------------------------------------
|
|---|
| 179 | //
|
|---|
| 180 | // Enables Nodeguard messages for a given node
|
|---|
| 181 | //
|
|---|
| 182 | void CanOpen::EnableNodeguard(BYTE_t node)
|
|---|
| 183 | {
|
|---|
| 184 | EnableCanMsg(node, kNodeguard);
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 | // --------------------------------------------------------------------------
|
|---|
| 189 | //
|
|---|
| 190 | // Enables SDO rx messages for a given node
|
|---|
| 191 | //
|
|---|
| 192 | void CanOpen::EnableSdoRx(BYTE_t node)
|
|---|
| 193 | {
|
|---|
| 194 | EnableCanMsg(node, kSDO_RX);
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | // --------------------------------------------------------------------------
|
|---|
| 198 | //
|
|---|
| 199 | // Enables PDO1 tx messages for a given node
|
|---|
| 200 | //
|
|---|
| 201 | void CanOpen::EnablePdo1Rx(BYTE_t node)
|
|---|
| 202 | {
|
|---|
| 203 | EnableCanMsg(node, kPDO1_TX);
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | // --------------------------------------------------------------------------
|
|---|
| 207 | //
|
|---|
| 208 | // Enables PDO2 tx messages for a given node
|
|---|
| 209 | //
|
|---|
| 210 | void CanOpen::EnablePdo2Rx(BYTE_t node)
|
|---|
| 211 | {
|
|---|
| 212 | EnableCanMsg(node, kPDO2_TX);
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | // --------------------------------------------------------------------------
|
|---|
| 216 | //
|
|---|
| 217 | // Enables PDO3 rx messages for a given node
|
|---|
| 218 | //
|
|---|
| 219 | void CanOpen::EnablePdo3Rx(BYTE_t node)
|
|---|
| 220 | {
|
|---|
| 221 | EnableCanMsg(node, kPDO1_TX);
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | // --------------------------------------------------------------------------
|
|---|
| 225 | //
|
|---|
| 226 | // Enables PDO4 rx messages for a given node
|
|---|
| 227 | //
|
|---|
| 228 | void CanOpen::EnablePdo4Rx(BYTE_t node)
|
|---|
| 229 | {
|
|---|
| 230 | EnableCanMsg(node, kPDO2_TX);
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | // --------------------------------------------------------------------------
|
|---|
| 234 | //
|
|---|
| 235 | // Sends a PDO1 message with the given data to the given node
|
|---|
| 236 | //
|
|---|
| 237 | void CanOpen::SendPDO1(BYTE_t node, BYTE_t data[8])
|
|---|
| 238 | {
|
|---|
| 239 | SendCanFrame(CobId(node, kPDO1_TX), data);
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | // --------------------------------------------------------------------------
|
|---|
| 243 | //
|
|---|
| 244 | // Sends a PDO2 message with the given data to the given node
|
|---|
| 245 | //
|
|---|
| 246 | void CanOpen::SendPDO2(BYTE_t node, BYTE_t data[8])
|
|---|
| 247 | {
|
|---|
| 248 | SendCanFrame(CobId(node, kPDO2_TX), data);
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | // --------------------------------------------------------------------------
|
|---|
| 252 | //
|
|---|
| 253 | // Sends a PDO3 message with the given data to the given node
|
|---|
| 254 | //
|
|---|
| 255 | void CanOpen::SendPDO3(BYTE_t node, BYTE_t data[8])
|
|---|
| 256 | {
|
|---|
| 257 | SendCanFrame(CobId(node, kPDO3_TX), data);
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | // --------------------------------------------------------------------------
|
|---|
| 261 | //
|
|---|
| 262 | // Sends a PDO4 message with the given data to the given node
|
|---|
| 263 | //
|
|---|
| 264 | void CanOpen::SendPDO4(BYTE_t node, BYTE_t data[8])
|
|---|
| 265 | {
|
|---|
| 266 | SendCanFrame(CobId(node, kPDO4_TX), data);
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | // --------------------------------------------------------------------------
|
|---|
| 270 | //
|
|---|
| 271 | // Sends a PDO1 message with the given data to the given node
|
|---|
| 272 | //
|
|---|
| 273 | void CanOpen::SendPDO1(BYTE_t node,
|
|---|
| 274 | BYTE_t m0=0, BYTE_t m1=0, BYTE_t m2=0, BYTE_t m3=0,
|
|---|
| 275 | BYTE_t m4=0, BYTE_t m5=0, BYTE_t m6=0, BYTE_t m7=0)
|
|---|
| 276 | {
|
|---|
| 277 | BYTE_t msg[8] = { m0, m1, m2, m3, m4, m5, m6, m7 };
|
|---|
| 278 | SendCanFrame(CobId(node, kPDO2_TX), msg);
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 281 | // --------------------------------------------------------------------------
|
|---|
| 282 | //
|
|---|
| 283 | // Sends a PDO2 message with the given data to the given node
|
|---|
| 284 | //
|
|---|
| 285 | void CanOpen::SendPDO2(BYTE_t node,
|
|---|
| 286 | BYTE_t m0=0, BYTE_t m1=0, BYTE_t m2=0, BYTE_t m3=0,
|
|---|
| 287 | BYTE_t m4=0, BYTE_t m5=0, BYTE_t m6=0, BYTE_t m7=0)
|
|---|
| 288 | {
|
|---|
| 289 | BYTE_t msg[8] = { m0, m1, m2, m3, m4, m5, m6, m7 };
|
|---|
| 290 | SendCanFrame(CobId(node, kPDO2_TX), msg);
|
|---|
| 291 | }
|
|---|
| 292 |
|
|---|
| 293 | // --------------------------------------------------------------------------
|
|---|
| 294 | //
|
|---|
| 295 | // Sends a PDO3 message with the given data to the given node
|
|---|
| 296 | //
|
|---|
| 297 | void CanOpen::SendPDO3(BYTE_t node,
|
|---|
| 298 | BYTE_t m0=0, BYTE_t m1=0, BYTE_t m2=0, BYTE_t m3=0,
|
|---|
| 299 | BYTE_t m4=0, BYTE_t m5=0, BYTE_t m6=0, BYTE_t m7=0)
|
|---|
| 300 | {
|
|---|
| 301 | BYTE_t msg[8] = { m0, m1, m2, m3, m4, m5, m6, m7 };
|
|---|
| 302 | SendCanFrame(CobId(node, kPDO3_TX), msg);
|
|---|
| 303 | }
|
|---|
| 304 |
|
|---|
| 305 | // --------------------------------------------------------------------------
|
|---|
| 306 | //
|
|---|
| 307 | // Sends a PDO4 message with the given data to the given node
|
|---|
| 308 | //
|
|---|
| 309 | void CanOpen::SendPDO4(BYTE_t node,
|
|---|
| 310 | BYTE_t m0=0, BYTE_t m1=0, BYTE_t m2=0, BYTE_t m3=0,
|
|---|
| 311 | BYTE_t m4=0, BYTE_t m5=0, BYTE_t m6=0, BYTE_t m7=0)
|
|---|
| 312 | {
|
|---|
| 313 | BYTE_t msg[8] = { m0, m1, m2, m3, m4, m5, m6, m7 };
|
|---|
| 314 | SendCanFrame(CobId(node, kPDO4_TX), msg);
|
|---|
| 315 | }
|
|---|
| 316 |
|
|---|
| 317 | // --------------------------------------------------------------------------
|
|---|
| 318 | //
|
|---|
| 319 | // Sends a SDO message with the given data to the given node:
|
|---|
| 320 | // - index describing the dictionary index to set
|
|---|
| 321 | // - subindex describing the dictionary subindex of theindex to set
|
|---|
| 322 | // - val describing the value to set.
|
|---|
| 323 | // - store describes whether the sdo should be stored in a list to
|
|---|
| 324 | // be able to wait for an answer
|
|---|
| 325 | //
|
|---|
| 326 | void CanOpen::SendSDO(BYTE_t node, WORD_t idx, BYTE_t subidx, BYTE_t val, bool store)
|
|---|
| 327 | {
|
|---|
| 328 | if (store)
|
|---|
| 329 | fSdoList.Add(node, idx, subidx);
|
|---|
| 330 |
|
|---|
| 331 | SendCanFrame(CobId(node, kSDO_TX), kSDO_RX1,
|
|---|
| 332 | word_to_lsb(idx), word_to_msb(idx), subidx, val);
|
|---|
| 333 | }
|
|---|
| 334 |
|
|---|
| 335 | // --------------------------------------------------------------------------
|
|---|
| 336 | //
|
|---|
| 337 | // Sends a SDO message with the given data to the given node:
|
|---|
| 338 | // - index describing the dictionary index to set
|
|---|
| 339 | // - subindex describing the dictionary subindex of theindex to set
|
|---|
| 340 | // - val describing the value to set.
|
|---|
| 341 | // - store describes whether the sdo should be stored in a list to
|
|---|
| 342 | // be able to wait for an answer
|
|---|
| 343 | //
|
|---|
| 344 | void CanOpen::SendSDO(BYTE_t node, WORD_t idx, BYTE_t subidx, WORD_t val, bool store)
|
|---|
| 345 | {
|
|---|
| 346 | if (store)
|
|---|
| 347 | fSdoList.Add(node, idx, subidx);
|
|---|
| 348 |
|
|---|
| 349 | SendCanFrame(CobId(node, kSDO_TX), kSDO_RX2,
|
|---|
| 350 | word_to_lsb(idx), word_to_msb(idx), subidx,
|
|---|
| 351 | word_to_lsb(val), word_to_msb(val));
|
|---|
| 352 | }
|
|---|
| 353 |
|
|---|
| 354 | // --------------------------------------------------------------------------
|
|---|
| 355 | //
|
|---|
| 356 | // Sends a SDO message with the given data to the given node:
|
|---|
| 357 | // - index describing the dictionary index to set
|
|---|
| 358 | // - subindex describing the dictionary subindex of theindex to set
|
|---|
| 359 | // - val describing the value to set.
|
|---|
| 360 | // - store describes whether the sdo should be stored in a list to
|
|---|
| 361 | // be able to wait for an answer
|
|---|
| 362 | //
|
|---|
| 363 | void CanOpen::SendSDO(BYTE_t node, WORD_t idx, BYTE_t subidx, LWORD_t val, bool store)
|
|---|
| 364 | {
|
|---|
| 365 | if (store)
|
|---|
| 366 | fSdoList.Add(node, idx, subidx);
|
|---|
| 367 |
|
|---|
| 368 | SendCanFrame(CobId(node, kSDO_TX), kSDO_RX4,
|
|---|
| 369 | word_to_lsb(idx), word_to_msb(idx), subidx,
|
|---|
| 370 | word_to_lsb(val&0xffff), word_to_msb(val&0xffff),
|
|---|
| 371 | word_to_lsb(val>>16), word_to_msb(val>>16));
|
|---|
| 372 | }
|
|---|
| 373 |
|
|---|
| 374 | // --------------------------------------------------------------------------
|
|---|
| 375 | //
|
|---|
| 376 | // Request a SDO message from the given node:
|
|---|
| 377 | // - index describing the dictionary index to request
|
|---|
| 378 | // - subindex describing the dictionary subindex of the index to request
|
|---|
| 379 | //
|
|---|
| 380 | void CanOpen::RequestSDO(BYTE_t node, WORD_t idx, BYTE_t subidx)
|
|---|
| 381 | {
|
|---|
| 382 | fSdoList.Add(node, idx, subidx);
|
|---|
| 383 |
|
|---|
| 384 | SendCanFrame(CobId(node, kSDO_TX), kSDO_RX_DATA, word_to_lsb(idx), word_to_msb(idx), subidx);
|
|---|
| 385 | }
|
|---|
| 386 |
|
|---|
| 387 | // --------------------------------------------------------------------------
|
|---|
| 388 | //
|
|---|
| 389 | // Send a NMT Message to the given node with command cmd
|
|---|
| 390 | //
|
|---|
| 391 | void CanOpen::SendNMT(BYTE_t node, BYTE_t cmd)
|
|---|
| 392 | {
|
|---|
| 393 | SendCanFrame(CobId(0, kNMT), cmd, node);
|
|---|
| 394 | }
|
|---|
| 395 |
|
|---|
| 396 | // --------------------------------------------------------------------------
|
|---|
| 397 | //
|
|---|
| 398 | // Send a Nodeguard Message to the given node with command cmd
|
|---|
| 399 | //
|
|---|
| 400 | void CanOpen::SendNodeguard(BYTE_t node)
|
|---|
| 401 | {
|
|---|
| 402 | BYTE_t msg[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
|
|---|
| 403 | SendCanFrame(CobId(node, kNodeguard), msg, 1);
|
|---|
| 404 | }
|
|---|
| 405 |
|
|---|
| 406 | // --------------------------------------------------------------------------
|
|---|
| 407 | //
|
|---|
| 408 | // Decodes node and function code into a CobId
|
|---|
| 409 | //
|
|---|
| 410 | WORD_t CanOpen::CobId(BYTE_t node, BYTE_t fcode) const
|
|---|
| 411 | {
|
|---|
| 412 | return (fcode<<7) | node&0x1f;
|
|---|
| 413 | }
|
|---|