STM32 × CLion 新建项目
新建和配置一个 STM32 项目
1 创建项目
假如是 ST 官方开发板,比如 NUCLEO 板,选择从 ST 板创建
假如是单芯片或淘宝买的那种 F103 开发板,选择从 MCU 创建
2 STM CubeMX 配置
2.1 Pinout & Configuration 外设配置
假如是 ST 官方开发板,可以使用默认设置
否则,在 System Core - SYS - Debug 选择 Serial Wire
:::danger 主警告
忘记配置 Debug 可能造成无法二次烧录代码 (恢复有点麻烦)
:::
2.2 Clock Configuration 时钟树
暂时可以忽略
2.3 Project Manager 项目配置
通常只用配置这两个
- Project - Project Settings - Toolchain/IDE : STM32CubeIDE 并勾选 Generate Under Root
- Code Generator - Generated files - Generate … ‘.c/.h’ … peripheral
2.4 Tools 工具
绝大多数情况可以忽略
2.5 生成代码
完成后点击右上角 GENERATE CODE
3 CLion 设置
CLion 新建项目时选择 嵌入式 - STM32CubeMX
3.1 非 ST 官方开发板 (且首次使用)
在 OpenOCD 目录新建对应芯片配置文件,如: .\OpenOCD-20240916-0.12.0\share\openocd\scripts\board\f103.cfg
# choose st-link/j-link/dap-link etc.
#adapter driver cmsis-dap
#transport select swd
source [find interface/stlink.cfg]
transport select hla_swd
source [find target/stm32f1x.cfg]
# download speed = 10MHz
adapter speed 10000
- source [find interface/stlink.cfg] 表示加载 STLink 配置
- transport select hla_swd 表示传输协议 hla_swd 为 STLink 协议,否则 swd
- source [find target/stm32f1x.cfg] 表示芯片为 F1 系列
3.2 创建项目
从现有的源创建
选择面板配置文件。ST 官方板一般都有支持: 比如 st_nucleo_l4.cfg
,非官方开发板选择 3.1 中创建的 cfg 文件就行
3.3 下载代码
下载和编写代码时应选中下面绿色的 OpenOCD 否则会标红一片
4 其他说明
4.1 代码编写
每次更改后在 CubeMX 中生成代码就相当于重新对项目进行生成,所以代码应该放在 CODE BEGIN 和 CODE END 中间,否则代码将会被覆盖
/* xxx CODE BEGIN n */
代码放这
/* xxx CODE END n */
4.2 重定向 printf
方便输出数字或者其他的,注意 printf 只有包含 \n 才会发送数据
4.2.1 CLion 操作
在 uart.c
最后的注释中,添加代码。这里只是示例,用其他 uart 或者中断 DMA 等方式都行
int __io_putchar (uint8_t ch) {
HAL_UART_Transmit(&huart2, &ch, 1, 5);
return 1;
}
4.2.2 Keil 操作
还是大概介绍一下友商,也是相同的位置。此外需要在 Output (魔法棒) 中勾选 use MicroLib
int fputc (int ch, FILE *f) {
HAL_UART_Transmit(&huart2, (uint8_t*)&ch, 1,5);
return ch;
}
4.3 C++支持
CLion 对这方面支持比较好,和平时写代码是一样的。友商仅支持 C99 (好像现在也能支持 C11)
4.3.1 自定义文件
新创建的文件在编译时会出现找不到编译规则的警告,应在 CubeMX 中重新生成代码,
C++不应引入过多特性 尤其是 STL 中的很多内容,很容易超内存。下面给出了 cppTest.hpp
/ cppTest.cpp
的示例
文件树:
f1Test
├── Core
│ ├── Inc
│ │ ├── cppTest.hpp
│ │ ...
│ ├── Src
│ │ ├── cppTest.cpp
│ └── ...
├── f1Test.ioc
├── ...
└── STM32F103C8TX_FLASH.ld
cppTest.hpp
头文件:
#ifndef CPPTEST_H
#define CPPTEST_H
// C 头文件开始
#include "stdlib.h"
// C 头文件结束
#ifdef __cplusplus
// C++ 头文件开始
#include "vector"
// C++ 头文件结束
#endif
#ifdef __cplusplus
extern "C"{
#endif
// C 开始
typedef struct structCppFeature {
void *ptr;
} sCppFeature;
int cal (int);
// C 结束
#ifdef __cplusplus
}
#endif
#ifdef __cplusplus
// C++ 开始
class CppFeature {
private:
std::vector<int> base;
public:
void eat (sCppFeature food);
};
// C++ 结束
#endif
#endif //CPPTEST_H
cppTest.cpp
源文件:
#include "../Inc/cppTest.hpp"
void CppFeature::eat (sCppFeature food) {
CppFeature *raw = static_cast<CppFeature*>(food.ptr);
}
int cal (const int a) {
int b = 0;
int c[]{0, 0, a};
for (auto element : c) {
b += a;
}
return b;
}
4.3.2 ETL库
类似于 STL 库,但更适合嵌入式资源受限的场景,官网
因为是仅头文件库,下载包后解压即可使用,在 CMakeLists_template.txt
中添加语句来引入
# project settings
project(${projectName} C CXX ASM)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_STANDARD 11)
include_directories(xxx/etl-20.40.0/include)
使用时:
#include "etl/vector.h"