_images/blinky-bazar.jpg

F0 Discovery Blinkies

A dubious and probably fake rumour claims that Bill Gates once said ‘640 Blinkies should be enough for anyone’ and I have to agree as I have only managed to make 8 Blinkies so far. Six are Forth powered and the seventh is written in Assembly language and just 28 bytes long. I’ve included a eighth ‘C’ Language blinky for completness.

Please step into the Blinky Bazar, we have Blinky programs to suit your STM32 Discovery Board, just help yourself!

_images/stm32-disco-sml.jpg

What is a blinky ?

In the programming world, a beginners first program usually prints ‘Hello World’.

In the embedded world our first Forth program usually blinks a LED, and we call this a ‘Blinky’.

This page is a collection of Blinkies that run on the STM32 Discovery Board (or any STM32F Cortex M0 running Mecrisp-Stellaris Forth with LED’s on GPIO’s PC-8 and PC-9).

For the technical reference to the MCU, see here.

Starting with simple Blinkies and proceeding to the more complex and useful ones, this article was created in the hope of demonstrating some of the versatility of the Forth Programming Language.

I don’t go into much detail about Forth or the STM32F0 MCU as these are massive areas, and the study of each one alone is worthy of considerable amounts of your time, but this is as good a place as any to start, so let’s get going …

Note

Wondering how to install Mecrisp-Stellaris Forth ?

Note

If entering any programs into the terminal by hand, don’t enter lines beginning with blackslash as they’re only comments for humans and Forth ignores them anyway.

Blinky 1

The simplest Blinky of all

blinky-1.fs

  • Requires no other files, Just upload it, or type the code by hand straight into your Forth Terminal.

  • All it does is blink, … forever.

  • Disadvantage: This Blinky takes up 100% of the MCU time, it won’t even answer the terminal, which will be frozen.

Blinky 2

Introducing Register Names and Memory Mapping

blinky-2.fs

  • Requires no other files, Just upload it, (or enter it by hand)

  • Introduces the concept of Register Names and Memory Mapping, to simplify Source File content.

  • To find out more about STM32 Register names, what they do etc, please refer to the STM32F Cortex F0 manual for more information.

  • GPIOC_MODER selects the GPIOC MODES such as input, output, analog etc. In this case, it’s Output Mode.

  • GPIOC_BSRR sets or resets GPIOC BIT or BITs, in this clase BIT 9 is being SET

  • GPIOC_BRR resets (clears) GPIOC BIT or BITs, in this clase BIT 9 is being RESET

  • Blinky 2 is otherwise identical in operation to Blinky 1.

Blinky 3

Introducing #require statements to preload vital library files

blinky-3.fs blinky-3-register-memory-map.fs

Note

This example introduces the very important concept of preloading Forth files via #require statements. This method will be used in the following Blinkies which are more complex and may have many more Memory Mapped Registers.

  • One excellent Serial Terminal which supports #require statements is the e4thcom Serial Terminal.

  • Why go to this trouble for a list of only four Register I hear you ask ?

  • What if that list was actually 500 lines long or even 5000 lines ? If it was, then one would have to scroll down pages and pages of lists of Register Memory Mappings just to get to the actual program code.

  • Blinky 3 is otherwise identical in operation to Blinky 1 and Blinky 2.

Blinky 4

Cooperative Multitasking.

blinky-4.fs blinky-3-register-memory-map.fs multitask.fs

  • Blinky 4 uses Cooperative Multitasking, which means it blinks the LED and talks to you on the terminal at the same time ! Ok, it’s not exactly the same time, but it’s so fast, you won’t notice.

  • Blinky 4 will also use the blinky-3-register-memory-map.fs.

  • The same code to blink the LED is used as in Blinkies 1, 2, and 3, but this time it’s controlled by a Cooperative Multitasker. Try it, enter “reset” and the board will be reset, and the LED will stop blinking (of course you’ll have to load the program again, as reset clears the memory.

  • Advantages: It’s easy to code a number of separate tasks all doing their own thing and the terminal is fully active while the LED blinks.

  • Disadvantages: Someone once said that cooperative multitasking is like a group of kids passing a ball to each other, it works fine until one of the kids decides to keep the ball all to himself.

Blinky 5

Interrupt Driven

blinky-5.fs blinky-5-register-memory-map.fs

  • Blinky 5 blinks the LED exactly once per second, controlled by the MCU Systick Interrupt.

  • Accurate and simple, the MCU isn’t spending most of its time counting loops like Blinkies, 1,2,3, and 4

  • Minimal MCU time is used, one interrupt means one change of state for the LED, leaving lots of time for other jobs.

  • The terminal is fully active while the LED blinks

Note

The System Interrupt is already enabled by Mecrisp-Stellaris.

Blinky 6

Cooperative Multitasking AND Interrupts

blinky-6.fs blinky-5-register-memory-map.fs multitask.fs

  • Blinky 5 blinks the LED exactly once per second, controlled by the MCU Systick Interrupt.

  • Accurate and fairly simple, the MCU isn’t spending most of its time counting loops like Blinkies, 1,2,3, and 4

  • Minimal MCU time is used, one interrupt means one change of state for the LED, leaving lots of time for other jobs.

  • The terminal is fully active while the LED blinks

  • Advantages: It’s easy to code a number of separate tasks all doing their own thing.

  • Disadvantages: Someone once said that cooperative multitasking is like a group of kids passing a ball to each other, it works fine until one of the kids decides to keep the ball all to himself.

Blinky 7

A pure Assembly language Blinky. It’s a totally minimal, 28 Byte standalone Blinky binary.

Written by Matthias Koch, Senior Assembly Wizzard, the creator of Mecrisp-Stellaris, it runs on a STM32F0 Discovery Board flashing the blue LED.

This is as small as we can make a Blinky binary for STM32F0, perhaps you can desgn a smaller one ? …. let me know and I’ll publish it here.

Download Tinyblinky Tarball Here. It has everything you need including a binary image ready to load.

  • All it will ever do is blink the selected LED. Default is blue, but the green led can be used, it’s a option in the tinyblinky.s source file.

  • The program uses a total of 28 bytes of Flash, which is way less than the minimum 19K bytes for Mecrisp-Stellaris alone.

  • You can’t talk to it over a serial terminal, it’s not interactive like Mecrisp-Stellaris.

  • The rest of the 64K Bytes of Flash in the MCU is wasted.

Blinky 8

Written in C, DOWNLOAD: stm32f0-C-blinky.zip complete with a Makefile, based on a helpful article by Frank Duignan.

MSP430 Blinkies

Exercises For the Reader

To test your Forth Blinky skills …

  1. Change the blinking LED COLOR ?

  2. Change the rate of the blink ?

  3. Add a second task so that both LED’s blink at different intervals

  4. Add a third task, so that both LED’s blink at different intervals and the terminal prints “BEEP” (it’s a virtual beeper)