1

I'm sending data through USART on an Arduino Due. I'm currently filling a buffer so the data gets sent just when a buffer is full.

The data I'm putting into the buffer is a lookup table of different wave shapes with 12 bits of depth (values from 0 to 4095). So I'm putting into the buffer values that are 2 bytes of depth, and the most significant byte is always 0.

My problem is that everyonce in a while a whole wave period gets shifted a byte (every value gets multiplicated by 256). The error is unpredictable: it might happen on the 2nd or 3rd period to be sent, but it happens soon. I tried slower baudrates, or adding two stopbits, but nothing fixes it. The relevant chunk of the code:

const int buflen = 2048;
int i = 0;
int j = 0;
int k = 1;
int wave = 0;
short buff[buflen];
volatile PROGMEM short sintab[3][512] = ...//there's no need to paste here the lookup tables

void setup(void){
  Serial3.begin(115200, SERIAL_8N2);
  }

void loop(void) {
  buff[j]= sintab[wave][i];
  i+= k;
  j++;
  if (i>511){
    i-=512;     
    }
  if (j>=buflen){
    byte* bytePointer =(byte*)buff;
    for (int l=0; l<=buflen; l++){
      Serial3.write(bytePointer[l]);
      Serial3.flush();
      }
    int j = =0;
    }

I'm checking the received data on both a serial monitor and a python program that stores the received values and print them. I think its weird that the error never happens in the middle of a wave: a one or two waves are copied good on the buffer and then a whole value gets shifted. How could I fix this?

1 Answers1

0

It looks like the issue is not in this block of code where you're writing the data out to your USART port, rather in storing the data to that array. When you have this byte offset occur, can you validate that the data in your array is as you expect it to be?

Edit: Change

for (int l=0; l<=buflen; l++)

to

for (int l=0; l< buflen; l++)

so you enumerate over the set 0 to 511, which is 512 elements. Now you are enumerating an additional element, which is reading data from an unexpected memory location and returning whatever is there (likely the next byte of your static structure).

Lev
  • 74
  • 5
  • The most obvious way to do so would be to send the data through serial. I just tried attaching a led and turning it on when the buffer value is over 4095 (which just happens if it has wrong values) and effectively it blinks every once in a while, in an unpredictable way. So apparently the data is being stored wrongly in the buffer. How could I fix this? – Dan Berezin Oct 22 '18 at 19:54
  • Not sure if its the problem, but, Allocate space for the buffer.. I don't see where you do that. – Lev Oct 22 '18 at 20:01
  • int * foo; foo = new int [5]; – Lev Oct 22 '18 at 20:03
  • Could you please explain how to do that? I just thought that declaring the buffer and its length there would be space saved for it. – Dan Berezin Oct 22 '18 at 20:09
  • I tried the following: `short* buff; ` then, on the setup `buff = new short(buflen)` where buflen is an int of the length of the buffer. It didn't fix the problem. Does that discard the memory allocation theory? – Dan Berezin Oct 22 '18 at 21:16
  • Oh I see it, I think. You have "<= bufflen" in your for loop where it is zero based, so should be < bufflen – Lev Oct 23 '18 at 01:49
  • I agree it should be < bufflen and I fixed it, but that still does not solve my problem. – Dan Berezin Oct 23 '18 at 16:18
  • I'm looking at your code, as posted, and see a line " int j = =0;". I'm guessing it got mangled -- I don't think i would parse. But, bigger issue, if you meant " int j=0;", it will create a NEW local variable "j". So your index will never start over (until it hits 65535). Eliminate the "int" (and the 2nd =). – bobwki Oct 23 '18 at 22:20