AlcapDAQ  1
Data Structures | Macros | Functions | Variables
sis3600.cpp File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.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  sis3600
 

Macros

#define SIS3600_MAX_DMA_SIZE   256
 
#define SIS3600_MAX_SIZE   (32*1024*4)
 
#define ACTIVE_READOUT_BUFFER_SIZE   (32*1024*4)
 
#define ACTIVE_READOUT_MAX_PER_POLL   (2*1024)
 
#define MAX_SIS3600   4
 
#define SIS3600_CSR   0x000
 
#define SIS3600_KA_CLEAR   0x020
 
#define SIS3600_KA_ENABLENEXT   0x028
 
#define SIS3600_KA_RESET   0x060
 
#define SIS3600_OUTBUF   0x100
 
#define SIS3600_CSR_FIFO_EMPTY_MASK   0x00000100
 
#define SIS3600_CSR_FIFO_ALMOST_EMPTY_MASK   0x00000200
 

Functions

INT sis3600_bor ()
 
INT sis3600_eor ()
 
INT sis3600_poll_live ()
 
INT sis3600_read (char *pevent)
 
INT sis3600_bor1 (struct sis3600 *sis)
 
INT sis3600_eor1 (struct sis3600 *sis)
 
INT sis3600_fifo_read (struct sis3600 *sis, unsigned char *buffer, int max_size, bool in_active_readout)
 
INT sis3600_poll1 (struct sis3600 *sis)
 
INT sis3600_read1 (struct sis3600 *sis, char *pevent)
 

Variables

struct readout_module sis3600_module
 
struct sis3600 sis3600 [MAX_SIS3600]
 

Macro Definition Documentation

#define ACTIVE_READOUT_BUFFER_SIZE   (32*1024*4)

Definition at line 34 of file sis3600.cpp.

Referenced by sis3600_poll1().

#define ACTIVE_READOUT_MAX_PER_POLL   (2*1024)

Definition at line 35 of file sis3600.cpp.

Referenced by sis3600_poll1().

#define MAX_SIS3600   4

Definition at line 48 of file sis3600.cpp.

#define SIS3600_CSR   0x000

Definition at line 51 of file sis3600.cpp.

Referenced by sis3600_bor1(), and sis3600_fifo_read().

#define SIS3600_CSR_FIFO_ALMOST_EMPTY_MASK   0x00000200

Definition at line 58 of file sis3600.cpp.

Referenced by sis3600_fifo_read().

#define SIS3600_CSR_FIFO_EMPTY_MASK   0x00000100

Definition at line 57 of file sis3600.cpp.

#define SIS3600_KA_CLEAR   0x020

Definition at line 52 of file sis3600.cpp.

Referenced by sis3600_bor1(), and sis3600_read1().

#define SIS3600_KA_ENABLENEXT   0x028

Definition at line 53 of file sis3600.cpp.

Referenced by sis3600_bor1().

#define SIS3600_KA_RESET   0x060

Definition at line 54 of file sis3600.cpp.

Referenced by sis3600_bor1().

#define SIS3600_MAX_DMA_SIZE   256

Definition at line 32 of file sis3600.cpp.

Referenced by sis3600_bor1().

#define SIS3600_MAX_SIZE   (32*1024*4)

Definition at line 33 of file sis3600.cpp.

Referenced by sis3600_read1().

#define SIS3600_OUTBUF   0x100

Definition at line 55 of file sis3600.cpp.

Referenced by sis3600_fifo_read().

Function Documentation

INT sis3600_bor ( )
INT sis3600_bor1 ( struct sis3600 sis)

Definition at line 65 of file sis3600.cpp.

References sis3600::active_readout_size, crate_number, diag_print(), sis3600::do_active_readout, sis3600::enabled, handle, odb_get_bool(), odb_get_dword(), sis3600::odb_name, SIS3600_CSR, SIS3600_KA_CLEAR, SIS3600_KA_ENABLENEXT, SIS3600_KA_RESET, SIS3600_MAX_DMA_SIZE, SUCCESS, sis3600::vme_base, sis3600::vme_handle, vme_open(), vme_read_d32(), vme_write_d32(), VMEMAP_ASPACE_A32, VMEMAP_DWIDTH_32, VMEMAP_PRGDATAAM_DATA, and VMEMAP_SUPUSERAM_SUPER.

66 {
67  // Get the VME base address of the module
68  sis->vme_base = odb_get_dword("/Equipment/Crate %d/Settings/%s/vme address",
69  crate_number, sis->odb_name);
70 
71  // Open a VME handle for memory-mapped access
72  struct vme_mapping_ctrl mapping = {
77  };
78 
79  struct vme_handle *handle =
80  vme_open(sis->vme_base, mapping, 0x200, SIS3600_MAX_DMA_SIZE);
81  sis->vme_handle = handle;
82 
83  // Reset
84  vme_write_d32(handle, sis->vme_base | SIS3600_KA_RESET, 0x1);
85 
86  // Clear for the next block
87  sis->active_readout_size = 0;
88 
89  // Read back current status register and check it
90  DWORD csr = vme_read_d32(handle, sis->vme_base | SIS3600_CSR);
91  if(csr != 0x00000300) {
92  diag_print(0, "Unexpected status 0x%08x on %s after reset, disabling\n",
93  csr, sis->odb_name);
94  sis->enabled = false;
95  return FE_ERR_HW;
96  }
97 
98  // Continue setup
99  vme_write_d32(handle, sis->vme_base | SIS3600_CSR, 0x08010000);
100  vme_write_d32(handle, sis->vme_base | SIS3600_KA_CLEAR, 0x1);
101  vme_write_d32(handle, sis->vme_base | SIS3600_KA_ENABLENEXT, 0x1);
102 
103  // Again check the status register value
104  csr = vme_read_d32(handle, sis->vme_base | SIS3600_CSR);
105  if(csr != 0x00018300) {
106  diag_print(0, "Unexpected status 0x%08x on %s after setup, disabling\n",
107  csr, sis->odb_name);
108  sis->enabled = false;
109  return FE_ERR_HW;
110  }
111 
112  // Determine whether we're supposed to do active readout
113  sis->do_active_readout =
114  odb_get_bool("/Equipment/Crate %d/Settings/%s/Active Readout mode",
115  crate_number, sis->odb_name);
116 
117  return SUCCESS;
118 }
INT sis3600_eor ( )
INT sis3600_eor1 ( struct sis3600 sis)

Definition at line 159 of file sis3600.cpp.

References SUCCESS, vme_close(), and sis3600::vme_handle.

160 {
161  vme_close(sis->vme_handle);
162  return SUCCESS;
163 }
INT sis3600_fifo_read ( struct sis3600 sis,
unsigned char *  buffer,
int  max_size,
bool  in_active_readout 
)

Definition at line 186 of file sis3600.cpp.

References diag_print(), MIN, sis3600::odb_name, SIS3600_CSR, SIS3600_CSR_FIFO_ALMOST_EMPTY_MASK, SIS3600_OUTBUF, size, status, sis3600::vme_base, vme_dma_read(), sis3600::vme_handle, and vme_read_d32().

188 {
189  // If we're not in the active readout phase, then we simply do a big DMA.
190  // There is a 50% chance of losing one word at the end of the block.
191  if(!in_active_readout) {
192  int status = vme_dma_read(sis->vme_handle,
193  sis->vme_base | SIS3600_OUTBUF,
194  buffer,
195  max_size);
196  return status;
197  }
198 
199  // If we are in the active readout phase, then we have to be more careful.
200  // Before initiating a block transfer, we check whether the CSR
201  // indicates "almost empty." If it does not, a 256 byte (64 word)
202  // block transfer is safe. If it does, then we return nothing; we'll
203  // be back when there is enough data to bother with.
204  int size = 0;
205  while(size < max_size) {
206 
207  DWORD csr = vme_read_d32(sis->vme_handle, sis->vme_base | SIS3600_CSR);
209  return size;
210  }
211 
212  int size_this_time = MIN(max_size - size, 256);
213 
214  int status = vme_dma_read(sis->vme_handle,
215  sis->vme_base | SIS3600_OUTBUF,
216  buffer + size,
217  size_this_time);
218 
219  if(status >= 0) {
220  size += status;
221  }
222 
223  if(status != size_this_time) {
224  diag_print(0, "Retrieved only %d bytes from %s", status, sis->odb_name);
225  }
226  }
227 
228  return size;
229 }
INT sis3600_poll1 ( struct sis3600 sis)

Definition at line 236 of file sis3600.cpp.

References sis3600::active_readout_buffer, ACTIVE_READOUT_BUFFER_SIZE, ACTIVE_READOUT_MAX_PER_POLL, sis3600::active_readout_size, diag_print(), sis3600::do_active_readout, FE_NEED_STOP, MIN, sis3600::odb_name, sis3600_fifo_read(), size, status, SUCCESS, and TRUE.

237 {
238  // If active readout is not enabled, skip it.
239  if(!sis->do_active_readout) {
240  return SUCCESS;
241  }
242 
243  // First check how much space we have available in the active
244  // readout buffer.
245  int size_left = ACTIVE_READOUT_BUFFER_SIZE - sis->active_readout_size;
246  int size = MIN(ACTIVE_READOUT_MAX_PER_POLL, size_left);
247 
248  // Now try to read up to that amount.
249  int status = sis3600_fifo_read(sis,
251  size, TRUE);
252 
253  // Check the status
254  if(status >= 0) {
255  sis->active_readout_size += status;
256  } else {
257  diag_print(0, "Status from sis3600_fifo_read is %d for %s.\n", status,
258  sis->odb_name);
259  return FE_ERR_HW;
260  }
261 
263  return FE_NEED_STOP;
264  } else {
265  return SUCCESS;
266  }
267 }
INT sis3600_poll_live ( )
INT sis3600_read ( char *  pevent)
INT sis3600_read1 ( struct sis3600 sis,
char *  pevent 
)

Definition at line 298 of file sis3600.cpp.

References sis3600::active_readout_buffer, sis3600::active_readout_size, sis3600::bank_name, FALSE, sis3600_fifo_read(), SIS3600_KA_CLEAR, SIS3600_MAX_SIZE, status, SUCCESS, sis3600::vme_base, sis3600::vme_handle, and vme_write_d32().

299 {
300  // Create the MIDAS bank
301  DWORD *pdata;
302  bk_create(pevent, sis->bank_name, TID_DWORD, &pdata);
303 
304  // Copy data that was read during the active readout
305  int active_size = sis->active_readout_size;
306  memcpy(pdata, sis->active_readout_buffer, active_size);
307 
308  // Read any data remaining in the module
309  int status =
310  sis3600_fifo_read(sis, ((unsigned char *) pdata) + active_size,
312 
313  // Check the status
314  int final_size = 0;
315  if(status >= 0) {
316  final_size = status;
317  } else {
318  // handle the error
319  }
320 
321  // Close the bank
322  bk_close(pevent, pdata + (active_size + final_size)/sizeof(DWORD));
323 
324  // Clear for the next block
325  sis->active_readout_size = 0;
326 
327  // Clear FIFO
329 
330  return SUCCESS;
331 }

Variable Documentation

Definition at line 49 of file sis3600.cpp.

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

Definition at line 17 of file sis3600.cpp.