GCC Code Coverage Report


Directory: ./
File: src/devices/PhoenixIoLink.cpp
Date: 2026-11-24 14:33:39
Exec Total Coverage
Lines: 62 140 44.3%
Branches: 74 214 34.6%

Line Branch Exec Source
1 /*****************************************************************************
2 *
3 * This file is part of the ecapp library (EtherCAT application devices).
4 *
5 * Copyright (C) 2026 Florian Pose <fp@igh.de>
6 *
7 * The ecapp library is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * as published by the Free Software Foundation, version 3 of the
10 * License.
11 *
12 * The ecapp library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with the ecapp library. If not, see
19 * <https://www.gnu.org/licenses/>.
20 *
21 * IO-Link port drivers for Phoenix Contact devices (on an EL6224/
22 * ED6224 port).
23 *
24 ****************************************************************************/
25
26 #include "devices/PhoenixTrio3.h"
27 #include "ecapp/Domain.h"
28 #include "ecapp/Exceptions.h"
29 #include "ecapp/IoLinkDevice.h"
30
31 #include <cstring>
32 #include <sstream>
33 #include <stdexcept>
34 #include <string>
35 #include <vector>
36
37 /****************************************************************************/
38
39 using std::runtime_error;
40 using std::size_t;
41 using std::string;
42 using std::stringstream;
43
44 /****************************************************************************/
45
46 namespace EcApp {
47
48 /****************************************************************************/
49 // Phoenix Contact TRIO3-PS/1AC/24DC/20/8C/IOL (1252697): 24 V power supply
50 // with an integrated 8-channel electronic circuit breaker.
51 //
52 // Inputs (18 byte PDin): DC/power status, output voltage, total current,
53 // and per channel: state, rated current (INom) and load current.
54 //
55 // Outputs (7 byte PDout): the device ignores the PDout as long as
56 // "Valid" is false -- which is the default here. Once "Valid" is set,
57 // *every* output field is applied at once (channel on/off, INom codes,
58 // priority, reset behaviour), so set them all to sensible values before
59 // setting "Valid". Defaults: all channels on, INom code 0 (1 A), last
60 // state restored after reset, highest current switched off first.
61 /****************************************************************************/
62
63 2 class PhoenixTrio3 : public IoLinkDevice
64 {
65 public:
66 1 PhoenixTrio3(pdserv *pdServ, pdtask *task, const string &prefix) :
67
25/50
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
✓ Branch 13 taken 1 times.
✗ Branch 14 not taken.
✓ Branch 18 taken 1 times.
✗ Branch 19 not taken.
✓ Branch 23 taken 1 times.
✗ Branch 24 not taken.
✓ Branch 28 taken 1 times.
✗ Branch 29 not taken.
✓ Branch 33 taken 1 times.
✗ Branch 34 not taken.
✓ Branch 37 taken 1 times.
✗ Branch 38 not taken.
✓ Branch 41 taken 1 times.
✗ Branch 42 not taken.
✓ Branch 45 taken 1 times.
✗ Branch 46 not taken.
✓ Branch 49 taken 1 times.
✗ Branch 50 not taken.
✓ Branch 53 taken 1 times.
✗ Branch 54 not taken.
✓ Branch 57 taken 1 times.
✗ Branch 58 not taken.
✓ Branch 61 taken 1 times.
✗ Branch 62 not taken.
✓ Branch 65 taken 1 times.
✗ Branch 66 not taken.
✓ Branch 68 taken 8 times.
✓ Branch 69 taken 1 times.
✓ Branch 81 taken 1 times.
✗ Branch 82 not taken.
✓ Branch 85 taken 1 times.
✗ Branch 86 not taken.
✓ Branch 89 taken 1 times.
✗ Branch 90 not taken.
✓ Branch 93 taken 1 times.
✗ Branch 94 not taken.
✓ Branch 97 taken 1 times.
✗ Branch 98 not taken.
✓ Branch 101 taken 1 times.
✗ Branch 102 not taken.
✓ Branch 104 taken 5 times.
✓ Branch 105 taken 1 times.
✗ Branch 119 not taken.
✗ Branch 120 not taken.
✗ Branch 131 not taken.
✗ Branch 132 not taken.
1 IoLinkDevice(pdServ, task, prefix)
68 {
69
2/4
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
1 registerInputChannel("DcNotOk", dcNotOk, AsBoolean {});
70
2/4
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
1 registerInputChannel("PowerAbove90", powerAbove90, AsBoolean {});
71
2/4
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
1 registerInputChannel("OutputVoltage", outputVoltage);
72
2/4
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
1 registerInputChannel("TotalCurrent", totalCurrent);
73
2/4
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
1 registerInputChannel("ChannelState", channelState);
74
2/4
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
1 registerInputChannel("RatedCurrentCode", ratedCurrentCode);
75
2/4
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
1 registerInputChannel("RatedCurrent", ratedCurrent);
76
2/4
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
1 registerInputChannel("LoadCurrent", loadCurrent);
77
78
2/4
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
1 registerOutputChannel("Valid", valid, AsBoolean {});
79
2/4
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
1 registerOutputChannel(
80 "AllOffAfterReset", allOffAfterReset, AsBoolean {});
81
2/4
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
1 registerOutputChannel("SetRatedCurrentCode", setRatedCurrentCode);
82
2/4
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
1 registerOutputChannel(
83 "PriorityByChannel", priorityByChannel, AsBoolean {});
84
2/4
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
1 registerOutputChannel("ChannelOn", channelOn, AsBoolean {});
85 1 }
86
87 ✗ void addConfig(ec_slave_config_t *sc, uint16_t sdoIndex) override
88 {
89 // PHOENIX_CONTACT-TRIO3-PS_1AC_24DC_20_8C_IOL-20221219-IODD1.1.xml:
90 // DeviceIdentity vendorId="176" deviceId="262421", PhysicalLayer
91 // minCycleTime="40000" (us) sioSupported="false", iolinkRevision
92 // V1.1, Features dataStorage="true".
93 ✗ IoLinkPortIdentity identity;
94 ✗ identity.vendorId = 176;
95 ✗ identity.deviceId = 262421; // 0x040115
96 ✗ identity.minCycleTimeMs = 4.0;
97 ✗ identity.processDataInBits = Trio3::PdInBytes * 8;
98 ✗ identity.processDataOutBits = Trio3::PdOutBytes * 8;
99 ✗ identity.sioSupported = false;
100 ✗ configurePort(sc, sdoIndex, identity);
101 }
102
103 ✗ bool addOutputs(
104 ec_slave_config_t *sc,
105 uint16_t pdoIndex,
106 uint16_t pdoEntryIndex,
107 Domain *) override
108 {
109 ✗ mapBlock(sc, 2, pdoIndex, pdoEntryIndex, Trio3::PdOutBytes * 8);
110 ✗ outputPdoEntryIndex = pdoEntryIndex;
111 ✗ return true;
112 }
113
114 ✗ bool addInputs(
115 ec_slave_config_t *sc,
116 uint16_t pdoIndex,
117 uint16_t pdoEntryIndex,
118 Domain *) override
119 {
120 ✗ mapBlock(sc, 3, pdoIndex, pdoEntryIndex, Trio3::PdInBytes * 8);
121 ✗ inputPdoEntryIndex = pdoEntryIndex;
122 ✗ return true;
123 }
124
125 ✗ void registerOutputs(ec_slave_config_t *sc, Domain *domainOut) override
126 {
127 ✗ domainOut_ = domainOut;
128 ✗ offOut = ecrt_slave_config_reg_pdo_entry(
129 ✗ sc, outputPdoEntryIndex, 0x01, domainOut->getDomain(), NULL);
130 ✗ if (offOut < 0) {
131 throw runtime_error(
132 ✗ "Failed to register PhoenixTrio3 output PDO entry.");
133 }
134 }
135
136 ✗ void registerInputs(ec_slave_config_t *sc, Domain *domainIn) override
137 {
138 ✗ domainIn_ = domainIn;
139 ✗ offIn = ecrt_slave_config_reg_pdo_entry(
140 ✗ sc, inputPdoEntryIndex, 0x01, domainIn->getDomain(), NULL);
141 ✗ if (offIn < 0) {
142 throw runtime_error(
143 ✗ "Failed to register PhoenixTrio3 input PDO entry.");
144 }
145 }
146
147 1 void updateInputs() override
148 {
149
2/8
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
1 if (!domainIn_ || !domainIn_->getData()) {
150 1 return;
151 }
152 ✗ Trio3::Inputs in = Trio3::decodeInputs(domainIn_->getData() + offIn);
153 ✗ dcNotOk = in.dcNotOk;
154 ✗ powerAbove90 = in.powerAbove90;
155 ✗ outputVoltage = in.outputVoltage;
156 ✗ totalCurrent = in.totalCurrent;
157 ✗ for (unsigned int c = 0; c < Trio3::NumChannels; c++) {
158 ✗ channelState[c] = in.channelState[c];
159 ✗ ratedCurrentCode[c] = in.ratedCurrentCode[c];
160 ✗ ratedCurrent[c] = Trio3::RatedCurrentAmps[in.ratedCurrentCode[c]];
161 ✗ loadCurrent[c] = in.loadCurrent[c];
162 }
163 }
164
165 1 void updateOutputs() override
166 {
167
2/8
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
1 if (!domainOut_ || !domainOut_->getData()) {
168 1 return;
169 }
170 ✗ Trio3::Outputs out {};
171 ✗ out.valid = valid;
172 ✗ out.allOffAfterReset = allOffAfterReset;
173 ✗ out.priorityByChannel = priorityByChannel;
174 ✗ for (unsigned int c = 0; c < Trio3::NumChannels; c++) {
175 ✗ out.ratedCurrentCode[c] = setRatedCurrentCode[c];
176 ✗ out.channelOn[c] = channelOn[c];
177 }
178 ✗ auto pd = Trio3::encodeOutputs(out);
179 ✗ std::memcpy(domainOut_->getData() + offOut, pd.data(), pd.size());
180 }
181
182 6 const std::vector<ChannelKind> &inputKinds() const override
183 {
184 6 return inputKinds_;
185 }
186 7 const std::vector<ChannelKind> &outputKinds() const override
187 {
188 7 return outputKinds_;
189 }
190
191 protected:
192 2 ChannelValue getInputAt(size_t i) const override
193 {
194 // Kind blocks concatenated in inputKinds_ order.
195 2 constexpr size_t N = Trio3::NumChannels;
196
2/5
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
2 switch (i) {
197 1 case 0:
198 1 return bool(dcNotOk);
199 ✗ case 1:
200 ✗ return bool(powerAbove90);
201 ✗ case 2:
202 ✗ return outputVoltage;
203 ✗ case 3:
204 ✗ return totalCurrent;
205 }
206 1 i -= 4;
207
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (i < N) {
208 ✗ return channelState[i];
209 }
210 1 i -= N;
211
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (i < N) {
212 ✗ return ratedCurrentCode[i];
213 }
214 1 i -= N;
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (i < N) {
216 ✗ return ratedCurrent[i];
217 }
218 1 i -= N;
219
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (i < N) {
220 1 return loadCurrent[i];
221 }
222 ✗ throw UnknownChannel("PhoenixTrio3: invalid input channel index.");
223 }
224
225 5 void setOutputAt(size_t i, const ChannelValue &value) override
226 {
227 // Kind blocks concatenated in outputKinds_ order.
228 5 constexpr size_t N = Trio3::NumChannels;
229
2/3
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
5 switch (i) {
230 1 case 0:
231 1 valid = std::get<bool>(value);
232 1 return;
233 ✗ case 1:
234 ✗ allOffAfterReset = std::get<bool>(value);
235 ✗ return;
236 }
237 4 i -= 2;
238
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (i < N) {
239 2 uint8_t code = std::get<uint8_t>(value);
240
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (code > 6) {
241 throw std::out_of_range(
242
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 "PhoenixTrio3: rated current code must be 0..6.");
243 }
244 1 setRatedCurrentCode[i] = code;
245 1 return;
246 }
247 2 i -= N;
248
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (i == 0) {
249 ✗ priorityByChannel = std::get<bool>(value);
250 ✗ return;
251 }
252 2 i -= 1;
253
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (i < N) {
254 2 channelOn[i] = std::get<bool>(value);
255 1 return;
256 }
257 ✗ throw UnknownChannel("PhoenixTrio3: invalid output channel index.");
258 }
259
260 private:
261 ✗ static void mapBlock(
262 ec_slave_config_t *sc,
263 uint8_t syncIndex,
264 uint16_t pdoIndex,
265 uint16_t pdoEntryIndex,
266 uint8_t bits)
267 {
268 // Whole IO-Link process data as one entry (like ifm SM4000),
269 // decoded byte-wise in Trio3::decodeInputs()/encodeOutputs().
270 ✗ if (ecrt_slave_config_pdo_assign_add(sc, syncIndex, pdoIndex)) {
271 ✗ stringstream err;
272 ✗ err << "Failed to assign PDO " << pdoIndex;
273 ✗ throw runtime_error(err.str());
274 }
275 ✗ if (ecrt_slave_config_pdo_mapping_clear(sc, pdoIndex)) {
276 ✗ stringstream err;
277 ✗ err << "Failed to clear PDO mapping of " << pdoIndex;
278 ✗ throw runtime_error(err.str());
279 }
280 ✗ if (ecrt_slave_config_pdo_mapping_add(
281 ✗ sc, pdoIndex, pdoEntryIndex, 0x01, bits)) {
282 ✗ stringstream err;
283 ✗ err << "Failed to map PDO entry in " << pdoIndex;
284 ✗ throw runtime_error(err.str());
285 }
286 }
287
288 // Inputs
289 uint8_t dcNotOk = 0;
290 uint8_t powerAbove90 = 0;
291 float outputVoltage = 0.0f; // V
292 float totalCurrent = 0.0f; // A
293 std::vector<uint8_t> channelState =
294 std::vector<uint8_t>(Trio3::NumChannels, 0);
295 std::vector<uint8_t> ratedCurrentCode =
296 std::vector<uint8_t>(Trio3::NumChannels, 0);
297 std::vector<float> ratedCurrent =
298 std::vector<float>(Trio3::NumChannels, 0.0f); // A
299 std::vector<float> loadCurrent =
300 std::vector<float>(Trio3::NumChannels, 0.0f); // A
301
302 // Outputs (safe defaults, see class comment)
303 uint8_t valid = 0;
304 uint8_t allOffAfterReset = 0;
305 uint8_t priorityByChannel = 0;
306 std::vector<uint8_t> setRatedCurrentCode =
307 std::vector<uint8_t>(Trio3::NumChannels, 0);
308 std::vector<uint8_t> channelOn =
309 std::vector<uint8_t>(Trio3::NumChannels, 1);
310
311 int offIn = -1;
312 int offOut = -1;
313 uint16_t inputPdoEntryIndex = 0x0000;
314 uint16_t outputPdoEntryIndex = 0x0000;
315 Domain *domainIn_ = nullptr;
316 Domain *domainOut_ = nullptr;
317
318 std::vector<ChannelKind> inputKinds_ {
319 {"DcNotOk", 1, false},
320 {"PowerAbove90", 1, false},
321 {"OutputVoltage", 1, float(0)},
322 {"TotalCurrent", 1, float(0)},
323 {"ChannelState", Trio3::NumChannels, uint8_t(0)},
324 {"RatedCurrentCode", Trio3::NumChannels, uint8_t(0)},
325 {"RatedCurrent", Trio3::NumChannels, float(0)},
326 {"LoadCurrent", Trio3::NumChannels, float(0)}};
327 std::vector<ChannelKind> outputKinds_ {
328 {"Valid", 1, false},
329 {"AllOffAfterReset", 1, false},
330 {"SetRatedCurrentCode", Trio3::NumChannels, uint8_t(0)},
331 {"PriorityByChannel", 1, false},
332 {"ChannelOn", Trio3::NumChannels, false}};
333 };
334
335 /****************************************************************************/
336
337
4/8
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 15 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 15 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 15 times.
✗ Branch 13 not taken.
16 ECAPP_REGISTER_IOLINK_DEVICE(
338 "Phoenix-TRIO3-PS-1AC-24DC-20-8C-IOL",
339 PhoenixTrio3);
340
341 45 } // namespace EcApp
342
343 /****************************************************************************/
344