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  if (timeOut(to)) return errSet(ERROR_CTX(E_TIMEOUT));
80  }
81  return true;
82 }
83 
84 static inline bool wrxWaitForReply(uint16_t code) {
85  if (!_wrxUp) return errSet(ERROR_CTX(E_INVSTATE));
86  if (!wrxReady()) return false;
87 
88  if (_wrx->info.cmdcode != code) {
89  return errSet(ERROR_CTX(E_NOTFOUND));
90  }
91  return true;
92 }
93 
94 
95 bool wrxSetAutoNeg(bool on)
96 {
97  if (!wrxReady()) return false;
98 
99  if (on) {
100  _wrx->cmd.code = WRX_COMMAND_AUTONEG_ON;
101  } else {
102  _wrx->cmd.code = WRX_COMMAND_AUTONEG_OFF;
103  }
104 
105  return true;
106 }
107 
108 
109 uint64_t wrxUtcTime()
110 {
111  if (!_wrxUp) return 0;
112 
113  return _wrx->info.utcTime;
114 }
115 
116 
117 bool wrxSetTuneWord(int32_t tuneWord)
118 {
119  if (!wrxReady()) return false;
120 
121  _wrx->cmd.params.tuneWord = tuneWord;
122  _wrx->cmd.code = WRX_COMMAND_SET_TUNEWORD;
123 
124  return true;
125 }
126 
127 
128 bool wrxGetTuneInfo(WrxTuneInfo * info)
129 {
130  if (!wrxReady()) return false;
131 
132  _wrx->cmd.code = WRX_COMMAND_GET_TUNEINFO;
133 
134  if (!wrxWaitForReply(WRX_COMMAND_GET_TUNEINFO)) return false;
135 
136  *info = _wrx->info.cmdreply.tuneInfo;
137 
138  return true;
139 }
140 
141 
142 bool wrxGetSFPSerialNo(char sn[17])
143 {
144  if (!wrxReady()) return false;
145 
146  _wrx->cmd.code = WRX_COMMAND_GET_SFP_VENDOR_SN;
147 
148  if (!wrxWaitForReply(WRX_COMMAND_GET_SFP_VENDOR_SN)) return false;
149 
150  memcpy(sn, _wrx->info.cmdreply.sfpVendorSN, 16);
151  sn[16] = '\0';
152 
153  return true;
154 }
155 
156 bool wrxSetSFPThreshold(int index, uint16_t value)
157 {
158  if (!wrxReady()) return false;
159 
160  _wrx->cmd.params.threshold.value = value;
161  _wrx->cmd.params.threshold.index = index;
162  _wrx->cmd.code = WRX_COMMAND_SET_THRESHOLD;
163 
164  return true;
165 }
166 
167 
168 
bool wrxGetTuneInfo(WrxTuneInfo *info)
Sets the fields of the WrxTuneInfo structure.
Definition: wrx.c:128
#define WRX_BASE
WhiteRabbit exchange address, not a real device.
Definition: cfg_soc.h:94
#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:109
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:117
bool wrxGetSFPSerialNo(char sn[17])
Gets the sfp serial number.
Definition: wrx.c:142
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
#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:95
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