| Directory: | ./ |
|---|---|
| File: | src/devices/PhoenixTrio3.h |
| Date: | 2026-11-24 14:33:39 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 41 | 41 | 100.0% |
| Branches: | 14 | 14 | 100.0% |
| 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 | * Process data codec for the Phoenix Contact TRIO3-PS/1AC/24DC/20/8C/IOL | ||
| 22 | * (power supply with 8-channel electronic circuit breaker, IO-Link | ||
| 23 | * device). Pure byte <-> struct conversion, without any EtherCAT | ||
| 24 | * dependency, so it can be unit-tested on its own -- the IO-Link driver | ||
| 25 | * itself lives in PhoenixIoLink.cpp. | ||
| 26 | * | ||
| 27 | * Sources: PHOENIX_CONTACT-TRIO3-PS_1AC_24DC_20_8C_IOL-20221219- | ||
| 28 | * IODD1.1.xml (record items V_PDin/D_ProcessDataOut) and data sheet | ||
| 29 | * 111031_de_05 "TRIO3-PS/.../IOL Prozessdaten und Zustandsinformationen". | ||
| 30 | * | ||
| 31 | * IO-Link bit offsets count from the *end* of the process data (bit 0 is | ||
| 32 | * the LSB of the last byte), and the bytes are transferred MSB first. | ||
| 33 | * | ||
| 34 | ****************************************************************************/ | ||
| 35 | |||
| 36 | #ifndef ECAPP_PHOENIXTRIO3_H | ||
| 37 | #define ECAPP_PHOENIXTRIO3_H | ||
| 38 | |||
| 39 | /****************************************************************************/ | ||
| 40 | |||
| 41 | #include <array> | ||
| 42 | #include <cstddef> | ||
| 43 | #include <stdint.h> | ||
| 44 | |||
| 45 | /****************************************************************************/ | ||
| 46 | |||
| 47 | namespace EcApp { namespace Trio3 { | ||
| 48 | |||
| 49 | constexpr unsigned int NumChannels = 8; | ||
| 50 | constexpr std::size_t PdInBytes = 18; // 144 bit | ||
| 51 | constexpr std::size_t PdOutBytes = 7; // 56 bit | ||
| 52 | |||
| 53 | /** Rated current (INom) code -> Ampere; codes 0..6 are valid, the device | ||
| 54 | * treats 7 as 0. */ | ||
| 55 | constexpr float RatedCurrentAmps[8] = {1.0f, 2.0f, 3.8f, 4.0f, | ||
| 56 | 6.0f, 8.0f, 10.0f, 1.0f}; | ||
| 57 | |||
| 58 | /** Reads `len` bits at IO-Link bit offset `bitOffset` from a big-endian | ||
| 59 | * process data image of `size` bytes. */ | ||
| 60 | inline uint32_t | ||
| 61 | 56 | getBits(const uint8_t *pd, | |
| 62 | std::size_t size, | ||
| 63 | unsigned int bitOffset, | ||
| 64 | unsigned int len) | ||
| 65 | { | ||
| 66 | 56 | uint32_t value = 0; | |
| 67 |
2/2✓ Branch 0 taken 252 times.
✓ Branch 1 taken 56 times.
|
308 | for (unsigned int i = len; i-- > 0;) { |
| 68 | 252 | unsigned int bit = bitOffset + i; | |
| 69 | 252 | std::size_t byte = size - 1 - bit / 8; | |
| 70 | 252 | value = (value << 1) | ((pd[byte] >> (bit % 8)) & 0x01); | |
| 71 | } | ||
| 72 | 56 | return value; | |
| 73 | } | ||
| 74 | |||
| 75 | /** Counterpart of getBits(); only touches the addressed bits. */ | ||
| 76 | inline void | ||
| 77 | 38 | setBits(uint8_t *pd, | |
| 78 | std::size_t size, | ||
| 79 | unsigned int bitOffset, | ||
| 80 | unsigned int len, | ||
| 81 | uint32_t value) | ||
| 82 | { | ||
| 83 |
2/2✓ Branch 0 taken 70 times.
✓ Branch 1 taken 38 times.
|
108 | for (unsigned int i = 0; i < len; i++) { |
| 84 | 70 | unsigned int bit = bitOffset + i; | |
| 85 | 70 | std::size_t byte = size - 1 - bit / 8; | |
| 86 | 70 | uint8_t mask = static_cast<uint8_t>(1u << (bit % 8)); | |
| 87 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 38 times.
|
70 | if ((value >> i) & 0x01) { |
| 88 | 32 | pd[byte] |= mask; | |
| 89 | } | ||
| 90 | else { | ||
| 91 | 38 | pd[byte] &= static_cast<uint8_t>(~mask); | |
| 92 | } | ||
| 93 | } | ||
| 94 | 38 | } | |
| 95 | |||
| 96 | /** Decoded input process data (PDin). */ | ||
| 97 | struct Inputs | ||
| 98 | { | ||
| 99 | bool dcNotOk; // sub 1: UOut < 21 V | ||
| 100 | bool powerAbove90; // sub 2: POut > 90 % | ||
| 101 | float outputVoltage; // sub 4: [V], 0.1 V resolution | ||
| 102 | float totalCurrent; // sub 5: [A], 0.1 A resolution | ||
| 103 | uint8_t channelState[NumChannels]; // sub 6..13: 0 OFF, 1 ON, | ||
| 104 | // 2 RESTART, 3 TRIPPED/DEFECT | ||
| 105 | uint8_t ratedCurrentCode[NumChannels]; // sub 14..21, see | ||
| 106 | // RatedCurrentAmps | ||
| 107 | float loadCurrent[NumChannels]; // sub 22..29: [A], 0.1 A | ||
| 108 | }; | ||
| 109 | |||
| 110 | 2 | inline Inputs decodeInputs(const uint8_t *pd) | |
| 111 | { | ||
| 112 | 2 | Inputs in {}; | |
| 113 | 2 | in.dcNotOk = getBits(pd, PdInBytes, 140, 1); | |
| 114 | 2 | in.powerAbove90 = getBits(pd, PdInBytes, 139, 1); | |
| 115 | 2 | in.outputVoltage = getBits(pd, PdInBytes, 128, 10) * 0.1f; | |
| 116 | 2 | in.totalCurrent = getBits(pd, PdInBytes, 112, 10) * 0.1f; | |
| 117 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
|
18 | for (unsigned int c = 0; c < NumChannels; c++) { |
| 118 | 16 | in.channelState[c] = | |
| 119 | 16 | static_cast<uint8_t>(getBits(pd, PdInBytes, 110 - 2 * c, 2)); | |
| 120 | // 3 bit codes, two per byte, upper one at bit 3 of the byte | ||
| 121 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
|
16 | unsigned int off = 88 - 8 * (c / 2) + ((c % 2) ? 0 : 3); |
| 122 | 16 | in.ratedCurrentCode[c] = | |
| 123 | 16 | static_cast<uint8_t>(getBits(pd, PdInBytes, off, 3)); | |
| 124 | 16 | in.loadCurrent[c] = getBits(pd, PdInBytes, 56 - 8 * c, 8) * 0.1f; | |
| 125 | } | ||
| 126 | 2 | return in; | |
| 127 | } | ||
| 128 | |||
| 129 | /** Output process data (PDout). The device ignores the whole PDout | ||
| 130 | * unless `valid` is set -- and then applies every field at once. */ | ||
| 131 | struct Outputs | ||
| 132 | { | ||
| 133 | bool valid; // sub 1: "PDout übernehmen" | ||
| 134 | bool allOffAfterReset; // sub 2: false = restore last state | ||
| 135 | uint8_t ratedCurrentCode[NumChannels]; // sub 3..10, 0..6 | ||
| 136 | bool priorityByChannel; // sub 11: false = highest current first | ||
| 137 | bool channelOn[NumChannels]; // sub 12..19 | ||
| 138 | }; | ||
| 139 | |||
| 140 | 2 | inline std::array<uint8_t, PdOutBytes> encodeOutputs(const Outputs &out) | |
| 141 | { | ||
| 142 | 2 | std::array<uint8_t, PdOutBytes> pd {}; | |
| 143 | 2 | setBits(pd.data(), PdOutBytes, 55, 1, out.valid); | |
| 144 | 2 | setBits(pd.data(), PdOutBytes, 48, 1, out.allOffAfterReset); | |
| 145 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
|
18 | for (unsigned int c = 0; c < NumChannels; c++) { |
| 146 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
|
16 | unsigned int off = 40 - 8 * (c / 2) + ((c % 2) ? 0 : 3); |
| 147 | 16 | setBits(pd.data(), PdOutBytes, off, 3, | |
| 148 | 16 | out.ratedCurrentCode[c] & 0x07); | |
| 149 | 16 | setBits(pd.data(), PdOutBytes, 7 - c, 1, out.channelOn[c]); | |
| 150 | } | ||
| 151 | 2 | setBits(pd.data(), PdOutBytes, 8, 1, out.priorityByChannel); | |
| 152 | 2 | return pd; | |
| 153 | } | ||
| 154 | |||
| 155 | }} // namespace EcApp::Trio3 | ||
| 156 | |||
| 157 | #endif // ECAPP_PHOENIXTRIO3_H | ||
| 158 | |||
| 159 | /****************************************************************************/ | ||
| 160 |