Skip to content

Instantly share code, notes, and snippets.

@csukuangfj
Last active September 4, 2024 23:20
Show Gist options
  • Save csukuangfj/bfe2be78eda3a24a5164005b1092c94b to your computer and use it in GitHub Desktop.
Save csukuangfj/bfe2be78eda3a24a5164005b1092c94b to your computer and use it in GitHub Desktop.
stm32-macos

STM32 MacOS X Environment Setup

Table of Contents

1. Install JDK 8

2. Install Eclipse

Option 1: GNU MCU Eclipse IDE for C/C++ Developers

  • https://github.com/gnu-mcu-eclipse/org.eclipse.epp.packages/releases
  • 20190101-2023-gnumcueclipse-4.5.1-2018-12-R-macosx.cocoa.x86_64.tar.gz
  • unzip and move to the folder Applications
  • start eclipse, menu, help, install new software, work with, select "cdt", check "CDT main features", next, finish, restart eclipse.
  • start eclipse, menu, help, install new software, work with, select ""

3. Cross Compile Toolchain (arm-none-eabi-*))

Option 1: GNU MCU Eclipse ARM Embedded GCC

mkdir $HOME/opt
cd $HOME/opt
tar xvf gnu-mcu-eclipse-arm-none-eabi-gcc-8.2.1-1.1-20190102-1122-macos.tgz
chmod -R -w $HOME/opt/gnu-mcu-eclipse/arm-none-eabi-gcc/8.2.1-1.1-20190102-1122/

we have $HOME/opt/gnu-mcu-eclipse.

cd $HOME/opt/gnu-mcu-eclipse/arm-none-eabi-gcc/8.2.1-1.1-20190102-1122/bin/
./arm-none-eabi-gcc --version

# output:
arm-none-eabi-gcc (GNU MCU Eclipse ARM Embedded GCC, 64-bit) 8.2.1 20181213 (release) [gcc-8-branch revision 267074]
Copyright (C) 2018 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.

Option 2: GNU ARM Embedded Toolchain

mkdir -p $HOME/opt
cd $HOME/opt
tar xvf gcc-arm-none-eabi-8-2018-q4-major-mac.tar.bz2
chmod -R -w ./gcc-arm-none-eabi-8-2018-q4-major
cd gcc-arm-none-eabi-8-2018-q4-major/bin/
./arm-none-eabi-gcc --version

# output:
arm-none-eabi-gcc (GNU Tools for Arm Embedded Processors 8-2018-q4-major) 8.2.1 20181213 (release) [gcc-8-branch revision 267074]
Copyright (C) 2018 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.

4. Install STLink

brew install stlink

st-info --version

# it prints:
v1.5.1

st-info --descr

# it prints
F7 device

5. OpenCD

cd $HOME/opt
tar xvf gnu-mcu-eclipse-openocd-0.10.0-10-20181020-0522-macos.tgz
# its content is in $HOME/opt/gnu-mcu-eclipse/openocd

To test OpenCD

cd $HOME/opt/gnu-mcu-eclipse/openocd/0.10.0-10-20181020-0522/scripts/target
mv stm32f7x.cfg stm32f7x.cfg-fangjun-bak
wget https://repo.or.cz/openocd.git/blob_plain/HEAD:/tcl/target/stm32f7x.cfg
cd ..
../bin/openocd -f board/st_nucleo_f7.cfg

It should output

GNU MCU Eclipse 64-bit Open On-Chip Debugger 0.10.0+dev-00352-gaa6c7e9b (2018-10-20-08:28)
Licensed under GNU GPL v2
For bug reports, read
	http://openocd.org/doc/doxygen/bugs.html
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
adapter speed: 2000 kHz
adapter_nsrst_delay: 100
srst_only separate srst_nogate srst_open_drain connect_deassert_srst
srst_only separate srst_nogate srst_open_drain connect_deassert_srst
Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Info : Unable to match requested speed 2000 kHz, using 1800 kHz
Info : Unable to match requested speed 2000 kHz, using 1800 kHz
Info : clock speed 1800 kHz
Info : STLINK v2 JTAG v25 API v2 SWIM v14 VID 0x0483 PID 0x374B
Info : using stlink api v2
Info : Target voltage: 3.240235
Warn : Silicon bug: single stepping will enter pending exception handler!
Info : stm32f7x.cpu: hardware has 8 breakpoints, 4 watchpoints
Info : Listening on port 3333 for gdb connections

We get an updated stm32f7x.dap to avoid the following error

GNU MCU Eclipse 64-bit Open On-Chip Debugger 0.10.0+dev-00352-gaa6c7e9b (2018-10-20-08:28)
Licensed under GNU GPL v2
For bug reports, read
	http://openocd.org/doc/doxygen/bugs.html
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
adapter speed: 2000 kHz
adapter_nsrst_delay: 100
srst_only separate srst_nogate srst_open_drain connect_deassert_srst
target/stm32f7x.cfg:155: Error: invalid command name "stm32f7x.dap"
in procedure 'script'
at file "embedded:startup.tcl", line 60
at file "board/st_nucleo_f7.cfg", line 8
at file "target/stm32f7x.cfg", line 155

Start gdb

arm-none-eabi-gdb
(gdb) target remote localhost:3333

6. Install STM32CubeMX

mkdir stm32cubemx
mv en.stm32cubemx.zip stm32cubemx
cd stm32cubemx
unzip en.stm32cubemx.zip

java -jar SetupSTM32CubeMX-5.0.1.exe

# use the default path: /Applications/STMicroelectronics/STM32CubeMX

# the uninstaller program is in /Applications/STMicroelectronics/STM32CubeMX.app/Contents/Resources/Uninstaller

# The installed application is available with the name "STM32CubeMX"

Its user manual is at https://my.st.com/content/ccc/resource/technical/document/user_manual/10/c5/1a/43/3a/70/43/7d/DM00104712.pdf/files/DM00104712.pdf/jcr:content/translations/en.DM00104712.pdf

7. STM32CubeProgrammer

mkdir stm32cubeprogrammer
mv en.stm32cubepro.zip stm32cubeprogrammer
cd stm32cubeprogrammer
unzip en.stm32cubepro.zip
java -jar SetupSTM32CubeProgrammer-1.4.0.exe

# Use the default installation path: /Applications/STMicroelectronics/STM32Cube/STM32CubeProgrammer
# install only the components that are selected by default, i.e., do not install STM32TrustedPackageCreator
# The installed application is available with the name "STM32CubeProgrammer"

When it starts, an popup windows says:

Error: Old ST-LINK firmware version. Upgrade ST-LINK firmware.

unzip en.stsw-link007.zip
cd stsw-link007
cd AllPlatforms
java -jar STLinkUpgrade.jar

# then, click the button "Open in update mode", then click "upgrade"
# after a few seconds, it displays: "Upgrade successful".
# close the window.

Open "STM32CubeProgrammer", it should display no error messages.

Inside /Applications/STMicroelectronics/STM32Cube/STM32CubeProgrammer/STM32CubeProgrammer.app/Contents/MacOs/bin, we can find STM32_Programmer_CLI, which is the commandline tool.

8. Hello World

  • Generate a Makefile project from CubeMx. Select the specified board.
make GCC_PATH=$HOME/opt/gcc-arm-none-eabi-8-2018-q4-major/bin
cp build/hello_f746_fangjun.bin ~/opt/gnu-mcu-eclipse/openocd/0.10.0-10-20181020-0522/scripts/a.bin
cd ~/opt/gnu-mcu-eclipse/openocd/0.10.0-10-20181020-0522/scripts/
../bin/openocd -f board/st_nucleo_f7.cfg -c "program a.bin exit 0x08000000"
  • Press the black reset button.

  • Or use CubeProgrammer to upload the binary:

    • Open STM32CubeProgrammer
    • Click Connect
    • Select "Erase & Programming"
    • For the file path, select "/path/to/xxxx.bin"
    • For the start address, use the default value "0x08000000"
    • Check "Run after programming"
    • Click the button "Start Programming"
  • Or use the following command

st-flash write ./build/*.bin 0x08000000

LED

HAL_GPIO_TogglePin(LD1_GPIO_Port, LD1_Pin);
HAL_Delay(1000);

USART

In cubemx,

  • Clock Configuration, HCLK (MHz) --> 216 MHz (for STM32F746 Nucleon-144 board)
  • Pinout & Configuration
    • Computing: CRC, activated
    • Right panel "Pinout view": PD9 (select USART3_RX), PD8(USART3_TX)
    • Connectivity: select USART3, mode (select Asynchronous). Leave others to their defaults, i.e., baudrate is 115200, 8bits, no parity check.
  • Generate Code

In the code

char buf[100];
int i = 0;
while (1) {
  /* USER CODE END WHILE */
  HAL_GPIO_TogglePin(LD1_GPIO_Port, LD1_Pin);
  i++;
  snprintf(buf, sizeof(buf), "hello %d\r\n", i);

  HAL_UART_Transmit(&huart3, buf, strlen(buf), 5000);
  HAL_Delay(1000);
}

After uploading the binary to the board. On macOS, do the following:

  • brew install minicom
  • ls /dev/tty.usbmodem1423, it may be tty.usbmodelxxxx, replace xxxx in the following command
  • minicom -D /dev/tty.usbmodem1423
  • it should print the message sent from the board
  • Note that the metakey in iterm2 is ESC. Press ESC + Z ot invoke the help window.
  • ESC + Z, then press X to exit minicom

To redirect printf, do the following:

  • Copy syscalls.c from $HOME/STM32Cube/Repository/STM32Cube_FW_F7_V1.14.0/Projects/STM32F769I_EVAL/Examples/UART/UART_Printf/SW4STM32 to the project

  • add the following function to main.c

int __io_putchar(int ch) {
  HAL_UART_Transmit(&huart3, (uint8_t *)&ch, 1, 0xFFFF);
  return ch;
}

X-CUBE-AI

Go to https://my.st.com/content/my_st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32cube-expansion-packages/x-cube-ai.license=1546650638389.product=X-CUBE-AI.version=3.3.0.html to download the software.

mkdir x-cube-ai
cd x-cube-ai
mv ../en.x-cube-ai.zip .
unzip en.x-cube-ai.zip

Then open STM32CubeMx, menu, help, manage embedded software packages, from local..., select "STMicroelectronics.X-CUBE-AI.3.3.0.pack".

Refer to the youtube video Getting Started with STM32Cube.AI.

Refer to https://github.com/araobp/stm32-mcu/tree/master/NUCLEO-F401RE/AI for an example of using x-cube-ai.

References

Refer to

@nalzok
Copy link

nalzok commented Jan 8, 2020

Hi there, I'm getting the following error on macOS Mojava

$ open /Applications/STMicroelectronics/STM32Cube/STM32CubeProgrammer/STM32CubeProgrammer.app
LSOpenURLsWithRole() failed with error -10810 for the file /Applications/STMicroelectronics/STM32Cube/STM32CubeProgrammer/STM32CubeProgrammer.app.

Any idea?

@csukuangfj
Copy link
Author

@nalzok

I think I did not post it here to open the app from the commandline and do NOT know why you tried it this way.
No idea how to solve your problem.

You can just Google your error message or open the app by double clicking it instead of from the cmd line.

@nalzok
Copy link

nalzok commented Jan 9, 2020

@nalzok

I think I did not post it here to open the app from the commandline and do NOT know why you tried it this way.
No idea how to solve your problem.

You can just Google your error message or open the app by double clicking it instead of from the cmd line.

I did it this way because the STM32CubeProgrammer just quits after I double clicking it, and I'm asking here because Google didn't solve the problem :(

Thanks for the reply anyway! By the way, this guide is really comprehensive, thanks for your contribution. May I ask if there is any "pure command line" approach from programming a Blue Pill? I prefer uniform CLI editors than those vendor-provided IDEs.

@csukuangfj
Copy link
Author

For your information:

$ cd SetupSTM32CubeProgrammer-1.4.0.app
SetupSTM32CubeProgrammer-1.4.0.app$ cd Contents/MacOs/
SetupSTM32CubeProgrammer-1.4.0.app/Contents/MacOs$ ./SetupSTM32CubeProgrammer-1_4_0_macos

It should popup a window for installation.

As for the IDE, I use CubeProgrammer to flash the board. You can also use STLink mentioned in the post, which is a cmdline tool.
Vim is used for writing code.

@csukuangfj
Copy link
Author

@nalzok

If you followed this post step by step, you should have tried the following commands mentioned above

mkdir stm32cubeprogrammer
mv en.stm32cubepro.zip stm32cubeprogrammer
cd stm32cubeprogrammer
unzip en.stm32cubepro.zip
java -jar SetupSTM32CubeProgrammer-1.4.0.exe

I repeat the commands here

stm32cubeprogrammer$ ls
SetupSTM32CubeProgrammer-1.4.0.app	SetupSTM32CubeProgrammer-1.4.0.linux
SetupSTM32CubeProgrammer-1.4.0.exe	en.stm32cubeprog.zip
$ java -jar SetupSTM32CubeProgrammer-1.4.0.exe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment