Segmentation fault when using RP API

Applications, development tools, FPGA, C, WEB
Post Reply
earlforeal
Posts: 33
Joined: Thu Aug 20, 2020 9:00 pm

Segmentation fault when using RP API

Post by earlforeal » Thu Jul 20, 2023 3:13 pm

I have used the rp.h library to write this triggered pulse acquisition. It works and acquires the signal with good accuracy. However, the program always ends in a "Segmentation fault." The code isn't too complex, but I don't have much experience in C to begin with. Any suggestions on what is causing this?

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "rp.h" // /RedPitaya/rp-api/api/include/redpitaya
#include "rp_hw-profiles.h" // /RedPitaya/rp-api/api-hw-profiles/include



int main(int argc, char **argv){

    if(rp_Init() != RP_OK){
        fprintf(stderr, "RP API initialization failed.\n");
    }

    uint32_t buff_size = 256;
    float *buff_ch1 = (float *)malloc(buff_size * sizeof(float));

    rp_AcqReset();
    rp_AcqSetDecimation(RP_DEC_8);
    rp_AcqSetTriggerDelay(0);

    rp_AcqSetGain(0, RP_HIGH);
    rp_AcqSetTriggerLevel(RP_CH_1, 0.0);

    rp_AcqStart();
    sleep(0.000002);

    rp_AcqSetTriggerSrc(RP_TRIG_SRC_CHA_PE);
    rp_acq_trig_state_t state = RP_TRIG_STATE_TRIGGERED;

    while(1){
        rp_AcqGetTriggerState(&state);
        if(state == RP_TRIG_STATE_TRIGGERED){
            break;
        }
    }

    bool fillstate = false;
    while(!fillstate){
        rp_AcqGetBufferFillState(&fillstate);
    }

    /* Get write pointer where trigger was met. */
    uint32_t pos = 0;
    rp_AcqGetWritePointerAtTrig(&pos);
    
    /* Get buffer data from channels A, B, and C */
    buffers_t b;
    b.size = buff_size;
    b.ch_f[0] = buff_ch1;
    rp_AcqGetDataV2(pos, &b);

    int i;

    for(i = 1; i < buff_size; i++){
        printf("%f\n",buff_ch1[i]);
    }

    free(buff_ch1);
    rp_Release();
    return 0;

}

User avatar
redpitaya
Site Admin
Posts: 912
Joined: Wed Mar 26, 2014 7:04 pm

Re: Segmentation fault when using RP API

Post by redpitaya » Fri Jul 21, 2023 11:47 am

Hello earlforeal,

Red Pitaya always returns 16384 data samples regardless of the Decimation and other factors.
This is for acquiring data from fast analog inputs.

The problem is that you only reserved space for 256 samples. When you pass the

Code: Select all

rp_AcqGetDataV2(pos, &b);
function the address of b (pointer to the first float inside the saved space), Red Pitaya will fill the available space and then start overwriting the address space in the memory right after the allocated memory (causing a segmentation fault - your program does not have access to the memory space beyond what was reserved). Check pointer arithmetics online if you are unfamiliar with this.

I believe the function you used is for acquiring data from all Input buffers (in your case, at least IN1 and IN2), so the code would still fail due to a lack of space for additional data buffers.

If you are looking to acquire code from only one channel, try using one of these functions:

Code: Select all

rp_AcqGetDataV(rp_channel_t channel, uint32_t pos, uint32_t* size, float* buffer)

Code: Select all

rp_AcqGetOldestDataV(rp_channel_t channel, uint32_t* size, float* buffer)
Or, if you are looking for only 256 samples:

Code: Select all

int rp_AcqGetDataPosV(rp_channel_t channel, uint32_t start_pos, uint32_t end_pos, float* buffer, uint32_t* buffer_size)
Also, check out the other functions at:
https://github.com/RedPitaya/RedPitaya/ ... i/src/rp.c

earlforeal
Posts: 33
Joined: Thu Aug 20, 2020 9:00 pm

Re: Segmentation fault when using RP API

Post by earlforeal » Fri Jul 21, 2023 5:22 pm

Thanks for the info, when I changed the buffer size back to 16384 the segmentation fault went away.

earlforeal
Posts: 33
Joined: Thu Aug 20, 2020 9:00 pm

Re: Segmentation fault when using RP API

Post by earlforeal » Tue Jul 25, 2023 3:39 pm

I recently updated my ecosystem to the latest test OS (ecosystem-2.00-141-80a5c35bb.zip) and without any changes to the code posted above, when I run it I get these errors:

Code: Select all

[Error] Invalid gain fast_adc_1_1[0] = 0.100000
[Error:calib_InitModel] Cannot correctly recalculate gain on calibration.
[FATAL ERROR] convertToCnt devide by zero
[FATAL ERROR] convertToCnt devide by zero
[FATAL ERROR] convertToCnt devide by zero
[FATAL ERROR] convertToCnt devide by zero
Any suggestions on what is going wrong?

User avatar
redpitaya
Site Admin
Posts: 912
Joined: Wed Mar 26, 2014 7:04 pm

Re: Segmentation fault when using RP API

Post by redpitaya » Wed Jul 26, 2023 8:13 am

I will take a look at this. Thank you for reporting this.

User avatar
redpitaya
Site Admin
Posts: 912
Joined: Wed Mar 26, 2014 7:04 pm

Re: Segmentation fault when using RP API

Post by redpitaya » Thu Jul 27, 2023 1:16 pm

This seems to be a library issue. The issue was forwarded to the developers.

User avatar
redpitaya
Site Admin
Posts: 912
Joined: Wed Mar 26, 2014 7:04 pm

Re: Segmentation fault when using RP API

Post by redpitaya » Mon Jul 31, 2023 9:17 am

Hello earlforreal,

Please connect to the Red Pitaya web interface (rp-xxxxxx.local), and go to system ==> Calibration.
Click manual DC calibration. In the top right corner, you should see three buttons:
* Reset
* Apply
* Close

Click on Reset and select Factory from the dropdown menu. This will reconfigure the default calibration values to factory ones. This is a necessary step because the 2.00 OS reads the "old" calibration values incorrectly. This was the easiest and cleanest solution.

We will add this fix to the documentation.

Post Reply
jadalnie klasyczne ekskluzywne meble wypoczynkowe do salonu ekskluzywne meble tapicerowane ekskluzywne meble do sypialni ekskluzywne meble włoskie

Who is online

Users browsing this forum: No registered users and 100 guests