AlcapDAQ  1
Data Structures | Functions | Variables
new_fadc_old.cpp File Reference
#include <map>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/ethernet.h>
#include <netinet/in.h>
#include <netpacket/packet.h>
#include "midas.h"
#include "crate.h"
#include "vme.h"
#include "odb_wrapper.h"
#include "diag.h"

Go to the source code of this file.

Data Structures

struct  fadc_packet
 
struct  fadc_board
 

Functions

INT new_fadc_bor ()
 
INT new_fadc_eor ()
 
INT new_fadc_poll_live ()
 
INT new_fadc_read (char *pevent)
 
bool allPacketsReceived ()
 
void receivePackets ()
 
void forgetPackets ()
 
void setReg (char *if_name, int board, int fadc, int reg, unsigned long long value)
 
void setupRegs ()
 

Variables

const int fadc_buffer_size = 256*1024 * sizeof(int)
 
struct readout_module new_fadc_module
 
int packet_socket
 
const int max_boards = 256
 
struct fadc_board board [max_boards]
 
char * buf = NULL
 
char * bufp
 

Function Documentation

bool allPacketsReceived ( )

Definition at line 143 of file new_fadc_old.cpp.

References board, enabled, i, max_boards, fadc_board::start_packet, and fadc_board::stop_packet.

144 {
145  // check whether we have a start packet, a stop packet, and a stream of
146  // continuous serial numbers in between.
147  for(int i = 0; i < max_boards; i++) {
148  if(!board[i].enabled) continue;
149  if(!board[i].start_packet_seen || !board[i].stop_packet_seen) {
150  //printf("Still waiting: admin %d %d %d\n", i, board[i].start_packet_seen, board[i].stop_packet_seen);
151  return false;
152  }
153  }
154  for(int i = 0; i < max_boards; i++) {
155  if(!board[i].enabled) continue;
156  int num_packets = board[i].stop_packet - board[i].start_packet;
157  if(num_packets < 0) {
158  num_packets += 0x10000;
159  }
160 
161  for(int j = num_packets; j >= 0; j--) {
162  int pn = (board[i].start_packet + j) & 0xffff;
163  if(board[i].packets[pn] == NULL) {
164  // printf("Still waiting: packet %d %d\n", i, j);
165  return false;
166  }
167  }
168  }
169  return true;
170 }
void forgetPackets ( )

Definition at line 131 of file new_fadc_old.cpp.

References board, buf, bufp, enabled, i, max_boards, fadc_board::packets, fadc_board::start_packet_seen, and fadc_board::stop_packet_seen.

132 {
133  for(int i = 0; i < max_boards; i++) {
134  if(board[i].enabled) {
135  board[i].packets.clear();
136  board[i].start_packet_seen = false;
137  board[i].stop_packet_seen = false;
138  }
139  }
140  bufp = buf;
141 }
INT new_fadc_bor ( )
INT new_fadc_eor ( )
INT new_fadc_poll_live ( )
INT new_fadc_read ( char *  pevent)
void receivePackets ( )

Definition at line 81 of file new_fadc_old.cpp.

References fadc_packet::admin_message, allPacketsReceived(), board, fadc_packet::buffer_number, bufp, fadc_packet::content_length, i, fadc_packet::packet_serial, packet_socket, fadc_board::packets, printf(), fadc_packet::src_addr, fadc_board::start_packet, fadc_board::start_packet_seen, status, fadc_board::stop_packet, and fadc_board::stop_packet_seen.

82 {
83  while(1) {
84  int status = recv(packet_socket, bufp, 1518, 0);
85  if(status < 0) {
86  if(errno != EWOULDBLOCK && errno != EAGAIN) {
87  perror("receivePackets");
88  }
89  break;
90  }
91 
92 #if 0
93  for(int i = 0; i < status; i++) {
94  printf("%02x ", 0xff & bufp[i]);
95  }
96  printf("\n");
97 #endif
98 
99  struct fadc_packet *pkt = (struct fadc_packet *) bufp;
100  int board_number = pkt->src_addr[5];
101  int content_length = ntohs(pkt->content_length);
102  int buffer_number = pkt->buffer_number;
103  int admin_message = pkt->admin_message;
104  int packet_serial = ntohs(pkt->packet_serial);
105 
106  if(content_length > 1500) {
107  printf("board %d length %d buffer %d admin %d serial %d\n",
108  board_number, content_length, buffer_number,
109  admin_message, packet_serial);
110  }
111 
112  board[board_number].packets[packet_serial] = pkt;
113  if(admin_message & 0x1) {
114  board[board_number].start_packet = packet_serial;
115  board[board_number].start_packet_seen = true;
116  }
117  if(admin_message & 0x2) {
118  board[board_number].stop_packet = packet_serial;
119  board[board_number].stop_packet_seen = true;
120  }
121 
122  bufp += status;
123 
124  if(allPacketsReceived()) {
125  break;
126  }
127 
128  }
129 }
void setReg ( char *  if_name,
int  board,
int  fadc,
int  reg,
unsigned long long  value 
)

Definition at line 173 of file new_fadc_old.cpp.

References board, packet_socket, printf(), and value.

175 {
176  // look up the index of the interface
177  int if_index = if_nametoindex(if_name);
178  if(if_index < 0) {
179  printf("Unknown interface %s\n", if_name);
180  return;
181  }
182 
183  struct ifreq req;
184  strcpy(req.ifr_name, if_name);
185  if(ioctl(packet_socket, SIOCGIFHWADDR, &req) < 0) {
186  perror("SIOCGIFHWADDR");
187  return;
188  }
189  unsigned char src[ETH_ALEN];
190  memcpy(src, req.ifr_hwaddr.sa_data, ETH_ALEN);
191 
192  unsigned char dst[ETH_ALEN] = { 0x00, 0x12, 0x6d, 0x12, 0x34, 0xff};
193  dst[5] = board;
194  struct sockaddr_ll dstaddr;
195  dstaddr.sll_family = AF_PACKET;
196  dstaddr.sll_protocol = htons(ETH_P_ALL);
197  dstaddr.sll_ifindex = if_index;
198  dstaddr.sll_hatype = 0;
199  dstaddr.sll_pkttype = 0;
200  dstaddr.sll_halen = ETH_ALEN;
201  memcpy(dstaddr.sll_addr, dst, ETH_ALEN);
202 
203  // format a packet
204  unsigned char packet[512];
205  memcpy(&packet[0], dst, ETH_ALEN);
206  memcpy(&packet[6], src, ETH_ALEN);
207 
208  // protocol
209  packet[12] = 0x0b;
210  packet[13] = 0x04;
211 
212  // the content
213  packet[14] = (fadc << 5) | reg;
214  packet[15] = (value >> 56) & 0xff;
215  packet[16] = (value >> 48) & 0xff;
216  packet[17] = (value >> 40) & 0xff;
217  packet[18] = (value >> 32) & 0xff;
218  packet[19] = (value >> 24) & 0xff;
219  packet[20] = (value >> 16) & 0xff;
220  packet[21] = (value >> 8) & 0xff;
221  packet[22] = (value ) & 0xff;
222 
223  int packet_length = 512;
224 
225  if(sendto(packet_socket, packet, packet_length, 0, (const sockaddr *) &dstaddr, sizeof(dstaddr)) < 0) {
226  perror("sendto");
227  }
228 }
void setupRegs ( )

Definition at line 230 of file new_fadc_old.cpp.

References board, crate_number, enabled, fadc_board::enabled, i, max_boards, odb_find_key(), odb_get_bool(), odb_get_int(), printf(), and setReg().

231 {
232  for(int i = 0; i < max_boards; i++) {
233 
234  // If no settings exist for the board, disable it.
235  if(!odb_find_key("/Equipment/Crate %d/Settings/NFADC %02x",
236  crate_number, i)) {
237  board[i].enabled = false;
238  continue;
239  }
240  printf("Found board %02x\n", i);
241 
242  bool enabled =
243  odb_get_bool("/Equipment/Crate %d/Settings/NFADC %02x/Enabled",
244  crate_number, i);
245  board[i].enabled = enabled;
246 
247 
248  for(int chan = 0; chan < 8; chan++) {
249  int led_mode =
250  odb_get_int("/Equipment/Crate %d/Settings/NFADC %02x/Channel %d/LED Mode",
251  crate_number, i, chan);
252 
253  int lower_threshold =
254  odb_get_int("/Equipment/Crate %d/Settings/NFADC %02x/Channel %d/Lower threshold",
255  crate_number, i, chan);
256  int upper_threshold =
257  odb_get_int("/Equipment/Crate %d/Settings/NFADC %02x/Channel %d/Upper threshold",
258  crate_number, i, chan);
259  int pulser_period =
260  odb_get_int("/Equipment/Crate %d/Settings/NFADC %02x/Channel %d/Pulser period",
261  crate_number, i, chan);
262  int trigger_mask =
263  odb_get_int("/Equipment/Crate %d/Settings/NFADC %02x/Channel %d/Trigger mask",
264  crate_number, i, chan);
265  int presamples =
266  odb_get_int("/Equipment/Crate %d/Settings/NFADC %02x/Channel %d/Presamples",
267  crate_number, i, chan);
268  int stretch_samples =
269  odb_get_int("/Equipment/Crate %d/Settings/NFADC %02x/Channel %d/Stretch samples",
270  crate_number, i, chan);
271  int dcm_phase =
272  odb_get_int("/Equipment/Crate %d/Settings/NFADC %02x/Channel %d/DCM phase",
273  crate_number, i, chan);
274 
275  if(enabled) {
276  unsigned long long r0 =
277  (lower_threshold & 0xfff) |
278  ((upper_threshold & 0xfff) << 12) |
279  ((trigger_mask & 0xf) << 24) |
280  ((presamples & 0xf) << 28) |
281  (((unsigned long long) (stretch_samples & 0xff)) << 32);
282  unsigned long long r1 =
283  (pulser_period & 0xffffffff) |
284  (((unsigned long long) (led_mode & 0xf)) << 32);
285  unsigned long long r2 = dcm_phase;
286 
287  int frontend = chan/2;
288  if(frontend == 1) {
289  frontend = 2;
290  } else if(frontend == 2) {
291  frontend = 1;
292  }
293 
294  if(chan % 2 == 0) {
295  setReg("eth0", i, frontend, 0, r0);
296  setReg("eth0", i, frontend, 1, r1);
297  setReg("eth0", i, frontend, 4, r2);
298  } else {
299  setReg("eth0", i, frontend, 2, r0);
300  setReg("eth0", i, frontend, 3, r1);
301  setReg("eth0", i, frontend, 5, r2);
302  }
303  }
304  }
305  }
306 }

Variable Documentation

struct fadc_board board[max_boards]

Definition at line 77 of file new_fadc_old.cpp.

char* buf = NULL

Definition at line 78 of file new_fadc_old.cpp.

char* bufp

Definition at line 79 of file new_fadc_old.cpp.

const int fadc_buffer_size = 256*1024 * sizeof(int)

Definition at line 35 of file new_fadc_old.cpp.

const int max_boards = 256

Definition at line 67 of file new_fadc_old.cpp.

struct readout_module new_fadc_module
Initial value:
= {
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
}

Definition at line 37 of file new_fadc_old.cpp.

int packet_socket

Definition at line 52 of file new_fadc_old.cpp.