AlcapDAQ  1
Data Structures | Functions | Variables
new_fadc_2007_07_17.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 <linux/if_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_read (char *pevent)
 
bool allPacketsReceived (bool diag=false)
 
void receivePackets ()
 
void forgetPackets ()
 
void setReg (char *if_name, int board, int fadc, int reg, unsigned long long value)
 
void setupRegs ()
 

Variables

const int TP_BLOCK_SIZE = 8192
 
const int TP_NUM_BLOCKS = 512
 
const int FRAME_SIZE = 2048
 
const int NUM_FRAMES = 2048
 
unsigned char * packet_space = 0
 
bool frame_examined [NUM_FRAMES]
 
struct readout_module new_fadc_module
 
int packet_socket
 
const int max_boards = 256
 
struct fadc_board board [max_boards]
 
static struct timeval tv_start
 

Function Documentation

bool allPacketsReceived ( bool  diag = false)
void forgetPackets ( )

Definition at line 139 of file new_fadc_2007_07_17.cpp.

References board, enabled, fadc_board::first_packet_time, frame_examined, FRAME_SIZE, i, max_boards, NUM_FRAMES, packet_space, fadc_board::packets, fadc_board::start_packet_seen, and fadc_board::stop_packet_seen.

140 {
141  for(int i = 0; i < max_boards; i++) {
142  if(board[i].enabled) {
143  board[i].packets.clear();
144  board[i].start_packet_seen = false;
145  board[i].stop_packet_seen = false;
146  board[i].first_packet_time = -1;
147  }
148  }
149 
150  // release all frame buffers back to the kernel
151  for(int i = 0; i < NUM_FRAMES; i++) {
152  unsigned char *frame = packet_space + i*FRAME_SIZE;
153  struct tpacket_hdr *tph = (struct tpacket_hdr *) frame;
154  tph->tp_status = TP_STATUS_KERNEL;
155  frame_examined[i] = false;
156  }
157 }
INT new_fadc_bor ( )
INT new_fadc_eor ( )
INT new_fadc_read ( char *  pevent)
void receivePackets ( )

Definition at line 88 of file new_fadc_2007_07_17.cpp.

References fadc_packet::admin_message, board, fadc_packet::buffer_number, fadc_packet::content_length, enabled, fadc_board::first_packet, fadc_board::first_packet_time, frame_examined, FRAME_SIZE, i, fadc_board::last_packet, fadc_board::last_packet_time, NUM_FRAMES, fadc_packet::packet_serial, packet_space, fadc_board::packets, printf(), fadc_packet::src_addr, fadc_board::start_packet, fadc_board::start_packet_seen, fadc_board::stop_packet, and fadc_board::stop_packet_seen.

89 {
90  for(int i = 0; i < NUM_FRAMES; i++) {
91  if(frame_examined[i]) continue;
92 
93  unsigned char *frame = packet_space + i*FRAME_SIZE;
94  struct tpacket_hdr *tph = (struct tpacket_hdr *) frame;
95  if(!(tph->tp_status & TP_STATUS_USER)) continue;
96 
97  struct fadc_packet *pkt = (struct fadc_packet *) (frame + tph->tp_mac);
98  int board_number = pkt->src_addr[5];
99  if(!board[board_number].enabled) continue;
100 
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 
107 #if 0
108  printf("board %d length %d buffer %d admin %d serial %d\n",
109  board_number, content_length, buffer_number,
110  admin_message, packet_serial);
111 #endif
112 
113  board[board_number].packets[packet_serial] = pkt;
114  if(admin_message & 0x1) {
115  board[board_number].start_packet = packet_serial;
116  board[board_number].start_packet_seen = true;
117  }
118  if(admin_message & 0x2) {
119  board[board_number].stop_packet = packet_serial;
120  board[board_number].stop_packet_seen = true;
121  }
122 
123  double packet_time = tph->tp_sec*1e6 + tph->tp_usec;
124  double fpt = board[board_number].first_packet_time;
125  if(packet_time < fpt || fpt < 0) {
126  board[board_number].first_packet = packet_serial;
127  board[board_number].first_packet_time = packet_time;
128  }
129  double lpt = board[board_number].last_packet_time;
130  if(packet_time > lpt || lpt < 0) {
131  board[board_number].last_packet = packet_serial;
132  board[board_number].last_packet_time = packet_time;
133  }
134 
135  frame_examined[i] = true;
136  }
137 }
void setReg ( char *  if_name,
int  board,
int  fadc,
int  reg,
unsigned long long  value 
)

Definition at line 226 of file new_fadc_2007_07_17.cpp.

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

228 {
229  // look up the index of the interface
230  int if_index = if_nametoindex(if_name);
231  if(if_index < 0) {
232  printf("Unknown interface %s\n", if_name);
233  return;
234  }
235 
236  struct ifreq req;
237  strcpy(req.ifr_name, if_name);
238  if(ioctl(packet_socket, SIOCGIFHWADDR, &req) < 0) {
239  perror("SIOCGIFHWADDR");
240  return;
241  }
242  unsigned char src[ETH_ALEN];
243  memcpy(src, req.ifr_hwaddr.sa_data, ETH_ALEN);
244 
245  unsigned char dst[ETH_ALEN] = { 0x00, 0x12, 0x6d, 0x12, 0x34, 0xff};
246  dst[5] = board;
247  struct sockaddr_ll dstaddr;
248  dstaddr.sll_family = AF_PACKET;
249  dstaddr.sll_protocol = htons(ETH_P_ALL);
250  dstaddr.sll_ifindex = if_index;
251  dstaddr.sll_hatype = 0;
252  dstaddr.sll_pkttype = 0;
253  dstaddr.sll_halen = ETH_ALEN;
254  memcpy(dstaddr.sll_addr, dst, ETH_ALEN);
255 
256  // format a packet
257  unsigned char packet[512];
258  memcpy(&packet[0], dst, ETH_ALEN);
259  memcpy(&packet[6], src, ETH_ALEN);
260 
261  // protocol
262  packet[12] = 0x0b;
263  packet[13] = 0x04;
264 
265  // the content
266  packet[14] = (fadc << 5) | reg;
267  packet[15] = (value >> 56) & 0xff;
268  packet[16] = (value >> 48) & 0xff;
269  packet[17] = (value >> 40) & 0xff;
270  packet[18] = (value >> 32) & 0xff;
271  packet[19] = (value >> 24) & 0xff;
272  packet[20] = (value >> 16) & 0xff;
273  packet[21] = (value >> 8) & 0xff;
274  packet[22] = (value ) & 0xff;
275 
276  int packet_length = 512;
277 
278  if(sendto(packet_socket, packet, packet_length, 0, (const sockaddr *) &dstaddr, sizeof(dstaddr)) < 0) {
279  perror("sendto");
280  }
281 }
void setupRegs ( )

Definition at line 283 of file new_fadc_2007_07_17.cpp.

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

284 {
285  for(int i = 0; i < max_boards; i++) {
286 
287  // If no settings exist for the board, disable it.
288  if(!odb_find_key("/Equipment/Crate %d/Settings/NFADC %02x",
289  crate_number, i)) {
290  board[i].enabled = false;
291  continue;
292  }
293  printf("Found board %02x\n", i);
294 
295  bool enabled =
296  odb_get_bool("/Equipment/Crate %d/Settings/NFADC %02x/Enabled",
297  crate_number, i);
298  board[i].enabled = enabled;
299 
300 
301  for(int chan = 0; chan < 8; chan++) {
302  int led_mode =
303  odb_get_int("/Equipment/Crate %d/Settings/NFADC %02x/Channel %d/LED Mode",
304  crate_number, i, chan);
305 
306  int lower_threshold =
307  odb_get_int("/Equipment/Crate %d/Settings/NFADC %02x/Channel %d/Lower threshold",
308  crate_number, i, chan);
309  int upper_threshold =
310  odb_get_int("/Equipment/Crate %d/Settings/NFADC %02x/Channel %d/Upper threshold",
311  crate_number, i, chan);
312  int pulser_period =
313  odb_get_int("/Equipment/Crate %d/Settings/NFADC %02x/Channel %d/Pulser period",
314  crate_number, i, chan);
315  int trigger_mask =
316  odb_get_int("/Equipment/Crate %d/Settings/NFADC %02x/Channel %d/Trigger mask",
317  crate_number, i, chan);
318  int presamples =
319  odb_get_int("/Equipment/Crate %d/Settings/NFADC %02x/Channel %d/Presamples",
320  crate_number, i, chan);
321  int stretch_samples =
322  odb_get_int("/Equipment/Crate %d/Settings/NFADC %02x/Channel %d/Stretch samples",
323  crate_number, i, chan);
324  int dcm_phase =
325  odb_get_int("/Equipment/Crate %d/Settings/NFADC %02x/Channel %d/DCM phase",
326  crate_number, i, chan);
327 
328  if(enabled) {
329  unsigned long long r0;
330  if(i == 0xff) {
331  r0 = (lower_threshold & 0xfff) |
332  ((upper_threshold & 0xfff) << 12) |
333  ((trigger_mask & 0xf) << 24) |
334  ((presamples & 0xf) << 28) |
335  (((unsigned long long) (stretch_samples & 0xff)) << 32);
336  } else {
337  r0 = (lower_threshold & 0xfff) |
338  ((upper_threshold & 0xfff) << 12) |
339  ((trigger_mask & 0xf) << 24) |
340  ((presamples & 0xfff) << 28) |
341  (((unsigned long long) (stretch_samples & 0xfff)) << 40);
342  }
343  unsigned long long r1 =
344  (pulser_period & 0xffffffff) |
345  (((unsigned long long) (led_mode & 0xf)) << 32);
346  unsigned long long r2 = dcm_phase;
347 
348  int frontend = chan/2;
349  if(i == 0xff) {
350  if(frontend == 1) {
351  frontend = 2;
352  } else if(frontend == 2) {
353  frontend = 1;
354  }
355  }
356 
357  if(chan % 2 == 0) {
358  setReg("eth1", i, frontend, 0, r0);
359  setReg("eth1", i, frontend, 1, r1);
360  setReg("eth1", i, frontend, 4, r2);
361  } else {
362  setReg("eth1", i, frontend, 2, r0);
363  setReg("eth1", i, frontend, 3, r1);
364  setReg("eth1", i, frontend, 5, r2);
365  }
366  }
367  }
368  }
369 }

Variable Documentation

struct fadc_board board[max_boards]

Definition at line 86 of file new_fadc_2007_07_17.cpp.

bool frame_examined[NUM_FRAMES]

Definition at line 40 of file new_fadc_2007_07_17.cpp.

const int FRAME_SIZE = 2048

Definition at line 36 of file new_fadc_2007_07_17.cpp.

const int max_boards = 256

Definition at line 72 of file new_fadc_2007_07_17.cpp.

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

Definition at line 42 of file new_fadc_2007_07_17.cpp.

const int NUM_FRAMES = 2048

Definition at line 37 of file new_fadc_2007_07_17.cpp.

int packet_socket

Definition at line 57 of file new_fadc_2007_07_17.cpp.

unsigned char* packet_space = 0

Definition at line 39 of file new_fadc_2007_07_17.cpp.

const int TP_BLOCK_SIZE = 8192

Definition at line 34 of file new_fadc_2007_07_17.cpp.

const int TP_NUM_BLOCKS = 512

Definition at line 35 of file new_fadc_2007_07_17.cpp.

struct timeval tv_start
static

Definition at line 418 of file new_fadc_2007_07_17.cpp.