Set up & program the STM32F103C8 series microcontroller via USB when UART's are not available

The STM32F103C8 is a powerful microcontroller from ST Microelectronics. It is a low power high performance chip with a ARM®Cortex®-M3 32-bit RISC core operating at a 72 MHz suitable for a wide range of applications such as motor drives, application control, medical and handheld equipment, PC and gaming peripherals, GPS platforms, industrial applications, PLCs, inverters, printers, scanners, alarm systems, video intercoms, and HVACs and whatever else you can think of. Some features of this chip are:

  • rus on 2.0V - 3.6V.
  • high-speed embedded 64 Kbytes of Flash memory and 20k of SRAM.
  • 37 GPIO's.
  • CANbus, I²C, IrDA, LINbus, SPI, UART/USART, USB 2.0.
  • interfaces with DMA, Motor Control PWM, PDR, POR, PVD, PWM, Temp Sensor, WDT and more.
  • two 10x12b DAC's.
  • three 16-bit Timers, one 16-bit PWM Timer.

Now, ST has a great IDE envirornment called STM32CubeMX. i especially like the graphical set up and configuration of all pins, timers, etc. It's fully integrated with their products, but the install is quite large for a complete development envirornment, as it is for mostly all maufacturers. If i were programming chips on a daily basis, or within large projects, i would definately want to use these applications all the time.

But i'm a tinkerer & hobbyist, and i am comfortable with and love using the Arduino IDE for programming and uploading code. Programming the Teensy's, Pro-Micro's and Nano's is fast and convienient. So when i first used the Arduino environment to program a STM8 chip, naturally, i wanted to do the same for the STM32 line. Unfortunately, this dev-board does not work out of the box with your PC's USB port and allow you to get started immediately. Unlike most of the other chips which may have in-chip support or a dedicated USB chip for interfacing - there is no bootloader pre-loaded here.

There are 4 ways to basically program this chip

  • 1) Serial via UART pins A9/A10 using any USB to serial converter which breaks out 5V, GND, RX and TX.

  • 2) ST-Link SWIM/SWDIO (Separate Hardware Programmer from ST or you can get an inexpensive clone).

  • 3) USB port connector via on chip bootloader.

  • 4) J-Link/J-TAG (can be done, but you have to use one of the above methods first).

When i received my dev-board, it was pre-loaded by the distributor with a led blinky program. The serial UART's were either disabled or not configured on the chip and thus unavailable. So option 1, connecting up a USB serial converter to the UART A9/A10 pins and using the ST Flash Loader Demonstrator application from ST as described all over the net did not work for me. I did plug in the USB connecter to check, but it was not recognized. That quickly removed options 3 and 4 too!

i had a ST-Link Clone programmer though. So option 2 it was. It was actually quite easy to set up and install a bootloader using this method.

First you have to install the Arduino IDE software. It's a quick download and install.

Next from within the Arduino application, navigate to 'File' -> 'Preferences'. You will need to tell the IDE that you want to work with STM32 types of dev-boards. Do this by entering the Dan Drown .json path into the 'Additional Boards Manager URL's' field:

     Enter ->
Then select 'Ok'. If you have more than one .json, comma separate them.

Next select 'Tools'' -> ''Board'' -> ''Boards Manager''. Look way towards the end of the list for "'STM32F1xx/GD32F1xx boards'" then click 'Install'. Don't worry about the version, leave it as default. This should install everything needed to work with these dev-boards in the Arduino IDE and also the drivers for the 'Maple Mini' which is needed to recognize the STM32F103C8 after you flash the USB bootloader.

Next you need to go to the ST Microelectronic's website and download the STM32 ST-LINK Utility which will be used to flash the bootloader. You will have to enter a valid email address to get the download link. Not to worry, ST does not spam you, plus i did not mind as i want to be updated about new things. Once you have the .zip, extract and install it.

It is now time to connect up the dev-board to the ST-LInk Programmer. On the dev-board there are 4 distinct heaader pins at the top/edge. They are labled GND, DCLK, DIO, and 3V3. You will connect these to your ST-LINK along the bottom row pins (with Logo & writing facing you as seen above) pin 2-SWCLK, pin 4-SWDIO, pin 6-GND and pin 8-3.3V to the corresponding pins on the board. You will also need to move the BOOT 0 yellow jumper over to enter program mode (See picture below).

You can mount the dev-board to a breadboard or if you have wire jumpers, just directly connect those to the dev-board pins(you might have to solder some headers). Your ST-Link probably came with wire jumpers anyway. Go ahead and connect it up and plug you ST-Link into your PC. It should be recognized in DeviceMgr. If not, you can update to the latest driver through the ST-Link Utility.

Next, go over to Robert Clark's GitHub site and get the generic_boot20_pc13.bin bootloader image for this chip. I used this one because my board has the LED on pin 13. If your boards LED has a different location, then grab the correct one.

Now, once you have the bootloader image. Open the STM32 ST-LINK Utility installed earlier. Choose 'File' -> 'Open File', and select your .bin image file. Then choose the 'Connect to the target", you should see that the device's memory was read. If you don't get any errors, then select the icon 'Program verfiy'. This will pop up a new window. The application can not automatically reboot this dev-board into operating mode as we already moved the yellow jumper on boot 0, so i unselected 'Reset after programming'. You may then select 'Start'.