Program STM32 or CKS32 via USB Ubuntu

Posted on

STM32 Blue Pill

How to Program STM32 or CKS32 via USB Ubuntu? In this tutorial, I will share how we program an STM32 Bluepill or STM32F103 using Arduino on Linux.

In the market, I found that this STM32 Bluepill uses the STM32F103C8T6 and CKS32F103C8T6 chips.

STM32 is a chip produced by the company STMicroelectronics, while CKS32 is a chip manufactured by the company CKS.

To be able to upload program code to stm32 or cks32 via usb, you can follow these steps.

Install LIBUSB Linux

First, install libusb. libusb is a C library that provides access to USB devices. The command is:

sudo apt-get install libusb-1.0-0-dev

Install Library STLink Linux

STLink is a library and a collection of commands for programming and debugging STM32 produced by STMicroelectronics. There are four generations of STLink hardware available in the market, namely STLINK / v1 , STLINK / v2, STLINK / v2-1 and STLINK / v3.

How to Install STLink Library use this command:

git clone https://github.com/texane/stlink stlink.git
cd stlink
make

#install binaries:
sudo cp build/Debug/st-* /usr/local/bin

#install udev rules
sudo cp etc/udev/rules.d/49-stlinkv* /etc/udev/rules.d/

#and restart udev
sudo udevadm control --reload

If You Using CKS32

Important!!! If you are using the CKS32F103C8T6 chip, then you have to edit the core id address.

The steps is:

  • Open stm32.h in folder stlink /home/your_username/stlink/include, add this code: #define CS32VL_CORE_ID 0x2ba01477
  • Open the flash_loader.c in folder stlink /home/your_username/stlink/src.
  • Find “else if (sl->core_id == STM32VL_CORE_ID ||“, add this code “sl->chip_id == CS32VL_CORE_ID ||“. So it looks like the following image:
Program STM32 or CKS32

If the above method does not work, use the following command:

sudo apt install stlink-tools

Burn Bootloader

If previously the blue pill was programmed using UART and ST-Link Serial Communication, we had to change the program on the chip so that we could use USB for programming communication by means of Burn Bootloader or insert (embed) a file containing configuration so that USB can be used for programming.

Connect the STM32 Blue Pill to the ST-Link V2 with the following pin configuration:

ST-LINK V2STM32 BLUE PILL
3.3V3.3V
SWDIOSWIO
SWCLKSWCLK
GNDGND

Insert the ST-Link device (in this case I’m using V2) into the computer. After that check the stlink is connected or not by opening a terminal and entering the following code:

st-info --probe

If no installed stlink is found then the notification that appears in the terminal is:

Found 0 stlink programmers

If a stlink is found installed, then the notification that appears in the terminal is:

Found 1 stlink programmers
  serial:     3f3f010110134753384c4e00
  hla-serial: "\x3f\x3f\x01\x01\x10\x13\x47\x53\x38\x4c\x4e\x00"
  flash:      65536 (pagesize: 1024)
  sram:       20480
  chipid:     0x0410
  descr:      F1xx Medium-density

After that, download the bootloader by clicking the following button.

Download File generic_boot20_pc13.bin

After the download is complete, move the generic_boot20_pc13.bin download file to home.

Open terminal, navigate to home directory, then Flash stm32 with command:

st-flash write generic_boot20_pc13.bin 0x8000000

Install DFU-UTIL Linux

What is DFU? DFU (Device Firmware Upgrade) is used to be able to download and upload firmware to/from a microcontroller connected via USB.

Install the latest dfu-util by:

git clone git://git.code.sf.net/p/dfu-util/dfu-util
cd dfu-util
./autogen.sh
./configure
make
sudo make install

Install Library Board Arduino STM32

There are two ways to add it, download it from the Board Manager or download it manually. If you want to go through the board manager, please read here.

If you still have the Arduino IDE open, please close and reopen your Arduino IDE. then set the board settings like this:

BoardGeneric STM32F103C series
VariantSTM32F103C8 (20k RAM. 64k Flash)
Upload methodSTM32duino bootloader
CPU Speed(Mhz)72 Mhz (Normal)
OptimizeSmallest (default)
Port/dev/ttyASM0 (Maple Mini)
ProgrammerAVRISP mkll

Upload Blink Code

To find out whether the above method works or not, we try to upload a blik program to turn on/off the LED on the STM32 board.

// This program is to turn on the embedded led on the board.
// If arduino, LED_BUILTIN is on pin 13
// For STM32, LED_BUILDIN is PC13

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH); 
  delay(1000);                       
  digitalWrite(LED_BUILTIN, LOW);    
  delay(1000);                       
}

If the upload error and the error appears is dfu-util: Invalid DFU suffix signature, try to see how to fix it on the page How to Solve Errors in the STM32 Program Process Using Linux.

If successful, you can see a notification on the Arduino IDE as shown in the following image:

Program STM32 or CKS32

or like this:

Program STM32 or CKS32

I hope this Program STM32 or CKS32 tutorial is useful.

Read more…
> CH340E Schematic USB Serial
> Arduino Port Not Found USB Serial CH340 Driver
> Keyes CNC Shield V4 GRBL How To Use It
> avrdude stk500 recv() programmer is not responding [Solved]
> Burn ATmega328 Bootloader Using USBasp
> [Solved] ESP8266 NodeMCU Error Time out waiting for packet header In Ubuntu Linux
> ATtiny Serial UART and Computer

Source

  1. https://libusb.info/
  2. https://github.com/stlink-org/stlink
  3. https://fishpepper.de/2016/09/16/installing-using-st-link-v2-to-flash-stm32-on-linux/
  4. https://github.com/rogerclarkmelbourne/Arduino_STM32
  5. https://gitmemory.com/issue/texane/stlink/833/531831018
  6. http://dfu-util.sourceforge.net/