[STM32GENERIC] SerialUSB Print(F( speed and availableForWrite()

Discussions about the STM32generic core
User avatar
Pito
Posts: 1628
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

[STM32GENERIC] SerialUSB Print(F( speed and availableForWrite()

Post by Pito » Sun Jun 18, 2017 4:00 pm

While messing with the Zmodem example:
1. we miss the Serial.availableForWrite() method
2. when I look at the SerialUSB speed while printing out a bunch of strings (via Serial.print(F(..)) ) my impression is it runs like 9k6, I would guess it should be the fastest mode we've got.. Maybe I do something wrong?
Last edited by Pito on Wed Jun 21, 2017 6:33 am, edited 1 time in total.
Pukao Hats Cleaning Services Ltd.

User avatar
Pito
Posts: 1628
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: [STM32GENERIC] SerialUSB speed and availableForWrite()

Post by Pito » Sun Jun 18, 2017 4:59 pm

This is a bench for the Serial.print(F()) via SerialUSB (168MHz F407ZET):
UPDATE: println(F( vs. println(

Code: Select all

// Serial.print(F()) via SerialUSB benchmark
// Pito 6/2017
void setup() {
  Serial.begin(115200);
  delay(3000);

  uint32_t elapsed = micros();
  for (uint32_t i = 0; i < 20; i++) {
    Serial.println(F("THIS IS A TEST THIS IS A TEST THIS IS A TEST THIS IS A TEST THIS IS A TEST"));
  }
  elapsed = micros() - elapsed;
  
  Serial.print("Elapsed with (F(: ");
  Serial.print(elapsed);
  Serial.println(" usecs");
  Serial.println(" ");
  
  elapsed = micros();
  for (uint32_t i = 0; i < 20; i++) {
    Serial.println("THIS IS A TEST THIS IS A TEST THIS IS A TEST THIS IS A TEST THIS IS A TEST");
  }
  elapsed = micros() - elapsed;
  
  Serial.print("Elapsed: ");
  Serial.print(elapsed);
  Serial.println(" usecs");
}

void loop() {
}
Result:

Code: Select all

..
THIS IS A TEST THIS IS A TEST THIS IS A TEST THIS IS A TEST THIS IS A TEST
Elapsed with (F(: 1496123 usecs
..
THIS IS A TEST THIS IS A TEST THIS IS A TEST THIS IS A TEST THIS IS A TEST
Elapsed: 39971 usecs
Last edited by Pito on Mon Jun 19, 2017 10:49 am, edited 10 times in total.
Pukao Hats Cleaning Services Ltd.

danieleff
Posts: 336
Joined: Thu Sep 01, 2016 8:52 pm
Location: Hungary
Contact:

Re: [STM32GENERIC] SerialUSB speed and availableForWrite()

Post by danieleff » Sun Jun 18, 2017 5:07 pm

F407VE
...
THIS IS A TEST THIS IS A TEST THIS IS A TEST THIS IS A TEST THIS IS A TEST
Elapsed: 198278 usecs

User avatar
Pito
Posts: 1628
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: [STM32GENERIC] SerialUSB speed and availableForWrite()

Post by Pito » Sun Jun 18, 2017 5:09 pm

That is 8x faster.. :(
My clocks config:

Code: Select all

  RCC_OscInitStruct.PLL.PLLM = 8;
  RCC_OscInitStruct.PLL.PLLN = 336;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 7;
My usb stuff is from May 13th 2017.
Pukao Hats Cleaning Services Ltd.

User avatar
Pito
Posts: 1628
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: [STM32GENERIC] SerialUSB speed and availableForWrite()

Post by Pito » Sun Jun 18, 2017 5:42 pm

Can somebody try under Win7, plz?
Pukao Hats Cleaning Services Ltd.

stevestrong
Posts: 1823
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany

Re: [STM32GENERIC] SerialUSB speed and availableForWrite()

Post by stevestrong » Sun Jun 18, 2017 6:46 pm

not generic, but just as comparison to libmaple F407Vet6:

Code: Select all

Elapsed: 484 usecs
But we have 2*2kB buffer...

User avatar
Pito
Posts: 1628
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: [STM32GENERIC] SerialUSB speed and availableForWrite()

Post by Pito » Sun Jun 18, 2017 6:59 pm

That is 3100x faster that mine.. So it seems it is something wrong with my F407ZET's SerialUSB then :D
Pukao Hats Cleaning Services Ltd.

User avatar
Pito
Posts: 1628
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: [STM32GENERIC] SerialUSB speed and availableForWrite()

Post by Pito » Mon Jun 19, 2017 10:39 am

I've updated the benchmark above and:
1. with Serial.println("THIS.. it takes 39971 usecs
2. with Serial.println(F("THIS.. it takes 1496123 usecs
So it seems the "F" is the issue here.. (Black F407ZET)
Pukao Hats Cleaning Services Ltd.

User avatar
Pito
Posts: 1628
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: [STM32GENERIC] SerialUSB speed and availableForWrite()

Post by Pito » Tue Jun 20, 2017 11:57 pm

@Daniel and Steve: I do not understand how you can get such speeds with printing with "F".
When stepping with debugger through it, the "F" prints each char individually via USB (after each write(c) below I see a new char appears in the TeraTerm terminal)

Code: Select all

size_t Print::print(const __FlashStringHelper *ifsh)
{
  PGM_P p = reinterpret_cast<PGM_P>(ifsh);
  size_t n = 0;
  while (1) {
    unsigned char c = pgm_read_byte(p++);
    if (c == 0) break;
    if (write(c)) n++;
    else break;
  }
  return n;
}
which multiplied by 1ms gives the number above (==76*20*1ms = around 1500ms), while the standard print passes the pointer of the string buffer and its size to an USB tx routine, and the buffer is then txed via USB in one shot (~20x 1ms).
Or, am I using a wrong core lib? :?
Pukao Hats Cleaning Services Ltd.

danieleff
Posts: 336
Joined: Thu Sep 01, 2016 8:52 pm
Location: Hungary
Contact:

Re: [STM32GENERIC] SerialUSB speed and availableForWrite()

Post by danieleff » Wed Jun 21, 2017 4:31 am

Libmaple has optimizes version of it

Code: Select all

size_t Print::print(const __FlashStringHelper *ifsh)
{
  return print(reinterpret_cast<const char *>(ifsh));
}
I only just used the vanilla arduino one with the loop. (Edit: I do not know why my was faster than your actually.)
Last edited by danieleff on Wed Jun 21, 2017 4:46 am, edited 2 times in total.

Post Reply