I've been working with RP more than a year in a project that involves measurements with different configurations. To that end I used the C API functions to acquire sweeps of measures with multiple thresolds and samplings. My problem is that I have been able to acquire only one measurement, given that in the second iteration of my code it always hangs. Here's a sample of my code:
Code: Select all
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "redpitaya/rp.h"
#include <signal.h>
#include <sched.h>
#include <fcntl.h>
#include <math.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define TCP_PORT 1001
#define CMA_ALLOC _IOWR('Z', 0, uint32_t)
uint64_t dna;
char id[7];
int wflag=0;
int tflag=0;
int wait_t;
char timebuffer[16];
char ms[12];
struct timeval tv;
struct timeval tf;
time_t curtime;
char str[51]; //18 FROM DIRECTORY NAME AND 26 FROM DATE + 7 RPID
char info[20]; //FOR DECIMATION AND THLEV
rp_acq_sampling_rate_t rst;
rp_acq_decimation_t dec1;
//int dec_in = 1;
uint32_t buff_size = 16384;
rp_acq_trig_state_t statet = RP_TRIG_STATE_WAITING;
uint32_t trigger_position;
void setup ( float thlev ) {
if(rp_Init() != RP_OK){ /* Print error, if rp_Init() function failed */
fprintf(stderr, "Rp api init failed!\\n");
}
rp_IdGetDNA(&dna);
printf("RP DNA: %llu \n",dna);
int status = rp_ApinSetValue(0, thlev);
if (status != RP_OK) {
printf("Could not set AO0 voltage.\\n");}
status = rp_ApinSetValue(1, thlev);
if (status != RP_OK) {
printf("Could not set AO1 voltage.\\n");}
status = rp_ApinSetValue(2, thlev);
if (status != RP_OK) {
printf("Could not set AO2 voltage.\\n");}
status = rp_ApinSetValue(3, thlev);
if (status != RP_OK) {
printf("Could not set AO3 voltage.\\n");}
rp_DpinSetState (RP_LED3, RP_HIGH);
rp_DpinSetState (RP_LED4, RP_LOW);
rp_DpinSetState (RP_LED5, RP_LOW);
rp_DpinSetState (RP_LED6, RP_LOW);
statet = RP_TRIG_STATE_WAITING;
}
void def_params ( int dec_in ){
if(dec_in==1){
rst=RP_SMP_125M;
dec1=RP_DEC_1;
wait_t=131;
//tsample=8;
}
else if(dec_in==8){
rst=RP_SMP_15_625M;
dec1=RP_DEC_8;
wait_t=1048;
//tsample=64.026;
}
else if(dec_in==64){
rst=RP_SMP_1_953M;
dec1=RP_DEC_64;
wait_t=8400;
//tsample=512.024;
}
else if(dec_in==256){
rst=RP_SMP_448_281K;
dec1=RP_DEC_256;
wait_t=335600;
//tsample=4093.63;
}
else if(dec_in==1024){
rst=RP_SMP_122_070K;
dec1=RP_DEC_1024;
wait_t=134200;
//tsample=8192.02;
}
else if(dec_in==8192){
rst=RP_SMP_15_258K;
dec1=RP_DEC_8192;
wait_t=1073600;
//tsample=65551.76;
}
else {
printf("decimation must be 1,8,64,1024,8192\\n");}
rp_AcqReset();
rp_AcqSetSamplingRate(rst); // it should be this one. However, the next one is always used.
rp_AcqSetDecimation(dec1);
rp_AcqSetGain(RP_CH_1,RP_HIGH);
rp_AcqSetGain(RP_CH_2,RP_HIGH);
rp_AcqSetAveraging(0);
rp_AcqSetTriggerSrc(RP_TRIG_SRC_EXT_PE);//Trigger externo, Positive Edge
rp_DpinSetState (RP_LED4, RP_HIGH);
}
void adq_data(float thlev, int decimation, int delay){
float *buff1 = (float *)malloc(buff_size * sizeof(float));
float *buff2 = (float *)malloc(buff_size * sizeof(float));
rp_AcqStart();
rp_acq_trig_state_t statet = RP_TRIG_STATE_WAITING;
gettimeofday(&tf, NULL);
curtime=tf.tv_sec;
strftime(timebuffer, 16, "%Y%m%d_%H%M%S",localtime(&curtime));
sprintf(ms, "_%ld", tf.tv_usec); //tv_nsec in nano seconds
printf("Trigger ready at %s_%s\\n", timebuffer, ms);
rp_DpinSetState(RP_LED5, RP_HIGH);
int timer=0;
while(wflag==0){
rp_AcqGetTriggerState(&statet);
timer++;
if(statet == RP_TRIG_STATE_TRIGGERED){
printf("TRIGGERED, statet is: %i \\n", statet);
statet = RP_TRIG_STATE_WAITING;
rp_DpinSetState (RP_LED6, RP_HIGH);
wflag=1;} //END IF TRIGG
if (timer>2849000*300){printf("TIMER DONE\\n"); tflag=1; break;}
}//END WHILE
gettimeofday(&tv, NULL); //WHEN TRIGGER IS ON=> GET TIME
// BUFFER ACQUISITION
if(tflag==0){
rp_AcqGetWritePointerAtTrig(&trigger_position);
rp_AcqGetDataV(RP_CH_1, trigger_position-delay, &buff_size, buff1);
rp_AcqGetDataV(RP_CH_2, trigger_position-delay, &buff_size, buff2);
// COMPLETE FILE NAME WITH EXTENSION AND OPEN IT
curtime=tv.tv_sec;
snprintf(info, 20,"dec=%i, th=%f\\n", decimation, thlev);
strftime(timebuffer, 16, "%Y%m%d_%H%M%S",localtime(&curtime));
sprintf(ms, "_%ld.bin", tv.tv_usec); //tv_nsec in nano seconds
strcpy(str,"/home/TestMeas/");
strcat(str, id);
strcat(str, timebuffer);
strcat(str, ms);
printf("%s\\n",info);
FILE *f=fopen(str, "wb"); //for bin file
fwrite(info, sizeof(info), sizeof(info), f);
fwrite(buff1, sizeof(float), buff_size, f);
fwrite(buff2, sizeof(float), buff_size, f);
fclose(f);
printf("Ruta del archivo: %s \\n", str);
printf("Adquisition finished \\n");//mqtt
free(buff1);
free(buff2);
wflag=0;
rp_DpinSetState (RP_LED3, RP_LOW);
rp_DpinSetState (RP_LED4, RP_LOW);
rp_DpinSetState (RP_LED5, RP_LOW);
rp_DpinSetState (RP_LED6, RP_LOW);
//rp_DpinSetState (RP_DIO0_P, &statet);
//printf("statet is: %i \\n", statet);
}
}
int acq( float umbral, int decimation, int delay){//van a ser lo mismo que thlev y dec_in, respectivamente
setup(umbral);
def_params(decimation);
sleep(1);
adq_data(umbral, decimation, delay);
return EXIT_SUCCESS;
}
int main ( int argc, char **argv ){
int i;
int dec_array[5]={1, 8, 64, 1024, 8192};
for (i=0; i < 5; i++){
acq(1, dec_array[i], 8192);
}
return EXIT_SUCCESS;
}
The output in console shows that all the code is executed in the first iteration, but it hangs waiting the trigger as the led 5 is on, which happends when the RP is waiting for a trigger event.
My guessing is that im not rearming correctly the trigger, but I tried using different functions in the API to that end without success. If someone can give any hint about that im gratefull. Moreover, I dont expect a direct solution for the code as I would prefer to understand better about the API and the HW itself. Bringing it up, is there any way to check the code from the API other than the rp.h file? In the future I'll be interested in modifying it but I haven't been able to.
Thank you all in advance,
Alex.