Serial buffer extension for SAMD21 feather m0 express board

Hello i am using an atmel based SAMD21 microcontroller Feather M0 express board.
I want to increase the serial buffer size to receive 100 bytes of data from uart of a different microcontroller.

I am trying to follow the following link, will this be the right approach ?

https://www.hobbytronics.co.uk/arduino-serial-buffer-size#:~:text=However%2C%20this%20data%20buffer%20is,vast%20amount%20of%20RAM%20available.

I have a doubt that in this tutorial the core mentioned is of AVR, but i am using a samd21 which is arm based. Please let me know what should be my approach?

Thanks,
Shivam

Study Robin's Serial Input Basics - updated to get ideas how to solve your problem (without hacking the core). Change const byte numChars = 32 to the desired size.

I believe the serial buffer size on samd21 boards is already 256 bytes. ArduinoCore-samd/Uart.h at master · arduino/ArduinoCore-samd · GitHub

@ sterretje Yes, this is a correct way of solving the issue. But i don't want to change my existing program logic as implementing this will require to change the existing logic of the code.

Thanks,
Shivam

Actually, it turns out that Adafruit has increased the buffer size to 350 bytes on their SAMD21 boards...

westfw Thanks, i will check. Actually the task is that i am reading a 100 byte string terminated by \n from a SD card via SPI protocol using arduino function readStringUntil('\n') and after reading the string i am doing some data processing on it. So when i am reading upto 56 bytes. The results are as expected but when i am increasing the number of bytes to be read then the results are not correct.
What should i do ?

Show your code.

Where does the serial buffer fit in this? SPI does not use the buffer that you want to increase.

I have no reason not to believe @westfw, so whatever it is, your logic is flawed.

@ sterretje Okay, so now this is the problem i have to solve. Actually i was earlier using uart instead of spi. But now i am using SPI.
Here is my code:

 if (Filec) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    int i=0;
    int j=0;
    while (Filec.available()) 
    {

      String Dc = Filec.readStringUntil('\n');
      int ind1 = Dc.indexOf(',');
      delay (20);
      int ind2 = Dc.indexOf(',',ind1+1);
      delay (20);
      int ind3 = Dc.indexOf(',',ind2+1);
      delay (20);
      int ind4 = Dc.indexOf(',',ind3+1);
      delay (20);
          A = Dc.substring(0,ind1);
          delay (20);
          B = Dc.substring(ind1+1,ind2);
          delay (20);
          C = Dc.substring(ind2+1,ind3);
          delay (20);
          D = Dc.substring(ind3+1);
          delay (20);
          Serial.print(A);
          Serial.print("  ");
          delay (25);
          Serial.print(B);
          Serial.print("  ");
          delay (25);
          Serial.print(C);
          Serial.print("  ");
          delay (25);
          Serial.print(D);
          Serial.println();
          delay (25);
          if (i<32)
          {
          arr [i][j]   = A.toFloat();
          arr [i][j+1] = B.toFloat();
          arr [i][j+2] = C.toFloat();
          arr [i][j+3] = D.toFloat();
          i=i+1;
          }
          else 
          {
            i=0;
          }
          
          Serial.println("                         ");
          Serial.println("Now printing float values");
          Serial.println("                         ");
          Serial.print(arr[i][j]);
          Serial.print("   ");
          delay(25);
          Serial.print(arr[i][j+1]);
          Serial.print("   ");
          delay(25);
          Serial.print(arr[i][j+2]);
          Serial.print("   ");
          delay(25);
          Serial.println(arr[i][j+3]);
          delay(25);
      } 
    Filec.close();
    }    

    // close the file:

     
    else 
    {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
    }

I have attached the snapshot for the result, i am getting 4 "0.00" which should not be such values.

Please post full code or at least code that we can compile and test.

Please post a small example if the data on the card or that you want to read (couple of lines). Or attach a small example file.

Here is the code below:

#include <SPI.h>
#include <SD.h>
#include <Arduino.h>   // required before wiring_private.h
#include "wiring_private.h" // pinPeripheral() function
#include <U8x8lib.h>
#include <Wire.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
#define PIN      8
#define NUMPIXELS 1
#define SERIAL_BUFFER_SIZE 256 
Uart Serial2 (&sercom1, 11, 10, SERCOM_RX_PAD_0, UART_TX_PAD_2);
void SERCOM1_Handler()
{
  Serial2.IrqHandler();
}
File dataFile;
File Filec;
String Data;
String A;
String B;
String C;
String D;
String R;
String Ch;
float arr [32][3]; //3 column
int k=0;
int l=0;
char timestamp[30];
const int chipSelect = 6;
void setup() 
{
  Serial1.begin(9600);
  Serial2.begin(9600);
  Wire.begin();
  // Assign pins 10 & 11 SERCOM functionality
  pinPeripheral(10, PIO_SERCOM);
  pinPeripheral(11, PIO_SERCOM);
  Serial2.print("Initializing SD card...");
  if (!SD.begin(chipSelect)) 
  {
    Serial2.println("Card failed, or not present");
    return;
  }
  Serial2.println("card initialized.");
  delay(2000);
  Filec = SD.open("test.txt");
  if (Filec) {
    // read from the file until there's nothing else in it:
    int i=0;
    int j=0;
    while (Filec.available()) 
    {

      String Dc = Filec.readStringUntil('\n');
      int ind1 = Dc.indexOf(',');
      delay (20);
      int ind2 = Dc.indexOf(',',ind1+1);
      delay (20);
      int ind3 = Dc.indexOf(',',ind2+1);
      delay (20);
      int ind4 = Dc.indexOf(',',ind3+1);
      delay (20);
          A = Dc.substring(0,ind1);
          delay (20);
          B = Dc.substring(ind1+1,ind2);
          delay (20);
          C = Dc.substring(ind2+1,ind3);
          delay (20);
          D = Dc.substring(ind3+1);
          delay (20);
          Serial.print(A);
          Serial.print("  ");
          delay (25);
          Serial.print(B);
          Serial.print("  ");
          delay (25);
          Serial.print(C);
          Serial.print("  ");
          delay (25);
          Serial.print(D);
          Serial.println();
          delay (25);
          if (i<32)
          {
          arr [i][j]   = A.toFloat();
          arr [i][j+1] = B.toFloat();
          arr [i][j+2] = C.toFloat();
          arr [i][j+3] = D.toFloat();
          i=i+1;
          }
          else 
          {
            i=0;
          }
          
          Serial.println("                         ");
          Serial.println("Now printing float values");
          Serial.println("                         ");
          Serial.print(arr[i][j]);
          Serial.print("   ");
          delay(25);
          Serial.print(arr[i][j+1]);
          Serial.print("   ");
          delay(25);
          Serial.print(arr[i][j+2]);
          Serial.print("   ");
          delay(25);
          Serial.println(arr[i][j+3]);
          delay(25);
      } 
    Filec.close();
    }    

    // close the file:  
    else 
    {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
    }
}

void loop() 
{

}

This is the attached file which i am reading from the SD card.
WD002.txt (2.3 KB)

print defaults to two decimal places for floats, if you want to print more decimal places you need to specify how many:

Serial.print(arr[i][j], 7); //print seven decimal places

Also you need to check how float is implemented on the Feather M0, there is a limit to how many significant digits can be stored, might need to use double.

You're better off biting the bullet now and doing it correctly rather than trying to patch up a hacky solution.

@ david_2018 yes, i am now using the following code below but i am not getting the exact decimal places matching. I am attaching a snapshot of image showing comparison of what i am able to read as float and how the numbers are stored in txt file.

#include <SPI.h>
#include <SD.h>
#include <Arduino.h>   // required before wiring_private.h
#include "wiring_private.h" // pinPeripheral() function
#include <U8x8lib.h>
#include <Wire.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
#define PIN      8
#define NUMPIXELS 1
#define SERIAL_BUFFER_SIZE 256 
Uart Serial2 (&sercom1, 11, 10, SERCOM_RX_PAD_0, UART_TX_PAD_2);
void SERCOM1_Handler()
{
  Serial2.IrqHandler();
}
File Filec;
String Data;
String A;
String B;
String C;
String D;
float arr [32][4]; //3 column
const int chipSelect = 6;
void setup() 
{
  Serial1.begin(9600);
  Serial2.begin(9600);
  Wire.begin();
  // Assign pins 10 & 11 SERCOM functionality
  pinPeripheral(10, PIO_SERCOM);
  pinPeripheral(11, PIO_SERCOM);
  Serial2.print("Initializing SD card...");
  if (!SD.begin(chipSelect)) 
  {
    Serial2.println("Card failed, or not present");
    return;
  }
  Serial2.println("card initialized.");
  delay(2000);
  Filec = SD.open("test.txt");
  if (Filec) {
    // read from the file until there's nothing else in it:
    int i=0;
    int j=0;
    delay(1000);
    Serial.println("     Printing coefficients          ");
    delay(1000);
    while (Filec.available()) 
    {

      String Dc = Filec.readStringUntil('\n');
      int ind1 = Dc.indexOf(',');
      delay (20);
      int ind2 = Dc.indexOf(',',ind1+1);
      delay (20);
      int ind3 = Dc.indexOf(',',ind2+1);
      delay (20);
      int ind4 = Dc.indexOf(',',ind3+1);
      delay (20);
          A = Dc.substring(0,ind1);
          delay (20);
          B = Dc.substring(ind1+1,ind2);
          delay (20);
          C = Dc.substring(ind2+1,ind3);
          delay (20);
          D = Dc.substring(ind3+1);
          delay (20);
          if (i<32)
          {
          arr [i][j]   = A.toFloat();
          arr [i][j+1] = B.toFloat();
          arr [i][j+2] = C.toFloat();
          arr [i][j+3] = D.toFloat();
          Serial.print(arr[i][j],16);
          Serial.print("   ");
          delay(25);
          Serial.print(arr[i][j+1],16);
          Serial.print("   ");
          delay(25);
          Serial.print(arr[i][j+2],16);
          Serial.print("   ");
          delay(25);
          Serial.println(arr[i][j+3],16);
          delay(25);
          i=i+1;
          }
          else 
          {
            i=0;
          }
      } 
    Filec.close();
    }    

    // close the file:  
    else 
    {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
    }
}

void loop() 
{

}

txt file containg the data to be read and stored as float.

WD002.txt (2.3 KB)

Try using double instead of float.

@ david_2018 It worked using double thanks. Now my doubt is when i am using double and storing this number into a variable will it use all 15 decimal digits for calculation or less digits it will use? Since for serial.print i am doing serial.print(arr[i][j],16) but for calculation there is no such command but i want to use all decimal digits in the formula so that the results are accurate.
Thanks,
Shivam

Calculations are done with the type (double in your case). The 16 in the serial prints is only for text representation.

So you mean all decimal digits will be used in calculations.

Yes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.