scietex.hal.current-amplifier 0.3.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,347 @@
1
+ """Static relay-to-function map for the EBIC v1.0 preamp board.
2
+
3
+ A Modbus coil value is the raw GPIO level of its relay, not a feature flag: every
4
+ relay except relay 12 engages its function at coil value ``False``, and relay 12
5
+ engages at ``True``. This module owns that polarity, the ``Rf`` and booster-gain
6
+ tables, and the pure decode from a coil snapshot to the function state it
7
+ encodes. It touches no bus, so a caller can name relays and decode readbacks
8
+ without a device.
9
+
10
+ The map transcribes ``docs/relay-function-map.md``, which in turn transcribes the
11
+ ``ebic_control`` firmware.
12
+
13
+ Classes:
14
+ Relay: The seventeen relay outputs, numbered 1-17.
15
+ RfChannel: The two input-path channels relays 1 and 2 select.
16
+ BiasCompensationSource: The source relay 13 selects.
17
+ RelayReadback: The function state a coil snapshot encodes.
18
+
19
+ Constants:
20
+ RF_OHMS: The eight feedback resistors in ohm, ascending.
21
+ RF_RELAY: Feedback resistor to the relay that connects it.
22
+ RF_CHANNEL: ``Rf`` relay to the channel it belongs to.
23
+ GAIN_RELAY: Booster gain to the relay that selects it.
24
+ CHANNEL_COILS: Channel to the coil value that selects it.
25
+ BOOSTER_COILS: Channel to the relay-3 coil that feeds it to the booster.
26
+ POWER_ON_COILS: The coil snapshot of the power-on and blank-EEPROM state.
27
+
28
+ Functions:
29
+ coil_index: The coil index a relay addresses.
30
+ coil_level: The coil value for a desired feature state.
31
+ relay_engaged: The feature state a coil value encodes.
32
+ engaged_relays: The relays a coil snapshot holds engaged.
33
+ decode_coils: Decode a coil snapshot into a :class:`RelayReadback`.
34
+ """
35
+
36
+ from collections.abc import Sequence
37
+ from dataclasses import dataclass
38
+ from enum import Enum, IntEnum
39
+
40
+
41
+ class Relay(IntEnum):
42
+ """The seventeen relay outputs, numbered as the board's tester numbers.
43
+
44
+ The member value is the one-based relay number; the firmware derives the
45
+ coil index from it without a lookup table.
46
+ """
47
+
48
+ CHANNEL_SELECT = 1
49
+ DESELECTED_FEEDBACK = 2
50
+ BOOST_SOURCE = 3
51
+ BIAS_ENABLE = 4
52
+ RF_HIGH_10G = 5
53
+ RF_HIGH_1G = 6
54
+ RF_HIGH_100M = 7
55
+ RF_HIGH_10M = 8
56
+ RF_LOW_1M = 9
57
+ RF_LOW_100K = 10
58
+ RF_LOW_10K = 11
59
+ RF_LOW_1K = 12
60
+ BIAS_COMP_SOURCE = 13
61
+ GAIN_X3 = 14
62
+ GAIN_X10 = 15
63
+ GAIN_X30 = 16
64
+ COMPENSATION_ENABLE = 17
65
+
66
+
67
+ class RfChannel(str, Enum):
68
+ """The two input-path channels relays 1 and 2 select.
69
+
70
+ The member value is the spelling a driver reports; it doubles as the
71
+ serialised form because the class inherits from ``str``.
72
+ """
73
+
74
+ LOW_Z = "low_z"
75
+ HIGH_Z = "high_z"
76
+
77
+
78
+ class BiasCompensationSource(str, Enum):
79
+ """The source relay 13 selects for bias and compensation together."""
80
+
81
+ DAC = "dac"
82
+ MANUAL = "manual"
83
+
84
+
85
+ RF_OHMS: tuple[float, ...] = (
86
+ 1_000.0,
87
+ 10_000.0,
88
+ 100_000.0,
89
+ 1_000_000.0,
90
+ 10_000_000.0,
91
+ 100_000_000.0,
92
+ 1_000_000_000.0,
93
+ 10_000_000_000.0,
94
+ )
95
+ """Feedback resistors in ohm, ascending: the low-Z bank then the high-Z bank."""
96
+
97
+ RF_RELAY: dict[int, Relay] = {
98
+ 1_000: Relay.RF_LOW_1K,
99
+ 10_000: Relay.RF_LOW_10K,
100
+ 100_000: Relay.RF_LOW_100K,
101
+ 1_000_000: Relay.RF_LOW_1M,
102
+ 10_000_000: Relay.RF_HIGH_10M,
103
+ 100_000_000: Relay.RF_HIGH_100M,
104
+ 1_000_000_000: Relay.RF_HIGH_1G,
105
+ 10_000_000_000: Relay.RF_HIGH_10G,
106
+ }
107
+ """Map a feedback resistor in ohm to the relay that connects it."""
108
+
109
+ RF_CHANNEL: dict[Relay, RfChannel] = {
110
+ Relay.RF_HIGH_10G: RfChannel.HIGH_Z,
111
+ Relay.RF_HIGH_1G: RfChannel.HIGH_Z,
112
+ Relay.RF_HIGH_100M: RfChannel.HIGH_Z,
113
+ Relay.RF_HIGH_10M: RfChannel.HIGH_Z,
114
+ Relay.RF_LOW_1M: RfChannel.LOW_Z,
115
+ Relay.RF_LOW_100K: RfChannel.LOW_Z,
116
+ Relay.RF_LOW_10K: RfChannel.LOW_Z,
117
+ Relay.RF_LOW_1K: RfChannel.LOW_Z,
118
+ }
119
+ """Map each feedback-relay bank to the channel it serves."""
120
+
121
+ GAIN_RELAY: dict[float, Relay | None] = {
122
+ 1.0: None,
123
+ 3.0: Relay.GAIN_X3,
124
+ 10.0: Relay.GAIN_X10,
125
+ 30.0: Relay.GAIN_X30,
126
+ }
127
+ """Map a booster gain to the relay that selects it; unity has no relay."""
128
+
129
+ CHANNEL_COILS: dict[RfChannel, bool] = {
130
+ RfChannel.LOW_Z: True,
131
+ RfChannel.HIGH_Z: False,
132
+ }
133
+ """Map a channel to the coil value relays 1 and 2 carry when it is selected."""
134
+
135
+ BOOSTER_COILS: dict[RfChannel, bool] = {
136
+ RfChannel.LOW_Z: True,
137
+ RfChannel.HIGH_Z: False,
138
+ }
139
+ """Map a channel to the relay-3 coil that routes its stage-1 output to the booster.
140
+
141
+ Firmware commit ``b83e474`` fixes which booster input each level closes: coil
142
+ ``True`` selects ``OUT2``, the low-Z output, and coil ``False`` selects
143
+ ``OUT1``, the high-Z output. The level therefore matches ``CHANNEL_COILS`` for
144
+ every channel, so the power-on state already feeds the selected low-Z channel
145
+ to the booster.
146
+ """
147
+
148
+ POWER_ON_COILS: tuple[bool, ...] = (True,) * 17
149
+ """Coil values of the power-on and blank-EEPROM state; relay 12 alone engages."""
150
+
151
+ _OHM_BY_RELAY: dict[Relay, float] = {
152
+ relay: float(ohm) for ohm, relay in RF_RELAY.items()
153
+ }
154
+ """Map a feedback relay to its resistor, keeping the ``float`` ohm domain."""
155
+
156
+ _GAIN_BY_RELAY: dict[Relay, float] = {
157
+ relay: gain for gain, relay in GAIN_RELAY.items() if relay is not None
158
+ }
159
+ """Map a gain relay to its gain, the inverse of ``GAIN_RELAY`` without unity."""
160
+
161
+ _INVERTED_RELAYS: frozenset[Relay] = frozenset({Relay.RF_LOW_1K})
162
+ """Relays that engage at coil value ``True``; relay 12 alone connects at GPIO 1."""
163
+
164
+
165
+ def coil_index(relay: Relay) -> int:
166
+ """
167
+ Return the coil index that addresses ``relay``.
168
+
169
+ Args:
170
+ relay: The relay output, numbered 1-17.
171
+
172
+ Returns:
173
+ int: The zero-based coil index, equal to ``relay - 1``.
174
+ """
175
+ return int(relay) - 1
176
+
177
+
178
+ def coil_level(relay: Relay, engaged: bool) -> bool:
179
+ """
180
+ Return the coil value that drives ``relay`` to the wanted feature state.
181
+
182
+ Args:
183
+ relay: The relay output to drive.
184
+ engaged: Whether the relay's function should be engaged.
185
+
186
+ Returns:
187
+ bool: The raw GPIO level to write. Every relay except relay 12 engages at
188
+ ``False``, so for those the level is the negation of ``engaged``.
189
+ """
190
+ if relay in _INVERTED_RELAYS:
191
+ return engaged
192
+ return not engaged
193
+
194
+
195
+ def relay_engaged(relay: Relay, level: bool) -> bool:
196
+ """
197
+ Return whether coil value ``level`` engages ``relay``'s function.
198
+
199
+ Args:
200
+ relay: The relay output the level is read for.
201
+ level: The raw GPIO level, as reported by the coil or the mirror.
202
+
203
+ Returns:
204
+ bool: The feature state, the inverse of :func:`coil_level`.
205
+ """
206
+ if relay in _INVERTED_RELAYS:
207
+ return level
208
+ return not level
209
+
210
+
211
+ def engaged_relays(coils: Sequence[bool]) -> tuple[Relay, ...]:
212
+ """
213
+ Return the relays a coil snapshot holds engaged, in relay order.
214
+
215
+ Args:
216
+ coils: Coil values in relay order, index 0 = relay 1. A sequence shorter
217
+ than seventeen is padded with the power-on level.
218
+
219
+ Returns:
220
+ tuple[Relay, ...]: The engaged relays; empty when every function is idle.
221
+ """
222
+ levels = _levels(coils)
223
+ return tuple(
224
+ relay for relay in Relay if relay_engaged(relay, levels[coil_index(relay)])
225
+ )
226
+
227
+
228
+ @dataclass(frozen=True, slots=True)
229
+ class RelayReadback:
230
+ """The function state a coil snapshot encodes.
231
+
232
+ Every field carries the state of one function, and a field whose encoding is
233
+ ambiguous carries ``None`` instead of guessing.
234
+
235
+ Attributes:
236
+ channel: The selected input-path channel, or ``None`` when relays 1 and
237
+ 2 disagree, which the firmware never writes on purpose.
238
+ feedback_resistor_ohm: The selected ``Rf`` in ohm, or ``None`` unless the
239
+ ``Rf`` relays name exactly one resistor and that resistor belongs to
240
+ the selected channel.
241
+ boost_gain: The dimensionless booster gain. ``1.0`` when no gain relay is
242
+ engaged, the selected gain when exactly one is, and ``None`` when two
243
+ or more are, since the one-hot coding leaves that state undefined.
244
+ dc_bias_enabled: Whether DC bias injection is on (relay 4).
245
+ input_compensation_enabled: Whether input-capacitance compensation is on
246
+ (relay 17).
247
+ booster_source_coil: The raw coil value of relay 3, which feeds ``OUT1``
248
+ to the booster at ``False`` and ``OUT2`` at ``True``.
249
+ bias_compensation_source: The source relay 13 selects for bias and
250
+ compensation together.
251
+ """
252
+
253
+ channel: RfChannel | None
254
+ feedback_resistor_ohm: float | None
255
+ boost_gain: float | None
256
+ dc_bias_enabled: bool
257
+ input_compensation_enabled: bool
258
+ booster_source_coil: bool
259
+ bias_compensation_source: BiasCompensationSource
260
+
261
+
262
+ def decode_coils(coils: Sequence[bool]) -> RelayReadback:
263
+ """
264
+ Decode a coil snapshot into the function state it represents.
265
+
266
+ The decode is total: every snapshot yields a readback, and a combination the
267
+ board does not define yields ``None`` for the field it leaves undefined
268
+ rather than an error.
269
+
270
+ Args:
271
+ coils: Coil values in relay order, index 0 = relay 1. A sequence shorter
272
+ than seventeen is padded with the power-on level.
273
+
274
+ Returns:
275
+ RelayReadback: The decoded function state.
276
+ """
277
+ levels = _levels(coils)
278
+ channel = _decode_channel(levels)
279
+ return RelayReadback(
280
+ channel=channel,
281
+ feedback_resistor_ohm=_decode_feedback_resistor(levels, channel),
282
+ boost_gain=_decode_boost_gain(levels),
283
+ dc_bias_enabled=relay_engaged(
284
+ Relay.BIAS_ENABLE, levels[coil_index(Relay.BIAS_ENABLE)]
285
+ ),
286
+ input_compensation_enabled=relay_engaged(
287
+ Relay.COMPENSATION_ENABLE, levels[coil_index(Relay.COMPENSATION_ENABLE)]
288
+ ),
289
+ booster_source_coil=levels[coil_index(Relay.BOOST_SOURCE)],
290
+ bias_compensation_source=(
291
+ BiasCompensationSource.MANUAL
292
+ if levels[coil_index(Relay.BIAS_COMP_SOURCE)]
293
+ else BiasCompensationSource.DAC
294
+ ),
295
+ )
296
+
297
+
298
+ def _levels(coils: Sequence[bool]) -> tuple[bool, ...]:
299
+ """Pad a snapshot to seventeen values with the power-on level.
300
+
301
+ The firmware mirrors seventeen coils, so a shorter sequence cannot be a
302
+ complete snapshot; its missing coils take the reset level instead of making
303
+ the decode raise.
304
+ """
305
+ levels = tuple(coils)[: len(Relay)]
306
+ return levels + (True,) * (len(Relay) - len(levels))
307
+
308
+
309
+ def _decode_channel(levels: tuple[bool, ...]) -> RfChannel | None:
310
+ """Return the channel relays 1 and 2 agree on, or ``None`` when they differ."""
311
+ select = levels[coil_index(Relay.CHANNEL_SELECT)]
312
+ if select != levels[coil_index(Relay.DESELECTED_FEEDBACK)]:
313
+ return None
314
+ if select == CHANNEL_COILS[RfChannel.LOW_Z]:
315
+ return RfChannel.LOW_Z
316
+ return RfChannel.HIGH_Z
317
+
318
+
319
+ def _decode_feedback_resistor(
320
+ levels: tuple[bool, ...], channel: RfChannel | None
321
+ ) -> float | None:
322
+ """Return the single selected ``Rf`` of the selected channel, else ``None``."""
323
+ if channel is None:
324
+ return None
325
+ engaged = [
326
+ relay for relay in RF_CHANNEL if relay_engaged(relay, levels[coil_index(relay)])
327
+ ]
328
+ if len(engaged) != 1:
329
+ return None
330
+ relay = engaged[0]
331
+ if RF_CHANNEL[relay] is not channel:
332
+ return None
333
+ return _OHM_BY_RELAY[relay]
334
+
335
+
336
+ def _decode_boost_gain(levels: tuple[bool, ...]) -> float | None:
337
+ """Return the one-hot booster gain, ``1.0`` for none and ``None`` for two."""
338
+ engaged = [
339
+ relay
340
+ for relay in _GAIN_BY_RELAY
341
+ if relay_engaged(relay, levels[coil_index(relay)])
342
+ ]
343
+ if not engaged:
344
+ return 1.0
345
+ if len(engaged) > 1:
346
+ return None
347
+ return _GAIN_BY_RELAY[engaged[0]]