Cod­ing 32-bit STM32 chips with Ar­duino IDE

Cod­ing low-cost 32-bit chips can be done us­ing the Ar­duino soft­ware. Dar­ren Yates re­veals the tools and know-how you’ll need.

APC Australia - - Contents -

Last month, we in­tro­duced the idea of mak­ing the jump from 8-bit Ar­duino boards to build­ing your projects us­ing more pow­er­ful 32-bit ARM Cor­tex M-se­ries chips, sim­i­lar to those pow­er­ing wear­able de­vices like Fit­bit fit­ness bands. Now ide­ally, you’d use ded­i­cated in­te­grated de­vel­op­ment en­vi­ron­ment (IDE) tools such as Sys­tem Work­bench for STM32 (­, but you can also code cer­tain Cor­tex M-se­ries chips us­ing the stan­dard free (and sim­pler) Ar­duino IDE. We rec­om­mend this only for those with rea­son­able Ar­duino ex­pe­ri­ence.


Un­like the Ar­duino boards, most generic Cor­tex M-se­ries boards don’t have built-in USB boot­loader soft­ware to al­low the board to be recog­nised by your PC via its USB port. Sure, the port is there on the board, but with­out that boot­loader code, Win­dows won’t recog­nise it. What you need is a USB-to-se­rial adapter and given the ma­jor­ity of low-cost Cor­tex M-se­ries boards on eBay and else­where use the pop­u­lar STM32 se­ries from chip­maker STMi­cro­elec­tron­ics, we’ll use its ST-LINK v2 USB-to-se­rial adapter. You can pick one up for around $5 on eBay. It’s es­sen­tially a USB Type-A plug on one end and a se­ries of header pins on the other, pro­vid­ing the USB link be­tween your PC and the STM32 chip to al­low the Ar­duino IDE to code it. You don’t need to worry about adapter de­vice driv­ers — Win­dows will find the driver soft­ware on­line when you plug it in.

Now there are two STM32 chip fam­i­lies com­monly found on most boards ­— the 72MHz STM32F103 se­ries based on the ARM Cor­tex M3 or the more re­cent 48MHz STM32F030 built on the ARM Cor­tex M0. Boards us­ing ei­ther of these sell for un­der $5 on eBay, but we rec­om­mend STM32F103-based boards.


Be­fore you go any fur­ther, you need to up­date your Ar­duino IDE with the 32-bit ARM core li­braries ( ar­ You write your code in the rel­a­tively high-level lan­guage known as ‘Wiring’, which is a loose C++/Java trans­la­tion. The Ar­duino IDE con­verts or ‘com­piles’ this code into ma­chine code the STM32 chips can

un­der­stand — and for that, you need the ARM core li­braries.

Launch the IDE and from the menu, se­lect ‘Tools > Board > Boards Man­ager’ and look for the ‘Ar­duino SAM Boards (32-bits ARM Cor­tex-M3) en­try. If not al­ready in­stalled, click on the In­stall but­ton and download the li­braries. Shut down the Ar­duino IDE when you’re done. We also need the Ar­duino_STM32 hard­ware files cre­ated by Aus­tralian soft­ware en­gi­neer Roger Clark. You’ll find these at GitHub via Click on the green ‘Clone or download’ but­ton, se­lect the ‘Download ZIP’ link, grab the file, un­zip it and copy the STM32_Ar­duino-Master folder to the /doc­u­ments/Ar­duino/hard­ware folder on your PC (if the path doesn’t ex­ist, make it).


The ST-LINK adapter only re­quires four wires to con­nect to most STM32 boards — Vcc (3.3V), DCLK, DIO and GND. Vcc is the pos­i­tive sup­ply rail (it must be 3.3V), GND is ground or 0V, while DCLK is ‘dig­i­tal clock’ and DIO is the dig­i­tal in­put/out­put pin. Now look on the ST-LINK adapter it­self, and on the front, you’ll see a printed le­gend show­ing the pins num­bered and their func­tions. The open notch in the rear bracket is your guide to pin ori­en­ta­tion. You’ll no­tice there are mul­ti­ple 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 re­ceive a set of jumper wires — con­nect these ap­pro­pri­ately.

A word of warn­ing here: these STM32 boards come with a tiny mi­croUSB port on the other end. Some will have four sol­dered an­chor points, but oth­ers only two — you don’t need it now, but be very care­ful when us­ing this port. It doesn’t take much to rip the two an­chor-point con­nec­tor right off the board.


Now with the code li­braries in­stalled, the ST-LINK con­nected up to the STM32 board and plugged into a USB port, we’re ready to code some­thing. And by law, you must be­gin with the ‘Hello World’ of mi­cro­con­troller projects — flash­ing the built-in test LED. Re­boot your Ar­duino IDE and, from the menu, se­lect ‘File > Ex­am­ples > 01. Ba­sics’ and ‘Blink’. When the code loads, you’ll see ref­er­ences to ‘13’ in the pinMode and dig­i­talWrite com­mands. You must change these to ‘PC13’, oth­er­wise, it won’t work.

But be­fore you com­pile the code, you need to set the IDE’s link with the STLink adapter. From the Ar­duino IDE menu, se­lect ‘Tools > Board’ and if you have one of the boards we’ve shown, scroll down to ‘STM32 boards (STM32­’ and se­lect ‘Generic STM32F103C Se­ries’. Set the ‘Vari­ant’ op­tion to ‘STM32F103C­8 (64K flash)’ and the ‘Up­load method’ to ‘STLink’. When you’re done, com­pile the code (use the green tick icon at top-left just un­der the text menu). Don’t worry about any warn­ings in the sta­tus area at the bottom, un­less they cause the com­pile to fail. If you get the ‘Done Com­pil­ing’ mes­sage, 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 com­pile and flash the code to the STM32 board us­ing the green ‘right-ar­row’ icon on the Ar­duino IDE, the blue LED should start flash­ing blue-red while the code is upload­ing to the board. As soon as it com­pletes, you’ll get ‘MCU re­set’ and ‘ap­pli­ca­tion started’ in the sta­tus area. Your STM32 board’s test LED should now start blink­ing on and off. Try chang­ing the de­lay pe­ri­ods (they’re in mil­lisec­onds) and see what ef­fect it has. If you’ve got this far, con­grats — you’ve just coded your first 32-bit mi­cro­con­troller app.


Once your code has been flashed to the board, you no longer need the ST-LINK adapter. In­stead, you can now plug a ca­ble into the mi­croUSB port and power it from any suit­able USB power source. You won’t be able to code the board di­rectly through the USB port, but it will pro­vide power to run your project.

These STM32 boards aren’t of­fi­cially sup­ported by the Ar­duino fo­rums, so head to stm32ar­ for more de­tails. They can be a lower-cost way to start 32-bit de­vel­op­ment with­out the in­vest­ment of an Ar­duino Zero or 101.

These small STM32 boards are de­signed to use with bread­boards.

You need the STM32_ Ar­duino hard­ware 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 con­nect to the ST-Link adapter.

Set the Ar­duino IDE board set­tings to those shown be­fore cod­ing. The ST-LINK V2 USB adapter hooks up your PC to the STM32 board.

Win­dows will find the STLink driv­ers on­line when you plug in the adapter.

Newspapers in English

Newspapers from Australia

© PressReader. All rights reserved.