| Directory: | ./ |
|---|---|
| File: | src/devices/BalluffIoLink.cpp |
| Date: | 2026-11-24 14:33:39 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 44 | 97 | 45.4% |
| Branches: | 44 | 138 | 31.9% |
| 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 Balluff devices (on an EL6224/ED6224 port). | ||
| 22 | * | ||
| 23 | ****************************************************************************/ | ||
| 24 | |||
| 25 | #include "ecapp/Domain.h" | ||
| 26 | #include "ecapp/Exceptions.h" | ||
| 27 | #include "ecapp/IoLinkDevice.h" | ||
| 28 | |||
| 29 | #include <cstring> | ||
| 30 | #include <sstream> | ||
| 31 | #include <stdexcept> | ||
| 32 | #include <string> | ||
| 33 | #include <vector> | ||
| 34 | |||
| 35 | /****************************************************************************/ | ||
| 36 | |||
| 37 | using std::runtime_error; | ||
| 38 | using std::size_t; | ||
| 39 | using std::string; | ||
| 40 | using std::stringstream; | ||
| 41 | |||
| 42 | /****************************************************************************/ | ||
| 43 | |||
| 44 | namespace EcApp { | ||
| 45 | |||
| 46 | /****************************************************************************/ | ||
| 47 | // Balluff BIS M-4A3-082-401-07-S4 (order code BIS01E5): 13.56 MHz RFID | ||
| 48 | // read/write head (ISO 15693) with IO-Link interface. | ||
| 49 | // | ||
| 50 | // Transparent driver: the IODD only describes the process data as | ||
| 51 | // "Bitheader 1, Byte 1..8, Bitheader 2" in both directions -- the meaning | ||
| 52 | // of the header bits (job, toggle, tag present, error, ...) and the | ||
| 53 | // command codes is Balluff's BIS protocol, documented in the device | ||
| 54 | // manual, not in the IODD. So the raw bytes are exposed as-is and the | ||
| 55 | // application implements the protocol: | ||
| 56 | // | ||
| 57 | // Inputs: BitHeader1, Data[0..7], BitHeader2 | ||
| 58 | // Outputs: SetBitHeader1, SetData[0..7], SetBitHeader2 (all 0 by | ||
| 59 | // default) | ||
| 60 | // | ||
| 61 | // Data[0] is "Byte 1" of the manual/IODD. The image is sent MSB first, so | ||
| 62 | // the bytes are in wire order (IODD bit offset 72 = first byte). | ||
| 63 | // | ||
| 64 | // The head must be in its "Default" device variant (ISDU 0x0055 = 1, | ||
| 65 | // factory setting). In the "Comprehensive Condition Monitoring" variant | ||
| 66 | // (0x0055 = 2) it reports device ID 0x060234 and 11 byte PD input, so the | ||
| 67 | // port's identity check fails (configuration guide DOK 951215, tab. 3-1). | ||
| 68 | /****************************************************************************/ | ||
| 69 | |||
| 70 | 2 | class BalluffBisM4A3 : public IoLinkDevice | |
| 71 | { | ||
| 72 | public: | ||
| 73 | static constexpr size_t PdBytes = 10; // 80 bit, both directions | ||
| 74 | static constexpr size_t DataBytes = 8; | ||
| 75 | |||
| 76 | 1 | BalluffBisM4A3(pdserv *pdServ, pdtask *task, const string &prefix) : | |
| 77 |
14/28✓ 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 17 taken 1 times.
✗ Branch 18 not taken.
✓ Branch 21 taken 1 times.
✗ Branch 22 not taken.
✓ Branch 25 taken 1 times.
✗ Branch 26 not taken.
✓ Branch 28 taken 3 times.
✓ Branch 29 taken 1 times.
✓ Branch 36 taken 1 times.
✗ Branch 37 not taken.
✓ Branch 40 taken 1 times.
✗ Branch 41 not taken.
✓ Branch 44 taken 1 times.
✗ Branch 45 not taken.
✓ Branch 48 taken 1 times.
✗ Branch 49 not taken.
✓ Branch 51 taken 3 times.
✓ Branch 52 taken 1 times.
✗ Branch 60 not taken.
✗ Branch 61 not taken.
✗ Branch 67 not taken.
✗ Branch 68 not taken.
|
1 | IoLinkDevice(pdServ, task, prefix) |
| 78 | { | ||
| 79 |
2/4✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
|
1 | registerInputChannel("BitHeader1", bitHeader1In); |
| 80 |
2/4✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
|
1 | registerInputChannel("Data", dataIn); |
| 81 |
2/4✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
|
1 | registerInputChannel("BitHeader2", bitHeader2In); |
| 82 | |||
| 83 |
2/4✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
|
1 | registerOutputChannel("SetBitHeader1", bitHeader1Out); |
| 84 |
2/4✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
|
1 | registerOutputChannel("SetData", dataOut); |
| 85 |
2/4✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
|
1 | registerOutputChannel("SetBitHeader2", bitHeader2Out); |
| 86 | 1 | } | |
| 87 | |||
| 88 | ✗ | void addConfig(ec_slave_config_t *sc, uint16_t sdoIndex) override | |
| 89 | { | ||
| 90 | // Balluff-BISM4A308240107S4-20210928-IODD1.1.xml: DeviceIdentity | ||
| 91 | // vendorId="888" deviceId="393776", PhysicalLayer | ||
| 92 | // minCycleTime="1600" (us) sioSupported="false" bitrate="COM3", | ||
| 93 | // iolinkRevision V1.1, Features dataStorage="true". | ||
| 94 | ✗ | IoLinkPortIdentity identity; | |
| 95 | ✗ | identity.vendorId = 888; | |
| 96 | ✗ | identity.deviceId = 393776; // 0x060230 | |
| 97 | ✗ | identity.minCycleTimeMs = 1.6; | |
| 98 | ✗ | identity.processDataInBits = PdBytes * 8; | |
| 99 | ✗ | identity.processDataOutBits = PdBytes * 8; | |
| 100 | ✗ | identity.sioSupported = false; | |
| 101 | ✗ | configurePort(sc, sdoIndex, identity); | |
| 102 | } | ||
| 103 | |||
| 104 | ✗ | bool addOutputs( | |
| 105 | ec_slave_config_t *sc, | ||
| 106 | uint16_t pdoIndex, | ||
| 107 | uint16_t pdoEntryIndex, | ||
| 108 | Domain *) override | ||
| 109 | { | ||
| 110 | ✗ | mapBlock(sc, 2, pdoIndex, pdoEntryIndex, PdBytes * 8); | |
| 111 | ✗ | outputPdoEntryIndex = pdoEntryIndex; | |
| 112 | ✗ | return true; | |
| 113 | } | ||
| 114 | |||
| 115 | ✗ | bool addInputs( | |
| 116 | ec_slave_config_t *sc, | ||
| 117 | uint16_t pdoIndex, | ||
| 118 | uint16_t pdoEntryIndex, | ||
| 119 | Domain *) override | ||
| 120 | { | ||
| 121 | ✗ | mapBlock(sc, 3, pdoIndex, pdoEntryIndex, PdBytes * 8); | |
| 122 | ✗ | inputPdoEntryIndex = pdoEntryIndex; | |
| 123 | ✗ | return true; | |
| 124 | } | ||
| 125 | |||
| 126 | ✗ | void registerOutputs(ec_slave_config_t *sc, Domain *domainOut) override | |
| 127 | { | ||
| 128 | ✗ | domainOut_ = domainOut; | |
| 129 | ✗ | offOut = ecrt_slave_config_reg_pdo_entry( | |
| 130 | ✗ | sc, outputPdoEntryIndex, 0x01, domainOut->getDomain(), NULL); | |
| 131 | ✗ | if (offOut < 0) { | |
| 132 | throw runtime_error( | ||
| 133 | ✗ | "Failed to register BalluffBisM4A3 output PDO entry."); | |
| 134 | } | ||
| 135 | } | ||
| 136 | |||
| 137 | ✗ | void registerInputs(ec_slave_config_t *sc, Domain *domainIn) override | |
| 138 | { | ||
| 139 | ✗ | domainIn_ = domainIn; | |
| 140 | ✗ | offIn = ecrt_slave_config_reg_pdo_entry( | |
| 141 | ✗ | sc, inputPdoEntryIndex, 0x01, domainIn->getDomain(), NULL); | |
| 142 | ✗ | if (offIn < 0) { | |
| 143 | throw runtime_error( | ||
| 144 | ✗ | "Failed to register BalluffBisM4A3 input PDO entry."); | |
| 145 | } | ||
| 146 | } | ||
| 147 | |||
| 148 | 1 | void updateInputs() override | |
| 149 | { | ||
| 150 |
2/6✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
|
1 | if (!domainIn_ || !domainIn_->getData()) { |
| 151 | 1 | return; | |
| 152 | } | ||
| 153 | ✗ | const uint8_t *pd = domainIn_->getData() + offIn; | |
| 154 | ✗ | bitHeader1In = pd[0]; | |
| 155 | ✗ | std::memcpy(dataIn.data(), pd + 1, DataBytes); | |
| 156 | ✗ | bitHeader2In = pd[PdBytes - 1]; | |
| 157 | } | ||
| 158 | |||
| 159 | 1 | void updateOutputs() override | |
| 160 | { | ||
| 161 |
2/6✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
|
1 | if (!domainOut_ || !domainOut_->getData()) { |
| 162 | 1 | return; | |
| 163 | } | ||
| 164 | ✗ | uint8_t *pd = domainOut_->getData() + offOut; | |
| 165 | ✗ | pd[0] = bitHeader1Out; | |
| 166 | ✗ | std::memcpy(pd + 1, dataOut.data(), DataBytes); | |
| 167 | ✗ | pd[PdBytes - 1] = bitHeader2Out; | |
| 168 | } | ||
| 169 | |||
| 170 | 4 | const std::vector<ChannelKind> &inputKinds() const override | |
| 171 | { | ||
| 172 | 4 | return inputKinds_; | |
| 173 | } | ||
| 174 | 6 | const std::vector<ChannelKind> &outputKinds() const override | |
| 175 | { | ||
| 176 | 6 | return outputKinds_; | |
| 177 | } | ||
| 178 | |||
| 179 | protected: | ||
| 180 | 3 | ChannelValue getInputAt(size_t i) const override | |
| 181 | { | ||
| 182 | // Kind blocks concatenated in inputKinds_ order. | ||
| 183 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
|
3 | if (i == 0) { |
| 184 | 1 | return bitHeader1In; | |
| 185 | } | ||
| 186 | 2 | i -= 1; | |
| 187 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (i < DataBytes) { |
| 188 | 1 | return dataIn[i]; | |
| 189 | } | ||
| 190 | 1 | i -= DataBytes; | |
| 191 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (i == 0) { |
| 192 | 1 | return bitHeader2In; | |
| 193 | } | ||
| 194 | ✗ | throw UnknownChannel("BalluffBisM4A3: invalid input channel index."); | |
| 195 | } | ||
| 196 | |||
| 197 | 4 | void setOutputAt(size_t i, const ChannelValue &value) override | |
| 198 | { | ||
| 199 | // Kind blocks concatenated in outputKinds_ order. | ||
| 200 | 4 | uint8_t byte = std::get<uint8_t>(value); | |
| 201 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
|
3 | if (i == 0) { |
| 202 | 1 | bitHeader1Out = byte; | |
| 203 | 1 | return; | |
| 204 | } | ||
| 205 | 2 | i -= 1; | |
| 206 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (i < DataBytes) { |
| 207 | 1 | dataOut[i] = byte; | |
| 208 | 1 | return; | |
| 209 | } | ||
| 210 | 1 | i -= DataBytes; | |
| 211 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (i == 0) { |
| 212 | 1 | bitHeader2Out = byte; | |
| 213 | 1 | return; | |
| 214 | } | ||
| 215 | ✗ | throw UnknownChannel("BalluffBisM4A3: invalid output channel index."); | |
| 216 | } | ||
| 217 | |||
| 218 | private: | ||
| 219 | ✗ | static void mapBlock( | |
| 220 | ec_slave_config_t *sc, | ||
| 221 | uint8_t syncIndex, | ||
| 222 | uint16_t pdoIndex, | ||
| 223 | uint16_t pdoEntryIndex, | ||
| 224 | uint8_t bits) | ||
| 225 | { | ||
| 226 | // Whole IO-Link process data as one entry (like ifm SM4000). | ||
| 227 | ✗ | if (ecrt_slave_config_pdo_assign_add(sc, syncIndex, pdoIndex)) { | |
| 228 | ✗ | stringstream err; | |
| 229 | ✗ | err << "Failed to assign PDO " << pdoIndex; | |
| 230 | ✗ | throw runtime_error(err.str()); | |
| 231 | } | ||
| 232 | ✗ | if (ecrt_slave_config_pdo_mapping_clear(sc, pdoIndex)) { | |
| 233 | ✗ | stringstream err; | |
| 234 | ✗ | err << "Failed to clear PDO mapping of " << pdoIndex; | |
| 235 | ✗ | throw runtime_error(err.str()); | |
| 236 | } | ||
| 237 | ✗ | if (ecrt_slave_config_pdo_mapping_add( | |
| 238 | ✗ | sc, pdoIndex, pdoEntryIndex, 0x01, bits)) { | |
| 239 | ✗ | stringstream err; | |
| 240 | ✗ | err << "Failed to map PDO entry in " << pdoIndex; | |
| 241 | ✗ | throw runtime_error(err.str()); | |
| 242 | } | ||
| 243 | } | ||
| 244 | |||
| 245 | // Inputs | ||
| 246 | uint8_t bitHeader1In = 0; | ||
| 247 | std::vector<uint8_t> dataIn = std::vector<uint8_t>(DataBytes, 0); | ||
| 248 | uint8_t bitHeader2In = 0; | ||
| 249 | |||
| 250 | // Outputs | ||
| 251 | uint8_t bitHeader1Out = 0; | ||
| 252 | std::vector<uint8_t> dataOut = std::vector<uint8_t>(DataBytes, 0); | ||
| 253 | uint8_t bitHeader2Out = 0; | ||
| 254 | |||
| 255 | int offIn = -1; | ||
| 256 | int offOut = -1; | ||
| 257 | uint16_t inputPdoEntryIndex = 0x0000; | ||
| 258 | uint16_t outputPdoEntryIndex = 0x0000; | ||
| 259 | Domain *domainIn_ = nullptr; | ||
| 260 | Domain *domainOut_ = nullptr; | ||
| 261 | |||
| 262 | std::vector<ChannelKind> inputKinds_ { | ||
| 263 | {"BitHeader1", 1, uint8_t(0)}, | ||
| 264 | {"Data", DataBytes, uint8_t(0)}, | ||
| 265 | {"BitHeader2", 1, uint8_t(0)}}; | ||
| 266 | std::vector<ChannelKind> outputKinds_ { | ||
| 267 | {"SetBitHeader1", 1, uint8_t(0)}, | ||
| 268 | {"SetData", DataBytes, uint8_t(0)}, | ||
| 269 | {"SetBitHeader2", 1, uint8_t(0)}}; | ||
| 270 | }; | ||
| 271 | |||
| 272 | /****************************************************************************/ | ||
| 273 | |||
| 274 |
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( |
| 275 | "Balluff-BIS-M-4A3-082-401-07-S4", | ||
| 276 | BalluffBisM4A3); | ||
| 277 | |||
| 278 | 45 | } // namespace EcApp | |
| 279 | |||
| 280 | /****************************************************************************/ | ||
| 281 |