KM3NeT CLB  2.0
KM3NeT CLB v2 Embedded Software
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
sub_sys.c
1 /*
2  * KM3NeT CLB v2 Firmware
3  * ----------------------
4  *
5  * Copyright 2013-2016 KM3NeT Collaboration
6  *
7  * All Rights Reserved.
8  *
9  *
10  * File : sub_sys.c
11  * Created : 6 jan. 2014
12  * Author : Vincent van Beveren
13  */
14 
15 #include "clbstate.h"
16 
17 #include "stdlib.h"
18 
19 #include "util/log.h"
20 #include "types.h"
21 
22 #include "drv/wb/wrx.h"
23 #include "drv/wb/ticks.h"
24 #include "drv/wb/stmach.h"
25 #include "drv/wb/mboot.h"
26 #include "drv/wb/xadc.h"
27 #include "drv/wb/ipmux.h"
28 #include "drv/i2c/ahrs.h"
29 
30 #include "kernel/sys.h"
31 #include "kernel/scheduler.h"
32 #include "kernel/errata.h"
33 #include "modules/power.h"
34 #include "modules/wltune.h"
35 #include "lm32soc/lm32.h"
36 #include "kernel/tm.h"
37 // breaks layering... but alas
38 #include "drv/wb/tdc.h"
39 #include "drv/wb/acou.h"
40 
41 #include "subsys/sub_sys.h"
42 #include "subsys/sub_ins.h"
43 #include "errtrack.h"
44 
45 #include "pv/access.h"
46 #include "pv/vars.h"
47 
48 static ResetType _armResetType = resetTypeNone;
49 static int _armResetImg = -1;
50 
51 // #define FLUSH_ON_PAUSE
52 
53 LOG_DEF(SubSystem)
54 
55 #define MON_FLAGS_AHRS_VALID 0x1 // AHRS data is valid
56 #define MON_TSLICE_DUR 0x2 // Contains timeslice duration
57 
58 typedef struct {
59  uint32_t flags;
60  CompassData ahrs;
61  int16_t temp;
62  uint16_t humid;
63  uint32_t tdcFull;
64  uint32_t aesFull;
65  uint32_t flushCount;
66  uint32_t tSliceDur; // XXX added 2017-04-24
67  uint16_t tdcStCommit; // XXX added 2018-08-02
68  uint16_t aesStCommit; // XXX added 2018-08-02
69 } MonData;
70 
71 static MonData _monData;
72 
73 static uint32_t _tdcFull;
74 static uint32_t _aesFull;
75 static uint32_t _flushCount;
76 
77 
78 static volatile bool _ofListen = false;
79 
80 #ifndef BASE
81 static uint32_t _ofTdcEnabledCh;
82 #endif
83 static uint32_t _ofStmEnabledCh;
84 
85 void _stmachOverflowBegin(uint32_t tdcFifo, uint32_t aesFifo)
86 {
87  // should not listen to overflow
88  if (!_ofListen) return;
89 
90  _tdcFull |= tdcFifo;
91  _aesFull |= aesFifo;
92  _flushCount++;
93 
94 
95  logWarn("State-machine overflow, TDC=%08x, AES=%08x", tdcFifo, aesFifo);
96  uint32_t stmFlush = 0;
97  uint32_t ipmFlush = 0;
98  _ofStmEnabledCh = stmachEnabled();
99 
100 
101 #ifndef BASE
102  if (tdcFifo != 0)
103  {
104  ipmFlush |= IPM_CH_0;
105  stmFlush |= STMACH_CH_TDC;
106  // retreive enabled channels, and disable all
107  _ofTdcEnabledCh = tdcChannels();
108  tdcDisableAll();
109  }
110 #endif
111 
112 
113  if (aesFifo != 0) {
114  stmFlush |= STMACH_CH_AES;
115  ipmFlush |= IPM_CH_1;
116  acouOff();
117  }
118 
119  // disable channels to flush
120  stmachEnable(_ofStmEnabledCh & ~stmFlush);
121 
122  // flush channels
123  stmachFlush(stmFlush, STMACH_FLUSH_OP_START);
124 
125  // flush IP Mux
126  ipMuxFlush(ipmFlush, IPMUX_FLUSH_OP_START);
127 
128 }
129 
130 void _stmachOverflowEnd(uint32_t tdcFifo, uint32_t aesFifo)
131 {
132  if (!_ofListen) return;
133 
134  // flush IP Mux
135  ipMuxFlush(0, IPMUX_FLUSH_OP_END);
136 
137  // flush channels
138  stmachFlush(0, STMACH_FLUSH_OP_END);
139 
140 
141  // enable channels again
142  stmachEnable(_ofStmEnabledCh);
143 
144 
145 #ifndef BASE
146  // enable TDCs
147  if (tdcFifo != 0)
148  {
149  tdcSetChannels(_ofTdcEnabledCh, TDC_CH_ALL_MASK);
150  }
151 #endif
152 
153  // enable AES
154  if (aesFifo != 0)
155  {
156  acouOn();
157  }
158 }
159 
161 {
162  if (clbState(clbSys2Idx[CLB_SUB_SYS]) < clbStateReady) return;
163  _monData.flags = ins.ahrs_valid ? MON_FLAGS_AHRS_VALID : 0;
164  _monData.flags|= MON_TSLICE_DUR;
165  memcpy(&_monData.ahrs, &insCompassData, sizeof(insCompassData));
166  _monData.humid = ins.humid;
167  _monData.temp = ins.temp;
168  _monData.tdcFull = _tdcFull;
169  _monData.aesFull = _aesFull;
170  _monData.flushCount = _flushCount;
171  _monData.tSliceDur = sys.time_slice_dur;
172  stmachGetStCommit(&_monData.tdcStCommit, &_monData.aesStCommit);
173 
174 
175  //logInfo("TDC commit=%d, AES commit=%d", _monData.tdcStCommit, _monData.aesStCommit);
176  //if (_flushCount > 0) logInfo("Flushes per second: %d", _flushCount);
177  _tdcFull = 0;
178  _aesFull = 0;
179  //_flushCount = 0;
180 }
181 
182 void _subsSysExecEvent (ClbEvent event)
183 {
184  switch (event)
185  {
186  case clbEventBoot:
187 
188 
189 #ifdef WHITERABBIT
190 
191 
192  // Initialize WhiteRabbit eXchange.
193  if (!wrxInit())
194  {
195  _clbStateModErr(clbSys2Idx[CLB_SUB_SYS]);
196  return;
197  } else {
198  // use part of MAC as ID
199  // this can be done because the MAC address is derived from a serial number
200  // burned inside a temperature sensor (yes, really). The selected bytes
201  // are supposed to be guaranteed unique.
202 
203  volatile WrxInfo * info = wrxInfo();
204 
205  if (!timeOutWaitFor((uint32_t *)& (info->status),
206  WRX_STATUS_MAC_ADDRESS_VALID,
207  WRX_STATUS_MAC_ADDRESS_VALID, 2000)) return;
208 
209 
210  sys.dom_id =
211  ( info->macAddr[2] << 24 ) |
212  ( info->macAddr[3] << 16 ) |
213  ( info->macAddr[4] << 8 ) |
214  ( info->macAddr[5] );
215 
216  errataInit(sys.dom_id);
217 
218  if (errataActive(ERRATA_TEST)) {
219  logInfo("Test Errata active!");
220  }
221 
222  }
223 #else
224  sys.dom_id = 0xDEADBEEF;
225 #endif
226  /* no break */
227  case clbEventReset:
228  xsLockConfig = false;
229  srand(sys.dom_id);
230  // HACK: set sensible defaults, for now - replace by no defaults? PRR recommendation
231  sys.stmach_pktsize = STMACH_PL_SIZE_MAX; // payload of udp jumbo packet
232  sys.time_slice_dur = 100000; // slice duration is in us. (100ms)
233  sys.sw_date_rev = sysSwDateRev(); // BugFix: Thanks Mieke :)
236 
237  /* Initialize state-machine with DOM ID and monitoring channel set-up */
238  if (!stmachInit(sys.dom_id, &_monData, sizeof(_monData))) return;
239  stmachMUCfg(1000);
240  if (!stmachEnable(0)) {
241  errPrint(true);
242  }
243  if (!etIsDisabled(SYS_SYS_DISABLE_PWR_MEAS)) {
244  if (!pwrInit()) break;
245  }
246  break;
247  case clbEventInit:
248  break;
249  case clbEventConfigure:
250  // if the state change can not be made, return immediately with an error
252 
253 
254  xsLockConfig = true; // lock changing the parameters
255  break;
256  case clbEventStart:
257 
259  logInfo("Starting run %d", sys.run_number);
260  /* no break */
261  case clbEventContinue:
262  _ofListen = true;
263  stmachFlush(STMACH_CH_AES | STMACH_CH_TDC | STMACH_CH_MCH, STMACH_FLUSH_OP_TOGGLE);
264  int ena = 0;
265 #ifndef BASE
267 #endif
270  stmachEnable(ena);
271  break;
272  case clbEventPause:
273  _ofListen = false;
274  stmachEnable(0);
275  // clears the flushed state if the pause happened to be *exactly* during a overflow condition.
276  stmachFlush(STMACH_CH_AES | STMACH_CH_TDC | STMACH_CH_MCH, STMACH_FLUSH_OP_END);
277 #ifdef FLUSH_ON_PAUSE
278  // flush all hardware channels on pause, done after STMACH is paused.
279  stmachFlush(STMACH_CH_AES | STMACH_CH_TDC | STMACH_CH_MCH, STMACH_FLUSH_OP_TOGGLE);
280  ipMuxFlush(IPM_CH_HW, IPMUX_FLUSH_OP_TOGGLE);
281 #endif
282  break;
283  case clbEventStop:
284  /* no break */
285  case clbEventQuit:
286  xsLockConfig = false; // unlock changing the parameters
287  break;
288  default:
289  break;
290  }
291  _clbStateUpdate(clbSys2Idx[CLB_SUB_SYS], event, 0);
292 }
293 
294 
295 
296 #define TCONV(X) (X*503975/4096-273150)/10
297 
298 bool _subsSysUpdate(ClbState state, uint32_t time)
299 {
300  // handle resets
301  // -------------
302  switch (_armResetType) {
303  case resetTypeHard:
304  if (state != clbStateIdle) break;
305  /* no break */
306  case resetTypeHardForce:
307  // FIX 2019-3-29: boot command does not boot correct imaage.
308  if (_armResetImg == -1 || _armResetImg >= FLASH_MAX_IMAGES ) _armResetImg = MBOOT_GOLDEN;
309  mbootLoad(_armResetImg);
310  break;
311  case resetTypeSoft:
312  if (state == clbStateIdle) sysReboot();
313  break;
314  default:
315  break;
316  }
317 
318 
319 #ifndef BASE
320  if (!etIsDisabled(SYS_SYS_DISABLE_PWR_MEAS)) {
321  if (!pwrMeasureAll(sys.pwr_meas)) {
322  // Present failure
323  // errPrint(true);
324  etCheckFailure(SYS_SYS_FAIL_PWR_MEAS);
325  memset(sys.pwr_meas, 0, sizeof(sys.pwr_meas));
326  return false;
327  }
328  } else {
329  // clear it
330  memset(sys.pwr_meas, 0, sizeof(sys.pwr_meas));
331  }
332 #endif
333 
334 
335  sys.fpga_temp = TCONV(xadcGetMeas(xadcChanTemp, xadcTypeCur));
341 
342  volatile WrxInfo * info = wrxInfo();
343 
344  if (info != NULL)
345  {
346  sys.sys_temp_hum[SYS_SYS_TEMP_HUM_WR] = info->brdTemp * 100 + info->brdTempFrac;
347  sys.sys_temp_hum[SYS_SYS_TEMP_HUM_SFP] = info->sfpTemp * 100
348  + (info->sfpTempFrac * 100) / 256;
349  } else {
352  }
353 
355 
356 /*
357  for (int i = 0; i < sizeof(sys.pwr_meas)/sizeof(sys.pwr_meas[0]); i++) {
358  printf("%d;", sys.pwr_meas[i]);
359  }
360  for (int i = 0; i < sizeof(sys.sys_temp_hum)/sizeof(sys.sys_temp_hum[0]); i++) {
361  printf("%d;", sys.sys_temp_hum[i]);
362  }
363  puts("");
364 */
365  return true;
366 }
367 
368 
369 
370 void sysArmReset(ResetType rTyp, int imgNo)
371 {
372  _armResetType = rTyp;
373  _armResetImg = imgNo;
374 }
375 
376 
377 
uint32_t stmachEnabled()
Returns which state-machine channels are enabled.
Definition: stmach.c:144
bool stmachInit(uint32_t domId, void *monbuf, size_t monlen)
Initializes the state-machine with the DOM ID and the monitoring channel CPU data pointer...
Definition: stmach.c:72
bool stmachEnable(uint32_t enable)
Enables one or more STMACH channels.
Definition: stmach.c:127
#define SYS_SYS_TEMP_HUM_WR
Array index of Power board temperature (MAX123)
Definition: vars.h:76
uint32_t hw_date_rev
Hardware revision (YYMMDDBB hex)
Definition: vars.h:118
#define MBOOT_GOLDEN
The golden image index position.
Definition: mboot.h:31
Ready state.
Definition: clbstate.h:64
This module provides access to the peripherals on the power board.
#define SYS_SYS_RUN_ENA_ACS
Bit which enables Acoustics during run.
Definition: vars.h:90
This driver is to read and configure the AHRS I2C sensor.
uint16_t stmach_pktsize
Max packet size in bytes as chopped by the HW-StateMachine.
Definition: vars.h:124
Instrumentation subsystem.
Idle state.
Definition: clbstate.h:62
Running =&gt; Paused.
Definition: clbstate.h:95
ClbEvent
All state change events.
Definition: clbstate.h:88
State Machine Driver.
uint32_t time_slice_dur
Timeslice duration in microseconds.
Definition: vars.h:115
void mbootLoad(int imgNo)
Boots a specific image.
Definition: mboot.c:38
uint32_t sysHwDateRev()
Returns the Hardware date revision.
Definition: sys.c:386
void acouOn()
Turns the hydrophone on.
Definition: acou.c:30
static void _clbStateModErr(int idx)
Invoked by subsystem to indicate an error happened.
Definition: clbstate.h:189
uint16_t humid
Humidity in 1/100th RH.
Definition: vars.h:437
#define STMACH_CH_AES
Bit of state machine AES channel, relative.
Definition: dev_stmach.h:42
uint16_t aes_ts_fifoc
AES timestamp fifo count.
Definition: vars.h:143
uint16_t pwr_meas[18]
Power measurement readings.
Definition: vars.h:127
#define SYS_SYS_TEMP_HUM_CLB
Array index of Temperature (SHT21)
Definition: vars.h:73
void _stmachOverflowEnd(uint32_t tdcFifo, uint32_t aesFifo)
Invoked when an AES or TDC fifo should be enabled again.
Definition: sub_sys.c:130
Provides access to all variables of the various subsystems.
Low level routines for LM32, including interrupt handling.
White Rabbit simple timer &#39;Ticks&#39; driver.
IPMUX Driver for CLBv2.
ClbState clbState(int idx)
Returns the current clbSubState for the specified subsystem.
Definition: clbstate.c:92
void stmachFlush(uint32_t flush, uint8_t op)
Flushes one or more state-machine channels.
Definition: stmach.c:113
System start up and management.
void sysReboot()
Reboot the system.
Definition: sys.c:515
uint32_t tdcChannels()
Returns which TDC channels are enabled/disabled.
Definition: tdc.c:49
Ready =&gt; Running.
Definition: clbstate.h:94
void stmachMUCfg(uint32_t intervalMs)
Configure Monitor Update.
Definition: stmach.c:90
Paused =&gt; Running.
Definition: clbstate.h:96
XADC Driver.
int16_t sys_temp_hum[6]
System temperatures and humidity summery, depreciated All values in 1/100th of a degree or 1/100th of...
Definition: vars.h:131
Simple task scheduler for tasks.
#define STMACH_CH_TDC
Bit of machine TDC channel, relative.
Definition: dev_stmach.h:41
void ipMuxFlush(uint32_t channelMask, uint8_t ipmFlushOp)
Flushes the IPMUX&#39;s FIFO&#39;s.
Definition: ipmux.c:191
bool wrxInit()
Initializes the whiteRabbit eXchange.
Definition: wrx.c:34
int16_t power_temp
Power board temperature in 1/100th of a degree.
Definition: vars.h:137
Multiboot Driver, exposes one function, mbootLoad.
CompassData insCompassData
Provides the AHRS data.
Definition: sub_ins.c:58
void _stmachUpdateMonitor()
Stub function invoked when it is time to update the monitor channel.
Definition: sub_sys.c:160
uint8_t clbSys2Idx[6]
Mapping from subsystem ID to index.
Definition: clbstate.c:59
#define SYS_SYS_RUN_ENA_MON
Bit which enables Monitoring during run.
Definition: vars.h:91
#define logWarn(MSG,...)
Format a log message with warning level.
Definition: log.h:219
bool pwrMeasureAll(uint16_t *results)
Initiates a conversion for the given channel.
Definition: power.c:73
uint32_t run_number
The current run number 20160704 Made run number configurable.
Definition: vars.h:109
Structure defines data from a compass/tilt/gyro sensor.
Definition: types.h:22
static void stmachSetRunNo(uint32_t runNo)
Sets the run-number.
Definition: stmach.h:84
static bool timeOutWaitFor(uint32_t *flags, uint32_t mask, uint32_t result, uint32_t msec)
Function to wait for a specific flag to be set or cleared.
Definition: tm.h:95
sys_t sys
Provides access to all process variables of subsystem System.
Definition: vars.c:8
uint16_t tdc_ts_fifoc
TDC timestamp fifo count.
Definition: vars.h:140
ClbState
Various states.
Definition: clbstate.h:59
Soft reset (Software only)
Definition: sub_sys.h:31
void acouOff()
Truns the hydrohpone off.
Definition: acou.c:39
int16_t temp
Temperature in 1/100th of a degree.
Definition: vars.h:434
void sysArmReset(ResetType rTyp, int imgNo)
Resets the CLB.
Definition: sub_sys.c:370
Undefined =&gt; Idle, for internal use only.
Definition: clbstate.h:91
void errPrint(bool clear)
Prints the last error.
Definition: err.c:79
bool stmachConfig(uint32_t packSize, uint32_t duration)
Configures the stateMachine.
Definition: stmach.c:98
uint32_t sw_date_rev
Software revision (YYMMDDBB hex)
Definition: vars.h:121
bool ahrs_valid
Compass data are valid.
Definition: vars.h:443
#define SYS_SYS_FAIL_PWR_MEAS
Bit which disables the power (voltage/current) measurement.
Definition: vars.h:102
bool xsLockConfig
Variable to lock or unlock writing of configuration variables.
void _stmachOverflowBegin(uint32_t tdcFifo, uint32_t aesFifo)
Invoked when an AES or TDC fifo overflow occurs.
Definition: sub_sys.c:85
#define SYS_SYS_TEMP_HUM_CLB_HUM
Array index of Humidity (SHT21)
Definition: vars.h:74
Force, reset at any point, select image, 0 - golden.
Definition: sub_sys.h:33
#define TDC_CH_ALL_MASK
All channels.
Definition: tdc.h:27
StandBy =&gt; Idle.
Definition: clbstate.h:99
Simple timer functions.
volatile WrxInfo * wrxInfo()
Returns the whiteRabbit information structure if available, else NULL.
Definition: wrx.c:63
TDC Driver.
Hard reset, but only if idle, select image, 0 - golden.
Definition: sub_sys.h:32
uint8_t sys_run_ena
Run-state Enable mask.
Definition: vars.h:149
int16_t fpga_temp
FPGA core temperature in 1/100th of a degree.
Definition: vars.h:134
#define SYS_SYS_DISABLE_PWR_MEAS
Bit which disables the power (voltage/current) measurement.
Definition: vars.h:87
#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...
static void tdcDisableAll()
Disables all channels.
Definition: tdc.h:104
uint32_t sysSwDateRev()
Returns the Software date revision.
Definition: sys.c:391
ins_t ins
Provides access to all process variables of subsystem Instrumentation.
Definition: vars.c:212
Ready =&gt; StandBy.
Definition: clbstate.h:98
uint32_t dom_id
DOM identifier.
Definition: vars.h:112
Idle =&gt; StandBy.
Definition: clbstate.h:92
ResetType
Reset the CLB.
Definition: sub_sys.h:29
bool pwrInit()
Initializes the powerboard.
Definition: power.c:63
StandBy =&gt; Ready.
Definition: clbstate.h:93
#define SYS_SYS_RUN_ENA_TDC
Bit which enables TDC during run.
Definition: vars.h:89
Implements a generic logger facility.
Paused =&gt; StandBy.
Definition: clbstate.h:97
Access provides &#39;introspective&#39; access to process variables.
#define SYS_SYS_TEMP_HUM_PWR
Array index of Power board temperature (MAX123)
Definition: vars.h:75
System subsystem.
WhiteRabbit exchange exchanges information between the 2nd LM32 and WhiteRabbit though a small client...
#define SYS_PWR_MEAS_TEMP_LVL
Array index of Temperature sensor voltage, in millivolts.
Definition: vars.h:63
Don&#39;t reset.
Definition: sub_sys.h:30
#define logInfo(MSG,...)
Write a log message with formatting on info level.
Definition: log.h:202
void _clbStateUpdate(int idx, ClbEvent event, uint8_t status)
Invoked by the subsystem to indicate a state change has happened.
Definition: clbstate.c:204
#define SYS_SYS_TEMP_HUM_SFP
Array index of SFP Transceiver temperature.
Definition: vars.h:77
#define SYS_SYS_TEMP_HUM_FPGA
Array index of FPGA core temperature.
Definition: vars.h:72
Acoustic Driver.
void stmachGetStCommit(uint16_t *tdc, uint16_t *aes)
Debug feature: Get TDC and AES state machine Fifo commits.
Definition: stmach.c:149
void tdcSetChannels(uint32_t channels, uint32_t mask)
Set the enable/disable channels.
Definition: tdc.c:44
#define STMACH_CH_MCH
Bit of state machine Monitoring channel, relative.
Definition: dev_stmach.h:43