Uploading a sketch

From STM32duino wiki
Jump to: navigation, search

Using Arduino IDE

In Arduino IDE, the first step is to select the appropriate STM32 board from the Tools -> Board menu.

The preferred upload method can be selected in the IDE from Tools -> Upload method.

If you're just starting, you can try the 'blink' sketch. That's a very small sketch and since it makes an LED blink, it's easy to check if it's running or not. You can find it in the menu at File -> Examples -> A_STM32_Examples -> Digital -> Blink.

Currently, there are four supported upload methods:

  • STM32duino bootloader
  • Serial
  • STLink
  • BMP (Black Magic Probe)

Upload via STM32duino bootloader

First, you need to ensure you have the drivers for the Serial and DFU (upload) devices installed -- see Installation for instructions.

Also, your STM32 board needs to have the STM32dunio bootloader -- see Burning the bootloader.

If your board does not yet have a sketch running, it appears as a Maple DFU device. This will be used to upload the first sketch.

Once a sketch is running, the DFU is not present, but the Maple Serial device is. Select its COM port from the Tools -> Port menu.

Run 'Sketch -> Upload' in Arduino IDE. After the compilation output you will see the following:

maple_loader v0.1
Resetting to bootloader via DTR pulse
[Reset via USB Serial Failed! Did you select the right serial port?]
Searching for DFU device [1EAF:0003]...
Assuming the board is in perpetual bootloader mode and continuing to attempt dfu programming...

dfu-util - (C) 2007-2008 by OpenMoko Inc.

This action is accompanied by a beep sound and the on-board LED will start blinking. The the upload tool ('maple_loader') will upload the generated binary file to the board.

Manual reset

If you cannot hear any beep sound nor see the LED of the board blinking, the following message may appear:

Couldn't find the DFU device: [1EAF:0003]

This indicates that resetting the board into bootloader DFU mode was not successful. You should manually reset the board when the first text in red is printed in the IDE output window.

If you don't do this within 3 seconds, the upload will fail, and you will have to repeat the upload procedure. Under Linux the timing may be very sensitive. It seems that you want to release the reset button to enter the fast blink upload mode just before the compilation finishes. It may be useful to use the Arduino IDE 'Sketch -> Verify/Compile' action so that it will compile before trying an upload, because the first compile can be slow, even for something as simple as a blink sketch. Then when you try to upload, it will compile again, but much faster and with more predictable timing to allow you to coordinate the release of the reset button.

Upload via Serial

For this method an additional USB to Serial adapter is needed.

A full description of the upload process is available in the video Roger posted here: [1]

Basically it boils down to these steps.

  • Ensure that the serial adapter is using 3.3 Volt (or else, that the Tx/Tx pins of the target device are 5 Volt tolerant).
  • Make sure you have installed a working driver for the serial adapter.
  • Connect power, ground, and Tx/Rx pins of the serial adapter to your STM32. Adapter Tx goes to STM32 USART1 Rx (PA10), Rx goes to STM32 Tx (PA9).
  • Plug in the serial adapter to USB. Take not of the serial port it's using.
  • In Arduino IDE, select Tools -> Upload method -> Serial and set Tools -> Port -> [your serial port].
  • Place your STM32 in 'native bootloader' mode by changing the BOOT0 pin value and resetting the board.
  • Upload the sketch in Arduino IDE: Sketch -> Upload.

Beware! When uploading in this way, the sketch may be uploaded to the start of the STM32's flash memory. If an STM32duino bootloader is installed there, this may get overwritten.

Upload via ST Link

For this method you have to use a STLink V2 [2] device connected to the PC over USB and to your board via the SWD interface.

For another guide to uploading by ST Link comptatible devices see the Github wiki: [3]

Also, be sure that the necessary driver for Windows is installed and the STLink adapter is updated to the latest firmware (version V2J27S6 or later).

You can use the official ST-Link V2 programmer or one of the many clones.

There are several versions of the cloned ST-Link V2 adapters available, they may break out different numbers of pins, depending on the manufacturer, but all should have at least the pins listed below.

We only need three pins for basic programming and debugging of the STM32FXXX devices, (four if you are powering the board from the programmer) using Serial Debug Wire (SWD).

Connect to following pins of the programmer to the corresponding pins on the STM board:

   3.3V (Optional)  NOTE: Not needed if powering the target board from USB or an external supply.  
                    Check your board schematic to see which pin is best for this. 
                    Pin 1 (and optionally pin 2) of the 20 way JTAG/SWD header, if fitted are designated as VCC 
                    which should be 3.3V on the board, but test with a multimeter if you are in any doubt. 
                    Avoid sending 3.3V to the target board if using an external supply or you may damage the board, programmer, USB port or external supply.  
   GND          -   Pin 4,6,8,10,12,14,16,18,20 of the 20 way header if fitted, or any other available GND pin.
                    NOTE:If your board has a separate analog and digital GND, use the digital GND.

   SWCLK (PA14) -   Pin 9 on the 20 way header if fitted. aka JTCK/SWCLK/PA14
   SWDIO (PA13) -   Pin 7 on the 20 way header if fitted. aka JTMS/SWDIO/PA13 


After a successful build process, the IDE outputs something similar to the following message:

STM32 ST-LINK CLI v2.1.0
STM32 ST-LINK Command Line Interface

ST-LINK SN : [serial_numer_of_the_adapter]
ST-LINK Firmware version : V2J27S6
Connected via SWD.
SWD Frequency = 1800K.
Connection mode : Normal.
Device ID:0x410 
Device flash Size : 64 Kbytes
Device family :STM32F10xx Medium-density

Loading file...
Flash Programming:
  File : C:\Users\[user]\AppData\Local\Temp\buildbf9409c1ae369e3d22510a2824c2116e.tmp\Blink.ino.bin
  Address : 0x08000000
Flash memory programming...
±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±±± 0%
 0%ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ 100%
Flash memory programmed in 0s and 594ms.
Programming Complete.

MCU Reset.

Application started.

If you get the message:

No target connected
Unable to connect to ST-LINK!

This means that the ST-Link V2 adapter cannot communicate with the board. In this case check the hardware wires. Or, as alternative, you can also supply the 3.3V to the board only over the adapter, removing the USB cable, if any. Finally, if none of these helps, you should manually reset the board within 3 second after the upload process begins (red text printed in the output window).

Upload via BMP (Black Magic Probe)

To Do. Details on how to upload a sketch via BMP (Black Magic Probe).


Using Eclipse

It is possible to setup a project under Eclipse using the system files of the Arduino environment.

Arduino with Eclipse

The same upload methods are supported as listed above.

The preferred upload method can be picked up by clicking the current project in the Project Explorer window and selecting via menu: File -> Properties -> Arduino. Acknowledge your selection by clicking "Apply", then "OK".