Home > Software design >  Interleaving Data in C
Interleaving Data in C

Time:05-21

I have the following problem - my embedded system has four fifos consisting of 64-byte chunks of data. These fifo's and the corresponding data chunks are represented in the diagram below. Once all 4 fifo's have been popped, I need to interleave the data chunks. I can do a simple loop but I am concerned this might be too processor intensive. Does anyone have a smarter way that is less processor intensive in which I can rapidly interleave the different chunks of memory?

data interleaving

CodePudding user response:

Use an array of FIFO pointers:

#define FIFO_COUNT 4

fifo *fifos[FIFO_COUNT] = {&fifo0, &fifo1, &fifo2, &fifo3];
int cur_fifo = 0;

Then when you read from a FIFO read from the one referenced by cur_fifo, then increment it with wraparound:

next_block = fifo_read(fifos[cur_fifo]);
cur_fifo = (cur_fifo   1) % FIFO_COUNT;

CodePudding user response:

#define FIFO1   (volatile uint64_t *)FIFO1_ADDRESS
#define FIFO2   (volatile uint64_t *)FIFO2_ADDRESS
#define FIFO3   (volatile uint64_t *)FIFO3_ADDRESS
#define FIFO4   (volatile uint64_t *)FIFO4_ADDRESS

static inline uint64_t *popfifos(uint64_t *data)
{
    data[0] = *FIFO1; 
    data[1] = *FIFO2;
    data[2] = *FIFO3;
    data[3] = *FIFO4;
    return data   4;
}


void readFIFOS(uint64_t *buff, size_t nreads)
{
    while(nreads--)
    {   
        if(fifosReady()) buff = popfifos(buff);
    }
}

fifosReady reads some hardware registers waiting for data to be ready.

  • Related