Monday, September 13, 2021

Arduino ID and CH32F103C8T6

(if you are catching up, start here: initial Arduino w/STM post)

OK, so I was looking to get a second STM32F103C8T6 board and a second STM32F411CEU6 board as well. I wanted to have two each so I could keep the latest circuit and code build on one board while the other was used to work on the next part of the project. Once tested, it would be rolled into the primary and then on to the next bit. Well, when my F103 order arrived, it was actually a CH32F103C8T6. I can not tell if I made a mistake and ordered the wrong board or if I was shipped the wrong board. The vendor was able to produce a invoice copy (of course) that showed the order was for the CH32F103C8T6, so I am stuck with it, which I expected as it would no way be worth shipping it back for a replacement. Figured I would keep it to see "how bad it could be" and just place another order for the STM.

Did some searching and it seems these boards can be actually fairly good and compatible with the STM boards or total crap. The one I wound up with is a blue/black-pill. It has the F103C8T6 chip by WCH and is on a blue circuit board but has the black pill push buttons for boot0 and NRST. It also has a USB-C connector.

I tried just hooking it up like the STM F103 and no luck.It came up as a universal serial bus unknown device. Then I figued what do I have to lose and connected it to the PC just like I connected the real Black-Pill. Used the ST-Link with the power disconnected and plugged in the USB-C connector. In Arduino, I selected the "Generic STM32F1 Series", "BluePill F103C8", set the rest up as normal and picked the upload method as "STM32CubeProgrammer (SWD)" (I also tried selecting "STLink" and think that worked better). This is the Cube results (the STLink results are lower down).


Sketch uses 23936 bytes (36%) of program storage space. Maximum is 65536 bytes.

Global variables use 3556 bytes (17%) of dynamic memory, leaving 16924 bytes for local variables. Maximum is 20480 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   : 0x410
Revision ID : Rev B
Device name : STM32F101/F102/F103 Medium-density
Flash size  : 64 KBytes
Device type : MCU
Device CPU  : Cortex-M3
BL Version  : 0x__


Memory Programming ...

Opening and parsing file: STM32_Blink.ino.bin
  File          : STM32_Blink.ino.bin
  Size          : 24232 Bytes
  Address       : 0x08000000 


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


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

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

Using STLink, got these results:

Sketch uses 14804 bytes (22%) of program storage space. Maximum is 65536 bytes.
Global variables use 2072 bytes (10%) of dynamic memory, leaving 18408 bytes for local variables. Maximum is 20480 bytes.
STM32 ST-LINK CLI v3.3.0.0
STM32 ST-LINK Command Line Interface

ST-LINK SN: 36FF6C06344E393734192457
ST-LINK Firmware version: V2J29S7 (Need Update)
Connected via SWD.
SWD Frequency = 4000K.
Target voltage = 3.2 V
Connection mode: Normal
Reset mode: Software reset
Device ID: 0x410 
Device flash Size: 64 Kbytes
Device family: STM32F10xx Medium-density

Loading file...
Flash Programming:
  File : c:\TEMPFI~1\arduino_build_155575\STM32_Blink.ino.bin
  Address : 0x08000000
Memory programming...
 0%������������������������� 51%������������������������� 100%
Memory programmed in 21s and 500ms.
Programming Complete.

MCU Reset.

Application started.

In addition, if you are having issues getting the program loaded via Arduino BUT you can connect successfully to the board using the ST-Link, you can load your Arduino sketch that way. Verify your sketch to make sure it is error free. Then select "Sketch", "Export compiled Binary". You will find the BIN file in the sketch directory. In my case that is "C:\Users\{login}\Documents\Arduino\STM32_Blink" and the binary was called "STM32_Blink.ino.BLUEPILL_F103C8.bin". Just launch ST-LINK, connect to the board, open the BIN and do the "program & verify". When I initially connect to the CH32F103C8T6, ST-Link displays:

21:26:14 : ST-LINK SN : 36FF6C06344E393734192457
21:26:14 : V2J29S7
21:26:14 : Connected via SWD.
21:26:14 : SWD Frequency = 4,0 MHz.
21:26:14 : Connection mode : Normal.
21:26:14 : Debug in Low Power mode enabled.
21:26:14 : Device ID:0x410 
21:26:14 : Device flash Size : 64KBytes
21:26:14 : Device family :STM32F10xx Medium-density

When the BIN file is opened, it shows:

21:27:46 : [STM32_Blink.ino.BLUEPILL_F103C8.bin] opened successfully.
21:27:46 : [STM32_Blink.ino.BLUEPILL_F103C8.bin] checksum : 0x0024D8A5 

After uploading the BIN, it shows:

21:28:33 : Memory programmed in 35s and 328ms.
21:28:33 : Verification...OK
21:28:33 : Programmed memory Checksum: 0x0024D8A5

Am still "playing" with the process/sequence but it looks to be working/workable. I will finish connecting the IC pins and likely use this as the second board until the "real" one comes in but so far it is "compatible" enough for what I am doing and the specs for memory and speed match so we shall see.

Update 21/09/15:

Did some simple interfacing and programming to this board using 2 sets of status light LED boards (each board has 6 LEDs that you can drive from the board pins). I first wired it all up using the STM32F411. It works completely and as expected. I then swapped out the F411 for the CH32F103C8T6 board. The sketch did need some minor tweaking on the pin assignments (as it would have if I were to swap in an F103) but other than that it compiled, loaded, and ran just as the BlackPill did. When I get a chance I will do an STM-F103 to the CH-F103 compare but it will need to wait for now as I don't have a free STM-F103 at the moment.

It does "appear" to be circuit board compatible as the various pins are labeled the same as the STM32F103 I have but I would like to test/verify some to be sure.

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!

Friday, February 26, 2021

 The minimum wage, in one cartoon...