// sis3302_sgl_event_external_start.cpp : Definiert den Einstiegspunkt für die Konsolenanwendung.
//

#include <Windows.h>
#include <tchar.h>
#include <stdio.h>
#include <iostream>

//#include <stdlib.h>
//#include <errno.h>
//#include <string.h>
//#include <sys/types.h>
//#include <fcntl.h>
#include <time.h>

using namespace std;


#define PCI_VME_INTERFACE		// sis1100/3100 optical interface
//#define USB_VME_INTERFACE

#ifdef PCI_VME_INTERFACE
	#include "sis1100w_vme_class.h"
#endif

sis1100 *gl_vme_crate ;
unsigned int gl_dma_read_data[0x1000000] ;
#include "sis3302.h"

int SIS3302_ConfigurationSetupAdc_SingleEvent_Start (int module_base_address, unsigned int sample_length, unsigned int pretrigger_delay);

int _tmain(int argc, _TCHAR* argv[])
{
CHAR char_messages[128];
UINT nof_found_devices ;

unsigned int addr, data;
unsigned int module_base_addr   ;
unsigned int poll_counter   ;
int return_code ;

UINT request_nof_words;
UINT got_nof_words ;
unsigned int sample_length ;
unsigned int pretrigger_delay ;
unsigned int i ;

unsigned short *adc_data_ptr ;

	printf("Hallo \n");      
#ifdef PCI_VME_INTERFACE
	// create SIS1100/SIS310x vme interface device
	gl_vme_crate = new sis1100(0);
#endif

// open Vme Interface device
	return_code = gl_vme_crate->vmeopen ();  // open Vme interface
	gl_vme_crate->get_vmeopen_messages (char_messages, &nof_found_devices);  // open Vme interface
	printf("\n%s\   (found %d vme interface device[s])\n\n",char_messages, nof_found_devices);

	if(return_code != 0x0) {
		printf("ERROR: vme_crate->vmeopen: return_code = 0x%08x\n\n", return_code);
		//program_stop_and_wait();
		return -1 ;
	}

	module_base_addr = 0x30000000;
	return_code = gl_vme_crate->vme_A32D32_read ( module_base_addr + 0x4, &data);  
	printf("vme_A32D32_read: data = 0x%08x     return_code = 0x%08x\n", data, return_code);



	// setup
	//sample_length = 0x2000 ; // 8192
	sample_length = 0x400 ; // 1024
	//sample_length = 128 ; // 128
	pretrigger_delay = sample_length/2 ; //max 4095
	if (pretrigger_delay > 4095) {pretrigger_delay = 4095;}
	SIS3302_ConfigurationSetupAdc_SingleEvent_Start (module_base_addr,  sample_length, pretrigger_delay);

	do { // main loop

		// 1. arm 
		addr = module_base_addr + SIS3302_KEY_ARM   ;
		return_code = gl_vme_crate->vme_A32D32_write ( addr, 0);  
		//printf("vme_A32D32_write: addr = 0x%08x     return_code = 0x%08x\n", addr, return_code);



		// wait for stop and disarm 
		poll_counter=0;
		addr = module_base_addr + SIS3302_ACQUISTION_CONTROL   ;
		do {
			return_code = gl_vme_crate->vme_A32D32_read ( addr, &data);  
			if (return_code != 0) { printf("\nvme_A32D32_read:  return_code = 0x%08x at addr = 0x%08x\n",return_code,addr);return -1;}
   			poll_counter++;
			if (poll_counter >= 500000) {
				printf("waiting for LEMO STOP_IN pulse \n");
				poll_counter=0;
			}
		}  while ( (data & 0x10000 ) == 0x10000 ) ; /*  */
		printf("got external trigger \n");


// this is do in IRQ Ackn. routine
		data = 0x10000 ; //  Clear/Disable IRQ: End of Event
		addr = module_base_addr + SIS3302_IRQ_CONTROL  ;
		return_code = gl_vme_crate->vme_A32D32_write( addr, data ) ;
		if (return_code != 0) { printf("\vme_A32D32_write:  return_code = 0x%08x at addr = 0x%08x\n",return_code,addr);return -1;}

		data = 1 ; //  Enable IRQ again: End of Event
		addr = module_base_addr + SIS3302_IRQ_CONTROL  ;
		return_code = gl_vme_crate->vme_A32D32_write( addr, data ) ;
		if (return_code != 0) { printf("\vme_A32D32_write:  return_code = 0x%08x at addr = 0x%08x\n",return_code,addr);return -1;}

		data = 0x80000000 ; //  Update IRQ Pulse, has to be used in case if a new event happened
		addr = module_base_addr + SIS3302_IRQ_CONTROL  ;
		return_code = gl_vme_crate->vme_A32D32_write( addr, data ) ;
		if (return_code != 0) { printf("\vme_A32D32_write:  return_code = 0x%08x at addr = 0x%08x\n",return_code,addr);return -1;}





	// ADC1
		// read actual sample address
		addr = module_base_addr + SIS3302_ACTUAL_SAMPLE_ADDRESS_ADC1  ;
		return_code = gl_vme_crate->vme_A32D32_read ( addr, &data);  
		printf("SIS3302_ACTUAL_SAMPLE_ADDRESS_ADC1 = 0x%08x  \n", data);


	// read data from Channel 1
		addr = module_base_addr + SIS3302_ADC1_OFFSET  ;
		request_nof_words = (sample_length/2)  ; // must on 8-Byte boundary because MBLT64 read

		gl_vme_crate->vme_A32MBLT64_read ( addr, gl_dma_read_data, request_nof_words, &got_nof_words ) ;
		printf("vme_A32MBLT64_read: got_nof_words = 0x%08x  \n", got_nof_words);
		
		// print data
		adc_data_ptr = (unsigned short*) gl_dma_read_data ;
		for (i=0; i<(sample_length);i=i+8 ) {
			printf("i = 0x%08x      0x%04x    0x%04x    0x%04x    0x%04x    0x%04x    0x%04x    0x%04x    0x%04x \n", 2*i, 
								adc_data_ptr[i], adc_data_ptr[i+1], adc_data_ptr[i+2], adc_data_ptr[i+3],
								adc_data_ptr[i+4], adc_data_ptr[i+5], adc_data_ptr[i+6], adc_data_ptr[i+7]) ;
			}


	}  while ( 1) ; /*  */

	return 0;
}








/***********************************************************************************************************************************/
/* --------------------------------------------------------------------------
   SIS3302 Configuration 
-------------------------------------------------------------------------- */
int SIS3302_ConfigurationSetupAdc_SingleEvent_Start (int module_base_address, unsigned int sample_length, unsigned int pretrigger_delay)
{
	unsigned int return_code;
	unsigned int data, addr;


	/*  Reset SIS3302  */
	addr = module_base_address + SIS3302_KEY_RESET   ;
	return_code = gl_vme_crate->vme_A32D32_write( addr, 0x0 ) ;
	if (return_code != 0) { printf("\vme_A32D32_write:  return_code = 0x%08x at addr = 0x%08x\n",return_code,addr);return -1;}

	// set IRQ Level and vector
	data = (0x3) << 8 ; // IRQ Level 
	data = data + 0x800 ; // enable IRQ 
//	data = data + 0x1000 ; // ROAK release on IRQ Ack 
 	data = data + 0x0000 ; // RORA release on Register Access 
	data = data + 0x55 ; // dummy IRQ Vector
	addr = module_base_address + SIS3302_IRQ_CONFIG  ;
	return_code = gl_vme_crate->vme_A32D32_write( addr, data ) ;
	if (return_code != 0) { printf("\vme_A32D32_write:  return_code = 0x%08x at addr = 0x%08x\n",return_code,addr);return -1;}

	data = 1 ; //  Enable IRQ: End of Event
	addr = module_base_address + SIS3302_IRQ_CONTROL  ;
	return_code = gl_vme_crate->vme_A32D32_write( addr, data ) ;
	if (return_code != 0) { printf("\vme_A32D32_write:  return_code = 0x%08x at addr = 0x%08x\n",return_code,addr);return -1;}

		


	//set Clock source
	data = SIS3302_ACQ_SET_CLOCK_TO_SECOND_100MHZ ;  		
		
	//data = data + SIS3302_ACQ_DISABLE_LEMO_START_STOP ; 
	data = data + SIS3302_ACQ_ENABLE_LEMO_START_STOP ; 
	

	data = data + SIS3302_ACQ_DISABLE_INTERNAL_TRIGGER ; 
	//data = data + SIS3302_ACQ_ENABLE_INTERNAL_TRIGGER ; 

	data = data + SIS3302_ACQ_DISABLE_AUTOSTART ; 
	//data = data + SIS3302_ACQ_ENABLE_AUTOSTART ; 

	data = data + SIS3302_ACQ_DISABLE_MULTIEVENT ; 
	//data = data + SIS3302_ACQ_ENABLE_MULTIEVENT ; 
		
	addr = module_base_address + SIS3302_ACQUISTION_CONTROL  ;
	return_code = gl_vme_crate->vme_A32D32_write( addr, data ) ;
	if (return_code != 0) { printf("\vme_A32D32_write:  return_code = 0x%08x at addr = 0x%08x\n",return_code,addr);return -1;}



	// no Start Delay
	addr = module_base_address + SIS3302_START_DELAY  ;
	data = 5  ; // set to 5 !! Delay for autostart - should be greater 4
	return_code = gl_vme_crate->vme_A32D32_write( addr, data ) ;
	if (return_code != 0) { printf("\vme_A32D32_write:  return_code = 0x%08x at addr = 0x%08x\n",return_code,addr);return -1;}


	//  Stop Delay
	addr = module_base_address + SIS3302_STOP_DELAY  ;
	data = 0  ; //  
	return_code = gl_vme_crate->vme_A32D32_write( addr, data ) ;
	if (return_code != 0) { printf("\vme_A32D32_write:  return_code = 0x%08x at addr = 0x%08x\n",return_code,addr);return -1;}


	// Ringbuffer Delay (PreTrigger)
	data = pretrigger_delay ; //  max. value = 4095
	addr = module_base_address + SIS3302_PRETRIGGER_DELAY_ALL_ADC  ;
	return_code = gl_vme_crate->vme_A32D32_write( addr, data ) ;
	if (return_code != 0) { printf("\vme_A32D32_write:  return_code = 0x%08x at addr = 0x%08x\n",return_code,addr);return -1;}

	// Event Configuration register 
	data = EVENT_CONF_ENABLE_SAMPLE_LENGTH_STOP ;
	//data =    EVENT_CONF_ENABLE_WRAP_PAGE_MODE  
	//        + EVENT_CONF_PAGE_SIZE_4K_WRAP  ;
	addr = module_base_address + SIS3302_EVENT_CONFIG_ALL_ADC  ;
	return_code = gl_vme_crate->vme_A32D32_write( addr, data ) ;
	if (return_code != 0) { printf("\vme_A32D32_write:  return_code = 0x%08x at addr = 0x%08x\n",return_code,addr);return -1;}



	// Sample Length
	data =sample_length-1 ; // //  
	addr = module_base_address + SIS3302_SAMPLE_LENGTH_ALL_ADC  ;
	return_code = gl_vme_crate->vme_A32D32_write( addr, data ) ;
	if (return_code != 0) { printf("\vme_A32D32_write:  return_code = 0x%08x at addr = 0x%08x\n",return_code,addr);return -1;}

	// sample start Address
	data = 0 ;
	addr = module_base_address + SIS3302_SAMPLE_START_ADDRESS_ALL_ADC  ;
	return_code = gl_vme_crate->vme_A32D32_write( addr, data ) ;
	if (return_code != 0) { printf("\vme_A32D32_write:  return_code = 0x%08x at addr = 0x%08x\n",return_code,addr);return -1;}

// Clear all DACs
	data =  3 ; // Clear cmd
	addr = module_base_address + SIS3302_DAC_CONTROL_STATUS  ;
	return_code = gl_vme_crate->vme_A32D32_write( addr, data ) ;
	if (return_code != 0) { printf("\vme_A32D32_write:  return_code = 0x%08x at addr = 0x%08x\n",return_code,addr);return -1;}
	
 	
 	return 0x0 ;

	
} // end function  ()


