AlcapDAQ  1
Data Structures | Functions | Variables
new_fadc0.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 (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 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 ( bool  diag = false)
void forgetPackets ( )

Definition at line 131 of file new_fadc0.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 ( )

Definition at line 327 of file new_fadc0.cpp.

References buf, fadc_buffer_size, forgetPackets(), packet_socket, setupRegs(), and SUCCESS.

328 {
329  packet_socket = socket(PF_PACKET, SOCK_RAW, htons(0x0b04));
330 
331  if(packet_socket < 0) {
332  perror("socket");
333  }
334 
335  buf = new char[fadc_buffer_size];
336 
337 #if 0
338  // set up a timeout so that recv() calls return after ~1 ms even if
339  // there is no packet.
340  struct timeval timeout;
341  timeout.tv_sec = 0;
342  timeout.tv_usec = 1000;
343  if(setsockopt(packet_socket, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) < 0) {
344  perror("setsockopt");
345  }
346 #endif
347 #if 1
348  // make socket non-blocking
349  if(fcntl(packet_socket, F_SETFL, O_NONBLOCK)) {
350  perror("fcntl O_NONBLOCK");
351  }
352 #endif
353 
354  setupRegs();
355  forgetPackets();
356 
357  return SUCCESS;
358 }
INT new_fadc_eor ( )
INT new_fadc_poll_live ( )

Definition at line 370 of file new_fadc0.cpp.

References receivePackets(), and SUCCESS.

371 {
372  receivePackets();
373  return SUCCESS;
374 }
INT new_fadc_read ( char *  pevent)
void receivePackets ( )

Definition at line 81 of file new_fadc0.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 192 of file new_fadc0.cpp.

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

194 {
195  // look up the index of the interface
196  int if_index = if_nametoindex(if_name);
197  if(if_index < 0) {
198  printf("Unknown interface %s\n", if_name);
199  return;
200  }
201 
202  struct ifreq req;
203  strcpy(req.ifr_name, if_name);
204  if(ioctl(packet_socket, SIOCGIFHWADDR, &req) < 0) {
205  perror("SIOCGIFHWADDR");
206  return;
207  }
208  unsigned char src[ETH_ALEN];
209  memcpy(src, req.ifr_hwaddr.sa_data, ETH_ALEN);
210 
211  unsigned char dst[ETH_ALEN] = { 0x00, 0x12, 0x6d, 0x12, 0x34, 0xff};
212  dst[5] = board;
213  struct sockaddr_ll dstaddr;
214  dstaddr.sll_family = AF_PACKET;
215  dstaddr.sll_protocol = htons(ETH_P_ALL);
216  dstaddr.sll_ifindex = if_index;
217  dstaddr.sll_hatype = 0;
218  dstaddr.sll_pkttype = 0;
219  dstaddr.sll_halen = ETH_ALEN;
220  memcpy(dstaddr.sll_addr, dst, ETH_ALEN);
221 
222  // format a packet
223  unsigned char packet[512];
224  memcpy(&packet[0], dst, ETH_ALEN);
225  memcpy(&packet[6], src, ETH_ALEN);
226 
227  // protocol
228  packet[12] = 0x0b;
229  packet[13] = 0x04;
230 
231  // the content
232  packet[14] = (fadc << 5) | reg;
233  packet[15] = (value >> 56) & 0xff;
234  packet[16] = (value >> 48) & 0xff;
235  packet[17] = (value >> 40) & 0xff;
236  packet[18] = (value >> 32) & 0xff;
237  packet[19] = (value >> 24) & 0xff;
238  packet[20] = (value >> 16) & 0xff;
239  packet[21] = (value >> 8) & 0xff;
240  packet[22] = (value ) & 0xff;
241 
242  int packet_length = 512;
243 
244  if(sendto(packet_socket, packet, packet_length, 0, (const sockaddr *) &dstaddr, sizeof(dstaddr)) < 0) {
245  perror("sendto");
246  }
247 }
void setupRegs ( )

Definition at line 249 of file new_fadc0.cpp.

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

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

Variable Documentation

struct fadc_board board[max_boards]

Definition at line 77 of file new_fadc0.cpp.

char* buf = NULL
char* bufp

Definition at line 79 of file new_fadc0.cpp.

Referenced by forgetPackets(), and receivePackets().

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

Definition at line 35 of file new_fadc0.cpp.

Referenced by new_fadc_bor().

const int max_boards = 256

Definition at line 67 of file new_fadc0.cpp.

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

Definition at line 37 of file new_fadc0.cpp.

int packet_socket

Definition at line 52 of file new_fadc0.cpp.