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 
40 static uint32_t _chEnabled = 0;
41 
42 static void optReset()
43 {
44  tdcDisableAll();
47  int i;
48  opt.chan_enable = 0;
49  // create default configuration
50  for (i = 0; i < OPT_CH_COUNT; ++i)
51  {
52  opt.chan_status[i] = 0;
53  opt.chan_config[i] = 0;
54  opt.pmt_highvolt[i] = PRMS_HV_V2DAC(OPT_HV_DEFAULT);
55  opt.pmt_threshold[i] = PRMS_THRS_MV2DAC(OPT_THRS_DEFAULT);
56 
57  }
58 }
59 
60 static void optScan()
61 {
62  int i;
63  tdcDisableAll();
64  for (i = 0; i < OPT_CH_COUNT; ++i)
65  {
66  opt.chan_status[i] = 0;
67  opt.pmt_id[i] = 0xFFFFFFFF;
68 
69  int brd = _OCT_BRD(i);
70  int ch = _OCT_CH(i);
71 
72  if (! ( opt.chan_status[i] & OPT_SYS_ERR_ANY) ) {
73 
74  bool idFault;
75 
76  if (!octoPMTID(brd, ch, &(opt.pmt_id[i]), &idFault)) {
78  errClear();
79  } else if (idFault)
80  {
82  }
83  }
84  }
85 }
86 
87 static bool optConfig()
88 {
89  int i;
90 
91  _chEnabled = 0;
93  tdcDisableAll();
94  for (i = 0; i < OPT_CH_COUNT; ++i)
95  {
96  int brd = _OCT_BRD(i);
97  int ch = _OCT_CH(i);
98 
99  opt.chan_status[i] = 0;
100  if ((opt.chan_enable & BIT(i)) /* BWC */ || (opt.chan_config[i] & 0x40) /* BWC */) {
101  bool chOk = true;
102 
103  if (!octoInitChan(brd, ch, opt.pmt_highvolt[i], opt.pmt_threshold[i])) chOk = false;
104  if ((!(sys.sys_disable & SYS_SYS_DISABLE_HV)) && chOk && !octoHighVoltage(brd, ch, true)) chOk = false;
105  if (chOk) {
106  _chEnabled |= BIT(i);
107 // tdcChEnable(i, true);
109  } else {
110 #ifdef DEBUG
111 // tdcChEnable(i, true);
112  _chEnabled |= BIT(i);
114 #else
116 #endif
117  logError("Failed to initialize channel %d (brd=%s, ch=%d)",
118  i, brd == 0 ? "small" : "large", ch);
119  errPrint(true);
120  }
121  } else {
122  // turn HV off, even it if wasn't on.
123  if (!octoHighVoltage(brd, ch, false)) {
125  errClear();
126  }
127  }
128 
129  }
130  return optUpdate();
131 }
132 
133 
134 static bool optUnConfig()
135 {
136  int i;
137 
138 
139 
140  for (i = 0; i < OPT_CH_COUNT; ++i)
141  {
142  opt.chan_status[i] = 0;
143 
144  int brd = _OCT_BRD(i);
145  int ch = _OCT_CH(i);
146 
147  // always turn it off, even if its not configured.
148  octoHighVoltage(brd, ch, false);
149  octoDeinitChan(brd, ch);
150  if (errHas()) {
151  errPrint(true);
152  }
153  }
154  tdcReset();
155  return true;
156 }
157 
158 
159 bool _showErrOnce = true;
160 
161 bool optUpdate()
162 {
163  uint32_t fault[2] = { 0xFFFFFFFF, 0xFFFFFFFF };
164  bool wasOk[2];
165  int i;
166 
167 
168  // if none of the channels are enabled for a specific octopus board, we do not query it
169  // periodically. It could be broken!
170  if ((opt.chan_enable & _OCT_LARGE_MASK) != 0)
171  {
173  } else
174  {
175  wasOk[OCTO_BRD_LARGE] = true;
176  }
177 
178  if ((opt.chan_enable & _OCT_SMALL_MASK) != 0)
179  {
181  } else
182  {
183  wasOk[OCTO_BRD_SMALL] = true;
184  }
185 
186  if (errHas() && _showErrOnce) {
187  if (!wasOk[OCTO_BRD_LARGE]) logWarn("Octopus board large did not respond!");
188  if (!wasOk[OCTO_BRD_SMALL]) logWarn("Octopus board small did not respond!");
189  // we ignore the octo-state
190  errPrint(false);
191  _showErrOnce = false;
192  }
193  errClear();
194 
195  for (i = 0; i < OPT_CH_COUNT; ++i)
196  {
197  int brd = _OCT_BRD(i);
198  int ch = _OCT_CH(i);
199 
200  if (!wasOk[brd])
201  {
204  } else
205  {
206  if ((fault[brd] & (1 << ch)) == 0)
207  {
209  } else
210  {
212  }
213  }
214  }
215  return true;
216 }
217 
218 bool _subsOptUpdate(ClbState state, uint32_t time)
219 {
220  if (state != clbStateIdle || state != clbStateUndefined) {
221  return optUpdate();
222  }
223  return true;
224 }
225 
226 void _subsOptExecEvent (ClbEvent event)
227 {
228  switch (event)
229  {
230  case clbEventBoot:
231  optReset();
232  // basic config for exclusive mode.
233  opt.nano_delay = 0;
234  opt.nano_length = 4;
235  opt.nano_period = 0xc35;
236  opt.nano_pcount = 100;
237  opt.nano_volt = 30000;
238  opt.nano_enable = 0;
239  opt.hr_veto_thres = 0x7d0;
240  opt.mulhit_ena_ch = 0x7FFFFFF;
241  opt.hr_veto_ena_ch= 0x7FFFFFF;
242  opt.hr_veto_ena_ch= 0x7FFFFFF;
243  opt.min_tot=0x00000004;
244 
245  // sys.sys_disable |= SYS_SYS_DISABLE_HV;
247  pwrSetLED(0);
248  break;
249  case clbEventInit:
250  /* Implement actions to go to standby state */
252  optScan();
253  break;
254  case clbEventConfigure:
255 
257  { // check for nano-beacon
259  return;
260  }
261 
262  if ((!etIsDisabled(SYS_SYS_DISABLE_NANO_STEP_UP))) {
263  if (!pwrSetLED(opt.nano_volt)) {
264  etCheckFailure(SYS_SYS_FAIL_NANO_STEP_UP);
265  return;
266  }
267  // already enables step-up.
269  } else {
270  if (!pwrSetLED(0)) return;
271  }
272 
273  }
274 
275  /* Implement actions to go to configured state */
276  if (!optConfig()) {
277  return;
278  }
279 
283 #ifdef DEBUG
284  if (!(sys.sys_disable & SYS_SYS_DISABLE_HV)) logWarn("** High Voltage enabled! Be careful! **");
285 #endif
286  break;
287  case clbEventStart:
288  case clbEventContinue:
290  tdcSetChannels(_chEnabled, TDC_CH_ALL_MASK);
291  break;
292  case clbEventPause:
293  tdcDisableAll();
295  break;
296  case clbEventStop:
297  // PMTs should not be disabled, so this only happens on the reset event
298  // TDCs are already stopped since we transit from the pause state.
299  if (!pwrSetLED(0)) {
300  logWarn("Failed set LED voltage to 0 on power board");
301  errPrint(true);
302  }
303 
304  break;
305  case clbEventReset:
306  optUnConfig();
307  break;
308  default:
309  break;
310  }
311  _clbStateUpdate(clbSys2Idx[CLB_SUB_OPT], event, 0);
312 }
313 
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
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:161
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