![image.png](https://upload-images.jianshu.io/upload_images/35286-eb41010f6d4e8764.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
> 图引用自稚晖君的[《配置CLion用于STM32开发【优雅の嵌入式开发】》](https://www.bilibili.com/read/cv6308000)
# 前言
前一阵子接触到STM32开发,从零开始现学现用,还好有点Arduino开发经验,上手起来不算是太困难,然而,开发STM32的IDE或者叫“编辑器”简直就是上古产物啊!!! Keil5完全没有代码提示,连代码自动对齐都是奢望
跑通几个Demo后就实在受不了了,由于我习惯使用Idea系的IDE,如:AndroidStudio、PyCharm、WebStorm、CLion,所以就在找用CLion开发STM32的方法,然后就找的了稚晖君的[《配置CLion用于STM32开发【优雅の嵌入式开发】》](https://www.bilibili.com/read/cv6308000),如获至宝啊
不过,稚晖君的教程是基于【HAL库】的,然而,网上的大部分样例代码也是基于【标准库】的,对于我这种小白,样例代码无比重要,毕竟也就只会搬砖嘛,【标准库】基本都是使用的Keil5,也有一部分人推荐IAR+SourceInsight开发,都是上古产物,完全无感
好奇的跟着稚晖君的教程试了下,发现是通过STM32CubeIDE生成一个基于CMake的基础项目,既然是CMake项目,那跑【标准库】不就是改下CMake的事情吗?作为一个写过两年C++开发的菜鸟,改个CMake岂不是易如反掌,所以就有了这篇文章了
# 配置环境
> 详细步骤参考稚晖君的文章,写的很详细了,我这只列依赖的软件,STM32CubeMX其实并不必要,我们只需要配一份CMake和startup的*.s文件即可,可以通过STM32CubeMX生成,同一块板子也可以直接copy过来用就好,生成逻辑参考稚晖君的文章
* MinGW 【https://sourceforge.net/projects/mingw-w64/files/】
* arm-none-eabi-gcc 【https://developer.arm.com/open-source/gnu-toolchain/gnu-rm/downloads】
* OpenOCD 【https://gnutoolchains.com/arm-eabi/openocd/】
这三个是必要软件,只需要下压缩包解压放到一个的目录即可,最好是 **没有中文和空格** ,配置好Path环境,保证在cmd下这几行命令都能正常运行
``` bash
gcc -v
arm-none-eabi-gcc -v
```
运行成功如下:
![image.png](https://upload-images.jianshu.io/upload_images/35286-02512060ad194751.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
# 配置CLion
### 1. 设置里配置编译工具链
![image.png](https://upload-images.jianshu.io/upload_images/35286-815534c61019c8a4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
> 只需要配置下路径,其他都不用改
![image.png](https://upload-images.jianshu.io/upload_images/35286-c59747954ab31a89.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
### 2. 确认CMake使用了刚才配置的工具链
![image.png](https://upload-images.jianshu.io/upload_images/35286-a7cc8606c774e62b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
### 3. 创建CubeMX工程
![image.png](https://upload-images.jianshu.io/upload_images/35286-c20c5c6ad7f9b032.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
### 4. 将相关文件Copy到项目里【或者基于STM32CubeMX生成的项目删除不必要的文件】
> 如下图所示,include&source是我自己的代码,startup和STM32F103C8Tx_FLASH.ld是从STM32CubeMX生成的项目里copy过来的
![image.png](https://upload-images.jianshu.io/upload_images/35286-e751d540a5fda7bd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
### 5. 修改CMakeLists.txt
![image.png](https://upload-images.jianshu.io/upload_images/35286-1a6810f9c1b4b951.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
修改完CMake后记得刷新下,要不然编译时会出现找不到文件,也可设置自动刷新
![image.png](https://upload-images.jianshu.io/upload_images/35286-4870b7994a06f1c2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
正常刷新后可以看到自动创建了一个运行任务,代码配置没问题的话可以直接编译出固件了
![image.png](https://upload-images.jianshu.io/upload_images/35286-3e08eb5ec9a72eba.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
### 6. 配置烧录
运行配置里新建OpenOCD烧录
![image.png](https://upload-images.jianshu.io/upload_images/35286-f140721f3690dbe3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image.png](https://upload-images.jianshu.io/upload_images/35286-628e6c98b3accb04.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
选择好可执行程序和板子配置
![image.png](https://upload-images.jianshu.io/upload_images/35286-db29b16e881b56bc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
在OpenOCD里有很多默认的配置,我这里用的F103,所以选了一个103的配置
![image.png](https://upload-images.jianshu.io/upload_images/35286-82ce71d4db5fc593.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
很多文章都建议修改下配置文件,注释掉最后一行“reset_config srst_only”,好像不注释也没问题
![image.png](https://upload-images.jianshu.io/upload_images/35286-bc373e7d160ad5c7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
### 7. 大功告成
![image.png](https://upload-images.jianshu.io/upload_images/35286-ad0c4e09dbf5b6b8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![image.png](https://upload-images.jianshu.io/upload_images/35286-d250d2ea4ea584ff.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
# 最后
放一个我编译烧录都OK的项目在github上,供大家参考,我是基于STM32F103C8T6的板子移植的,这两天用CLion开发STM32简直不要太爽,希望大家都能优雅的嵌入式开发
https://github.com/vell001/CLionSTM32Demo
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
vscode配置stm32开发调试环境工程模板源码。(默认适用于STM32F1系列单片机)。 但是替换你的start_.s文件和ld文件,则适用于任何stm32系列。因为都是使用了HAL库,工程的cmake模板配置是通用的。详情参见博客介绍:【vscode搭建开发调试STM32环境IDE配置】https://blog.csdn.net/yyz_1987/article/details/131000116
资源推荐
资源详情
资源评论
收起资源包目录
vscodeSTM32Demo.zip (61个子文件)
vscodeSTM32Demo
include
main.h 120B
CMakeLists.txt 3KB
STM32F103C8Tx_FLASH.ld 6KB
readme.md 6KB
source
printf_uart.c 3KB
printf_uart.h 357B
main.c 239B
.gitignore 26B
startup
startup_stm32f103xb.s 10KB
STM32F10x_FWLib
inc
stm32f10x_bkp.h 7KB
stm32f10x_conf.h 3KB
stm32f10x_sdio.h 21KB
stm32f10x_dbgmcu.h 4KB
misc.h 9KB
stm32f10x_cec.h 6KB
stm32f10x_can.h 26KB
stm32f10x_fsmc.h 26KB
stm32f10x_spi.h 17KB
stm32f10x_wwdg.h 3KB
stm32f10x_dma.h 20KB
stm32f10x_exti.h 6KB
stm32f10x_tim.h 50KB
core_cm3.h 81KB
stm32f10x_crc.h 2KB
stm32f10x.h 610KB
stm32f10x_rtc.h 4KB
system_stm32f10x.h 2KB
stm32f10x_usart.h 16KB
stm32f10x_rcc.h 29KB
stm32f10x_dac.h 15KB
stm32f10x_adc.h 21KB
stm32f10x_i2c.h 29KB
stm32f10x_gpio.h 19KB
stm32f10x_pwr.h 4KB
stm32f10x_flash.h 24KB
stm32f10x_iwdg.h 4KB
src
stm32f10x_adc.c 46KB
stm32f10x_wwdg.c 5KB
misc.c 7KB
stm32f10x_spi.c 29KB
system_stm32f10x.c 35KB
stm32f10x_gpio.c 23KB
stm32f10x_tim.c 106KB
stm32f10x_sdio.c 28KB
stm32f10x_bkp.c 8KB
stm32f10x_rtc.c 8KB
stm32f10x_dac.c 18KB
stm32f10x_dma.c 29KB
stm32f10x_fsmc.c 34KB
core_cm3.c 16KB
stm32f10x_exti.c 7KB
stm32f10x_pwr.c 8KB
stm32f10x_i2c.c 44KB
stm32f10x_crc.c 3KB
stm32f10x_cec.c 11KB
stm32f10x_can.c 45KB
stm32f10x_iwdg.c 5KB
stm32f10x_usart.c 37KB
stm32f10x_dbgmcu.c 5KB
stm32f10x_flash.c 61KB
stm32f10x_rcc.c 49KB
共 61 条
- 1
特立独行的猫a
- 粉丝: 7w+
- 资源: 38
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页