AlcapDAQ  1
Macros | Functions | Variables
vmic_ttl.cpp File Reference
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/io.h>
#include "midas.h"
#include "crate.h"
#include "diag.h"
#include "odb_wrapper.h"
#include "vme.h"

Go to the source code of this file.

Macros

#define VMIC_PORT12   0x12
 
#define VMIC_PORT34   0x10
 
#define VMIC_PORT56   0x16
 
#define VMIC_PORT78   0x14
 
#define VMIC_CSRU   0x20
 
#define VMIC_CSRL   0x21
 
#define VMIC_OUTPUT_1   0x00004000
 
#define VMIC_OUTPUT_2   0x00000800
 
#define VMIC_OUTPUT_3   0x00000100
 
#define VMIC_OUTPUT_4   0x00000030
 
#define VMIC_OUTPUT_5   0x00000004
 
#define VMIC_OUTPUT_6   0x00800000
 
#define VMIC_OUTPUT_7   0x00100000
 
#define VMIC_OUTPUT_8   0x00020000
 
#define VMIC_OUTPUT_EW1   VMIC_OUTPUT_1
 
#define VMIC_OUTPUT_EW2   VMIC_OUTPUT_2
 
#define VMIC_OUTPUT_LATCH_RESET   VMIC_OUTPUT_3
 
#define VMIC_OUTPUT_CAENandCOMP   VMIC_OUTPUT_4
 
#define VMIC_OUTPUT_CRATE4   VMIC_OUTPUT_5
 
#define VMIC_OUTPUT_SOFTSTOP   VMIC_OUTPUT_6
 
#define VMIC_OUTPUT_CLOCK_RESET   VMIC_OUTPUT_7
 
#define VMIC_OUTPUT_START1   (VMIC_OUTPUT_1 | VMIC_OUTPUT_4)
 
#define VMIC_OUTPUT_START2   (VMIC_OUTPUT_2 | VMIC_OUTPUT_4)
 
#define VMIC_INPUT_END_EVENT   62
 
#define VMIC_INPUT_BLOCK   59
 

Functions

INT vmic_ttl_init ()
 
INT vmic_ttl_start_block ()
 
INT vmic_ttl_stop_block ()
 
INT vmic_ttl_poll_live ()
 
INT cycle_ram ()
 
void vmic_set (DWORD new_value)
 
void vmic_turn_on (DWORD new_channels)
 
INT vmic_read_input (int channel)
 

Variables

struct readout_module vmic_ttl_module
 
static WORD vmic_vme_base
 
static struct vme_handlevmic_handle
 
static WORD vmic_current_low
 
static WORD vmic_current_high
 
static BOOL vmic_enabled
 
static BOOL not_really_started
 

Macro Definition Documentation

#define VMIC_CSRL   0x21

Definition at line 40 of file vmic_ttl.cpp.

#define VMIC_CSRU   0x20

Definition at line 39 of file vmic_ttl.cpp.

Referenced by vmic_ttl_init().

#define VMIC_INPUT_BLOCK   59

Definition at line 71 of file vmic_ttl.cpp.

Referenced by vmic_ttl_start_block().

#define VMIC_INPUT_END_EVENT   62

Definition at line 70 of file vmic_ttl.cpp.

Referenced by vmic_ttl_poll_live(), and vmic_ttl_start_block().

#define VMIC_OUTPUT_1   0x00004000

Definition at line 45 of file vmic_ttl.cpp.

#define VMIC_OUTPUT_2   0x00000800

Definition at line 46 of file vmic_ttl.cpp.

#define VMIC_OUTPUT_3   0x00000100

Definition at line 47 of file vmic_ttl.cpp.

#define VMIC_OUTPUT_4   0x00000030

Definition at line 48 of file vmic_ttl.cpp.

#define VMIC_OUTPUT_5   0x00000004

Definition at line 49 of file vmic_ttl.cpp.

#define VMIC_OUTPUT_6   0x00800000

Definition at line 50 of file vmic_ttl.cpp.

#define VMIC_OUTPUT_7   0x00100000

Definition at line 51 of file vmic_ttl.cpp.

#define VMIC_OUTPUT_8   0x00020000

Definition at line 52 of file vmic_ttl.cpp.

#define VMIC_OUTPUT_CAENandCOMP   VMIC_OUTPUT_4

Definition at line 60 of file vmic_ttl.cpp.

#define VMIC_OUTPUT_CLOCK_RESET   VMIC_OUTPUT_7

Definition at line 63 of file vmic_ttl.cpp.

#define VMIC_OUTPUT_CRATE4   VMIC_OUTPUT_5

Definition at line 61 of file vmic_ttl.cpp.

Referenced by vmic_ttl_start_block().

#define VMIC_OUTPUT_EW1   VMIC_OUTPUT_1

Definition at line 57 of file vmic_ttl.cpp.

Referenced by vmic_ttl_start_block().

#define VMIC_OUTPUT_EW2   VMIC_OUTPUT_2

Definition at line 58 of file vmic_ttl.cpp.

Referenced by vmic_ttl_start_block().

#define VMIC_OUTPUT_LATCH_RESET   VMIC_OUTPUT_3

Definition at line 59 of file vmic_ttl.cpp.

Referenced by vmic_ttl_start_block().

#define VMIC_OUTPUT_SOFTSTOP   VMIC_OUTPUT_6

Definition at line 62 of file vmic_ttl.cpp.

Referenced by vmic_ttl_stop_block().

#define VMIC_OUTPUT_START1   (VMIC_OUTPUT_1 | VMIC_OUTPUT_4)

Definition at line 64 of file vmic_ttl.cpp.

Referenced by vmic_ttl_start_block().

#define VMIC_OUTPUT_START2   (VMIC_OUTPUT_2 | VMIC_OUTPUT_4)

Definition at line 65 of file vmic_ttl.cpp.

Referenced by vmic_ttl_start_block().

#define VMIC_PORT12   0x12

Definition at line 35 of file vmic_ttl.cpp.

Referenced by vmic_read_input(), and vmic_set().

#define VMIC_PORT34   0x10

Definition at line 36 of file vmic_ttl.cpp.

Referenced by vmic_read_input(), and vmic_set().

#define VMIC_PORT56   0x16

Definition at line 37 of file vmic_ttl.cpp.

Referenced by vmic_read_input().

#define VMIC_PORT78   0x14

Definition at line 38 of file vmic_ttl.cpp.

Referenced by vmic_read_input().

Function Documentation

INT cycle_ram ( )

Definition at line 202 of file rpc_master.cpp.

203 {
204  return ((event_number + 1) % 2) + 1;
205 }
INT vmic_read_input ( int  channel)

Definition at line 116 of file vmic_ttl.cpp.

References channel, status, vme_read_d16(), VMIC_PORT12, VMIC_PORT34, VMIC_PORT56, VMIC_PORT78, and vmic_vme_base.

Referenced by vmic_ttl_poll_live(), and vmic_ttl_start_block().

117 {
118  short status, mask;
119 
120  int reg;
121 
122  // decide which port and bit we're reading
123  if ((channel >= 0) && (channel < 16)) {
124  reg = VMIC_PORT56;
125  mask = (1 << channel);
126  } else if ((channel >= 16) && (channel < 32)) {
127  reg = VMIC_PORT78;
128  mask = (1 << (channel-16));
129  } else if ((channel >= 32) && (channel < 48)) {
130  reg = VMIC_PORT12;
131  mask = (1 << (channel-32));
132  } else if ((channel >= 48) && (channel < 64)) {
133  reg = VMIC_PORT34;
134  mask = (1 << (channel-48));
135  }
136 
137  status = vme_read_d16(vmic_handle, vmic_vme_base | reg);
138 
139  if ((status & mask)==0) {
140  return 0;
141  } else {
142  return 1;
143  }
144 }
void vmic_set ( DWORD  new_value)

Definition at line 90 of file vmic_ttl.cpp.

References vme_write_d16(), vmic_current_high, vmic_current_low, VMIC_PORT12, VMIC_PORT34, and vmic_vme_base.

Referenced by vmic_ttl_init(), vmic_ttl_poll_live(), vmic_ttl_start_block(), and vmic_turn_on().

91 {
92  WORD low = new_value & 0xffff;
93  WORD high = (new_value >> 16) & 0xffff;
94 
95  if(low != vmic_current_low) {
97  vmic_current_low = low;
98  }
99 
100  if(high != vmic_current_high) {
102  vmic_current_high = high;
103  }
104 }
INT vmic_ttl_init ( )

Definition at line 150 of file vmic_ttl.cpp.

References crate_number, diag_print(), not_really_started, odb_find_key(), odb_get_word(), status, SUCCESS, vme_open(), vme_write_d16_checked(), VMEMAP_ASPACE_A16, VMEMAP_DWIDTH_16, VMEMAP_PRGDATAAM_DATA, VMEMAP_SUPUSERAM_SUPER, VMIC_CSRU, vmic_enabled, vmic_set(), and vmic_vme_base.

151 {
152  vmic_enabled =
153  (odb_find_key("/Equipment/Crate %d/Settings/VMIC", crate_number) != 0);
154 
155  if(!vmic_enabled) {
156  return SUCCESS;
157  }
158 
159  diag_print(4,"vmic_ttl_init()\n");
160 
161  vmic_vme_base =
162  odb_get_word("/Equipment/Crate %d/Settings/VMIC/vme address", crate_number);
163 
164  struct vme_mapping_ctrl mapping = {
169  };
170 
171  vmic_handle = vme_open(vmic_vme_base, mapping, 0x100, 0);
172  if(vmic_handle == NULL) {
173  diag_print(0, "Unable to open handle for VMIC TTL I/O register\n");
174  return FE_ERR_HW;
175  }
176 
177  // Set control register: port 4 as input, all others as output
178  int status =
180  if(status != SUCCESS) {
181  diag_print(0, "Unable to write CSRU value to VMIC TTL I/O register\n");
182  return FE_ERR_HW;
183  }
184 
185  // Wait a moment to allow this setting to take effect
186  ss_sleep(400);
187 
188  // Turn off all outputs
189 /*
190  vme_write_d16(vmic_handle, vmic_vme_base | VMIC_PORT12, 0);
191  vmic_current_high = 0;
192  vme_write_d16(vmic_handle, vmic_vme_base | VMIC_PORT34, 0);
193  vmic_current_low = 0;
194 */
195  vmic_set(0);
196 
197  not_really_started = 0;
198 
199  return SUCCESS;
200 }
INT vmic_ttl_poll_live ( )

Definition at line 302 of file vmic_ttl.cpp.

References FE_END_BLOCK, not_really_started, SUCCESS, vmic_enabled, VMIC_INPUT_END_EVENT, vmic_read_input(), and vmic_set().

303 {
304  if(!vmic_enabled) {
305  return SUCCESS;
306  }
307 
308  //diag_print(4,"vmic_ttl_poll_live()\n");
309 
310  INT end_event = vmic_read_input(VMIC_INPUT_END_EVENT);
311 
312  if(end_event || not_really_started) {
313  vmic_set(0);
314  return FE_END_BLOCK;
315  } else {
316  return SUCCESS;
317  }
318 }
INT vmic_ttl_start_block ( )

Definition at line 206 of file vmic_ttl.cpp.

References crate_is_participating(), cycle_ram(), diag_print(), not_really_started, SUCCESS, vmic_enabled, VMIC_INPUT_BLOCK, VMIC_INPUT_END_EVENT, VMIC_OUTPUT_CRATE4, VMIC_OUTPUT_EW1, VMIC_OUTPUT_EW2, VMIC_OUTPUT_LATCH_RESET, VMIC_OUTPUT_START1, VMIC_OUTPUT_START2, vmic_read_input(), and vmic_set().

207 {
208  if(!vmic_enabled) {
209  return SUCCESS;
210  }
211 
212  diag_print(4,"vmic_ttl_start_block()\n");
213 
214  // Is crate 4 going to be part of the cycle?
215  DWORD maybe_crate4 = 0;
216  if(crate_is_participating(4)) {
217  maybe_crate4 = VMIC_OUTPUT_CRATE4;
218  }
219 
220  // If there is already a block in progress, refuse to start another one.
221  int block_input = vmic_read_input(VMIC_INPUT_BLOCK);
222  if(block_input != 0) {
223  diag_print(0, "BLOCK is high when it should be low!\n");
224  vmic_set(0);
225  ss_sleep(10);
227  ss_sleep(10);
228  vmic_set(0);
229  ss_sleep(100);
230  not_really_started = 1;
231  return FE_ERR_HW;
232  }
233 
234  // First send a pulse on the latch reset line:
235  vmic_set(0);
236  vmic_set(VMIC_OUTPUT_LATCH_RESET | maybe_crate4);
237 // vmic_set(0 | maybe_crate4);
238 
239 #if 1
240  // Now check whether the reset was effective--did it make the EndEvent
241  // signal clear?
243  diag_print(0, "END_EVENT signal not clearing with reset\n");
244  not_really_started = 1;
245  return FE_ERR_HW;
246  }
247 #endif
248 
249 #if 0
250  // Try another reset...going straight into the start of the cycle
251  // so that the compressor timing matches the CAENs.
252  vmic_set(VMIC_OUTPUT_LATCH_RESET | maybe_crate4);
253 #endif
254 
255  int ram = cycle_ram();
256 
257  diag_print(2, "Starting RAM %d cycle\n", ram);
258 
259  //
260  // Finally, start the cycle.
261  //
262  if(ram == 1) {
263  vmic_set(VMIC_OUTPUT_START1 | maybe_crate4);
264  vmic_set(VMIC_OUTPUT_EW1 | maybe_crate4);
265  } else {
266  vmic_set(VMIC_OUTPUT_START2 | maybe_crate4);
267  vmic_set(VMIC_OUTPUT_EW2 | maybe_crate4);
268  }
269 
270  // We don't currently have any way of testing that the cycle started
271  // correctly. Perhaps something to think about adding...
272  not_really_started = 0;
273  return SUCCESS;
274 }
INT vmic_ttl_stop_block ( )

Definition at line 280 of file vmic_ttl.cpp.

References diag_print(), SUCCESS, vmic_enabled, VMIC_OUTPUT_SOFTSTOP, and vmic_turn_on().

281 {
282  if(!vmic_enabled) {
283  return SUCCESS;
284  }
285 
286  diag_print(4,"vmic_ttl_stop_block()\n");
287 
288  // set SOFTSTOP output
290 }
void vmic_turn_on ( DWORD  new_channels)

Definition at line 109 of file vmic_ttl.cpp.

References vmic_current_high, vmic_current_low, and vmic_set().

Referenced by vmic_ttl_stop_block().

110 {
111  WORD low = vmic_current_low | (new_channels & 0xffff);
112  WORD high = vmic_current_high | ((new_channels >> 16) & 0xffff);
113  vmic_set((high << 16) | low);
114 }

Variable Documentation

BOOL not_really_started
static

Definition at line 83 of file vmic_ttl.cpp.

Referenced by vmic_ttl_init(), vmic_ttl_poll_live(), and vmic_ttl_start_block().

WORD vmic_current_high
static

Definition at line 79 of file vmic_ttl.cpp.

Referenced by vmic_set(), and vmic_turn_on().

WORD vmic_current_low
static

Definition at line 78 of file vmic_ttl.cpp.

Referenced by vmic_set(), and vmic_turn_on().

BOOL vmic_enabled
static
struct vme_handle* vmic_handle
static

Definition at line 77 of file vmic_ttl.cpp.

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

Definition at line 19 of file vmic_ttl.cpp.

WORD vmic_vme_base
static

Definition at line 76 of file vmic_ttl.cpp.

Referenced by vmic_read_input(), vmic_set(), and vmic_ttl_init().