KM3NeT CLB  2.0
KM3NeT CLB v2 Embedded Software
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
wrx.c
1 /*
2  * KM3NeT CLB v2 Firmware
3  * ----------------------
4  *
5  * Copyright 2013 KM3NeT Collaboration
6  *
7  * All Rights Reserved.
8  *
9  *
10  * File : wrx.c
11  * Created : 25 nov. 2013
12  * Author : Vincent van Beveren
13  */
14 
15 
16 #include "drv/wb/wrx.h"
17 
18 #include "lm32soc/dev_soc.h"
19 
20 #include "kernel/tm.h"
21 #include "kernel/err.h"
22 
23 #include "util/log.h"
24 
25 #include <string.h>
26 
27 LOG_DEF(Wrx);
28 
29 
30 static volatile Wrx * _wrx = (volatile Wrx *)(WRX_BASE);
31 
32 bool _wrxUp;
33 
34 bool wrxInit()
35 {
36  _wrxUp = false;
37 
38  logInfo("Waiting for WRPC...");
39 
40  while (_wrx->magic != WRX_MAGIC)
41  {
42  // endless loop. System won't work without wrx. Will cause reboot.
43  }
44 
45  // give it some time to set the version correctly.
46  timeDelay(2);
47 
48  if (_wrx->ver != WRX_VERSION) {
49  errSet(ERROR_CTX(E_WRX_EP_VER));
50  return false;
51  }
52  logInfo("Ok, WRPC up!, Version " STR(WRX_VERSION));
53  _wrxUp = true;
54  return true;
55 
56 }
57 
58 bool wrxUp()
59 {
60  return _wrxUp;
61 }
62 
63 volatile WrxInfo * wrxInfo()
64 {
65  if (!_wrxUp) return NULL;
66  return &(_wrx->info);
67 }
68 
69 
71 {
72  return _wrx->cmd.code != WRX_COMMAND_NONE;
73 }
74 
75 static bool wrxReady() {
76  if (!_wrxUp) return errSet(ERROR_CTX(E_INVSTATE));
77  uint32_t to = timeOutInit(1000);
78  while (_wrx->cmd.code != WRX_COMMAND_NONE)
79  {
80  if (timeOut(to)) {
81  return errSet(ERROR_CTX(E_TIMEOUT));
82  }
83  }
84  return true;
85 }
86 
87 static inline bool wrxWaitForReply(uint16_t code) {
88  if (!_wrxUp) return errSet(ERROR_CTX(E_INVSTATE));
89  if (!wrxReady()) return false;
90  if (_wrx->info.cmdcode != code) {
91  return errSet(ERROR_CTX(E_NOTFOUND));
92  }
93  return true;
94 }
95 
96 
97 bool wrxSetAutoNeg(bool on)
98 {
99  if (!wrxReady()) return false;
100 
101  if (on) {
102  _wrx->cmd.code = WRX_COMMAND_AUTONEG_ON;
103  } else {
104  _wrx->cmd.code = WRX_COMMAND_AUTONEG_OFF;
105  }
106 
107  return true;
108 }
109 
110 
111 uint64_t wrxUtcTime()
112 {
113  if (!_wrxUp) return 0;
114 
115  return _wrx->info.utcTime;
116 }
117 
118 
119 bool wrxSetTuneWord(int32_t tuneWord)
120 {
121  if (!wrxReady()) return false;
122 
123  _wrx->cmd.params.tuneWord = tuneWord;
124  _wrx->cmd.code = WRX_COMMAND_SET_TUNEWORD;
125 
126  return true;
127 }
128 
129 
130 bool wrxGetTuneInfo(WrxTuneInfo * info)
131 {
132  if (!wrxReady()) return false;
133 
134  _wrx->cmd.code = WRX_COMMAND_GET_TUNEINFO;
135 
136  if (!wrxWaitForReply(WRX_COMMAND_GET_TUNEINFO)) return false;
137 
138  *info = _wrx->info.cmdreply.tuneInfo;
139 
140  return true;
141 }
142 
143 
144 bool wrxGetSFPSerialNo(char sn[17])
145 {
146  if (!wrxReady()) return false;
147 
148  _wrx->cmd.code = WRX_COMMAND_GET_SFP_VENDOR_SN;
149 
150  if (!wrxWaitForReply(WRX_COMMAND_GET_SFP_VENDOR_SN)) return false;
151 
152  memcpy(sn, _wrx->info.cmdreply.sfpVendorSN, 16);
153  sn[16] = '\0';
154 
155  return true;
156 }
157 
158 bool wrxSetSFPThreshold(int index, uint16_t value)
159 {
160  if (!wrxReady()) return false;
161 
162  _wrx->cmd.params.threshold.value = value;
163  _wrx->cmd.params.threshold.index = index;
164  _wrx->cmd.code = WRX_COMMAND_SET_THRESHOLD;
165 
166  return true;
167 }
168 
169 bool wrxModI2cRead(uint8_t addr, uint8_t reg, uint8_t len, uint8_t * buf)
170 {
171  if (!wrxReady()) return false;
172  if (len == 0 || len > 4) return false;
173  _wrx->cmd.params.i2cWrt.addr = addr;
174  _wrx->cmd.params.i2cWrt.reg = reg;
175  _wrx->cmd.params.i2cWrt.len = len;
176  _wrx->cmd.code = WRX_COMMAND_READ_SFP_I2C;
177  if (!wrxWaitForReply(WRX_COMMAND_READ_SFP_I2C)) {
178  return false;
179  }
180  memcpy(buf, _wrx->info.cmdreply.i2cRd.data, len);
181  return true;
182 
183 }
184 
185 bool wrxModI2cWrite(uint8_t addr, uint8_t reg, uint8_t len, uint8_t * buf)
186 {
187  if (!wrxReady()) return false;
188  if (len == 0 || len > 4) return false;
189 
190 
191  _wrx->cmd.params.i2cWrt.addr = addr;
192  _wrx->cmd.params.i2cWrt.reg = reg;
193  _wrx->cmd.params.i2cWrt.len = len;
194  memcpy(&_wrx->cmd.params.i2cWrt.data[0], buf, len);
195  _wrx->cmd.code = WRX_COMMAND_WRITE_SFP_I2C;
196  return true;
197 
198 
199 }
200 
201 
202 
bool wrxGetTuneInfo(WrxTuneInfo *info)
Sets the fields of the WrxTuneInfo structure.
Definition: wrx.c:130
#define WRX_BASE
WhiteRabbit exchange address, not a real device.
Definition: cfg_soc.h:94
bool wrxModI2cWrite(uint8_t addr, uint8_t reg, uint8_t len, uint8_t *buf)
Write SFP I2C bus (raw)
Definition: wrx.c:185
#define E_NOTFOUND
Generic error: not found (ID or resource.
Definition: errorcode.h:121
uint64_t wrxUtcTime()
Returns the UTC time, or 0 if not available.
Definition: wrx.c:111
bool wrxInit()
Initializes the whiteRabbit eXchange.
Definition: wrx.c:34
#define STR(EXPR)
Stringyfies an expression.
Definition: macro.h:37
void timeDelay(uint32_t msec)
Simple busy-wait delay.
Definition: tm.c:18
#define E_INVSTATE
Generic error: Module is in a state in which.
Definition: errorcode.h:103
static uint32_t timeOutInit(uint32_t msec)
Initializes a timeout with the specified no of msecs.
Definition: tm.h:53
static bool timeOut(uint32_t to)
Checks whether or not the timeout has expired.
Definition: tm.h:77
Manages the global system error.
bool wrxSetTuneWord(int32_t tuneWord)
Sets the tune-word.
Definition: wrx.c:119
bool wrxGetSFPSerialNo(char sn[17])
Gets the sfp serial number.
Definition: wrx.c:144
Simple timer functions.
bool wrxUp()
Returns whether or not the WhiteRabbit interface is up and running.
Definition: wrx.c:58
volatile WrxInfo * wrxInfo()
Returns the whiteRabbit information structure if available, else NULL.
Definition: wrx.c:63
bool wrxModI2cRead(uint8_t addr, uint8_t reg, uint8_t len, uint8_t *buf)
Read SFP I2C bus (raw)
Definition: wrx.c:169
#define E_TIMEOUT
Generic error: Timeout error.
Definition: errorcode.h:100
bool wrxCmdPending()
Returns whether or not there is a command still pending to be executed.
Definition: wrx.c:70
#define LOG_DEF(NAME,...)
Define a logger for a module.
Definition: log.h:129
bool wrxSetAutoNeg(bool on)
Returns autonegotation on or off.
Definition: wrx.c:97
This file assigns all device structures to memory mapped structures.
bool errSet(uint32_t code, const char *error, const char *name)
Sets an error.
Implements a generic logger facility.
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