一.配置Clion-STM32环境

1.安装Clion

在你想要写代码的位置新建一个项目文件夹,右键用Clion打开。Clion为收费软件,但教育群体可以申请免费使用(学生邮箱白嫖JetBrains全家桶——保姆级教程 - 知乎)

2.安装MSYS2(点击此处进入官网下载)

安装路径建议默认。安装之后,打开MSYS2 MSYS2软件,他是一个类shell的界面,实际上它提供了包括mingw64、ucrt64、clang64等多种不同编译环境在内的一组类linux开发工具合集。

输入以下命令然后一路回车即可:

pacman -S mingw-w64-x86_64-toolchain mingw-w64-x86_64-arm-none-eabi-toolchain mingw-w64-x86_64-ccache  mingw-w64-x86_64-openocd
# 需要注意ctrl+V不是黏贴快捷键,而是Ins+Shift.或者右键点击空白处选择黏贴也可以.

安装好之后,把Msys2下的mingw64的bin加入PATH环境变量(win10系统如何添加环境变量-CSDN博客):C:\Msys2\mingw64\bin(这是默认路径)。

二.PULL电控框架到项目文件夹

1.开始下载电控框架代码

重新回到Clion在终端输入git clone --recursive https://gitee.com/justrm/justfw_-clion_-demo.git

回车并等待电控框架代码从云端下载完成

若提示GIT不是有效命令则安装GITGit - 安装 Git)!!

2.配置工具链

找到项目路径,鼠标右键选择Open Folder as CLion Project。打开后,进行配置:

1.点击File->Settings->Build,Execution,Deploment->Toolchains,参照下图进行设置:

点击➕,新建一个工具链

2.重命名,这里叫MinGW-STM32

3.选择前面MSYS(默认为C:\msys64)安装路径下的mingw64文件夹

4.保持默认捆绑

5.保持默认

6.分别选择MSYS安装目录(默认为C:\msys64\mingw64\bin)下的arm-none-eabi-gcc、arm-none-eabi-g++、arm-none-eabi-gdb

7.设置完成点击OK。

8.在Embedded Development选项中选择openocd位置(默认‪C:\msys64\mingw64\bin\openocd.exe)

3.更新Cmake项目

双击下载成功项目的CMakeLists.txtJUSTFW.ioc文件(此举是让Clion识别到项目中有这两个文件),最后点击左上角文件,依次点击重新加载Cmake项目和使用STM32cubemx更新Cmake项目

若提示cmake版本低于要求则进入CMakeLists.txt把要求改低即可

4.配置openocd

若出现让你选择项目配置文件,则选择stm32f4discovery.cfg并点击 选择并复制到项目 按钮。若未出现则在Openocd安装目录(默认C:\msys64\mingw64\share\openocd\scripts\board)下找到stm32f4discovery.cfg并复制到项目文件夹下(方便后续切换下载器),并在OCD编辑配置下选中此文件。

3.Clion双击打开上述cfg文件,通过更改上述cfg文件中的adapter driver参数切换下载器如ST-Link或DAP-Link

# SPDX-License-Identifier: GPL-2.0-or-later

# STM32F103C8 "Blue Pill"

# NOTE:
# There is a fair bit of confusion about whether the "Blue Pill" has 128kB or 64kB flash size.
# The most likely cause is that there exist a -C8 and a -CB variant of the STM32F103, where
# the C8 has 64kB, the CB has 128kB as per specification. "Blue Pill" boards are manufactured
# by a lot of different vendors, some may actually use the CB variant but from a cursory look
# it very hard to tell them apart ("C8" and "CB" look very similar). Nevertheless, people have
# tried using the full 128kB of flash on the C8 and found it to be working. Hence this board file
# overrides the internal size detection. Be aware though that you may be using you particular
# board outside of its specification. If in doubt, comment the following line.
set FLASH_SIZE 0x20000
adapter driver cmsis-dap
transport select swd

source [find target/stm32f4x.cfg]

三.Enjoy!