void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* AdcHandle) { int i,y, preamb = 0; int succsiveOne = 0; int sample = 0; uint8_t filtered = 0; memcpy(&tmp_resultDMA, &resultDMA, DMA_BUFFER_SIZE * 4); compute_stat(tmp_resultDMA, DMA_BUFFER_SIZE); noise = variance < 1000 ? average : noise; for (i = 0; i < DMA_BUFFER_SIZE; i++) { sample++; ADCVoltageValue = tmp_resultDMA[i] / 16; int ADCVoltageValueUART = tmp_resultDMA[i]; val = itoa(ADCVoltageValueUART); HAL_UART_Transmit(&huart2, (uint8_t*)val, strlen(val), 10); HAL_UART_Transmit(&huart2, (uint8_t*)&",", 2, 10); uint8_t bit = 0; bit = ARA_ADC_Threashold(ADCVoltageValueUART); ADCVoltageValue8 = (uint8_t) ADCVoltageValue; /* USELESS uint8_t filtered = 0; filtered = ((ADCVoltageValueUART - average) > 0) ? (ADCVoltageValueUART - average) : 0; valFiltered = itoa(filtered); HAL_UART_Transmit(&huart2, (uint8_t*)valFiltered, strlen(valFiltered), 10); HAL_UART_Transmit(&huart2, (uint8_t*)&",", 2, 10); */ valFiltered = bit == 0 ? itoa(1) : itoa(10); if (sample == (BIT_LENGTH)) { sample = 0; if (bit == 0) { valFiltered = itoa(-1); } else { succsiveOne = succsiveOne++; valFiltered = 1; } sampledDMA[y] = bit; appendBit(bit, buff_ptr); if (*buff_ptr == CODE_4B6B_PRE2 && preamb == 0) { preamb = 1; } else if(*buff_ptr == CODE_4B6B_PRE2 && preamb == 1) { preamb = 0; } if (preamb == 1) { if (bit == 1) { bitBuffer |= (1u << (7 - counter)); } counter++; if (counter > 7) { FIFO_write_trample(&AdcFIFO, &bitBuffer, 1); counter = 0; bitBuffer = 0x00; } } y = y++; HAL_UART_Transmit(&huart2, (uint8_t*)valFiltered, strlen(valFiltered), 10); HAL_UART_Transmit(&huart2, (uint8_t*)&",", 2, 10); } else { valFiltered = itoa(0); HAL_UART_Transmit(&huart2, (uint8_t*)valFiltered, strlen(valFiltered), 10); HAL_UART_Transmit(&huart2, (uint8_t*)&",", 2, 10); } valBit = itoa(bit); HAL_UART_Transmit(&huart2, (uint8_t*)valBit, strlen(valBit), 10); HAL_UART_Transmit(&huart2, (uint8_t*)&"\r", 4, 10); /* DISABLE FOR TEST bit = ARA_ADC_Threashold(ADCVoltageValue); if (bit == 1) { bitBuffer |= (1u << (7 - counter)); } counter++; if (counter > 7) { FIFO_write_trample(&AdcFIFO, &bitBuffer, 1); counter = 0; bitBuffer = 0x00; } */ } //HAL_UART_Transmit(&huart2, (uint8_t*)&"\r", 4, 10); }
void guppi_rawdisk_thread(void *_args) { /* Set cpu affinity */ cpu_set_t cpuset, cpuset_orig; sched_getaffinity(0, sizeof(cpu_set_t), &cpuset_orig); CPU_ZERO(&cpuset); CPU_SET(1, &cpuset); int rv = sched_setaffinity(0, sizeof(cpu_set_t), &cpuset); if (rv<0) { guppi_error("guppi_rawdisk_thread", "Error setting cpu affinity."); perror("sched_setaffinity"); } /* Get args */ struct guppi_thread_args *args = (struct guppi_thread_args *)_args; /* Set priority */ rv = setpriority(PRIO_PROCESS, 0, 0); if (rv<0) { guppi_error("guppi_rawdisk_thread", "Error setting priority level."); perror("set_priority"); } /* Attach to status shared mem area */ struct guppi_status st; rv = guppi_status_attach(&st); if (rv!=GUPPI_OK) { guppi_error("guppi_rawdisk_thread", "Error attaching to status shared memory."); pthread_exit(NULL); } pthread_cleanup_push((void *)guppi_status_detach, &st); pthread_cleanup_push((void *)set_exit_status, &st); /* Init status */ guppi_status_lock_safe(&st); hputs(st.buf, STATUS_KEY, "init"); guppi_status_unlock_safe(&st); /* Read in general parameters */ struct guppi_params gp; #if FITS_TYPE == PSRFITS struct sdfits pf; pf.sub.dat_freqs = NULL; pf.sub.dat_weights = NULL; pf.sub.dat_offsets = NULL; pf.sub.dat_scales = NULL; pthread_cleanup_push((void *)guppi_free_psrfits, &pf); #else struct sdfits pf; pthread_cleanup_push((void *)guppi_free_sdfits, &pf); #endif /* Attach to databuf shared mem */ struct guppi_databuf *db; db = guppi_databuf_attach(args->input_buffer); if (db==NULL) { guppi_error("guppi_rawdisk_thread", "Error attaching to databuf shared memory."); pthread_exit(NULL); } pthread_cleanup_push((void *)guppi_databuf_detach, db); /* Init output file */ FILE *fraw = NULL; pthread_cleanup_push((void *)safe_fclose, fraw); /* Pointers for quantization params */ double *mean = NULL; double *std = NULL; printf("casper: raw disk thread created and running\n"); /* Loop */ int packetidx=0, npacket=0, ndrop=0, packetsize=0, blocksize=0; int orig_blocksize=0; int curblock=0; int block_count=0, blocks_per_file=128, filenum=0; int got_packet_0=0, first=1; int requantize = 0; char *ptr, *hend; signal(SIGINT,cc); while (run_threads) { /* Note waiting status */ guppi_status_lock_safe(&st); hputs(st.buf, STATUS_KEY, "waiting"); guppi_status_unlock_safe(&st); /* Wait for buf to have data */ rv = guppi_databuf_wait_filled(db, curblock); if (rv!=0) continue; printf("casper: raw disk thread rcvd data.\n"); /* Read param struct for this block */ ptr = guppi_databuf_header(db, curblock); if (first) { guppi_read_obs_params(ptr, &gp, &pf); first = 0; } else { guppi_read_subint_params(ptr, &gp, &pf); } /* Parse packet size, npacket from header */ hgeti4(ptr, "PKTIDX", &packetidx); hgeti4(ptr, "PKTSIZE", &packetsize); hgeti4(ptr, "NPKT", &npacket); hgeti4(ptr, "NDROP", &ndrop); #if FITS_TYPE == PSR_FITS /* Check for re-quantization flag */ int nbits_req = 0; if (hgeti4(ptr, "NBITSREQ", &nbits_req) == 0) { /* Param not present, don't requantize */ requantize = 0; } else { /* Param is present */ if (nbits_req==8) requantize = 0; else if (nbits_req==2) requantize = 1; else /* Invalid selection for requested nbits * .. die or ignore? */ requantize = 0; } #endif /* Set up data ptr for quant routines */ #if FITS_TYPE == PSR_FITS pf.sub.data = (unsigned char *)guppi_databuf_data(db, curblock); #else pf.data_columns.data = (unsigned char *)guppi_databuf_data(db, curblock); #endif /* Wait for packet 0 before starting write */ if (got_packet_0==0 && packetidx==0 && gp.stt_valid==1) { got_packet_0 = 1; guppi_read_obs_params(ptr, &gp, &pf); #if FITS_TYPE == PSR_FITS orig_blocksize = pf.sub.bytes_per_subint; #endif char fname[256]; sprintf(fname, "%s.%4.4d.raw", pf.basefilename, filenum); fprintf(stderr, "Opening raw file '%s'\n", fname); // TODO: check for file exist. fraw = fopen(fname, "w"); if (fraw==NULL) { guppi_error("guppi_rawdisk_thread", "Error opening file."); pthread_exit(NULL); } #if FITS_TYPE == PSR_FITS /* Determine scaling factors for quantization if appropriate */ if (requantize) { mean = (double *)realloc(mean, pf.hdr.rcvr_polns * pf.hdr.nchan * sizeof(double)); std = (double *)realloc(std, pf.hdr.rcvr_polns * pf.hdr.nchan * sizeof(double)); compute_stat(&pf, mean, std); fprintf(stderr, "Computed 2-bit stats\n"); } #endif } /* See if we need to open next file */ if (block_count >= blocks_per_file) { fclose(fraw); filenum++; char fname[256]; sprintf(fname, "%s.%4.4d.raw", pf.basefilename, filenum); fprintf(stderr, "Opening raw file '%s'\n", fname); fraw = fopen(fname, "w"); if (fraw==NULL) { guppi_error("guppi_rawdisk_thread", "Error opening file."); pthread_exit(NULL); } block_count=0; } /* See how full databuf is */ //total_status = guppi_databuf_total_status(db); /* Requantize from 8 bits to 2 bits if necessary. * See raw_quant.c for more usage examples. */ #if FITS_TYPE == PSR_FITS if (requantize && got_packet_0) { pf.sub.bytes_per_subint = orig_blocksize; /* Does the quantization in-place */ quantize_2bit(&pf, mean, std); /* Update some parameters for output */ hputi4(ptr, "BLOCSIZE", pf.sub.bytes_per_subint); hputi4(ptr, "NBITS", pf.hdr.nbits); } #endif /* Get full data block size */ hgeti4(ptr, "BLOCSIZE", &blocksize); /* If we got packet 0, write data to disk */ if (got_packet_0) { /* Note waiting status */ guppi_status_lock_safe(&st); hputs(st.buf, STATUS_KEY, "writing"); guppi_status_unlock_safe(&st); /* Write header to file */ hend = ksearch(ptr, "END"); for (ptr=ptr; ptr<=hend; ptr+=80) { fwrite(ptr, 80, 1, fraw); } /* Write data */ printf("block size: %d\n", blocksize); ptr = guppi_databuf_data(db, curblock); rv = fwrite(ptr, 1, (size_t)blocksize, fraw); if (rv != blocksize) { guppi_error("guppi_rawdisk_thread", "Error writing data."); } /* Increment counter */ block_count++; /* flush output */ fflush(fraw); } /* Mark as free */ guppi_databuf_set_free(db, curblock); /* Go to next block */ curblock = (curblock + 1) % db->n_block; /* Check for cancel */ pthread_testcancel(); } pthread_exit(NULL); pthread_cleanup_pop(0); /* Closes fclose */ pthread_cleanup_pop(0); /* Closes guppi_databuf_detach */ pthread_cleanup_pop(0); /* Closes guppi_free_psrfits */ pthread_cleanup_pop(0); /* Closes set_exit_status */ pthread_cleanup_pop(0); /* Closes guppi_status_detach */ }