Go Down

Topic: C/C++/Arduino use of Due(SAM3X8E), PDC, DMA etc. Seeking advice. (Read 676 times) previous topic - next topic

Microzod

This do not have anything to do with the subject but is this right?
         
         boolean mySwitch=false;

         /* If mySwitch is true, then populate the num1 variable
         otherwise populate the num2 variable*/

         if(!mySwitch)
         {
            num1=(num1*10)+(byteRead-48);
         }
         else
         {
            num2=(num2*10)+(byteRead-48);
         }

I interpret if(!mySwitch) as if(not mySwitch) as if mySwitch was not true, how can !mySwitch mean that mySwitch is true?

Magician

Quote
Am I right in thinking that if using SerialUSB.write there are no problem with 16bit values being sent?
My guess, you are not.  Serial was defined by ASCII  to deal with bytes, 8-bit unsigned values. To send anything else over serial, like int16 int32 uintxx long float etc. you have to split a variable down to a few 8-bits, and than combine bytes back into variable at the other end. In other words create a communication protocol, that may include additional logical subroutine to initiate-keep-close a communication sessions.
It doesn't matter what is the hardware media you are using, USB RS232  BlueTooth etc.

Microzod

I had been sloppy, when I finally googled "pingpong-buffer" and read about that it all made perfect sense.
I have a working code such as this:
Code: [Select]
void DMA_fcn()  //function called in loop after 'option = Serial.readStringUntil('|');', its one of a few _fcn
{
  while (option == "DMA")
  {
    adc_start(ADC);
    num = Serial.parseInt();
    for (int i = num; i > 0; i--)
    {
      if ( flag )
      {  
        uint16_t idx = flag -1;
        for (i = 0; i < nume; i++)  // nume = INP_BUFF/FFT_SIZE = 2048
          Serial.write(inp[idx][i] >> 8);
          Serial.write(inp[idx][i] & 0xff);
      
        }
      }
      flag = 0;
    }
    adc_stop(ADC);
  }  
}
//besides that the code has all the ADC ADC_handler setup from your example(-adc_start(ADC);


with the idea to call DMA|10 to get 10x'buffer size', I may re-work the hole communication system when I've learned more about parsing strings.
Anyway, now I am thinking about how to receive the values most efficient.

Magician

Quote
I have a working code such as this:
I doubt its working.

Code: [Select]
    for (int i = num; i > 0; i--)
    {
      if ( flag )
      {   
        uint16_t idx = flag -1;
        for (i = 0; i < nume; i++)  // nume = INP_BUFF/FFT_SIZE = 2048

Using same variable name inside nested loop may exhibits totally insane behavior.

Microzod

Well, it might look like a misstake. But num is num is not the same as nume.
I can't defend the choice of variable name but in my head it works out very clearly:)
num is the number of arrays to read to the PC and nume is 2047, to end up as 2048 samples in matlab.

Thank you very much for all help and the example, I would never had been able to do this my self this nor next year.

Microzod

Oh, you meant i...
Thanks, yes that did make funny things happen.

Microzod

Okey, I'll start over.
Something is very wrong in my now rather large code, its unresponsive and made up of 7 files.

I have lost track of things and its time to look at string parsing anyway.

Microzod

I am wondering, I am using as you did buffers of 2048 samples.

I am writing a Matlab GUI that requests a number of buffers of readings from my Due, skipping setup and all it works like this:

Code: [Select]

void loop()
{
   while (Serial.available())
  {
    option = Serial.readStringUntil('|');
    DMA_fcn();
    LED_fcn();
    adc_fcn();
  }
}

void DMA_fcn()
{
  while (option == "DMA")
  {
    num = Serial.parseInt();
    adc_start(ADC);
   
    for (int ii = 0; ii < num;)
    {
      if ( flag )
      {   
        uint16_t idx = flag -1;
        for (int i = 0; i < nume; i++)
        {
          Serial.write(inp[idx][i] >> 8);
          Serial.write(inp[idx][i] & 0xff);
     
        }
        ii++;
      }
      flag = 0;
    }
      adc_stop(ADC);
      option = "";
  }
 
}

// I am using the ADC_handler you provided and all the setups, many thanks again.



But I would like to have greater control of the samples from Matlab, as it is now I can specify a multiple of 2048 readings. Sending through the serial port from the PC 'DMA|10' returns 10x2048 values, as 10x2048x2 bytes.
I am thinking as I am writing but I'm thinking maybe something like enabling matlab to dictate the size of the pingpong buffer to be used by the Due's ADC_PDC. Or by some other means set up a system that allows to request a specified number of readings, or to specify a exact time frame during which the Due sends as many values as possible. That might be the most useful way to request samples...

Do you have any thoughts on this?

Regards

Go Up