Bluepill Blinky¶
And a gentle introduction to Forth.
If you use the STM32F103C8 DIAGNOSTICS image and have wondered about what Forth is and what it can do (apart from make cool standalone binary diagnostcs programs for Blue Pills) this page may be for you.
This blinky runs on the STM32F103C8 DIAGNOSTICS image. The Bluepill LED is connected to GPIOC-13.
Download¶
The STM32F103C8 DIAGNOSTICS image was made to test Blue Pill boards for hidden extra 64kB Flash plus read chip ID etc, but it’s also a complete working Forth which can do all kinds of cool things.
Blinking a LED is one of the simpler things this image can do.
All you have to do is load the f103-diagnostics-image-blinky.fs file into your Blue Pill and the LED should begin flashing.
From your serial terminal select f103-diagnostics-image-blinky.fs and upload it to the Blue Pill. When the file finishes uploading the LED should begin flashing and after 10 flashes, you will be returned to the terminal prompt.
Some common issues¶
Lockups¶
Symptom: The f103-diagnostics-image-blinky.fs upload only goes as far as a few characters and then appears to lock up. Only pressing the Blue Pill reset button fixes it. See this page.
Note
For the 75Mhz STM32F103C8 DIAGNOSTICS image you are running on your Blue Pill, 100ms EOL delay should be plenty.
LED does not blink¶
Symptom: The program runs and then after 10 seconds it stops and control is returned to the terminal, but the LED doesn’t blink ?
Why? On your board, the LED may not be connected to GPIOC-13. Find out where it is and edit the f103-diagnostics-image-blinky.fs source code or connect a external LED with 510 Ohm Resistor between 3.3V and GPIOC-13.
Source Code¶
1 \ Program Name: f103-diagnostics-image-blinky.fs 2 \ Date: Wed 5 Feb 2020 13:33:55 AEDT 3 \ Copyright 2020 by t.j.porter <terry@tjporter.com.au>, licensed under the GPLV2 4 \ For Mecrisp-Stellaris by Matthias Koch. 5 \ https://sourceforge.net/projects/mecrisp/ 6 \ Board: Blue Pill with LED on PC-13 7 \ Designed to be used with STM32F103C8-DIAGNOSTICS.bin 8 \ https://sourceforge.net/projects/mecrisp-stellaris-folkdoc/files/STM32F103C8-DIAGNOSTICS.bin 9 \ All register names are CMSIS-SVD compliant 10 \ Note: gpio a,b,c,d,e, and uart1 are enabled by Mecrisp-Stellaris Core. 11 \ Standalone: no preloaded support files required 12 \ 13 \ This Program : Blinks the Blue Pill LED on PC-13 14 \ 15 \ The Blue Pill LED anode is connected to +3.3v via a 510R resistor and 16 \ the Cathode to PC-13. 17 \ This INVERTS the normal operation so LED is on at bootup. 18 \ PC-13 = HIGH, LED = OFF 19 \ PC-13 = LOW, LED = ON 20 \ ---------------------------------------------------------------------------\ 21 compiletoram 22 \ compiletoflash 23 24 \ Configure PC-13 as Open Drain, OUTPUT. 25 \ PC13->output \ See STM document RM008 page 172 0 1134 26 \ 23 22 21 20 27 \ CNF13 MODE13 28 \ 0 1 1 1 29 %11 20 lshift GPIOC_CRH bis! \ GPIOC_CRH_MODE13 Output mode: 50MHz 30 %01 22 lshift GPIOC_CRH bis! \ GPIOC_CRH_CNF13 Open Drain 31 32 : led.off %1 13 lshift GPIOC_BSRR bis! ; 33 : led.on %1 13 lshift GPIOC_BRR bis! ; 34 35 : blink 36 10 0 do \ blink 10 times, 1/2 second on, 1/2 second off. 37 led.on 38 500 ms.delay\ use ms.delay Word included in STM32F103C8-DIAGNOSTICS.bin 39 led.off 40 500 ms.delay 41 loop 42 ; 43 44 blink
Fun Stuff¶
Interactivity¶
Now you have your blinky working, try this:
led.on
The LED should turn on ?
led.off
The LED should have turned off ?
This is the INTERACTIVE nature of Forth, instant real world feedback. No need to compile and flash as is so common with programming languages like C, because with Forth you can obtain instant hardware feedback from the terminal.
Register Pretty Printing¶
Now try this:
gpioc.
Note
the period “.” at the end of the command
You should have a list of the CURRENT STATE of EVERY GPIOC REGISTER displayed ?
The idea is to view this in conjunction with the STM32F103 ST Micro Reference Manual RM0008 Rev 20 open on page 171/1134.
GPIOC_ODR¶
Enter this into the terminal:
led.on
The GPIOC OUTPUT DATA REGISTER (ODR) is the one which is telling the LED to light via BIT 13. To view it, enter:
GPIOC_ODR.
Note
the period “.” at the end of the command
It should look like this:
gpioc_odr.
GPIOC_ODR $00000000
3|3|2|2|2|2|2|2|2|2|2|2|1|1|1|1|1|1|1|1|1|1|
1|0|9|8|7|6|5|4|3|2|1|0|9|8|7|6|5|4|3|2|1|0|9|8|7|6|5|4|3|2|1|0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
No bits are HIGH, the LED is ON because BIT 13 is LOW, due to the Open Drain wiring.
Now enter:
led.off
And again enter :
GPIOC_ODR.
See what has changed ? BIT 13 is now HIGH.
gpioc_odr.
GPIOC_ODR $00002000
3|3|2|2|2|2|2|2|2|2|2|2|1|1|1|1|1|1|1|1|1|1|
1|0|9|8|7|6|5|4|3|2|1|0|9|8|7|6|5|4|3|2|1|0|9|8|7|6|5|4|3|2|1|0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
Real Time Views¶
You can view IN REAL TIME the contents of all registers that have support Words loaded into your chip.
To view all the support Words in your chip right now, type the following and you will see that there are about 760 support programs (Words) in your chip ready to be used !
Words4
The STM32F103C8 DIAGNOSTICS image only has a small subset of programs because it had to be smaller than 64kB to fit any Blue Pill chip, but you can get and even make images with EVERY STM32F103 register Word ready to use.