Blue Pill running slow

Post here first, or if you can't find a relevant section!
IanNoob
Posts: 15
Joined: Wed Oct 07, 2020 5:35 pm

Blue Pill running slow

Post by IanNoob »

Hi. New here, please be kind to me!
I managed to get a blue pill working on the Arduino IDE - it took a while because the memory was write protected - but I got round that. Great joy when Blinky worked! Changed the delay and that worked too!
Now moved the output from PC13 to PC14 and put a scope on the output. Perfect square wave of the expected time interval.
Now removed the delay altogether to see what I got from a 72MHz chip.
500kHz.
1uS high, 1uS low (actually, about 30nS longer on low, which I assume to be the 'cost' of the loop instruction).
I did see, somewhere, something that gave options of 1MHz up to 50MHz but I can't find that.
Can anyone tell me what's going on and, better still, tell me what I can do something about it.

The actual sketch is:
void setup() {
pinMode(PC14, OUTPUT);
}
void loop() {
digitalWrite(PC14, HIGH);
digitalWrite(PC14, LOW);
}

Thank you

Ian

User avatar
fpiSTM
Posts: 851
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 33
Location: Le Mans
Contact:

Re: Blue Pill running slow

Post by fpiSTM »

Try:

Code: Select all

digitalWriteFast(PC_14, HIGH);
Same for low.

IanNoob
Posts: 15
Joined: Wed Oct 07, 2020 5:35 pm

Re: Blue Pill running slow

Post by IanNoob »

Thank you fpiSTM, that certainly worked.
The High and Low together come in at about 50nS instead of 2uS, so about 40 times faster.

Are there a list of tips like this that I need to know about? I've never seen this before and I'm not sure that I understand the purpose - digitalWrite is slowed down?

Ian

User avatar
fpiSTM
Posts: 851
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 33
Location: Le Mans
Contact:

Re: Blue Pill running slow

Post by fpiSTM »

Well several are described in the Wiki.
For the slow digitalWrite, this is due to arduino API compatibilty.

mrburnette
Posts: 314
Joined: Thu Dec 19, 2019 1:23 am
Answers: 1

Re: Blue Pill running slow

Post by mrburnette »

IanNoob wrote:
Wed Oct 07, 2020 6:11 pm
Hi. New here, please be kind to me!
...
Ian,

I wrote this years ago for the original forum; it has been edited to reflect new links:
viewtopic.php?f=2&t=301

This is a more recent post regarding headings about "broken" stuff (spoiler, user error is statistically the root cause):
viewtopic.php?f=2&t=673

When fpiSTM used "compatibility" he did not mean that the switching was slowed down on purpose, he meant the architecture necessary to produce Arduino-like results is inherently inefficient - by design. This explains things well:
https://www.eeworldonline.com/understan ... g-the-mcu/

Welcome aboard. Remember, the forum often uses 2 separate cores so we really need to know if you are using the older libmaple (Roger's) core or the newest STM Corporate core. For the old libmaple core, this WiKi is useful and parallels much of the Official core:
https://stm32duinoforum.com/forum/wiki_ ... e_API.html


Ray

IanNoob
Posts: 15
Joined: Wed Oct 07, 2020 5:35 pm

Re: Blue Pill running slow

Post by IanNoob »

Thank you so much Ray. I've spent the last couple of days following the links that you gave - and then the links from the links - and then the links from the links from the links.....

In your last paragraph you ask what cores I am using. I'm using a Blue Pill from Aliexpress. I spent 3 days following different protocols online to get them working with Arduino. I even soldered up a second Pill in case the first one was faulty. I downloaded some Maple software as well as STMCube Programmer but, to be honest, it works now but I have no idea which bits are doing what. Is there an easy way to see what I am actually using - "the older libmaple (Roger's) core or the newest STM Corporate core."?

My project is (slightly) described in the intro that I just wrote here: viewtopic.php?f=3&t=706 to which you have already replied!
I was using assembler within the Arduino IDE to produce fast (<100nS) "digital writes" and <200nS "digital reads" and I will need to achieve similar on the STM32. I am also looking to build in a fast 'stopwatch' with a resolution better than 1uS (not too worried about accuracy, just reasonable precision).

Thanks again.

Ian

mrburnette
Posts: 314
Joined: Thu Dec 19, 2019 1:23 am
Answers: 1

Re: Blue Pill running slow

Post by mrburnette »

IanNoob wrote:
Fri Oct 09, 2020 5:23 pm
Thank you so much Ray. I've spent the last couple of days following the links that you gave - and then the links from the links - and then the links from the links from the links.....
You are welcome. I' am old-as-dirt and my style is the same: old school; so I rarely directly answer, preferring to provide links into material that will both answer your question(s) and provide a greater learning activity. If the links do not make sense then I failed to read & understand your question.
IanNoob wrote:
Fri Oct 09, 2020 5:23 pm
In your last paragraph you ask what cores I am using. I'm using a Blue Pill from Aliexpress. I spent 3 days following different protocols online to get them working with Arduino. I even soldered up a second Pill in case the first one was faulty. I downloaded some Maple software as well as STMCube Programmer but, to be honest, it works now but I have no idea which bits are doing what. Is there an easy way to see what I am actually using - "the older libmaple (Roger's) core or the newest STM Corporate core."?
I have heard this before, the short answer is the current board selected from the board Group. Below, I have the Maple Mini selected from Roger's Core, manually installed in {HOME}Arduino/hardware/
ArduinoIDE Board Selection_.jpg
ArduinoIDE Board Selection_.jpg (59.85 KiB) Viewed 759 times
Note: Roger's core has no installation file (no JSON) - it is just expanded (copied) with structure intact.

The longer answer is here: https://arduino.github.io/arduino-cli/p ... ification/
IanNoob wrote:
Fri Oct 09, 2020 5:23 pm
My project is (slightly) described in the intro that I just wrote here: viewtopic.php?f=3&t=706 to which you have already replied!
I was using assembler within the Arduino IDE to produce fast (<100nS) "digital writes" and <200nS "digital reads" and I will need to achieve similar on the STM32. I am also looking to build in a fast 'stopwatch' with a resolution better than 1uS (not too worried about accuracy, just reasonable precision).
As the 8-bit Atmega328P is running at 16MHz and the STM32F103xx is running at 72MHz (or 128MHz over-clocked) 100nS is simply not an issue. The starting place is here: https://stm32duinoforum.com/forum/wiki_ ... _registers

If you are using Roger's libmaple core, best read these threads:
http://stm32duinoforum.com/forum/viewto ... rt_30.html
http://stm32duinoforum.com/forum/viewto ... rt_40.html

If you really want to get deep into the STM32, I recommend the excellent eBook (free):
https://freecomputerbooks.com/Discoveri ... oller.html
and of course the STM reference manual:
https://www.st.com/resource/en/referenc ... ronics.pdf

mrburnette
Posts: 314
Joined: Thu Dec 19, 2019 1:23 am
Answers: 1

Re: Blue Pill running slow

Post by mrburnette »

Another resource is the stm32-base.org pages.

Thanks to tomtom381
About
The purpose of the STM32-base project is to provide you with a simple and easy to use base project for working with STM32 microcontrollers. More information on why this project came into being can be found on the about page.


IanNoob
Posts: 15
Joined: Wed Oct 07, 2020 5:35 pm

Re: Blue Pill running slow

Post by IanNoob »

Ok, now I'm completely happy about digitalWriteFast() - it does exactly what I wanted and fits in with what I need to do.

I have tried with digitalReadFast() and while it is a little faster than digitalRead() it is nowhere near as fast as it could be.
Using this code as a test bed:

Code: Select all

int buttonState = 0;  

void setup() {
    pinMode(PB4, OUTPUT);
    pinMode(PB3, INPUT);
}

void loop() {
  
// buttonState = digitalRead(PB3);       // Method A
   buttonState = digitalReadFast(PB_3);  // Method B

  if (buttonState == HIGH) {
      digitalWrite(PB4, HIGH);
  } else {
      digitalWrite(PB4, LOW);
  }
}
I found (using an oscilloscope) that the reaction time to a repeating pulse from a Nano is very 'juddery' and varies between:
1.7uS to 3.8uS using Method A (digitalRead) and
1.0uS to 2.6uS using Method B (digitalReadFast)

Is this what you would expect? Am I doing something fundamentally flawed?
I have read a lot about using the registers directly (which I do on the Nano already) but so far have not managed to do on the Blue Pill. There are plenty of examples using code like

Code: Select all

GPIOB->MODER  &= ~(0x3 << (BUTTON_PIN*2));
GPIOB->PUPDR  &= ~(0x3 << (BUTTON_PIN*2));
GPIOB->PUPDR  |=  (0x1 << (BUTTON_PIN*2));


- but I haven't been able to 'interface' this with the Arduino IDE so I'm assuming that this code is for a different environment.

Any help gratefully received.

Ian

User avatar
fpiSTM
Posts: 851
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 33
Location: Le Mans
Contact:

Re: Blue Pill running slow

Post by fpiSTM »

You can use directly the register access instead of HAL or LL. The CMSIS definition are available.

Post Reply

Return to “General discussion”