Introduction: Setting Up Blue Pill Board in STM32CubeIDE
The Blue Pill is a very cheap bare bones ARM development board. It has a STM32F103C8 as its processor which has 64 kbytes of flash and 20 kbytes of RAM memories. It runs up to 72 MHz and is the cheapest way to get into ARM embedded software development.
Most example projects and how to's describe programming the Blue Pill board using the Auduino environment. While this works and is a way to get started it has its limitations. The Arduino environment shelters you a bit from the underlying hardware - that's its design goal. Because of this you will not be able to take advantage of all the features the processor offers, and integrating a real time operating system is not really supported. This means that the Arduino environment is not widely used in industry. If you are wanting to make a career in embedded software development, Arduino is a good starting place, but you need to move on and use a development environment that is used industrially. ST helpfully provide a completely free development environment suite for their processors called STM32CubeIDE. This is widely used in industry, so it's a good one to move on to.
However, and this is the big however, STM32CubeIDE is fearsomely complicated and is a daunting piece of software to use. It supports all features of all ST's processors and allows them to be intimately configured, which you don't come across in the Arduino IDE because it's all done for you.
You need to set up your board as a first step in STM32CubeIDE. The IDE knows about ST's own development boards and sets them up for you, but the Blue Pill, while using a ST processor, it is not an ST product, so you are on your own here.
This instructable takes you through the process of setting up your Blue Pill board, enabling a serial port, and writing out some text. It's not much, but it's an important first step.
STM32CubeIDE - download from ST's website. You need to register and it takes a while to download.
A Blue Pill board. You can get them from ebay. You need one that has a genuine ST processor on it as some don't. In ebay zoom in on the picture and look for the ST logo on the processor.
A ST-LINK v2 debugger/programmer available from ebay for a few pounds.
A FTDI TTL to USB 3.3V serial cable for output and 2 male to female header wires to connect it.
A serial terminal program like PuTTY.
Step 1: Creating a New Project
- Start STM32CubeIDE and then from the menu choose File|New|STM32 Project.
- In the Part Number Search box enter STM32F103C8.
- In the MCUs/MPUs List you should see STM32F103C8. Select this line as in the image above.
- Click Next.
- In the Project Setup dialog give you project a name.
- Leave everything else as it is and click Finish. Your project will appear to the left in the Project Explorer pane.
Step 2: Configuring the Processor
- In the Project Explorer pane open up your project and double click the .ioc file.
- On the Project & Configuration tab expand System Core then select SYS.
- Under SYS Mode and Configuration in the Debug drop-down choose Serial Wire.
- Now select RCC in the System Core list just above SYS you selected above.
- Under RCC Mode & Configuration from the High Speed Clock (HSE) drop-down select Crystal/Ceramic Resonator.
- Now under Categories again, open up Connectivity and select USART2.
- Under USART2 Mode and Configuration from the Mode drop-down select Asynchronous.
- Now select the Clock Configuration tab and go to the next step.
Step 3: Configuring the Clocks
You can now see a rather daunting clock diagram, but it only needs setting up once. This the hardest to describe here as the diagram is complex. All the things you need to change are highlighted in the image above.
- The Blue Pill board comes with an 8 MHz crystal on the board and that is what the clock configuration diagram defaults to, so we don't need to change that.
- Under PLL Source Mux select the lower choice, HSE.
- Just to the right set PLLMul to X9.
- To the right again under System Clock Mux select PLLCLK.
- To the right again under APB1 Prescalar select /2.
- That's it. If you see any parts of the diagram highlighted in purple you've done something wrong.
Step 4: Save and Build
- Save the .ioc configuration with Ctrl-S. When you are asked if you want to generate code select Yes (and tick Remember my decision so that you are not asked every time). You can close the .ioc file.
- Now do a build from the menu Project|Build Project.
Step 5: Adding Some Code
Now we'll add some code to use the serial port we configured.
- In Project Explorer open up Core\Src and double click main.c to edit it.
- Scroll down until you find the main() function and add the code shown below just below the comment /* USER CODE BEGIN 3 */ then do a build again.
HAL_UART_Transmit(&huart2, (uint8_t *)"Hello, world!\r\n", 15U, 100U);<br>
Next it's connect the up hardware and give it a go.
Step 6: Connecting Up the Hardware
Connecting the ST-LINK v2
The ST-LINK v2 should have come with a 4 wire female to female header ribbon cable. You need to make the following connections:
Blue Pill to ST-LINK v2
GND to GND
CLK to SWCLK
DIO to SWDIO
3.3 to 3.3V
See the first image above.
Connecting the Serial Cable
If you go back to the .ioc file and look at the chip diagram on the right you'll see that UART2's Tx line is on pin PA2. Therefor connect the pin labelled PA2 on the Blue Pill board to the connection with the yellow wire on the FTDI Serial cable. Also connect one of the Blue Pill's ground pins (labelled G) to the black wire on the FTDI serial cable.
See the second image above.
Step 7: Debugging
Plug in you FTDI Serial cable and fire up a serial terminal at 115200 baud. Then plug in your ST-LINK v2 and you're ready to go.
- From STM32CubeIDE choose Run|Debug. When a Debug as dialog pops up choose STM32 Cortex-M C/C++ Application and OK.
- When a Edit Configuration dialog pops up just press OK.
- The debugger will break on the first line of main(). From the menu choose Run|Resume and check for messages in the serial terminal.
Step 8: Doing More
That's it, your first STM32CubeIDE application is configured and running. This example doesn't do much - just sends some data out of the serial port.
To use other peripherals and write drivers for external devices you have to tackle that daunting configuration editor again! To help, I have produced a series of STM32CubeIDE example projects that configure and exercise all the peripherals on the Blue Pill's processor in small easy to understand projects. They are all open source and you are free to do whatever you want to with them. Each peripheral is configured and then has sample code to exercise it in isolation (nearly!) so that you can concentrate on getting just one peripheral going at a time.
There are also drivers for external devices from simple EEPROM chips to pressure sensors, text and graphic LCD's, SIM800 modem for TCP, HTTP and MQTT, keypads, radio modules, USB and also integration with FatFS, SD cards and FreeRTOS.
They can all be found in Github here...