Category: STM32 ARM CORTEX M3


 

In the previous post we explored the ways of uploading code on to STM32 using UART1 & Serial Wire Interface.

Its also possible to upload code through USB port of STM32. By default Bluepill modules are not shipped with boot loader. To enable USB uploading we need to flash a bootloader.

This post is related to USB uploading on to an Arduino core Bluepill.

Firmware (.bin file) for bootloader can be flashed only through UART1 of STM32

USB-TTL module built on CP2102 is used for this purpose.

Connection details as below :

PA9 Tx   to Rx of USB-TTL

PA10 Rx  to  Tx of USB-TTL

5v to 5v , GND to GND

 

IMG_20181109_120316

Plug in the USB-TTL module to PC after installing the CP2102 driver

https://www.silabs.com/products/development-tools/software/usb-to-uart-bridge-vcp-drivers

A port will be allotted to the module.

On STM32 Bluepill shift the BOOT 0 jumper to 1 position & press Reset button.

Image 1

Before proceeding , collect the following Software tool , driver & .bin file

Image 2

 

https://github.com/rogerclarkmelbourne

 

https://github.com/rogerclarkmelbourne/Arduino_STM32/archive/794d078de080054ea39095f8980bdcc2de9f3020.zip

Firmware .bin file can be downloaded from Roger Clark’s github. Ensure to download the bin file with suffix _pc13,bin  ,  as the built in LED of our module is mapped to PC13.

Image 3

Flasher tool can be downloaded from :

https://www.st.com/en/development-tools/flasher-stm32.html

 

Image 5

Install the Flasher tool

 

Image 6

Note the install location of the tool & create a shortcut manually.

 

Image 4

Start the Flash Loader software.

The UART port is automatically detected .

Image 5

Click next.

If you get an Error saying No Response from Target , ensure that BOOT 0 jumper is placed on 1 position & start the flash loader again.

 

Image 2

 

Image 6

Now the Target is readable. Click Next.

 

Image 7

Target Device is automatically detected.Click Next.

Image 8

 

Select Download to Device & browse to location of .bin file.

Image 10

 

Now the   boot loader is installed successfully.

Image 10

 

Now replace the BOOT0 jumper back to 0 position & then remove

USB-TTL module.With power on position , the Boot  0 jumper must be set back to 0.

Connect the usb of STM32 to that of PC.

Image 14

Open Device manager.

Under Ports you may see an exclamation mark or just a USB serial device.

Related driver should be installed , so that it shows Maple Serial COM port.

You can download the driver here :

 https://github.com/rogerclarkmelbourne/Arduino_STM32/archive/794d078de080054ea39095f8980bdcc2de9f3020.zip

Just Double click the install_drivers batch file .

bp1

Now you can see Maple Serial COM port under Device Manager.

Image 11

Open the Arduino IDE.

Select Board as Generic STM32F103C Series.

Select Upload method as STM32duino bootloader.

 

Image 12

 

Select the Maple Port

Image 13

 

Now upload the blink sketch ( with LED_BUILTIN defined as PC13)

There is no need to shift BOOT0 jumper to upload code.Directly through USB you can do the upload.

boot1

With some PCs you may get upload done , but the USB port resets before starting to function with the newly uploaded code.

 

Image 14

This is caused by the faulty R10 value resistor seen back to the board.

Most PC USBs accept this pull up value.But sometimes you get the Reset error.

 

Image 16

 

The Blue Pill  has  design fault as it uses a 10k USB pull up resistor, but the USB spec requires 1.5k

You can remove the smd resistor R10 & replace with a 1.5k resistor.

BP2

Or simply solder a 1.8k resistor between PA12 & 3.3V , if you cannot handle SMD component.

blog_image

 

The STM32 F1-series was the first group of STM32 microcontrollers based on the

ARM Cortex-M3 core and considered their mainstream ARM microcontrollers. The F1-series has evolved over time by increasing CPU speed, size of internal memory, variety of peripherals.

There are five F1 lines: Connectivity (STM32F105/107), Performance (STM32F103), USB Access (STM32F102), Access (STM32F101), Value (STM32F100).

In this post we shall explore the ways to implement Arduino core on to STM32 so that programming can be done through Arduino IDE itself.You can enjoy the speed of 32 bit microcontroller with ease of Arduino IDE programming.

STM32 is a 72 MHz processor. The full part number STM32F103C8T6  decodes to

C8 = 128 KB flash, 20 KB RAM, 48-pin LQFP:

STM32 F1xx development boards are available in wide range.Here we shall use a simple board which is also called BLUEPILL . A very efficient & economy module easily available.

 

stm32_ok2

 

Programming can be done in many ways.This post is related to using Arduino core.

As  seen in above picture , STM32 pill has 3 UARTs of which UART1 (PA9, PA10) is used to upload code.For this you need a USB-TTL module .Although the STM32 is 3.3v device many GPIO pins  (shown in yellow underline in above picture) are 5V tolerant. So you can use this USB-TTL *CP2102) module without any level shifter.

The other way to upload code is through SINGLE WIRE INTERFACE ModuleInterface (SWIM) .This uses only 2 wires SWCLK, SWDIO apart from power pins .You can see this as 4 separate header pins to the right end of module.You need a ST-LINK dongle or device to connect with the SWIM.

The ST-LINK/V2 is an in-circuit debugger/programmer for the STM8 and STM32 microcontrollers. The single wire interface module (SWIM)  interfaces facilitate the communication with any STM8 or STM32 microcontroller operating on an application board.

stlinkUsb_ttl

 

The bare STM32F103 board only comes with a default USART boot loader. Even though there is a USB port on the board, you cannot use it to program it because it does not have the relevant bootloader.

In the next post we shall see how to upload using USB.

For now connect the USB-TTL module to STM32 .

PA9  TX    —->   Rx of USB-TTL

PA10  RX   —->   Tx of USB-TTL

5V   —>  5V

GND  —>  GND

Plug in the USB-TTL module to USB of PC. CP2102 device driver is required so that PC allots a port number to the module.

 

IMG_20181109_120318

 

Image 1

You can see 2 yellow jumpers on board.The bottom one is BOOT 1 & the top one is BOOT 0.

For programming you need to shift the BOOT 0 jumper to make High as shown in photo below.

 

Image 2

 

The setting of Jumpers specify  the source of code for the STM32.

In the default state of both being 0, the micro-controller uses its own flash memory bootloader (factory setting is none , there is no bootloader in fresh boards)

To program the micro using USART, you need to set BOOT0 as 1 and leave BOOT1 to 0. Move the first jumper and leave the second one.

 

boot

 

Open the Arduino IDE .

Go to   File –> Preferences

 

Image 3

 

At the bottom of the preferences screen , copy & paste the following link

 

http://dan.drown.org/stm32duino/package_STM32duino_index.json

 

If you have some other link already here ( for e.g you might have installed for NodeMCU) , put a comma after the existing one & then paste the link

Image 4

Click OK.

Now under Tools —> Boards  select Boards Manager

Internet connection is required  to install board support chain link.

Type in STM32F1 & select STM32F1xx boards by stm32duino.

 

Image 6

Select the latest version & click install.

It takes some time to get installed.

Image 7

Now reopen the ARDUINO IDE.

Under Tools make the following settings:

Board : Generic STM32F103C Series

Variant : 20k ram 64k flash

Very important is Upload method which is SERIAL

Port : port no. alloted to USB-TTL

Progemmer : USBasp

Image 15

 

Open the Blink sketch

Add this before void setup

#define LED_BUILTIN PC13

Save the file.

Image 9

 

As seen in picture below , the built in LED on STM32 bluepill is mapped to PC13

Image 10

 

Click on Upload

Image 13

 

 

Image 3

 

Once the code is uploaded you can see the built in LED blinking accordingly.Now you can reset the BOOT 0 jumper back to 0 position & press Reset to start the freshly uploaded code.

For next upload you need to set the BOOT 0 to position 1 & press Reset button.

Let us see the second method to upload code through SWIM.

For this you need a ST LINK V2 dongle as shown in picture below :

 

Image 2

 

 

stlink

The pin details of STLINK is printed on the module itself.

Pin number starts from top , as shown below

stlink_10pin

 

Connect STLINK to STM32 as below :

Pin2 SWCLK   —>  DCLK of STM32

Pin4 SWDIO  —>  DIO of STM32

Pin6 GND  –>  GND

Pin 8   3.3V –>  3,3V

 

IMG_20181109_114834

Before connecting ST LINK to PC , download & install the driver from below link

https://www.st.com/en/development-tools/stsw-link009.html

This USB driver (STSW-LINK009) is for ST-LINK/V2 and ST-LINK/V2-1 boards and derivatives (STM32 discovery boards, STM32 evaluation boards, STM32 Nucleo boards)

 

Image 11

Once the drivers are installed you see under Device Manager – > Universal Serial Bus Device

STM32 ST Link

 

dmanager

 

Open the Arduino IDE

Select board as STM32F103C Series

Upload method to be selected as ST Link

Port will be greyed out ,as No selection of port required

 

Image 11

 

Note that there is no need to shift Jumper in this method of uploading.

 

Image 13

Just click on upload to see the inbuilt LED blinking.

VIDEO DEMO:

blog_image