/* file: bpmd.c SLAC BPMD module test code W. Ross, SLAC Created 4/30/07, WCR Modified 8/15/07, WCR (Initial debugging) */ #define DUT_IS_BPMD TRUE #define DUT_IS_AB FALSE #include #include #include #include #include #include #include #include "test_dma.h" #include "test_bpmd.h" #define TSI_CSR_ADDR 0x9e0ff000 #define AB_PCI_ADDR 0x9fc00120 #define AB_VME_ADDR 0x00c00120 #define VME_CSR_SIZE 0x00080000 /* THIS CODE REQUIRES THAT A VME BUS WINDOW FOR CR/CSR ACCESSES HAS BEEN DEFINED AT PCI ADDRESS 0x9E000000 */ #define PORT0_MAP \ "0: 0x20000000 0x0e000000 0x90000000 A32, SUP, D32, SCT\n" #define PORT1_MAP \ "1: 0x00000000 0x00ff0000 0x9f000000 A24, SUP, D32, SCT\n" #define PORT2_MAP \ "2: 0x00000000 0x00010000 0x9fff0000 A16, SUP, D32, SCT\n" #define PORT3_MAP \ "3: 0x00000000 0x01000000 0x9e000000 CSR, SUP, D32, SCT\n" #define A32VME_BASE 0x20000000 #define A32PCI_BASE 0x90000000 #define A24VME_BASE 0x00000000 #define A24PCI_BASE 0x9f000000 #define A16VME_BASE 0x00000000 #define A16PCI_BASE 0x9fff0000 #define CR_VME_BASE 0x00000000 #define CR_PCI_BASE 0x9e000000 #define A32_SLOT_ALLOC 0x00700000 #define MAX_XFERS_PER_MBLT 256 #define MBLT_BYTES_PER_XFER 8 /* Define a buffer size for accepting DMA transfers */ #define SML_BUF_SZ 128 static unsigned SmlMemBlock[SML_BUF_SZ]; /* define large buffer for memory testing using MBLT DMAs */ #define LRG_BUF_SZ 32768 typedef unsigned LargeBuf[LRG_BUF_SZ]; typedef unsigned *p_LargeBuf; /******************************************************/ static unsigned le2be(unsigned value) { /* reverse the order of the four bytes in a 32-bit word */ unsigned uppers, lowers, newval; uppers = (value & 0xff00ff00)>>8; lowers = (value & 0x00ff00ff)<<8; newval = uppers | lowers; uppers = (newval & 0xffff0000)>>16; lowers = (newval & 0x0000ffff)<<16; return( uppers | lowers); } /******************************************************/ static int repack_cr_val(volatile unsigned *p_source, int n_wrds, unsigned *p_packed_val) { /* pack the least significant byte of n words into one word */ int i, pack_stat; *p_packed_val = 0x00000000; pack_stat = OK_STAT; if ((n_wrds < 1) || (n_wrds > 4)) { printf("\nrepqack_cr_val: parameter error, n_wrds\n"); return(PROG_ERROR); } for (i=0; i 4)) { printf("\nsplit_csr_val: parameter error, n_wrds\n"); return(PROG_ERROR); } for (i=(n_wrds-1); i>=0; i--) { p_csr[i] = source_val & 0x000000ff; source_val = source_val >> 8; } return(OK_STAT); } /******************************************************/ static int test_checksum(unsigned cr_addr) { #define MAX_CHKSUM_LEN 0x1000 /* perform a checksum calculation on every 4th byte in the VME64 standard CR area starting at 'cr_addr'. 32-bit words are read, but the top 3 bytes of each word are expected to contain 0x000000 */ unsigned packed_val; unsigned *p_rom_word; unsigned cur_count, total_count; unsigned reqd_chksum, chksum_accum; int rtn_stat, pack_stat, nonzero_msb_cnt; printf("\n Performing VME CR checksum test...\n"); rtn_stat = OK_STAT; nonzero_msb_cnt = 0; /* Read checksum value from VME64 ROM */ p_rom_word = (unsigned *)cr_addr; pack_stat = repack_cr_val(p_rom_word, CR_CHKSUM_NWRDS, &reqd_chksum); if (pack_stat == PROG_ERROR) return(PROG_ERROR); if (pack_stat == WARN_STAT) ++nonzero_msb_cnt; printf(" Expected checksum value is: 0x%08X\n", CR_CHKSUM_IVAL); printf(" Checksum value in ROM is: 0x%08X\n", reqd_chksum); if (reqd_chksum != CR_CHKSUM_IVAL) { printf( "\ntest_checksum: VM64E ROM contains unexpected checksum value\n"); rtn_stat = WARN_STAT; } /* read number of bytes to check from VME64 ROM */ p_rom_word += CR_CHKSUM_NWRDS; pack_stat = repack_cr_val(p_rom_word, CR_ROM_LEN_NWRDS, &total_count); if (pack_stat == PROG_ERROR) return(PROG_ERROR); if (pack_stat == WARN_STAT) ++nonzero_msb_cnt; printf("\n Expected byte count value is: 0x%08X\n", CR_ROM_LEN_IVAL); printf(" Byte count value in ROM is: 0x%08X\n", total_count); if (total_count != CR_ROM_LEN_IVAL) { printf( "\ntest_checksum: VM64E ROM contains unexpected byte count value\n"); rtn_stat = WARN_STAT; } if (total_count > MAX_CHKSUM_LEN) { printf( "\ntest_checksum: VM64E ROM byte count too large\n"); return(WARN_STAT); } /* Accumulate the checksum from ROM */ chksum_accum = 0; nonzero_msb_cnt = 0; for (cur_count=0; cur_count %08x\n",reg_value); reg_value = *(unsigned *)(bridge_base + INTR_ENO_OFS); printf("Interrupt out enable => %08x\n",reg_value); reg_value = *(unsigned *)(bridge_base + INTR_STT_OFS); printf("Interrupt status => %08x\n",reg_value); /* Start DMA transfer */ printf("\nDMA Go!\n"); *(unsigned *)(bridge_base + DMA0_CTL_OFS) = (dma_ctrl | D_GO); /* put this program to sleep so DMA can obtain the bus */ rtems_stat = rtems_task_wake_after((rtems_interval) 50); intrpt_stat = *(unsigned *) (bridge_base + INTR_STT_OFS); reg_value = intrpt_stat & (INTR_DMAC0 | INTR_PERR | INTR_VERR); i = 0; max_retries = 3; while ((reg_value == 0) && (i < max_retries)) { rtems_stat = rtems_task_wake_after((rtems_interval) 50); intrpt_stat = *(unsigned *) (bridge_base + INTR_STT_OFS); reg_value = intrpt_stat & (INTR_DMAC0 | INTR_PERR | INTR_VERR); ++i; } printf("\n %d of %d bus access retries\n", i, max_retries); printf("\nInterrupt status => %08x\n", intrpt_stat); reg_value = *(unsigned *)(bridge_base + INTR_ENA_OFS); printf("Interrupt enable => %08x\n",reg_value); /* Clear the DMA interrupt pending flags */ *(unsigned *)(bridge_base + INTR_CLR_OFS) = (INTR_DMAC0 | INTR_PERR | INTR_VERR); dma_stat = *(unsigned *) (bridge_base + DMA0_STA_OFS); printf("DMA completion status => %08x\n\n",dma_stat); switch (dma_stat) { case D_STT_ERR: printf("DMA BUS ERROR\n"); rtn_stat = WARN_STAT; break; case D_STT_ABORT: printf("DMA ABORT\n"); rtn_stat = WARN_STAT; break; case D_STT_PAUSE: printf("DMA PAUSE\n"); rtn_stat = WARN_STAT; break; case D_STT_BUSY: printf("DMA indicates still busy ???\n"); rtn_stat = WARN_STAT; break; case D_STT_DONE: printf("%u-BYTE DMA SUCCESSFUL COMPLETION\n", dma_n_bytes); rtn_stat = OK_STAT; break; default: printf("DMA completed with unknown status code\n"); rtn_stat = WARN_STAT; } /* disable Tsi148 bus-bridge DMA interrupts */ *(unsigned *)(bridge_base + INTR_ENA_OFS) = intrpt_lvl_ctl; return(rtn_stat); } /******************************************************/ static int check_dcms(unsigned cr_addr) { int n_not_rdy; short unsigned reg16_value; n_not_rdy = 0; /* Check DCMs for DLL Locked status */ reg16_value = *(short unsigned *)(cr_addr + CSR_D4S_OFS); if (reg16_value == (CSR_DCS_LOCKED | CSR_DCS_ADJ_DONE)) printf("DCM-4 OK\n"); else { printf("DCM-4 Bad Status: 0x%04x\n", reg16_value); ++n_not_rdy; } reg16_value = *(short unsigned *)(cr_addr + CSR_D3S_OFS); if (reg16_value == (CSR_DCS_LOCKED | CSR_DCS_ADJ_DONE)) printf("DCM-3 OK\n"); else { printf("DCM-3 Bad Status: 0x%04x\n", reg16_value); ++n_not_rdy; } reg16_value = *(short unsigned *)(cr_addr + CSR_D2S_OFS); if (reg16_value == (CSR_DCS_LOCKED | CSR_DCS_ADJ_DONE)) printf("DCM-2 OK\n"); else { printf("DCM-2 Bad Status: 0x%04x\n", reg16_value); ++n_not_rdy; } reg16_value = *(short unsigned *)(cr_addr + CSR_D1S_OFS); if (reg16_value == (CSR_DCS_LOCKED | CSR_DCS_ADJ_DONE)) printf("DCM-1 OK\n"); else { printf("DCM-1 Bad Status: 0x%04x\n", reg16_value); ++n_not_rdy; } reg16_value = *(short unsigned *)(cr_addr + CSR_D0S_OFS); if (reg16_value == (CSR_DCS_LOCKED | CSR_DCS_ADJ_DONE)) printf("DCM-0 OK\n"); else { printf("DCM-0 Bad Status: 0x%04x\n", reg16_value); ++n_not_rdy; } return(n_not_rdy); } /******************************************************/ static void print_acq_help() { /* Commands are: * q : exit the loop * h : help on command list * n : set number of samples per acq * c : set DCM delays, degrees of clock frequency * p <"path"> : set path, default: /data/bpmd_dat/ * f <"file name"> : set output file name * k <"file name">: load kernel vector * v <"file name">: load data test vector * w : set DSP window * a : set acquisition * m : set ADC mode * g : 'go', arm and software trigger acquisition * d : dump to file * o : set DAC output source * t : transfer n bits of serial data * s : strobe acquisition status, DCM status, and report */ printf("\n Acquisition Loop Commands:\n"); printf(" q : exit loop\n"); printf(" h : display command list\n"); printf(" n (number_of_samples> : set number of samples to acquire\n"); printf(" c (delay> : set DCM delays, degrees (0 = bypass DCM)\n"); printf(" p : set file path, default: /data/bpmd_dat/\n"); printf(" f : set file name for data dump\n"); printf(" k : load kernel vector\n"); printf(" v : load data test vector\n"); printf(" w : set DSP window\n"); printf(" a : set acquisition\n"); printf(" m : set mode\n"); printf(" g : 'go', arm and software trigger acquisition\n"); printf(" d : dump acquired ADC data to file\n"); printf(" o : set DAC output sources\n"); printf(" t : transfer serial data\n"); printf(" s : show status and control registers\n\n"); return; } /******************************************************/ static int dump_to_file(int n_samples, char *file_path, char *file_name, unsigned a32_pci_addr) { int i, file_stat, n_written; char *file_path_name; FILE *fp; short int *sample_ptr; file_path_name = calloc(512, sizeof(char)); sprintf(file_path_name, "%s/%s", file_path, file_name); printf("\n Opening file %s...\n", file_path_name); fp = fopen(file_path_name,"wb"); if (fp == NULL) { printf("File open ERROR\n"); return(WARN_STAT); } printf(" Writing 4x %d <0x%x> samples ...\n", n_samples, n_samples); sample_ptr = (short int *)(a32_pci_addr + F0_DMEM_C4C3_OFS); for (i=0; i sample sets written\n", i, i); file_stat = fclose(fp); if ( i != n_samples) return(WARN_STAT); return(OK_STAT); } /******************************************************/ static int load_from_file(int max_samples, char *file_path, char *file_name, unsigned buffer_offset, unsigned a32_pci_addr) { int i, file_stat, n_read; char *file_path_name; FILE *fp; short int *sample_ptr; file_path_name = calloc(512, sizeof(char)); sprintf(file_path_name, "%s/%s", file_path, file_name); printf("\n Opening file %s...\n", file_path_name); fp = fopen(file_path_name,"rb"); if (fp == NULL) { printf("File open ERROR\n"); return(WARN_STAT); } sample_ptr = (short int *)(a32_pci_addr + buffer_offset); for (i=0; i sample sets loaded\n", i, i); file_stat = fclose(fp); return(i); } /******************************************************/ static int user_check() { char std_in_line[80]; int cmd_len; printf("\nEnter 'c' to continue, anything else to exit: "); fflush(stdout); fgets(std_in_line, 80, stdin); if (((cmd_len = strlen(std_in_line)) != 2) || (std_in_line[0] != 'c' && std_in_line[0] != 'C')) { printf("Exiting test program.\n"); return(EXIT_STAT); } printf("\n==================================================\n\n"); return(OK_STAT); } /****************************************************** * * Top level test program * *******************************************************/ #define MAX_LINE 80 #define MAX_BRD_ID_LEN 256 #define MAX_FILE_NAME 256 #define DCM_DELAY (192 + CSR_DCC_RETARD90) void tstbpmd() { rtems_status_code rtems_stat; rtems_interval tick_interval; char std_in_line[MAX_LINE]; char brd_id_line[MAX_BRD_ID_LEN]; char data_file_name[MAX_FILE_NAME]; char *p_file_name = data_file_name; char data_file_path[MAX_FILE_NAME]; char *p_file_path = data_file_path; char krn_file_name[MAX_FILE_NAME]; char *p_krn_name = krn_file_name; char vect_file_name[MAX_FILE_NAME]; char *p_vect_name = vect_file_name; char name_default[] = "bpmd_data"; char path_default[] = "/data/bpmd_dat"; char kernel_default[] = "bpmd_kernel"; char vector_default[] = "bpmd_vector"; char *p_char; int i, cmd_len, dut_slot; int pack_stat, rtn_stat, dma_stat; unsigned intrpt_lvl_ctl; unsigned address; unsigned cr_vme_addr; unsigned cr_pci_addr; unsigned a32_vme_addr; unsigned a32_pci_addr; unsigned mem_tst_value; unsigned mem_rtn_value; unsigned bridge_base; unsigned dma_n_bytes; unsigned dma_blk_size; unsigned reg_value; unsigned *p_reg_value; short unsigned reg16_value; int reg_init_val_err; int dbuf_err_count; int kbuf_err_count; int max_reports; int n_bad_dcms; int delay_bits; int dcms_bypassed; int dcms_cur_delay; int quit_acq_loop; int new_int1, new_int2, new_int3, new_int4, new_int5; int acq_len; int n_loaded, max_samples; unsigned dsp_op, seq_id, trig_src, trig_op, sh_gain, adc_mode; unsigned hi_gain, ext_clk, alt_fmt, random, dither; unsigned dac_on_off, dac_ch_a, dac_ch_b, dac_sync; unsigned acq_ctl_reg_base; unsigned aux_ctl_reg_base; unsigned aux_ctl_usr_mode; unsigned dac_ctl_val; unsigned new_krn_loc, new_krn_len, new_vect_loc, new_vect_len; unsigned new_unsigned, serial_dat, serial_loop, serial_nbits; short unsigned serial_ctl; p_LargeBuf p_data_buf, p_data_val; unsigned exponents, expn1, expn2, expn3, expn4; double pwr1, pwr2, pwr3, pwr4; double mag1, mag2, mag3, mag4; /* Be sure 'cr_pci_addr' is set up for the following macros */ #define P_CR16(A) ((volatile short unsigned *)((cr_pci_addr + A) & 0xfffffffe)) #define P_CR32(A) ((volatile unsigned *)((cr_pci_addr + A) & 0xfffffffc)) /* Be sure 'a32_pci_addr' is set up for the following macros */ #define P_BUF16(A) ((short unsigned *)((a32_pci_addr + A) & 0xfffffffe)) #define P_BUF32(A) ((unsigned *)((a32_pci_addr + A) & 0xfffffffc)) printf("\n\nBPMD TEST PROGRAM\n\n"); printf("Check PCI-X/VME Ports Setup:\n\n"); printf("\nThe current port mapping is:\n"); BSP_VMEOutboundPortsShow(NULL); printf("\nThe port mapping MUST include a CSR space as below:\n\n"); printf(PORT0_MAP); printf(PORT1_MAP); printf(PORT2_MAP); printf(PORT3_MAP); printf("\n(Additional ports may also be defined.)\n"); printf("\nEnter 'c' to continue, anything else to exit: "); fflush(stdout); fgets(std_in_line, MAX_LINE, stdin); if (((cmd_len = strlen(std_in_line)) != 2) || (std_in_line[0] != 'c' && std_in_line[0] != 'C')) { printf("Exiting test program.\n"); return; } printf("Getting tick interval, should be 50 ticks/second:\n"); tick_interval = 0; rtems_stat = rtems_clock_get ( RTEMS_CLOCK_GET_TICKS_PER_SECOND, &tick_interval ); switch (rtems_stat) { case RTEMS_SUCCESSFUL: printf("\nGet Tick: Tick interval = %u ticks/sec.\n", tick_interval); if (tick_interval != 50) { printf("Wrong tick interval. Exiting test program\n"); return; } break; case RTEMS_NOT_DEFINED: printf("\nGet Tick: Time-date not set. Exiting test program\n"); return; case RTEMS_INVALID_ADDRESS: default: printf("\nGet Tick: Unknown return flag. Exiting test program\n"); return; } /* check for presence of Tsi148 bus bridge (i.e., the processor is an MVME6100, not MVME5500) */ printf( "\nChecking Slot-1 CSR space for Tsi148 bus bridge:\n"); address = TSI_CSR_ADDR; printf("at address 0x%08x\n", address); p_reg_value = (unsigned *)address; if (*p_reg_value != BYTESWAPPED_TSI148_ID) { printf("Tsi148 Bus Bridge ID not found. Exiting test program\n"); return; } else printf("Found Tsi148 Bus Bridge ID\n"); /* Look into the VME bus slot-1 CSR area to get the PCI-X base address for Tsi148 registers */ /* Note: The PCFS registers are little endian to big endian swapped when seen from the processor, hence le2be() is used to un-swap the bytes. (The register groups LCSR, GCSR and CR/CSR are as is) */ address = TSI_CSR_ADDR + TSI148_MBARL_OFS; bridge_base = le2be(*(unsigned *) address) - 0x4; printf("\nTsi148 PCI-X BAR at location %08x = %08x\n", address, bridge_base); if (bridge_base == 0x0) { printf("Tsi148 PCI-X BAR is NULL. Exiting test program.\n"); return; } /* Get the slot number of the installed BPM Digitizer module */ dut_slot = 0; while ((dut_slot < 2) || (dut_slot > 21)) { printf("\nEnter BPMD SLOT NUMBER (2 to 21, defaults to 4): "); fflush(stdout); fgets(std_in_line, MAX_LINE, stdin); if ((cmd_len = strlen(std_in_line)) == 1) { printf("Defaulting to slot-4\n"); dut_slot = 4; } else { dut_slot = atoi(std_in_line); if ((dut_slot < 2) || (dut_slot > 21)) printf("Out of range slot number\n"); } } printf("BPMD slot number is %d\n", dut_slot); cr_vme_addr = (dut_slot * VME_CSR_SIZE) + CR_VME_BASE; cr_pci_addr = (dut_slot * VME_CSR_SIZE) + CR_PCI_BASE; #if DUT_IS_AB /* Allen Bradley module used for testing this software */ cr_vme_addr = AB_VME_ADDR; cr_pci_addr = AB_PCI_ADDR; #endif /* Attempt to read first value in CR */ printf("\nAttempting to read modules CR...\n"); printf( "Enter q to exit, to read, anything else to proceed > "); fflush(stdout); fgets(std_in_line, MAX_LINE, stdin); while ((cmd_len = strlen(std_in_line)) < 2 ) { reg16_value = *P_CR16(CR_CHKSUM_OFS); printf(" 0x%04x > ", reg16_value); fflush(stdout); fgets(std_in_line, MAX_LINE, stdin); } if ((cmd_len == 2) && (std_in_line[0] == 'q')) return; /* test DMA read */ /* Load the receiving buffer area with constant '0xdeadbeef' */ printf("\n\nPre-loading %u-byte buffer area with: '0xdeadbeef'\n", (SML_BUF_SZ * 4)); for (i=0; i : set number of samples per acq * c : set path, default: /data/bpmd_dat/ * f <"file name"> : set output file name * k <"file name">: load kernel vector * v <"file name">: load data test vector * w : set DSP window * a : set acquisition * m : set ADC mode * g : 'go', arm and software trigger acquisition * d : dump to file * o : set DAC output source * t : transfer n bits of serial data * s : strobe acquisition status, DCM status, and report */ dsp_op = 0; seq_id = 0; trig_src = 0; trig_op = 1; sh_gain = 0; acq_ctl_reg_base = CSR_ACQ_TRG_MODE; *P_CR16(CSR_ACQ_OFS) = acq_ctl_reg_base; hi_gain = 0; ext_clk = 0; alt_fmt = 0; random = 0; dither = 0; serial_dat = 0; serial_nbits = 0; serial_loop = 0; aux_ctl_usr_mode = 0; *P_CR16(CSR_AUX_OFS) = aux_ctl_usr_mode; adc_mode = 0; aux_ctl_reg_base = 0; *P_CR16(CSR_AUX_OFS) = aux_ctl_reg_base; acq_len = 0; *P_CR16(CSR_ACP_OFS) = (CSR_ACP_MAX_PRS - acq_len); *P_CR16(CSR_KLR_OFS) = 0; /* kernel length */ *P_CR16(CSR_KOF_OFS) = 0; /* kernel offset */ *P_CR16(CSR_DOF_OFS) = 0; /* data offset */ *P_CR16(CSR_BAB_OFS) = 0; /* auxilary bits */ *P_CR16(CSR_INT_OFS) = 0; /* interrupt control */ *P_CR16(CSR_DAC_OFS) = 0; /* dac output control */ dac_ctl_val = 0; delay_bits = DCM_DELAY; p_file_name = strncpy(p_file_name, name_default, MAX_FILE_NAME); p_file_path = strncpy(p_file_path, path_default, MAX_FILE_NAME); p_krn_name = strncpy(p_krn_name, kernel_default, MAX_FILE_NAME); p_vect_name = strncpy(p_vect_name, vector_default, MAX_FILE_NAME); new_krn_loc = 0; new_krn_len = 0; new_vect_loc = 0; new_vect_len = 0; dac_on_off = 0; dac_ch_a = 0; dac_ch_b = 0; dac_sync = 0; quit_acq_loop = FALSE; while (quit_acq_loop == FALSE) { printf("\n acq_loop: enter q,h,n,c,p,f,k,v,w,a,m,g,d,o,t,s > "); fflush(stdout); fgets(std_in_line, MAX_LINE, stdin); if ((cmd_len = strlen(std_in_line)) < 2 ) print_acq_help(); else { switch (std_in_line[0]) { case 'q': /* exit loop */ if (cmd_len == 2) quit_acq_loop = TRUE; else print_acq_help(); break; case 'n': /* set number of samples */ i = sscanf(std_in_line, "n %i", &new_int1); if (i == 1) { if ((new_int1 > 0) && (new_int1 <= CSR_ACP_MAX_PRS)) acq_len = new_int1; else printf( "\n New length is out of range %d-%d, 0x%x-0x%x\n", 1, CSR_ACP_MAX_PRS, 1, CSR_ACP_MAX_PRS); } printf("\n Acquisition length = %d: 0x%x\n", acq_len, acq_len); *P_CR16(CSR_ACP_OFS) = (CSR_ACP_MAX_PRS - acq_len) & 0x3fff; break; case 'c': /* set DCMs delay */ i = sscanf(std_in_line, "c %i", &new_int1); if (i == 1) { if ((new_int1 >= 0) && (new_int1 <= 360)) { dcms_cur_delay = new_int1; if (dcms_cur_delay == 0) { *P_CR16(CSR_D4C_OFS) = (delay_bits + CSR_DCC_BYPASS); *P_CR16(CSR_D3C_OFS) = (delay_bits + CSR_DCC_BYPASS); *P_CR16(CSR_D2C_OFS) = (delay_bits + CSR_DCC_BYPASS); *P_CR16(CSR_D1C_OFS) = (delay_bits + CSR_DCC_BYPASS); *P_CR16(CSR_D0C_OFS) = (delay_bits + CSR_DCC_BYPASS); dcms_bypassed = TRUE; } else { /* Set the delays of all five DCMs */ if (dcms_cur_delay > 100) { delay_bits = (CSR_DCC_RETARD90 + floor(((dcms_cur_delay-90)*(256.0/360.0))+0.5)); } else { delay_bits = floor((dcms_cur_delay*(256.0/360.0))+0.5); } printf("\nResetting DCMs...\n"); *P_CR16(CSR_D0C_OFS) = (delay_bits + CSR_D0C_RST_ALL); *P_CR16(CSR_D0C_OFS) = delay_bits; /* Give some time for the DCMs to reset and re-lock */ rtems_stat = rtems_task_wake_after((rtems_interval) 10); dcms_bypassed = FALSE; *P_CR16(CSR_D4C_OFS) = delay_bits; *P_CR16(CSR_D3C_OFS) = delay_bits; *P_CR16(CSR_D2C_OFS) = delay_bits; *P_CR16(CSR_D1C_OFS) = delay_bits; *P_CR16(CSR_D0C_OFS) = delay_bits; *P_CR16(CSR_D0C_OFS) = (delay_bits + CSR_D0C_ADJ_ALL); *P_CR16(CSR_D0C_OFS) = delay_bits; /* Give some time for the DCMs adjust phase */ rtems_stat = rtems_task_wake_after((rtems_interval) 10); n_bad_dcms = check_dcms(cr_pci_addr); if (n_bad_dcms != 0) printf("%d DCMs not ready.\n:", n_bad_dcms); } } else printf( "\n New DCMs delay is out of range %d-%d degrees\n", 0, 360); } printf("\n DCM delay value = %d degrees\n", dcms_cur_delay); break; case 'p': /* set path name */ if (cmd_len == 2) p_file_path = strncpy(p_file_path, path_default, MAX_FILE_NAME); else { i = sscanf(std_in_line, "p %254s", p_file_path); } printf("\n Output file path: %s\n", data_file_path); break; case 'f': /* set file name */ if (cmd_len == 2) p_file_name = strncpy(p_file_name, name_default, MAX_FILE_NAME); else { i = sscanf(std_in_line, "f %254s", p_file_name); } printf("\n Output file name: %s\n", data_file_name); break; case 'k': /* load kernel from file */ if (cmd_len == 2) printf("\n Kernel file name: %s, location: 0x%x\n", krn_file_name, new_krn_loc); else { i = sscanf(std_in_line, "k %254s %i", p_krn_name, &new_int1); if ((i >= 2) && (new_int1 >= 0) && (new_int1 < 16384)) { new_krn_loc = new_int1; max_samples = 16384 - new_krn_loc; n_loaded = load_from_file( max_samples, p_file_path, p_krn_name, ((8*new_krn_loc) + F0_KMEM_I_OFS), a32_pci_addr); if (n_loaded >= 0) { new_krn_len = n_loaded; printf("\n %d kernel samples loaded at 0x%x\n", n_loaded, new_krn_loc); } } else { if (i <2) printf("\n Buffer offset not specified\n"); else printf( "\n New kernel offset is out of range %d-%d, 0x%x-0x%x\n", 0, 16383, 0, 16383); } } break; case 'v': /* load data test vector from file */ if (cmd_len == 2) printf("\n Test vector file name: %s, location: 0x%x\n", vect_file_name, new_vect_loc); else { i = sscanf(std_in_line, "v %254s %i", p_vect_name, &new_int1); printf("\n%s %d %d\n", vect_file_name, new_int1, i); if ((i >= 2) && (new_int1 >= 0) && (new_int1 < 16384)) { new_vect_loc = new_int1; max_samples = 16384 - new_vect_loc; n_loaded = load_from_file( max_samples, p_file_path, p_vect_name, ((8*new_vect_loc) + F0_DMEM_C4C3_OFS), a32_pci_addr); if (n_loaded >= 0) { new_vect_len = n_loaded; printf("\n %d test vector samples loaded at 0x%x\n", n_loaded, new_vect_loc); } } else { if (i <2) printf("\n Buffer offset not specified\n"); else printf( "\n New vector offset is out of range %d-%d, 0x%x-0x%x\n", 0, 16383, 0, 16383); } } break; case 'w': /* Set DSP window length and offsets */ i = sscanf(std_in_line, "w %i %i %i", &new_int1, &new_int2, &new_int3); if (i >= 1) { if ((new_int1 >= 0) && (new_int1 < 16384)) *P_CR16(CSR_DOF_OFS) = new_int1; else printf("Data offset out of range 0x%x-0x%x\n", 0, 16383); if (i >= 2) { if ((new_int2 >= 0) && (new_int2 < 16384)) *P_CR16(CSR_KOF_OFS) = new_int2; else printf("Kernel offset out of range 0x%x-0x%x\n", 0, 16383); if (i >= 3) { if ((new_int3 >= 0) && (new_int3 < 16384)) *P_CR16(CSR_KLR_OFS) = new_int3; else printf("Kernel length out of range 0x%x-0x%x\n", 0, 16383); } } } printf("\n DSP Windows:\n"); printf(" Data Offset: %d <0x%x>\n", *P_CR16(CSR_DOF_OFS), *P_CR16(CSR_DOF_OFS)); printf(" Kernel Offset: %d <0x%x>\n", *P_CR16(CSR_KOF_OFS), *P_CR16(CSR_KOF_OFS)); printf(" Kernel Length: %d <0x%x>\n", *P_CR16(CSR_KLR_OFS), *P_CR16(CSR_KLR_OFS)); break; case 'a': /* set acquisition mode */ i = sscanf(std_in_line, "a %i %i %i %i %i", &new_int1, &new_int2, &new_int3, &new_int4, &new_int5); if (i >= 1) { if ((new_int1 >= 0) && (new_int1 <= 2)) { dsp_op = new_int1; acq_ctl_reg_base &= 0x9fff; if (dsp_op > 0) acq_ctl_reg_base |= CSR_ACQ_EN_DSP; if (dsp_op == 2) acq_ctl_reg_base |= CSR_ACQ_DSP_OVRL; } else printf("DSP Op value out of range %d-%d\n", 0, 2); if (i >= 2) { if ((new_int2 >= 0x0) && (new_int2 <= 0xf)) { seq_id = new_int2; acq_ctl_reg_base &= 0xff0f; acq_ctl_reg_base |= (seq_id << 4); } else printf("Sequence ID out of range 0x%x-0x%x\n", 0x0, 0xf); if (i >= 3) { if ((new_int3 >= 0) && (new_int3 <= 2)){ trig_src = new_int3; acq_ctl_reg_base &= 0xfff3; if (trig_src > 0) acq_ctl_reg_base |= CSR_ACQ_EXT_TRIG; if (trig_src == 2) acq_ctl_reg_base |= CSR_ACQ_SEL_TRG2; } else printf("Trigger Source out of range %d-%d\n", 0, 2); if (i >= 4) { if ((new_int4 >= 0) && (new_int4 <= 1)) { trig_op = new_int4; acq_ctl_reg_base &= 0xfffd; acq_ctl_reg_base |= trig_op << 1; } else printf("Trigger Op out of range %d-%d\n", 0, 1); if (i >= 5) { if ((new_int5 >= 0) && (new_int5 <= 31)) { sh_gain = new_int5; acq_ctl_reg_base &= 0xe0ff; acq_ctl_reg_base |= (sh_gain << 8); } else printf("Shift Gain out of range %d-%d\n", 0, 31); } } } } } *P_CR16(CSR_ACQ_OFS) = acq_ctl_reg_base; printf("\n Acquisition Mode:\n"); printf(" DSP Op: %d\n", dsp_op); printf(" Seq_ID: 0x%x\n", seq_id); printf(" Trig Src: %d\n", trig_src); printf(" Trig Op: %d\n", trig_op); printf(" Shift Gain: %d\n", sh_gain); break; case 'm': /* set ADC mode */ aux_ctl_usr_mode = *P_CR16(CSR_AUX_OFS); i = sscanf(std_in_line, "m %i %i %i %i %i", &new_int1, &new_int2, &new_int3, &new_int4, &new_int5); if (i >= 1) { if ((new_int1 >= 0) && (new_int1 <= 1)) { hi_gain = new_int1; aux_ctl_usr_mode &= (~CSR_AUX_ADC_PGA); if (hi_gain > 0) aux_ctl_usr_mode |= CSR_AUX_ADC_PGA; } else printf("Hi-Gain On/Off value out of range %d-%d\n", 0, 1); if (i >= 2) { if ((new_int2 >= 0) && (new_int2 <= 1)) { ext_clk = new_int2; aux_ctl_usr_mode &= (~CSR_AUX_EXT_CLK); if (ext_clk > 0) aux_ctl_usr_mode |= CSR_AUX_EXT_CLK; } else printf("Ext_Clock Selection out of range 0x%x-0x%x\n", 0x0, 0x1); if (i >= 3) { if ((new_int3 >= 0) && (new_int3 <= 1)){ alt_fmt = new_int3; aux_ctl_usr_mode &= (~CSR_AUX_ALT_FMT); if (alt_fmt > 0) aux_ctl_usr_mode |= CSR_AUX_ALT_FMT; } else printf("Alt_Format Selection out of range %d-%d\n", 0, 1); if (i >= 4) { if ((new_int4 >= 0) && (new_int4 <= 1)) { random = new_int4; aux_ctl_usr_mode &= (~CSR_AUX_ADC_RAND); if (random > 0) aux_ctl_usr_mode |= CSR_AUX_ADC_RAND; } else printf("Random On/Off out of range %d-%d\n", 0, 1); if (i >= 5) { if ((new_int5 >= 0) && (new_int5 <= 1)) { dither = new_int5; aux_ctl_usr_mode &= (~CSR_AUX_ADC_DITH); if (dither > 0) aux_ctl_usr_mode |= CSR_AUX_ADC_DITH; } else printf("Dither On/Off out of range %d-%d\n", 0, 1); } } } } } *P_CR16(CSR_AUX_OFS) = aux_ctl_usr_mode; printf("\n ADC Mode:\n"); printf(" Hi-Gain (on/off): %d\n", hi_gain); printf(" Ext Clock select: %d\n", ext_clk); printf(" Alt format select: %d\n", alt_fmt); printf(" Random (on/off): %d\n", random); printf(" Dither (on/off): %d\n", dither); break; case 'g': /* 'go' start acquisition */ if (cmd_len == 2) { /* arm the acquisition firmware */ *P_CR16(CSR_ACQ_OFS) = acq_ctl_reg_base; *P_CR16(CSR_ACQ_OFS) = (acq_ctl_reg_base | CSR_ACQ_ACQ_ARM); /* if s/w trig selected, generate s/w gate-trigger */ if ((acq_ctl_reg_base & CSR_ACQ_EXT_TRIG) == 0) { *P_CR16(CSR_ACQ_OFS) = (acq_ctl_reg_base | CSR_ACQ_SW_GATE); /* keeping s/w gate active masks parallel output noise */ rtems_stat = rtems_task_wake_after((rtems_interval) 3); *P_CR16(CSR_ACQ_OFS) = acq_ctl_reg_base; } } else case 'd': /* dump sample buffer to binary file */ if (cmd_len == 2) { rtn_stat = dump_to_file(acq_len, p_file_path, p_file_name, a32_pci_addr); } else print_acq_help(); break; case 's': /* show status */ if (cmd_len == 2) { printf("\nStatus Registers:\n"); /* acquisition status */ printf(" Acquisition: 0x%04x\n", *P_CR16(CSR_ASL_OFS)); /* end count */ printf(" End count: 0x%04x\n", *P_CR16(CSR_ECS_OFS)); /* DSP status */ printf(" DSP: 0x%04x\n", *P_CR16(CSR_DSP_OFS)); /* CH4 magnitude squared */ printf(" CH4 Magn Sqr: 0x%08x\n", *P_CR32(CSR_MS4_OFS)); /* CH3 magnitude squared */ printf(" CH3 Magn Sqr: 0x%08x\n", *P_CR32(CSR_MS3_OFS)); /* CH2 magnitude squared */ printf(" CH2 Magn Sqr: 0x%08x\n", *P_CR32(CSR_MS2_OFS)); /* CH1 magnitude squared */ printf(" CH1 Magn Sqr: 0x%08x\n", *P_CR32(CSR_MS1_OFS)); /* DSP exponents */ printf(" Exponents: 0x%08x\n", *P_CR32(CSR_EXP_OFS)); /* interrupt */ printf(" Interrupt Stt:0x%04x\n", *P_CR16(CSR_ISR_OFS)); /* DCM4 */ printf(" DCM-4 Stat: 0x%04x\n", *P_CR16(CSR_D4S_OFS)); /* DCM3 */ printf(" DCM-3 Stat: 0x%04x\n", *P_CR16(CSR_D3S_OFS)); /* DCM2 */ printf(" DCM-2 Stat: 0x%04x\n", *P_CR16(CSR_D2S_OFS)); /* DCM1 */ printf(" DCM-1 Stat: 0x%04x\n", *P_CR16(CSR_D1S_OFS)); /* DCM0 */ printf(" DCM-0 Stat: 0x%04x\n", *P_CR16(CSR_D0S_OFS)); /* serial number */ printf(" Ser. No.: 0x%04x\n", *P_CR16(CSR_SNR_OFS)); printf("\nControl Registers:\n"); /* VME control */ printf(" VME Control: 0x%04x\n", *P_CR16(CSR_VCTL_OFS)); /* auxiliary control */ printf(" Aux. Control: 0x%04x\n", *P_CR16(CSR_AUX_OFS)); /* acquisition control */ printf(" Acq. Control: 0x%04x\n", *P_CR16(CSR_ACQ_OFS)); /* acquisition counter preset */ printf(" Acq. Preset.: 0x%04x\n", *P_CR16(CSR_ACP_OFS)); /* kernel length */ printf(" Kernel Len.: 0x%04x\n", *P_CR16(CSR_KLR_OFS)); /* kernel offset */ printf(" Kernel Ofs.: 0x%04x\n", *P_CR16(CSR_KOF_OFS)); /* data offset */ printf(" Data Ofs.: 0x%04x\n", *P_CR16(CSR_DOF_OFS)); /* buffer aux bits */ printf(" Buf Aux Bits: 0x%04x\n", *P_CR16(CSR_BAB_OFS)); /* dac/sync mux */ printf(" DAC/Sync Mux: 0x%04x\n", *P_CR16(CSR_DAC_OFS)); /* interrupt control */ printf(" Interrupt Ctl:0x%04x\n", *P_CR16(CSR_DAC_OFS)); /* DCM4 */ printf(" DCM-4 Ctl: 0x%04x\n", *P_CR16(CSR_D4C_OFS)); /* DCM3 */ printf(" DCM-3 Ctl: 0x%04x\n", *P_CR16(CSR_D3C_OFS)); /* DCM2 */ printf(" DCM-2 Ctl: 0x%04x\n", *P_CR16(CSR_D2C_OFS)); /* DCM1 */ printf(" DCM-1 Ctl: 0x%04x\n", *P_CR16(CSR_D1C_OFS)); /* DCM0 */ printf(" DCM-0 Ctl: 0x%04x\n", *P_CR16(CSR_D0C_OFS)); printf("\n"); exponents = *P_CR32(CSR_EXP_OFS); expn1 = (exponents & CSR_EXP_MAG1_SQ); expn2 = (exponents & CSR_EXP_MAG2_SQ) >> 8; expn3 = (exponents & CSR_EXP_MAG3_SQ) >> 16; expn4 = (exponents & CSR_EXP_MAG4_SQ) >> 24; printf("\nExp1 = 0x%x\n", expn1); pwr1 = *P_CR32(CSR_MS1_OFS) / pow(2.0, expn1); pwr2 = *P_CR32(CSR_MS2_OFS) / pow(2.0, expn2); pwr3 = *P_CR32(CSR_MS3_OFS) / pow(2.0, expn3); pwr4 = *P_CR32(CSR_MS4_OFS) / pow(2.0, expn4); mag1 = pow(pwr1, 0.5); mag2 = pow(pwr2, 0.5); mag3 = pow(pwr3, 0.5); mag4 = pow(pwr4, 0.5); printf(" DSP Ch1 Mag_Sq: %g, Mag: %g\n", pwr1, mag1); printf(" DSP Ch2 Mag_Sq: %g, Mag: %g\n", pwr2, mag2); printf(" DSP Ch3 Mag_Sq: %g, Mag: %g\n", pwr3, mag3); printf(" DSP Ch4 Mag_Sq: %g, Mag: %g\n", pwr4, mag4); printf("\n"); } else print_acq_help(); break; case 'o': /* set DAC output source locations */ dac_ctl_val = *P_CR16(CSR_DAC_OFS); i = sscanf(std_in_line, "o %i %i %i %i", &new_int1, &new_int2, &new_int3, &new_int4); if (i >= 1) { if ((new_int1 >= 0) && (new_int1 < 2)) { dac_ctl_val = ((dac_ctl_val & 0x7fff) | (new_int1 << 15)); dac_on_off = new_int1; } else printf("DAC Enable value out of range 0x%02x-0x%02x\n", 0, 1); if (i >= 2) { if ((new_int2 >= 0) && (new_int2 < 32)) { dac_ctl_val = ((dac_ctl_val & 0xffe0) | new_int2); dac_ch_a = new_int2; } else printf("Ch-A selection out of range 0x%02x-0x%02x\n", 0, 31); if (i >= 3) { if ((new_int3 >= 0) && (new_int3 < 32)) { dac_ctl_val = ((dac_ctl_val & 0xfc1f) | (new_int3 << 5)); dac_ch_b = new_int3; } else printf("Ch-B selection out of range 0x%02x-0x%02x\n", 0, 31); if (i >= 4) { if ((new_int4 >= 0) && (new_int4 < 32)) { dac_ctl_val = ((dac_ctl_val & 0x83ff) | (new_int4 << 10)); dac_sync = new_int4; } else printf("Sync selection out of range 0x%02x-0x%02x\n", 0, 31); } } } } *P_CR16(CSR_DAC_OFS) = dac_ctl_val; printf("\n DAC control values:\n"); printf(" DAC On/Off: %d\n", dac_on_off); printf(" DAC Ch A: %d\n", dac_ch_a); printf(" DAC Ch B: %d\n", dac_ch_b); printf(" DAC Sync: %d\n", dac_sync); break; case 't': /* send and receive n-bits of serial data */ i = sscanf(std_in_line, "t %x %i %i", &new_unsigned, &new_int2, &new_int3); if (i >= 1) { serial_dat = new_unsigned; if (i >= 2) { if ((new_int2 >= 0) && (new_int2 <= 32)) serial_nbits = new_int2; else printf("Number of bits out of range 0x%02x-0x%02x\n", 0, 32); if (i >=3) { if (new_int3 == 1) serial_loop = 1; else serial_loop = 0; } } } printf("Sending serial %d bits: 0x%08x ", serial_nbits, serial_dat); *P_CR32(CSR_SDAT_OFS) = serial_dat; serial_ctl = *P_CR16(CSR_AUX_OFS); if (serial_loop == 1) *P_CR16(CSR_AUX_OFS) = ((serial_ctl & 0xff80) | CSR_AUX_SIO_LPBK | serial_nbits); else *P_CR16(CSR_AUX_OFS) = ((serial_ctl & 0xff80) | serial_nbits); serial_ctl = *P_CR16(CSR_AUX_OFS); *P_CR16(CSR_AUX_OFS) = (serial_ctl | CSR_AUX_SIO_ACTV); for (i=0; i<32; i++) { reg16_value = *P_CR16(CSR_AUX_OFS); if ((reg16_value & CSR_AUX_SIO_ACTV) == 0) break; else printf("*"); } if (i == 32) printf("\nSerial timed out without completing\n"); printf("\n 0x%08x returned\n", *P_CR32(CSR_SDAT_OFS)); *P_CR16(CSR_AUX_OFS) = serial_ctl; break; case 'h': default: /* display help list */ print_acq_help(); } } } #endif printf("\nExiting test program\n\n"); return; } /****************************************************** * * Temporary file access test program * *******************************************************/ void tstfile() { FILE *fp; int i, rtn_val; char char_line[256]; char *newline; short unsigned dat_buf[256]; short unsigned *ptr; printf("\nTrying to open and read existing file...\n"); fp = fopen("/data/bpmd_dat/my_read_file","r"); if (fp == NULL) { printf("\nSorry, no luck.\n"); return; } newline = fgets(char_line, 256, fp); rtn_val = printf("\n%s\n", char_line); rtn_val = fclose(fp); printf("\nTrying to open (or create) and write to file...\n"); fp = fopen("/data/bpmd_dat/my_write_file","w"); if (fp == NULL) { printf("\nSorry, no luck.\n"); return; } rtn_val = fprintf(fp,"This is my write file.\n"); rtn_val = fclose(fp); printf("\nTrying to open (or create) and write to binary file...\n"); fp = fopen("/data/bpmd_dat/my_binary_file","wb"); if (fp == NULL) { printf("\nSorry, no luck.\n"); return; } for (i=0; i<256; i++) dat_buf[i] = i - 128; ptr = dat_buf; for (i=0; i<64; i++){ rtn_val = fwrite(ptr, 2, 4, fp); ptr += 4; if (rtn_val != 4) break; } printf("wrote %d sample sets\n", i); rtn_val = fclose(fp); return; }