KM3NeT CLB  2.0
KM3NeT CLB v2 Embedded Software
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
sub_bse.c
1 /*
2  * KM3NeT CLB v2 Firmware
3  * ----------------------
4  *
5  * Copyright 2013 KM3NeT Collaboration
6  *
7  * All Rights Reserved.
8  *
9  *
10  * File : sub_net.c
11  * Created : 6 jan. 2014
12  * Author : Vincent van Beveren
13  */
14 
15 #include "util/log.h"
16 #include "clbstate.h"
17 #include "pv/vars.h"
18 #include <string.h>
19 
20 #include "drv/uart/edfa-eau.h"
21 #ifdef BPS_V3
22 #include "drv/uart/bps_v3.h"
23 #else
24 #include "drv/uart/bps.h"
25 #endif
26 #include "drv/uart/acdc.h"
27 
28 LOG_DEF(SysBase);
29 
30 void _subsBseExecEvent (ClbEvent event)
31 {
32  switch (event)
33  {
34  case clbEventReset:
35  case clbEventBoot:
36  edfaInit();
37  bpsInit();
38  acdcInit(true);
39  break;
40  default:
41  break;
42  }
43  _clbStateUpdate(clbSys2Idx[CLB_SUB_BSE], event, 0);
44 }
45 
46 
47 
48 
49 
50 static void configureEdfa() {
51  char *returnedString;
52 
53  if(bse.edfa_emi)edfaEmOn();
54  else edfaEmOff();
55 
56  edfaWriteSetpoint(bse.edfa_setp_wr, &returnedString);
57 
58 }
59 
61 {
62  configureEdfa();
63  return true;
64 }
65 
66 bool bseReset()
67 {
68  acdcClearBuffer();
69  // TODO set defaults
70  configureEdfa();
71  return true;
72 }
73 
74 
75 #ifdef BPS_V3
76 static bool _monitorBps()
77 {
78  BpsRdSensAverageAnswV3 sens_mean;
79  BpsRdSensAnswV3 sens_max;
80  BpsAlarmV3 sens_alarm_fired;
81  uint8_t i_loop;
82 
83  // SENSOR_MAXVALUES_GETALL
84  if (!bpsReadSensorMaxV3(&sens_max))
85  {
86  bse.bps_5v_i_max = 0;
87  bse.bps_lbl_i_max = 0;
88  bse.bps_du_i_max = 0;
89  bse.bps_du_irtn_max = 0;
90  bse.bps_v_max = 0;
91  bse.bps_hydro_i_max = 0;
92  bse.bps_mon_theatsink_max = 0;
93  bse.bps_tboard_max =0;
94  bse.bps_alarm_max = 0;
95  return false;
96  }
97 
98  bse.bps_5v_i_max = sens_max.mon_5V_i;
99  bse.bps_lbl_i_max = sens_max.mon_LBL_i;
100  bse.bps_du_i_max = sens_max.mon_DU_i;
101  bse.bps_du_irtn_max = sens_max.mon_DU_irtn;
102  bse.bps_v_max = sens_max.mon_BPS_v;
103  bse.bps_hydro_i_max = sens_max.mon_HYDRO_i;
104  bse.bps_mon_theatsink_max = sens_max.mon_MON_THEATSINK;
105  bse.bps_tboard_max = sens_max.mon_TBOARD;
106 
107 #ifdef BPS_V3_DBG
108  // echo MAX_VAL
109  printf("MAX_VAL: %d ", bse.bps_5v_i_max);
110  printf("%d ", bse.bps_lbl_i_max);
111  printf("%d ", bse.bps_du_i_max);
112  printf("%d ", bse.bps_du_irtn_max);
113  printf("%d ", bse.bps_v_max);
114  printf("%d ", bse.bps_hydro_i_max);
115  printf("%d ", bse.bps_mon_theatsink_max);
116  printf("%d ", bse.bps_tboard_max);
117 #endif
118 
119  bse.bps_alarm_max = (sens_max.mon_ALRMNEG2 & 0x01)<<3 | (sens_max.mon_ALRMNEG1 & 0x01)<<2 | (sens_max.mon_ALRMPOS2 & 0x01)<<1 | sens_max.mon_ALRMPOS1 & 0x01;
120 
121 #ifdef BPS_V3_DBG
122  // echo alarm_max
123  printf("%x \n", bse.bps_alarm_max);
124 #endif
125 
126  // ALARM_FIRED_GET
127  bse.bps_alarm_fired = 0;
128  for(i_loop=0;i_loop<21;i_loop++)
129  {
130  if (!bpsAlarmFiredGetV3(21-i_loop,&sens_alarm_fired))
131  {
132  bse.bps_alarm_fired = 0;
133  return false;
134  }
135  bse.bps_alarm_fired = ( bse.bps_alarm_fired << 1 ) | sens_alarm_fired.alarm_val & 0x01;
136  }
137 
138  // Echo alarm_fired
139 #ifdef BPS_V3_DBG
140  printf("ALARM_FIRED: %x \n",bse.bps_alarm_fired);
141 #endif
142 
143  // SENSOR_AVERAGE_GETALL
144  if (!bpsReadSensorAverageV3(&sens_mean))
145  {
146  bse.bps_5v_i_m = 0;
147  bse.bps_lbl_i_m = 0;
148  bse.bps_du_i_m = 0;
149  bse.bps_du_irtn_m = 0;
150  bse.bps_v_m = 0;
151  bse.bps_hydro_i_m = 0;
152  bse.bps_mon_theatsink_m = 0;
153  bse.bps_tboard_m =0;
154  return false;
155  }
156 
157  bse.bps_5v_i_m = sens_mean.mon_5V_i_M;
158  bse.bps_lbl_i_m = sens_mean.mon_LBL_i_M;
159  bse.bps_du_i_m = sens_mean.mon_DU_i_M;
160  bse.bps_du_irtn_m = sens_mean.mon_DU_irtn_M;
161  bse.bps_v_m = sens_mean.mon_BPS_v_M;
162  bse.bps_hydro_i_m = sens_mean.mon_HYDRO_i_M;
163  bse.bps_mon_theatsink_m = sens_mean.mon_MON_THEATSINK_M;
164  bse.bps_tboard_m = sens_mean.mon_TBOARD_M;
165 
166 #ifdef BPS_V3_DBG
167  // Echo Average Val
168  printf("MEAN_VAL: %d ", bse.bps_5v_i_m);
169  printf("%d ", bse.bps_lbl_i_m);
170  printf("%d ", bse.bps_du_i_m);
171  printf("%d ", bse.bps_du_irtn_m);
172  printf("%d ", bse.bps_v_m);
173  printf("%d ", bse.bps_hydro_i_m);
174  printf("%d ", bse.bps_mon_theatsink_m);
175  printf("%d \n", bse.bps_tboard_m);
176 #endif
177  return true;
178 }
179 
180 #else
181 
182 static bool _monitorBps()
183 {
184  BpsRdSensAnsw sens;
185 
186  if (!bpsReadSensor(&sens)) {
187  bse.bps_iback = 0;
188  bse.bps_i12 = 0;
189  bse.bps_v375 = 0;
190  bse.bps_i375 = 0;
191  bse.bps_v5 = 0;
192  bse.bps_alarm = 0;
193  bse.bps_breaker = 0;
194 
195  return false;
196  }
197 
198  bse.bps_iback = sens.mon_i;
199  bse.bps_i12 = sens.mon1_i12;
200  bse.bps_v375 = sens.mon2_v375;
201  bse.bps_i375 = sens.mon3_i375;
202  bse.bps_v5 = sens.mon4_v375;
203  bse.bps_alarm = sens.alarm;
204  bse.bps_breaker = sens.breaker;
205 
206  return true;
207 
208 }
209 
210 #endif
211 
212 static bool _monitorEdfa()
213 {
214 
215  bool emi,start;
216  char *returnedString;
217 
218  if (!edfaReadSerial(&returnedString)) goto failed;
219  memcpy(bse.edfa_sn, returnedString, 32);
220 
221  bse.edfa_oop = edfaOutputPower();
222  bse.edfa_iop = edfaInputPower();
223  bse.edfa_pumpdc = edfaPumpCur();
224  bse.edfa_temp = edfaTemp();
225  bse.edfa_setp_rd = edfaReadSetpoint();
226 
227  if (!edfaReadStatus(&emi,&start)) goto failed;
228  bse.edfa_emi = emi;
229 
230  return true;
231 failed:
232  bse.edfa_oop = 0;
233  bse.edfa_iop = 0;
234  bse.edfa_pumpdc = 0;
235  bse.edfa_temp = 0;
236  bse.edfa_setp_rd = 0;
237  bse.edfa_emi = 0;
238  return false;
239 }
240 
241 static bool _monitorAcdc()
242 {
243  AcdcFrame frame;
244 
245  // get the latest read-out
246  if (!acdcGetNFrames(&frame, 1)) {
247  bse.acdc_cur = 0;
248  bse.acdc_volt = 0;
249  bse.acdc_temp = 0;
250  return false;
251  }
252  // clear RX fifo so we know we have a fresh reading next time.
253  acdcClearBuffer();
254 
255  bse.acdc_cur = frame.current * ISCALE + IOFF;
256  bse.acdc_volt = frame.volt * VSCALE + VOFF;
257  bse.acdc_temp = frame.temp * TSCALE + TOFF;
258 
259  return true;
260 }
261 
262 
263 #define CAROUSEL_MAX 3
264 static int _carousel = 0;
265 static const uint8_t _DEVICES[] = { BSE_INST_FAIL_EDFA, BSE_INST_FAIL_BPS, BSE_INST_FAIL_ACDC};
266 
267 
268 bool _subsBseUpdate(ClbState state, uint32_t time)
269 {
270  bool s = true;
271 
272  // only execute read-out if the device did not fail
273  if ( ( bse.inst_fail & _DEVICES[_carousel] ) == 0) {
274  switch(_carousel) {
275  case 0: s = _monitorEdfa(); break;
276  case 1: s = _monitorBps(); break;
277  case 2: s = _monitorAcdc(); break;
278  default: break;
279  }
280 // logDebug("Device=%d, result=%d", _carousel, s);
281  // otherwise add it to the instrumentation fail register
282  // so it will not be queried again
283  if (!s)
284  {
285  errPrint(true);
286  bse.inst_fail |= _DEVICES[_carousel];
287  }
288  }
289 
290 
291  _carousel++;
292  if (_carousel == CAROUSEL_MAX) _carousel = 0;
293 
294  return true;
295 }
bool bpsReadSensorMaxV3(BpsRdSensAnswV3 *stat)
return all sensors&#39; max values
Definition: bps_v3.c:857
bool bpsAlarmFiredGetV3(uint8_t ch, BpsAlarmV3 *AlarmFiredGet)
Return flag of one alarm.
Definition: bps_v3.c:1047
BPS Uart driver (.
ClbEvent
All state change events.
Definition: clbstate.h:88
bool bseConfigure()
Base Configure event.
Definition: sub_bse.c:60
static void edfaInit()
Initializes the EDFA.
Definition: edfa-eau.h:63
bool bseReset()
Base Reset event.
Definition: sub_bse.c:66
uint8_t clbSys2Idx[6]
Mapping from subsystem ID to index.
Definition: clbstate.c:59
ClbState
Various states.
Definition: clbstate.h:59
Undefined =&gt; Idle, for internal use only.
Definition: clbstate.h:91
void errPrint(bool clear)
Prints the last error.
Definition: err.c:79
bool bpsReadSensorAverageV3(BpsRdSensAverageAnswV3 *stat)
return all sensors&#39; mean values
Definition: bps_v3.c:773
StandBy =&gt; Idle.
Definition: clbstate.h:99
bool bpsReadSensor(BpsRdSensAnsw *stat)
return sensor values
Definition: bps.c:577
void acdcInit(bool active)
Initializes the ACDC.
Definition: acdc.c:35
ACDC Uart driver (.
#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...
BPS Uart driver (.
static void bpsInit()
Initializes the BPS.
Definition: bps.h:87
EDFA-EAU (30-C3-20-C) Uart driver.
Implements a generic logger facility.
Definition: acdc.h:37
void _clbStateUpdate(int idx, ClbEvent event, uint8_t status)
Invoked by the subsystem to indicate a state change has happened.
Definition: clbstate.c:204