T3.x/LC - SerialX - Adding support for addMemoryForRead/Write ...

KurtE

Senior Member+
I know yet another diversion,

but got tired of telling people for T3.x go edit serail1.c ... whenever they were finding their sketch did not work properly with the default buffer sizes, when in T4.x we have methods to increase the size on a per sketch basis.

So I am trying out adding this to the T3.x code base.

It is a little more of a pain as on T4, all of the hardware code is one class, so just needed to fix it once. Where as there is code and a class for each of the Hardware Serial objects so have to replicate the changes 7 times.

I have a first pass done, up in a branch: https://github.com/KurtE/cores/tree/T3X_SerialX_AddMemory

Right now I am in the process of debugging. Running into some strange things, which probably implies when duplicating the changes from one file to the next, I did not properly update something like the names of the MAX or registers or...
But hopefully soon.

One thing I running into now. Is the one case which reasonably different is Serial6 on T3.6 as it is it uses a completely different subsystem (LPUART instead of UART). And on the one T3.6 I have found in my box of stuff where I brought out the bottom pins, the USBToSerial is not echoing properly on this port. Actually even with the current released code. Maybe an issue on my one board, which may imply that I need to solder up another one...

But has anyone else have a T3.6 with the bottom pads brought out that can see if the current released Teensyduino works for Serial6 with the USBToSerial sketch?

Now back to debugging.

EDIT: Ran the pin test HiLow test and it does not see 48... So may try touch up of solder.
I have another one where the USB port is flaky... Does not work unless i press down certain spot of the USB... Will see if I have USB breakout board easily findable to plug into it.

But I may just punt and verify code still works when thumb on those for a couple of tests.
 
Last edited:
Quick update - I went through with test program:
Code:
uint32_t current_baud = 38400;
#define EXTRA_BUFS
#ifdef EXTRA_BUFS
uint8_t rxBuffer1[100]  DMAMEM;
uint8_t txBuffer1[100]  DMAMEM;
uint8_t rxBuffer2[100]  DMAMEM;
uint8_t txBuffer2[100]  DMAMEM;
uint8_t rxBuffer3[100]  DMAMEM;
uint8_t txBuffer3[100]  DMAMEM;
#if defined(__MK64FX512__) || defined(__MK66FX1M0__)|| defined(__IMXRT1062__)
uint8_t rxBuffer4[100]  DMAMEM;
uint8_t txBuffer4[100]  DMAMEM;
uint8_t rxBuffer5[100]  DMAMEM;
uint8_t txBuffer5[100]  DMAMEM;
uint8_t rxBuffer6[100]  DMAMEM;
uint8_t txBuffer6[100]  DMAMEM;
#endif
#endif
uint16_t counts_read[6];
uint16_t max_counts[6];
uint16_t calls_count[6];
void setup() {
  pinMode(2, OUTPUT);
  pinMode(13, OUTPUT);
  digitalWrite(2, LOW);
  while (!Serial && (millis() < 3000)) ;
  Serial.begin(38400);
  Serial.println("Test Serial ports");

#ifdef EXTRA_BUFS
  Serial1.addMemoryForRead(rxBuffer1, sizeof(rxBuffer1));
  Serial1.addMemoryForWrite(txBuffer1, sizeof(txBuffer1));
  Serial2.addMemoryForRead(rxBuffer2, sizeof(rxBuffer2));
  Serial2.addMemoryForWrite(txBuffer2, sizeof(txBuffer2));
  Serial3.addMemoryForRead(rxBuffer3, sizeof(rxBuffer3));
  Serial3.addMemoryForWrite(txBuffer3, sizeof(txBuffer3));
#if defined(__MK64FX512__) || defined(__MK66FX1M0__)|| defined(__IMXRT1062__)
  Serial4.addMemoryForRead(rxBuffer4, sizeof(rxBuffer4));
  Serial4.addMemoryForWrite(txBuffer4, sizeof(txBuffer4));
  Serial5.addMemoryForRead(rxBuffer5, sizeof(rxBuffer5));
  Serial5.addMemoryForWrite(txBuffer5, sizeof(txBuffer5));
  Serial6.addMemoryForRead(rxBuffer6, sizeof(rxBuffer6));
  Serial6.addMemoryForWrite(txBuffer6, sizeof(txBuffer6));
#endif
#endif
  Serial.println("Test Serial ports");
  Serial1.begin(current_baud);
  Serial2.begin(current_baud);
  Serial3.begin(current_baud);
#if defined(__MK64FX512__) || defined(__MK66FX1M0__)|| defined(__IMXRT1062__)
  Serial4.begin(current_baud);
  Serial5.begin(current_baud);
  Serial6.begin(current_baud);
#endif
}

void loop() {
  Serial.printf("Output to all Serial ports at Baud %d\n", current_baud);  Serial.flush();
  digitalWriteFast(2, HIGH);
  for (uint8_t i = 0; i < 6; i++) {
    counts_read[i] = 0;
    max_counts[i] = 0;
    calls_count[i] = 0;
  }
  Serial1.println("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
  // Test some of the Serial1 helper functions that still may be used for debug

  digitalWriteFast(2, LOW);
  delay(200); // give time for it to happen
  Serial.print("Counts(T:C:M) ");
  for (uint8_t i = 0; i < 6; i++) {
    Serial.printf("(%u:%u:%u)", counts_read[i], calls_count[i], max_counts[i]);
  }

  Serial.println("\nEnter new baud or hit enter to continue using previous baud");

  uint32_t new_baud = 0;
  int ch;
  while (!Serial.available()) ;
  while ((ch = Serial.read()) > 0) {
    if ((ch >= '0') && (ch <= '9'))
      new_baud = new_baud * 10 + ( ch - '0');
  }
  if (new_baud && (new_baud != current_baud)) {
    current_baud = new_baud;
    Serial.printf("Changing Serial port buad rates to %u\n", current_baud); Serial.flush();
    Serial1.end();
    Serial1.begin(current_baud);
    Serial2.end();
    Serial2.begin(current_baud);
    Serial3.end();
    Serial3.begin(current_baud);
    Serial.println("After Serial1-3 begin"); Serial.flush();
#if defined(__MK64FX512__) || defined(__MK66FX1M0__)|| defined(__IMXRT1062__)
    Serial4.end();
    Serial5.end();
    Serial6.end();
    Serial.println("After Serial4-6 end"); Serial.flush();
    Serial4.begin(current_baud);
    Serial5.begin(current_baud);
    Serial6.begin(current_baud);
    Serial.println("After Serial4-6 begin"); Serial.flush();
#endif

  }
  Serial.println("exit loop"); Serial.flush();
}

void serialEvent1() {
  digitalWriteFast(13, HIGH);
  uint8_t buffer[128];
  uint8_t cb = min (min((int)sizeof(buffer), Serial1.available()), Serial.availableForWrite());
  static const uint8_t index = 0; //
  calls_count[index]++;
  counts_read[index] += cb;
  if (cb > max_counts[index]) max_counts[index] = cb;
  Serial1.readBytes(buffer, cb);
  Serial.write(buffer, cb);
  digitalWriteFast(13, LOW);
}

void serialEvent2() {
  uint8_t buffer[128];
  uint8_t cb = min (min((int)sizeof(buffer), Serial2.available()), Serial2.availableForWrite());
  static const uint8_t index = 1; //
  calls_count[index]++;
  counts_read[index] += cb;
  if (cb > max_counts[index]) max_counts[index] = cb;
  Serial2.readBytes(buffer, cb);
  Serial2.write(buffer, cb);
}
void serialEvent3() {
  uint8_t buffer[128];
  uint8_t cb = min (min((int)sizeof(buffer), Serial3.available()), Serial3.availableForWrite());
  static const uint8_t index = 2; //
  calls_count[index]++;
  counts_read[index] += cb;
  if (cb > max_counts[index]) max_counts[index] = cb;
  Serial3.readBytes(buffer, cb);
  Serial3.write(buffer, cb);
}
#if defined(__MK64FX512__) || defined(__MK66FX1M0__)|| defined(__IMXRT1062__)
void serialEvent4() {
  uint8_t buffer[128];
  uint8_t cb = min (min((int)sizeof(buffer), Serial4.available()), Serial4.availableForWrite());
  static const uint8_t index = 3; //
  calls_count[index]++;
  counts_read[index] += cb;
  if (cb > max_counts[index]) max_counts[index] = cb;
  Serial4.readBytes(buffer, cb);
  Serial4.write(buffer, cb);
}
void serialEvent5() {
  uint8_t buffer[128];
  uint8_t cb = min (min((int)sizeof(buffer), Serial5.available()), Serial5.availableForWrite());
  static const uint8_t index = 4; //
  calls_count[index]++;
  counts_read[index] += cb;
  if (cb > max_counts[index]) max_counts[index] = cb;
  Serial5.readBytes(buffer, cb);
  Serial5.write(buffer, cb);
}
void serialEvent6() {
  uint8_t buffer[128];
  uint8_t cb = min (min((int)sizeof(buffer), Serial6.available()), Serial6.availableForWrite());
  static const uint8_t index = 5; //
  calls_count[index]++;
  counts_read[index] += cb;
  if (cb > max_counts[index]) max_counts[index] = cb;
  Serial6.readBytes(buffer, cb);
  Serial6.write(buffer, cb);
}
#endif

And now appears to be working.

But I ran into issue where the bypass code for only calling serialEventX when the user actually has that code had an issue that if you call SerialX.begin(...) multiple times it will keep adding itself to the list and overrun memory.
So fixed that.

Anyway decided to create a Pull Request for it... https://github.com/PaulStoffregen/cores/pull/500
 
Good work @KurtE - was wondering why the .end()'s were there.

I see addToSerialEventsList() in .begin() won't double add. Though .end() doesn't remove, which is probable less clean.
 
Hi guys,

Interested in seeing what this will do, but my "HardwareSerial.h" does not have the addMemoryForRead( , ) function.


btw, I am working in platformIO, so I looked in the teensy cores that are used within that platform

- The teensy3 cores "HardwareSerial.h" do not contain the function

- In the teensy4 cores there is something similar, but it's called "addStorageForRead( , )"


Am I missing something or do I need to wait for the release of a new core for platformIO ?
 
Sorry, I don't remember if this went in for the last release or not or only one of the recent betas...

As for when/how to get updates here on PlatformIO... Not sure as I really don't use it. But I believe I have seen some postings of where people have done things like copy the current cores into the equivalent directory somewhere where PlatformIO uses it from...
 
Sorry, I don't remember if this went in for the last release or not or only one of the recent betas...

As for when/how to get updates here on PlatformIO... Not sure as I really don't use it. But I believe I have seen some postings of where people have done things like copy the current cores into the equivalent directory somewhere where PlatformIO uses it from...


Thanks for the update.
I don't really want to mess around with swapping cores in platformIO.
I have just ordered a Teensy4.0 and see how that works. LoL

Thanks all the same.
 
Back
Top