Search code examples
cbufferuart

Transmitte Two buffers Consequtively


I have Two character Buffers, on which I am gathering data.

TxBuffer[1400];
TxBuffer2[1400]

I have a situation is that, One buffer will be transmitting at a time over uart and on the other buffer I will be felling data,after one buffer is transmitted other buffer should transmit. Is there any way to do it, Without using memset.? My code flow is as below.

  1. Data will be collected from sensors in buff1
  2. at this time the buff2 will be transmitting
  3. after buff2 is transmitted, the buff1 should transmit
  4. when buff1 is transmitting , data will be collected on buff2.

As per above I would like add what i have tried, as below. I took 2 char pointers pointing to particular address offset in one buffer as below.

 unsigned char *block1_ptr = &TxBuffer[Sensor1_Data];
 unsigned char *block2_ptr = &TxBuffer[Sensor1_Data +700];

and one 1ms I am collecting my data from sensor to this buffer. as below.

Sensor1_dataptr =  Sensor1_GetData();
*block1_ptr = Sensor1_dataptr->datax0;
block1_ptr++;
*block1_ptr = Sensor1_dataptr->datax1;
block1_ptr++;
*block1_ptr = Sensor1_dataptr->datay0;
block1_ptr++;
*block1_ptr = Sensor1_dataptr->datay1;
block1_ptr++;
*block1_ptr = Sensor1_dataptr->dataz0;
block1_ptr++;
*block1_ptr = Sensor1_dataptr->dataz1;
block1_ptr++;

as like this I am also collecting data from other sensor as well. After collecting data I am sending to uart over dma. My problem is , I have to collect data in one buffer while other is transmitting and vice versa, but how to change buffer, as tx happens at arond every 50ms and collecting of data every 1ms. My dma config. is as below,

dmaconfig->source = &TxBuffer[Sensor1_Data];
dmaconfig->destination = &UCA0TXBUF;
dmaconfig->size = 1400;
DMAInit(dmaconfig);
DMA_TRIGGER();

I have Tried To have one flag that will reset when dma is done with Tx. Also I am keeping one condition as follows

if(flag == set)
{
  Transmit_buff = &TxBuffer[1400];
}else{ 
   &TxBuffer[1400];
}

I am setting flag while I am collecting data from sensors.But it's not working.


Solution

  • You can use buffer pointers, and swap them instead of copying the data, perhaps like this

    int main(void) {
    
        char Buff1[180];
        char Buff2[180];
        char *prxbuf = Buff1;
        char *ptxbuf = Buff2;
        char *temp;
    
        while (1) {
    
            // receive using prxbuf
            // transmit using ptxbuf
    
            // swap the buffer pointers
            temp = prxbuf;
            prxbuf = ptxbuf;
            ptxbuf = temp;
        }
        return 0;
    }