KM3NeT CLB  2.0
KM3NeT CLB v2 Embedded Software
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
sub_opt.c
1 /*
2  * KM3NeT CLB v2 Firmware
3  * ----------------------
4  *
5  * Copyright 2013 KM3NeT Collaboration
6  *
7  * All Rights Reserved.
8  *
9  *
10  * File : sub_opt.c
11  * Created : 6 jan. 2014
12  * Author : Vincent van Beveren
13  */
14 
15 
16 #include "clbstate.h"
17 
18 #include <stdio.h>
19 
20 
21 #include "util/log.h"
22 
23 #include "kernel/err.h"
24 #include "errtrack.h"
25 
26 #include "drv/wb/tdc.h"
27 #include "drv/wb/nano.h"
28 #include "drv/i2c/promis.h"
29 #include "modules/octopus.h"
30 #include "modules/power.h"
31 
32 #include "subsys/sub_opt.h"
33 
34 #include "pv/vars.h"
35 
36 LOG_DEF(Optics)
37 
38 
39 // #define SLOW_ENABLE
40 
41 
42 static uint32_t _chEnabled = 0;
43 
44 static void optReset()
45 {
46  tdcDisableAll();
49  int i;
50  opt.chan_enable = 0;
51  // create default configuration
52  for (i = 0; i < OPT_CH_COUNT; ++i)
53  {
54  opt.chan_status[i] = 0;
55  opt.chan_config[i] = 0;
56  opt.pmt_highvolt[i] = PRMS_HV_V2DAC(OPT_HV_DEFAULT);
57  opt.pmt_threshold[i] = PRMS_THRS_MV2DAC(OPT_THRS_DEFAULT);
58 
59  }
60 }
61 
62 static void optScan()
63 {
64  int i;
65  tdcDisableAll();
66  for (i = 0; i < OPT_CH_COUNT; ++i)
67  {
68  opt.chan_status[i] = 0;
69  opt.pmt_id[i] = 0xFFFFFFFF;
70 
71  int brd = _OCT_BRD(i);
72  int ch = _OCT_CH(i);
73 
74  if (! ( opt.chan_status[i] & OPT_SYS_ERR_ANY) ) {
75 
76  bool idFault;
77 
78  if (!octoPMTID(brd, ch, &(opt.pmt_id[i]), &idFault)) {
80  errClear();
81  } else if (idFault)
82  {
84  }
85  }
86  }
87 }
88 
89 static bool optConfig()
90 {
91  int i;
92 
93  _chEnabled = 0;
95  tdcDisableAll();
96  for (i = 0; i < OPT_CH_COUNT; ++i)
97  {
98  int brd = _OCT_BRD(i);
99  int ch = _OCT_CH(i);
100 
101  opt.chan_status[i] = 0;
102  if ((opt.chan_enable & BIT(i)) /* BWC */ || (opt.chan_config[i] & 0x40) /* BWC */) {
103  bool chOk = true;
104 
105  if (!octoInitChan(brd, ch, opt.pmt_highvolt[i], opt.pmt_threshold[i])) chOk = false;
106  chOk = chOk && octoHighVoltage(brd, ch, !(sys.sys_disable & SYS_SYS_DISABLE_HV));
107 
108  if (chOk)
109  {
110  _chEnabled |= BIT(i);
111 // tdcChEnable(i, true);
113  }
114  else
115  {
116 #ifdef DEBUG
117 // tdcChEnable(i, true);
118  _chEnabled |= BIT(i);
120 #else
122 #endif
123  logError("Failed to initialize channel %d (brd=%s, ch=%d)",
124  i, brd == 0 ? "small" : "large", ch);
125  errPrint(true);
126  }
127  } else {
128  // turn HV off, even it if wasn't on.
129  if (!octoHighVoltage(brd, ch, false))
130  {
132  errClear();
133  }
134  }
135 #ifdef SLOW_ENABLE
136  timeDelay(150);
137 #endif
138  }
139  return optUpdate();
140 }
141 
142 
143 static bool optUnConfig()
144 {
145  int i;
146 
147 
148 
149  for (i = 0; i < OPT_CH_COUNT; ++i)
150  {
151  opt.chan_status[i] = 0;
152 
153  int brd = _OCT_BRD(i);
154  int ch = _OCT_CH(i);
155 
156  // always turn it off, even if its not configured.
157  octoHighVoltage(brd, ch, false);
158  octoDeinitChan(brd, ch);
159  if (errHas()) {
160  errPrint(true);
161  }
162  }
163  tdcReset();
164  return true;
165 }
166 
167 
168 bool _showErrOnce = true;
169 
170 bool optUpdate()
171 {
172  uint32_t fault[2] = { 0xFFFFFFFF, 0xFFFFFFFF };
173  bool wasOk[2];
174  int i;
175 
176 
177  // if none of the channels are enabled for a specific octopus board, we do not query it
178  // periodically. It could be broken!
179  if ((opt.chan_enable & _OCT_LARGE_MASK) != 0)
180  {
182  } else
183  {
184  wasOk[OCTO_BRD_LARGE] = true;
185  }
186 
187  if ((opt.chan_enable & _OCT_SMALL_MASK) != 0)
188  {
190  } else
191  {
192  wasOk[OCTO_BRD_SMALL] = true;
193  }
194 
195  if (errHas() && _showErrOnce) {
196  if (!wasOk[OCTO_BRD_LARGE]) logWarn("Octopus board large did not respond!");
197  if (!wasOk[OCTO_BRD_SMALL]) logWarn("Octopus board small did not respond!");
198  // we ignore the octo-state
199  errPrint(false);
200  _showErrOnce = false;
201  }
202  errClear();
203 
204  for (i = 0; i < OPT_CH_COUNT; ++i)
205  {
206  int brd = _OCT_BRD(i);
207  int ch = _OCT_CH(i);
208 
209  if (!wasOk[brd])
210  {
213  } else
214  {
215  if ((fault[brd] & (1 << ch)) == 0)
216  {
218  } else
219  {
221  }
222  }
223  }
224  return true;
225 }
226 
227 bool _subsOptUpdate(ClbState state, uint32_t time)
228 {
229  if (state != clbStateIdle || state != clbStateUndefined) {
230  return optUpdate();
231  }
232  return true;
233 }
234 
235 void _subsOptExecEvent (ClbEvent event)
236 {
237  switch (event)
238  {
239  case clbEventBoot:
240  optReset();
241  // basic config for exclusive mode.
242  opt.nano_delay = 0;
243  opt.nano_length = 4;
244  opt.nano_period = 0xc35;
245  opt.nano_pcount = 100;
246  opt.nano_volt = 30000;
247  opt.nano_enable = 0;
248  opt.hr_veto_thres = 0x7d0;
249  opt.mulhit_ena_ch = 0x7FFFFFF;
250  opt.hr_veto_ena_ch= 0x7FFFFFF;
251  opt.hr_veto_ena_ch= 0x7FFFFFF;
252  opt.min_tot=0x00000004;
253 
254  // sys.sys_disable |= SYS_SYS_DISABLE_HV;
256  pwrSetLED(0);
257  break;
258  case clbEventInit:
259  /* Implement actions to go to standby state */
261  optScan();
262  break;
263  case clbEventConfigure:
264 
266  { // check for nano-beacon
268  {
269  return;
270  }
271 
272  if ((!etIsDisabled(SYS_SYS_DISABLE_NANO_STEP_UP)))
273  {
274 # ifdef SLOW_ENABLE
275  timeDelay(150);
276 # endif
277  if (!pwrSetLED(opt.nano_volt)) {
278  etCheckFailure(SYS_SYS_FAIL_NANO_STEP_UP);
279  return;
280  }
281 # ifdef SLOW_ENABLE
282  timeDelay(150);
283 # endif
284  // already enables step-up.
286  }
287  else
288  {
289  if (!pwrSetLED(0)) return;
290  }
291 
292  }
293 
294  /* Implement actions to go to configured state */
295  if (!optConfig())
296  {
297  return;
298  }
299 
303 #ifdef DEBUG
304  if (!(sys.sys_disable & SYS_SYS_DISABLE_HV)) logWarn("** High Voltage enabled! Be careful! **");
305 #endif
306  break;
307  case clbEventStart:
308  case clbEventContinue:
310  tdcSetChannels(_chEnabled, TDC_CH_ALL_MASK);
311  break;
312  case clbEventPause:
313  tdcDisableAll();
315  break;
316  case clbEventStop:
317  // PMTs should not be disabled, so this only happens on the reset event
318  // TDCs are already stopped since we transit from the pause state.
319  if (!pwrSetLED(0)) {
320  logWarn("Failed set LED voltage to 0 on power board");
321  errPrint(true);
322  }
323 
324  break;
325  case clbEventReset:
326  optUnConfig();
327  break;
328  default:
329  break;
330  }
331  _clbStateUpdate(clbSys2Idx[CLB_SUB_OPT], event, 0);
332 }
333 
bool errHas()
Returns whether there is an error pending.
Definition: err.c:52
This module implements the functionality required by the octopus board (large).
This module provides access to the peripherals on the power board.
bool nanoConfig(uint32_t delay, uint32_t period, uint32_t width, uint32_t noOfPulses)
Configures the nano-beacon.
Definition: nano.c:22
bool octoHighVoltage(int brd, int ch, bool hvOn)
Turns the high-voltage on or off.
Definition: octopus.c:221
Idle state.
Definition: clbstate.h:62
Running =&gt; Paused.
Definition: clbstate.h:95
ClbEvent
All state change events.
Definition: clbstate.h:88
void tdcReset()
Resets the TDC to defaults.
Definition: tdc.c:20
void tdcSetMultiHitEnabled(uint32_t channels)
Sets on which channels the mutli-hit veto must be enabled.
Definition: tdc.c:70
uint16_t nano_pcount
Nano-beacon pulse train count, each second.
Definition: vars.h:362
uint32_t chan_enable
Channel enable, bit per channel.
Definition: vars.h:338
uint32_t hr_veto_ena_ch
High-rate veto enable, bit per channel.
Definition: vars.h:341
Provides access to all variables of the various subsystems.
#define BIT(N)
Makes a value with the specified bit set.
Definition: macro.h:108
#define OPT_STS_ERR_OVERCUR
Over current.
Definition: sub_opt.h:44
#define SYS_SYS_RUN_ENA_NANO
Bit which enables Nano-Beacon during run.
Definition: vars.h:92
void tdcSetMinTot(uint32_t m_tot)
Sets the HR veto rate per time-slice.
Definition: tdc.c:29
Ready =&gt; Running.
Definition: clbstate.h:94
Paused =&gt; Running.
Definition: clbstate.h:96
#define SYS_SYS_DISABLE_HV
Bit which disables High Voltage.
Definition: vars.h:85
uint16_t nano_length
Length of pulse in 16 ns ticks, default is 4 ticks.
Definition: vars.h:350
void octoResetBoard(int brd)
Definition: octopus.c:100
uint32_t mulhit_ena_ch
Multi-hit enable, bit per channel.
Definition: vars.h:344
uint16_t nano_period
Period of beacon in 16 ns ticks, default is 0xC35 ticks, or 50us.
Definition: vars.h:353
#define OPT_STS_WARN_ID
ID is inconsistent.
Definition: sub_opt.h:46
void timeDelay(uint32_t msec)
Simple busy-wait delay.
Definition: tm.c:18
bool octoInitChan(int brd, int ch, uint8_t highVolt, uint8_t threshold)
Initializes a channel on the octopus board.
Definition: octopus.c:140
Nanobeacon driver.
bool octoPMTID(int brd, int ch, uint32_t *id, bool *pmtIdFault)
Reads a PMT ID.
Definition: octopus.c:168
uint8_t clbSys2Idx[6]
Mapping from subsystem ID to index.
Definition: clbstate.c:59
uint8_t pmt_highvolt[31]
High voltage settings per channel, 0=PMT at location 0, 31=PMT at location 31 Scaling: 0: -700V...
Definition: vars.h:372
#define logWarn(MSG,...)
Format a log message with warning level.
Definition: log.h:219
static void nanoEnable(NanoEnable nanoEnable)
Enable or disable the nano-beacon.
Definition: nano.h:69
uint8_t chan_status[31]
Channel status, 0=PMT at location 0, 31=PMT at location 31.
Definition: vars.h:368
nanoEnableStepUp
Definition: nano.h:38
#define PRMS_HV_V2DAC(V)
Converts the high voltage value into the high voltage DAC byte value.
Definition: promis.h:65
#define OCTO_BRD_SMALL
Octo small.
Definition: octopus.h:39
sys_t sys
Provides access to all process variables of subsystem System.
Definition: vars.c:8
#define OCTO_BRD_LARGE
Octo large.
Definition: octopus.h:40
ClbState
Various states.
Definition: clbstate.h:59
void tdcSetHRVeto(uint32_t thrs)
Sets the Minimun ToT width allowed.
Definition: tdc.c:33
#define SYS_SYS_FAIL_NANO_STEP_UP
Bit which disables the Nano-beacon step-up regulator.
Definition: vars.h:101
Manages the global system error.
uint32_t min_tot
Min ToT.
Definition: vars.h:347
uint32_t hr_veto_thres
High-rate veto (hits per timeslice)
Definition: vars.h:335
uint8_t sys_disable
Disable parts of the system.
Definition: vars.h:146
Undefined =&gt; Idle, for internal use only.
Definition: clbstate.h:91
void errPrint(bool clear)
Prints the last error.
Definition: err.c:79
uint8_t chan_config[31]
Channel configuration (depricated, use CHAN_ENABLE), 0=PMT at location 0, 31=PMT at location 31...
Definition: vars.h:365
#define TDC_CH_ALL_MASK
All channels.
Definition: tdc.h:27
StandBy =&gt; Idle.
Definition: clbstate.h:99
void errClear()
Clears the current error.
Definition: err.c:46
bool octoDeinitChan(int brd, int ch)
De-Initializes a channel on the octopus board.
Definition: octopus.c:130
TDC Driver.
#define OPT_STS_ENABLED
Enabled.
Definition: sub_opt.h:48
bool optUpdate()
Updates the status of the optics.
Definition: sub_opt.c:170
uint8_t sys_run_ena
Run-state Enable mask.
Definition: vars.h:149
void tdcSetHrvEnabled(uint32_t channels)
Sets on which channels the high-rate veto must be applied.
Definition: tdc.c:60
#define SYS_SYS_DISABLE_NANO_STEP_UP
Bit which disables the Nano-beacon step-up regulator.
Definition: vars.h:86
Optics subsystem.
#define logError(MSG,...)
Format a log message with fatal level.
Definition: log.h:232
#define LOG_DEF(NAME,...)
Define a logger for a module.
Definition: log.h:129
The CLB stare module tracks is responsible for state management of the various sub-systems on the CLB...
opt_t opt
Provides access to all process variables of subsystem Optics.
Definition: vars.c:144
bool octoReadFault(int brd, uint32_t *fault)
Reads the fault register of the CLPD.
Definition: octopus.c:210
static void tdcDisableAll()
Disables all channels.
Definition: tdc.h:104
uint16_t nano_volt
Nano-beacon voltage in millivolts, 0-30000V.
Definition: vars.h:356
uint16_t nano_delay
Nano-beacon delay in 16 ns ticks.
Definition: vars.h:359
This driver interfaces with the PROMiS PMT ASIC.
Idle =&gt; StandBy.
Definition: clbstate.h:92
nanoEnableCore
Definition: nano.h:37
#define PRMS_THRS_MV2DAC(MV)
Converts a threshold value in milliVolts to the DAC value.
Definition: promis.h:45
StandBy =&gt; Ready.
Definition: clbstate.h:93
nanoEnableNone
Definition: nano.h:36
bool pwrSetLED(uint32_t mv)
Set the LED output in millivolts.
Definition: power.c:91
Implements a generic logger facility.
Paused =&gt; StandBy.
Definition: clbstate.h:97
uint32_t pmt_id[31]
All PMT ID&#39;s, 0=PMT at location 0, 31=PMT at location 31.
Definition: vars.h:332
uint8_t pmt_threshold[31]
Threshold settings per channel, 0=PMT at location 0, 31=PMT at location 31 Scaling: 0: 800mV...
Definition: vars.h:376
void _clbStateUpdate(int idx, ClbEvent event, uint8_t status)
Invoked by the subsystem to indicate a state change has happened.
Definition: clbstate.c:204
bool nano_enable
Enable nano-beacon (deprecated, see SYS_RUN_ENA)
Definition: vars.h:379
Undefined state (prior to module init, should never be seen on shore)
Definition: clbstate.h:61
nanoEnableCoreAndStepUp
Definition: nano.h:39
#define OPT_STS_ERR_COMM
I2C communication error.
Definition: sub_opt.h:43
void tdcSetChannels(uint32_t channels, uint32_t mask)
Set the enable/disable channels.
Definition: tdc.c:44