STM32 development settings on Mac
ST-Link connection
Ref: [1]
- VCC 3.3v
- GND
- SWDCLK
- SWDIO
Install Stm32CubeIde
- install stm32cubeclt
- install stm32cubeide
$ arm-none-eabi-gcc --version
arm-none-eabi-gcc (GNU Tools for STM32 13.3.rel1.20240926-1715) 13.3.1 20240614
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ STM32_Programmer_CLI --version
-------------------------------------------------------------------
STM32CubeProgrammer v2.19.0
-------------------------------------------------------------------
STM32CubeProgrammer version: 2.19.0
$ ST-LINK_gdbserver --version
ST-LINK_gdbserver version: 7.10.0
$ cmake --version
cmake version 3.28.1
CMake suite maintained and supported by Kitware (kitware.com/cmake).
$ ninja --version
1.11.1
However, could not use with my SD_link:
STM32_Programmer_CLI -c port=SWD freq=4000 -e all
-------------------------------------------------------------------
STM32CubeProgrammer v2.19.0
-------------------------------------------------------------------
Error: Old ST-LINK firmware version. Upgrade ST-LINK firmware
Error: Old ST-LINK firmware!Please upgrade it.
Install ST Link Driver
brew install stlink
$ st-info --probe
Found 1 stlink programmers
version: V2J17S4
serial: 56FF74066580515144522467
flash: 524288 (pagesize: 2048)
sram: 65536
chipid: 0x414
dev-type: F1xx_HD
Install PlatformIo
Install platformio extension in vscode, and then install stm32 platform:
MDK-ARM
Windows7x64 virtual machine on Mac
- Download and install mdk-arm
- VC_redist.x64.exe
The pack installer seems useless. Just close it, and the install link is somehow disabled.
Download stm32f103ze DFP and install
MDK4.7
MDK 4.72 + keygen (Menu/License manage/CID), generated key only valid to 2020, but still can use
Project structure(https://github.com/drriguz/stm32-examples/tree/master/stm32-led):
Set c++ options:
Set debuger to use ST-link+SW:
Note:
- Don't use startup_stm32f10x_hd.s by Mdk, use the library one
- Don't call SystemInit in main.c, it's already called in startup_stm32f10x_hd.s
OpenOCD
https://openocd.org/pages/getting-openocd.html
# don't brew install, lacks of stlink support
# brew install openocd
brew install jimtcl libusb
# ./doc/openocd.texi:12874: Unmatched `@end'.
# makeinfo: Removing output file `doc/openocd.info' due to errors; use --force to preserve.
brew install texinfo # need newer version
# configure: error: libusb-1.x is required for adapter "ST-Link Programmer".
export PKG_CONFIG_PATH="/usr/local/opt/libusb/lib/pkgconfig:$PKG_CONFIG_PATH"
git clone https://git.code.sf.net/p/openocd/code openocd-code
cd openocd-code
# 12.0 Error: ST-Link version does not support DAP direct transport
git checkout v0.10.0
./bootstrap
./configure --enable-stlink --enable-ftdi --disable-werror
make -j 8
sudo make install
OpenOCD install path: /usr/local/share/openocd/scripts/
Start debug:
$ openocd -f interface/stlink-v2.cfg -f target/stm32f1x.cfg
Open On-Chip Debugger 0.10.0 (2025-04-04-01:17)
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
Info : auto-selecting first available session transport "hla_swd". To override use 'transport select <transport>'.
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
adapter speed: 1000 kHz
adapter_nsrst_delay: 100
none separate
Info : Unable to match requested speed 1000 kHz, using 950 kHz
Info : Unable to match requested speed 1000 kHz, using 950 kHz
Info : clock speed 950 kHz
Info : STLINK v2 JTAG v17 API v2 SWIM v4 VID 0x0483 PID 0x3748
Info : using stlink api v2
Info : Target voltage: 3.316364
Info : stm32f1x.cpu: hardware has 6 breakpoints, 4 watchpoints
STM32CubeMX
Select STM32F103ZET6:
Then config to use cmake, and click "Generate":
Make the project:
$ cd stm32-hello
$ mkdir build
$ cd build
$ make
[100%] Linking C executable stm32-hello.elf
Memory region Used Size Region Size %age Used
RAM: 1584 B 64 KB 2.42%
FLASH: 3772 B 512 KB 0.72%
[100%] Built target stm32-hello
stm32-for-vscode
Led blink
Ref:[6]
main.h
#ifndef __MAIN_H_
#define __MAIN_H_
#include "stm32f1xx_hal.h"
void Error_Handler(void);
#endif
main.cpp:
#include "main.h"
#define LED_PIN GPIO_PIN_4
#define LED_GPIO_PORT GPIOD
#define LED_GPIO_CLK_ENABLE() __HAL_RCC_GPIOD_CLK_ENABLE()
void SystemClock_Config(void);
void LED_Init();
int main(void)
{
HAL_Init();
SystemClock_Config();
LED_Init();
while (1)
{
HAL_GPIO_TogglePin(LED_GPIO_PORT, LED_PIN);
HAL_Delay(200);
}
}
void LED_Init()
{
LED_GPIO_CLK_ENABLE();
GPIO_InitTypeDef gpioConfig = {0};
gpioConfig.Pin = LED_PIN;
gpioConfig.Mode = GPIO_MODE_OUTPUT_PP;
gpioConfig.Pull = GPIO_NOPULL;
gpioConfig.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(LED_GPIO_PORT, &gpioConfig);
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
void Error_Handler(void)
{
__disable_irq();
while (1)
{
}
}
LED not blinking
The led is not blinking, but always on, similar as [7]
Issues
- If "st-info" shows no device, then check if the usb wine is good
- Stm32CubeIde does not work with none-original ST-Link device, error:
- If install old version of stm32cubeide, "“STM32CubeIDE is damaged and can’t be opened" [8]
- ↑ https://www.cnblogs.com/jpeg/p/hello-world-of-cocox-ide.html
- ↑ https://github.com/MaJerle/stm32-cube-cmake-vscode?tab=readme-ov-file
- ↑ https://static.assets-stash.eet-china.com/original/3X/2/b/2b319d51f9e583c6c76b6ac42bb2181c11102cb8.pdf
- ↑ https://pfeerick.github.io/InfiniTime/doc/openOCD.html
- ↑ https://rudi-horn.de/Home/7-debugging-the-stmf-with-an-st-linkv-and-openocd
- ↑ https://stm32-base.org/guides/platformio.html
- ↑ https://www.reddit.com/r/embedded/comments/ucohur/new_to_stm_tried_making_an_led_blink_but_it/
- ↑ https://community.st.com/t5/stm32cubeide-mcus/stm32cubeide-is-damaged-and-can-t-be-opened/td-p/256814