author | Florian Pose <fp@igh-essen.com> |
Thu, 24 Jul 2008 07:40:45 +0000 | |
changeset 1139 | 074caa25365e |
parent 1137 | a73c0f54be42 |
permissions | -rw-r--r-- |
1126
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
1 |
/***************************************************************************** |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
2 |
* |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
3 |
* $Id$ |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
4 |
* |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
5 |
****************************************************************************/ |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
6 |
|
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
7 |
#include <iostream> |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
8 |
#include <iomanip> |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
9 |
#include <fstream> |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
10 |
using namespace std; |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
11 |
|
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
12 |
#include "globals.h" |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
13 |
#include "sii_crc.h" |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
14 |
|
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
15 |
/****************************************************************************/ |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
16 |
|
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
17 |
const char *help_sii_write = |
1137
a73c0f54be42
Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents:
1136
diff
changeset
|
18 |
"[OPTIONS] <FILENAME>\n" |
1126
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
19 |
"\n" |
1137
a73c0f54be42
Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents:
1136
diff
changeset
|
20 |
"Writes SII contents from a local file to a slave.\n" |
1126
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
21 |
"\n" |
1137
a73c0f54be42
Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents:
1136
diff
changeset
|
22 |
"The file contents are checked for validity and integrity. These checks\n" |
a73c0f54be42
Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents:
1136
diff
changeset
|
23 |
"can be overridden with the --force option.\n" |
a73c0f54be42
Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents:
1136
diff
changeset
|
24 |
"\n" |
a73c0f54be42
Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents:
1136
diff
changeset
|
25 |
"Arguments:\n" |
a73c0f54be42
Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents:
1136
diff
changeset
|
26 |
" FILENAME must be a path to a file that contains a positive number\n" |
a73c0f54be42
Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents:
1136
diff
changeset
|
27 |
" of words.\n" |
a73c0f54be42
Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents:
1136
diff
changeset
|
28 |
"\n" |
a73c0f54be42
Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents:
1136
diff
changeset
|
29 |
"Command-specific options:\n" |
a73c0f54be42
Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents:
1136
diff
changeset
|
30 |
" --slave -s <index> Positive numerical ring position (mandatory).\n" |
a73c0f54be42
Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents:
1136
diff
changeset
|
31 |
" --force Override validity checks.\n" |
a73c0f54be42
Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents:
1136
diff
changeset
|
32 |
"\n" |
a73c0f54be42
Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents:
1136
diff
changeset
|
33 |
"Numerical values can be specified either with decimal (no prefix),\n" |
a73c0f54be42
Added command-line help for every ethercat command.
Florian Pose <fp@igh-essen.com>
parents:
1136
diff
changeset
|
34 |
"octal (prefix '0') or hexadecimal (prefix '0x') base.\n"; |
1126
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
35 |
|
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
36 |
/****************************************************************************/ |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
37 |
|
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
38 |
void command_sii_write(void) |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
39 |
{ |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
40 |
stringstream err; |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
41 |
ec_ioctl_slave_sii_t data; |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
42 |
ifstream file; |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
43 |
unsigned int byte_size; |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
44 |
const uint16_t *categoryHeader; |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
45 |
uint16_t categoryType, categorySize; |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
46 |
uint8_t crc; |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
47 |
|
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
48 |
if (slavePosition < 0) { |
1136
a0982873d655
Improved exceptions and commandName usage.
Florian Pose <fp@igh-essen.com>
parents:
1130
diff
changeset
|
49 |
err << "'" << commandName << "' requires a slave! " |
a0982873d655
Improved exceptions and commandName usage.
Florian Pose <fp@igh-essen.com>
parents:
1130
diff
changeset
|
50 |
<< "Please specify --slave."; |
a0982873d655
Improved exceptions and commandName usage.
Florian Pose <fp@igh-essen.com>
parents:
1130
diff
changeset
|
51 |
throw InvalidUsageException(err); |
1126
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
52 |
} |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
53 |
data.slave_position = slavePosition; |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
54 |
|
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
55 |
if (commandArgs.size() != 1) { |
1136
a0982873d655
Improved exceptions and commandName usage.
Florian Pose <fp@igh-essen.com>
parents:
1130
diff
changeset
|
56 |
err << "'" << commandName << "' takes exactly one argument!"; |
a0982873d655
Improved exceptions and commandName usage.
Florian Pose <fp@igh-essen.com>
parents:
1130
diff
changeset
|
57 |
throw InvalidUsageException(err); |
1126
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
58 |
} |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
59 |
|
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
60 |
file.open(commandArgs[0].c_str(), ifstream::in | ifstream::binary); |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
61 |
if (file.fail()) { |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
62 |
err << "Failed to open '" << commandArgs[0] << "'!"; |
1136
a0982873d655
Improved exceptions and commandName usage.
Florian Pose <fp@igh-essen.com>
parents:
1130
diff
changeset
|
63 |
throw CommandException(err); |
1126
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
64 |
} |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
65 |
|
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
66 |
// get length of file |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
67 |
file.seekg(0, ios::end); |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
68 |
byte_size = file.tellg(); |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
69 |
file.seekg(0, ios::beg); |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
70 |
|
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
71 |
if (!byte_size || byte_size % 2) { |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
72 |
err << "Invalid file size! Must be non-zero and even."; |
1136
a0982873d655
Improved exceptions and commandName usage.
Florian Pose <fp@igh-essen.com>
parents:
1130
diff
changeset
|
73 |
throw CommandException(err); |
1126
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
74 |
} |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
75 |
|
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
76 |
data.nwords = byte_size / 2; |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
77 |
if (data.nwords < 0x0041 && !force) { |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
78 |
err << "SII data too short (" << data.nwords << " words)! Mimimum is" |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
79 |
" 40 fixed words + 1 delimiter. Use --force to write anyway."; |
1136
a0982873d655
Improved exceptions and commandName usage.
Florian Pose <fp@igh-essen.com>
parents:
1130
diff
changeset
|
80 |
throw CommandException(err); |
1126
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
81 |
} |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
82 |
|
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
83 |
// allocate buffer and read file into buffer |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
84 |
data.words = new uint16_t[data.nwords]; |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
85 |
file.read((char *) data.words, byte_size); |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
86 |
file.close(); |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
87 |
|
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
88 |
if (!force) { |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
89 |
// calculate checksum over words 0 to 6 |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
90 |
crc = calcSiiCrc((const uint8_t *) data.words, 14); |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
91 |
if (crc != ((const uint8_t *) data.words)[14]) { |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
92 |
err << "CRC incorrect. Must be 0x" |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
93 |
<< hex << setfill('0') << setw(2) << (unsigned int) crc |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
94 |
<< ". Use --force to write anyway."; |
1136
a0982873d655
Improved exceptions and commandName usage.
Florian Pose <fp@igh-essen.com>
parents:
1130
diff
changeset
|
95 |
throw CommandException(err); |
1126
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
96 |
} |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
97 |
|
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
98 |
// cycle through categories to detect corruption |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
99 |
categoryHeader = data.words + 0x0040U; |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
100 |
categoryType = le16tocpu(*categoryHeader); |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
101 |
while (categoryType != 0xffff) { |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
102 |
if (categoryHeader + 1 > data.words + data.nwords) { |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
103 |
err << "SII data seem to be corrupted! " |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
104 |
<< "Use --force to write anyway."; |
1136
a0982873d655
Improved exceptions and commandName usage.
Florian Pose <fp@igh-essen.com>
parents:
1130
diff
changeset
|
105 |
throw CommandException(err); |
1126
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
106 |
} |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
107 |
categorySize = le16tocpu(*(categoryHeader + 1)); |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
108 |
if (categoryHeader + 2 + categorySize + 1 |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
109 |
> data.words + data.nwords) { |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
110 |
err << "SII data seem to be corrupted! " |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
111 |
"Use --force to write anyway."; |
1136
a0982873d655
Improved exceptions and commandName usage.
Florian Pose <fp@igh-essen.com>
parents:
1130
diff
changeset
|
112 |
throw CommandException(err); |
1126
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
113 |
} |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
114 |
categoryHeader += 2 + categorySize; |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
115 |
categoryType = le16tocpu(*categoryHeader); |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
116 |
} |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
117 |
} |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
118 |
|
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
119 |
// send data to master |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
120 |
masterDev.open(MasterDevice::ReadWrite); |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
121 |
data.offset = 0; |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
122 |
masterDev.writeSii(&data); |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
123 |
} |
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
124 |
|
b09fd81894cb
Moved every command into a separate file.
Florian Pose <fp@igh-essen.com>
parents:
diff
changeset
|
125 |
/*****************************************************************************/ |