KM3NeT CLB  2.0
KM3NeT CLB v2 Embedded Software
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
wltune.c
1 /*
2  * KM3NeT CLB v2 Firmware
3  * ----------------------
4  *
5  * Copyright 2012-2017 KM3NeT Collaboration
6  *
7  * All Rights Reserved.
8  *
9  *
10  * File : wltune.c
11  * Created : 15 sep. 2017
12  * Author : Vincent van Beveren
13  */
14 
15 #include "modules/wltune.h"
16 #include "errorcode.h"
17 #include "kernel/err.h"
18 #include "util/log.h"
19 
20 LOG_DEF(WlTune);
21 
22 
23 static bool _initialized = false;
24 static int _timeout = 0; // shared timeout variable
25 static int32_t _lastTune = -1;
26 
27 WrxTuneInfo wlTuneInfo;
28 WlTuneState wlTuneState;
29 bool wlLastTuneNoAck;
30 
31 
32 static const char * MODES[] = { "not possible", "eoptolink", "OESolutions", "simulated" };
33 
34 void wltune_show_info()
35 {
36  logInfo("Tuning procedure is %s (%d), state=%d, to=%d", MODES[wlTuneInfo.tuneproc], wlTuneInfo.tuneproc, wlTuneState, _timeout);
37  if (wlTuneInfo.tuneproc == 0) return;
38  logInfo("Tuning value: %d, Laser temperature: %lu", wlTuneInfo.tuneword, wlTuneInfo.laserTmpWl);
39 }
40 
41 bool wltune_init()
42 {
43  if (wltune_update_info()) {
44  wltune_show_info();
45  if (wlTuneInfo.tuneproc == 0) return true;
46  wlTuneState = WL_TUNE_STATE_IDLE;
47  } else {
48  wlTuneState = WL_TUNE_STATE_ERROR;
49  return false;
50  }
51 
52  _initialized = true;
53  return true;
54 }
55 
56 
57 
58 /**
59  * Request update of tuning information.
60  */
61 bool wltune_update_info()
62 {
63  return wrxGetTuneInfo(&wlTuneInfo);
64 }
65 
66 /**
67  * Set wavelength tune word.
68  */
69 bool wltune_set_word(int32_t t)
70 {
71  if (wlTuneInfo.tuneproc == 0) return errSet(ERROR(E_NOTSUPPORTED));
72  if (!_initialized) return errSet(ERROR(E_INVSTATE));
73  if (wlTuneState != WL_TUNE_STATE_IDLE) return errSet(ERROR(E_INVSTATE));
74 
75  _lastTune = wlTuneInfo.tuneword;
76  if (wrxSetTuneWord(t))
77  {
78  wlLastTuneNoAck = false;
79  wlTuneState = WL_TUNE_STATE_BUSY;
80  _timeout = 2;
81  return true;
82  } else {
83  wlTuneState = WL_TUNE_STATE_ERROR;
84  }
85  return false;
86 }
87 
88 /**
89  * Set acknowledgment. Only invoke if you are 100% sure the laser is in band.
90  */
91 bool wltune_ack()
92 {
93  if (wlTuneState != WL_TUNE_STATE_WAITACK) return errSet(ERROR(E_INVSTATE));
94 
95  _timeout = 0;
96  wlTuneState = WL_TUNE_STATE_IDLE;
97 
98 
99 
100  return true;
101 }
102 
103 /**
104  * Must be called each second.
105  */
106 bool wltune_periodic()
107 {
108  if (wlTuneState != WL_TUNE_STATE_IDLE) logInfo("State=%d, To=%d\n", wlTuneState, _timeout);
109  if (_timeout > 0) _timeout--;
110  switch (wlTuneState)
111  {
112  case WL_TUNE_STATE_BUSY:
113  wltune_update_info();
114  if (_timeout == 0 && wlTuneInfo.laserTmpWl != 65535)
115  {
116 
117  // set alarm/warn thresholds to +/- 1 degree C.
118  uint16_t mint = wlTuneInfo.laserTmpWl > 256 ? wlTuneInfo.laserTmpWl - 256 : 0;
119  uint16_t maxt = wlTuneInfo.laserTmpWl < 65535-256 ? wlTuneInfo.laserTmpWl + 256 : 65535;
120 
121  //uint16_t mint = 0;
122  //uint16_t maxt = 65535;
123 
124  wrxSetSFPThreshold(WRX_SFP_LASER_TEMP_HIGH_ALARM, maxt);
125  wrxSetSFPThreshold(WRX_SFP_LASER_TEMP_HIGH_WARN, maxt);
126  wrxSetSFPThreshold(WRX_SFP_LASER_TEMP_LOW_ALARM, mint);
127  wrxSetSFPThreshold(WRX_SFP_LASER_TEMP_LOW_WARN, mint);
128 
129  if (_lastTune == wlTuneInfo.tuneword)
130  {
131  // in case we're actually reverting the DAC value.
132  wlTuneState = WL_TUNE_STATE_IDLE;
133  } else {
134  _timeout = 15;
135  wlTuneState = WL_TUNE_STATE_WAITACK;
136  }
137  }
138 
139  break;
140  case WL_TUNE_STATE_WAITACK:
141  if (_timeout == 0)
142  {
143  logError("No ack received, reverting DAC value");
144  if (!wrxSetTuneWord(_lastTune)) {
145  wlTuneState = WL_TUNE_STATE_ERROR;
146  return false;
147  }
148  wlTuneState = WL_TUNE_STATE_BUSY;
149  _timeout = 2;
150  wlLastTuneNoAck = true;
151  }
152  break;
153  default:
154  break;
155  }
156  return true;
157 }
bool wrxGetTuneInfo(WrxTuneInfo *info)
Sets the fields of the WrxTuneInfo structure.
Definition: wrx.c:128
#define E_INVSTATE
Generic error: Module is in a state in which.
Definition: errorcode.h:103
Manages the global system error.
bool wrxSetTuneWord(int32_t tuneWord)
Sets the tune-word.
Definition: wrx.c:117
This module is responsible for distributing error codes.
#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
bool errSet(uint32_t code, const char *error, const char *name)
Sets an error.
#define ERROR(CODE,...)
Expands an error code to an error code with a description (if ERROR_W_DESCR is declared).
Implements a generic logger facility.
#define logInfo(MSG,...)
Write a log message with formatting on info level.
Definition: log.h:202
#define E_NOTSUPPORTED
Generic error: not supported.
Definition: errorcode.h:115