KM3NeT CLB  2.0
KM3NeT CLB v2 Embedded Software
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
sub_net.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 <assert.h>
16 #include "util/log.h"
17 #include "clbstate.h"
18 #include "subsys/sub_net.h"
19 #include "net/network.h"
20 #include "modules/wltune.h"
21 #include "drv/wb/stmach.h"
22 #include "drv/wb/ipmux.h"
23 #include "drv/wb/wrx.h"
24 #include "pv/vars.h"
25 #include "ms.h"
26 
27 LOG_DEF(SysNet);
28 
29 // if you do not require WR to be in a valid state.
30 #ifdef DEBUG
31 #define IGNORE_WR_STATE
32 #endif
33 
34 // info->wState != WRX_WRS_IDLE || info->pState != WRX_PTP_SLAVE || info->sState != WRX_WR_TRACK_PHASE
35 
36 bool wrStateValid()
37 {
38  volatile WrxInfo * info = wrxInfo();
39 
40  if (info->wState == WRX_WRS_IDLE &&
41  info->pState == WRX_PTP_SLAVE &&
42  info->sState == WRX_WR_TRACK_PHASE) return true;
43 
44  // State if no WR master is attached
45  if (info->wState == WRX_WRS_IDLE &&
46  info->pState == WRX_PTP_LISTENING &&
47  info->sState == 0) {
48  logInfo("No WhiteRabbit - For testing only!");
49  return true;
50  }
51 
52  return false;
53 }
54 
55 void _subsNetExecEvent (ClbEvent event)
56 {
57  switch (event)
58  {
59  case clbEventReset:
60  net.ipmux_srv_ip = 0xFFFFFFFF;
61  net.ipmux_srv_mac[0] = 0xFFFF;
62  net.ipmux_srv_mac[1] = 0xFFFF;
63  net.ipmux_srv_mac[2] = 0xFFFF;
64  break;
65  case clbEventBoot:
66  net.ipmux_srv_ip = 0xFFFFFFFF;
67  net.ipmux_srv_mac[0] = 0xFFFF;
68  net.ipmux_srv_mac[1] = 0xFFFF;
69  net.ipmux_srv_mac[2] = 0xFFFF;
70 
71  /* Implement actions to go to idle state */
72  // check network
73 
74  if (!netInit()) {
75  _clbStateModErr(clbSys2Idx[CLB_SUB_NET]);
76  return;
77  }
78  netShowInfo(false);
79 
80  if (!wltune_init())
81  {
82  logError("Failed to initialize Wavelength tuning");
83  errPrint(true);
84  }
85 
86  break;
87  case clbEventInit:
88  assert(wrxUp());
89  if (!netIpValid()) {
90  _clbStateError(clbSys2Idx[CLB_SUB_NET], ERROR_CTX(E_NET_NETSTATE));
91  return;
92  }
93  break;
94  case clbEventConfigure:
95  ipMuxFlush(IPM_CH_HW, IPMUX_FLUSH_OP_TOGGLE);
96 
97  volatile WrxInfo * info = wrxInfo();
98 #ifdef IGNORE_WR_STATE
99  if (!wrStateValid()) {
100  logWarn("Wrx not in correct state: whiteRabbit=%d, PTP=%d, SERVO=%d", info->wState, info->pState, info->sState);
101  }
102 #else
103  if (!wrStateValid()) {
104  _clbStateError(clbSys2Idx[CLB_SUB_NET], ERROR_CTX(E_NET_WRXSTATE));
105  return;
106  }
107 #endif
108 
110  errClear();
111  _clbStateError(clbSys2Idx[CLB_SUB_NET], ERROR_CTX(E_NET_DQ_CFG));
112  return;
113  }
114  break;
115  default:
116  break;
117  }
118  _clbStateUpdate(clbSys2Idx[CLB_SUB_NET], event, 0);
119 
120 }
121 
122 
123 static void resolveSvrIp()
124 {
125 #ifdef KM3SIM
126  // in simulation, we'll make up a mac!
127  net.ipmux_srv_mac[0] = 0xD00D;
128  net.ipmux_srv_mac[1] = 0xDAAD;
129  net.ipmux_srv_mac[2] = 0xDEED;
130 #else
131  ms_ipa_t ip;
132  ms_ipa_set_u32(net.ipmux_srv_ip, &ip);
133 
134  ms_mac_t * mac = ms_arp_lookup(&ip);
135 
136  if (mac == NULL) { // mac unknown? query!
137  ms_arp_request(&ip);
138  return;
139  }
140 
141  ms_mac_get_3u16(mac, net.ipmux_srv_mac);
142 
143  logInfo("Resolved MAC: %04x:%04x:%04x\n",
145 #endif
146 }
147 
148 bool _subsNetUpdate(ClbState state, uint32_t time)
149 {
150  if (state == clbStateIdle || state == clbStateStandBy) {
151  if (net.ipmux_srv_ip != 0xFFFFFFFF &&
152  (net.ipmux_srv_mac[0] == 0xFFFF && net.ipmux_srv_mac[1] == 0xFFFF &&
153  net.ipmux_srv_mac[2] == 0xFFFF)) {
154  // try to resolve
155  resolveSvrIp();
156  }
157  }
158 
159  if (wrxUp()) {
160 
161  wltune_periodic();
162 
163  volatile WrxInfo * info = wrxInfo();
164  net.wr_delta[NET_WR_DELTA_SLAVE_RX] = info->sDeltaRX;
165  net.wr_delta[NET_WR_DELTA_SLAVE_TX] = info->sDeltaTX;
166  net.wr_delta[NET_WR_DELTA_MASTER_RX] = info->mDeltaRX;
167  net.wr_delta[NET_WR_DELTA_MASTER_TX] = info->mDeltaTX;
168  net.wr_bitslide = info->bitslide;
169  net.wr_mu = info->mu;
170  net.cable_rtt = 0;
171  net.rx_input_power = info->rxInputPower;
172  net.tx_output_power = info->txOutputPower;
173  net.wr_st_gen = info->wState;
174  net.wr_st_ptp = info->pState;
175  net.wr_st_serv = info->sState;
176  net.wr_temp = info->brdTemp * 100 + info->brdTempFrac;;
177  net.sfp_temp = info->sfpTemp * 100 + (info->sfpTempFrac * 100) / 256;
178  }
179 
180 
181 
182  return true;
183 }
184 
#define NET_WR_DELTA_MASTER_RX
Master receive delta delay index.
Definition: vars.h:183
Idle state.
Definition: clbstate.h:62
ClbEvent
All state change events.
Definition: clbstate.h:88
State Machine Driver.
#define NET_WR_DELTA_SLAVE_TX
Slave transmit delta delay index.
Definition: vars.h:180
int16_t sfp_temp
SFP module temperature, in 1/100th of a degree.
Definition: vars.h:281
static void _clbStateModErr(int idx)
Invoked by subsystem to indicate an error happened.
Definition: clbstate.h:189
uint16_t tx_output_power
Receiver output power in steps of 0.1 uW per count.
Definition: vars.h:275
uint16_t ipmux_srv_mac[3]
Server MAC address, set to 00 to resolve.
Definition: vars.h:263
Provides access to all variables of the various subsystems.
IPMUX Driver for CLBv2.
bool netIpValid()
Returns whether or not the Ip is valid.
Definition: network.c:677
#define NET_WR_DELTA_MASTER_TX
Master transmit delta delay index.
Definition: vars.h:182
void ipMuxFlush(uint32_t channelMask, uint8_t ipmFlushOp)
Flushes the IPMUX&#39;s FIFO&#39;s.
Definition: ipmux.c:191
uint8_t clbSys2Idx[6]
Mapping from subsystem ID to index.
Definition: clbstate.c:59
bool netMuxDest(uint32_t destIp, uint16_t *destMac)
Sets the IPMux destination.
Definition: network.c:361
#define logWarn(MSG,...)
Format a log message with warning level.
Definition: log.h:219
uint32_t ipmux_srv_ip
Server IP address (0xAABBCCDD = AA.BB.CC.DD)
Definition: vars.h:249
int8_t wr_st_gen
White Rabbit general state.
Definition: vars.h:287
bool netInit()
Initializes the network.
Definition: network.c:687
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
net_t net
Provides access to all process variables of subsystem Networking &amp; IPMux.
Definition: vars.c:76
int8_t wr_st_ptp
White Rabbit PTP State.
Definition: vars.h:284
int32_t wr_delta[4]
White Rabbit Delta values, where each entry is a delta-value.
Definition: vars.h:252
StandBy =&gt; Idle.
Definition: clbstate.h:99
StandBy state.
Definition: clbstate.h:63
bool wrxUp()
Returns whether or not the WhiteRabbit interface is up and running.
Definition: wrx.c:58
void errClear()
Clears the current error.
Definition: err.c:46
volatile WrxInfo * wrxInfo()
Returns the whiteRabbit information structure if available, else NULL.
Definition: wrx.c:63
Network subsystem.
uint16_t wr_bitslide
White Rabbit Bitslide value.
Definition: vars.h:269
uint16_t rx_input_power
Receiver input power in steps of 0.1 uW per count.
Definition: vars.h:272
uint64_t wr_mu
White Rabbit Round-Trip time.
Definition: vars.h:243
#define NET_WR_DELTA_SLAVE_RX
Slave receive delta delay index.
Definition: vars.h:181
#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...
void netShowInfo(bool extended)
Shows network configuration information.
Definition: network.c:541
Idle =&gt; StandBy.
Definition: clbstate.h:92
StandBy =&gt; Ready.
Definition: clbstate.h:93
Implements a generic logger facility.
void _clbStateError(int idx, int error, const char *message, const char *name)
Invoked by subsystem to indicate an error happened.
Definition: clbstate.c:258
int16_t wr_temp
White Rabbit temperature (DS18S20), in 1/100th of a degree.
Definition: vars.h:278
WhiteRabbit exchange exchanges information between the 2nd LM32 and WhiteRabbit though a small client...
#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
int64_t cable_rtt
Cable roundtrip time in ps.
Definition: vars.h:246
int8_t wr_st_serv
White Rabbit servo state.
Definition: vars.h:290