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", "lumentum", "JDSU" };
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 && wlTuneState != WL_TUNE_STATE_ERROR)
74  return errSet(ERROR(E_INVSTATE));
75 
76  _lastTune = wlTuneInfo.tuneword;
77  if (wrxSetTuneWord(t))
78  {
79  wlLastTuneNoAck = false;
80  wlTuneState = WL_TUNE_STATE_BUSY;
81  _timeout = 2;
82  return true;
83  } else {
84  wlTuneState = WL_TUNE_STATE_ERROR;
85  }
86  return false;
87 }
88 
89 /**
90  * Set acknowledgment. Only invoke if you are 100% sure the laser is in band.
91  */
92 bool wltune_ack()
93 {
94  if (wlTuneState != WL_TUNE_STATE_WAITACK) return errSet(ERROR(E_INVSTATE));
95 
96  _timeout = 0;
97  wlTuneState = WL_TUNE_STATE_IDLE;
98 
99  return true;
100 }
101 
102 /**
103  * Must be called each second.
104  */
105 bool wltune_periodic()
106 {
107  if (wlTuneState != WL_TUNE_STATE_IDLE && wlTuneState != WL_TUNE_STATE_ERROR) logInfo("State=%d, To=%d\n", wlTuneState, _timeout);
108  if (_timeout > 0) _timeout--;
109  switch (wlTuneState)
110  {
111  case WL_TUNE_STATE_BUSY:
112  wltune_update_info();
113  if (_timeout == 0)
114  {
115 
116  if (wlTuneInfo.laserTmpWl != 65535 && wlTuneInfo.laserTmpWl != 0)
117  {
118  // set alarm/warn thresholds to +/- 1 degree C.
119  uint16_t mint = wlTuneInfo.laserTmpWl > 256 ? wlTuneInfo.laserTmpWl - 256 : 0;
120  uint16_t maxt = wlTuneInfo.laserTmpWl < 65535-256 ? wlTuneInfo.laserTmpWl + 256 : 65535;
121 
122  //uint16_t mint = 0;
123  //uint16_t maxt = 65535;
124 
125  wrxSetSFPThreshold(WRX_SFP_LASER_TEMP_HIGH_ALARM, maxt);
126  wrxSetSFPThreshold(WRX_SFP_LASER_TEMP_HIGH_WARN, maxt);
127  wrxSetSFPThreshold(WRX_SFP_LASER_TEMP_LOW_ALARM, mint);
128  wrxSetSFPThreshold(WRX_SFP_LASER_TEMP_LOW_WARN, mint);
129  }
130 
131  _timeout = 15;
132  wlTuneState = WL_TUNE_STATE_WAITACK;
133  }
134 
135  break;
136  case WL_TUNE_STATE_WAITACK:
137  if (_timeout == 0)
138  {
139  logError("No ack received, reverting DAC value");
140  if (!wrxSetTuneWord(_lastTune)) {
141  wlTuneState = WL_TUNE_STATE_ERROR;
142  return false;
143  }
144  wlTuneState = WL_TUNE_STATE_BUSY;
145  _timeout = 2;
146  wlLastTuneNoAck = true;
147  }
148  break;
149  default:
150  break;
151  }
152  return true;
153 }
bool wrxGetTuneInfo(WrxTuneInfo *info)
Sets the fields of the WrxTuneInfo structure.
Definition: wrx.c:130
#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:119
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