STM32F103C8T6 Blue Pill Board with Arduino IDE on Linux

 USB to UART Wiring

Update: it looks like it was working because I had support for the Arduino Due (ARM Cortex M3) already installed, so I added a step to install this below.

As we have seen in a previous post, the ST32F103C8T6 “Blue Pill” or “Red Pill” boards are dirty cheap but extremely powerful MCU hardware boards. Compared to a marginally cheaper (- $0.30) Arduino Pro Mini board featuring an ATMega328 MCU, you get:

  • 72 MHz 32-bit ARM CPU vs. 16 MHz 8-bit AVR CPU
  • 64 kB / 128 kB Flash memory vs. 32 kB
  • 20 kB SRAM memory vs. 2 kB
  • unfortunately, no EEPROM memory vs. 1 kB
  • In the package used in this board, up to 35 GPIOs, most of these are 5 V tolerant, vs. 23 GPIOs
  • 7x timers vs. 3, 2 ADCs vs. 1, 6x PWM channels, 2x SPI,, 3x USART vs. 1, 2x I2C vs. 1,
  • additional USB device and CAN interfaces

However, having a nice hardware platform is useless if it is not backed by a good software environment. And despite the sophisticated IDEs and tools available for the STM32, the ATMega328 is still very attractive because of its free and easy to grasp Arduino IDE and numerous libraries, that seriously lowers the required knowledge to be able to tackle with the MCU low-level programming for non-specialists.

Thus, using the same Arduino tools used on the Atmel AVR platform on the STM32 may seem like a good idea… Doing this on a Linux computer provides some additional challenges, but nothing really overwhelming, though.

The first consideration is that there are several ways to program an STM32F103C8T6 MCU:

  1. Just like all modern MCUs today, there is a way to program (and most of the time, debug) the chip at the lowest level using a JTAG probe. For the particular STM32 case, you can use an STLink, a JLink or a Black Magic Probe that all use the standard ARM SWD programming / debugging interface, readily available on  a separate 4-pin header on the Blue/Red Pill board short edge facing the micro USB connector
  2. Just like for the ATMega328, we can use an Arduino bootloader stored into Flash memory, here the equivalent is the STM32duino bootloader
  3. Serial (UART): unlike the ATMega328, the STM32 features an UART bootloader in ROM, i.e. available in an empty chip straight out of the production line

As we try to lower the prerequisite to program these Blue / Red Pill boards, the third method has the main advantage to not require any additional hardware, except for a standard serial UART to USB cable (as I suppose that your host PC does not provide a native RS232 UART DB9 or DB25 connector for a while…). This is the method that we will use here.

Here is the step-by-step procedure:

Requirements:

  • an STM32F103C8T6 “Blue Pill” or “Red Pill”module (ARM32 Cortex-M3, 72 Mhz, 64K flash, 20K SRAM, 33 I/O pins, 3.3V), available here for $1.52 (expect a 1 month delay to EEC with standard shipment method)
  • a soldering iron (in order to solder the large 2.54 mm pitch male headers provided with the board)
  • a Serial-to-USB cable, I used my old faithful FTDI TTL-232R-3V3 cable (datasheet here), but any know working cable or adapter will do
  • 4x Male / Female short “Dupont” wires

Step #1: Wiring

Wire the STM32 module and the Serial-to-USB cable as shown below:

 

STM32F103C8T6 FTDI TTL3.3V Cable

If you only have +3.3V power supply available, connect it to the “3.3” pin instead of “5V”.

Connect the cable to one of your free USB port on the PC. In order to find out which Linux device is associated with the cable, run the “dmesg” command in a terminal and search for the assigned device name near the last lines. You should see something like this:

$ dmesg
...
[19086.232386] ftdi_sio 3-4:1.0: FTDI USB Serial Device converter detected
[19086.232488] usb 3-4: Detected FT232RL
[19086.232858] usb 3-4: FTDI USB Serial Device converter now attached to ttyUSB0

Step #2: Setup the Arduino IDE

As the Arduino IDE packaged in the standard Linux distros is generally outdated and/or difficult to match with an official Arduino IDE release, it is better to scratch it if already installed, and download and install the latest Arduino IDE (I did use 1.6.12) directly from the Arduino official download page, either for Linux 32 bits or Linux 64 bits. This should create a folder “arduino-1.6.12” in your home directory.

If you want to add a menu item, icons and mime type for Arduino for the current user, run the “install.sh” script from this directory:

$ cd ~/arduino-1.6.12
$ ./install.sh

While you are at it, I suggest to check that your standard user has access to the Serial-to-USB cable. This can be done by running the command “id” and check for group “dialout”. If not found, you can add your user to the corresponding group:

$ id
uid=1000(your_user_here) gid=1000(your_user_here) groups=1000(your_user_here),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),113(lpadmin),128(sambashare)
$ sudo adduser your_user_here dialout

Restart your session (this is required), and check that you now are part of the “dialout” group:

$ id
uid=1000(your_user_here) gid=1000(your_user_here) groups=1000(your_user_here),4(adm),20(dialout),24(cdrom),27(sudo),30(dip),46(plugdev),113(lpadmin),128(sambashare)

Step #3: Setup the Arduino Due (ARM Cortex M3) package

In order to install the required GNU cross toolchain for the ARM Cortex M3 core (the one used in the STM32F103C8T6), we must install support for the Arduino SAM Boards (Arduino Due) which contains the same core.

In the Arduino IDE “Tools” menu, select the “Board” item, and in the cascaded menu, select the “Boards Manager…” item:

board-manager-menuBoards MAnager Menu

This will display the Boards Manager dialog box, in which we need to select the “Arduino SAM Boards (32-bits ARM Cortex M3) by Arduino” and click on the corresponding “Install” button to install the latest version (mine is 1.6.9):

Boards Manager

You can then close the “Boards Manager” Dialog box.

Step #4: Setup the Arduino STM32 package

Despite an active community, the STM32 is not (yet) an officially-supported Arduino platform, thus it is not available from the standard “Board Manager” in the IDE menus. You will have to manually download it from “https://github.com/rogerclarkmelbourne/Arduino_STM32“, extract it and copy the folder ‘Arduino_STM32-master’ to your Arduino/hardware folder (“~/Arduino/hardware”), then rename the folder by removing the “-master” suffix from the folder name in order to obtain a new folder named “~/Arduino/hardware/Arduino_STM32” (this suffix is the repository branch added automatically by Github, we don’t need it).

 

Step #5: Select the Board parameters

Launch the Arduino IDE and in the “Tools” menu, select the correct values for “Board”, “Variant”, “Upload method” and “Port”:

  • Board: “Generic STM32F103C series”
  • Variant: “STM32F103C8 (20k RAM 64k Flash)” (we will speak about it later, in the “Bonus” section below)
  • Upload method: “Serial”
  • Port: “/dev/ttyUSB0” or the one found at end of step #1 above

Board Selection

Variant Selection

Upload method Selection

Port Selection

Step #6: Compile a Sketch

Copy & Paste the following code to replace the current sketch code:

#define pinLED PC13

void setup() {
 Serial.begin(9600);
 pinMode(pinLED, OUTPUT);
 Serial.println("START"); 
}

void loop() {
 digitalWrite(pinLED, HIGH);
 delay(1000);
 digitalWrite(pinLED, LOW);
 delay(1000);
 Serial.println("Hello World"); 
}

Nothing fancy, if you already know the Arduino language, we just set up the UART @ 9600 bps, set the PC13 GPIO as an output (this one has a onboard LED attached to it) and send the “START” welcome message on the UART. Then in a loop, we toggle the LED ON/OFF with a 50% duty cycle with a 2 s period, while at the same time sending “Hello World” greetings on the UART.

By clicking on the “Check” icon in the Arduino IDE, you can compile (“Verify”) the sketch, you should not get any error.

Step #7: Upload the Sketch to the Board

On the board, set the yellow jumper for BOOT0 to the “1” position (please refer to the board picture above) and press the RESET button.

In the Arduino IDE, click on the Arrow button to upload the sketch to the board.

If everything goes as expected, you should see the red LED blink @ 2 Hz and get our messages sent on the UART by opening the “Serial Monitor” @ 9600 bps in the Arduino IDE “Tools” menu.

Not working? If you get a message like “Failed to open port: /dev/ttyUSB0”, please check that the cable is indeed associated to the correct Linux device by running the “dmesg” command like above, and make sure that your current user has access to the UART by performing the step #2 above.

For my part, I had a rather unusual message while trying to upload the sketch, I obtained a:

Got NACK from device on command 0x43
 Can't initiate chip erase!

Googling around, I found this thread that helped me to identify the root cause of this problem: the chip is actually locked, which is rather unusual for a development board! However, the provided solution involved using a tool from ST that only works under Windows 🙁

I found a purely Linux solution by running one of the tools installed by the Arduino STM32 package above: using the “stm32flash” utility, you can read and write unprotect the chip, using the following commands:

$ cd ~/Arduino/hardware/Arduino_STM32/tools/linux/stm32flash
$ ./stm32flash -k /dev/ttyUSB0
stm32flash Arduino_STM32_0.9

http://github.com/rogerclarkmelbourne/arduino_stm32

Interface serial_posix: 57600 8E1
Version : 0x22
Option 1 : 0x00
Option 2 : 0x00
Device ID : 0x0410 (Medium-density)
- RAM : 20KiB (512b reserved by bootloader)
- Flash : 128KiB (sector size: 4x1024)
- Option RAM : 16b
- System RAM : 2KiB
Read-UnProtecting flash
Done.

$ ./stm32flash -u /dev/ttyUSB0
stm32flash Arduino_STM32_0.9

http://github.com/rogerclarkmelbourne/arduino_stm32

Interface serial_posix: 57600 8E1
Version : 0x22
Option 1 : 0x00
Option 2 : 0x00
Device ID : 0x0410 (Medium-density)
- RAM : 20KiB (512b reserved by bootloader)
- Flash : 128KiB (sector size: 4x1024)
- Option RAM : 16b
- System RAM : 2KiB
Write-unprotecting flash
Done.

Going back to the Arduino IDE, you should now be able to upload your sketch successfully to the board!

Bonus

If you were careful and checked either the “stm32flash” utility output above, or check into the Arduino IDE messages during the sketch upload, you may have notice this:

Using Parser : Raw BINARY
Interface serial_posix: 230400 8E1
Version : 0x22
Option 1 : 0x00
Option 2 : 0x00
Device ID : 0x0410 (Medium-density)
- RAM : 20KiB (512b reserved by bootloader)
- Flash : 128KiB (sector size: 4x1024)
- Option RAM : 16b
- System RAM : 2KiB

The “128 KiB” line above means that we are not on an STM32F103C8 MCU with only 64 kB of Flash, but more likely on an STM32F103CB MCU with 128 kB of Flash! This confirms the last point in my previous message that both variants share the same chip production mask and only differ by memory tests.

You may also like...

5 Responses

  1. roman says:

    El mejor tutorial. Me salvo para programar mi stm32. Gracias. Copiare algo en mi blog

  2. ramezan says:

    please send us the price for stm32f103c8t6 board

  3. Thanks for the post, very useful. In case it helps someone, the Arduino IDE 1.6.5 showed this error when compiling: “arm-none-eabi-g++: error: core.a No such file or directory”. The IDE version 1.8.3 worked fine.
    Also a CH340 USB to serial adapter was giving some trouble.

    I successfully burned the STM32duino bootloader (generic_boot20_pc13.bin) following this guide: http://wiki.stm32duino.com/index.php?title=Burning_the_bootloader

    After flashing it and adding the udev rules (http://stm32duino.com/viewtopic.php?t=353) the board is programmable through its USB port choosing as upload method “STM32duino bootloader”.

  4. Another thing: I used the boot jumpers configuration described here: http://wiki.stm32duino.com/index.php?title=Bootloader
    That is:
    “Allow firmware upload through USART1:
    Boot0 HIGH
    Boot1 LOW”

Leave a Reply

Your email address will not be published. Required fields are marked *