PlatformIO and Arduino IDE compilation results are different

Post here first, or if you can't find a relevant section!
ozcar
Posts: 149
Joined: Wed Apr 29, 2020 9:07 pm
Answers: 5

Re: PlatformIO and Arduino IDE compilation results are different

Post by ozcar »

GonzoG wrote: Thu Mar 11, 2021 12:57 am ...
Also you're ISR isn't as fast as it can be. This 10 bit shift needs time.
You can use

Code: Select all

digitalReadFast(digitalPinToPinName(pin));
and set optimization to fastest (-O3)

Code: Select all

if ( GPIOB->IDR & GPIO_IDR_ID10 ) receiver_input1_previous = no
Would obviate 10 bit shift (at execution time), but I doubt that (or digitalReadFast) would make much difference to the timing.
leonardo
Posts: 18
Joined: Sat Mar 06, 2021 2:37 pm

Re: PlatformIO and Arduino IDE compilation results are different

Post by leonardo »

Oh sorry, I did not notice the error in this line of code,

Code: Select all

  timecnt = micros();                                        
because the calculation result is still correct

I have another RC receiver, so I want to use these codes to read pwm waves. But I don’t want too much delay
Last edited by leonardo on Thu Mar 11, 2021 5:26 am, edited 1 time in total.
leonardo
Posts: 18
Joined: Sat Mar 06, 2021 2:37 pm

Re: PlatformIO and Arduino IDE compilation results are different

Post by leonardo »

Oh sorry, I did not notice the error in this line of code,

Code: Select all

  timecnt = micros();                                        
because the calculation result is still correct

I have another RC receiver, so I want to use these codes to read pwm waves. But I don’t want too much delay
leonardo
Posts: 18
Joined: Sat Mar 06, 2021 2:37 pm

Re: PlatformIO and Arduino IDE compilation results are different

Post by leonardo »

Thanks for your reply, I tried the function you suggested,

Code: Select all

digitalReadFast(digitalPinToPinName(pin));
but the loop is still 1100 microseconds after using this function
ozcar
Posts: 149
Joined: Wed Apr 29, 2020 9:07 pm
Answers: 5

Re: PlatformIO and Arduino IDE compilation results are different

Post by ozcar »

I looked at what the compiler does for statements like these:

Code: Select all

    if ( GPIOB->IDR & 1 << 10 ) ...; 
    if ( GPIOB->IDR >> 10 & 1 ) ...;
I thought that potentially the first of those might execute a tad faster than the second, but no, it turns out the compiler is smart enough to generate exactly the same code, like this, with the shift effectively done at compile time for both of those:

Code: Select all

0x08005d7c: 0d 4b           	ldr	r3, [pc, #52]	;  point to GPIOB 
0x08005d7e: 1b 69           	ldr	r3, [r3, #16]    ;  grab IDR
0x08005d80: 13 f4 80 6f     	tst.w	r3, #1024	; & with 1 << 10
0x08005d84: 01 d0           	beq.n	0x8005d8a <setup()+102>
By contrast, using digitalReadFast(digitalPinToPinName(PB10)) generated way more instructions. Even digitalReadFast(PB_10) was about twice as many instructions as directly referring to the IDR. Still, you would have to be generating interrupts at a very high rate for this to make any difference.
ozcar
Posts: 149
Joined: Wed Apr 29, 2020 9:07 pm
Answers: 5

Re: PlatformIO and Arduino IDE compilation results are different

Post by ozcar »

leonardo wrote: Thu Mar 11, 2021 6:03 am but the loop is still 1100 microseconds after using this function
Maybe show us your code as it is now. I'm not sure what you are measuring.
leonardo
Posts: 18
Joined: Sat Mar 06, 2021 2:37 pm

Re: PlatformIO and Arduino IDE compilation results are different

Post by leonardo »

I want to test the execution time of the function "digitalReadFast(digitalPinToPinName(pin))"
When I execute the print function, the loop time of the entire loop() is 1146 μs

Code: Select all

#include "Arduino.h"

void setup() {
  Serial.begin(9600); 
}

void loop() {

  unsigned int timecnt;
  timecnt = micros();

  Serial.print(digitalReadFast(digitalPinToPinName(PB10)));
  
  timecnt = micros()-timecnt;//Subtract the last result after the function test is completed
  Serial.print("\t");
  Serial.print("micros=");//print the "timecnt"
  Serial.println(timecnt);

}
But if I don’t print this function, the loop time of the entire loop() is 1 μs

Code: Select all

#include "Arduino.h"

void setup() {
  Serial.begin(9600); 
}

void loop() {

  unsigned int timecnt;
  timecnt = micros();
  
  digitalReadFast(digitalPinToPinName(PB10));

  timecnt = micros()-timecnt;//Subtract the last result after the function test is completed
  Serial.print("\t");
  Serial.print("micros=");//print the "timecnt"
  Serial.println(timecnt);

}
This is a simple test, I think the too low serial port baud rate consumes time.
When I after configure monitor_speed = 2000000 (the last monitor_speed = 9600),The execution time of the second loop() is 27μs. :shock:
ozcar
Posts: 149
Joined: Wed Apr 29, 2020 9:07 pm
Answers: 5

Re: PlatformIO and Arduino IDE compilation results are different

Post by ozcar »

If you really wanted to print the result of the digitalReadFast(), but exclude the time taken for the Serial.print() from your measurement, then you could assign the return value from digitalReadFast() to an int variable, and then print that together with the timecnt value.

Based on the code I saw generated for the digitalReadFast(), 27μS seems like a very long time. I picked up your code and tried it on a 72MHz F303 I happen to have lying here, and it prints times of 1 or 2 μS.
GonzoG
Posts: 460
Joined: Wed Jan 15, 2020 11:30 am
Answers: 32
Location: Prudnik, Poland

Re: PlatformIO and Arduino IDE compilation results are different

Post by GonzoG »

leonardo wrote: Thu Mar 11, 2021 8:47 am This is a simple test, I think the too low serial port baud rate consumes time.
When I after configure monitor_speed = 2000000 (the last monitor_speed = 9600),The execution time of the second loop() is 27μs. :shock:
If you're using USB serial, then baud rate does not matter as there is no fixed baud rate. It's USB and it always goes as fast as possible.
mrburnette
Posts: 633
Joined: Thu Dec 19, 2019 1:23 am
Answers: 7

Re: PlatformIO and Arduino IDE compilation results are different

Post by mrburnette »

Strictly speaking, micros() returns an unsigned long
Post Reply

Return to “General discussion”