Coding 32-bit STM32 chips with Arduino IDE
Coding low-cost 32-bit chips can be done using the Arduino software. Darren Yates reveals the tools and know-how you’ll need.
Last month, we introduced the idea of making the jump from 8-bit Arduino boards to building your projects using more powerful 32-bit ARM Cortex M-series chips, similar to those powering wearable devices like Fitbit fitness bands. Now ideally, you’d use dedicated integrated development environment (IDE) tools such as System Workbench for STM32 ( www.openstm32.org), but you can also code certain Cortex M-series chips using the standard free (and simpler) Arduino IDE. We recommend this only for those with reasonable Arduino experience.
ST-LINK V2 USB ADAPTER
Unlike the Arduino boards, most generic Cortex M-series boards don’t have built-in USB bootloader software to allow the board to be recognised by your PC via its USB port. Sure, the port is there on the board, but without that bootloader code, Windows won’t recognise it. What you need is a USB-to-serial adapter and given the majority of low-cost Cortex M-series boards on eBay and elsewhere use the popular STM32 series from chipmaker STMicroelectronics, we’ll use its ST-LINK v2 USB-to-serial adapter. You can pick one up for around $5 on eBay. It’s essentially a USB Type-A plug on one end and a series of header pins on the other, providing the USB link between your PC and the STM32 chip to allow the Arduino IDE to code it. You don’t need to worry about adapter device drivers — Windows will find the driver software online when you plug it in.
Now there are two STM32 chip families commonly found on most boards — the 72MHz STM32F103 series based on the ARM Cortex M3 or the more recent 48MHz STM32F030 built on the ARM Cortex M0. Boards using either of these sell for under $5 on eBay, but we recommend STM32F103-based boards.
ARDUINO 32-BIT LIBRARIES
Before you go any further, you need to update your Arduino IDE with the 32-bit ARM core libraries ( arduino.cc). You write your code in the relatively high-level language known as ‘Wiring’, which is a loose C++/Java translation. The Arduino IDE converts or ‘compiles’ this code into machine code the STM32 chips can
understand — and for that, you need the ARM core libraries.
Launch the IDE and from the menu, select ‘Tools > Board > Boards Manager’ and look for the ‘Arduino SAM Boards (32-bits ARM Cortex-M3) entry. If not already installed, click on the Install button and download the libraries. Shut down the Arduino IDE when you’re done. We also need the Arduino_STM32 hardware files created by Australian software engineer Roger Clark. You’ll find these at GitHub via tinyurl.com/o6swzz3. Click on the green ‘Clone or download’ button, select the ‘Download ZIP’ link, grab the file, unzip it and copy the STM32_Arduino-Master folder to the /documents/Arduino/hardware folder on your PC (if the path doesn’t exist, make it).
HOOKING UP THE ST-LINK ADAPTER
The ST-LINK adapter only requires four wires to connect to most STM32 boards — Vcc (3.3V), DCLK, DIO and GND. Vcc is the positive supply rail (it must be 3.3V), GND is ground or 0V, while DCLK is ‘digital clock’ and DIO is the digital input/output pin. Now look on the ST-LINK adapter itself, and on the front, you’ll see a printed legend showing the pins numbered and their functions. The open notch in the rear bracket is your guide to pin orientation. You’ll notice there are multiple GND and 3.3V pins — you can use any of these. But you will need Pin 2 (SWCLK) and Pin 4 (SWDIO). When you buy the ST-LINK adapter, you should also receive a set of jumper wires — connect these appropriately.
A word of warning here: these STM32 boards come with a tiny microUSB port on the other end. Some will have four soldered anchor points, but others only two — you don’t need it now, but be very careful when using this port. It doesn’t take much to rip the two anchor-point connector right off the board.
Now with the code libraries installed, the ST-LINK connected up to the STM32 board and plugged into a USB port, we’re ready to code something. And by law, you must begin with the ‘Hello World’ of microcontroller projects — flashing the built-in test LED. Reboot your Arduino IDE and, from the menu, select ‘File > Examples > 01. Basics’ and ‘Blink’. When the code loads, you’ll see references to ‘13’ in the pinMode and digitalWrite commands. You must change these to ‘PC13’, otherwise, it won’t work.
But before you compile the code, you need to set the IDE’s link with the STLink adapter. From the Arduino IDE menu, select ‘Tools > Board’ and if you have one of the boards we’ve shown, scroll down to ‘STM32 boards (STM32duino.com)’ and select ‘Generic STM32F103C Series’. Set the ‘Variant’ option to ‘STM32F103C8 (64K flash)’ and the ‘Upload method’ to ‘STLink’. When you’re done, compile the code (use the green tick icon at top-left just under the text menu). Don’t worry about any warnings in the status area at the bottom, unless they cause the compile to fail. If you get the ‘Done Compiling’ message, you’re good to go. At this point, the ST-LINK V2 USB adapter should have a pin-point blue LED lit. If you now compile and flash the code to the STM32 board using the green ‘right-arrow’ icon on the Arduino IDE, the blue LED should start flashing blue-red while the code is uploading to the board. As soon as it completes, you’ll get ‘MCU reset’ and ‘application started’ in the status area. Your STM32 board’s test LED should now start blinking on and off. Try changing the delay periods (they’re in milliseconds) and see what effect it has. If you’ve got this far, congrats — you’ve just coded your first 32-bit microcontroller app.
POWERING THE STM32 BOARD
Once your code has been flashed to the board, you no longer need the ST-LINK adapter. Instead, you can now plug a cable into the microUSB port and power it from any suitable USB power source. You won’t be able to code the board directly through the USB port, but it will provide power to run your project.
These STM32 boards aren’t officially supported by the Arduino forums, so head to stm32arduino.com for more details. They can be a lower-cost way to start 32-bit development without the investment of an Arduino Zero or 101.
These small STM32 boards are designed to use with breadboards.
You need the STM32_ Arduino hardware files from this GitHub page.
You can code the ‘Blink’ app to your STM32 board with the STLink adapter.
Generic STM32 boards use four wires to connect to the ST-Link adapter.
Set the Arduino IDE board settings to those shown before coding. The ST-LINK V2 USB adapter hooks up your PC to the STM32 board.
Windows will find the STLink drivers online when you plug in the adapter.