Wednesday, September 8, 2021

Arduino IDE and the STM32F411CEU6 (Black Pill)

Many folks that make use of the Arduino platform for development, experimentation, and creation reach a point where they feel they have outgrown the various Arduino boards that are available (Uno being one such VERY popular board). Many of those folks have migrated to the more powerful STM32 microcontrollers. Many of these STM boards can be used with the Arduino IDE, some more easily than others.

I originally started using the STM32F103C8T6 board (also known as the "Blue Pill"). I really like the enhanced number of GPIO pins available as well as the higher processor speed. There are a number of videos and posts on how to get the F103 working with Arduino 1.8.15 IDE and they are [for the most part] straight forward and easy to follow. Once done, you can connect the F103 to your PC via USB and directly download your programs from the IDE. Be sure to follow one of the more recent posts/videos as some of the earlier ones now use outdated libraries/definitions. The board manager file I show below also works with the Blue Pill boards.

After I got that all up and running, I decided that for one specific project, I would prefer to have the greater speed of the STM32F411CEU6 (or "Black Pill") board and set out to get that working. My journey was not nearly so straight forward as it was getting the F103 connected.

Hopefully, the following steps will save others the weeks I took to get this figured out and back to the entire point of this exercise (the initial project). I was able to reproduce these steps with both the 1.8.15 and the 2.0 beta of the Arduino IDE. Pick your poison, the process is the same. If you already use a Blue Pill, you will see a LOT of overlap (at least early on) in the steps for supporting the Black Pill. Also, as I noted earlier, there are plenty of sources for getting the Blue Pill working with Arduino, just duckduckgo "STM32F103" and "Arduino" and follow those steps.

To add support for the STM32F411CEUT6 (or similar Black Pill boards) you will need:

  • An STM32F411CEU6 or similar "Black Pill" board.
  • An ST-Link interface with appropriate cables.
    • If you don't already have one be sure to review posts/videos on HOW to connect this interface device to the STM32 boards, connecting it wrong can fry your board.
  • The STM32CubeIDE and STM32 ST-Link Utility software
  • A USB cable compatible with your chosen board.

Now folllow these steps:

  • Download and install the STM software (if you have not already done so).
    • If you have a Blue Pill you should already have these as they were used to install the boot loader - the Black Pill boards already contain a boot loader so no need here.
  • Start the Arduino IDE.
  • Load a sketch (like blink or whatever).
  • Go to "Files", "Preferences", and select the far right graphic for "Additional Boards Manager URLs".
    • If it is not already listed in the list, on a new line add the following URL:
https://github.com/stm32duino/BoardManagerFiles/raw/master/package_stmicroelectronics_index.json

  • Go to "Tools", "Board", "Boards Manager" and search for "STM32" (or scroll to bottom of the list).
    • You want to install the "STM32 MCU based boards" by STMicroelectronics ver 2.0.0
  • Once installed you want to "select your board" by going to "Tools", "Board", and under "Boards Manager..." select:
    • STM32 boards groups (Board to be selected from Tools submenu 'Board part number')
    • This will open a list of boards, I selected "BlackPill F411CE"
      • I also tried the "Generic F411CEUx" and that worked but generated a LOT of compiler warnings so I will stick with the BlackPill selection.
  • Go to "Tools", "U(S)ART support":
    • Enabled (generic 'Serial')
  • Go to "Tools", "USB support":
    • CDC (generic 'Serial' supersede U(S)ART)
  • Go to "Tools", "USB speed":
    • Low/Full Speed
  • Go to "Tools", "Optimize":
    • Smallest (-0s default)
  • Go to "Tools", "C Runtime Library":
    • Newlib Nano (default)
  • Connect the STM32F411 to the PC using the ST-Link interface device.
  • Go to "Tools", "Upload method":
    • STMCubeProgrammer (SWD)

Now just compile and upload your sketch. Yes it actually was that easy! Your results should look something like this:

Sketch uses 23552 bytes (4%) of program storage space. Maximum is 524288 bytes.
Global variables use 3868 bytes (2%) of dynamic memory, leaving 127204 bytes for local variables. Maximum is 131072 bytes.
      -------------------------------------------------------------------
                       STM32CubeProgrammer v2.8.0                  
      -------------------------------------------------------------------

ST-LINK SN  : 36FF6C06344E393734192457
ST-LINK FW  : V2J29S7
Board       : --
Voltage     : 3.18V
SWD freq    : 4000 KHz
Connect mode: Under Reset
Reset mode  : Hardware reset
Device ID   : 0x431
Revision ID : Rev A
Device name : STM32F411xC/E
Flash size  : 512 KBytes
Device type : MCU
Device CPU  : Cortex-M4
BL Version  : 0x__


Memory Programming ...
Opening and parsing file: STM32_Blink.ino.bin
  File          : STM32_Blink.ino.bin
  Size          : 23996 Bytes
  Address       : 0x08000000


Erasing memory corresponding to segment 0:
Erasing internal memory sectors [0 1]
Download in Progress:


File download complete
Time elapsed during download operation: 00:00:00.745

RUNNING Program ...
  Address:      : 0x8000000
Application is running, Please Hold on...
Start operation achieved successfully

  • The board will immediately start running the uploaded program.
    • No jumpers to change, no push buttons to set, nothing.

If you also want to use the serial connection for monitoring / debugging just do the following additional steps:

  • Be SURE to disconnect the 3.3v connection between the ST-Link and the F411, do this FIRST. This is CRITICAL - if you don't do this you WILL fry your board as it will get power from multiple sources at the same time.
  • Now (once the ST-Link power cable is disconnected) just plug the USB cable to the PC and the STM32F411 while leaving the ST-Link in place (with NO ST-Link power cable).
  • This USB connection will appear as "STM Serial" on a com port and should automatically be selected by Arduino.
  • Just open the Arduino serial monitor and you should see your output there.
That is all there is to it! Have fun with your "Black Pill" board!

No comments:

Post a Comment