Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows changing HW CDC Buffer Size after or before begin() #8559

Merged
merged 1 commit into from
Aug 24, 2023

Conversation

SuGlider
Copy link
Collaborator

@SuGlider SuGlider commented Aug 23, 2023

Description of Change

This PR fixes HW CDC for the C3 and S3 in order to keep the buffer size already set after calling HWCDC::begin(). In case no size was set before HWCDC:begin(), it will use a default RX/TX buffer size of 256 bytes.
HWCDC::setTxBufferSize() and HWCDC::setRxBufferSize() will set the buffer size if called at any time and it won't change until a new call to this function.
HWCDC:begin() will only set a default buffer size if none has been set already. If HWCDC::setTxBufferSize() and/or HWCDC::setRxBufferSize() was called before, it won't change the buffer size as set by the user. If those functions are called after HWCDC::begin() a new buffer will be allocated with the proper new size.

Therefore, the RX/TX buffer size can be set at any time, after or before HWCDC::begin() is called, even more than one time, in any order.
Previous memory will be released and a new buffer is allocated.

Tests scenarios

Tested with the ESP32-C3 and ESP32-S3 using HW Serial (HW USB CDC).

Sending 100, 200, 400, 500 and 1000 bytes using the Arduino IDE Serial Monitor.

void setup() {
  Serial.begin(115200);
  Serial.setRxBufferSize(1024);
  Serial.setTxBufferSize(1024);
  Serial.begin(115200);
}

#define MAX_INPUT_BUFFER_SIZE 4096

uint32_t inputBufferPos = 0;
uint8_t inputBuffer[MAX_INPUT_BUFFER_SIZE + 1];

void loop() {
  uint32_t readInLoopCount = 0;
  while (Serial.available()) {
    uint8_t readByte = Serial.read();
    inputBuffer[inputBufferPos] = readByte;
    inputBufferPos += 1;
    readInLoopCount += 1;

    if (readByte == '\n') {
      inputBuffer[inputBufferPos] = 0;
      // Serial.print((char*)inputBuffer);
      Serial.printf("GOT: %d\n", strlen((char*)inputBuffer));
      inputBufferPos = 0;
    }
    if (inputBufferPos >= MAX_INPUT_BUFFER_SIZE) {
      Serial.println("ERR INPUT TOO LONG");
      inputBufferPos = 0;
    }
  }

  if (readInLoopCount > 0) {
    Serial.printf("GOT IN LOOP: %d\n", readInLoopCount);
  }
}

Related links

Closes #8522
Related to #8528

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging this pull request may close these issues.

Serial communication breaking at around 300 characters, regression on 2.0.10
2 participants