How to program STM32 via USB

The STM32  development board with the STM32F103C8 core is  gaining popularity due to its ARM Cortex M3 architecture, which has higher operating speed and more peripheral options. Also, since the board can be easily programmed using the Arduino IDE, it has become the first choice for many hobbyists and engineers for rapid prototyping.

In the previous article, we covered programming the STM32F103C8 board to blink the onboard LED using "Arduino IDE + FTDI". But it has a small disadvantage, when we need to program it, we need to use the FTDI module, and we have to switch back and forth between jumpers 0 and 1 when programming and testing the code, which is a bit cumbersome. The mini-USB port on the board is also completely unused. And when we plugged the USB into the computer, it didn't recognize the board because it didn't have an  Arduino  bootloader . Next, we will program the Arduino bootloader into the STM32F103C8 to implement the USB programming program. It is not known whether there will be unpredictable bugs in this way, and it is not recommended for critical applications.

materials needed

§ STM32 development board (  STM32F103C8  )

§ FTDI module

§ Breadboard

§ Connection cable

§ An internet-connected computer

Connection circuit diagram

 To program the STM32  directly through the  USB  port , we need to flash the  bootloader into the MCU first. For this, we need to use an FTDI board (modules like CH340 can also be used). The board is connected to the Rx and Tx pins of the STM32 as shown below.

The Vcc pin of the FTDI board is connected to the STM32 5V pin that supplies power to the board. The ground is connected to the ground of the STM32. The Rx and Tx pins of the FTDI board are connected to the A9 and A10 pins of the STM32, respectively. Where A9 is the Tx pin of the STM32 MCU and A10 is the Rx pin.

When uploading the bootloader, make sure the bootstrap 0 jumper pin on the board is set to 1. After uploading (burning) is complete, the pin changes back to its original position.

Upload the Bootloader to the STM32 development board

After completing the above connections, connect the FTDI board to your computer and follow the steps to flash the BootLoader into the STM32.

Step  1  : Prepare the BootLoader file.

Step 2 :  Next, download and install the STM Flash loader software, and flash the BootLoader file into the STM32.

Go to ST website, scroll to the bottom and click Get latest

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

Step  3  : You need to fill in the relevant information before downloading. The main thing is to enter the correct email address. Other information is not important. The download address will be sent to the email. Click to download from the email.

Step  4  : After downloading and installing the software, make sure that the Boot 0 jumper of the STM32 board is in the 1 position and has been connected to the computer through the FTDI board, then start the software. The software will automatically detect the COM port, but it is recommended to check the hardware management to make sure you have selected the correct COM port.

Step 5:  Click the Next button twice, the software will automatically detect the details of the board again and display the following picture. The board we are using is an STM32F1 with 128K flash.

Step  6  :  In Next, select "Download to device", browse to the location where we prepared the BootLoader file in step 1 and select it. Click Next.

Step 7 :  Wait for the programming to complete.

Step  8  :  After the programming is successful, we will see the following screen. Click Close and exit the application. Next we prepare the Arduino IDE and install the drivers before we can use the USB to burn the STM32 board.

Prepare the Arduino IDE and install the driver

Follow the steps below to download and prepare the Arduino IDE for use with the STM 32 board.

Step  1  :  - Installation steps (omitted)

Step 2:  - After installing the Arduino IDE, download the packages required to install the STM32 board refer to the previous article

Install the development library of STM32 through the following URL

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

Step  3  : Install the driver file (the URL can't be found) 

install_drivers.bat 和 install_STM_COM_drivers.bat

Step  4  :  Click on the two bat files and install the driver. You will see the DOS screen shown below.

Now the Arduino IDE is ready to program the STM32 board and the drivers are installed.

Direct programming of STM32 via USB port

Remove the FTDI board and other connections from the development board. Just connect it to your computer using the micro-USB port on the STM32 board as shown in the picture below. Make sure the  jumper  0  pin is positioned back to  0  . After this, we no longer need to switch jumpers to burn and run the program.

Connect the development board to the computer with a USB cable, and now the computer should be able to discover the development board. If you see any other drivers being installed, please wait a moment. Then go into Device Manager and check if your STM32 board is found as shown below. Mine is connected to COM6 and named Maple.

Open the Arduino IDE go to " Tools " and scroll down to find the Generic STM32F103C series as shown below. Then make sure the variant is  64k Flash  type , the CPU speed is  72MHz  , and change the "Upload method" to  STM32duino Bootloader . Also select the correct COM port according to Device Manager.

After making all the changes, check the bottom right corner of the Arduino IDE and you should see the configuration information. My STM32 board is connected to COM6, but yours may be different.

Now the Arduino IDE is ready to program the STM32F103C8 board. Let's upload the sample blinking program from the Arduino IDE to the STM32F103C8 board to test if it works. A sample program can be found by following the steps below

In the opened sample program,  we have to make a small change . By default, the example is programmed for PB1, but in our onboard LED is connected to PC13  , so replace all  PB1s  with  PC13  . The modified  complete sample program is  as follows.

Press the "Upload" button on the Arduino IDE and your program should be compiled and uploaded. If all went well, then you should see the following on the Arduino IDE console.

Next, you should see the blue LED blink at 1 second intervals, you can also modify the delay time to increase or decrease the delay to verify that the upload (burning) was successful. Now you can start using the STM32F103C8 board like any other Arduino board, i.e. no need to change jumper positions or use external hardware to upload (burn) and test programs.

Guess you like

Origin blog.csdn.net/m0_67034740/article/details/124263437